我对 Maya 2013 中的示例命令插件有疑问 API 为了清楚起见,插件的代码已分为 .h 和 .cpp 文件,但在其他方面应该是正确的。
pluginCmd.h:
// Include the needed headers.
#include <stdio.h>
#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MIOStream.h>
// Class to represent our command.
class commandExample : public MPxCommand
{
public:
commandExample();
virtual ~commandExample();
MStatus doIt( const MArgList& );
MStatus redoIt();
MStatus undoIt();
bool isUndoable() const;
static void* creator();
};
pluginCmd.cpp:
// Include the header for the file.
#include "pluginCmd.h"
// Constructor for the command object.
commandExample::commandExample() {
cout << "In commandExample::commandExample()\n";
}
// Destructor for the command object.
commandExample::~commandExample() {
cout << "In commandExample::~commandExample()\n";
}
// The actual command/work to be performed.
MStatus commandExample::doIt( const MArgList& ) {
cout << "In commandExample::doIt()\n";
return MS::kSuccess;
}
// The creator is called when the command is invoked and sets up the command object.
void* commandExample::creator() {
cout << "In commandExample::creator()\n";
return new commandExample();
}
// Gets called when the plugin is loaded into Maya.
MStatus initializePlugin( MObject obj ) {
// Set plugin registration info: Author, plugin-version and Maya version needed.
MFnPlugin plugin( obj, "Martin Jørgensen", "1.0", "Any" );
plugin.registerCommand( "commandExample", commandExample::creator );
// Print to show plugin command was registered.
cout << "In initializePlugin()\n";
return MS::kSuccess;
}
// Gets called when the plugin is unloaded from Maya.
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
plugin.deregisterCommand( "commandExample" );
// Print to show the plugin was unloaded.
cout << "In uninitializePlugin()\n";
return MS::kSuccess;
}
使用 Maya 2013 x64 库在 Windows 7 x64 Visual Studio 12 上成功编译。 当它加载到插件管理器中时,会发生注释(它应该打印初始化状态)。但是当它被卸载时,会出现初始化打印。有谁知道这是为什么吗?
我在使用 Visual Studio 2015 和 Maya 2016 x64 编写插件时遇到同样的问题。
建议的解决方法
cout << "Something out" << endl;
似乎不再起作用了。
我发现写入
cerr
的内容确实会显示在 Maya 的输出窗口中。
因此,作为临时解决方法,我将
cout
重定向到 cerr
:
cout.rdbuf(cerr.rdbuf());
不理想,但至少我们可以看到输出......
尝试使用:
cout << "Something out" << endl;
正如 PeterT 在评论中建议的那样,这很有效。
作为奖励,我意识到调用命令时它没有“挂起”,这是因为命令对象在撤消/重做历史记录中仍然处于活动状态。这是我掌握 Maya 工作原理的错误。
对于 Maya 2023.3 我正在使用:
#include <maya/MGlobal.h>
MGlobal::displayInfo("this is a normal message");
MGlobal::displayError("this is an error");
MGlobal::displayWarning("this is a warning");