Maya's dockableWorkspaceWidget.py and deleting the UI upon closing maya

Hi,

I am using the dockableWorkspaceWidget.py from maya’s devkit as a base for dockable window I am creating. I am having an issue though that if I leave the window open and close maya, then re launch maya, the window is still open (I would rather not have it automatically open), and the window is completely blank (widgets are all missing). Trying to relaunch the window will just cause errors.

Does anyone know how to make sure this UI is deleted upon closing maya, and so it does not re open when maya is re launched.

for reference

# Copyright 2017 Autodesk, Inc. All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk license
# agreement provided at the time of installation or download, or which
# otherwise accompanies this software in either electronic or hard copy form.
import maya.cmds as cmds
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
from maya import OpenMayaUI as omui
from PySide2 import QtWidgets, QtCore, QtGui

if not 'customMixinWindow' in globals():
    customMixinWindow = None

''' MayaQWidgetDockableMixin is not a workspace control in itself, it is added 
    as a child to a created workspace control. See help(MayaQWidgetDockableMixin) 
    for more details. The following class is a simple widget with a layout and  a push button.
''' 
class DockableWidget(MayaQWidgetDockableMixin, QtWidgets.QDialog):
    def __init__(self, parent=None):

        super(DockableWidget, self).__init__(parent=parent)

        self.button1 = QtWidgets.QPushButton()
        self.button1.setText('PushMe')
        self.setSizeGripEnabled(False)
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.button1)
        self.setLayout(layout)
        self.setWindowTitle('Custom Maya Mixin Workspace Control')    

''' A workspace control is created by calling show() on the DockableWidget class. 
    This control is only created once if the retain property is set to true, which 
    is the default. The uiScript argument passed to the show() method will be 
    invoked every time this control is opened/restored. It is recommended to add 
    the proper import statement in the uiScript argument.
    
    For example, in renderSetup.py file the UIScript for renderSetupWindow is
      "uiScript='import maya.app.renderSetup.views.renderSetup as renderSetup\nrenderSetup.createUI(restore=True)'"
        
    The following method needs to be invoked in order to create the workspace control for the example above.
    If the control is being restored, then Maya will call the method by passing restore=True
'''  

def DockableWidgetUIScript(restore=False):
    print restore
    global customMixinWindow
    ''' When the control is restoring, the workspace control has already been created and
        all that needs to be done is restoring its UI.
    '''
    if restore == True: 
        # Grab the created workspace control with the following.
        restoredControl = omui.MQtUtil.getCurrentParent()
        print restoredControl
    
    if not customMixinWindow:
        # Create a custom mixin widget for the first time
        customMixinWindow = DockableWidget()     
        customMixinWindow.setObjectName('customMayaMixinWindow')

    if restore == True:
        # Add custom mixin widget to the workspace control
        mixinPtr = omui.MQtUtil.findControl(customMixinWindow.objectName())
        omui.MQtUtil.addWidgetToMayaLayout(long(mixinPtr), long(restoredControl))
    else:
        # Create a workspace control for the mixin widget by passing all the needed parameters. See workspaceControl command documentation for all available flags.
        customMixinWindow.show(dockable=True, area='right', floating=False, height=900, width=400, uiScript='DockableWidgetUIScript(restore=True)')
        cmds.workspaceControl('customMayaMixinWindowWorkspaceControl', e=True, ttc=["AttributeEditor", -1], wp="preferred", mw=200)
        return customMixinWindow
 
''' Using the workspaceControl Maya command to query/edit flags about the created 
    workspace control can be achieved this way:
        maya.cmds.workspaceControl('customMayaMixinWindowWorkspaceControl', q=True, visible=True)
        maya.cmds.workspaceControl('customMayaMixinWindowWorkspaceControl', e=True, visible=False)
        
    Note that Maya automatically appends the "WorkspaceControl" string to the 
    workspace control child object name. In this example it is the child widget name (customMayaMixinWindow)
'''

def start_test_ui():
    ui = DockableWidgetUIScript()
    return ui

You might need to make sure that before you try to relaunch the window, you’re deleting the old one. I have a docked window in the same format that you do, and here’s my main code.

# -*- coding: utf-8 -*-

from PySide2 import QtCore, QtWidgets, QtGui
import maya.cmds as cmds
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
#import our widget to run

from AMS_Widget import AMS_Widget as CoreWidget


def deleteControl(control):
    if cmds.workspaceControl(control, q=True, exists=True):
        cmds.workspaceControl(control,e=True, close=True)
        cmds.deleteUI(control,control=True)

class dockableAMSWidget(MayaQWidgetDockableMixin, QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(dockableAMSWidget, self).__init__(parent=parent)
        #set the name and title of the widget
        self.setObjectName('dockableAMSWidget')
        self.setWindowTitle('Database Asset Search')
        self.setLayout(self.createLayout())

#create a widget and lay it out vertically
def createLayout(self):
    self.main_layout = QtWidgets.QVBoxLayout()
    self.central_widget = CoreWidget(parent=self)
    self.main_layout.addWidget(self.central_widget)
    return self.main_layout


def makeCoreWidgetMain():
    #delete any pre-existing widget before making a new one
    deleteControl("dockableAMSWidgetWorkspaceControl")
    Core = dockableAMSWidget()


    #configure and show the new widget
    Core.show(dockable=True, floating=False, area="right", allowedArea="right")
    cmds.workspaceControl("dockableAMSWidgetWorkspaceControl", e=True , ttc=["AttributeEditor", 0], wp="preferred", mw=420)
    #bring to the front
    Core.raise_()

I hope that helped!

Awesome that worked! thanks!

Whats funny is i did try that, but I think something else in my code was causing it to relaunch when opening maya again.

figured it out, was the uiscript flag in the .show that was causing it to relaunch with maya

ahhhh, wonderful. Glad you got it figured out! : D