每次在 textScrollList 中更改选择时更新 optionMenu 项
Update optionMenu items every time selection is changed in a textScrollList
我有一个 window,它有一个 textScrollList 和一个 optionMenu。每当文本列表中的选择发生更改时,我想刷新选项菜单项。
不太确定从这个开始。
我使用的是普通 Maya Python。
您必须使用一种功能来填充文本滚动并将其附加到 on selectCommand 标志。
您可能必须在函数中使用部分传递文本滚动名称作为 arg
希望对您有所帮助。
基本技巧只是确保更新函数的定义方式使其知道 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
我有一个 window,它有一个 textScrollList 和一个 optionMenu。每当文本列表中的选择发生更改时,我想刷新选项菜单项。
不太确定从这个开始。
我使用的是普通 Maya Python。
您必须使用一种功能来填充文本滚动并将其附加到 on selectCommand 标志。
您可能必须在函数中使用部分传递文本滚动名称作为 arg
希望对您有所帮助。
基本技巧只是确保更新函数的定义方式使其知道 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