Maya 中的事件 Api 以捕获当前时间/帧变化
Event in Maya Api to capture currentTime / frame change
我目前正在开发一个 Maya 插件。我如何设置每次在场景中更改帧号/当前时间时触发的回调?
我查看了 MSceneMessage Class,但它似乎不包含我要查找的内容。
谢谢
您可以使用 MEventMessage
设置每次 frame/current 时间更改时的回调。代码胜于雄辩,所以这里有一些代码和注释穿插以说明如何设置它:(首先为不耐烦的TLDR,下一节将提供完整的代码摘录)
TLDR:
不耐烦的代码摘要:
// ...
// Our callback Id array to
// store the Ids of all our callbacks
// for later removal
MCallbackIdArray myCallbackIds;
// This is where the actual adding callback happens
// We register our callback to the "timeChanged" event
MCallbackId callbackId = MEventMessage::addEventCallback("timeChanged", (MMessage::MBasicFunction) MySampleCmd::userCB);
// ...
if(myCallbackIds.length() != 0)
// Make sure we remove all the callbacks we added
stat = MEventMessage::removeCallbacks(myCallbackIds);
- 参考 MEventMessage from the docs。
- 列出的事件 here(在
scriptJob
命令中)可以与 MEventMessage
一起使用。
带有注释的完整(大概)代码:
MySampleCommand.h
class MySampleCmd : public MPxCommand {
public:
MySampleCmd();
virtual ~MySampleCmd();
// Our callback - implemented as a static method
static void userCB(void* clientData);
MStatus doIt( const MArgList& );
MStatus redoIt();
MStatus undoIt();
bool isUndoable() const;
static void* creator();
public:
// Our callback Id array to
// store the Ids of all our callbacks
// for later removal
MCallbackIdArray myCallbackIds;
};
MySampleCommand.cpp(摘录)
// Constructor
MySampleCmd::MySampleCmd() {
// Clearing our callback Id array
// for housekeeping
myCallbackIds.clear();
}
// Destructor
MySampleCmd::~MySampleCmd() {
// Make sure we remove all the callbacks we added
// Failing to do so will result in fatal error
if(myCallbackIds.length() != 0)
// Remove the MEventMessage callback
MEventMessage::removeCallbacks(myCallbackIds);
}
MStatus MySampleCmd::redoIt() {
// This is where the actual adding callback happens
// We register our callback to the "timeChanged" event
MCallbackId callbackId = MEventMessage::addEventCallback("timeChanged", (MMessage::MBasicFunction) MySampleCmd::userCB);
// Append the newly added callback's ID to our list of callback ids
// for future removal
myCallbackIds.append(callbackId);
return MS::kSuccess;
}
MStatus MySampleCmd::undoIt() {
MStatus stat;
if(myCallbackIds.length() != 0)
// Make sure we remove all the callbacks we added
stat = MEventMessage::removeCallbacks(myCallbackIds);
return stat;
}
// Our callback function
void SafeSelect::userCB(void* clientData) {
MGlobal::displayInfo( "Callback userCB called!\n" );
return;
}
此外,根据应用程序,您可以只创建一个调用任意 python/mel 的表达式(包括插件中的 NPX 命令)。
我目前正在开发一个 Maya 插件。我如何设置每次在场景中更改帧号/当前时间时触发的回调?
我查看了 MSceneMessage Class,但它似乎不包含我要查找的内容。
谢谢
您可以使用 MEventMessage
设置每次 frame/current 时间更改时的回调。代码胜于雄辩,所以这里有一些代码和注释穿插以说明如何设置它:(首先为不耐烦的TLDR,下一节将提供完整的代码摘录)
TLDR:
不耐烦的代码摘要:
// ...
// Our callback Id array to
// store the Ids of all our callbacks
// for later removal
MCallbackIdArray myCallbackIds;
// This is where the actual adding callback happens
// We register our callback to the "timeChanged" event
MCallbackId callbackId = MEventMessage::addEventCallback("timeChanged", (MMessage::MBasicFunction) MySampleCmd::userCB);
// ...
if(myCallbackIds.length() != 0)
// Make sure we remove all the callbacks we added
stat = MEventMessage::removeCallbacks(myCallbackIds);
- 参考 MEventMessage from the docs。
- 列出的事件 here(在
scriptJob
命令中)可以与MEventMessage
一起使用。
带有注释的完整(大概)代码:
MySampleCommand.h
class MySampleCmd : public MPxCommand {
public:
MySampleCmd();
virtual ~MySampleCmd();
// Our callback - implemented as a static method
static void userCB(void* clientData);
MStatus doIt( const MArgList& );
MStatus redoIt();
MStatus undoIt();
bool isUndoable() const;
static void* creator();
public:
// Our callback Id array to
// store the Ids of all our callbacks
// for later removal
MCallbackIdArray myCallbackIds;
};
MySampleCommand.cpp(摘录)
// Constructor
MySampleCmd::MySampleCmd() {
// Clearing our callback Id array
// for housekeeping
myCallbackIds.clear();
}
// Destructor
MySampleCmd::~MySampleCmd() {
// Make sure we remove all the callbacks we added
// Failing to do so will result in fatal error
if(myCallbackIds.length() != 0)
// Remove the MEventMessage callback
MEventMessage::removeCallbacks(myCallbackIds);
}
MStatus MySampleCmd::redoIt() {
// This is where the actual adding callback happens
// We register our callback to the "timeChanged" event
MCallbackId callbackId = MEventMessage::addEventCallback("timeChanged", (MMessage::MBasicFunction) MySampleCmd::userCB);
// Append the newly added callback's ID to our list of callback ids
// for future removal
myCallbackIds.append(callbackId);
return MS::kSuccess;
}
MStatus MySampleCmd::undoIt() {
MStatus stat;
if(myCallbackIds.length() != 0)
// Make sure we remove all the callbacks we added
stat = MEventMessage::removeCallbacks(myCallbackIds);
return stat;
}
// Our callback function
void SafeSelect::userCB(void* clientData) {
MGlobal::displayInfo( "Callback userCB called!\n" );
return;
}
此外,根据应用程序,您可以只创建一个调用任意 python/mel 的表达式(包括插件中的 NPX 命令)。