Search code examples
pythonpyqtmaya

Maya UI not docking after rerunning script


I have made a UI window for Autodesk Maya 2015. Every time I run the script within Maya's script editor, it works fine, the UI gets launched and it docks properly on the main Maya window.

However, after closing the UI window and rerunning the script, the window doesn't dock anymore. It gets stuck on the main screen and I have to close the whole software. It seems that the UI can't find the main Maya Window anymore after I close it the first time round. I'm not sure how to fix this. Can anyone give me some advice on how to fix this problem?

Here is my code:

def getMayaWindow():
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)

class pipeWindow(formClass,baseClass):
    def __init__(self, parent=getMayaWindow()):
        super(pipeWindow,self).__init__(parent) 

        self.setupUi(self)
        self.setObjectName('pipe_window')

        try:
            cmds.deleteUI('dockPane')
        except:
            pass                
        self.pane = cmds.paneLayout('dockPane', cn='single')

        if 'pipeDock' not in cmds.lsUI(ctl=1) :
            cmds.dockControl('pipeDock', con=self.pane, area='right', 
                              allowedArea = ['right','left'], label = 
                             "ANMD_Pipeline", w=365)            
        else:
            pass
        cmds.control( 'pipe_window', e=True, p=self.pane)

Solution

  • After trying out multiple things, I have managed to resolve this issue. I have used

    if cmds.dockControl('pipeDock', q=1, ex=1):
        cmds.deleteUI('pipeDock')
    

    Then I have a super(className, self).closeEvent(event) inside a closeEvent function. This did the trick for me. This did the trick for me. Previously I had the cmds.deleteUI within my closeEvent function which did not work, I suspect it was because I did not close the Event itself, so the UI just hides itself whenever I press the cross button instead of deleting it.