Search code examples
pythonmaya

Maya Python read and set optionMenu value via variable


I have a module that creates a savedSettings.py file after the user used my tool this file is filled with variables to load into the gui next time the tool is used.

I have some checkboxes and a optionMenu. Reading and setting the variables for the checkboxes is as simple as:

# loadsettings into gui
if os.path.exists(userSettings):
    sys.path.append(toolFolder)
    import savedSettings

    viewCBvalue = savedSettings.viewCheck
    ornamentCBvalue = savedSettings.ornamentCheck
    renderCBvalue = savedSettings.renderCheck

I thought the optionMenu would be the same and wrote:

    encodingOMvalue = savedSettings.encodingCheck

When I now tell the GUI to use the variables:

cmds.checkBoxGrp( 'viewCB', label = 'View: ', value1 = viewCBvalue)
cmds.checkBoxGrp( 'ornamentCB', label = 'Show Ornaments: ', value1 = ornamentCBvalue)
cmds.checkBoxGrp( 'renderCB', label = 'Render offscreen: ', value1 = renderCBvalue)

cmds.optionMenuGrp( 'encodingOM', label = 'Encoding ', value = encodingOMvalue )
cmds.menuItem( 'tif', label = 'tif')
cmds.menuItem( 'jpg', label = 'jpg')
cmds.menuItem( 'png', label = 'png')

I get the follwing error:

RuntimeError: Item not found: tif # 

My savedSettings.py looks like this:

# User Settings Savefile: 
viewCheck = False
ornamentCheck = False
renderCheck = False
encodingCheck = "tif"

Would be great if someone explains me what I am doing wrong and how to set variables for the optionMenu.

Thanks for taking the time in advance and have a nice day coding!


Solution

  • Don't do this. instead use mayas internal mechanism, optionVar, for this.

    But if you must do this then know that when you do:

    import savedSettings
    
    • whatever is defined in savedSettings is stored inside savedSettings so if you have the var viewCBvalue then you call it with savedSettings.viewCBvalue. You could load this into main by calling import savedSettings as *, but you really, really do not want to do this!

    • Python import is not a normal function, results get cached.

    • Various other problems. Dont use import for this purpose

    If you do not want to use optionVar, consider using pickle