Accessing Environment Maps [MEL]

 

 

 

In Maya there are two types of environment mappings available; Sphere maps and cube maps. Both of these node types are usually connected to the reflected color attribute of a material, and they will both have a 3D texture placement node to center and orientate the mapping. The 3D placement node can be gathered in exactly the same way as a transform, though you may want one or two extra attributes from the texture placement. Consult the node and attribute reference within the Maya docs for more information.

 

 


 

 

The MFn::kEnvSphere nodes are used within maya to hold information about a sphere mapping. These are usually connected to a single MFn::kFileTexture node.

 

 

 

 

The MFn::kEnvCube nodes are used within maya to hold information about a cube mapping. These are usually connected to six individual MFn::kFileTexture nodes representing each face of the cube.

 

 

 

 

Accessing A file texture node Connected to an Attribute

In order to reduce the amount of work needed for both cube and sphere maps, we can make use of a simple utility function that returns the name of the file texture node connected to the specified attribute.

The oEnv paremeter is the environment map node you want to query the texture from and name is the name of the attribute which may have a texture node connected.

This simply uses methods described before to access a plug to the specified attribute and then queries any connections to it.

 

 

 

 

 

 

 

 

 


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

MString GetTexture(MObject& oEnv,MString& name)
{
 

// attach a function set to the environment map node
MFnDependencyNode fn(oEnv);

// get the image plug on the env map node
MPlug imgPlug = fn.findPlug(name);

MPlugArray img_connections;

// get the connections to the image attribute
// to see if we have any file textures attached

imgPlug.connectedTo(img_connections,true,false);

for(unsigned int j=0;j<img_connections.length();++j)
{

 

MObject oTex = img_connections[j].node();

// if a texture is found
if (oTex.apiType() == MFn::kFileTexture)
{

 

// attach a function set to the file texture
MFnDependencyNode fnTex(oTex);

// output the environment map as a sphere map
return fnTex.name() ;

}

}
return "none";

}

 

 

Determining The 3D Placement Matrix

When using a sphere map, or cube map within Maya you can place and scale the projection using a 3D texture placement node.

In an API such as openGL you can re-create this using the texture matrix stack. As far as extracting the data goes, you can start by just treating it as a transform node.

We will also need to find the texture placement node which should be connected to the "placementMatrix" attribute.

 

 

 

 

 

 


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

MObject GetTexturePlacement(MObject EnvMap)
{
 

// attach the function set to the object
MFnDependencyNode
fn(EnvMap);

// get a plug to the 3D placement matrix
MPlug
pm = fn.findPlug("placementMatrix");

// now we need to output the transform ID of the
// placement node that determines the size,
// position and orientation of the mapping

MPlugArray Connections;
pm.connectedTo(Connections,true,false);

// loop through all of the connections

for(unsigned int j=0;j<Connections.length();++j)
{

  MObject obj = Connections[j].node();
if(obj.apiType() == MFn::kPlace3dTexture)
{
  // return the 3D placement node
return obj;

}

}

return MObject::kNullObj;

}

 

 

Outputting an Environment Map

Given any material, we want to know if it is using a sphere or cube map. To do this we can look at the reflected colour attribute on the material and check to see if it is attached to either an MFn::kEnvSphere or MFn::kEnvCube node.

If it is, we can write out the environment map details.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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

void OutputEnvMap(MObject Material)
{
 

// attach the function set to the object
MFnDependencyNode
fn(Material);

// Environment maps are usually plugged into the
// reflected colour attribute, so we will need to
// get a plug to the attribute

MPlug
rc = fn.findPlug("rc");

// now we need to output the transform ID of the
// placement node that determines the size,
// position and orientation of the mapping

MPlugArray Connections;
rc.connectedTo(Connections,true,false);

// loop through all of the connections

for(unsigned int j=0;j<Connections.length();++j)
{

  MObject obj = Connections[j].node();
if(obj.apiType() == MFn::kEnvSphere)
{
 

// return the 3D placement node
cout << "EnvMap sphere\n\tfile ";
cout
<< GetTexture(obj,"image") << endl;

MObject tp = GetTexturePlacement(obj);
MFnDependencyNode
fnp( tp );

// output the texture placement
cout
<<"\ntexplacement ";
cout<<fnp.name().asChar()<< endl;

return;


} else
if(obj.apiType() == MFn::kEnvCube)
{
 

// return the 3D placement node
cout << "EnvMap sphere\n";
cout
<<"\n\tleft"<<GetTexture(obj,"left");
cout
<<"\n\tright"<<GetTexture(obj,"right");
cout
<<"\n\ttop"<<GetTexture(obj,"top");
cout
<<"\n\tbottom"<<GetTexture(obj,"bottom");
cout
<<"\n\tfront"<<GetTexture(obj,"front");
cout
<<"\n\tback"<<GetTexture(obj,"back");

MObject tp = GetTexturePlacement(obj);
MFnDependencyNode
fnp( tp );

// output the texture placement
cout
<<"\ntexplacement ";
cout<<fnp.name().asChar()<< endl;

return;


}

}

cout << "EnvMap none\n";

}




 

 

What Next?

Material Data

Texture Data

Bump Maps

Transforms

Anim Curves

index

Rob Bateman [2004]


[HOME] [MEL] [API]