Need help with formLayout!


#1

Hey, guys . Trying to make UI for one of my first simple tools. Can anybody help?
I am trying to use formLayout to make UI and I am having troubles to make it scalable.
Biggest problem is to wrap up my head around flag “attachPosition”, don’t understand exactly how it works.
I will put my script of GUI down below, maybe somebody can understand which part I am lacking or doing not right. Thanks a lot!

import maya.cmds as cmds
class hairToolWindow(object):
def showUI(cls):
win = cls()
win.create()
return win
def init(self):
self.window = ‘ar_optionsWindow’
self.title = ‘OptionsWindow’
self.size = (546,350)
self.supportsToolAction = False
self.actionName = ‘Apply and Close’
self.applyName = ‘Apply’
self.closeName = ‘Close’

def create(self):
    if cmds.window(self.window, exists= True):
        cmds.deleteUI(self.window ,window= True)
    self.window = cmds.window(self.window, title= self.title, widthHeight= self.size, menuBar= True)
    self.mainFormLayout = cmds.formLayout(nd=100)
    self.ui_commonMenuCmd()
    self.ui_commonButtonsCmd()
    self.ui_hairListCmd()
    cmds.showWindow()
    
def ui_commonMenuCmd(self):
    self.editMenu = cmds.menu(label= 'Edit')
    
    self.editMenuSave = cmds.menuItem(label= 'Save Settings', command= self.ui_editMenuSaveCmd)
    self.editMenuReset = cmds.menuItem(label= 'Reset Settings', command= self.ui_editMenuResetCmd)
    
    self.editMenuDiv = cmds.menuItem(d= True)
    self.editMenuRadio = cmds.radioMenuItemCollection()
    
    self.editMenuTool = cmds.menuItem(label= 'As Tool', radioButton= True, enable= self.supportsToolAction, command= self.ui_editMenuToolCmd)
    self.editMenuAction = cmds.menuItem(label= 'As Action', radioButton= True, enable= self.supportsToolAction, command= self.ui_editMenuActionCmd)
    
    self.helpMenu = cmds.menu('Help')
    self.helpMenuItem = cmds.menuItem(label= 'Help on {0}'.format(self.title), command= self.ui_helpMenuCmd)
def ui_commonButtonsCmd(self):
    self.commonBtnSize = ((self.size[0]-18)/3, 26)
    self.actionBtn = cmds.button(label=self.actionName, height=self.commonBtnSize[1], command=self.ui_actionBtnCmd)
    self.applyBtn = cmds.button(label=self.applyName, height=self.commonBtnSize[1], command=self.ui_applyBtnCmd)
    self.closeBtn = cmds.button(label=self.closeName, height=self.commonBtnSize[1], command=self.ui_closeBtnCmd)
    cmds.formLayout(self.mainFormLayout, e= True,
                        attachForm = ([self.actionBtn, 'left', 5], [self.actionBtn, 'bottom', 5], [self.applyBtn, 'bottom', 5], [self.closeBtn, 'bottom', 5], [self.closeBtn, 'right',5]),
                        attachPosition = ([self.actionBtn, 'right', 1, 33], [self.closeBtn, 'left', 0, 67]),
                        attachControl = ([self.applyBtn, 'left', 4, self.actionBtn],[self.applyBtn, 'right', 4, self.closeBtn]),
                        attachNone = ([self.actionBtn, 'top'], [self.applyBtn, 'top'], [self.closeBtn, 'top']))

def ui_helpMenuCmd(self, *args):
    cmds.launch(web= 'http://google.com')
    
def ui_editMenuSaveCmd(self, *args):
    pass

def ui_editMenuResetCmd(self, *args):
    pass
    
def ui_editMenuToolCmd(self, *args):
    pass

def ui_editMenuActionCmd(self, *args):
    pass
    
def ui_actionBtnCmd(self,*args):
    self.applyBtnCmd()
    self.closeBtnCmd()

def ui_applyBtnCmd(self, *args):
    pass
    
def ui_closeBtnCmd(self, *args):
    cmds.deleteUI(self.window, window=True)
def appendItemsCmd(self,*args):
    pass
def removeItemsCmd(self,*args):
    pass
def ui_hairCheckCmd(self,*args):
    pass
    #self.hairFrontCheckBox = cmds.checkBox(label='Front')

def ui_hairListCmd(self,*args):
    self.hairListSize = (self.size[0]-30)/3
    self.hairFrontFrameLayoutA = cmds.frameLayout(label='               Front Hair', backgroundColor=[0.0, 0.0, 1.0], font='boldLabelFont', labelVisible=True, labelAlign='center', parent=self.mainFormLayout)
    self.hairFrontFrameLayoutB = cmds.frameLayout(label='             Load example:', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True, labelAlign='center')
    self.hairFrontFrameLayoutC = cmds.frameLayout(label='"hairFrontA_jtA_L/R", "*_jtB_*", ...', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True, labelAlign='left')
    self.hairFrontTabLayout = cmds.textScrollList(allowMultiSelection=True, width=self.hairListSize, parent= self.hairFrontFrameLayoutC)
    self.hairFrontRowLayout = cmds.rowLayout(numberOfColumns=2, adjustableColumn=True, parent= self.hairFrontFrameLayoutC)
    self.hairFrontLoadButton = cmds.button(label='+', parent=self.hairFrontRowLayout, command= self.appendItemsCmd)
    self.hairFrontRemoveButton = cmds.button(label='-', parent=self.hairFrontRowLayout, command= self.removeItemsCmd)
    ######################################################################################################################################################################################################################
    self.hairSideFrameLayoutA = cmds.frameLayout(label='               Side Hair', backgroundColor=[0.0, 0.0, 1.0], font='boldLabelFont', labelVisible=True, parent=self.mainFormLayout)
    self.hairSideFrameLayoutB = cmds.frameLayout(label='             Load example:', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairSideFrameLayoutC = cmds.frameLayout(label='"hairSideA_jtA_L/R", "*_jtB_*", ...', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairSideTabLayout = cmds.textScrollList(allowMultiSelection=True, width=self.hairListSize, parent= self.hairSideFrameLayoutC)
    self.hairSideRowLayout = cmds.rowLayout(numberOfColumns=2, adjustableColumn=True, parent= self.hairSideFrameLayoutC)
    self.hairSideLoadButton = cmds.button(label='+', parent=self.hairSideRowLayout, command= self.appendItemsCmd)
    self.hairSideRemoveButton = cmds.button(label='-', parent=self.hairSideRowLayout, command= self.removeItemsCmd)
    ######################################################################################################################################################################################################################
    
    self.hairBackFrameLayoutA = cmds.frameLayout(label='               Back Hair', backgroundColor=[0.0, 0.0, 1.0], font='boldLabelFont', labelVisible=True, parent=self.mainFormLayout)
    self.hairBackFrameLayoutB = cmds.frameLayout(label='             Load example:', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairBackFrameLayoutC = cmds.frameLayout(label='"hairBackA_jtA_L/R", "*_jtB_*", ...', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairBackTabLayout = cmds.textScrollList(allowMultiSelection=True, width=self.hairListSize, parent= self.hairBackFrameLayoutC)
    self.hairBackRowLayout = cmds.rowLayout(numberOfColumns=2, adjustableColumn=True, parent= self.hairBackFrameLayoutC)
    self.hairBackLoadButton = cmds.button(label='+', parent=self.hairBackRowLayout, command= self.appendItemsCmd)
    self.hairBackRemoveButton = cmds.button(label='-', parent=self.hairBackRowLayout, command= self.removeItemsCmd)
    ######################################################################################################################################################################################################################
    
    self.hairNeckFrameLayoutA = cmds.frameLayout(label='               Neck Hair', backgroundColor=[0.0, 0.0, 1.0], font='boldLabelFont', labelVisible=True, parent=self.mainFormLayout)
    self.hairNeckFrameLayoutB = cmds.frameLayout(label='             Load example:', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairNeckFrameLayoutC = cmds.frameLayout(label='"hairNeckA_jtA_L/R", "*_jtB_*", ...', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairNeckTabLayout = cmds.textScrollList(allowMultiSelection=True, width=self.hairListSize, parent= self.hairNeckFrameLayoutC)
    self.hairNeckRowLayout = cmds.rowLayout(numberOfColumns=2, adjustableColumn=True, parent= self.hairNeckFrameLayoutC)
    self.hairNeckLoadButton = cmds.button(label='+', parent=self.hairNeckRowLayout, command= self.appendItemsCmd)
    self.hairNeckRemoveButton = cmds.button(label='-', parent=self.hairNeckRowLayout, command= self.removeItemsCmd)
    ######################################################################################################################################################################################################################
    
    self.hairTopFrameLayoutA = cmds.frameLayout(label='               Top Hair', backgroundColor=[0.0, 0.0, 1.0], font='boldLabelFont', labelVisible=True, parent=self.mainFormLayout)
    self.hairTopFrameLayoutB = cmds.frameLayout(label='             Load example:', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairTopFrameLayoutC = cmds.frameLayout(label='"hairTopA_jtA_L/R", "*_jtB_*", ...', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairTopTabLayout = cmds.textScrollList(allowMultiSelection=True, width=self.hairListSize, parent= self.hairTopFrameLayoutC)
    self.hairTopRowLayout = cmds.rowLayout(numberOfColumns=2, adjustableColumn=True, parent= self.hairTopFrameLayoutC)
    self.hairTopLoadButton = cmds.button(label='+', parent=self.hairTopRowLayout, command= self.appendItemsCmd)
    self.hairTopRemoveButton = cmds.button(label='-', parent=self.hairTopRowLayout, command= self.removeItemsCmd)
    ######################################################################################################################################################################################################################
    
    self.hairExtraFrameLayoutA = cmds.frameLayout(label='               Extra Hair', backgroundColor=[0.0, 0.0, 1.0], font='boldLabelFont', labelVisible=True, parent=self.mainFormLayout)
    self.hairExtraFrameLayoutB = cmds.frameLayout(label='             Load example:', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairExtraFrameLayoutC = cmds.frameLayout(label='"hairExtraA_jtA_L/R", "*_jtB_*", ...', backgroundColor=[0.5, 0.5, 0.5], font='smallBoldLabelFont', labelVisible=True)
    self.hairExtraTabLayout = cmds.textScrollList(allowMultiSelection=True, width=self.hairListSize, parent= self.hairExtraFrameLayoutC)
    self.hairExtraRowLayout = cmds.rowLayout(numberOfColumns=2, adjustableColumn=True, parent= self.hairExtraFrameLayoutC)
    self.hairExtraLoadButton = cmds.button(label='+', parent=self.hairExtraRowLayout, command= self.appendItemsCmd)
    self.hairExtraRemoveButton = cmds.button(label='-', parent=self.hairExtraRowLayout, command= self.removeItemsCmd)
    ######################################################################################################################################################################################################################
    
    cmds.formLayout(self.mainFormLayout, edit= True,
            attachForm=([self.hairFrontFrameLayoutA, 'top', 2], 
                        [self.hairFrontFrameLayoutA, 'left', 4],
                        [self.hairFrontFrameLayoutA, 'bottom', 40],
                        ##########################################
                        [self.hairSideFrameLayoutA, 'top', 2],
                        [self.hairSideFrameLayoutA, 'bottom', 40],
                        ##########################################
                        [self.hairBackFrameLayoutA, 'top', 2],
                        [self.hairBackFrameLayoutA, 'bottom', 40],
                        ##########################################
                        [self.hairNeckFrameLayoutA, 'top', 2],
                        [self.hairNeckFrameLayoutA, 'bottom', 40],
                        ##########################################
                        [self.hairTopFrameLayoutA, 'top', 2],
                        [self.hairTopFrameLayoutA, 'bottom', 40],
                        ##########################################
                        [self.hairExtraFrameLayoutA, 'top', 2], 
                        [self.hairExtraFrameLayoutA, 'right', 4],
                        [self.hairExtraFrameLayoutA, 'bottom', 40]),
            
            attachPosition=([self.hairFrontFrameLayoutA, 'right', 1, 10],
                            [self.hairSideFrameLayoutA, 'right', 1, 30],
                            [self.hairBackFrameLayoutA,'right', 1, 50],
                            [self.hairNeckFrameLayoutA,'left', 1, 50],
                            [self.hairTopFrameLayoutA, 'left', 1, 70],
                            [self.hairExtraFrameLayoutA,'left', 1, 85]),
            
            attachControl=([self.hairSideFrameLayoutA, 'left', 2, self.hairFrontFrameLayoutA],
                           ##################################################################
                           [self.hairBackFrameLayoutA, 'left', 2, self.hairSideFrameLayoutA],
                           ##################################################################
                           [self.hairNeckFrameLayoutA, 'right', 2, self.hairTopFrameLayoutA],
                           ##################################################################
                           [self.hairTopFrameLayoutA, 'right', 2, self.hairExtraFrameLayoutA]))

testWindow = hairToolWindow()
testWindow.create()