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\Lib\site-packages\maya\app\general\shelfEditorWindow.py
.../lib/python2.6/site-packages/maya/app/general/shelfEditorWindow.py
.../Maya.app/Contents/Frameworks/Python.framework/Versions/2.6/lib/python2.5/site-packages/maya/app/general
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.
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.
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))
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.
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.
# 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.