pythonmayapymelmaya-api

How can I change the textScrollList depending upon the options I have selected in optionMenu in Maya?


` This is the function that i wrote the operation, where om = optionMenu

def selection(*args):
    
    selected = cmds.optionMenu(om,sl=True, q=True) 
       
    cmds.textScrollList(tsl, e=True, removeAll=True)
    
    
    for item in temp[selected]:
        cmds.textScrollList(label=item, parent=om)

`

enter image description here

So basically I have object types in the OM and all the objects in TSL, I wanna display items of only selected object types from OM, for example if I select "mesh" in the OM the TSL will display only mesh objects.

where OM = optionMenu and TSL - textScrollList


Solution

  • to get a qualified answer, you should provide a minimal executable script. And you did not write what exactly does not work. Please describe exactly what errors you get. What I can see is that you create a list of textScrollList ui elements instead of filling the existing text scroll list. A complete solution can look like this:

    import maya.cmds as cmds
    
    class MyWindow(object):
        def __init__(self):
            self.types = ["mesh", "light"]    
            self.tsl = None
            self.win = None
            self.om = None
            self.buildUI()
    
        def updateList(self, *args):
            geoType = cmds.optionMenu(self.om, query=True, value=True)
            cmds.textScrollList(self.tsl, edit=True, removeAll=True)
            elements = cmds.ls(type=geoType)
            for e in elements:
                cmds.textScrollList(self.tsl, edit=True, append=e)
                
        def buildUI(self):
            if self.win is not None:
                if cmds.window(self.win, exists=True):
                    cmds.deleteUI(self.win)
            self.win = cmds.window()
            cmds.columnLayout()
            self.om = cmds.optionMenu(cc=self.updateList)
            for i in self.types:
                cmds.menuItem(label=i)
            self.tsl = cmds.textScrollList()
    
    cmds.showWindow(self.win)
    

    I use a class here because it is easier to maintain and to keep all data inside.