Tuesday, June 18, 2013

Finding the shader for a face component

You can find the shader group attached to a face component using python like this:

import maya.cmds as cmds

def getFaceShader(face):  
     ''' Return the shading group for the given face or None '''  
     # get all shading groups  
     allSG = cmds.ls(type='shadingEngine')  
     for shadingGrp in allSG:  
         # check if the face is in the set for the shading group  
         if cmds.sets(face, isMember=shadingGrp):  
             return shadingGrp  
     # no shading group was found for the face  
     return None  
To use this you need to give it a face component. For example, select a face on a polygon object and do this:
selectedFace = cmds.ls(selection=True)[0]
shadingGroup = getFaceShader(selectedFace)
After you retrieve the shading group, if you want the actual shader (ex: blinn), you just need to do:
shader = cmds.listConnections("%s.surfaceShader" % shadingGroup, source=True)
Once you've extracted the shader from a component, its really easy to transfer it over to another selected polygon or faces. You can actually do this by adding selected objects to the shading group set. If the items are in another set which is in the same partition as the given set, the items will be removed from the other set in order to keep the sets in the partition mutually exclusive with respect to membership. This is the code to do it:
selected = cmds.ls(sl=True)
if selected:
     cmds.sets(e=True, forceElement=shadingGroup)

0 comments:

Post a Comment