MPlugs and Attributes


 

 

In general you try to make use of specialised function sets whenever you need to access data within the Maya API. There is however the option of querying the data from specific attributes on a maya node.

Generally the workflow of Maya makes use of nodes, which can be connected together via their attributes. Due to this feature, the Maya API views all atrtributes as plugs. This means that when you access attributes directly, you will need to make use of the class MPlug. This class allows you to both query an attributes value, as well as finding any connections to the node.

This section simply deals with how to retrieve MPlugs from a Maya node, and then how to query the attributes value.

 

 

 

 

Querying a simple attribute

This example simply queries the x translation attribute (tx) from all transform nodes within the scene.

When trying to access an attribute, we can use the findPlug method of the MFnDependencyNode function set. This will simply return an MPlug to the requested attribute. Having retrieved the plug, we can use the getValue() methd to query the data contained.

As you will see, this process is more labourious than using a specialised function set. It does however mean that you can always access any attributes you need to.

 

 


#include<maya/MItDag.h>
#include<maya/MPlug.h>
#include<maya/MFnDependencyNode.h>

// create an iterator to go through all transforms
MItDag it(MItDag::kDepthFirst,MFn::kTransform);

while(!it.isDone())
{
 

// attach a function set to the transform
MFnDependencyNode fn(it.item());

// get the MPlug for the tx attribute

MPlug plug = fn.findPlug("tx");

// get the value from the plug

float tx;
plug.getValue(tx);

// move to next node

it.next();

}


 

 

Querying a Compound attribute

 

Attributes within Maya, can also be groupings of individual attributes (compound attributes). One such example would be the translate attribute of a transform node. This is composed of the individual attributes translateX, translateY and translateZ.

 

If we wish to query this data, we must use the child member function of MPlug to return a plug to the child attribute of the compound plug.

 

The example code here queries the three child plugs of translate for the x,y and z translation values.

 

 

 

 

 


#include<maya/MItDag.h>
#include<maya/MPlug.h>
#include<maya/MFnDependencyNode.h>

// create an iterator to go through all transforms
MItDag it(MItDag::kDepthFirst,MFn::kTransform);

while(!it.isDone())
{
 

// attach a function set to the transform
MFnDependencyNode fn(it.item());

// get the MPlug for the compound attribute

MPlug plug = fn.findPlug("translate");

// get the value from the plug

float tx=0,ty=0,tz=0;

if(plug.isCompound()) {

 

// get the plugs to the child attributes
MPlug
plug_x = plug.child(0);
MPlug plug_y = plug.child(1);
MPlug plug_z = plug.child(2);

// get the values from the child plugs
plug_x.
getValue( tx );
plug_y.getValue( ty );
plug_z.getValue( tz );

}

// print out translation
cout
<< fn.name().asChar() << "\t";
cout
<< tx << " " << ty << " " << tz << endl;

// move to next node

it.next();

}


 

 

Querying an array attribute


Attributes within Maya can also be stored as array attributes. For example, this code sample queries the vrts array attribute on a mesh. This is an array of compound attributes describing the vertex positions for a mesh.

 

The process involves first accessing the MPlug for the array attribute. We can then use this plug to determine the number of array elements, by using the numElements member function.

 

In order to access an array element, we can use the member function elementByPhysicalIndex(). This will return an MPlug to the specified array element.

 

Each array element in the vrts array, is a compound attribute type (x,y,z). We therefore need to retrieve plugs from the compound plug in order to get hold of the individual x,y and z components.

 

 

 

 


#include<maya/MItDag.h>
#include<maya/MPlug.h>
#include<maya/MFnDependencyNode.h>

// create an iterator to go through all transforms
MItDag it(MItDag::kDepthFirst,MFn::kMesh);

while(!it.isDone())
{
 

// attach a function set to the transform
MFnDependencyNode fn(it.item());

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

// get the MPlug for the compound attribute

MPlug Array = fn.findPlug("vrts");

for(int i=0;i<Array.numElements();++i) {

 

// get the MPlug for the i'th array element
MPlug Compound = Array.elementByPhysicalIndex(i);

// get the value from the plug

float x=0,y=0,z=0;

if(Compound.isCompound()) {

 

// get the plugs to the child attributes
MPlug
plug_x = Compound.child(0);
MPlug plug_y = Compound.child(1);
MPlug plug_z = Compound.child(2);

// get the values from the child plugs
plug_x.
getValue( x );
plug_y.getValue( y );
plug_z.getValue( z );

}

// print out vertex coord for this mesh
cout
<<"\t"<< x <<" "<< y <<" "<< z <<endl;

}

// move to next mesh
it.next();

}




 

 

What Next?

Traversing the Scene

How To Access Connections Between Attributes

index

Rob Bateman [2004]

[HOME] [MEL] [API]