好吧,ray_cast 函数期望开始和结束坐标都在 object.ray_cast 中对象的 LOCAL 坐标系中,这意味着您的 a.location 和 b.location 坐标,它们是全局/世界坐标,需要转换为局部坐标.
这边走:
globalcoordinate = Vector((x, y, z))
localcoordinateforobject = (globalcoordinate - object.location) * object.matrix_world.inverted()
通常人们会像上面那样使用matrix_world,但这不适用于在没有应用这种旋转和缩放(Ctrl-A)的情况下旋转/缩放的对象。所以我分享了这段代码,我在很多插件中都使用了它:
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
它为对象“selobj”返回称为“matrix”的正确变换矩阵。使用它代替上面代码示例中的 object.matrix_world。