0

I have written up a simple UI that requires user to select something from a drop-down list, then using that selection, the code will executes the rest of the stuff

Right now, I am having 2 issues.. 1. The 'value' is not exactly returning, as soon as a Format is selected and the OK button is hit... Am I missing something? 2. How can I make my UI closes upon a OK button has been selected?

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
dissidia
  • 1,531
  • 3
  • 23
  • 53

1 Answers1

0

Use python dictionnaries or class to pass data in your script. I really don't understand what the problem is.

When you say : "The 'value' is not exactly returning", what do you mean ? Can you tell us what do you get and what do you expect ?

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
    ...

Here the value selection from the "input value", I guess it is :

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

But as there is not filmbackMenu in your code, I'm not sure.

Your second question has been answered on google groups by Justin. You just have to do :

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

Or maybe "upon a OK button has been selected?" doesn't mean "OK button pressed" ?

If you want to see how use dictionnaries, you can read this other post where I have answered : Maya Python - Using data from UI You are in a good path to using partial, I recommend you to read about it : Calling back user input values inside maya UI

--- EDIT ---

I tried to create a fully functionnal example :

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()
Community
  • 1
  • 1
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • By value is not returning, I mean that after user has selected a format and hit ok, the selected format is not being returned in the `ObjectSelection` function. However upon using `cmds.optionMenu('filmbackMenu', edit = True, value = ObjectSelection())`, the UI does not pops up and simply uses the first item in the menu as selection. Hence my dilemma – dissidia Jun 27 '16 at 21:37
  • I modified your code to have something to test. I have edited my answer. When you hit okay, it refresh the second one. Not sure if it was you are asking. Try to be more precise if it is not. – DrWeeny Jun 27 '16 at 23:02
  • Pardon me if I was unclear, I made a slight edit to my post. In my mainCode() function, between the lines that holds `showUI` and `print "A format has been selected"`, I am expecting my code to 'stop' here and wait until the OK button on the UI has been clicked (meaning a format has been selected) before executing the next line of code. Instead of that scenario, my code runs from start to end without a care about the UI selection, and selection can be done until the code exec. ends. It is doing the same thing to your code too, running start to end without taking in the UI selection – dissidia Jun 27 '16 at 23:59
  • To put it simply, it takes the first format in the list as the selection without taking in the User selection.. hopefully that makes sense? – dissidia Jun 28 '16 at 00:08