# Get All Shaders From Mesh

**URL:** https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677
**Category:** Maya
**Tags:** mel
**Created:** [February 13, 2008, 8:14pm UTC](https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677 "2008-02-13T20:14:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![overdrivediamond](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/o/a88e4f/32.png) [@overdrivediamond](https://forum.highend3d.com/u/overdrivediamond)
#### Post date: [February 13, 2008, 8:14pm UTC](https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677/1 "2008-02-13T20:14:04Z")

</div>

How can I get all of the shaders/materials from a single mesh?

We have 2 scenarios:  
1. mesh can have a single shader/material  
2. mesh can have multiple shaders/materials (due to combining nodes).

This is what we do to get a single shader from a mesh

CODE  
#========================================================================  
def GetShaderFromMesh (mesh):  
&nbsp;&nbsp;&nbsp;&nbsp;future = mayaCmd.listHistory(str(mesh), f=1, pdo=1)

&nbsp;&nbsp;&nbsp;&nbsp;if future is None:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return None

&nbsp;&nbsp;&nbsp;&nbsp;# Search for the connecting shading group  
&nbsp;&nbsp;&nbsp;&nbsp;for n in future:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if mayaCmd.attributeQuery("surfaceShader",node=n, exists=True):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Get shader from incoming connection of the shading group  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shader = mayaCmd.listConnections("%s.surfaceShader" % (str(n)), source=True)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if shader is None:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shader = str(shader[0])

&nbsp;&nbsp;&nbsp;&nbsp;return None

Thanks in advance!

---

<div class="post-metadata">

### Author: ![ewerybody](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewerybody](https://forum.highend3d.com/u/ewerybody)
#### Post date: [February 13, 2008, 10:54pm UTC](https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677/2 "2008-02-13T22:54:51Z")

</div>

oh python.... this is actually my special subject ![]() but in python??!!? ... well lets have a look.

here a mel example:

CODE  
global proc string[] getShaders(string $input[])  
{  
&nbsp;&nbsp;&nbsp;&nbsp;string $shaders[];

&nbsp;&nbsp;&nbsp;&nbsp;// shaders can be attached to shapes even if not on a face!  
&nbsp;&nbsp;&nbsp;&nbsp;// so get only the faces  
&nbsp;&nbsp;&nbsp;&nbsp;string $faces[] = polyListComponentConversion("-tf",$input);  
&nbsp;&nbsp;&nbsp;&nbsp;if (!size($faces)) // break if there are no faces  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $shaders;

&nbsp;&nbsp;&nbsp;&nbsp;// now list all shadinggroups that these faces are members of  
&nbsp;&nbsp;&nbsp;&nbsp;string $connectedSGs[] = `listSets -type 1 -object $faces`;  
&nbsp;&nbsp;&nbsp;&nbsp;if (!size($connectedSGs)) // break if there are no shadingGroups  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $shaders;

&nbsp;&nbsp;&nbsp;&nbsp;// type lambert would not return shaders that  
&nbsp;&nbsp;&nbsp;&nbsp;// aren't derived from lambert1, ls -materials works better  
&nbsp;&nbsp;&nbsp;&nbsp;$shaders = ls("-materials",(`listConnections -s 1 -d 0 $connectedSGs`));

&nbsp;&nbsp;&nbsp;&nbsp;return $shaders;  
}

now listHistory works well. but for example: If you select all faces of a poly object and then assign a different shader, you'd get the old shadingGroup too! Even if its not connected to any surface.  
As well as the surfaceShader plug is good. Thats the common case.. but what about a volumeShader? ls -materials would get any shaders and you can parse an array! Thats the key I guess :]

---

<div class="post-metadata">

### Author: ![overdrivediamond](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/o/a88e4f/32.png) [@overdrivediamond](https://forum.highend3d.com/u/overdrivediamond)
#### Post date: [February 14, 2008, 12:25am UTC](https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677/3 "2008-02-14T00:25:54Z")

</div>

Oh wow, thanks! Does this work for the scenario when a material is added to the whole object?

---

<div class="post-metadata">

### Author: ![ewerybody](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewerybody](https://forum.highend3d.com/u/ewerybody)
#### Post date: [February 14, 2008, 8:13am UTC](https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677/4 "2008-02-14T08:13:46Z")

</div>

QUOTE(Overdrive D @ 02/14/08, 01:25 AM) [snapback]281057[/snapback]  
Oh wow, thanks! Does this work for the scenario when a material is added to the whole object?

Yes! absolutely :]

---

<div class="post-metadata">

### Author: ![lacio-drom](https://forum.highend3d.com/user_avatar/forum.highend3d.com/lacio-drom/32/3642_2.png) [@lacio-drom](https://forum.highend3d.com/u/lacio-drom)
#### Post date: [July 7, 2018, 9:31pm UTC](https://forum.highend3d.com/t/get-all-shaders-from-mesh/1677/5 "2018-07-07T21:31:05Z")

</div>

Hi to all, I finded this but i need of a reverse script,select all mesh with same material without use "hyperShade -objects " command.  
Can you help me?
