Maya Python Tutorial
Putting the power of animation, 3D modeling, visual effects, compositing and 3D rendering into the hands of creative professionals, Maya has become the go-to tool for creative graphics and motion graphics professionals everywhere. The power of Maya lies not only in its creation tools but in its workflow management and extensibility. Offering a choice of two Application Programming Interfaces, C++ and Python, Maya allows the user to automate and extend Maya's functionality. Python allows rapid creation of Maya plugins without the overhead of writing and compiling C++ programs.
Instructions
-
-
1
Open a terminal session and check your version of Python by typing the following at the command line prompt:
My-iMac:~ MyAccount$ python -v
This will return a list of all the standard Python libraries and the current version of python, which should be 2.6 or higher.
-
2
Open the Maya script editor from the menu bar: Window -> General Editors -> Script Editor.
Type the following Python script into the main text window:
import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
#Define a new command, "Hello World." The sp prefix makes this a scripted plugin.
kPluginCmdName = "spHelloWorld"
# command
class scriptedCommand(OpenMayaMPx.MPxCommand):
def __init__(self):
OpenMayaMPx.MPxCommand.__init__(self)
def doIt(self,argList): print "Hello World!"
# Creator
def cmdCreator():
return OpenMayaMPx.asMPxPtr( scriptedCommand() )
# Initialize the script plug-in
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.registerCommand(kPluginCmdName, cmdCreator )
except:
sys.stderr.write( "Failed to register command: "%s\n" %
kPluginCmdName )
raise
# Uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterCommand( kPluginCmdName )
except:
sys.stderr.write( "Failed to unregister command: %s\n" %
kPluginCmdName )
raise
print "Script done"
Save this file as HelloWorld to the Maya scripts directory.
-
-
3
Type the following in the Maya command line window, located at the bottom of the main Maya window:
loadPlugin "helloWorld";
A window will open with the message, "hello world" and the Maya command line will display the status message, "script done."
-
1
References
- Photo Credit Thinkstock/Comstock/Getty Images