次のトピックでは、コマンド プラグインの基本的なコード構造を示します。
次の Python コードは、コマンド プラグインのサンプルです。このサンプル コマンド プラグインについては、後の関連するセクションで説明します。
# 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 プラグインには initializePlugin() および uninitializePlugin() という 2 つの関数が必要であり、それぞれ Maya がプラグインのロードおよびアンロードを試みるときに呼び出されます。これら 2 つの関数がファイル内に存在しない場合、プラグインはロードに失敗します。
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 )これらの関数のコードはプラグインのタイプに依存します。現在、プラグインのクラスは MPxCommand から派生するので、MFnPlugin.registerCommand() を呼び出して登録する必要があります。この関数には次の 2 つのパラメータがあります。
import maya.cmds as cmds cmds.myCommandName()
def cmdCreator():
''' Create an instance of our command. '''
return OpenMayaMPx.asMPxPtr( MyCommandClass() )コマンドの動作は、MyCommandClass クラスで定義されています。このクラスは、すべてのカスタム Maya コマンドの基本クラスである OpenMayaMPx.MPxCommand から派生します。OpenMayaMPx Python モジュールで MPx というプリフィックスが付けられたクラスはプロキシ クラスと呼ばれ、さまざまなプラグイン タイプを作成するために使用されます。ディペンデンシー グラフ プラグインの基本セクションでは、OpenMayaMPx.MPxNode から派生したクラスを使用して、カスタム デフォーマ、コンストレイント、ソフトウェアおよびハードウェアのシェーディング ノード、サーフェスなどの他のプラグイン タイプを作成する方法が説明されています。
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クラス名(MyCommandClass)は任意ですが、特定の 2 つの関数が含まれている必要があります。