Python API Plugin - rt4×4MatrixToTRS Python urllib2 library module
Feb 03

I found this out the hard way while working on my 4×4MatrixToTRS plugin. So I thought it would be handy to pass along. There are a few bugs with the MTransformationMatrix class when using Python. You can get around them, you just need to know about them to do so. The main bug seems to be that any function of this class that asks for RotationOrder &order will not work with Python. It will always throws an error.

So if you are attempting to get the Euler rotation in a certain order like I was then you need to find a work around. What I found was I just need to use the MTransformationMatrix.rotation() function to get the quaternion then I could use the MQuaternion.asEulerRotation() function and from there I could use the MEulerRotation functions to get what I needed. Here are two examples one that does not work that should and one that does work. I have let Autodesk know about this issue.

If you want to get the rotation value of the MTransformationMatrix class this is the wrong way to do it:

import maya.OpenMaya as OpenMaya

getMatrix = OpenMaya.MMatrix()
matrixList = ( 0.673, 1.545 , 0.0, 0.0,
    -0.327, 2.545, 0.0, 0.0,
    -0.327, 1.545, 1.0, 0.0,
    -0.327, 1.545, 0.0, 1.0)

OpenMaya.MScriptUtil().createMatrixFromList(matrixList, getMatrix)

mTM = OpenMaya.MTransformationMatrix( getMatrix )

rotDoubleArray = OpenMaya.MScriptUtil()
rotDoubleArray.createFromList( [0.0, 0.0, 0.0], 3 )
rotDoubleArrayPtr = rotDoubleArray.asDoublePtr()
rotOrder = OpenMaya.MTransformationMatrix().kXYZ

mTM.getRotation( rotDoubleArrayPtr, rotOrder )

# Error: Wrong number of arguments for overloaded function 'MTransformationMatrix_getRotation'.
  Possible C/C++ prototypes are:
    getRotation(double [3],MTransformationMatrix::RotationOrder &)
    getRotation(double [3],MTransformationMatrix::RotationOrder &,MSpace::Space)
# Traceback (most recent call last):
#   File "", line 1, in 
#   File "C:\buildforge\Maya_Main_Win32_Build\build\wrk\optim\runTime\Python\Lib\site-packages\maya\OpenMaya.py", line 8044, in getRotation
# NotImplementedError: Wrong number of arguments for overloaded function 'MTransformationMatrix_getRotation'.
#   Possible C/C++ prototypes are:
#     getRotation(double [3],MTransformationMatrix::RotationOrder &)
#     getRotation(double [3],MTransformationMatrix::RotationOrder &,MSpace::Space)
#  #

Here is the right way:

import maya.OpenMaya as OpenMaya

getMatrix = OpenMaya.MMatrix()
matrixList = ( 0.673, 1.545 , 0.0, 0.0,
    -0.327, 2.545, 0.0, 0.0,
    -0.327, 1.545, 1.0, 0.0,
    -0.327, 1.545, 0.0, 1.0)

OpenMaya.MScriptUtil().createMatrixFromList(matrixList, getMatrix)

mTM = OpenMaya.MTransformationMatrix( getMatrix )
rotOrder = OpenMaya.MTransformationMatrix().kXYZ

# Get the rotation as a quaternion then convert to Euler as needed
rotateOrder = 0
mquat = mTM.rotation()
rot = mquat.asEulerRotation()
rot.reorderIt( rotateOrder )

print rot.x, rot.y, rot.z

Happy coding,
-RyanT

One Response to “Python API MTransformationMatrix.getRotation() bug”

  1. alan Says:

    hi ryan,
    this is not just a problem of python, but also the c++ class has some bug. my advice for all, it’s not use this class but write a new one. You can see teh ogre3e source code for matrix decomposition algorithm.

    bye
    Alan

Leave a Reply