Friday, November 11, 2011

Auto-placing Toolbars

I fiddled around with Maya 2012's toolbar options a bit more today, and learned a few interesting things. The most interesting of all was the discovery of a way to auto-load a custom toolbar when Maya opens, and have it appear in the same position that it was in when you last closed the program. (This is all in Python, by the way; I'm not sure if it would work the exact same way in MEL.)

Saving Toolbar Positions
The first thing I had to figure out was how Maya saved toolbar placement. When I wrote the other day about fixing a co-worker's missing tool settings panel, I mentioned the discovery of Maya's startupMainWindowState settings file. I also found some flags on the windowPrefs command for saving and restoring these settings. Today I learned that when using those flags, the full absolute path to the file should not be passed. Maya only wants the name of the file; it already knows where it lives. So to save, it's as simple as this:


import maya.cmds as mc
mc.windowPref(saveMainWindowState="startupMainWindowState")

Maya also saves the toolbar settings automatically when it closes, and we'll take advantage of this as we move forward.

Restoring the Toolbar
Immediately after you have built your interface and wrapped it up in a toolbar, restoring Maya's window state settings will move the toolbar back into its original position (assuming it had an original position when the settings were last saved).  Here's a quick example:



import maya.cmds as mc
window = mc.window("myWindow")
column = mc.columnLayout(p=window)
button = mc.button(p=column, l="Click me!",
                   c="print('clicked!')")
toolbar = mc.toolbar("myToolbar", content=window, label="Clicker",
                     area="top")
mc.windowPref(restoreMainWindowState="startupMainWindowState")



Naming the toolbar is important, as this is what will allow you to re-position that toolbar automatically between sessions.  If it doesn't have a consistent name, it won't work.

Obviously the last line won't do much the first time that you run it, because there is no saved position for this toolbar.  However, move the toolbar to some other position, then close Maya, which will save the toolbar's position.  Open Maya, run the same code, and the toolbar should re-spawn in the same place where you moved it.

The next step is to make Maya auto-load your toolbar each time that it starts.

Auto-Loading the Toolbar
Maya looks for  -- and executes the contents of -- two files each time that it starts: userSetup.mel and userSetup.py.  If used, these files should be placed in Maya's "scripts" folder.  In this case, we're going to use the Python startup file to kick off our toolbar.

First, we'll need to wrap our code inside a function, and save it in a file.  Copy the following into a new file in your favorite text editor:



import maya.cmds as mc

def main():
    window = mc.window("myWindow")
    column = mc.columnLayout(p=window)
    button = mc.button(p=column, l="Click me!",
                       c="print('clicked!')")
    toolbar = mc.toolbar("myToolbar", content=window,
                         label="Clicker", area="top")
    mc.windowPref(restoreMainWindowState="startupMainWindowState")


Save this file as "myToolbar.py" into Maya's prefs/scripts folder, which is one of the places in the sys.path setting of Maya's embedded Python.

Now start a new text file, and add this to it:

import maya.cmds as mc
import myToolbar
mc.evalDeferred(myToolbar.main)

Save this file as "userSetup.py" into Maya's scripts folder.

The reason we're using "evalDeferred" is because Maya needs to wait to execute the UI code until it has a UI to work with.  If you don't defer the execution, Maya will crash before it even opens.

Close Maya and re-open it.  Your custom toolbar should automatically appear where you last placed it.


1 comment:

Custom Toolbar Development said...

Auto-Loading the Toolbar is very useful for programmer who is start work from last closed without any trouble.