Passing arguments to uiScript

maya
python

#1

Hi, I’m writing a thing in maya and have run in to trouble. Really don’t know what I did, I was going to adress something else when this happened, the last thing I did was add a button to the layout.
I have been messing around with this for a long time now and as far as I can tell, the uiScript flag doesn’t like arguments passed in the method call…? what happens is, it never sets the restore flag to True so that bit never hits. resulting in it spawning multiple windows in maya. I’m also trying to figure out where the cmds.deleteUI try clause should go, not entrirely sure where I had it before this happened. If anyone could offer any insight I would be most grateful, cheers /S

in the script below, I have replaced an instance of my ui with just a button, it makes no difference on the behaviour.

code:

/////////////////////

from PySide2 import QtWidgets, QtCore
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import maya.OpenMayaUI as mui
import maya.cmds as cmds
import weakref

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

class DockableWidget(MayaQWidgetDockableMixin, QtWidgets.QWidget):


    instances = list()
    CONTROL_NAME = 'customMixinWindow'

    def __init__(self, parent=None):

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

        DockableWidget.delete_instances()
        self.__class__.instances.append(weakref.proxy(self))
        self.main_layout = QtWidgets.QVBoxLayout()
        self.button = QtWidgets.QPushButton()
        self.main_layout.addWidget(self.button)
        self.setLayout(self.main_layout)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

    @staticmethod
    def delete_instances():
        print "deleting"
        for ins in DockableWidget.instances:
            try:
                ins.setParent(None)
                ins.deleteLater()
            except:
                pass
                DockableWidget.instances.remove(ins)
            del ins


def DockableWidgetUIScript(restore=False):

    global customMixinWindow

    if restore == True:
        restoredControl = mui.MQtUtil.getCurrentParent()

    customMixinWindow = DockableWidget()
    if customMixinWindow is None:
        #customMixinWindow = DockableWidget()
        customMixinWindow.setObjectName('customMayaMixinWindow')

    if restore == True:
        mixinPtr = mui.MQtUtil.findControl(customMixinWindow.objectName())
        mui.MQtUtil.addWidgetToMayaLayout(long(mixinPtr), long(restoredControl))

    else:
        try:
            cmds.workspaceControl('customMayaMixinWindowWorkspaceControl', e=True, close=True)
            cmds.deleteUI('customMayaMixinWindowWorkspaceControl')
        except:
            pass
        customMixinWindow.show(dockable=True, restore=True, height=400, width=400, uiScript='import dockWin; dockWin.DockableWidgetUIScript(restore=True)')

def main():

    ui = DockableWidgetUIScript()
    return ui


if __name__ == 'dockWin':
    main()

#2

ok, problem solved.
after a lot of troubleshooting, it turned out to be a QSpacerItem in my ui that caused the window to open once and then crash maya the second time. which made me think it had something to do with the deleteUI stuff.

I had the QSpacerItem added like so:

self.spacerFive = QtWidgets.QSpacerItem(5 , 5)
self.myLayout.addSpacerItem(self.spacerFive)

this made maya crash out completely when deleteing the UI/workspaceControl…
no idea why, garbage collection?

this fixed it:

self.myLayout.addSpacerItem(QtWidgets.QSpacerItem(5, 5))

alright, everything working as expected again. cheers /S