Tuesday, October 30, 2007

Python API in Maya 2008

This evening I thought I'd give the Maya 2008 Python API code a try, and it was pretty cool. In the past in the Maya API you always have to add code to your plugin, re-compile, perhaps even close Maya and re-start it to get the updated plugin to run... or at least you have to unload it and then reload it. So tonight I thought I'd give the new Maya Python API code a shot and it is soooooooooooooo cool. It took a bit to get the syntax right, since obviously static member functions don't exist in Python with the same Syntax as C++.

I.e. if you want to print something to the script output you can rock the 'ol school MGlobal style...

In C++ it's:
MString information("This is my display string");
MGlobal::displayInfo(information)

In Python it's:
import maya.OpenMaya as OpenMaya
information = "This is my display string"
OpenMaya.MGlobal.displayInfo(information)

So the usual new weirdness to get used to. I guess one obvious and awesome change is that there is no MString anymore. This is a good idea of course since Python's string stuff is just so awesome and it was a good choice to stick to that. The lack of types in Python is kinda nuts when you are dealing with random MFn's in Maya API and all that craziness, but I guess if you keep good coding style then it's probably not too insane.

As I was coding I realized that I had completely forgotten how to do a for loop in Python. I was trying some ugly syntax to try to iterate over an MItSelectionList iterator... and it totally didn't work. I ended up doing a

while not iter.isDone():
....do stuff
....iter.next()

And that did the trick. Totally weird when you are so used to one language and another one is as different as this. I don't really think for loops are a good idea for iterators in the Python API, but I guess maybe there is a nicer way to do it. I suppose in the classic Maya C++ API examples of using a for loop for an iterator, you don't even use the initialization condition so I guess a while loop really does the job anyway.

Pretty awesome stuff.

Another thing I found weird (for some reason, although on second glance it seems quite like the expected syntax) is that you need to kindof do this weird method call to get an MObject or MSelectionList or whatever you might want, and you set it to the standard dynamically-specified Python type.

I.e. if you want to get an the current active selection list, you do this...

import maya.OpenMaya as OpenMaya
selList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(selList)

So this seems kindof verbose, but the Maya API is generally kindof verbose (at least if you're comparing it to just doing the same thing in MEL), and so I guess it's as expected. But there is some weirdness like the lack of :: namespaces, and the fact that MSelectionList selList(); is now selList = OpenMaya.MSelectionList()?!??!! Haha so weird. But it makes sense. Just something new to get used to.

Anyway it's a reaaaaaaaaaaaaaaaaaally awesome thing that this is available now, and actually I've always really liked MEL because it's really easy to prototype something or just code something super fast and see if it works before you spend ages on it. So this gives you that speed and interpreted language benefit, but with the very simple ability to copy and paste (pretty much) your code from the Python script window into your C++ file after you get it working on smaller examples inside of Maya. Then you can compile it and try again on larger examples with your plugin. Sweet. This is going to be pretty fantastic, and I am actually quite impressed and excited at the possibility of using the Python API hand-in-hand with the C++ API for quicker development.

Every time I look at the Python changes in Maya 8.5 onwards I am pretty much blown away by how big of a change this is, and what it means for production software development in general. Very, very nice. And I am so psyched that it's really easy to move your knowledge from one to the other. See my thoughts on Python in Maya from a couple of weeks ago. It's nice to now have given myself an introduction to the Python API in Maya and to get an idea of where I can benefit from it. Good stuff.

2 comments:

Fabian Mejia said...

I have added a small tutorial in Maya with Python. Take a look

http://fabianmejia.blogspot.com/2008/06/maya-2008-plugin-development-part-2.html

Stephen Weber said...

Here's a for loop. For Mesh classes I found a count() function in the iterator, but no count() function in MItSelectionList, go figure...


import maya.OpenMaya as OM

#Assign all selected objects to a selection list
list = OM.MSelectionList()
OM.MGlobal_getActiveSelectionList(list)
stuff = OM.MItSelectionList(list)

for i in range(0,list.length()):

-- dpath= OM.MDagPath()
-- stuff.getDagPath(dpath)
-- print dpath.partialPathName()
-- stuff.next()

stephen_weber@hotmail.com