Maya API How-To #19

Back · Previous · Next Maya

How do I execute MEL commands from the API? And why would I want or need to execute MEL from the API?

Yes, it is possible to execute MEL commands from the API. Sometimes it's a convenience, but there are times you have no other choice. There are two methods in the API for this purpose:

MGlobal::executeCommand()
MDGModifier::commandToExecute()

The difference between these two is how the Undo is handled. Refer to this useful reply from Dean Edmonds in the Maya Developer's Mailing List:

http://www.highend3d.com/maya/devarchive/sp.3d?mail_id=3583

To prŠ¹cis: You can use MDGModifier::commandToExecute() to "stack" MEL commands so that a series is controllable through a single undo/redo event. Using MGlobal::executeCommand(), each MEL command creates an additional undo/redo event. If you are executing many small or iterative commands it would be inconvenient for the user to require multiple undos, and filling up the queue means that it would be impossible for the user to completely undo to a previous state.

There are a few cases that spring to mind where calling MEL is the only way out:

1. User Interface

Maya's UI control is handled exclusively via MEL. The API offers no access to generating, editing or querying windows, layouts or controls. For the most part you'll want to create separate MEL scripts to manage your UI, and limit the API involvement to simply querying states and such. In other words, don't try to build and manage a UI using MGlobal::executeCommand() calls.

2. Preferences

There is no API equivalent to the "optionVar" MEL command. If you want to query or set user preferences from within your plug-in you'll need to do via MEL.

3. componentListData

Here's one interesting attribute type. From what I've been able to determine, a 'componentListData' attribute type can only be queried via the API, and can only be set via MEL.

In the sake of convenience, it typically boils down to: "How much work are you willing to do?" Take the ‘polyMapCut’ command for example. Yes, it's possible to replicate its behavior using only API calls - provided you want to deal with introducing contruction history, generating the intermediate mesh, wiring up all the plug dependencies, and populating the polymapCut node (which, incidentally, uses a "componentListData" attribute) to describe the edges you wish to cut.

Or you can simply call, for example:

polyMapCut -ch 1 pPlane1.e[18];

And let Maya take care of all that.

Doing it the "hard way" provided me the opportunity to create a convenient library for introducting construction history on a mesh, and for managing undo/redo - both of which were handy when used on other projects. But if I was building a simple "one off" tool for an artist, I'd likely simply call MEL to do it.


Acknowledgements

01 Sep 2003


Related How-To's