PDA

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


blurgh
07-25-2012, 03: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
cmds.ls and usign the 'mat' flag, but it just prints '[]'

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

rgkovach123
07-25-2012, 03: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.

f3rry
08-03-2012, 01:56 AM
cmds.ls(cmds.listConnections(cmds.ls(sl=1,ap=1,dag=1,s=1),type='shadingEngine'), mat=1)

perhaps?

sonicstrawbery
08-15-2012, 02:03 PM
Try this, it works on multiple object selection :


objSel = cmds.ls(sl=True, 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.
Sebastien

ldunham1
08-15-2012, 07: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-15-2012, 07: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.