コマンド・プラグイン

コマンド・プラグインとは

コマンド・プラグインによって新しいMELコマンドを作ることができる。

コマンド・プラグインの作り方

コマンド・プラグインのクラスはMPxCommandクラスを継承して作られる。

class commandName : public MPxCommand    ← commandNameというMELコマンドが作られる
{
public:
	MStatus        doIt( const MArgList& args );
	static void*   creator();
};

コマンド・プラグインに必要なメソッド

MStatus commandName::doIt( const MArgList& args )
コマンドで実行される計算・処理を実行するメソッド。
static void* commandName::creator()
コマンドが実行された時にコマンドの実体(インスタンス)を作って返す。
MStatus initializePlugin( MObject obj )
プラグインがロードされるときに実行される。
コマンドの登録などを行う。
MStatus uninitializePlugin( MObject obj )
プラグインがアンロードされるときに実行される。
コマンドの登録取消などを行う。

コマンド・プラグインの例1

下のプログラムをコンパイルしMAYAにプラグインとしてロードしてMELコマンド(hello)として実行する。
例えば、hello studentと実行するとHello studentと表示される。

#include <stdio.h>
#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>

class hello : public MPxCommand    ← helloというMELコマンドが作られる
{
	public:
	MStatus        doIt( const MArgList& args );
	static void*   creator();
};

MStatus hello::doIt( const MArgList& args )
{
                     ↓ コマンドを実行するとHello "引数" と表示する
	printf("Hello %s\n", args.asString( 0 ).asChar() );
	return MS::kSuccess;
}

void* hello::creator()
{
	return new hello;
}

MStatus initializePlugin( MObject obj )
{
	MFnPlugin plugin( obj, "Alias|Wavefront", "1.0", "Any");    ← 決まり文句
	plugin.registerCommand( "hello", hello::creator );    ← コマンドの名前とcreatorメソッド
	return MS::kSuccess;
}

MStatus uninitializePlugin( MObject obj )
{
	MFnPlugin plugin( obj );
	plugin.deregisterCommand( "hello" );    ← コマンドの名前

	return MS::kSuccess;
}

コマンド・プラグインの例2

ノードにコネクトされているアトリビュートのリストを表示するコマンド。

#include <stdio.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MPlugArray.h>

class findConnectedAttr : public MPxCommand
{
public:
	MStatus doIt(const MArgList& args);
	static void* creator();
};

MStatus findConnectedAttr::doIt(const MArgList& args)
{
	if(args.length() != 2)
		return MS::kFailure;
	MString nodeName = args.asString(0);
	MString attr = args.asString(1);

	// 以下はノード名とそのアトリビュート名からコネクトされているアトリビュートを調べる手順
	// ノードを名前から MSelectionList によって探す
	MSelectionList tempList;
	tempList.add(nodeName);
	// 見つからない場合は終り
	if(tempList.length() <= 0)
		return MS::kFailure;
	// MSelectionList からノードを求める
	MObject node;
	// ここでは最初の一つ目のノードだけを求める
	tempList.getDependNode(0, node);
	// ノードから MFnDependencyNode を求める
	MFnDependencyNode nodeFn(node);
	// MFnDependencyNode からアトリビュートの MPlug を求める
	MPlug srcPlug = nodeFn.findPlug(attr);
	// アトリビュートにコネクトしているアトリビュートのリストを求める
	MPlugArray nodeConnections;
	srcPlug.connectedTo(nodeConnections, false, true);
	for(unsigned i = 0; i < nodeConnections.length(); i++)
	{
		cout << attr << " to " << nodeConnections[i].name() << "\n";
	}
	srcPlug.connectedTo(nodeConnections, true, false);
	for(unsigned i = 0; i < nodeConnections.length(); i++)
	{
		cout  << nodeConnections[i].name() << " to " << attr << "\n";
	}
	return MS::kSuccess;
}

void* findConnectedAttr::creator()
{
	return new findConnectedAttr;
}

MStatus initializePlugin(MObject obj)
{
	MFnPlugin plugin(obj, "Alias|Wavefront", "1.0", "Any");
	plugin.registerCommand("findConnectedAttr", findConnectedAttr::creator);
	return MS::kSuccess;
}

MStatus uninitializePlugin(MObject obj)
{
	MFnPlugin plugin(obj);
	plugin.deregisterCommand("findConnectedAttr");

	return MS::kSuccess;
}

コマンド・プラグインの例3

指定されたノードのアトリビュートに値を設定するコマンド。
ノードの名前にはワイルドカードを使用できる。 その場合、該当する名前のノードすべてのアトリビュートに値が設定される。

#include <stdio.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MPlug.h>

class setAttribute : public MPxCommand
{
public:
	MStatus doIt(const MArgList& args);
	static void* creator();
};

MStatus setAttribute::doIt(const MArgList& args)
{
	if(args.length() != 3)
		return MS::kFailure;
	MString nodeName = args.asString(0);
	MString attrName = args.asString(1);
	// アトリビュートは文字列に変換しておけば良い
	MString value = args.asString(2);

	// ノードを名前から探す
	MSelectionList tempList;
	tempList.add(nodeName);
	// 見つからなければ終り
	if(tempList.length() <= 0)
		return MS::kFailure;
	MObject node;
	for(int i = 0; i < tempList.length(); i++)
	{
		// i 番目のノードを取得する
		tempList.getDependNode(i, node);
		// ノードから MFnDependencyNode を求める
		MFnDependencyNode nodeFn(node);
		// MFnDependencyNode からアトリビュートの MPlug を求める
		MPlug plug = nodeFn.findPlug(attrName);
		// MPlug に値を設定する
		// 文字列 value から適切な型(int, float など)に変換されて設定される
		plug.setValue(value);
	}
	return MS::kSuccess;
}

void* setAttribute::creator()
{
	return new setAttribute;
}

MStatus initializePlugin(MObject obj)
{
	MFnPlugin plugin(obj, "Alias|Wavefront", "1.0", "Any");
	plugin.registerCommand("setAttribute", setAttribute::creator);
	return MS::kSuccess;
}

MStatus uninitializePlugin(MObject obj)
{
	MFnPlugin plugin(obj);
	plugin.deregisterCommand("setAttribute");

	return MS::kSuccess;
}

参考


Prev | Next
Home | Contents
Mail