View Single Post
# 3 13-01-2003 , 05:51 AM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
Well, it can get a little complicated. It's not that bad to find the shading group (or groups) to which an object or its components belong. For example, if you have an object called nurbsSphere1 with a shape node called nurbsSphereShape1, this will show you the connected shading groups:

listConnections -type shadingEngine nurbsSphereShape1

Once you have the shading group names, you can figure out from that what materials are assigned, by listing objects that are connected to the shading group's surfaceShader, displacementShader, and volumeShader attributes. Usually the surface shader is the one you're most concerned with, so let's just look at that:

For this code snippet, I'll assume that you've selected the object you're curious about AND that it only has one material assigned to the entire object:


proc string getSurfaceShader (string $objName) {

// This assumes you want ONLY the shape node at the selected level of the
// hierarchy, for the object whose transform name is passed in as the argument.

string $myShapeNode[] = `listRelatives -children -shapes $objName`;
string $mySGs[] = `listConnections -type shadingEngine $myShapeNode[0]`;
string $surfaceShader[] = `listConnections ($mySGs[0] + ".surfaceShader")`;
return $surfaceShader[0];
}


So, to see the surface shader on nurbsSphere1, type

getSurfaceShader("nurbsSphere1");

This isn't a general solution because different components of an object (such as individual polys in a mesh object) can have different shading groups assigned, and this would only give you the shader for the first one. Also, if for some reason you have multiple shapes under one transform (weird, but could happen) this will only give you one of their surface shaders.

-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com