Python Maya中带有PySide清理的可停靠窗口

Python Maya中带有PySide清理的可停靠窗口,python,pyside,maya,Python,Pyside,Maya,我创建了一个可以停靠在Maya主ui中的工具,但我想不出关闭后如何清理它。问题是,如果我创建了该工具的多个实例,然后将其拖动到位以固定它,那么当我右键单击Maya的窗口时,它们都会显示出来。工具关闭时,如何正确清理这些 我已经尝试了cmds.deleteUI,QObject.deleteLater(),最多只能清除工具的内容,但它仍将存在于Maya中。以下是我到目前为止的一个例子: from shiboken import wrapInstance from PySide import QtGu

我创建了一个可以停靠在Maya主ui中的工具,但我想不出关闭后如何清理它。问题是,如果我创建了该工具的多个实例,然后将其拖动到位以固定它,那么当我右键单击Maya的窗口时,它们都会显示出来。工具关闭时,如何正确清理这些

我已经尝试了
cmds.deleteUI
QObject.deleteLater()
,最多只能清除工具的内容,但它仍将存在于Maya中。以下是我到目前为止的一个例子:

from shiboken import wrapInstance
from PySide import QtGui, QtCore
from maya import OpenMayaUI as OpenMayaUI
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin

class Window(MayaQWidgetDockableMixin, QtGui.QWidget):
    def __init__(self, parent = None):
        super(self.__class__, self).__init__(parent = parent)
        mayaMainWindowPtr = OpenMayaUI.MQtUtil.mainWindow() 
        self.mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtGui.QWidget) 
        self.setWindowFlags(QtCore.Qt.Window)

        if cmds.window('myTool', q = True, ex = True):
            cmds.deleteUI('myTool')

        self.setObjectName('myTool')

        self.setWindowTitle('My tool')
        self.resize(200, 200)

        self.myButton = QtGui.QPushButton('TEMP')

        self.mainLayout = QtGui.QVBoxLayout()
        self.mainLayout.addWidget(self.myButton)
        self.setLayout(self.mainLayout)

    def dockCloseEventTriggered(self):
        self.deleteLater()

    def run(self):
        self.show(dockable = True)

myWin = Window()
myWin.run()

在对mayamyxin.py进行深入研究后,我终于找到了一个解决我所追求的行为的有效方法!其思想是,您需要在Maya的主窗口中挖掘并删除其中的任何实例

关闭窗口或创建新实例后,下面的示例将干净地删除所有实例。不管它是停靠的还是浮动的,它似乎工作正常。如果不希望在停靠时关闭该行为,则也可以随时调整该行为。我在代码中留下了很多注释,因为有很多“gotchas”


这样,它就不再污染玛雅的环境了。希望有帮助

对不起,我无法重现你的问题。如果我运行您的脚本,我可以以正确的方式拖动、停靠、取消停靠和关闭多个实例。您正在使用哪个版本的maya?另一件事:如果您的代码中有cmds.window('myTool',q=True,ex=True):cmds.deleteUI('myTool')您不应该运行多个window实例!也许我没有解释清楚。我也可以运行、停靠、卸载和关闭多个实例。假设您创建了多个实例,并将每个实例停靠到通道盒/属性编辑器,然后关闭它们。右键单击通道盒/属性编辑器窗口的标题,它将在那里显示一组“我的工具”复选框!是那些我不能清理的。^erm不。。不应该同时有多个实例。应该只有一个实例,这就是为什么它在那里尝试删除前一个。现在我明白了。我也尝试过使用
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setParent(None)
,但只删除dockwidget内容。如果我找到解决办法,我会告诉你
from shiboken import wrapInstance
from PySide import QtGui, QtCore
from maya import OpenMayaUI as OpenMayaUI
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
from maya.OpenMayaUI import MQtUtil

class MyWindow(MayaQWidgetDockableMixin, QtGui.QDialog):
    toolName = 'myToolWidget'

    def __init__(self, parent = None):
        # Delete any previous instances that is detected. Do this before parenting self to main window!
        self.deleteInstances()

        super(self.__class__, self).__init__(parent = parent)
        mayaMainWindowPtr = OpenMayaUI.MQtUtil.mainWindow() 
        self.mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtGui.QMainWindow)
        self.setObjectName(self.__class__.toolName) # Make this unique enough if using it to clear previous instance!

        # Setup window's properties
        self.setWindowFlags(QtCore.Qt.Window)
        self.setWindowTitle('My tool')
        self.resize(200, 200)

        # Create a button and stuff it in a layout
        self.myButton = QtGui.QPushButton('My awesome button!!')
        self.mainLayout = QtGui.QVBoxLayout()
        self.mainLayout.addWidget(self.myButton)
        self.setLayout(self.mainLayout)

    # If it's floating or docked, this will run and delete it self when it closes.
    # You can choose not to delete it here so that you can still re-open it through the right-click menu, but do disable any callbacks/timers that will eat memory
    def dockCloseEventTriggered(self):
        self.deleteInstances()

    # Delete any instances of this class
    def deleteInstances(self):
        mayaMainWindowPtr = OpenMayaUI.MQtUtil.mainWindow() 
        mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtGui.QMainWindow) # Important that it's QMainWindow, and not QWidget/QDialog

        # Go through main window's children to find any previous instances
        for obj in mayaMainWindow.children():
            if type( obj ) == maya.app.general.mayaMixin.MayaQDockWidget:
                #if obj.widget().__class__ == self.__class__: # Alternatively we can check with this, but it will fail if we re-evaluate the class
                if obj.widget().objectName() == self.__class__.toolName: # Compare object names
                    # If they share the same name then remove it
                    print 'Deleting instance {0}'.format(obj)
                    mayaMainWindow.removeDockWidget(obj) # This will remove from right-click menu, but won't actually delete it! ( still under mainWindow.children() )
                    # Delete it for good
                    obj.setParent(None)
                    obj.deleteLater()        

    # Show window with docking ability
    def run(self):
        self.show(dockable = True)

myWin = MyWindow()
myWin.run()