When
writing plug-ins for Maya, there are a number of different
proxy classes you can inherit from. These classes all begin
with the MPx prefix, ie, MPxFileTranslator,
MPxCommand, MPxNode etc. Within any one
plugin, you may register one or more of these types with
Maya.

I would generally recommend only using one plugin for your
full maya toolset - it makes it much easier to distribute
toolset updates to your Art Department. Another un-avoidable
aspect of using the Maya API, is that at some point, you
will end up requiring a few mel
scripts. These are usually required to create any custom
user interfaces that may be needed, or simply provide custom
menus or user interfaces.

In
this tutorial we will focus on creating our own mel functions,
and how mel interfaces with the rest of maya. We will then
proceed to make use of these mel functions to create an
automated graphics build process.

Essentially,
adding our own mel function into Maya involves deriving
our own class, from the proxy class MPxCommand.
This contains a virtual function doIt
which we need to overload to perform our required actions.
This may seem straight-forward (infact it is),
however there is an even easier way to create a simple function
- we can use MSimple.h.

// we can actually execute mel script
by using MGlobal::executeCommand. In
// this case all out function does is execute the mel script
call to create
// a sphere.
//
stat = MGlobal::executeCommand("sphere");

// check for any errors that occuredif(stat==MS::kSuccess)
{

//
send a nice result message back to the usersetResult("The
sphere is alive!");

}else {

//
oooh dear, something went wrong.displayError("[ERROR]
could not create a sphere");

}return stat;

}

The
DeclareSimpleCommand macro essentually
takes away a lot of the hassle of setting up a simple mel
function, however in practical terms it is often only useful
in 'quickfix' situations since it only allows a single mel
function to be registered in each plugin. Therefore, as
mentioned earlier, we have to derive a class from MPxCommand.

#include
<maya/MPxCommand.h>

class
lessSimpleCommand
: public MPxCommand
{public:

lessSimpleCommand()
{}
~lessSimpleCommand(){}

// this function is called whenever
we need to "action" the functionvirtual MStatusdoIt(
const MArgList& ) {

setResult("lessSimpleCommand
Called\n");return MS::kSuccess;

}

// this function creates an instance
of the functionstatic void* creator()
{

return
new lessSimpleCommand;

}

};

In
Addition, we also have to export two functions, one to initialise
the plugin, the other to un-initialise it. These functions
should use the MFnPlugin function set to register and deregister
any mel commands, nodes, file translators etc.For example,

#include
<maya/MFnPlugin.h>

// under Win32 we have to export the functions
for the dll. Under linux you
// simply need the -shared flag with gcc.
//#ifdef WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

//--------------------------------------------------------------------------
/// this method is called when the plug-in is loaded into Maya.
It registers
/// all of the services that this plug-in provides Maya with.
/// \param obj - a handle to the plug-in object (use MFnPlugin
to access it)
///EXPORT MStatus initializePlugin(
MObject obj )
{

//--------------------------------------------------------------------------
/// \brief this method is called when the plug-in is unloaded
from Maya. It
/// deregisters all of the services that it was providing.
/// \param obj - a handle to the plug-in object (use MFnPlugin
to access it)
///EXPORT MStatus uninitializePlugin(
MObject obj )
{