Object has no mesh data to be used for ray casting

When I try to make a ray_cast in object mode, I get an error “Object has no mesh data to be used for ray casting”
I tried to fix it by following the steps:
bpy.context.scene.update()
context.active_object.data.update()
context.active_object.data.update(calc_edges=False)
context.active_object.update_tag(refresh={‘OBJECT’, ‘DATA’, ‘TIME’})

And even:
bpy.ops.object.mode_set(mode=‘EDIT’)
bpy.ops.object.mode_set(mode=‘OBJECT’)

Code

Line 208

Thanks

I was having this problem, and could get around by creating a bmesh out of the object and then using BVHTree.FromBMesh(bm)
and using the raycast function in the bvhtree

I don’t use bmesh and bmhree it’s too slow

It was not working directly from the mesh, I had to intermediate with a bmesh. but from my tests, it only adds the extra bmesh generation time but the resulting tree is still fast. So dont generate many of them.

I will have no more than 10 rays, well, even 50, it is not much time. I spend more on generating bmesh and BVHTree.

Maybe this is a bug and it’s worth letting developers know?

In Blender 2.8 the object must be evaluated by the depsgraph first:

obj_eval = depsgraph.objects.get(target_obj.name, None)  
if obj_eval:
    success, hit, normal, index = obj_eval.ray_cast(ori, ray_dir)

This was a mix of half-finished feature/bug, should be fixed now (see https://developer.blender.org/rB61cb1a81a84c8a10c5abfb86011de9ea4bb2aa3d and linked report for details).

Now in most cases you can just pass regular object, and RNA function will get the evaluated one from current context’s depsgraph automatically (in other words: old 2.7x code should work just as expected in 2.8 now).

1 Like