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

Continue reading »

Oct 25

I wanted to give another example that is even closer to how C++ overloading functions work. With python you can create functions that use keywords which are basically predefined variables. The good thing about this is a keyword does not have to have a variable passed to it because it already has a valid value. You can use this in a way to make a function work much like a maya command. For example these three lines are valid:

import maya.cmds as cmds

cmds.polySphere ( r=1 )
cmds.polySphere ( sx=10, sy=15, r=1 )
cmds.polySphere ( sy=15, r=1)

You could write your own maya command with Python API, which would be more complicated but completely possible. The good part of using the Python API to write a command is it includes other nice features that Maya does for you, but I wont get into that now. For now I will give an example how a basic function can imitate this. Example:

Continue reading »

Oct 22

So as one of my first posts I thought I would publish something on one of the reasons I like python over MEL. In C++ there something called function overloading. Which means you can create a function with the same name but different argument types and number of arguments. For example:

int foo( int varA )
{
    return (varA * 2);
}

double foo( double varA, int varB )
{
    return (varA * varB);
}

In the above C++ example you can pass foo an int which is valid or pass foo a double and an int which is valid.

So getting back to MEL and Python, MEL has strongly typed variables like C++ and no general way to overload functions. You could pass your procedure a string array and maybe have the first argument say what to do with that string array, but in the end your still passing and only can pass a string array to that variable.

In Python there is no way to create completely separate overload functions, but you can create a function that receive different types of variables and does completely separate actions based on what types it receives. For example:

Continue reading »