Returning value from maya windows optionMenu

1,972 views
Skip to first unread message

likage

unread,
Jun 24, 2016, 3:29:44 PM6/24/16
to Python Programming for Autodesk Maya
Hi all, pardon my coding as it has really been a while since I have last touched on scripting. Currently I am trying to make some stuff work but keeps bumping into issues here and there.
Really in need of some insights
    
1. I am unable to return the text value from the selection in showUI
2. Prior to #1, when I tested without the showUI() function, it works where the Ok button does prints some stuff however the window is never closed upon clicking on the button. So my question here will be how do I close the window upon hitting the Ok button
3. Related to #1, I am trying to get the value - the text value from the selection and have it run in my main code - pcikAFormat() in the optionMenu command, value flag..

4. Just wondering what is the best way that I can select the latest created node/item? 

def defaultButtonPush(*args):
   
#valid = ""
    valid
= showUI.validFormatsLs.getValue()
   
#return valid
       
def showUI():
    win_name
= "Select Camera Format"
   
if cmds.window(win_name, exists = True):
        cmds
.deleteUI(win_name)
   
    main_win
= cmds.window(title = "Select Camera Format", width = 100, height = 50)
    cmds
.columnLayout()
   
    validFormats
= ['itemA-01', 'itemB-02', 'itemC-02', 'itemD-01', 'itemE-01', 'itemF-03']
   
    validFormatsLs
= pm.optionMenu (label = 'Select a format')
   
   
for item in validFormats:
        cmds
.menuItem(label = str(item))
       
    cmds
.button( label = 'Ok', aop = True, command = ('defaultButtonPush()'))
    cmds
.button( label = 'Cancel', command=('cmds.deleteUI(\"' + window + '\", window=True)') )  


    cmds
.showWindow( main_win )
   
showUI
()

This is my main code
def pickAFormat():
   
...
   
...
    mel
.eval('CBdeleteConnection "camera01_cameraFlattenedShape.lensSqueezeRatio";')
   
    showUI
()
    cmds
.optionMenu('filmbackMenu', edit = True, value = ??? ) # value is to take the text selection from the showUI

Justin Israel

unread,
Jun 24, 2016, 7:55:43 PM6/24/16
to python_in...@googlegroups.com
On Sat, Jun 25, 2016 at 7:29 AM likage <dissid...@gmail.com> wrote:
Hi all, pardon my coding as it has really been a while since I have last touched on scripting. Currently I am trying to make some stuff work but keeps bumping into issues here and there.
Really in need of some insights
    
1. I am unable to return the text value from the selection in showUI

There are two ways you can get the value. Either set a "changeCommand" callback on the menu that will be called when the selection changes. Or directly ask the menu for the current value on demand, using the query mode and value=True. In the second option, you will have to know the name of the menu from when it was created.
 
2. Prior to #1, when I tested without the showUI() function, it works where the Ok button does prints some stuff however the window is never closed upon clicking on the button. So my question here will be how do I close the window upon hitting the Ok button

There are a couple things you should address, to fix this behavior.
The "title" attribute of the window is not the same as the object name. You need the object name to be able to refer to it later and delete it or check if it exists.  That means "win_name" doesn't really work well, in the way you are doing it. Two possible approaches:

If you are using just functions, like your example, then you will want to store a global reference to the name of your singleton window:

WINDOW = None

def showUI():
    global WINDOW
    if WINDOW and cmds.window(WINDOW, exists=True):
        cmds.deleteUI(WINDOW)

    WINDOW = cmds.window(...)

This would track a global window name, in which you don't really care what it is called. It will get set by the return value to creating a new window. The previous value, if set, will cause it to get deleted if it exists.

And for the button callbacks, I would recommend not using the string form. You already have the ability to create first class functions so you can just pass the function object and not have to worry about scoping issues:

cmds.button(..., command=defaultButtonPush)
cmds.button(..., command=deleteUI)

These changes should fix your issue with the UI not closing.

 
3. Related to #1, I am trying to get the value - the text value from the selection and have it run in my main code - pcikAFormat() in the optionMenu command, value flag..

That call to showUI isn't going to block, so checking the value right afterwards isn't going to net you the user selection. You should wire up a callback on your menu using the "changeCommand". Your "showUI()" command could even take a callback from the user:

def handleMenuSelection(value):
    print "menu selection changed!", value

showUI(handleMenuSelection)


4. Just wondering what is the best way that I can select the latest created node/item? 

Can you explain this? Which node/item? UI? Scene?
 
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3b07d3e7-ed49-4ede-8993-eefff381a3cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

likage

unread,
Jun 27, 2016, 12:00:33 AM6/27/16
to Python Programming for Autodesk Maya
Pardon the late reply and my slow progress in my code. 
I had wanted to update and insert my latest code but have forgotten about it. I chanced upon this code which is similar to what I had wanted but still I am having trouble getting my program to 'wait' for user selection before executing the rest of the program..

def printObject(objectList, *args):
    objectName = cmds.optionMenu(objectList, query = True, value = True)
    print objectName

def showUI():
    ...
    ...
    validFormatsLs = cmds.optionMenu (label = 'Select a format')
    cmds.button(label = "Print selected option", command = partial(printObject, objectList))


And as for the last question on getting the latest created node...
Currently in my program, there is a custom module which is something as follows: import createCam; createCam.newNode()
Having that command does create a new node (this command is different from cmds.camera()), however after its creation, it does not select the new node
and hence, I am hardcoding it in my current code which is not ideal case, in the event if it is either selecting the wrong cam which has already existed with the same name, or it will simply duplicate a new camera where the naming could 'versioned up' - eg. newCam1, newCam2 etc 

Justin Israel

unread,
Jun 27, 2016, 2:27:40 AM6/27/16
to python_in...@googlegroups.com
On Mon, Jun 27, 2016 at 4:00 PM likage <dissid...@gmail.com> wrote:
Pardon the late reply and my slow progress in my code. 
I had wanted to update and insert my latest code but have forgotten about it. I chanced upon this code which is similar to what I had wanted but still I am having trouble getting my program to 'wait' for user selection before executing the rest of the program..

def printObject(objectList, *args):
    objectName = cmds.optionMenu(objectList, query = True, value = True)
    print objectName

def showUI():
    ...
    ...
    validFormatsLs = cmds.optionMenu (label = 'Select a format')
    cmds.button(label = "Print selected option", command = partial(printObject, objectList))



The fact that you call showUI() and show your window is waiting for input from the user. You have to rely fully on callbacks, as you are doing in your button push. Again, you could install a callback on the menu itself, to respond to the actual change, instead of a button push.
 
And as for the last question on getting the latest created node...
Currently in my program, there is a custom module which is something as follows: import createCam; createCam.newNode()
Having that command does create a new node (this command is different from cmds.camera()), however after its creation, it does not select the new node
and hence, I am hardcoding it in my current code which is not ideal case, in the event if it is either selecting the wrong cam which has already existed with the same name, or it will simply duplicate a new camera where the naming could 'versioned up' - eg. newCam1, newCam2 etc 

I must be missing something here. Why doesn't newNode() return the new node?
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Jun 27, 2016, 1:40:37 PM6/27/16
to Python Programming for Autodesk Maya
I will try and see what I can find for callbacks then..

As for the latter, apparently in the newNode() function, it has another function within and is calling that function - eg. createNewCam()...
While createNewCam() does have a return, but newNode() do not have. Is that a cause for concern or perhaps it is just me not knowing how to grab such return values...

FYI, this createCam was not done by me, it was an in-house thing.. 

Justin Israel

unread,
Jun 27, 2016, 3:41:15 PM6/27/16
to Python Programming for Autodesk Maya
Ok well obviously it wasn't deemed necessary to return the created node from their function. Maybe you can get the list of cameras before you call it. Then the list of cameras after you call it, and find the set difference. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages