Discussion:
[Maya-Python] Create customised shader with Python - maya
AMAL ARJUN
2018-06-21 09:09:19 UTC
Permalink
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.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/50de078a-7b38-4032-937c-61b90b80cce6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Boon
2018-06-22 00:42:30 UTC
Permalink
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?
Post by AMAL ARJUN
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
#pm.setAttr('%s.weight' %(name),0.25)
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.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b25345c9-6812-4a61-a735-29caaf74b215%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
AMAL ARJUN
2018-06-22 01:51:43 UTC
Permalink
No. Can you please give an example..
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/81ec8c8d-8891-46a3-8ab7-9e4b285e0b4f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
f***@gmail.com
2018-06-22 11:55:14 UTC
Permalink
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/372af65c-7101-4d84-8a46-4a9235ddd025%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
AMAL ARJUN
2018-06-26 03:46:38 UTC
Permalink
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
Post by f***@gmail.com
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/adf4c8f5-67ce-447e-8ae3-3a8ddd06bc7a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Boon
2018-06-26 04:09:15 UTC
Permalink
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.
Post by AMAL ARJUN
Not working. Still getting an error "# MayaAttributeError: Maya Attribute
does not exist (or is not unique):: u'CHROME3.kd' # "
def createShader(shaderType='aiStandard', name='CHROME', shaderOutput =
#pm.setAttr('%s.%s.kd' %(name,shaderOutput),0.25)
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
Post by f***@gmail.com
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b5dc0959-38dd-4a15-9025-0b7ffac53a39%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
AMAL ARJUN
2018-06-26 05:34:34 UTC
Permalink
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.
Post by Michael Boon
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
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.
Post by AMAL ARJUN
Not working. Still getting an error "# MayaAttributeError: Maya Attribute
does not exist (or is not unique):: u'CHROME3.kd' # "
def createShader(shaderType='aiStandard', name='CHROME', shaderOutput =
#pm.setAttr('%s.%s.kd' %(name,shaderOutput),0.25)
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
Post by f***@gmail.com
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/c486c41a-eefd-460c-b1b4-430dfcc46c6f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Boon
2018-06-26 06:46:53 UTC
Permalink
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')
Post by AMAL ARJUN
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.
Post by Michael Boon
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
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.
Post by AMAL ARJUN
Not working. Still getting an error "# MayaAttributeError: Maya
Attribute does not exist (or is not unique):: u'CHROME3.kd' # "
def createShader(shaderType='aiStandard', name='CHROME', shaderOutput =
#pm.setAttr('%s.%s.kd' %(name,shaderOutput),0.25)
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
Post by AMAL ARJUN
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/0e9dab98-1ae2-4af4-8d76-788c79077c47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
AMAL ARJUN
2018-06-26 06:58:40 UTC
Permalink
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"
Post by Michael Boon
If I create a ramp and connect it to the normalMapSamplerTexture input on
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
pm.PyNode('ramp1').attr('outColor') >> pm.PyNode('my_material').attr(
'normalMapSamplerTexture')
Post by AMAL ARJUN
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.
Post by Michael Boon
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
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.
Post by AMAL ARJUN
Not working. Still getting an error "# MayaAttributeError: Maya
Attribute does not exist (or is not unique):: u'CHROME3.kd' # "
def createShader(shaderType='aiStandard', name='CHROME', shaderOutput =
#pm.setAttr('%s.%s.kd' %(name,shaderOutput),0.25)
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
Post by AMAL ARJUN
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/6ccf3769-2f61-48ee-b085-538ea0575efa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Boon
2018-06-26 07:33:20 UTC
Permalink
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".
Post by AMAL ARJUN
I got your point. but don't know whats happening. Check my script below
def createShader2(shaderType='alSurface', name='PLASTIC', shaderOutput =
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'
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)
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"
Post by Michael Boon
If I create a ramp and connect it to the normalMapSamplerTexture input on
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
pm.PyNode('ramp1').attr('outColor') >> pm.PyNode('my_material').attr(
'normalMapSamplerTexture')
Post by AMAL ARJUN
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.
Post by Michael Boon
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
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.
Post by AMAL ARJUN
Not working. Still getting an error "# MayaAttributeError: Maya
Attribute does not exist (or is not unique):: u'CHROME3.kd' # "
def createShader(shaderType='aiStandard', name='CHROME', shaderOutput
#pm.setAttr('%s.%s.kd' %(name,shaderOutput),0.25)
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
Post by AMAL ARJUN
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/699a9a14-f30c-4246-b9fb-2b1d007b44e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
AMAL ARJUN
2018-06-26 07:35:41 UTC
Permalink
What i want is in button click a meterial with a ramp connected to its weight should happen.. 😃
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1a4ada61-be8c-4ceb-a740-2d803fb870d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
AMAL ARJUN
2018-06-26 04:17:38 UTC
Permalink
Thanks alot buddy. It was working... Thank you so much..... appreciate ur
help
Post by f***@gmail.com
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)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e4f8890b-96bd-48c6-850a-d377eaa3c59d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...