Search code examples
pythonuser-interfacemaya

Maya Python UI appear in Attribute Editor


It's my first time being here and I've been struggling with python coding dealing with figuring it out how to update stuff per action or mouse event.

Lately, whenever I tried to test out my script, I often see some of the buttons and layout panels in the Attribute Editor, when it's suppose to be in the window I've created. how can I make that stop?

I don't think I could post the code in here since it's about 1000 code long, but how can I find a way to prevent something like that? Is it because I used too much setParent('..') function?


Solution

  • If your buttons etc are appearing in the wrong layout, it is probably because you're calling UI commands after some other function has reset the existing parent.

    If you want to be sure your controls are going to the right place you'll need to store the names of any windows/layouts/panels you've created and explicitly set them to be the parent before you start making widgets. Otherwise the parenting is basically 'whatever got created last'. You can verify that by something like this:

    # make a button out of context
    import maya.cmds as cmds
    xxx = cmds.button('boo')
    
    # ask the parent of what we just made....
    print cmds.control(xxx, q=True, p=True)
    
    ## Result: u'MayaWindow|MainAttributeEditorLayout|formLayout2|AEmenuBarLayout|AErootLayout|AEselectAndCloseButtonLayout' # 
    

    Parentage will be switched if you create a top level container (a window or panel):

    w = cmds.window()
    c = cmds.columnLayout() 
    b = cmds.button("bar")
    
    # ask b's parent....
    print cmds.control(b, q=True, p=True)
    ## Result: window3|columnLayout49  #
    

    You can also switch parents explicitly:

    def make_a_layout(window_name):
        w = cmds.window(window_name)
        c = cmds.columnLayout()
        return c
    
    layout_a = make_a_layout('window_a')
    # any future widgets go into this layout...
    print cmds.button("layout 1 a") 
    #     window_a|columnLayout55|layout_1_a
    
    layout_b = make_a_layout('window_b')
    # now this is the active layout
    print cmds.button("layout 2 a ")  
    #     window_b|columnLayout56|layout_2_a
    
    # explicitly set the parent to the first layout
    # now new widgets will be there
    cmds.setParent(layout_a)
    print cmds.button("layout 1 b")
    #  window_a|columnLayout56|layout_1_b
    

    As you can see, current parent is set every time a new layout is created. You can pop up a level with setParent ('..') or set it to any layout explicitly with setParent('your_layout_here').