LumaPictures / pymel

Python in Maya Done Right
Other
479 stars 130 forks source link

pathButtonGrp / folderButtonGrp creates two instances #425

Open mwspace opened 4 years ago

mwspace commented 4 years ago

pathButtonGrp and folderButtonGrp will create two instances when a parent is specified, although the second instance in the UI is non-functional. textFieldGrp doesn't have the same problem.

def make_window():

    win = pm.window()
    fl = pm.frameLayout()
    layout = pm.formLayout(parent=fl)    
    fbg = pm.pathButtonGrp(parent=layout)

    layout.redistribute()
    for x in layout.getChildren():
        print x
    return win

import pymel.core as pm
w = make_window()
w.show()

Printed output:

window6|frameLayout53|formLayout122|textFieldButtonGrp15
window6|frameLayout53|formLayout122|window6_frameLayout53_formLayout122_textFieldButtonGrp15

When the parent isn't specified, only one is created...

window7|frameLayout54|formLayout123|textFieldButtonGrp16

As far as my primitive understanding goes, the problem is in pymel.core.uitypes.PyUI.__new__(), which creates the extra, non-functional instance despite receiving create=False.

I've worked around for myself it by pushing parent=None into kwargs before the super but I'm not smart enough to know what damage I might be doing...

class PathButtonGrp(TextFieldButtonGrp):
    PROMPT_FUNCTION = 'promptForPath'

    def __new__(cls, name=None, create=False, *args, **kwargs):

        if create:
            import windows

            kwargs.pop('bl', None)
            kwargs['buttonLabel'] = 'Browse'
            kwargs.pop('bc', None)
            kwargs.pop('buttonCommand', None)

            name = cmds.textFieldButtonGrp(name, *args, **kwargs)

            promptFunction = getattr(windows, cls.PROMPT_FUNCTION)

            def setPathCB(name):
                f = promptFunction()
                if f:
                    cmds.textFieldButtonGrp(name, e=1, text=f, forceChangeCommand=True)

            import windows
            cb = windows.Callback(setPathCB, name)
            cmds.textFieldButtonGrp(name, e=1, buttonCommand=cb)

        kwargs['parent']=None #Hack with unknown consequences
        return super(PathButtonGrp, cls).__new__(cls, name, create=False, *args, **kwargs)