I´ve finally started working with Python and Maya, yeeeeyyy!! It has been great and I thought would be nice to share a few ideas in this post.
MEL is definitely a great starting point and we still love it, but it is a bit old nowadays since python makes life much easier, suitable and powerfull in terms of programming for CG. So let´s begin.
The bindings for all of the native Maya commands are in the maya.cmds module, wich can be called as following:
# import as default
import maya.cmds
# testing it out
selection = maya.cmds.ls(sl=True)
# import using shorter namespace ie. cmds, cmd, mc
import maya.cmds as mc
selection = mc.ls(sl=1)
print 'selected nodes are %s \n' % selection
A top-level namespace to import all the names directly into the module could make things cleaner:
# built-in modules
import sys
# import using top-level
from maya.cmds import *
selection = ls(sl=1)
# print in output window using Python sys module
for sel in selection:
sys.__stdout__.write('selected node= ' + sel + '\n')
But If you find yourself writing that in your code, you should consider this Autodesk help link and also this article.
The cool new pythonic OOP based system inside 2011 and 2012 versions is Pymel, wich wrap classes and basically attaches attributes exposing their properties (don´t forget to check CG Bootcamp tutorial aswell):
# import pymel
import pymel.core as pm
# note the inheritance of methods
lastSelected = pm.ls(sl=1)[-1]
longNames = pm.listAttr(lastSelected.longName())
print 'node= %s attrs= %s' % (lastSelected, longNames)
Last but not least, it is a good idea to choose and configure one IDE to speed up the workflow. I personally use Eclipse as Maya IDE. Check out Jason Parks Power Python for Maya (Plus) LIVE lecture recorded by Rigging Dojo to learn more about that.
cheers ;)