栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用PySide清理Maya中的可停靠窗口

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用PySide清理Maya中的可停靠窗口

在研究了mayaMixin.py之后,我设法获得了我所追求的行为的有效解决方案!这个想法是,您需要在Maya的主窗口中进行挖掘,然后删除其中的所有实例。

关闭窗口或创建新实例后,下面的示例将彻底删除所有实例。停靠或浮动都没关系,看起来工作正常。如果您不想在停靠时将其关闭,也可以随时对其进行调整。由于有很多“陷阱”,我在代码中留下了很多注释。

from shiboken import wrapInstancefrom PySide import QtGui, QtCorefrom maya import OpenMayaUI as OpenMayaUIfrom maya.app.general.mayaMixin import MayaQWidgetDockableMixinfrom maya.OpenMayaUI import MQtUtilclass 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()

这样,就不再污染Maya的环境了。希望能帮助到你!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517068.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号