In the following topic, we outline the basic code structure of a command plug-in.
The following Python code is a sample command plug-in. We examine the relevant sections of this sample command plug-in below.
# sampleCommand.py import sys import maya.api.OpenMaya as OpenMaya # ... additional imports here ... ########################################################## # Plug-in ########################################################## class MyCommandClass( OpenMaya.MPxCommand ): kPluginCmdName = 'myCommandName' def __init__(self): ''' Constructor. ''' OpenMaya.MPxCommand.__init__(self) @staticmethod def cmdCreator(): ''' Create an instance of our command. ''' return MyCommandClass() def doIt(self, args): ''' Command execution. ''' # Remove the following 'pass' keyword and replace it with # the code you want to run. pass ########################################################## # Plug-in initialization. ########################################################## def maya_useNewAPI(): """ The presence of this function tells Maya that the plugin produces, and expects to be passed, objects created using the Maya Python API 2.0. """ pass def initializePlugin( mobject ): ''' Initialize the plug-in when Maya loads it. ''' mplugin = OpenMaya.MFnPlugin( mobject ) try: mplugin.registerCommand( MyCommandClass.kPluginCmdName, MyCommandClass.cmdCreator ) except: sys.stderr.write( 'Failed to register command: ' + MyCommandClass.kPluginCmdName ) def uninitializePlugin( mobject ): ''' Uninitialize the plug-in when Maya un-loads it. ''' mplugin = OpenMaya.MFnPlugin( mobject ) try: mplugin.deregisterCommand( MyCommandClass.kPluginCmdName ) except: sys.stderr.write( 'Failed to unregister command: ' + MyCommandClass.kPluginCmdName ) ########################################################## # Sample usage. ########################################################## ''' # Copy the following lines and run them in Maya's Python Script Editor: import maya.cmds as cmds cmds.loadPlugin('sampleCommand.py') cmds.myCommandName() '''
# sampleCommand.py
import sys
import maya.OpenMayaMPx as OpenMayaMPx
# ... additional imports here ...
kPluginCmdName = 'myCommandName'
##########################################################
# Plug-in 
##########################################################
class MyCommandClass( OpenMayaMPx.MPxCommand ):
    
    def __init__(self):
        ''' Constructor. '''
        OpenMayaMPx.MPxCommand.__init__(self)
    
    def doIt(self, args):
        ''' Command execution. '''
        
        # Remove the following 'pass' keyword and replace it with 
        # the code you want to run.
        pass
    
##########################################################
# Plug-in initialization.
##########################################################
def cmdCreator():
    ''' Create an instance of our command. '''
    return OpenMayaMPx.asMPxPtr( MyCommandClass() )
def initializePlugin( mobject ):
    ''' Initialize the plug-in when Maya loads it. '''
    mplugin = OpenMayaMPx.MFnPlugin( mobject )
    try:
        mplugin.registerCommand( kPluginCmdName, cmdCreator )
    except:
        sys.stderr.write( 'Failed to register command: ' + kPluginCmdName )
def uninitializePlugin( mobject ):
    ''' Uninitialize the plug-in when Maya un-loads it. '''
    mplugin = OpenMayaMPx.MFnPlugin( mobject )
    try:
        mplugin.deregisterCommand( kPluginCmdName )
    except:
        sys.stderr.write( 'Failed to unregister command: ' + kPluginCmdName )
##########################################################
# Sample usage.
##########################################################
''' 
# Copy the following lines and run them in Maya's Python Script Editor:
import maya.cmds as cmds
cmds.loadPlugin('sampleCommand.py')
cmds.myCommandName()
'''Maya plug-ins require two specific functions: initializePlugin(), and uninitializePlugin(), which are respectively called when Maya attempts to load and unload the plug-in. If these two functions do not exist in the file, the plug-in will fail to load.
def initializePlugin( mobject ):
    ''' Initialize the plug-in when Maya loads it. '''
    ...
def uninitializePlugin( mobject ):
    ''' Uninitialize the plug-in when Maya un-loads it. '''
    ...The code within these functions depends on the plug-in type; in the current case, our plug-in class is derived from MPxCommand, so we must register it by calling MFnPlugin.registerCommand(). This function takes the following two parameters:
import maya.cmds as cmds cmds.myCommandName()
Note that in the Python API 1.0, the pointer must be cast to a valid Maya object using OpenMayaMPx.asMPxPtr(). This is not required in the Python API 2.0. However, a maya_useNewAPI() function must be defined to indicate what type objects are being passed.
def cmdCreator():
    ''' Create an instance of our command. '''
    ...The behavior of our command is defined in MyCommandClass, which inherits from MPxCommand - the base class of all custom Maya commands. Classes prefixed by MPx are known as proxy classes, and are used to create various plug-in types. In the Dependency Graph Plug-in Basics section, we show how classes derived from MPxNode are used to create other plug-in types such as custom deformers, constraints, software and hardware shading nodes, surfaces, etc.
class MyCommandClass( OpenMayaMPx.MPxCommand ):
    
    def __init__(self):
        ''' Constructor. '''
        ...
    
    def doIt(self, args):
        ''' Command execution. '''
        
        # Remove the following 'pass' keyword and replace it with 
        # the code you want to run.
        passThe class name (MyCommandClass) can be any arbitrary name, however it must contain two specific functions: