Find material of selected mesh in RenderLayers in python

Find material of selected mesh in RenderLayers in python

manue154
Enthusiast Enthusiast
1,435 Views
0 Replies
Message 1 of 1

Find material of selected mesh in RenderLayers in python

manue154
Enthusiast
Enthusiast

I’m trying to get the material name of a selected mesh in each renderLayers it is added,

without switching render layers.

However, I get this error: TypeError: object of type ‘RenderLayer’ has no len()

Thanks for any help because i’ve done many tests without any success …

Code to get material from object:

import pymel.core as pm
import maya.cmds as cmds
# Create a dictionary for {"object1": ["material1", "material2"]}
objectToMaterials = {}
for obj in pm.ls(sl=1):
    for layer in obj.listConnections(type="renderLayer"):
        print 'object: %s  layer: %s' % (obj, layer )  # result:     object: pSphere1  layer: layer3
        allChildren = cmds.listRelatives(ad=1)
        for eachChild in allChildren:
            if cmds.objectType(eachChild)=="mesh":
                eachChild = cmds.listRelatives(eachChild, parent=1)
                print eachChild # result:     [u'pSphere1']
                print layer  # result:      layer3
                cmds.editRenderLayerGlobals(crl=layer)  # result:   TypeError: object of type 'RenderLayer' has no len() # 
                # Get the shader groups attached to this particular object
                shaderGroups = cmds.listConnections(cmds.listHistory(eachChild))
                if shaderGroups is not None:
                    # Get the material attached to the shader group
                    materials = [x for x in cmds.ls(cmds.listConnections(shaderGroups), materials=1)]       
                objectToMaterials.setdefault(eachChild[0], []).append(eachRenderLayer)
                objectToMaterials.setdefault(eachChild[0], []).append(materials[0])
print(objectToMaterials)

 

0 Likes
1,436 Views
0 Replies
Replies (0)