A Python API 2.0 Hello World example explained
The Hello World examples demonstrates several important components of plug-in development using the Maya Python API 2.0.
You can find more information on Python functions and plug-ins in the Maya Python API section. You can also refer to the Python API reference.
Indicate that the plug-in uses the Python API 2.0
def maya_useNewAPI():
pass
Command plug-ins inherits from MPxCommand
class Py2HelloWorldCmd(om.MPxCommand):
kPluginCmdName = "py2HelloWorld"
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 Py2HelloWorldCmd()
def doIt(self, args):
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 = om.MFnPlugin(plugin)
try:
pluginFn.registerCommand(
Py2HelloWorldCmd.kPluginCmdName, Py2HelloWorldCmd.cmdCreator
)
except:
sys.stderr.write(
"Failed to register command: %s\n" % Py2HelloWorldCmd.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 = om.MFnPlugin(plugin)
try:
pluginFn.deregisterCommand(Py2HelloWorldCmd.kPluginCmdName)
except:
sys.stderr.write(
"Failed to unregister command: %s\n" % Py2HelloWorldCmd.kPluginCmdName
)
raise