【问题标题】:Maya Python: Change selected menuItem in an optionMenuMaya Python:更改选项菜单中的选定菜单项
【发布时间】:2023-03-28 04:05:01
【问题描述】:

我试图弄清楚如何更改 optionMenu 中的 menuItem 选择,但无法弄清楚。

mymenu = cmds.optionMenu(label ='select type', cc=selectTypeCallback)
cmds.menuItem(parent=mymenu, label='selection1')
cmds.menuItem(parent=mymenu, label='selection2')

现在我如何再次调用 mymenu 并检查当前选择的 menuItem 到我选择的那个?

【问题讨论】:

    标签: python user-interface maya optionmenu


    【解决方案1】:

    我已经为你写了一个完整的工作示例和一些很好的实践,你的答案是最后四行:)

        from functools import partial
    
        # Something to parse ui element, it is important to make difference between ui query and functions
        def optionMenuParser_string(opM_name, func, *args):
            # *args (or could be **kwargs) is here to pass maybe more arguments to a function
            string = cmds.optionMenu(opM_name, q=True, v=True) # you could even parse some flag or anything
            if args and len(args) > 1:
                # args len must be superior to 1 because maya always input True argument
                func(string, *args)
            else:
                func(string)
    
        # A function doing something with the string
        def printNewMenuItem( item ):
            print item 
    
        window = cmds.window()
        cmds.columnLayout()
        # change command='' is a placeholder to input back the option menu name
        mygroup = cmds.optionMenu( label='Colors', changeCommand='' )
        # partial is used to put arguments in a function trhought the ui
        cmds.optionMenu(mygroup, e=1, changeCommand= partial(optionMenuParser_string, mygroup, printNewMenuItem) )
        cmds.menuItem( p=mygroup, label='Yellow' )
        cmds.menuItem( p=mygroup, label='Purple' )
        cmds.menuItem( p=mygroup, label='Orange' )
        cmds.showWindow( window )
    
    
        cmds.optionMenu(mygroup, q=True, v=True) # to get the name of the current value
        cmds.optionMenu(mygroup, q=True, sl=True) # to get the current index
        cmds.optionMenu(mygroup, e=True, sl = 3 )# to change the current value
        cmds.optionMenu(mygroup, e=True, v = 'Purple' )# to change the current value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2011-11-05
      • 2017-04-18
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多