Rotation Matrices

Hi, I havn’t done much with rotations yet, but for a script I am working on at the moment I need to rotate a mesh (not Object Mesh) to be aligned with another.

The mesh I want to rotate is aligned with the x, y, z -axis, but I want to align it with 3 vectors (i, j, k). I have read a bit about rotation matrices and I think that is what I will be needing but how can I construct one with this data?

Thanks in advancehttp://sphotos.ak.fbcdn.net/hphotos-ak-snc6/hs049.snc6/167963_1398965232560_1785174803_728913_6915745_n.jpg

I think you’d be surprised how easy that is :slight_smile:
Let i,j,k be normalised vectors representing local x,y,z then:


m = mathutils.Matrix(i.xyz,j.xyz,k.xyz)
ob.matrix_world = m.resize4x4()

If you want to maintain ob’s original location:


m = mathutils.Matrix(i.xyz,j.xyz,k.xyz)
ob.matrix_world = mathutils.Matrix.Translation(ob.location) * m.resize4x4()

It seems to depend what type i,j,k really are.
I got this working:


import bpy
import mathutils
from mathutils import *
i = Vector((1,1,1))
j = Vector((1,2,3))
k = Vector((0,0,4))
m =  Matrix((tuple(i.xyz),tuple(j.xyz),tuple(k.xyz))).resize4x4()
print(m)
cube = bpy.context.active_object #default Cube
cube.matrix_world = m

EDIT:
tuple and .xyz may be removed :wink:

This suffices: m = Matrix((i,j,k)).resize4x4() #see extra ( and ) !!

Hi, thanks for the replys. Both these replys work for Mesh Objects, but I don’t see how they work on meshes. My situation is that I have a list of vertices and I want to rotate all of them by an unknown amount to align the mesh formed by these vertices to another mesh. I already have the three vectors(i, j, and k) which represent the rotation of the vertices relative to their object. And I know the new vertices(the once I wont to rotate) are presently aligned to their objects local axies.

So, I wont to change the rotation of the mesh without affecting the rotation of the object. Dose that make sense or am I being confusing?

Ok, that makes perfect sense. Suppose we have object ob of which we want to rotate the mesh data with matrix m:

me = ob.data
for v in me.vertices:
    v.co = m * v.co

Note that this will rotate the verts around the mesh origin.

Ah, I see, Thanks

Latest Blender you MUST reverse the matrix-vector product into vector*matrix!

“Latest Blender you MUST reverse the matrix-vector product into vector*matrix!”

PKHG you just saved my life! I couldn’t make something work and then a bell went off in the back of my head to this thread. Sometimes it’s easy to forget that 1x3 * 3x3 is ok but 3x3 * 1x3 is not!

that would be basic matrix math

you 3X 3 times 1X3 cannot be done or exist in math !

as long as you read the right way like 1 X 3 is 1 row X 1 col

happy 2.5

Whose bright idea was that? It is the near-universal mathematical convention that ordinary coordinate vectors are columns and matrices act on them from the left. Violating this expectation is very bad API design - especially when it was correct before!

Best wishes,
Matthew