Error: Object 'Sphere.022' has no mesh data to be used for ray casting I am getting above error when trying to obj.ray_cast() from a copy of an object:

obj = bpy.data.scenes[0].objects["Sphere"].copy()
is_hit, hit_loc, hit_normal, index = obj.ray_cast(localA, localB)


I figured out this was due to objects being linked. Making object data single user solved this issue. However, I need to keep it linked. Is there a way to keep objects in linked state and still use the ray_cast() function?
You need to link the object to the scene and update it for ray casting to work.

import bpy, mathutils

obj = bpy.data.scenes[0].objects["Cube"].copy()
# Insert your new object in the scene
bpy.context.scene.objects.link(obj)
# Update scene
bpy.context.scene.update()

is_hit, hit_loc, hit_normal, index = obj.ray_cast(mathutils.Vector(), mathutils.Vector([1,1,1]))

print(is_hit, hit_loc, hit_normal, index)


Prints:

True &lt;Vector (1.0000, 1.0000, 1.0000)&gt; &lt;Vector (0.0000, 1.0000, 0.0000)&gt; 5


You might need to delete the object after your operation

bpy.context.scene.objects.unlink(obj)
bpy.data.objects.remove(obj)
del obj