1
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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 <Vector (1.0000, 1.0000, 1.0000)> <Vector (0.0000, 1.0000, 0.0000)> 5

You might need to delete the object after your operation

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.