• 六、通过插件如何创建自己的MEL command


    1. MAYA API支持不同类型的plugin

      (1)Command Plugin——扩充MEL命令

      (2)Tool Commands——通过鼠标输出

      (3)DG plugin——对场景添加新的操作

      (4)Device Plugin——让其他的device链接到maya

    2. register 命令:MFnPlugin

    例子:

    #include <stdio.h>
    #include <maya/MString.h>
    #include <maya/MArgList.h>
    #include <maya/MFnPlugin.h>
    #include <maya/MPxCommand.h>
    #include <maya/MIOStream.h>
    class hello : public MPxCommand
    {
        public:
            MStatus doIt( const MArgList& args );
            static void* creator();
    };
    
    MStatus hello::doIt( const MArgList& args ) {
        cout << "Hello " << args.asString( 0 ).asChar() << endl;
        return MS::kSuccess;
    }
    
    void* hello::creator() {
        return new hello;
    }
    
    MStatus initializePlugin( MObject obj ) {
        MFnPlugin plugin( obj, "Autodesk", "1.0", "Any" );
        plugin.registerCommand( "hello", hello::creator );
        return MS::kSuccess;
    }
    
    MStatus uninitializePlugin( MObject obj ) {
        MFnPlugin plugin( obj );
        plugin.deregisterCommand( "hello" );
        return MS::kSuccess;
    }

    3. initializePlugin()

    The initializePlugin() function can be defined as either a C or C++ function. If you do not define this function, the plug-in will not be loaded.

    initializePlugin() contains the code to register any commands, tools, devices, and so on, defined by the plug-in with Maya.

    For example, commands and tools are registered by instantiating an MFnPlugin function set to operate on the MObjectpassed in. This MObject contains Maya private information such as the name of the plug-in file and the directory it was loaded from. 

    Once constructed, the MFnPlugin function set is used to register the contents of the plug-in file.

    4. MPxCommand:

    proxy让我们可以在maya中构建新的object类型,可以集成新的功能到maya中。

    A minimum of two methods must be defined. These are the doIt() method and the creator.

    In this case the destructor is called immediately since the command cannot be undone. Maya treats a non-undoable command as an action that does not affect the scene in any way. This means that no information needs to be saved after the command executes, and when undoing and redoing commands, it does not need to be executed since it does not change anything.

    The doIt() method is the only one that receives arguments. The undoIt()and redoIt() methods each take their data from internal data of the command itself.

    In the final else-clause, the displayError() method, inherited from MPxCommand, outputs the message in the command window and in the command output window. Messages output with displayError() are prefixed with "Error:". Another option isdisplayWarning() which would prefix the message with "Warning:".

    MStatus CommandExample::doIt( const MArgList& args)
    //
    //    Description:
    //        implements the MEL CommandExample command.
    //
    //    Arguments:
    //        args - the argument list that was passes to the command from MEL
    //
    //    Return Value:
    //        MS::kSuccess - command succeeded
    //        MS::kFailure - command failed (returning this value will cause the 
    //                     MEL script that is being run to terminate unless the
    //                     error is caught using a "catch" statement.
    //
    {
        MStatus stat = MS::kSuccess;
        //parse the arguments
        for(int i=0; i < args.length(); i ++){
            if(MString("-p") ==  args.asString(i, &stat) 
                && MS::kSuccess == stat){
                    double tmp = args.asDouble(++i, &stat);
                    if(MS::kSuccess == stat){
                        pitch = tmp;
                    }
            }
            else if(MString("-r") == args.asString(i, &stat)
                    &&MS::kSuccess == stat){
                double tmp = args.asDouble(++i, &stat);
                if(MS::kSuccess == stat)
                    radius = tmp;
            }
            else{
                MString msg = "Invalid flag";
                msg += args.asString(i);
                displayError( msg );
                return MS::kFailure;
            }
        }
    
        //get the first selected curve from the selection list
        MSelectionList slist;
        MGlobal::getActiveSelectionList( slist );
        MItSelectionList list(slist, MFn::kNurbsCurve, &stat);
        if(MS::kSuccess != stat){
            displayError("could not create selection list iterator");
            return stat;
        }
        if(list.isDone()){
            displayError("no curve selected");
            return MS::kFailure;
        }
    
        MObject component;
        list.getDagPath(fDagPath, component);
    
    
        return redoIt();
    }

    一旦所有需要的数据都get到了之后,就调用redoIt()函数。

     The doIt() method could perform the necessary actions itself, but these actions are always identical to those performed by redoIt() so, having doIt() call redoIt() reduces code duplication.

     Commands can also return results to MEL. This is done using the set of overloaded "setResult" and "appendToResult" methods inherited from MPxCommand

    Unless otherwise specified, all API methods use Maya internal units—cm and radians.

     

  • 相关阅读:
    149. Max Points on a Line(js)
    148. Sort List(js)
    147. Insertion Sort List(js)
    146. LRU Cache(js)
    145. Binary Tree Postorder Traversal(js)
    144. Binary Tree Preorder Traversal(js)
    143. Reorder List(js)
    142. Linked List Cycle II(js)
    141. Linked List Cycle(js)
    140. Word Break II(js)
  • 原文地址:https://www.cnblogs.com/bubbler/p/5155786.html
Copyright © 2020-2023  润新知