How to remember the inputs made in a maya ui after closing?


#1

I have created a GUI using maya.cmds commands in which there are a few checkboxes, textFields etc.

Is there a way that I can made the application to remember the settings/ inputs made and have it displayed accordingly as when it was last closed?

Eg. I input in a string - "my_name’ into a textField, and checked one of the checkbox, if I close my tool, and re-open/ clicked on my tool to call it again, the textField should displays “my_name” and the checkbox that I made earlier should also be checked…


#2

You’ll have to have your UI save the settings, but yes. The optionVar command lets you save named values that will be available in future Maya sessions, so you would save the UI values using optionVars and then use the optionVars to set defaults when the UI reopens:

import maya.cmds as cmds

def example_window():
    
    default = cmds.optionVar(q='saved_value') or "default text"
    
    w = cmds.window()
    col = cmds.columnLayout()
    textfld = cmds.textField(text = default)
    
    def save_and_close(*_):
        cmds.optionVar(sv = ('saved_value', cmds.textField(textfld, q=True,  text=True)))
        cmds.deleteUI(w)
        
    btn = cmds.button('save and close', c= save_and_close)
    
    cmds.showWindow(w)
    
    
example_window()

#3

Hi Theodox, many thanks for the reply.

I suppose that if I have 3 text fields, does that means I will need to create 3 different optionVar for it then?


#4

Yes. optionVar can also store arrays, I think, but more individual variables will be clearer


#5

Cool, I will check it out.

One more question - Instead of creating a button “save and close”, do you know if there is a way that allows me to detect when the GUI is closed? This is so that the values are saved upon the GUI closure (denoted by the ‘x’ button on the top right corner)…

FYI, the GUI is created using maya.cmds command and not PyQt…


#6

At least in 2018, the window function has a closeCommand parameter, that you should be able to pass a handler to, not sure how well this one works though.
The other option is a scriptJob listening for when the window gets deleted, we use this one in mGui for cleaning up window state, and it works rather well.


#7

Hey R.White.

Unfortunately I am not using Maya2018… But even so, I chanced upon some code that is able to detect maya gui closure and I incorporate it along with the code that Theodox has provided…

And while doing so, the inputs cannot be saved. Unsure of what went wrong (I have never used Maya api before…), could anyone kindly advise?

from maya import OpenMaya
from maya import OpenMayaUI
import maya.cmds as cmds

def makeTestWindow(win_closure=False):
    default = cmds.optionVar(q='saved_value') or "default text"
    
    w = cmds.window()
    col = cmds.columnLayout()
    textfld = cmds.textField(text = default)
    
    def save_and_close(*_):
        cmds.optionVar(sv = ('saved_value', cmds.textField(textfld, q=True,  text=True)))
        cmds.deleteUI(w)
    
    if win_closure:
        return save_and_close()

    # The initial code has this button, but I commented it out as I do not want the button
    # Having done that, it seems that it is unable to 'save' the inputs...
    #btn = cmds.button('save and close', c= save_and_close)
    
    cmds.showWindow(w)
 
def uiDeleteCallback( *args ):
    """
    This is the function that will be called whenever the ui, passed to the MUiMessage.addUiDeletedCallback( window, uiDeleteCallback )
    is deleted
    """
    makeTestWindow(True)
    cmds.confirmDialog(message = "The Window Was Closed!!!")  


# make a test window
win = makeTestWindow(False)

# create the callback to run when the ui is deleted
uiCallBack = OpenMayaUI.MUiMessage.addUiDeletedCallback( win, uiDeleteCallback )

#8

A simpler method would be to do this:

import maya.cmds as cmds

def window_with_death_notify():
    w = cmds.window()
    c = cmds.columnLayout()
    b = cmds.button()
    
    def death_notice(*_):
        print "window " + w + " was deleted"
    
    cmds.scriptJob(uiDeleted = (w, death_notice), runOnce=True)
    
    cmds.showWindow(w)
    
    
window_with_death_notify()