5

I've been trying to use blender for "line of sight" type tests. For this, I want to use the ray_cast function (the raycasting function not in the blender game engine). No matter what I try I can't get the code to work at all.

(assume I have 2 objects, a and b)

When I attempt to ray_cast from a to b, I can only get the correct answer if it is the first time I've used that function since opening blender, and if at least one of either the start or end locations is on the origin. Subsequent ray_casts done after moving one object do not change the result (as in, I get the same result as the first time) even after manually updating the scene. If I try to cast without either the start or end point being on the origin, then the returns null (Vector<0,0,0>, Vector<0,0,0>, -1)

I've noticed that similar things happen with other render functions, such as Object.closest_point_on_mesh, etc... can anyone help me? below is the code I use for rayCasting.

#

import bpy

def main():

a = bpy.data.objects['a']
b = bpy.data.objects['b']

x = a.ray_cast(a.location,b.location)
print(x[0])

main()

#

1
  • You should ask on blender.stackexchange.com
    – neomonkeus
    Jun 20, 2013 at 15:55

2 Answers 2

3

Well, the ray_cast function expects both start and end coordinates to be in the LOCAL coordinate system of the object in object.ray_cast This means your a.location and b.location coordinates, which are global/world coordinates need to be transformed into local coordinates.

This way:

globalcoordinate = Vector((x, y, z))
localcoordinateforobject = (globalcoordinate - object.location) * object.matrix_world.inverted()

Normally one would use matrix_world as above, but this doesnt work for objects that are rotated/scaled without this rotation and scale applied (Ctrl-A). So I share this code, which I use in a lot of my addons:

def adapt(selobj):

    # Rotating / panning / zooming 3D view is handled here.
    # Creates a matrix.
    if selobj.rotation_mode == "AXIS_ANGLE":
        # object rotation_quaternionmode axisangle
        ang, x, y, z =  selobj.rotation_axis_angle
        matrix = Matrix.Rotation(-ang, 4, Vector((x, y, z)))
    elif selobj.rotation_mode == "QUATERNION":
        # object rotation_quaternionmode euler
        w, x, y, z = selobj.rotation_quaternion
        x = -x
        y = -y
        z = -z
        quat = Quaternion([w, x, y, z])
        matrix = quat.to_matrix()
        matrix.resize_4x4()
    else:
        # object rotation_quaternionmode euler
        ax, ay, az = selobj.rotation_euler
        mat_rotX = Matrix.Rotation(-ax, 4, 'X')
        mat_rotY = Matrix.Rotation(-ay, 4, 'Y')
        mat_rotZ = Matrix.Rotation(-az, 4, 'Z')
        if selobj.rotation_mode == "XYZ":
            matrix = mat_rotX * mat_rotY * mat_rotZ
        elif selobj.rotation_mode == "XZY":
            matrix = mat_rotX * mat_rotZ * mat_rotY
        elif selobj.rotation_mode == "YXZ":
            matrix = mat_rotY * mat_rotX * mat_rotZ
        elif selobj.rotation_mode == "YZX":
            matrix = mat_rotY * mat_rotZ * mat_rotX
        elif selobj.rotation_mode == "ZXY":
            matrix = mat_rotZ * mat_rotX * mat_rotY
        elif selobj.rotation_mode == "ZYX":
            matrix = mat_rotZ * mat_rotY * mat_rotX
    # handle object scaling
    sx, sy, sz = selobj.scale
    mat_scX = Matrix.Scale(sx, 4, Vector([1, 0, 0]))
    mat_scY = Matrix.Scale(sy, 4, Vector([0, 1, 0]))
    mat_scZ = Matrix.Scale(sz, 4, Vector([0, 0, 1]))
    matrix = mat_scX * mat_scY * mat_scZ * matrix

    return matrix

It returns the right transformation matrix called "matrix" for the object "selobj". Use it instead of the object.matrix_world in the code example above.

1
  • It works really well! In Blender 2.8 you need to replace the * by @ and Matrix.Rotation by Matrix().Rotation. I also imported from mathutils import Matrix Aug 8, 2020 at 7:36
2

this should solve your problem:

def main():
a = bpy.data.objects['a']
b = bpy.data.objects['b']


localA = a.matrix_world.inverted() * a.location
localB = a.matrix_world.inverted() * b.location

print(localA, localB)

(location, normal, index) = a.ray_cast(localA, localB)

print(location, normal, index)

# reselect the originally selected face
if(index > 0):
    a.data.polygons[index].select = True

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.