I’m in 2014 currently. I’ve been writing in maxscript for over 8 years now and I’ve been wanting to port a lot of my tools over to maya. Could someone show me how to create this simple UI example I’ve attached, using python? It would answer a lot of my questions in regards to making a layout with better placement. Attached is my layout concept. If you could just recreate this using python for maya 2014 it would really help me better understand how to create layouts similar to this. Thank you very much everyone.
maya python ui
This should provide a good starting point when working with formLayouts. It just uses buttons but the setup is the same for other controls as well.
class TestUi(object):
WINDOW_NAME = "czTestUi"
FORM_OFFSET = 2
@classmethod
def display(cls):
cls.delete_window()
main_window = cmds.window(cls.WINDOW_NAME, title=cls.WINDOW_NAME, rtf=True)
main_layout = cmds.formLayout(parent=main_window)
# Create buttons
button01 = cmds.button(label="Button 01", parent=main_layout)
button02 = cmds.button(label="Button 02", parent=main_layout)
button03 = cmds.button(label="Button 03", parent=main_layout)
button04 = cmds.button(label="Button 04", parent=main_layout)
button05 = cmds.button(label="Button 05", parent=main_layout)
button06 = cmds.button(label="Button 06", parent=main_layout)
# Create layout
cmds.formLayout(main_layout, edit=True, af=(button01, "top", cls.FORM_OFFSET));
cmds.formLayout(main_layout, edit=True, af=(button01, "left", cls.FORM_OFFSET));
cmds.formLayout(main_layout, edit=True, ap=(button01, "right", 0, 50))
cmds.formLayout(main_layout, edit=True, aoc=(button02, "top", 0, button01));
cmds.formLayout(main_layout, edit=True, ac=(button02, "left", 0, button01))
cmds.formLayout(main_layout, edit=True, af=(button02, "right", cls.FORM_OFFSET));
cmds.formLayout(main_layout, edit=True, ac=(button03, "top", 0, button01));
cmds.formLayout(main_layout, edit=True, af=(button03, "left", cls.FORM_OFFSET));
cmds.formLayout(main_layout, edit=True, af=(button03, "right", cls.FORM_OFFSET))
cmds.formLayout(main_layout, edit=True, ac=(button04, "top", 0, button03));
cmds.formLayout(main_layout, edit=True, af=(button04, "left", cls.FORM_OFFSET));
cmds.formLayout(main_layout, edit=True, ap=(button04, "right", 0, 33))
cmds.formLayout(main_layout, edit=True, aoc=(button05, "top", 0, button04));
cmds.formLayout(main_layout, edit=True, ac=(button05, "left", 0, button04))
cmds.formLayout(main_layout, edit=True, ap=(button05, "right", 0, 66))
cmds.formLayout(main_layout, edit=True, aoc=(button06, "top", 0, button04));
cmds.formLayout(main_layout, edit=True, ac=(button06, "left", 0, button05))
cmds.formLayout(main_layout, edit=True, af=(button06, "right", cls.FORM_OFFSET));
cmds.showWindow(main_window)
@classmethod
def delete_window(cls):
if cmds.window(cls.WINDOW_NAME, exists=True):
cmds.deleteUI(cls.WINDOW_NAME, window=True)
if __name__ == "__main__":
TestUi.display()
-ChrisZ
The first answer is great, and over the long haul formLayout is the way to go. As you can see from the example, however, formLayout has a lot of irritating extra formatting commands to worry about.
For your example I might try something more like this, using just columnLayout and gridLayout to avoid all the extra formlayout management while learning.
import maya.cmds as cmds
win = cmds.window(width=256)
main_col = cmds.columnLayout() # everything flows vertically from here
top_section = cmds.gridLayout(nc = 2 , nr =2, cw = 128) # 2x label, control
cmds.text("label1")
button_1 = cmds.button("button")
cmds.text ("spinner label")
float_1 = cmds.floatSlider("floatfield")
cmds.setParent(main_col) # end grid, now back in main column
cmds.button("Big wide button", w=256)
cmds.gridLayout(nc = 3 , nr =1, cw = 85) # 3 checkboxes
cb1 = cmds.checkBox('cb 1')
cb2 = cmds.checkBox('cb 1')
cb3 = cmds.checkBox('cb 1')
cmds.setParent(main_col)
cmds.gridLayout(nc = 2 , nr =1, cw = 128) # 2 buttons
b_button_1 = cmds.button('button_l')
b_button_2 = cmds.button('button_r')
cmds.setParent(main_col)
cmds.gridLayout(nc = 2 , nr =1, cw = 128, ch=128) # label and big button
cmds.text('label')
fat_button= cmds.button('fat button')
cmds.setParent(main_col)
cmds.showWindow(win)
You’ll find that the formLayout example does more sophisticated things, like letting you control what happens when you resize a window – it just comes at the cost of a lot of tedious format commands.
If you’re just getting into this you should definitely check out this Tech-artists.org thread (and the ones linked to about half way down):
You might also find this useful as a starting place for complaining about Maya’s built in GUI.
http://techartsurvival.blogspot.com/2014/02/pity-for-outcast.html