yan yan - 2 months ago 9
Python Question

Getting the name of the mesh that shader was assigned to

How do I get the name of the mesh that the shader was assigned to, using python?

Example, lambert02 --> AreaA_01_geo, lambert03 --> AreaA_03_geo, lambert04 --> AreaA_04_geo

I tried using

Shader = cmds.ls(type = 'surfaceShader')
for i in Shader:
con = mc.listConnections('%s.outColor' % i)
name = cmds.listConnections(Shader, type="mesh")


But I was unable to get anything out of
name
variable

Answer

The shader is connected to one or more shading sets which house the assignments. So this is not a 1:1 assignment but rather one to many, and then one to many again (granted you don't see it that often). Please note that you use 2 namespaces when you only should need one.

import maya.cmds as mc

Shader = mc.ls(type = 'surfaceShader')
for i in Shader:
    con = mc.listConnections('%s.outColor' % i)
    names = mc.listConnections(con, type="mesh")
    print i, "->", ", ".join(names)
Comments