从矩阵获取旋转,OpenMaya

Getting rotation from matrix, OpenMaya

我正在使用 OpenMaya,我需要从矩阵中获取欧拉旋转,但我在启动这个简单代码时遇到错误:

代码:

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: in method 'new_MMatrix', argument 1 of type 'float const [4][4]'

问题是 OpenMaya.MTransformationMatrix(matrx),看起来不接受我的矩阵。 有帮助吗?

MTransformationMatrix documentation

我确定我缺少一个简单的修复程序

获取矩阵的一种方法是避免通过 MEL 命令并直接使用 OpenMaya API。

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))