1

I am trying to understand the basic math of a 4x4 matrix in Maya 3d software, and I can't seem to find anything specific enough to my scenario that I can understand.

I basically have an object with a matrix like this:

1 0 0 0
0 1 0 0
0 0 1 0 
0 0 0 1

I know that the buttom row represents translations, and the 1 in each row is the scale value.

But... if I rotate the object in X by 30*, then I get a matrix like this:

1  0      0    0
0  0.8    0.5  0
0  -0.5   0.8  0
0  0      0    1

Firstly, how would I go about mathematically calculating the rotation x value from knowing only the matrix?

Secondly, how would I go about calculating the matrix value based on only knowing rotations, translations and scale of a 3d object?

Bjørn
  • 71
  • 1
  • 12
  • 2
    see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) – Spektre Feb 05 '20 at 08:42
  • That's a complicated read. There is one equation which is: ``` rotation_matrix=rotation_around_x(ax)*rotation_around_y(ay)*rotation_around_z(az); ``` But I read it as rotation_matrix = rx = 30, ry = 0 , rz = 0 rotation_matrix = (30,0,0) which is basically the euler rotation values, and I already know these. If I read the sub thread there are some calculations, but I am a bit lost with it. Do I use sin(rx), sin(ry) and sin(rz) on my euler angles and then apply that result somewhere else to calculate the new matrix values? – Bjørn Feb 06 '20 at 09:02
  • no each rotation around axis has its own matrix (in which some elements are the `sin,cos` of your angle indeed) and you basically multiply the matrix to your objects one in desired order. See [Basic rotations](https://en.wikipedia.org/wiki/Rotation_matrix)... Each operation has its own matrix which you just multiply to your objects matrix (that usually starts with unit matrix) – Spektre Feb 06 '20 at 09:06
  • So if you want to extract euler angles from matrix see [Is there a way to calculate 3D rotation on X and Y axis from a 4x4 matrix](https://stackoverflow.com/a/56950130/2521214) which basically first detects the multiplication order from known set of angles and resulting matrix and then simply compute the angles from any matrix (created by the same transformation order). Translation and scale extraction is easy (size of basis vectors and origin vector is the translation directly) .... – Spektre Feb 06 '20 at 09:35
  • Does the ex = 10 [deg], ey = 20 [deg], ez = 30 [deg] represent euler rotations? In step 3, I can't seem to figure out where those resulting numbers come from. If I do sin of ex '10' I don't get 0.173648 as in the example, and I don't get that result from any of the combined values of the x row of the matrix in the example. Does M[8] stand for the eight value of the Matrix list of values? I don't quite understand if I need this calculation at all or if it's something else I am looking for. – Bjørn Feb 07 '20 at 10:53
  • omg ... `sin(10 deg) = sin(10*3.1415926535897932384626433832795/180 rad) = sin(0.17453292519943295769236907684886 rad) = 0.17364817766693034885171662676931` most programing languages and math implementations use `[rad]` radians as angle parameter units ... so if you got `[deg]` degrees you need to convert to radians ... but there are also degree based implementations where no conversion is required ... so you need to know which one you have. btw that is basic goniometrics math knowledge .... – Spektre Feb 07 '20 at 11:37
  • Hey @Bjørn, have you seen the answer to your question? – ababak Feb 13 '20 at 07:44

1 Answers1

1

As we talk about the Autodesk Maya, we can use the OpenMaya API:

import maya.cmds as cmds
import maya.api.OpenMaya as om

# An object of interest:
object = "pCube1"
# Get the transform matrix as a list of 16 floats
m_list = cmds.xform(object, query=True, matrix=True)
# Create the MMatrix object
m = om.MMatrix(m_list)
# Get the MTransformationMatrix
mt = om.MTransformationMatrix(m)
# Get the rotations
rot = mt.rotation()
# Rotations in radians (as if rotated in the xyz order):
print rot.x, rot.y, rot.z
# Rotations in degrees:
print om.MAngle(rot.x).asDegrees(), om.MAngle(rot.y).asDegrees(), om.MAngle(rot.z).asDegrees()
ababak
  • 1,685
  • 1
  • 11
  • 23