Create customised shader with Python - maya

578 views
Skip to first unread message

AMAL ARJUN

unread,
Jun 21, 2018, 5:09:19 AM6/21/18
to Python Programming for Autodesk Maya
Hi I tried the following code to get a shader with some parameters but cant add setAttr and gives an error. following is the code

def createShader(shaderType='aiStandard', name='CHROME'):
    

#pm.setAttr('%s.weight' %(name),0.25)
if name == '':
  name = shaderType
    name = pm.shadingNode(shaderType, asShader=True,  name=name)
    sg = pm.sets(renderable=True, noSurfaceShader=True, empty=True, name='%sSG' %(name))
       

what this does is create an aiStandard shader and rename it with Chrome. Please help me with this. Thanks in advance.



Michael Boon

unread,
Jun 21, 2018, 8:42:30 PM6/21/18
to Python Programming for Autodesk Maya
You won't be able to use setAttr on the node until after you've created it. Have you tried moving your setAttr line to after the pm.shadingNode line?

AMAL ARJUN

unread,
Jun 21, 2018, 9:51:43 PM6/21/18
to Python Programming for Autodesk Maya
No. Can you please give an example..

fruit...@gmail.com

unread,
Jun 22, 2018, 7:55:17 AM6/22/18
to Python Programming for Autodesk Maya
def createShader(shaderType='aiStandard', name='CHROME'):
name = pm.shadingNode(shaderType, asShader=True, name=name or shaderType)
sg = pm.sets(renderable=True, noSurfaceShader=True, empty=True, name='%sSG' %(name))
pm.setAttr('%s.weight' %(name),0.25)

AMAL ARJUN

unread,
Jun 25, 2018, 11:46:39 PM6/25/18
to Python Programming for Autodesk Maya
Not working. Still getting an error "# MayaAttributeError: Maya Attribute does not exist (or is not unique):: u'CHROME3.kd' # "


Script here:

def createShader(shaderType='aiStandard', name='CHROME', shaderOutput = 'sg'):
    
    
    
#pm.setAttr('%s.%s.kd' %(name,shaderOutput),0.25)
if name == '':
name = shaderType
    name = pm.shadingNode(shaderType, asShader=True,  name=name)
    sg = pm.sets(renderable=True, noSurfaceShader=True, empty=True, name='%sSG' %(name))
    pm.setAttr('%s.kd' %(name),0.25)



please help

Michael Boon

unread,
Jun 26, 2018, 12:09:16 AM6/26/18
to Python Programming for Autodesk Maya
I don't have Arnold installed here so I can't help a whole lot, but I can use very similar script to set "diffuse" on a Lambert material. This works for me:
import pymel.core as pm
name
= 'CHROME'
shaderType
= 'lambert'

name
= pm.shadingNode(shaderType, asShader=True,  name=name)
sg
= pm.sets(renderable=True, noSurfaceShader=True, empty=True, name='%sSG' %(name))

pm
.setAttr('%s.diffuse' %(name),0.25)

I'd be looking at the attribute name. Is 'kd' a valid attribute on a aiStandard material node? Maybe create the node and set the attribute using Hypershade, then look in the Script Editor and see what MEL commands have been printed.

AMAL ARJUN

unread,
Jun 26, 2018, 12:17:39 AM6/26/18
to Python Programming for Autodesk Maya
Thanks alot buddy. It was working... Thank you so much..... appreciate ur help


On Friday, June 22, 2018 at 5:25:17 PM UTC+5:30, vince touache wrote:

AMAL ARJUN

unread,
Jun 26, 2018, 1:34:34 AM6/26/18
to Python Programming for Autodesk Maya
This is working fine. But can you give me one more example to connect something to an attribute? for example, if I want to connect a ramp or aiNoise to weight. Thanks in advance.

Michael Boon

unread,
Jun 26, 2018, 2:46:53 AM6/26/18
to Python Programming for Autodesk Maya
If I create a ramp and connect it to the normalMapSamplerTexture input on a material, Script Editor shows something like this:
connectAttr -f ramp1.outColor my_material.normalMapSamplerTexture;

The maya.cmds equivalent is
cmds.connectAttr('ramp1.outColor', 'my_material.normalMapSamplerTexture', f=True)
or in PyMel
pm.connectAttr('ramp1.outColor', 'my_material.normalMapSamplerTexture', f=True)
or if you have PyMel nodes for your materials, or even PyMel attr objects, you can use the >> operator to connect them (assuming you don't need the 'f' switch):
pm.PyNode('ramp1').attr('outColor') >> pm.PyNode('my_material').attr('normalMapSamplerTexture')

AMAL ARJUN

unread,
Jun 26, 2018, 2:58:40 AM6/26/18
to Python Programming for Autodesk Maya
I got your point. but don't know whats happening. Check my script below

def createShader2(shaderType='alSurface', name='PLASTIC', shaderOutput = 'sg'):
    
     if name == '':
name = shaderType
    name = pm.shadingNode(shaderType, asShader=True,  name=name)
    sg = pm.sets(renderable=True, noSurfaceShader=True, empty=True, name='%sSG' %(name))

       
# connect shader to SG
shaderOutput = 'outValue'
if shaderType== 'mia_material' or shaderType == 'mia_material_x':
if shaderType == 'mia_material_x':
shaderOutput = "result";
pm.connectAttr('%s.%s' %(name,shaderOutput), '%s.miMaterialShader' %(sg), force=True)
pm.connectAttr('%s.%s' %(name,shaderOutput), '%s.miShadowShader' %(sg), force=True)
pm.connectAttr('%s.%s' %(name,shaderOutput), '%s.miPhotonShader' %(sg), force=True)
else:
pm.connectAttr('%s.outColor' %(name), '%s.surfaceShader' %(sg), force=True)
pm.setAttr('%s.weight1' %(name),0.85)
pm.setAttr('%s.roughness1' %(name),0)

pm.connectAttr('ramp1.outColor', '%s.metallic1' %(name), f=True)

return [name, sg]



This is the error I'm getting "# RuntimeError: The source attribute 'ramp1.outColor' cannot be found"

Michael Boon

unread,
Jun 26, 2018, 3:33:20 AM6/26/18
to Python Programming for Autodesk Maya
I would try running it one line at a time and see what's happening in Maya.

In this case, it looks like it's failing on the only line where you have entered the name as "ramp1" instead of using the name variable, so I guess your ramp is not actually named "ramp1".

AMAL ARJUN

unread,
Jun 26, 2018, 3:35:41 AM6/26/18
to Python Programming for Autodesk Maya
What i want is in button click a meterial with a ramp connected to its weight should happen.. 😃
Reply all
Reply to author
Forward
0 new messages