MEL How-To #43

Back · Previous · Next Maya

How do I get the assigned Shading Group for an object or face component?

To determine which Shading Group is assigned to an object, trace downstream of the object's shape node for all 'shadingEngine' nodes (a.k.a. Shading Groups).

// ////////////////////////////////////////////////////////////////////
//  getSGsFromShape
//
// Description: Returns an array of the Shading Groups (shadingEngine 
//  nodes) responsible for shading the specified shape node.

proc string[] getSGsFromShape( string $shape )
{
  string $shadingEngines[];

  if ( `objExists $shape` )
  {
    string $dest[] = `listConnections -destination true -source false
      -plugs false -type "shadingEngine" $shape`;

    // listConnections can return duplicates within its list.
    // The select below is a quick trick to avoid dupes in the 
    // returned array.
    if ( size( $dest ) )
    {
      string $select[] = `ls -sl`;
      select -r -ne $dest;
      $shadingEngines = `ls -sl`;
      select -r $select;
    }
  }

  return $shadingEngines;
}

Getting the Assigned Shading Group for a Face Component

Get a list of all Shading Groups in your scene and query the membership for each set to check if the target component is shaded by that shader.

proc string getFacetSG( string $facet )
// Input (string) - facet component to query (e.g. "pSphere1.f[0]")
// Result (string) - Shading Group shading facet (e.g. "lambert2SG")
{
  string $facetSG = "";

  // Get array of all Shading Groups
  string $shadingGroups[] = `ls -type shadingEngine`;

  for ( $shadingGroup in $shadingGroups )
  {
    // If this facet is a member of the shading set for this
    // Shading Group, tag this as the facet's shader
    if ( `sets -isMember $shadingGroup $facet` )
    {
      $facetSG = $shadingGroup;
      break;
    }
  }

  return $facetSG;
}

Related How-To's

Friday, September 14, 2001