MPxContextCommand
 
 
 

The MPxContextCommand is the class used to define the special command for creating contexts. Context commands are similar to regular commands in that they can be executed from the command line and put into MEL scripts. They can have edit and query options which modify the properties of the context. They create an instance of the context and give it to Maya. Context commands are not undoable.

Creating a context command

The following is the implementation of the context command used to create the marquee context described previously.

marqueeContextCmd::marqueeContextCmd() {}

This method is used by Maya to create an instance of the context.

MPxContext* marqueeContextCmd::makeObj()
{
    return new marqueeContext();
}
void* marqueeContextCmd::creator()
{
    return new marqueeContextCmd;
}
MStatus initializePlugin( MObject obj )
{
    MStatus status;
    MFnPlugin plugin( obj, "Autodesk", "1.0", "Any");
    status = plugin.registerContextCommand( \
    "marqueeToolContext", marqueeContextCmd::creator );

The context command must be registered, but in this case MFnPlugin::registerContextCommand() is used rather than MFnPlugin::registerCommand().

    return status;
}
MStatus uninitializePlugin( MObject obj )
{
    MStatus status;
    MFnPlugin plugin( obj );
    status = plugin.deregisterContextCommand( \
        "marqueeToolContext" );

MFnPlugin::deregisterContextCommand() is likewise used to deregister the context command.

    return status;
}

And that’s all that’s necessary to create a simple context.

Adding a context command to the Maya shelf

There are two ways to "activate" or make your context the current context in Maya. The first is through the use of the setToolTo command. This command takes the name of a context (tool) and makes it the current context.

A second method is by making an icon to represent your context and putting it in the Maya tool shelf. The Maya tool shelf can store two kinds of buttons, command buttons and tool buttons. When the tool is activated, its icon is displayed next to the standard Maya tools in the toolbar.

The following is a set of MEL commands you can use to create a context and tool button for the context.

marqueeToolContext marqueeToolContext1;
setParent Shelf1;
toolButton -cl toolCluster
 -t marqueeToolContext1
 -i1 "marqueeTool.xpm" marqueeTool1;

This MEL code instantiates an instance of the marqueeToolContext and adds it to the "Common" tools shelf.

marqueeTool.xpm, the icon for the tool, must be in the XBMLANGPATH to be found and added to the UI. If it is not found, a blank spot will appear on the shelf, but the tool will still be usable.

This code could either be sourced by hand from the MEL command window, or it could be invoked with MGlobal::sourceFile() in the initializePlugin() method of the plug-in.