每次在文本滚动列表中更改选择时更新选项菜单项



我有一个既有textScrollList又有optionMenu的窗口。每当文本列表中的选择发生更改时,我都希望刷新选项菜单项。

不一定要从这个开始。

我使用的是常规Maya Python。

基本技巧只是确保您的更新函数的定义方式使其知道optionMenu和textScrollList的名称,以便进行编辑。简单的方法是在两个项都被声明并存储在变量中之后定义回调——只要这一切都在一个范围内——python就会通过闭包自动插入你需要的值——这比手动要优雅得多

下面是一个非常简单的示例

w = cmds.window()
c = cmds.columnLayout()
sl = cmds.textScrollList( append = ['a', 'b', 'c'])
op = cmds.optionMenu(label = 'test')
cmds.menuItem('created by default')

# the callback is defined after the ui so it knows the values of 'sl' and 'op'
def edit_options():
    # gets a list of selected items from the textScroll
    selected = cmds.textScrollList(sl,q=True, si=True)
    # loop through existing menus in the optionMenu and destroy them
    for item in cmds.optionMenu(op, q=True, ill=True) or []:
        cmds.deleteUI(item)
    # add a new entry for every item in the selection
    for item in selected:
        cmds.menuItem(label = item, parent = op)
    # and something not dependent on the selection too
    cmds.menuItem(label = 'something else', parent = op)
# hook the function up - it will remember the control names for you
cmds.textScrollList(sl, e=True, sc = edit_options)
cmds.showWindow(w)

更多背景:http://techartsurvival.blogspot.com/2014/04/maya-callbacks-cheat-sheet.html

您必须使用一个动作来填充文本滚动并将其附加到on selectCommand标志。

您可能需要在函数中使用部分传递文本滚动名称作为arg

希望能有所帮助。

最新更新