Button placement - novice skills


#1

Hi,

I’m trying to understand why I’m getting the response I’m getting :banghead:

For the tool I’m writing I am trying form layout. I have a temp buttons in just to get this figured out. I have three forms, Top, left side, right side. The right side is a columnLayout. All I’m trying to do is get new buttons to go into the right side column. Ultimately the user will have lots of things they can add and remove from the right side but I’m just starting out with a button to make it easy.

What I expect to happen is I run the script, it shows a window, I click the “Add A Button” button and it creates a new button. It’s supposed to parent the new button inside the “right side” column. The first button just goes to the upper left corner of the window. Subsequent buttons go into the column though.

This could even be the completely wrong way of having the user add options into a column.

Why doesn’t my first button parent inside the column but all subsequent buttons do?

import maya.cmds as cmds

def mkBtn(pargs):
    temp = cmds.button(label = 'New Button')
    cmds.setParent(rightSide)
    
window = cmds.window(w=200, h=200)
form = cmds.formLayout(numberOfDivisions=100)

topSide = cmds.button(label="Add A Button", command = partial(mkBtn))

leftSide = cmds.button(label = "REPLACE WITH OTHER STUFF", command = partial(mkBtn))

rightSide = cmds.columnLayout(w=100)
cmds.button()
cmds.setParent("..")

cmds.formLayout( form, edit=True, af=[(topSide, 'top', 5), (topSide, 'left', 5), (topSide, 'right', 5), (leftSide, 'bottom', 5),(leftSide, 'left', 5), (rightSide, 'right', 5), (rightSide, 'bottom', 5) ],
                                  ac=[(leftSide,'top',5, topSide), (rightSide, 'top',5,topSide),(leftSide, 'right', 5, rightSide)])

cmds.showWindow( window )

#2

change make button function to:

def mkBtn(pargs):
    global rightSide
    cmds.setParent(rightSide)
    temp = cmds.button(label = 'New Button')

rightSide has to be global first… and you have to specify ‘current’ parent before you make a child control

or you can specify a parent for button when you create it:

def mkBtn(pargs):
    global rightSide
    # cmds.setParent(rightSide)
    temp = cmds.button(label = 'New Button', parent = rightSide)

(there also another ways to do the same)


#3

DenisT,
You are amazing. That’s twice you’ve come through for me.

Thanks buddy.
:bounce:


#4

technically you have to pass more than usually a parent for dynamic button, and usually this content will be available only after interface done, data made and filled, etc.

so the better way to set button command after making a window:

import maya.cmds as cmds
from functools import partial

def mkBtn(*args):
    temp = cmds.button(label = args[0], parent = args[1])
        
window = cmds.window(w=200, h=200)
form = cmds.formLayout(numberOfDivisions=100)

topSide = cmds.button(label="Add A Button")

leftSide = cmds.button(label = "REPLACE WITH OTHER STUFF")

rightSide = cmds.columnLayout(w=100)
cmds.button()
cmds.setParent("..")

cmds.formLayout( form, edit=True, af=[(topSide, 'top', 5), (topSide, 'left', 5), (topSide, 'right', 5), (leftSide, 'bottom', 5),(leftSide, 'left', 5), (rightSide, 'right', 5), (rightSide, 'bottom', 5) ],
                                  ac=[(leftSide,'top',5, topSide), (rightSide, 'top',5,topSide),(leftSide, 'right', 5, rightSide)])

cmds.button(topSide, e=1, command = partial(mkBtn, "New Button", rightSide))
cmds.showWindow( window )

#5

it’s always better to pass arguments instead of using a global where it’s possible of course