## Closest equivalent to Unity's "LookAt" rotation in

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

### Closest equivalent to Unity's "LookAt" rotation in

Hello all, i'm trying to rotate some vertices in a mesh about a 3D cursor while preserving the up vector.

If I were in Unity I'd use the "LookAt()" function by specifying what I want the object to look at and also specifying the 'worldUp' vector to make the rotation robust for all rotations:

---
from http://docs.unity3d.com/Documentation/S ... ookAt.html

"function LookAt (worldPosition : Vector3, worldUp : Vector3 = Vector3.up) :
Rotates the transform so the forward vector points at worldPosition. Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. worldUp is only a hint vector. The up vector of the rotation will only match the worldUp vector if the forward direction is perpendicular to worldUp. "
---

What would be the closest Blender equivalent to doing "LookAt" rotation?

Thanks for any help! Lost on this one!!

Dan,

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany
isn't that a rotation about the up-axis? you can simply calculate the angle difference between the target vector (looks at target) and the forward vector, then rotate around z-axis by that angle. If the rotation axis isn't at the same location as the up-axis, then do as follows:

Code: Select all

``````mat = center * rotation_mat * center.inverted()
me.transform(mat)
me.update()``````
center needs to be a 4x4 translation matrix. You can use the location of the object, the median point, the center of mass or whatever you want - depends on what you desire.

You can construct a translation matrix like
Matrix.Translation(vector)

and a rotation matrix like

axis can either be a char ('X', 'Y' or 'Z') or a vector.
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm
Hi CoDEmanX, thanks for taking the time and the trouble...

Intersting approach... didn't know you could multiply quaternions like that...

Will research this and try it out! Thanks again!

Dan,

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany
it's matrices, not quaternions. Not sure how to do the same with quats, but it would probably work as well.
I'm sitting, waiting, wishing, building Blender in superstition...