optionMenu不返回值

时间:2016-06-27 17:13:50

标签: python selection maya optionmenu

我写了一个简单的UI,要求用户从下拉列表中选择一些内容,然后使用该选择,代码将执行其余的东西

现在,我有两个问题.. 1.'价值'一旦选择了格式并且按下确定按钮,就不会完全返回...我错过了什么吗? 2.如何在选择确定按钮后关闭UI?

import maya.cmds as cmds

def mainCode():
    ...
    ...
    print "UI popping up"
    showUI()

    print "A format has been selected"
    cmds.optionMenu('filmbackMenu', edit = True, value = xxx ) # <-- I want to grab the value from the menu selection and input into this 'value' flag
    ...
    ...

def showUI():

    if cmds.window("UI_MainWindow", exists = True):
        cmds.deleteUI("UI_MainWindow")

    cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
    cmds.columnLayout("UI_MainLayout", w = 300, h =500)

    cmds.optionMenu("UI_FormatMenu", w = 250, label = "Select a Format")

    list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
    for x in list01:
        cmds.menuItem(label = str(x))

    cmds.button("UI_SelectButton", label = "OK", w = 200, command=ObjectSelection)

    cmds.showWindow("UI_MainWindow") #shows window

def ObjectSelection(*args):
    currentFormat = cmds.optionMenu("UI_FormatMenu", query=True, value=True)
    print currentFormat
    return currentFormat

1 个答案:

答案 0 :(得分:0)

使用python dictionnaries或类在脚本中传递数据。 我真的不明白这是什么问题。

当你说:“'价值'没有完全归还”时,你的意思是什么?你能告诉我们你得到了什么,你期待什么?

def mainCode():
    ...
    ...
    showUI()
    cmds.optionMenu('filmbackMenu', edit = True, value = xxx ) # <-- I want to grab the value from the menu selection and input into this 'value' flag
    ...

这里从“输入值”中选择值,我猜是:

cmds.optionMenu('filmbackMenu', edit = True, value = ObjectSelection())

但由于代码中没有filmbackMenu,我不确定。

贾斯汀在谷歌小组上回答了你的第二个问题。你必须这样做:

def ObjectSelection(*args):
    currentFormat = cmds.optionMenu("UI_FormatMenu", query=True, value=True)
    cmds.deleteUI("UI_MainWindow")#close the UI
    print currentFormat
    return currentFormat

或者“选择确定按钮后?”并不意味着“按下OK按钮”?

如果你想看看如何使用词典,你可以阅读我回答的其他帖子:Maya Python - Using data from UI 您正在使用partial的好方法,我建议您阅读它:Calling back user input values inside maya UI

---编辑---

我尝试创建一个完整的功能示例:

import maya.cmds as cmds

uiDic = {}
uiDic['this']= 1

def ui_refresh(*args):
    uiDic['this'] = cmds.optionMenu("UI_FormatMenu", query=True, value=True)
    return uiDic['this']

def showUI():

    if cmds.window("UI_MainWindow", exists = True):
        cmds.deleteUI("UI_MainWindow")

    cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
    cmds.columnLayout("UI_MainLayout", w = 300, h =500)

    cmds.optionMenu("UI_FormatMenu", w = 250, label = "Select a Format")

    list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
    for x in list01:
        cmds.menuItem(label = str(x))

    cmds.button("UI_SelectButton", label = "OK", w = 200, command=ObjectSelection)

    uiDic['om_filmback'] = cmds.optionMenu('filmbackMenu' )
    list01 = ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
    for x in list01:
        cmds.menuItem(label = str(x))


    cmds.showWindow("UI_MainWindow") #shows window

def ObjectSelection(*args):
    cmds.optionMenu(uiDic['om_filmback'], edit = True, value=ui_refresh())

showUI()