Object.ray_cast (and all RNA functions using "ob->runtime") broken in Blender 2.80 #57861
Operating system and graphics card
macOS X Mojave with GTX 980
Worked: no solution
Short description of error
Object.ray_cast is broken in Blender 2.80.
I have attached a blend that contains a simple ray_cast that works in 2.79 and fails in 2.80. Run blender from a console to see the error messages.
Exact steps for others to reproduce the error
Click Run Script to see the result of ray_cast in Blender 2.80
blender/blender-addons#58317 was marked as duplicate of this issue
blender/blender-addons#59790 was marked as duplicate of this issue
Hm, seems like since the removal of DerrivedMesh from RNA (
79615c5adb) there are a couple of issues.
rna_object_api() and all RNA functions using
- fails (says "Object 'bla' has no mesh data to be used for ray casting")
- fails (says "Object 'bla' has no mesh data to be used for finding nearest point")
- bpy.context.selected_objects- .dm_info("SOURCE") is OK (uses
- bpy.context.selected_objects- .dm_info("DEFORM") fails (returns empty string)
- bpy.context.selected_objects- .dm_info("FINAL") fails (returns empty string)
@KofiGarbrah: As a (temporary) workaround for this report you can create a BVH manually and raycast that:
from mathutils import Vector from mathutils import Matrix from math import * from mathutils.bvhtree import BVHTree def bvhtree_from_object(ob): import bmesh bm = bmesh.new() mesh = ob.to_mesh(bpy.context.depsgraph, True) bm.from_mesh(mesh) bm.transform(ob.matrix_world) bvhtree = BVHTree.FromBMesh(bm) bpy.data.meshes.remove(mesh) return bvhtree mesh = None for obj in bpy.context.selected_objects: if obj.type == "MESH": mesh = obj if mesh == None: bpy.ops.mesh.primitive_cube_add(location=(0,0,0)) for obj in bpy.context.selected_objects: if obj.type == "MESH": mesh = obj startRay = Vector((-2,-2,-2)) endRay = Vector((2,2,2)) p = endRay - startRay distance = sqrt(p.dot(p)) direction = p / distance print( "start", startRay, "end", endRay, "distance", distance, "direction", direction ) bvhtree = bvhtree_from_object(mesh) ray = bvhtree.ray_cast( startRay, direction, distance ) print( "Hits:", ray )
Imho there is no bug here… that has more to see with how blender/depsgraph/CoW work now in 2.8. Original data (from Main, of
bpy.data in python) is not supposed to have evaluated stuff, although we do 'backport' a few things for objects…
You should use context's despgraph object, something like:
start_ray = (-2, -2, -2) dir_ray = (1, 0, 0) depsgraph = bpy.context.depsgraph for obj in bpy.context.selected_objects:
if obj.type == "MESH":
obj_eval = depsgraph.objects.get(obj.name, None)
if obj_eval is not None:
I would expect this ray cast function to get a depsgraph parameter, which it will then look up the object in. That will at least make it more clear to users what is missing.
Thanks for the workarounds! I've marked this as resolved.
Somehow missed @brecht's comment, indeed think that would be a nice addition, will do that now.
This issue was referenced by
We can even use depsgraph from context, in fact, should work perfect for 99% of usecases ;)
Just tested it with the daily build (blender-2.80-e4dbfe0a98c1-linux-glibc224-x86_64) and repeatedly ray casting on the bvh now works but if I invoke the operator again (and create a new bvh from the same object) I get a segmentation fault. I'll create a separate bug report that references this one just in case this is not related.
Deleting a branch is permanent. Although the deleted branch may exist for a short time before cleaning up, in most cases it CANNOT be undone. Continue?