摩尔网 > CG教程 > Autodesk Maya教程
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函数 doIt(const MArglist &args); static void* creator()用于生成一个类对象.
下面看一下SimpleCommand的实现代码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类的实例用于执行具体的任务,那么该对象由谁创建并触发doIt()呢?
命令对象的注册
我们需要注册该命令对象,并且在输入命令的时候(这里使用"HelloWorld"), Maya创建该对象并触发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中的插件是一个动态链接库,只不过后缀名为mll. MLL_EXPORT MStatus initializePlugin(MObject obj) 和
MLL_EXPORT MStatus uninitializePlugin(MObject obj)是该DLL的导出函数,前者将在DLL加载时被调用;后者将在DLL
被卸载时调用.
/// register the commandMStatus status = plugin.registerCommand("HelloWorld", SimpleCommand::creator); 注册了命令"HelloWorld",并
告诉Maya:函数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. 查看输入结果
由此看出,每执行一次HelloWorld, 都会创建一个SimpleCommand对象实例,执行完毕后销毁!
上一篇: 你必须会:很冷门但是却很实用的maya技巧 下一篇: Maya如何将每帧都渲染成图片