PyMel and OptionMenu

Hey all.
Apologies if this has already been asked, but is there a way to dynamically alter the items in an optionMenu? I need it to update based on the selection i make on a textScrollList.

VijayL.

Here’s a fun (rough, unoptimized, and kinda sloppy) pattern you could mess around with to get some ideas from, it’s pretty straightforward, there are probably better ways to do it:


import pymel.core as pm

class ChangeMenuOnSelect(object):
    WINDOW_NAME = 'changeOnSelectWindow'
    menu_data = {'listItem1':['item1a','item1b','item1c'],
                    'listItem2':['item2a','item2b','item2c'],
                    'listItem3':['item3a','item3b','item3c']}
                        
    def __init__(self):
        try:
            pm.deleteUI(self.WINDOW_NAME)
        except:
            pass
        finally:
            self.layout()
        
    def layout(self):
        with pm.window(self.WINDOW_NAME, resizeToFitChildren=True):
            with pm.rowLayout(nc=2,rat=(1,'top',0)):
                with pm.optionMenuGrp() as self.main_menu:
                    labels = self.menu_data.get('listItem1')
                    labels.sort()
                    for k in labels:
                        pm.menuItem(l=k)
                self.option_menu = self.main_menu.menu()
                self.main_list = pm.textScrollList(allowMultiSelection=False, selectCommand=pm.Callback(self._on_list_select))
                
                list_items = self.menu_data.keys()
                list_items.sort()
                for l in list_items:
                    self.main_list.append(l)
        
    def _on_list_select(self, *args):

        list_contents = self.menu_data.get(self.main_list.getSelectItem()[0])
        list_contents.sort()
        
        self.option_menu.clear()
        pm.setParent(self.option_menu, menu=True)
        for l in list_contents:
            pm.menuItem(label=l)