Share

A Python API 1.0 Hello World example explained

The Hello World examples demonstrates several important components of plug-in development using the Maya Python API 1.0.

You can find more information on Python functions and plug-ins in the Maya Python API section.

Command plug-ins inherits from MPxCommand

class Py1HelloWorldCmd(OpenMayaMPx.MPxCommand):
    kPluginCmdName = "py1HelloWorld"

Instantiate the command

The command is instantiated using cmdCreator(). doIt() executes the command's actions. In more complex command plug-ins, doIt() is used to parse arguments, set internal data, and do other housekeeping before it calls the redoIt() function. The redoIt() function then performs the command's actions.

See The doIt() and redoIt() functions for more information.

@staticmethod
   def cmdCreator():
        return OpenMayaMPx.asMPxPtr( Py1HelloWorldCmd() )

        def doIt(self,argList):
            print ("Hello World!")

Initialize the plug-in

All plug-ins need to implement the initializePlugin() function. This function in turn calls registerCommand(). The plug-in will fail to load if initializePlugin() is not implemented. For more information, see Initializing and uninitializing plug-ins.

def initializePlugin(plugin):
    pluginFn = OpenMayaMPx.MFnPlugin(plugin)
    try:
        pluginFn.registerCommand(
        Py1HelloWorldCmd.kPluginCmdName, Py1HelloWorldCmd.cmdCreator
    )
    except:
        sys.stderr.write(
            "Failed to register command: %s\n" % Py1HelloWorldCmd.kPluginCmdName
    )
    raise

Uninitialize the plug-in

All plug-ins need to implement the uninitializePlugin() function. This function in turn calls deregisterCommand(). The plug-in will fail to load if uninitializePlugin() is not implemented. For more information, see Initializing and uninitializing plug-ins.

def uninitializePlugin(plugin):
    pluginFn = OpenMayaMPx.MFnPlugin(plugin)
    try:
        pluginFn.deregisterCommand(Py1HelloWorldCmd.kPluginCmdName)
    except:
        sys.stderr.write(
            "Failed to unregister command: %s\n" % Py1HelloWorldCmd.kPluginCmdName
        )
    raise

Was this information helpful?