# Python: How To Assign Shader To An Object?

**URL:** https://forum.highend3d.com/t/python-how-to-assign-shader-to-an-object/48778
**Category:** Maya
**Tags:** python
**Created:** [March 11, 2009, 1:33am UTC](https://forum.highend3d.com/t/python-how-to-assign-shader-to-an-object/48778 "2009-03-11T01:33:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kaymatrix](https://forum.highend3d.com/user_avatar/forum.highend3d.com/kaymatrix/32/54657_2.png) [@kaymatrix](https://forum.highend3d.com/u/kaymatrix)
#### Post date: [March 11, 2009, 1:33am UTC](https://forum.highend3d.com/t/python-how-to-assign-shader-to-an-object/48778/1 "2009-03-11T01:33:47Z")

</div>

import maya.cmds as cmds

cmds.sphere()

myBlinn = cmds.shadingNode('blinn', asShader=True)

cmds.select( 'nurbsSphere1' )

cmds.hyperShade( myBlinn, assign=True )

Whats wrong in this code?

---

<div class="post-metadata">

### Author: ![kurianos3d](https://forum.highend3d.com/user_avatar/forum.highend3d.com/kurianos3d/32/55947_2.png) [@kurianos3d](https://forum.highend3d.com/u/kurianos3d)
#### Post date: [March 11, 2009, 3:48am UTC](https://forum.highend3d.com/t/python-how-to-assign-shader-to-an-object/48778/2 "2009-03-11T03:48:26Z")

</div>

CODE  
cmds.hyperShade( assign=myBlinn)

---

<div class="post-metadata">

### Author: ![masternull](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@masternull](https://forum.highend3d.com/u/masternull)
#### Post date: [March 11, 2009, 1:10pm UTC](https://forum.highend3d.com/t/python-how-to-assign-shader-to-an-object/48778/3 "2009-03-11T13:10:04Z")

</div>

I prefer using the set function, and control everything myself. A bit more complex but you get much more control. Plus since the hyperSahder function works only on a selection, you don't have to select objects before assigning them, this will be faster in large scene and provide you with better control in the long run.

CODE  
import maya.cmds as mc

def getSGfromShader(shader=None):  
&nbsp;&nbsp;&nbsp;&nbsp;if shader:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if mc.objExists(shader):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sgq = mc.listConnections(shader, d=True, et=True, t='shadingEngine')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if sgq:   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sgq[0]

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

def assignObjectListToShader(objList=None, shader=None):  
&nbsp;&nbsp;&nbsp;&nbsp;"""   
&nbsp;&nbsp;&nbsp;&nbsp;Assign the shader to the object list  
&nbsp;&nbsp;&nbsp;&nbsp;arguments:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objList: list of objects or faces  
&nbsp;&nbsp;&nbsp;&nbsp;"""  
&nbsp;&nbsp;&nbsp;&nbsp;# assign selection to the shader  
&nbsp;&nbsp;&nbsp;&nbsp;shaderSG = getSGfromShader(shader)  
&nbsp;&nbsp;&nbsp;&nbsp;if objList:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if shaderSG:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mc.sets(objList, e=True, forceElement=shaderSG)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'The provided shader didn\'t returned a shaderSG'  
&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'Please select one or more objects'

def assignSelectionToShader(shader=None):  
&nbsp;&nbsp;&nbsp;&nbsp;sel = mc.ls(sl=True, l=True)  
&nbsp;&nbsp;&nbsp;&nbsp;if sel:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assignObjectListToShader(sel, shader)

assignSelectionToShader('lambert2')

---

<div class="post-metadata">

### Author: ![masternull](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@masternull](https://forum.highend3d.com/u/masternull)
#### Post date: [March 11, 2009, 1:20pm UTC](https://forum.highend3d.com/t/python-how-to-assign-shader-to-an-object/48778/4 "2009-03-11T13:20:06Z")

</div>

ho and also you can continue these function to get the sg from an object for example. This way it becomes easy to assign the shader of the first selected object on the second selected object...
