Projects | Tutorials
Date: 01/17/2008
Maya Version: 8.5
Contents:
- Introduction
- Python + MEL
- Python + OpenMaya
- IDE example: PyDev
Disclaimer: I'm still trying to figure this out myself, so if there is anything wrong written on this page, let me know, and I'll get it fixed up. I'm just trying to share what I've found so far, and document it for myself as well. I also apologize for the small font on the code... I need to find a way to not have the code parts of the page look so horrible.
Maya now supports Python scripting. As I understand it, there are actually two ways that you can invoke Maya commands in Python. There is the MEL equivalent Python-style scripting, and there is the Maya C++ API equivalent OpenMaya API. So you can now create Python scripts to control Maya.
In version 8.5, now when you open up the script editor, there are two tabs available to switch between MEL and Python. There is also a toggle in the bottom of the screen where you typed MEL commands.

So now you can type in python code like this in the script editor:
print "Hello World!"
a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
print i, a[i]
And you should see the output:
For more on Python, the official tutorial should give you a good idea of Python basics.
So, now that you've executed your first piece of Python code in Maya, let's try executing a bit of MEL. Autodesk calls what I think of as the MEL equivalent portion of Python as "Python in Maya". Python in Maya requires you to import all the python "bindings" for the native Maya commands. So at the beginning of each session or file, you must type in:
The "as cmds" portion lets you type in commands without typing the entire maya.cmds preamble. I love learning by example, so let's try opening up a dialog.
This will pop up a dialog box like the one below.
The equivalent command in MEL would have been:
Python commands need the cmds. preamble and also the flags look more like arguments to a function call. Notice that the button flag takes a list. This is because Python can't take the same argument twice, so you need to send the arguments as a list instead.
Now, let's try another more complicated example. Let's make a window show up with multiple tabs and buttons, and have it call other functions when we click on the button. We'll define functions like we did in MEL as well.
cmds.confirmDialog(title='Confirm me!', message='Sure about this?', button=['yes', 'no'],
defaultButton='yes', cancelButton='no')
def OpenWindow():
window = cmds.window( title="Testing a window", widthHeight=(300, 200) ) #define the window
tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5) #define the tabs
#First tab
tab1 = cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Do Nothing' )
cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
cmds.setParent( '..' )
cmds.tabLayout(tabs, edit=True, tabLabel=((tab1, 'Tab1')))
#Second tab
tab2 = cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Do Nothing' )
cmds.button( label='Confirm', command=('Confirm()') )
cmds.setParent( '..' )
cmds.tabLayout(tabs, edit=True, tabLabel=((tab1, 'Tab1'), (tab2, 'Tab2')))
cmds.showWindow( window )
Here, I just defined two functions, "Confirm", and "OpenWindow". The "Confirm" function will just open up the confirm window before. The "OpenWindow" function will create a new window with two tabs with two buttons on each tab.

As you can see, the preamble that you have to type in before every command gets a little annoying. The code looks pretty verbose for a dinky little window that you created, but I guess that's Python in Maya.
As an aside, Python in Maya is able to do everything that MEL is able to do. In fact, you can just call MEL from python by doing:
mm.eval("MELCMD") #and replace MELCMD with any MEL command
Python + OpenMaya
Autodesk as also provides a way for Python to latch onto the same sort of API that the C++ API was able to. This is called Open Maya, and it's just another module import:
import maya.OpenMaya as OpenMaya
Now, you can call any API functions just like in C++. What seems cooler to me about this is that you can load plug-ins into maya without compiling the code.
...More to come!
