RifPlugin
Parsing Input Args


return to main index



Introduction

The way in which the input args are received by a rif plugin depends on whether they are quoted or not quoted. For example, figure 1 shows the "Rif Args" text field with three characters (a,b and c). The info message from the rif (ParseArgs.cpp - listing 1) reports that only one arg was received by the plugin.



Figure 1


However, when the input characters are in quotations, as shown next, the info message reports the arg count is 3.



Figure 2


The arg counts for un-quoted and quoted inputs are the same when using Cutter's Rif Tool.



Figure 3


When testing how a rif should handle it's input args it is often more convenient to use Cutter's Rif Tool than Maya for two reasons. Firstly, once a rif has been used with Maya it is "stuck" in memory - changing the cpp code and re-building the .so file will have no effect in Maya. Secondly, rather than using "RixMessages" the standard output stream (std::cout) can be used to output diagnostic messages.


Listing 1 (ParseArgs.cpp)


#include <RifPlugin.h>
#include <iostream>
#include <RixInterfaces.h>
#include <rx.h>
  
class ParseArgs : public RifPlugin {
    public:
             ParseArgs();
    virtual ~ParseArgs() { }    
    virtual RifFilter &GetFilter() { return m_filter; }
    static void infoMsg(const char *str);
    
    private:
        RifFilter   m_filter;
    };
  
RifPlugin *RifPluginManufacture(int argc, char **argv) {
    char str[256];
    sprintf(str, "Args count: %d", argc);
    ParseArgs::infoMsg(str);
  
    std::cout << "Args count: " << argc << "\n";
    return new ParseArgs();
    }
    
ParseArgs::ParseArgs() { 
    
    }
  
void ParseArgs::infoMsg(const char *str) {
    RixContext    *rixContext = RxGetRixContext();
    RixMessages   *msg = (RixMessages*) rixContext->GetRixInterface(k_RixMessages);
    msg->InfoAlways(str);
    }


Arg Parsing

If a rif "expects" to receive mulitple args it cannot be assumed an artist using the rif in Maya will specify the inputs within quotations. For example, if two values are passed to a rif as an unquoted string the arg count will be one and, as such, it will be necessary to tokenize the single arg into separate values. The easiest approach is to tokenize the arg on white space. On the other hand, if the inputs are specified as a quoted string the arg count will correspond to the number of input args and no tokenization will be required.

Listing 1 provides a snippet of code that illustrates one way that arg(s) can be parsed.


Listing 1 (Mesh2Blobby.cpp)


RifPlugin *RifPluginManufacture(int argc, char **argv) {
    std::vector <std::string> inputs;
    if(argc == 1) {
        // Tokenize the input into separate strings and add them
        // to a list (vector) of inputs.
        char *ptr = strtok(*argv, " ");
        while(ptr) {
            inputs.push_back(std::string(ptr));
            ptr = strtok(NULL, " ");
            }
        }
    else // multiple inputs
        for(int n = 0; n < argc; n++) 
            inputs.push_back(std::string(argv[n]));
            
    // Process the inputs. For example, suppose we
    // expect to receive an integer followed by a float. 
    RtInt     ival = 1;
    RtFloat fval = 2.0;    
    if(inputs.size() == 2) {
        ival = std::stoi(inputs[0]); // string to int
        fval = std::stod(inputs[1]); // string to float
        }
    return new ParseArgs(ival, fval);
    }





© 2002- Malcolm Kesson. All rights reserved.