By default, Maya attempts to load the plug-ins placed in the C:\Users\<username>\Documents\maya\<version>\plug-ins directory. You can optionally define additional directories in the MAYA_PLUG_IN_PATH environment variable to instruct Maya to load plug-ins from these locations.
In Windows 7, open the Start menu, type "env" in the search box and press enter. In the User variables section, click on the "New" button, and enter "MAYA_PLUG_IN_PATH" as the variable name. Set the variable value to the path of your choosing, where you will later save your scripts. For example, the variable value "%USERPROFILE%\Documents\scripts" corresponds to the current user's My Documents\scripts directory. Multiple paths can be specified by separating these variable values with a semicolon ";".
The Python and MEL Script Editor window can be found under Window > General Editors > Script Editor. Python plug-ins can also be written in an external text editor of your choice, however we will pursue this topic using Maya's built-in interactive Script Editor.
In the Python tab of the Script Editor, paste the plug-in code below. This code creates a simple command which prints "Hello World!" out to the Script Editor output when it is invoked. Other sample Python plug-ins, including this one (helloWorldCmd.py), can be found in the left HTML frame under API Reference > Examples. We describe the code structure of plug-ins in subsequent topics. For now, we will focus on executing our first plug-in:
import sys import maya.OpenMaya as OpenMaya import maya.OpenMayaMPx as OpenMayaMPx kPluginCmdName = "spHelloWorld" # Command class scriptedCommand(OpenMayaMPx.MPxCommand): def __init__(self): OpenMayaMPx.MPxCommand.__init__(self) # Invoked when the command is run. 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 )
In the Script Editor window, select File > Save Script. Set the file name to myFirstPlugin.py (you may omit the .py extension - it will be appended automatically), and save the script in C:\Users\<username>\Documents\maya\<version>\plug-ins, or in one of the directories defined by your MAYA_PLUG_IN_PATH environment variable.
To verify that Maya has detected your newly saved script, access the Plug-in Manager window via Window > Settings/Preferences > Plug-in Manager, and press the "Refresh" button. Your plug-in file should appear here. Do not select any of the "Loaded" or "Auto load" checkboxes for now.
import maya.cmds maya.cmds.loadPlugin("myFirstPlugin.py")
The plug-in we just wrote is a "Command" plug-in. That is to say, our plug-in defines a new command which outputs "Hello World!" when it is invoked. If our plug-in is properly loaded, then it should appear as a new MEL command, and should also appear as a new function within the maya.cmds Python module. This module defines all the available Maya Python commands (which is a subset of the available MEL commands). To invoke our command, execute the following code in the Script Editor:
import maya.cmds as cmds cmds.spHelloWorld()
import maya.cmds as cmds cmds.spHelloWorld() Hello World!