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