Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- https://groups.google.com/d/topic/python_inside_maya/DdTn2qiW2vc/discussion
- Creating a tree view out of a list of Maya node paths
- """
- from PyQt4 import QtCore, QtGui
- class Window(QtGui.QMainWindow):
- PATH_ROLE = QtCore.Qt.UserRole+1
- def __init__(self, parent=None):
- super(Window, self).__init__(parent)
- self.tree = QtGui.QTreeWidget()
- self.setCentralWidget(self.tree)
- def populate(self, paths):
- view = self.tree
- view.clear()
- for path in sorted(paths):
- parts = [path.rsplit('|', i)[0] for i in xrange(path.count('|'))]
- parts.reverse()
- parent = view.invisibleRootItem()
- for i, part in enumerate(parts):
- # We don't want to create duplicate branches
- # so we can check if the path already exists
- found = self.itemFromPath(part)
- if found:
- parent = found
- continue
- # Create a new node under the current parent
- name = part.rsplit('|', 1)[-1]
- item = QtGui.QTreeWidgetItem([name])
- item.setData(0, self.PATH_ROLE, part)
- parent.addChild(item)
- parent = item
- def itemFromPath(self, path, parent=None):
- """Find item by maya path"""
- model = self.tree.model()
- if parent is None:
- parent = model.index(0, 0)
- else:
- parent = self.tree.indexFromItem(parent)
- found = model.match(parent, self.PATH_ROLE, path, flags=QtCore.Qt.MatchRecursive)
- if not found:
- return None
- return self.tree.itemFromIndex(found[0])
- if __name__ == "__main__":
- app = QtGui.QApplication([])
- win = Window()
- paths = [
- "|root|one|two|three",
- "|root|one",
- "|root2|one|two2|three",
- "|root|one|two2|three",
- "|root2|one|two2|three3",
- ]
- win.populate(paths)
- win.resize(800, 600)
- win.show()
- win.raise_()
- app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment