View Full Version : Getting the attached materials of a mesh (python)

07 July 2012, 02:37 PM
Ive made a simple python script that queries varies information about a mesh (poly count, name etc) Id like to add oen that prints which materials are connected to the mesh.

I tried using and usign the 'mat' flag, but it just prints '[]'

any ideas what the simpest, cleanest way of doing this is?

07 July 2012, 02:50 PM
If you use the Graph Editor and look at the relationship between Shape Nodes, Shading Groups and Materials, you will find your answer.

Basically, you will need to use the 'listConnections' command with the 'type' flag to find the materials, but meshes are not directly connected to materials - ShadingGroups are in between them.

08 August 2012, 12:56 AM,ap=1,dag=1,s=1),type='shadingEngine'), mat=1)


08 August 2012, 01:03 PM
Try this, it works on multiple object selection :

objSel =, s=1, dag=1)

for object in objSel :
SgNodes = cmds.listConnections(object, type='shadingEngine')
matMaya = cmds.listConnections(SgNodes [0] + '.surfaceShader')
objectName = object.replace('Shape', '')
print 'OBJECT: ' + objectName + ' | ' + 'MAYA SHADER: ' + matMaya[0]

It's needs improvements because actually it reads directly the .surfaceShader input from the shadingEngine.
Also, it will not work with per-face assignement on a same object. You should make a loop on each connected shading engine to do that.

Hope it helps.

08 August 2012, 06:08 PM
just a heads up, i'd really try to stay away from guessing what names will be, instead just its parent.
objectName = cmds.listConnections(object,p=True)[0]

CGTalk Moderation
08 August 2012, 06:08 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.