API Guide > Maya Python API > Maya Python API 1.0 >

# Your First Maya Python Plug-in

This topic will guide you through the creation and execution of your first Maya Python plug-in.

Setting the Maya Plug-in Environment Variable

To begin writing Python plug-ins for Maya, the first step is to define your MAYA_PLUG_IN_PATH environment variable, if you have not done so already. When Maya launches, this variable will tell Maya where to look for your plug-ins.

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\Maya\scripts" corresponds to the current user's My Documents\Maya\scripts directory. Multiple paths can be specified by separating these variable values with a semicolon ";".

When you launch Maya, the directories contained in the MAYA_PLUG_IN_PATH will appear as different sections under Window > Settings/Preferences > Plug-in Manager.

Writing a Python Plug-in using the Script Editor

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 choosing, however we will pursue this topic using Maya's built-in interactive Script Editor. Note that the current version of Maya supports the Python 2.6 interpreter.

In the Python tab of the Script Editor, paste the plug-in code below. 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 structure of a plug-in in Using the Maya Python API. 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 )

Now, in the Script Editor window, select File > Save Script, and save the script in the same directory specified by your MAYA_PLUG_IN_PATH environment variable, using the file name "myFirstPlugin.py" (you may omit the .py extension - it will be appended automatically).

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.

Loading your Python Plug-in

There are two ways to load you newly created plug-in into Maya:

- Programmatically - In the Script Editor, delete our previous Python code. Now write the following lines of code and execute them using the blue "Play" button on the Script Editor toolbar.

      import maya.cmds
      maya.cmds.loadPlugin("myFirstPlugin.py")

  Observe that if you go into the Plug-in Manager window, the plug-in's "Loaded" checkbox will now be checked.
- Manually - In the Plug-in Manager window, you may check the "Loaded" and/or "Auto load" checkboxes. Selecting these will respectively load the plug-in, and ensure that the plug-in is automatically loaded when Maya is launched. Un-checking and re-checking the "Loaded" checkbox will reload your plug-in, which may be useful for testing and debugging your code.

Invoking your Python Plug-in

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 also appear as a 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
    maya.cmds.spHelloWorld()

The Script Editor should display the following lines, which echo our commands as well as the "Hello World!" output.

    import maya.cmds
    maya.cmds.spHelloWorld()
    Hello World!

Congratulations! You have just run your first Maya Python plug-in!