matrix3dmayapymelmaya-api

Getting rotation from matrix, OpenMaya


I'm working with OpenMaya and I need to get the Euler rotation from a matrix but i'm getting an error while I launch this simple code:

code:

import maya.OpenMaya as OpenMaya
import maya.cmds as mc
crcl=mc.circle()
matrx=(OpenMaya.MMatrix(mc.xform(crcl,q=1,m=1)))
tMat = OpenMaya.MTransformationMatrix(matrx)
eulerRotation = tMat.rotation(asQuaternion=False)

error

Error: in method 'new_MMatrix', argument 1 of type 'float const [4][4]'

the problem is the OpenMaya.MTransformationMatrix(matrx), looks like is not accepting my matrix. Any help?

MTransformationMatrix documentation

I'm sure is a simple fix that I'm missing


Solution

  • One way to get the matrix is to avoid going through the MEL command and use the OpenMaya API directly.

    import math
    # I'm Using `maya.api.OpenMaya` here since I'm testing on Maya 2019.
    # Change according to your version.
    import maya.api.OpenMaya as OpenMaya
    import maya.cmds as mc
    
    crcl = mc.circle()
    mc.rotate(45.0, 10.0, 5.0, crcl)
    
    # Add the circle to the selection so we can retrieve its `MObject`.
    selection = OpenMaya.MSelectionList()
    selection.add(crcl[0])
    
    # Get the circle `MObject` and create a `MFnDagNode` from it.
    obj = selection.getDependNode(0)
    dagNode = OpenMaya.MFnDagNode(obj)
    
    matrix = dagNode.transformationMatrix()
    tMat = OpenMaya.MTransformationMatrix(matrix)
    eulerRotation = tMat.rotation(asQuaternion=False)
    
    # This should print the following (only differences would potentially be in
    # floating point accuracy):
    # [45.0, 10.0, 5.0]
    print(map(math.degrees, eulerRotation))