Getting the matrix data in MMatrix

2,977 views
Skip to first unread message

Ravi Jagannadhan

unread,
Aug 14, 2008, 9:36:44 PM8/14/08
to python_in...@googlegroups.com
Hi all, I'm trying to access and change the individual elements of the
matrix elements in an MMatrix object. The only problem is, I can't
seem to access these elements. I thought something like this would be
a good start:

import maya.OpenMaya as om
moe = om.MMatrix()
print moe.matrix

I get this: _68ccb214_p_a_4__double

which makes sense since the 'matrix' member variable is a double array
of 4x4. Fine, then how would I access this?

Thank you for your time,
Ravi
--
Where we have strong emotions, we're liable to fool ourselves - Carl Sagan

Matthew Chapman

unread,
Aug 14, 2008, 10:59:39 PM8/14/08
to python_in...@googlegroups.com
the MMatrix:: operator[] works well enough to print.

moe = om.MMatrix()
for a in range(3):
      for b in range(3):
            print moe[a][b]

I wont even bother with MMatrix:: get ( double dest[4][4] ) or MMatrix:: MMatrix (const double src_matrix[4][4] )
they both would require some kind of miracle with getting MScriptUtils to not crash maya. As far as I can find there is
no way to directly manipulate a MMatrix with Python API. I really hope I am wrong about this. You could try building
your matrix using MTransformationMatrix and then calling as matrix. I am often saddened by the lack of good thought
out support for python api.

I hope someone knows a better way to use MMatrix in python api.

Chad Vernon

unread,
Aug 15, 2008, 12:25:10 AM8/15/08
to python_in...@googlegroups.com
You can use code like this:

def setRow(matrix, newVector, row):
    OpenMaya.MScriptUtil.setDoubleArray( matrix[row], 0, newVector.x )
    OpenMaya.MScriptUtil.setDoubleArray( matrix[row], 1, newVector.y )
    OpenMaya.MScriptUtil.setDoubleArray( matrix[row], 2, newVector.z )
 
def setCell(matrix, value, row, column):
    OpenMaya.MScriptUtil.setDoubleArray( matrix[row], column, value )

def printMatrix(matrix):
    result = '% .06f, % .06f, % .06f, % .06f,\n% .06f, % .06f, % .06f, % .06f,\n% .06f, % .06f, % .06f, % .06f,\n% .06f, % .06f, % .06f, % .06f,\n'
    print result % (matrix(0, 0), matrix(0, 1), matrix(0, 2), matrix(0, 3), matrix(1, 0), matrix(1, 1), matrix(1, 2), matrix(1, 3), matrix(2, 0), matrix(2, 1), matrix(2, 2 ) matrix(2, 3), matrix(3, 0), matrix(3, 1), matrix(3, 2), matrix(3, 3))


-Chad

Matthew Chapman

unread,
Aug 15, 2008, 1:10:09 AM8/15/08
to python_in...@googlegroups.com
That is great! I have never seen any MScriptUtils implementations. I havn't seen much info on uses of MScriptUtils
and I have had a lot of crashes when working with its pointers. If there is good forums or info on it i would love it
if you could pass it on I don't feel like I got enough out of maya's docs.



Olivier Renouard

unread,
Aug 24, 2008, 1:29:45 PM8/24/08
to python_in...@googlegroups.com
Hi,

You can also do this :

import maya.OpenMaya as api
m = api.MMatrix()
print tuple(tuple(api.MScriptUtil.getDoubleArrayItem ( m[r], c) for c in xrange(4)) for r in xrange(4))
# ((1.0, 0.0, 0.0, 0.0), (0.0, 1.0, 0.0, 0.0), (0.0, 0.0, 1.0, 0.0), (0.0, 0.0, 0.0, 1.0))

All the trouble with MScriptUtil (and the pitfall I fell in when first trying them) is to remember it doesn't allocate memory for you, when you pass it a pointer it has to have been initialised first (was a related thread in this group a coupe of months ago). Still its a quite ugly thing nonetheless.

For the adventurous, latest pymel in SVN trunk has the MVector, MPoint, MColor and MMatrix wrap working (still some work on fine tuning MTransformationMatrix, MQuaternion, MEulerRotation, though, essentially deciding how to organise them).

Check the tests et the end of wrappedtypes.py, they give an idea of what is doable : MVector and MMatrix are rewrapped from the api as classes derived both from their OpenMaya equivalent (means the api methods will accept them directly) and from generic Vector and Matrix classes (means you can also use n-sized matrices and vectors, and inherit some more useful methods from these too).

Matrix and Vector are a very much numpy like Python implementation (check help on these), so that it would be easy to actually also derive them from numpy array / matrix classes if people feel they want that. I'd be happy to get feedback on these.

Olivier
-- 
Olivier Renouard
Reply all
Reply to author
Forward
0 new messages