Sunday, August 15, 2010

External Editor

Maya 2011 new features
Although Maya 2011 added a slew of new features to the script editor, it is still best practice to program in a standalone IDE.  Some of the benefits include:
  • Custom syntax highlighting.
  • Collapsible Code.
  • Regex Assistance.
  • File management, versioning, sharing is easier. 
  • Runs on an independent process.
Fortunately,  Maya automatically saves your scripts on exit.  However, you WILL lose data if you solely rely on this feature.  This can occur if you are coding on multiple instances of Maya simultaneously, or more likely, Maya crashes.  Also, keep in mind, your scripts can easily, unintentionally trigger a crash themselves.  Programming on an independent process will allow you to force quit from Maya with no data lose.

Again Python has the benefit over MEL in that it is an independent, open-source language.  Therefore IDE's are readily available.  Eclipse + PyDev [open-source], Komodo [commercial], PyCrust [commercial], etc.  In addition every Python interpreter comes with a bare bones editor called IDLE.  Install either version 2.5 or 2.6 (post Maya 2010).  http://www.python.org/download/  To launch the IDE go to Start > Python > IDLE.  You can also launch it through a shell using:
python C:/Python27/Lib/idlelib/idle.py
Yes, you are using a Python application to write your own Python scripts.  Once the Tk Python shell opens go to File > New Window.  Here you can type your code as if you were using Maya's Script Editor.  You will want to save your file to ~\Users\Paul\Documents\maya\scripts [Windows] or ~/Library/Preferences/Autodesk/maya/scripts [Mac] with a .py extension.


Then in order to execute this file from Maya type:
import filename
Once the file is imported successfully and you want to make changes you will need to run:
reload(filename)
There may be times while debugging when it may be more convenient to execute portions of your external script from Maya.  To interact with imported objects you must type the filename . variableName.
import HelloWorld
print HelloWorld.mySting  #call a variable defined in HelloWorld.py
HelloWorld.myFunction()   #call a function defined in HelloWorld.py
*Maya can read Python documents created after Maya has launched, but cannot read new MEL files until it has been restarted.