How To Add A Color To An Object In Autodesk Maya 2016 In Python Scripts?
Solution 1:
This is a roughly way of how to do it:
import maya.cmds as cmds
selections = cmds.ls(sl=True)
for sel in selections:
# get shape of selection:
sel_shape = cmds.ls(o=True, shapes=True)
# get shading groups from shape:
shadingGrps = cmds.listConnections(sel_shape,type='shadingEngine')
# get the shaders:
shaders = cmds.ls(cmds.listConnections(shadingGrps),materials=True)
# change the color of the material to red
cmds.setAttr(shaders[0]+".color", 1, 0, 0, type="double3")
Select what you want and run the script.
Pay attention: if multiples objects share the same material, selecting one will change the color of all others.
(If you want to change the material color when you select an object without run the script, you have to take a look at maya python command scriptJob)
Solution 2:
You can set the wirecolor using a two step process:
# there are 32 wire color numbered 0 to 31
cmds.setAttr(your_object + ".displayOverride", 1)
cmds.setAttr(your_object + ".overrideColor", color)
to set the surface colors you have to have a way to assign an individual color per material. The easy answer is to give each object its own material and control the colors by setting the .color property of the material as in @Ale_32's example. You can use a selectionChanged scriptJob as suggested there to change colors.
If you don't want too many materials lying around you could also create a shader use a tripleShadingSwitch node to drive its color. The tripleShadingSwitch will have inputs for each of your objects, you can set the colors directly using the indices of the objects in the switch:
def set_indexed_color(switchNode, index, color):
cmds.setAttr(switchNode+ ".input[%i]" % index, *color)
# note: that asterisk is important, since color is a 3-piece# value like [1,0,1]
If you're not setting this up by hand you can find out what the incoming objects are using
defget_input_shapes(switchNode):
input_count = cmds.getAttr(switchNode + ".input", size=True)
results = {}
for item inrange(input_count):
inshape = cmds.listConnections(switchNode + ".input[%i].inShape" % item)[0]
results[inshape] = item
return results
which will give you a dictionary mapping the shapes to their index numbers
Post a Comment for "How To Add A Color To An Object In Autodesk Maya 2016 In Python Scripts?"