change colour of curve objects

change colour of curve objects

danispag
Enthusiast Enthusiast
3,510 Views
4 Replies
Message 1 of 5

change colour of curve objects

danispag
Enthusiast
Enthusiast

So I have an option Menu with a list of colours. When I select the curve, and the colour from the options and I run the code nothing happens. Can someone help please?

 

CODE:

 

import maya.cmds as cmds

if cmds.window('change_colors', exists = True):
cmds.deleteUI('change_colors')

cmds.window('change_colors')

cmds.columnLayout()
cmds.optionMenu( label='Colors')

red = cmds.menuItem( label='Red' )
blue = cmds.menuItem( label='Blue' )
yellow = cmds.menuItem( label='Yellow' )
green = cmds.menuItem( label='Green' )
pink = cmds.menuItem( label='Pink' )
lightBlue = cmds.menuItem( label='Light Blue' )

cmds.showWindow()

crvSel = cmds.ls(sl=True)

crv_shape = cmds.listRelatives(crvSel, s = True)

cmds.setAttr(crv_shape[0] + '.overrideEnabled', 1)

if red == True:
cmds.setAttr(crv_shape[0] + '.overrideColor', 13)
if blue == True:
cmds.setAttr(crv_shape[0] + '.overrideColor', 6)
if yellow == True:
cmds.setAttr(crv_shape[0] + '.overrideColor', 17)
if green == True:
cmds.setAttr(crv_shape[0] + '.overrideColor', 14)
if pink == True:
cmds.setAttr(crv_shape[0] + '.overrideColor', 9)
if lightBlue == True:
cmds.setAttr(crv_shape[0] + '.overrideColor', 18)

0 Likes
Accepted solutions (2)
3,511 Views
4 Replies
Replies (4)
Message 2 of 5

forrest.brent
Enthusiast
Enthusiast
Accepted solution
import maya.cmds as cmds
from functools import partial

def changeColor(omenu, cDict, *ignore):
    crvSel = cmds.ls(sl=True)
    crv_shape = cmds.listRelatives(crvSel, s = True)
    if crv_shape is None:
        print("Nothing selected")
    else:
        cmds.setAttr(crv_shape[0] + ".overrideEnabled", 1)
        cmds.setAttr(crv_shape[0] + ".overrideRGBColors", 0)
        key = cmds.optionMenu(omenu, q=True, v=True)
        val = cDict.get(key, 0)
        cmds.setAttr(crv_shape[0] + ".overrideColor", val)

def buildMenu():
    if cmds.window("change_colors", exists = True):
        cmds.deleteUI("change_colors")
    cmds.window("change_colors")

    cmds.columnLayout()
    omenu = cmds.optionMenu( label="Colors")
    colorDict = {"Red" : 13, "Blue": 6, "Yellow": 17, "Green": 14, "Pink" : 9, "Light Blue" : 18}
    for k in sorted(colorDict.keys()):
        cmds.menuItem( label=k )
    cmds.optionMenu(omenu, e = True, cc=partial(changeColor, omenu, colorDict))
    cmds.showWindow()

buildMenu()

 

Try this on for size

 

 

Message 3 of 5

forrest.brent
Enthusiast
Enthusiast
Accepted solution

The key thing to remember when you're expecting a menu item to issue a command based in it's own value - you need to pass itself to the function its calling.  so in this case, omenu refers to "change_colors|columnLayout71|optionMenu37" Which has a weird long name because we didnt name any of our UI elements.  Maya needs this to query what the value is.  Red == True has no menaing because Red is not a variable that exists anywhere within the scope of your code.

You need a separate function to look at the ui widget, query what it says, and then do something based on what it says.  So basically, look at optionmenu called "change_colors|columnLayout71|optionMenu37" - get me the value in plain english (optionmenu query, value=True) that will give you "Red".  OK.  What to do with "Red"?  If red then assign 13.  I've put all the names and corresponding index numbers into key value pairs in a dictionary - so if you want to add more you just update the dictionary, instead of making an endless list of if statements.  The ui passes the name of the widget, and the dictionary to the changecolor function, and presto - best christmas ever.

0 Likes
Message 4 of 5

danispag
Enthusiast
Enthusiast

Thank you so much, and really appreciated your explanation. It worked :). Just one small thing. At the moment you can only change colour of 1 curve instead of multiple. I tried to add a for loop which is marked in red and still its not changing the colour on multiple curves. Am I missing something?

 

def changeColor(omenu, cDict, *ignore):
crvSel = cmds.ls(sl=True)
crv_shape = cmds.listRelatives(crvSel, s = True)
if crv_shape is None:
print("Nothing selected")
else:
for i in crv_shape:
cmds.setAttr(crv_shape[0] + ".overrideEnabled", 1)
cmds.setAttr(crv_shape[0] + ".overrideRGBColors", 0)
key = cmds.optionMenu(omenu, q=True, v=True)
val = cDict.get(key, 0)
cmds.setAttr(crv_shape[0] + ".overrideColor", val)

0 Likes
Message 5 of 5

forrest.brent
Enthusiast
Enthusiast
You want curveshape[i] not [0]
0 Likes