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

允我心安 提交于 2019-12-12 02:36:21

问题


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.. :)


回答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)




回答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

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



来源:https://stackoverflow.com/questions/31552388/how-to-add-a-color-to-an-object-in-autodesk-maya-2016-in-python-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!