MEL How-To #92

Back · Previous · Next Maya

How do I determine which Material or File Texture is associated with a Shading Group?

Trace upstream from the Shading Group to the Material then to the File Texture. To get the name of the image file assigned to the File Texture, query the file node's '.fileTextureName' attribute.

// ////////////////////////////////////////////////////////////////////
//  getMaterialFromSG
//
// Description: Returns the Material node feeing the '.surfaceShader'
//  attribute of the specified Shading Group node (shadingEngine).

proc string getMaterialFromSG( string $SG )
{
  string $material = "";

  if ( "shadingEngine" == `nodeType $SG` &&
    `connectionInfo -id ( $SG + ".surfaceShader" )` )
  {
    $material = rootNode( `connectionInfo -sfd
      ( $SG + ".surfaceShader" )` );
  }

  return $material;
}
// ////////////////////////////////////////////////////////
//   getTextureFromMaterial
//
// Description: Returns the File Texture node feeding the '.color'
//   attribute of the specified Material node

proc string getTextureFromMaterial( string $material )
{
  string $texture = "";

  string $class[] = getClassification( `nodeType $material` );

  if ( "shader/surface" == $class[0] && `connectionInfo -id
       ( $material + ".color" )` )
  {
    $texture = rootNode( `connectionInfo -sfd ( $material + ".color" )` );
  }

  return $texture;
}

Related How-To's

Friday, September 14, 2001