Creating an editor window using Python
 
 
 

If you want to create your own UI in Maya, we recommend that you use Python. The following are some helpful tips. We recommend that you use the shelfEditorWindow.py file as a start and customize it accordingly. You can find the shelfEditorWindow.py file in the Maya installation directory, under the following path:

Python advantages

Python is object oriented, making it easier for you to keep track of the names of your UI controls. It does not require the use of global variables like MEL does.

Python is generally more robust and extensible.

Using Python to create your own editor window

We recommend that you use the shelfEditorWindow.py file as a start and customize it using the following guidelines:

Using partial functions to bind a UI element as a constant to a function

You can use additional arguments with callbacks if you bind the argument using a partial function.

Notepartial is part of the python language. For more information, see the functools module in the official Python docs: http://www.python.org/doc/

For example, you can bind the UI element (currShelf) to the function:

queryItem = partial(cmds.shelfLayout, currShelf, query=True)

In this example, you have a method that moves the shelf up or down, and you can pass the parameter direction (that is, -1 and 1) to the partial function:

cmds.symbolButton(image="moveLayerUp.xpm", annotation=self.moveUpStr, command=partial(self.moveShelf, -1))
cmds.symbolButton(image="moveLayerDown.xpm", annotation=self.moveDownStr, command=partial(self.moveShelf, 1))
TipTo load the code for the first time, do the following:
import maya.app.general.shelfEditorWindow as shelfEditorWindow

To reload the code after having made edits, do the following:

import sys
reload(sys.modules['maya.app.general.shelfEditorWindow'])

Changing the content of a window via a variable

In MEL, the UI names are stored in global variables, or would be hardcoded. You must know the name of the UI before you can change its layout, and if you change the UI name, you must change each instance of the UI name. Otherwise, the code will break.

By comparison, in Python, you can assign a variable name to the UI and manipulate the UI using the variable name, which does not change. See the createShelfData() function in shelfEditorWindow.py file:

self.wItemTip = cmds.textFieldGrp(
  label=_L10N(kItemTooltip, "Tooltip:"),
  columnWidth=(1, self.col1width),
  columnAttach=[(1, "right", 5), (2, "both", 0)],
  changeCommand=self.itemData)

Using this example, you can use your own variables to customize your window.

Printing debug info

You can define a verbose variable, turn it on for debugging and print out your error messages for the window. Turn it off for shipping. Refer to the verbose variable and the info() function in the shelfEditorWindow.py example for more information.

TipBecause there are no global variables, you can easily write code that creates multiple instances of the same window. For example, you can open one window in default mode and another copy of the same window that prints out debugging messages. Example as follows:
# Open the window in default mode
win = shelfEditorWindow.shelfEditorWindow()
win.create()
# Open another copy of the window with debugging messages printed
win2 = shelfEditorWindow.shelfEditorWindow(windowName='secondCopy')
win2.verbose = True
win2.create()

Using helper functions to create layout of window

In Python, it is easy to manipulate arrays and write a general method that works for any number of children. You can pass multiple children as an array. It is easier to write loops and perform array manipulation in Python than it is in MEL. In the following example, you create a column that attaches children to its left or right. The last element is resizable.

def makeResizableColumn(self, form, children, left=None, right=None)

Using named arguments to create layout of window

Python has default parameters, but MEL does not. Using default parameters, you can create different variations of the same layout. In the makeResizableColumn example listed above, you can use the left and right parameters to design the layout of your UI and specify which elements should be left or right justified. For example:

self.makeResizableColumn(form,[buttons, self.wShelfName, self.wShelfList],left=[False, True, True])

This way, you can change the layout of your UI without changing the code.