Using MGlobal



 

 

MGlobal is a class that provides you with a few global controls of maya. Executing mel scripts; sending the user messages; selecting objects and setting the current frame can all be performed with MGlobal.

 

 

 

 

Executing a mel script

You can execute any mel commands you want using MGlobal::executeCommand.

You can also use MGlobal::sourceFile to execute an entire script

 



#include <maya/MGlobal.h>

// execute the mel command to make a sphere
MGlobal::executeCommand( "sphere" );

// execute the specified mel script
MGlobal::sourceFile( "C:\\scripts\\script.mel" );

 

 

Displaying messages to the User

MGlobal also offers you ways of notifying the user of errors, warnings and/or general information. These messages appear in the output pane of the maya script editor.

 

 



#include <maya/MGlobal.h>

// display some info in the script editor
MGlobal::displayInfo( "some info" );

// display some error in the script editor
MGlobal::displayError( "some error" );

// display some warning in the script editor
MGlobal::displayWarning( "some warning" );

 

 

 

Accessing currently selected items

MGlobal provides you with a mechanism for selecting objects, and querying currently selected items. The code on the right prints all of the currently selected objects.

 

 

 

 

 

 

 


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

// This will hold the currently selected items
MSelectionList SelList;

// get a list of the currently selected items from MGlobal
MGlobal::getActiveSelectionList( SelList );

// loop through each selected item
for(int i=0;i<SelList.length();++i) {
  // will hold this item in the selection list
MObject
obj;

// get the i'th selected item
SelList.getDependNode(i,obj);

// attach a function set to it

MFnDependencyNode fn(obj);

// print the name of the selected node

cout << fn.name().asChar() << endl;

}



 

 

What Next?

FileIO In Maya

Traversing The Scenegraph

Accessing Attributes

Accessing Node Connections

index

Rob Bateman [2004]

[HOME] [MEL] [API]