次のトピックでは、コマンド プラグインの基本的なコード構造を示します。
次の Python コードは、コマンド プラグインのサンプルです。このサンプル コマンド プラグインについては、後の関連するセクションで説明します。
Python API 2.0:
# 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()
'''
Python API 1.0:
# 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 つのパラメータがあります。
コマンド名: この変数(便宜的に kPluginCmdName
という名前にしてあります)は、コマンドの名前を含みます。値には 'myCommandName'
を代入してあり、これは MFnPlugin.dergisterCommand()
の中、および sys.stderr.write()
に対するエラー レポート呼び出しで再利用されます。'myCommandName'
というコマンド名を登録してあるので、Maya のスクリプト エディタでは次の Python 命令を使用してコマンドを実行できます。
import maya.cmds as cmds
cmds.myCommandName()
コマンド作成関数リファレンス: 2 番目のパラメータは、コマンドのインスタンスを作成する関数へのリファレンスです。ここでは、cmdCreator
がこのコマンド クラスのインスタンスを作成し、有効な Maya オブジェクト ポインタとして返します。
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 つの関数が含まれている必要があります。
maya.cmds
Python モジュールから実行されると、コマンドはインスタンス化され、args
パラメータで指定した引数が doIt()
メソッドに渡されます。注: コマンドの引数とフラグの定義の詳細については、「コマンド引数とフラグ」を参照してください。
注: コマンドは
undoIt()
およびredoIt()
関数をサポートしていて、元に戻したり、やり直しに対応することもできます。詳細については、「オブジェクトの作成と操作」を参照してください。