Scene Query.cpp

ATTENTION: THIS PAGE REFERS TO AN INTERNAL LUXOLOGY FILE THAT HAS NOT YET BEEN RELEASED TO THE SDK. THIS PAGE WILL PROBABLY BE HELPFUL IN UNDERSTANDING THE SDK REGARDLESS BUT YMMV.

Scene_Query.cpp is a basic example plugin. This wiki page is intended as a walkthrough of the code in order to help you better understand the SDK.

When installed, Scene_Query adds two plugins to modo:ResetScene and HardReset. ResetScene opens a file dialog from which you will choose a file; the current scene will then be closed and the chosen file loaded. HardReset will close the current scene then take the last selected file and load it. To change the file HardReset loads, run ResetScene and choose a different file.

__TOC__

Code Walkthrough

Definitions

To begin, we define an argument of the form ARGi_FILEPATH to have the index value of 0. So when 0 is being passed as an argument to functions later on, it is really referring to this variable. We also define a const int that forms the upper limit for the number of characters in the file path that we will be retrieving and the character array that will hold that file path.

        #define ARGi_FILEPATH   0
        using namespace std;
        const int MAXFILELENGTH = 1000;
char FILEPATH[MAXFILELENGTH];

Select_Scene class declaration

Here, Select_Scene is declared as a Command:_Server_basics#Helper_Classes with most of the basic methods associated with it. We are inheriting from basic commands because we want this plugin to perform state changes on modo.

        class Select_Scene : public CLxBasicCommand     //Select_Scene will open a file dialog and load the chosen scene
        {
            public:
                Select_Scene();
                int basic_CmdFlags()         LXx_OVERRIDE;

                bool basic_Enable      (CLxUser_Message &msg) LXx_OVERRIDE;

                void cmd_Interact() LXx_OVERRIDE;

                void cmd_Execute(unsigned int flags);
};

./Initialize_(index)

Here we set up the framework for the two commands. We indicate which interfaces both use, what type of server it is, and what the name will be.

        void initialize()
        {
            CLxGenericPolymorph     *srv;

            srv = new CLxPolymorph<X::Select_Scene>;
            srv->AddInterface (new CLxIfc_Command     <X::Select_Scene>);
            srv->AddInterface (new CLxIfc_Attributes  <X::Select_Scene>);
            srv->AddInterface (new CLxIfc_AttributesUI<X::Select_Scene>);
            lx::AddServer ("resetscene", srv);

            srv = new CLxPolymorph<X::Load_Action>;
            srv->AddInterface (new CLxIfc_Command     <X::Load_Action>);
            srv->AddInterface (new CLxIfc_Attributes  <X::Load_Action>);
            srv->AddInterface (new CLxIfc_AttributesUI<X::Load_Action>);
            lx::AddServer ("HardReset", srv);
}

Select_Scene constructor

The constructor adds an attribute to the command of type FILEPATH with the name FilePath. Next, its setFlags function set the index object in slot 0 equal to this attribute and allows the attribute to be queried. What adding an attribute ultimately does is create and argument that the user is queried for whenever the command is run.

        Select_Scene::Select_Scene()
        {
            dyna_Add("FilePath", LXsTYPE_FILEPATH); //queries the user for a FILEPATH object at startup-not same as above string

            basic_SetFlags( 0, LXfCMDARG_QUERY);
}

CmdFlags function

Next, we set the Command_(lx-command.hpp)#.287.29_SDK:_ILxCommand_interface flag.

        int
        Select_Scene::basic_CmdFlags()
        {
            return LXfCMD_MODEL;    //We'll be altering the internal state of modo, so we set the MODEL flag
}

basic_Enable function

Here we check for if the FILEPATH type argument has been set; if not, the user likely hit cancel, so we exit. If it has, we continue as normal

        bool
        Select_Scene::basic_Enable(CLxUser_Message &msg) LXx_OVERRIDE
        {
            if (!dyna_IsSet(ARGi_FILEPATH))
                return true;

            else
                return false;
}

./Command_(lx-command.hpp)#.2819.29_SDK:_ILxCommand_interface

Here, we set certain dialog values using the Command_(lx-command.hpp)#.28102.29_SDK:_ILxCommandService_interface command so that a file dialog is opened. We then perform a little housework that results in string with the value of the path to the file that was selected being put into the index 0.

        void
            Select_Scene::cmd_Interact()//Interact opens a file dialog and stores the given value in FILEPATH
        {
            CLxUser_CommandService  srv_cmd;
            srv_cmd.ExecuteArgString (-1, LXiCTAG_NULL,"dialog.setup fileOpen");    //-1 = LXiCMD_EXEC_WITH_PARENTS_FLAGS
            srv_cmd.ExecuteArgString (-1, LXiCTAG_NULL,"dialog.title {Load Scene}");
            srv_cmd.ExecuteArgString (-1, LXiCTAG_NULL,"dialog.fileType scene");
            srv_cmd.ExecuteArgString (-1, LXiCTAG_NULL,"dialog.open");

            CLxUser_Command             resCmd;
            CLxUser_ValueArray  va;
            LXtObjectID                       obj;
            unsigned int                n;

            srv_cmd.NewCommand (resCmd, "dialog.result");
            srv_cmd.QueryIndex (resCmd, 0, va);

            n = va.Count ();
               if (!n)
                     return;

             std::string                filename;

             va.String (0, filename);
             attr_SetString (0, filename.c_str ());
}

./Command_(lx-command.hpp)#.2828.29_SDK:_ILxCommand_interface

Finally, we use the Command_(lx-command.hpp)#.28102.29_SDK:_ILxCommandService_interface command to close the scene(using the ! before the command so that it will ignore pop-ups). We then put the value of the string in index 0 into the global character array FILEPATH. We then open a scene using the value of FILEPATH.

            void
        Select_Scene::cmd_Execute(unsigned int flags)//Execute takes FILEPATH and loads it
        {
            CLxUser_CommandService  cmd_srv;
            unsigned int execFlagsA = -1;
            string command = "!scene.close";

            cmd_srv.ExecuteArgString(execFlagsA, LXiCTAG_NULL, command.c_str());
            attr_GetString(0, FILEPATH, MAXFILELENGTH);
            command = "scene.open ";
            command = command+FILEPATH;
            cmd_srv.ExecuteArgString(execFlagsA, LXiCTAG_NULL, command.c_str());
}

Load_Action

This class is Select_Scene without a file dialog; the code is otherwise the same.