Mel Command命令插件
Mel(Maya Embeded Language)命令指的能夠在命令解釋器里運行的命令。所有的自定義命令要從MPxCommand類繼承,
看如下代碼MyCommand.h
[cpp] view plain copy print?#ifndef __MYCOMMAND_H__ #define __MYCOMMAND_H__ #ifdef WIN32 #ifndef NT_PLUGIN #define NT_PLUGIN #endif #endif #include
#define __MYCOMMAND_H__
#ifdef WIN32
#ifndef NT_PLUGIN
#define NT_PLUGIN
#endif
#endif
#include
#include
#include
#include
#include
#include
/// /brief
/// the simple PxCommand.
class SimpleCommand : public MPxCommand
{
public:
/// /brief
/// constructor && destructor
SimpleCommand() {
std::cout << "SimpleCommand Constructed!" << std::endl;
}
virtual ~SimpleCommand() {
std::cout << "~SimpleCommand Destructed!" << std::endl;
}
/// /brief
/// executes this command.
/// /param
/// args (in) the argument list specified
/// /ret
/// return MS::kSuccess or MS::kFailure
virtual MStatus doIt(const MArgList &args);
/// /brief
/// this function is used by maya to create a new instance of this class.
static void *creator() {
return new SimpleCommand;
}
};
#endif /// __MYCOMMAND_H__
SimpleCommand類需要改寫virtual函數(shù) doIt(const MArglist &args); static void* creator()用于生成一個類對象.
下面看一下SimpleCommand的實現(xiàn)代碼SimpleCommand.cpp
[cpp] view plain copy print?#include #include "MyCommand.h" MStatus SimpleCommand::doIt(const MArgList &args) { std::cout << "execute SimpleCommand: " << std::endl; return MS::kSuccess; } #include
#include "MyCommand.h"
MStatus SimpleCommand::doIt(const MArgList &args)
{
std::cout << "execute SimpleCommand: " << std::endl;
return MS::kSuccess;
}
可以看到SimpleCommand::doIt()只是簡單地打印出字符串, 這是為觀察SimpleCommand對象的行為做準備. SimpleCommand類的實例用于執(zhí)行具體的任務(wù),那么該對象由誰創(chuàng)建并觸發(fā)doIt()呢?
命令對象的注冊
我們需要注冊該命令對象,并且在輸入命令的時候(這里使用"HelloWorld"), Maya創(chuàng)建該對象并觸發(fā)doIt(). 看如下注冊代碼
plugMain.cpp
[c-sharp] view plain copy print?#include "MyCommand.h" #include
#include
#include
#ifdef WIN32
#define MLL_EXPORT __declspec(dllexport)
#else
#define MLL_EXPORT
#endif
/// /brief
/// initialize plugin
/// /param
/// obj (in) the plugin handle
/// /ret
/// return MS::kSuccess if OK.
/// /NOTE:
/// invoked only once by Maya when DLL is loaded.
MLL_EXPORT MStatus initializePlugin(MObject obj)
{
MFnPlugin plugin(obj, "Jett Huang", "1.0", "Any");
/// register the command
MStatus status = plugin.registerCommand("HelloWorld", SimpleCommand::creator);
if (!status) {
status.perror("Failed to register /"HelloWorld/"/n");
return status;
}
else {
std::cout << "initializePlugin" << std::endl;
}
return status;
}
/// /brief
/// unintializePlugin
/// /param
/// obj (in) the plugin handle to un-register
/// /ret
/// MS::kSuccess if OK.
MLL_EXPORT MStatus uninitializePlugin(MObject obj)
{
MFnPlugin plugin(obj);
/// deregister the PxCommand
MStatus status = plugin.deregisterCommand("HelloWorld");
if (!status) {
status.perror("failed to deregister /"HelloWorld/"/n");
return status;
}
else {
std::cout << "uninitializePlugin" << std::endl;
}
return status;
}
Maya中的插件是一個動態(tài)鏈接庫,只不過后綴名為mll. MLL_EXPORT MStatus initializePlugin(MObject obj) 和
MLL_EXPORT MStatus uninitializePlugin(MObject obj)是該DLL的導(dǎo)出函數(shù),前者將在DLL加載時被調(diào)用;后者將在DLL
被卸載時調(diào)用.
/// register the commandMStatus status = plugin.registerCommand("HelloWorld", SimpleCommand::creator); 注冊了命令"HelloWorld",并
告訴Maya:函數(shù)SimpleCommand::creator()將會返回命令對象.
/// deregister the PxCommandMStatus status = plugin.deregisterCommand("HelloWorld"); 注銷命令.
完整的Source Code下載URL://download.csdn.net/source/2700425
運行并觀察
1. 將Build成功的插件MyCommand.mll放入Maya的安裝目錄下的/bin/plug-ins/;
2. 在Maya中加載插件, 菜單路徑為: Window --> Settings/Preferences --> PluginManager;
3. 在腳本命令窗口輸入:HelloWorld
4. 查看輸入結(jié)果
由此看出,每執(zhí)行一次HelloWorld, 都會創(chuàng)建一個SimpleCommand對象實例,執(zhí)行完畢后銷毀!
上一篇: 你必須會:很冷門但是卻很實用的maya技巧 下一篇: Maya如何將每幀都渲染成圖片