How do you read individual elements in an MMatrix object in Maya Python? This is a problem that Ryan and I ran into about a year or so ago. Ryan brought this to the attention of an Autodesk developer at the time, who must have been misinformed and told us that it should work like C++. However, the following does not work:
# Create an MMatrix and print its first index
import maya.OpenMaya as OM
mat = OM.MMatrix()
print mat.matrix[0][0]
I was at Autodesk’s Maya API training last week during the GDC, and one of the trainers had run into this problem again. Unfortunately, she had also been misinformed by one of the developers as to how this should work. She was told to do the following:
# Create an MMatrix and print its first index
import maya.OpenMaya as OM
mat = OM.MMatrix()
print mat.matrix(0,0)
I assume that if people at Autodesk are still misinformed about this, there is a good chance most customers are too, so it probably bears mentioning here. Although the matrix member of an MMatrix object is the pointer to where the data are actually stored, Maya Python’s implementation does not follow the C++ paradigm. The correct way to read these elements out is therefore:
# Create an MMatrix and print its first index
import maya.OpenMaya as OM
mat = OM.MMatrix()
print mat(0,0)
To set these data, however, you must of course use the MScriptUtil class. Ryan did a good post about this on his blog, as did Rob Pringle on highend3d. I hope this saves somebody out there some trouble!
Tags: API