Adding Custom mel Functions into Maya


 
 

 

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.

[source project]

 
 

 

 


#include <maya/MSimple.h>
#include<maya/MGlobal.h>

// this declares our mel command
DeclareSimpleCommand
( SimpleCommand, "A simple mel function example", "4.0");

// we just need to provide the doIt() function
MStatus SimpleCommand
::doIt( const MArgList& args ) {

 

// the return status
MStatus stat = MS::kSuccess;

// 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 occured
if(stat==MS::kSuccess) {

  // send a nice result message back to the user
setResult("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 function
virtual MStatus doIt( const MArgList& ) {

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

}

// this function creates an instance of the function
static 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 )
{

 

// error code
MStatus status;

// attach a plugin function set to the plugin handle
MFnPlugin plugin( obj, "RobTheBloke", "1.0", "Any");

// register our command with maya
status = plugin.registerCommand( "lessSimpleCommand", lessSimpleCommand::creator );

// check for any errors
if (status != MS::kSuccess) {

  status.perror("registerCommand");
return status;

}

return status;

}

//--------------------------------------------------------------------------
/// \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 )
{

 

// error code
MStatus status;

// attach a plugin function set to the plugin handle
MFnPlugin plugin( obj );

// de-register our command from maya
status = plugin.deregisterCommand( "lessSimpleCommand" );

// check for an error
if (status != MS::kSuccess) {

  status.perror("deregisterCommand");
return status;

}

return status;

}

 


 

 

What Next?

A JavaDoc System for Maya

Automating the Graphics Build using mel

Writine a Command Line Exporter

Writing A Plugin File Translator

index

Rob Bateman [2004]


[HOME] [MEL] [API]