简体   繁体   中英

How to add a color to an object in Autodesk Maya 2016 in Python scripts?

I am new in Autodesk Maya. I looked over the internet to find some details about how can I command in the python scripting , to an object to change it's color when it is selected. I know how to see the selected object, however, I didn't succeed to change the color. I used this function -

enter code here

   'result = cmds.ls(orderedSelection =True)
    Trans = result[0]
    cmds.color(Trans,userDefined =8 ) '

when I press an object, it is being selected, but it's color doesn't change.

It will be helpful if you can help me.. :)

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 )

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

def get_input_shapes(switchNode):
    input_count = cmds.getAttr(switchNode + ".input", size=True)
    results = {}
    for item in range(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM