【问题标题】:Event in Maya Api to capture currentTime / frame changeMaya Api 中的事件以捕获 currentTime / 帧变化
【发布时间】:2015-06-01 08:27:19
【问题描述】:


我目前正在开发一个 Maya 插件。如何设置每次在场景中更改帧号/当前时间时触发的回调?
我查看了 MSceneMessage 类,但它似乎不包含我要查找的内容。
谢谢。

【问题讨论】:

    标签: c++ events callback message maya


    【解决方案1】:

    您可以使用MEventMessage 在每次帧/当前时间更改时设置回调。代码胜于雄辩,所以这里有一些代码,其中穿插了 cmets 来说明如何设置:(不耐烦的先用 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);
    

    带有 cmets 的完整(ish)代码:

    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;
    }
    

    【讨论】:

    • 补充问题:有没有办法让“userCB”不是静态函数?我想在同一个类中调用一个非静态函数。谢谢。
    • 您正在构建的插件是节点、Deformer 还是命令?这是什么插件?
    • 它是一个节点。但我想我找到了解决办法。我将当前类(this)作为 addEventCallback 中的第三个参数发送,并在我可以访问的 userCB 中接收此类。
    • 这似乎是唯一可行的解​​决方案。
    【解决方案2】:

    此外,根据应用程序,您可以创建一个调用 arbitray python/mel 的表达式(包括插件中的 MPXCommand)。

    【讨论】:

    • 根据用例,这可能是一个更加模块化的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2011-09-10
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多