In my scene there are a lot of instances of one object. Artists can assign different shaders to the instances. In the end I want to query the connected shader of each instance.
When querying the assigned shading networks I get a list of all of the shaders connected to all the instances. I know that maya links the instance to the shading group by using the “instObjGroups” attribute. But how can i find out which “instObjGroups”-ID refers to which instance? Or is there another way to query the shading network of a specific instance?
If the shaders are all assigned by object (not at the face level) you can use the sets command to query the shading groups and work backwards from there: that will give you the lists of all the objects grouped by shader. This will give you a dictionary with shaders as keys and a list of their contents as values:
assignments_by_shader = { sg: cmds.sets(sg, q=True) or [] for sg in cmds.ls(type='shadingEngine') }
print assignments_by_shader
to find the shaders on a particular instance you just need to do cmds.listConnections(type=‘shadingEngine’) on the instance. The connections are made by shapes, no transforms. So:
def assigned(object):
shape = cmds.listRelatives(s=True) or object
shaders = cmds.listConnections(shape, type='shadingEngine') or []
return shaders
[QUOTE=jumu;29312]In my scene there are a lot of instances of one object. Artists can assign different shaders to the instances. In the end I want to query the connected shader of each instance.
When querying the assigned shading networks I get a list of all of the shaders connected to all the instances. I know that maya links the instance to the shading group by using the “instObjGroups” attribute. But how can i find out which “instObjGroups”-ID refers to which instance? Or is there another way to query the shading network of a specific instance?[/QUOTE]
look at below:
import maya.api.OpenMaya as api
selections = api.MGlobal.getActiveSelectionList()
connect_info = []
if selections.length() != 0:
dagPath_first = selections.getDagPath(0)
node = dagPath_first.node()
if node.apiType() == api.MFn.kMesh:
fnDagNode = api.MFnDagNode( dagPath_first )
fnMesh = api.MFnMesh( node )
howManyInsts = fnDagNode.instanceCount(False)
for inst_id in range( howManyInsts ):
shadings, faceArray = fnMesh.getConnectedShaders( inst_id )
connect_info.append(
( map( lambda s : api.MFnDependencyNode(s).name(), shadings), \
tuple(faceArray) ) )
else:
print 'You need select a mesh!'
else:
print 'Nothing selected!'
if connect_info:
for idx, sg in enumerate( connect_info ):
print 'Instance {0} : {1} ->
{2}'.format( idx, sg[0], sg[1] )
else:
print 'Not any shader connections'