 # How Maya stores rotation in 4x4 Matrix

I have a question about how Maya stores rotate results in 4x4 matrix. To be more clear I will provide example.
Let’s say I have locator. And I will rotate it in Z axis by 45 degrees. To calculate this values I must use cos(45) and sin(45)
which in this case will be [0.707,0.707] for X axis and [-0.707, 0.707] for Y axis.
This values represents the 2 dimensional vectors for each axis. If we create 2 transform nodes and assign this values to X and Y
translate channels we will get 2 points in 3D space matching with locator’s X and Y vectors.
Now, my confusion starts when I rotate the locator in second axis, let’s say in Y by 45 degrees. The results in matrix are:

Note: my rotate order is default = XYZ and scale is 1.

X1=0.500, X2=0.500, X3=-0.707

Y1=-0.707, Y2=0.707, Y3=0.000

Z1=0.500, Z2=0.500, Z3=0.707

And again if use this 3 vectors as translate.xyz of the locator we will get 3 points in 3D space
which matches with cubes “sides” in all 3 Axis.

So, how Maya calculates this values? What happens when third axis enters in the game? I guess it is due to rotate order but how?

A 4x4 matrix encodes a 3x3 rotation matrix, a position, and an extra column of values that used to distinguish between the position and rotation parts (you can think of that fourth column as a binary “am I a translation row” value)

## recap

(this is pretty much what you posted)

The rotation matrix part encodes the 3 base coordinate axes for the transform of the matrix: In an unrotated matrix they are not rotated and they line up with the world:

``````   1    0     0    =  x
0    1     0    =  y
0    0     1    =  z
``````

At any given point these numbers change to be the local X, Y and Z vectors (technically, they also encode scale if they are not normalized… lets ignore that for now)

If you were rotating on only one axis, you’d only change he other two rows. Here for example is a 45 degree rotation in Z only:

``````  0.707       0.707   0
-0.707      0.707    0
0       1     0
``````

You can see that the X axis `1,0,0` is now rotated around to `0.707, 0.707, 0` and the Y axis is rotated similarly from `0,1,0` to `-0.707 , 0.707 , 0` but the Z axis is not changed.

## Euler rotations

You can represent any single-axis Euler rotation in the form above: a 3x3 matrix with the rotating axis left alone and the other two representing the ‘twist’ around that axis. Rotation order comes in because to represent a 3-d Euler rotation you have to multiply together 3 matrices, one for each of the axes – and in matrix math, multiplication is order dependent.

So if you had the 45-degrees-in-X matrix above and mutiplied it by a 30-degrees-in-Y matrrix
you’d get something like this (i’m using maya.api.OpenMaya MMatrices, you could do this in the listener)

``````   # numbers are approximated!
from maya.api.OpenMaya import MMatrix

# 45 degrees in Z
z_rot = MMatrix ([
0.707,  0.707, 0, 0,
-0.707, 0.707, 0, 0 ,
0, 0,  1, 0,
0, 0, 0, 1
])

# 30 degrees in Y
y_rot  = ([
0.866,  0, 0.5, 0
0 ,  1, 0, 0,
-0.5, 0 , 0.866, 0,
0, 0, 0, 1
])

yz_matrix = y_rot * z_rot
zy_matrix = z_rot * y_rot

print (yz_matrix)
#  MMatrix([0.612, 0.707, 0.354, 0, -0.612, 0.707, -0.354, -0.5, 0, 0.866, 0, 0,0, 0, 1])

print (zy_matrix)
#  MMatrix([0.612, 0.707, 0.354, 0, -707, 0.707, 0, -0.354, -0.354, 0.866, 0, 0,0, 0, 1])
``````

As you can see the pieces are the same but the rotation order changes their arrangement, which makes sense since the maya results also look different for the same rotations applied using different orders

So, under the hood maya multiples 3 euler rotation matrices representing the X, Y and Z rotations you specified. The order of the rotations is variable, and affects the final outcome. The final matrix that comes out of that becomes the 3x3 rotation matrix in the upper-left corner of the final Maya transform.

The details are all laid out here in the Maya docs along with the other matrices (for scale, sheer, pivot transform and so on) that all get multiplied together to make the final matrix you see when you ask Maya for the transform of an object

2 Likes

Thank you for such explicit explanation!

A transform is made up of 3 unit vectors and a 4th offset vector where the first three vectors are the rotation component and the last is the positional offset. Each vector of the first three defines an axis - otherwise known as a unit vector as the length of the vector determines its scale along that axis. The row length is 4 with the last element being 1.0 because it needs to be square to compute the inverse - for thing like matrix decomposition, multiplication etc…

Euler rotations essentially decompose a transforms rotation parent against a coordinate basis - they are essentially 2d projections. https://en.wikipedia.org/wiki/Euler_angles

2 Likes