Skip to content

Instantly share code, notes, and snippets.

@whoiscarlo
Last active July 29, 2019 07:11
Show Gist options
  • Save whoiscarlo/e0bb74461f1416cb054c4f8ef6735213 to your computer and use it in GitHub Desktop.
Save whoiscarlo/e0bb74461f1416cb054c4f8ef6735213 to your computer and use it in GitHub Desktop.
Maya Widget Hierarchy
"""
Widget Hierarchy
"""
from PyQt5 import QtWidgets, QtGui, QtCore
from sip import wrapinstance
from maya import OpenMayaUI as omui
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
from pprint import pprint
class WidgetHierarchyTree(QtWidgets.QTreeView):
def __init__(self, rootWidget=None, *args, **kwargs):
super(WidgetHierarchyTree, self).__init__(*args, **kwargs)
# Destroy this widget when closed. Otherwise it will stay around
#self.setAttribute(Qt.WA_DeleteOnClose, True)
# Determine root widget to scan
if rootWidget != None:
self.rootWidget = rootWidget
else:
mayaMainWindowPtr = omui.MQtUtil.mainWindow()
self.rootWidget = wrapinstance(long(mayaMainWindowPtr), QtWidgets.QWidget)
self.populateModel()
def populateModel(self):
# Create the headers
self.columnHeaders = ['Class', 'ObjectName', 'Children']
myModel = QtGui.QStandardItemModel(0, len(self.columnHeaders))
for col, colName in enumerate(self.columnHeaders):
myModel.setHeaderData(col, QtCore.Qt.Horizontal, colName)
# Recurse through child widgets
parentItem = myModel.invisibleRootItem()
self.populateModel_recurseChildren(parentItem, self.rootWidget)
self.setModel(myModel)
def populateModel_recurseChildren(self, parentItem, widget):
try:
# Construct the item data and append the row
classNameStr = str(widget.__class__).split("'")[1]
classNameStr = classNameStr.replace('PySide.','').replace('QtGui.', '').replace('QtCore.', '')
items = [QtGui.QStandardItem(classNameStr),
QtGui.QStandardItem(widget.objectName()),
QtGui.QStandardItem(str(len(widget.children()))),
]
# print parentItem, type(parentItem)
parentItem.appendRow(items)
# Recurse children and perform the same action
# print widget, widget.children()
for num, childWidget in enumerate(widget.children()):
self.populateModel_recurseChildren(items[0], childWidget)
# if num == 10:
# return
# return
except:
print widget, type(widget)
print parentItem, type(parentItem)
ui = WidgetHierarchyTree()
ui.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment