How do i list the shaders in a selected object?

Guys, i’m really needing a quick help here

i have a sphere with a shader attached to it.

I’m looking for a python command who displays the shader name of the selected object

could u guys help me?

thanks in advance!

There isn’t one maya command that does that. You need to figure out the name of the shape under the transform and then figure out to which shadingEngine the shape is connected. Then you figure out which material is connected to either the surfaceShader or the miMaterialShader plug or one of the other plugs, it all depends.

The quick and dirty way of doing it would be

sel = maya.cmds.ls(selection=True)
shape = maya.cmds.listRelatives ( sel, shapes=True )
shadingEngine = maya.cmds.listConnections (shape, source=False, destination=True)
material = maya.cmds.listConnections (shadingEngine, source=True, destination=False)
attr = shadingEngine[0]+‘.surfaceShader’
plug = maya.cmds.connectionInfo( attr, sourceFromDestination=True)

oh thanks a lot dude! it worked! =D

Here is the another way of getting  materials of selected objects

First select the objects and run these line from python editor

cmds.hyperShade(“”,smn = True)
print cmds.ls(sl = True)

Well yes however be careful with using this, it has a tendency to backfire. See the thing is hyperShade works on a gui item and gui items arent guaranteed to be loaded in all maya cases. This may lead you to a very hard to debug problem down the line when someone relys on this in a batch or rendering context.

Hi all,

I am wondering if per chance anyone knows how to get the materials actually assigned to component faces? i.e. if you have a cube with faces 1-3 assigned to shader_01 and faces 4-6 assigned to shader_02 …how can I query my cube “using python” to return all the faces with the various assigned shaders?

Cheers