Search code examples
pythonbuttoniconsmenuitemmaya

Maya Python - Change Button Icon Based On Active Command


Working with maya 16

I do have an iconTextButton setup, with a popupMenu containing several menuItem, where each menuItem comes with an icon. I want the parent, the iconTextButton, to display the menuItem icon based on which menuItem is currently active.

This is my current setup:

cmds.iconTextButton( style='iconAndTextHorizontal', image1='MainButton.png', h=40,w=100, bgc=[0.35, 0.35, 0.35], label='Open Submenu Pallet')

def someImportantFunction (*args):
    somethingImportant

cmds.popupMenu(b=1)
cmds.menuItem(label='do Something', command=someImportantFunction, image1='subMenuIconToChangeTo.png')

I`m still pretty new to scripting and logic in general, just cant get my head around it.


Solution

  • You can just edit the original button to change it's appearance when you fire off the menu commands:

    window = cmds.window(title='example')
    layout = cmds.columnLayout(adj=True)
    ict = cmds.iconTextButton( style='iconAndTextHorizontal', image1='MainButton.png', h=40,w=100, bgc=[0.35, 0.35, 0.35], label='Open Submenu Pallet')
    popup = cmds.popupMenu(b=1)
    
    def something(*_):
        print "something" # real work goes here
        cmds.iconTextButton(ict, e=True, label = 'something')
    
    def something_else(*_):
        print "something else"
        cmds.iconTextButton(ict, e=True, label = 'something else')
    
    cmds.menuItem(label='something', image1='somethingIcon', c= something)
    cmds.menuItem(label='something else', image1='otherIcon', c=something_else)
    cmds.setParent("..")
    cmds.showWindow(window)
    

    The key is to make sure you hang on to a reference to the widgets you create so you can edit or query them using the e=True or q=True flags for your gui items.