Page MenuHome

Object.ray_cast (and all RNA functions using "ob->runtime") broken in Blender 2.80
Closed, ResolvedPublic


System Information
Operating system and graphics card
macOS X Mojave with GTX 980

Blender Version
Broken: 2.80. 59f0db430a9
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

Event Timeline

This comment was removed by Kofi Garbrah (kgarbrah).
Philipp Oeser (lichtwerk) lowered the priority of this task from 90 to 50.

Confirmed, will have a look...

Hm, seems like since the removal of DerrivedMesh from RNA (rB79615c5adb46) there are a couple of issues.
Checked rna_object_api() and all RNA functions using ob->runtime fail.


  • 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[0].dm_info("SOURCE") is OK (uses ob->data directly)
  • bpy.context.selected_objects[0].dm_info("DEFORM") fails (returns empty string)
  • bpy.context.selected_objects[0].dm_info("FINAL") fails (returns empty string)

@Bastien Montagne (mont29): any ideas here (assigning to you first)?
or maybe @Brecht Van Lommel (brecht) can comment?

@Kofi Garbrah (kgarbrah): 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 =

    mesh = ob.to_mesh(bpy.context.depsgraph, True)

    bvhtree = BVHTree.FromBMesh(bm)
    return bvhtree

mesh = None
for obj in bpy.context.selected_objects:
    if obj.type == "MESH":
        mesh = obj

if mesh == None:
    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(
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 )
Philipp Oeser (lichtwerk) renamed this task from Object.ray_cast broken in Blender 2.80 to Object.ray_cast (and all RNA functions using "ob->runtime") broken in Blender 2.80.Nov 16 2018, 12:21 PM

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 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(, None)
        if obj_eval is not None:
            print(obj_eval.ray_cast(start_ray, dir_ray))

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.

Kofi Garbrah (kgarbrah) changed the task status from Unknown Status to Resolved.EditedNov 16 2018, 3:05 PM

Thanks for the workarounds! I've marked this as resolved.

Bastien Montagne (mont29) changed the task status from Resolved to Unknown Status.Dec 26 2018, 5:32 PM

Somehow missed @Brecht Van Lommel (brecht)'s comment, indeed think that would be a nice addition, will do that now.

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.