Nov 05

I have talked to a few people that think there are no good ways to deal with Vectors or Matrices in Python. I wanted to present an example of how to use the Maya API to create an MMatrix class and introduce how the MScriptUtil class works to deal with API functions that use pointers.

# import mayas general API classes
import maya.OpenMaya as OpenMaya

# the MScriptUtil class is very important to python API users
# it allows python users to pass variable to an API function like it were a pointer

# Called by RTrunPointerExample()
def RTpointerExample( ptr ):

	print 'ptr changed using MScriptUtil';
	OpenMaya.MScriptUtil.setDouble ( ptr, 2.0 )

def RTrunPointerExample():

	# Define a MScriptUtil object
	fooba = OpenMaya.MScriptUtil()

	# Define the pointer type
	# only use the types that end in Ptr
	ptr = fooba.asDoublePtr()

	# Set a value
	OpenMaya.MScriptUtil.setDouble ( ptr, 1.0 )

	# Print that value
	print 'ptr = ', (OpenMaya.MScriptUtil.getDouble( ptr ))

	# Call a function passing it the pointer
	RTpointerExample( ptr )

	# Print the new value
	print 'ptr = ', (OpenMaya.MScriptUtil.getDouble( ptr ))

# execute this
RTrunPointerExample()
# which prints:
>> ptr =  1.0
>> ptr changed using MScriptUtil
>> ptr =  2.0

The above is a simple example of how pass the variable ‘ptr’ as a pointer to RTpointerExample and to change the variable which in turn changes it in the first function RTrunPointerExample. This is sometimes needed to properly interact with API functions since python has no concept of pointers. My goal is not to describe the pointer concept completely but to just show how you interact with the MScriptUtil class.

So here is an example of using the MScriptUtil class to set MMatrix variables.

# import mayas general API classes
import maya.OpenMaya as OpenMaya

# initialize a matrix using a python list
# this is how a matrix can be initialized using a python list
valListA = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
valListB = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
matrixA = OpenMaya.MMatrix()
matrixB = OpenMaya.MMatrix()

# Create a matrix using the list
OpenMaya.MScriptUtil.createMatrixFromList( valList, matrixA)
OpenMaya.MScriptUtil.createMatrixFromList( valList, matrixB)

# Multiply matrixA * matrixB will return a matrix so you dont need to define it
matrixC = matrixA * matrixB

# print row 1
print matrixC(0, 0)
print matrixC(0, 1)
print matrixC(0, 2)
print matrixC(0, 3)

# print row 2
print matrixC(1, 0)
print matrixC(1, 1)
print matrixC(1, 2)
print matrixC(1, 3)

# print row 3
print matrixC(2, 0)
print matrixC(2, 1)
print matrixC(2, 2)
print matrixC(2, 3)

# print row 4
print matrixC(3, 0)
print matrixC(3, 1)
print matrixC(3, 2)
print matrixC(3, 3)

# Example of setting a certain index of a matrix to a specific value
matrix = OpenMaya.MMatrix()
OpenMaya.MScriptUtil.setDoubleArray(matrix[0], 0, 1.0) # Sets the first row, first column to 1.0
OpenMaya.MScriptUtil.setDoubleArray(matrix[3], 3, 16.0) # Sets the fourth row, fourth column to 16.0

print matrix(0, 0)
print matrix(3, 3)

Hopefully that gives you some ideas of how you might use a MMatrix with one of your python scripts. Note that it is ok to import maya.cmds and also maya.OpenMaya into the same script. Your script does not need to be a plugin to interact with API classes. Once you have a MMatrix variable you can multiple those variables together easily just like you would be able to do if you were using the MMatrix class in C++.

Note that if you want more information on the MMatrix class you can look it up in the Maya help documents.

-RyanT

4 Responses to “Matrices in Maya Python”

  1. GDJared Says:

    Спасибо!

  2. humam Says:

    Respect

  3. LnddMiles Says:

    The best information i have found exactly here. Keep going Thank you

  4. MaX Says:

    Nice man, thanks alot, you rock :) , thanks again.

Leave a Reply