Rotate an object toward a point in space?


I’m working on a procedural leg animation, and need to rotate a leg to face the hit position of a ray.

The problem is, I need preform the rotation, set a constraint, and rotate it back, in one iteration of code.

alignAxisToVect() takes at least 1 frame to complete; and I need a method that does it instantly (right when the code is run), before the constraint is set.

Any ideas?

So I think, if I could just
leg.worldOrientation = vector,
that might solve my dillema.

How doth one convert a vector to a worldOrientation matrix?

You can get the rotational difference between the angles as a rot, and apply this to a copy of the matrix


Does this look right?

def aim(object, target):
    diff = object.getAxisVect([0,0,1]).rotation_difference(object.getVectTo(target)[2])
    object.worldOrientation = diff

It doesn’t raise any errors, but ‘object’ spasms out.

Rot = object.worldOrientation @ diff
(in 0.3.0)

Rot = object.worldOrientation * diff
<0.2.5 >

object.worldOrientation = Rot
1 Like

Got an error for 0.3.0:
TypeError: Matrix multiplication: not supported between 'Matrix' and 'Quaternion' types

Is there a way to convert a quaternion to a matrix?

Thanks so much! Got it working.

def aim(object, target):
    diff = object.worldOrientation.col[1].rotation_difference(object.getVectTo(target)[1])
    object.worldOrientation = object.worldOrientation @ diff.to_matrix()

One last issue though; would there be a way to specify which axis to align?

object.worldOrientation.col[0] #x axis

object.worldOrientation.col[1] #this is comparing Y axis

object.worldOrientation.col[2] #Z

I tried all the list indices for object.worldOrientation.col and the only one that seems to work is col[1]… The rest just make the object spin wildly.
I’ve tried experimenting with some of the other indices, but have yet to find a working combination.

So I’ve isolated the issue (sort of); it seems that at certain angles, the aim() function - on any axis - becomes unstable and makes the object spasm into random rotations. Still not sure what causes it, though.

Here’s a demo of what’s going on, if it helps any:
look-toward-object-issue.blend (742.8 KB)

Solved the issue!

Turns out the orientation needs to be reset before making the rotation. If the object was already pointing one way, trying to run the function again is what results in the spasm.

def aim(object, target, reference_obj, axis, factor):
    object.worldOrientation = reference_obj.worldOrientation
    diff = object.worldOrientation.col[axis].rotation_difference(object.getVectTo(target)[1] * factor)
    object.worldOrientation = object.worldOrientation @ diff.to_matrix()

reference_obj is the orientation to match before aligning
axis is the axis to align
factor can be set to -1 to invert the axis

Fixed example:
look-toward-object-issue-FIX.blend (763.0 KB)

Currently working on setting/locking the rotation of whichever axis is being aligned (for example, if Z is being aligned, it doesn’t have to rotate; only X and Y need to be adjusted).