Creating a new material?

195 views
Skip to first unread message

marie...@gmail.com

unread,
Apr 20, 2009, 3:54:05 PM4/20/09
to python_inside_maya
I have a cylinder I created with polyCylinder which I'd like to assign
a color to (no texture, just a color). I figured I had to create a new
material first since I'd like to create many such polygons with a
different color each. Then I'd connect it to the polygon and modify
the color.

The problem is that I'm stuck at 'creating a new material', I can't
find anything like this in the documentation (or maybe I'm just not
understanding).

Any help?

P.S.: I'm stuck on Maya 8.5, this is all the school have licensed.

Thanks

Sylvain Berger

unread,
Apr 21, 2009, 10:07:40 AM4/21/09
to python_in...@googlegroups.com
here is a few function that should get you going:
in short, you create a shader and a shading group, connect the shader to the shading group
Then connecting an object or polygon face(s) to the set will assign the shader to the objects/faces

good luck

import maya.cmds as mc
""" create a new shader """
def createShader(shaderType='lambert', name=''):
if name == '':
name = shaderType
name = mc.shadingNode(shaderType, asShader=True,  name=name)
sg = mc.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";
mc.connectAttr('%s.%s' %(name,shaderOutput), '%s.miMaterialShader' %(sg), force=True)
mc.connectAttr('%s.%s' %(name,shaderOutput), '%s.miShadowShader' %(sg), force=True)
mc.connectAttr('%s.%s' %(name,shaderOutput), '%s.miPhotonShader' %(sg), force=True)
else:
mc.connectAttr('%s.outColor' %(name), '%s.surfaceShader' %(sg), force=True)
return [name, sg]

def assignToShader(shaderSG=None, objects=None):
# assign selection to the shader
if objects is None:
objects = mc.ls(sl=True, l=True)
for i in objects:
print i
try:
mc.sets(i, e=True, forceElement=shaderSG)
except:
pass

"""
Example:
# run the next line
shader, shaderSG = createShader('blinn', 'foobar')
# SELECT SOME OBJECTS
# run the next line to assign the selected object to the shader
assignToShader(shaderSG)
--
They say, "Evil prevails when good men fail to act." What they ought to say is, "Evil prevails."
Nicolas Cage as Yuri Orlov in Lord of War.

amala...@gmail.com

unread,
Jun 21, 2018, 7:27:11 AM6/21/18
to Python Programming for Autodesk Maya
How can I change an Attribute value of that shader?? for example if I want to change the diffuse value to .1 withing the script. how I can do that.?
Reply all
Reply to author
Forward
0 new messages