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’)
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
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.
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).