Accessing Material Data [MEL]



 

 

 

Generally a material "phong1" connects into a shadingGroup node "phong1SG". Any surfaces using that material will then plug into the shadingGroup node.

To find a material applied to a nurbs surface, you need to traverse that set of connections. Polygonal meshes on the other hand, have a method in MFnMesh that returns the shaders associated with all of it's faces. See the mesh section for more details.

 

 

 

 

Finding all Materials in the Scene

All material types in maya are derived from MFn::kLambert so we can iterate through a list of those to get all of the materials in the scene.

The materials usually contain a set of colours that describe the ambient, diffuse and specular colour channels.

These colour channels may also be attached to a file texture node. The OutputColor function outputs both texturing and the actual colour values.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


#include<maya/MFnLambertShader.h>
#include<maya/MFnBlinnShader.h>
#include<maya/MFnPhongShader.h>
#include<maya/MItDependencyNodes.h>

void OutputMaterials()
{
  // iterate through the mesh nodes in the scene
MItDependencyNodes itDep(MFn::kLambert);

// we have to keep iterating until we get through
// all of the nodes in the scene
//

while (!itDep.isDone())
{
  switch(itDep.item().apiType()) {
  // if found phong shader
case MFn::kPhong:
{
MFnPhongShader fn( itDep.item() );
cout<<"Phong "<<fn.name().asChar()<<"\n";
OutputColor(fn,"ambientColor");
OutputColor(fn,"color");
OutputColor(fn,"specularColor");
OutputColor(fn,"incandescence");
OutputColor(fn,"transparency");
cout<<"\tcos "<<fn.cosPower()<< endl;
OutputBumpMap(itDep.item());
OutputEnvMap(itDep.item());
}
break;
// if found lambert shader
case MFn::kLambert:
{
MFnLambertShader fn( itDep.item() );
cout<<"Lambert "<<fn.name().asChar()<<"\n";
OutputColor(fn,"ambientColor");
OutputColor(fn,"color");
OutputColor(fn,"incandescence");
OutputColor(fn,"transparency");
OutputBumpMap(itDep.item());
OutputEnvMap(itDep.item());
}
break;
// if found blinn shader
case MFn::kBlinn:
{
MFnBlinnShader fnBlinn( itDep.item() );
cout<<"Blinn "<<fn.name().asChar()<<"\n";
OutputColor(fn,"ambientColor");
OutputColor(fn,"color");
OutputColor(fn,"specularColor");
OutputColor(fn,"incandescence");
OutputColor(fn,"transparency");
cout <<"\teccentricity "
<<fn.eccentricity()<< endl;
cout << "\tspecularRollOff "
<< fn.specularRollOff()<< endl;
OutputBumpMap(itDep.item());
OutputEnvMap(itDep.item());
}
break;
default:
break;

}
itDep.next();

}

}



 

Accessing A Material Colour Channel

 

A specific colour within a material, ie ambientColor, color, specularColor etc can be accessed generically.

 

Generally they will have r,g,b,a channels, and the colour may be attached to an MFn::kFileTexture.

 

This requires us to query the attribute MPlug for any connections it may have.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


#include<maya/MPlugArray.h>
#include<maya/MFnDependencyNode.h>

bool OutputColor(MFnDependencyNode& fn,const char* name)
{
  MPlug p;

MString r = name;
r += "R";
MString g = name;
g += "G";
MString b = name;
b += "B";
MString a = name;
a += "A";

// get the color value
MColor color;

// get a plug to the attribute
p = fn.findPlug(r.c_str());
p.getValue(color.r);
p = fn.findPlug(g.c_str());
p.getValue(color.g);
p = fn.findPlug(b.c_str());
p.getValue(color.b);
p = fn.findPlug(a.c_str());
p.getValue(color.a);
p = fn.findPlug(name.c_str());

// will hold the txture node name
MString texname;

// get plugs connected to colour attribute
MPlugArray plugs;
p.connectedTo(plugs,true,false);

// see if any file textures are present
for(int i=0;i!=plugs.length();++i)
{
  // if file texture found
if(plugs[i].node().apiType() == MFn::kFileTexture)
{
  // bind a function set to it ....
MFnDependencyNode fnDep(plugs[i].node());

// to get the node name
texname = fnDep.
name();

// stop looping
break;

}

}
if( name == "color" &&
color.r <0.01 &&
color.g < 0.01 &&
color.b < 0.01)

{
  color.r=color.g=color.b=0.6f;

}
// output the name, color and texture ID
cout << "\t" << name << " "
<< color.r << " "
<< color.g << " "
<< color.b << " "
<< color.a << " tex= "
<< texname.asChar() << "\n";

return true;

}

 

 

 

What Next?

Texture Data

Mesh Surfaces

Nurbs Surfaces


Animation Curves

index

Rob Bateman [2004]


[HOME] [MEL] [API]