-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathhierarchyModel.py
485 lines (400 loc) · 17.5 KB
/
hierarchyModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#
# Copyright 2016 Pixar
#
# Licensed under the Apache License, Version 2.0 (the "Apache License")
# with the following modification; you may not use this file except in
# compliance with the Apache License and the following modification to it:
# Section 6. Trademarks. is deleted and replaced with:
#
# 6. Trademarks. This License does not grant permission to use the trade
# names, trademarks, service marks, or product names of the Licensor
# and its affiliates, except as required to comply with Section 4(c) of
# the License and to reproduce the content of the NOTICE file.
#
# You may obtain a copy of the Apache License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Apache License with the above modification is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
from __future__ import absolute_import
from ._Qt import QtCore, QtWidgets, QtGui
from pxr import Sdf, Tf, Usd
from ._bindings import PrimFilterCache, _HierarchyCache
from . import roles, qtUtils
if False:
from typing import *
class HierarchyBaseModel(QtCore.QAbstractItemModel):
"""Base class for adapting a stage's prim hierarchy for Qt ItemViews
Most clients will want to use a configuration of the `HierachyStandardModel`
which has a standard set of columns and data or subclass this to provide
their own custom set of columns.
Clients are encouraged to subclass this module because it provides both
robust handling of change notification and an efficient lazy population.
This model listens for TfNotices and emits the appropriate Qt signals.
"""
class LayoutChangedContext(object):
"""Context manager to ensure model layout changes are propagated if an
exception is thrown.
"""
def __init__(self, model):
self.model = model
def __enter__(self):
self.model.layoutAboutToBeChanged.emit()
def __exit__(self, exceptionType, exceptionValue, traceBack):
if exceptionType:
self.model._Invalidate()
self.model.layoutChanged.emit()
def __init__(self, stage=None, predicate=Usd.TraverseInstanceProxies(
Usd.PrimIsDefined | ~Usd.PrimIsDefined),
parent=None):
# type: (Optional[Usd.Stage], Usd.PrimFlags, Optional[QtCore.QObject]) -> None
"""Instantiate an QAbstractItemModel adapter for a UsdStage.
It's safe for the 'stage' to be None if the model needs to be instatiated
without knowing the stage its interacting with.
'predicate' specifies the prims that may be accessed via the model on
the stage. A good policy is to be as accepting of prims as possible
and rely on a QSortFilterProxyModel to interactively reduce the view.
Changing the predicate is a potentially expensive operation requiring
rebuilding internal caches, making not ideal for interactive filtering.
Parameters
----------
stage : Optional[Usd.Stage]
predicate : Usd.PrimFlags
parent : Optional[QtCore.QObject]
"""
super(HierarchyBaseModel, self).__init__(parent)
self._predicate = predicate
self._stage = None
self._index = None
self._listener = None
self.ResetStage(stage)
def _IsStageValid(self):
return self._stage and self._stage.GetPseudoRoot()
def ResetStage(self, stage):
# type: (Usd.Stage) -> None
"""Resets the model for use with a new stage.
If the stage isn't valid, this effectively becomes an empty model.
Parameters
----------
stage : Usd.Stage
"""
if stage == self._stage:
return
self.beginResetModel()
self._stage = stage
if not self._IsStageValid():
self._index = None
self._listener = None
else:
self._index = _HierarchyCache(
stage.GetPrimAtPath('/'), self._predicate)
self._listener = Tf.Notice.Register(
Usd.Notice.ObjectsChanged, self._OnObjectsChanged, self._stage)
self.endResetModel()
def GetPredicate(self):
"""Get the predicate used in stage hierarchy traversal"""
return self._index.GetPredicate()
def GetRoot(self):
"""Retrieve the root of the current hierarchy model"""
rootProxy = self._index.GetRoot()
return (rootProxy.GetPrim() if rootProxy and not rootProxy.expired
else None)
def _OnObjectsChanged(self, notice, sender):
resyncedPaths = notice.GetResyncedPaths()
resyncedPaths = [path for path in resyncedPaths if path.IsPrimPath()]
if len(resyncedPaths) > 0:
with self.LayoutChangedContext(self):
persistentIndices = self.persistentIndexList()
indexToPath = {}
for index in persistentIndices:
indexProxy = index.internalPointer()
indexPrim = indexProxy.GetPrim()
indexPath = indexPrim.GetPath()
for resyncedPath in resyncedPaths:
commonPath = resyncedPath.GetCommonPrefix(indexPath)
# if the paths are siblings or if the
# index path is a child of resynced path, you need to
# update any persistent indices
areSiblings = (commonPath == resyncedPath.GetParentPath()
and commonPath != indexPath)
indexIsChild = (commonPath == resyncedPath)
if areSiblings or indexIsChild:
indexToPath[index] = indexPath
self._index.ResyncSubtrees(resyncedPaths)
fromIndices = []
toIndices = []
for index in indexToPath:
path = indexToPath[index]
if self._index.ContainsPath(path):
newProxy = self._index.GetProxy(path)
newRow = self._index.GetRow(newProxy)
if index.row() != newRow:
for i in xrange(self.columnCount(QtCore.QModelIndex())):
fromIndices.append(index)
toIndices.append(self.createIndex(
newRow, index.column(), newProxy))
else:
fromIndices.append(index)
toIndices.append(QtCore.QModelIndex())
self.changePersistentIndexList(fromIndices, toIndices)
def GetIndexForPath(self, path):
# type: (Sdf.Path) -> Optional[QtCore.QModelIndex]
"""Given a path, retrieve the appropriate model index.
Parameters
----------
path : Sdf.Path
Returns
-------
Optional[QtCore.QModelIndex]
"""
if self._index.ContainsPath(path):
proxy = self._index.GetProxy(path)
row = self._index.GetRow(proxy)
return self.createIndex(row, 0, proxy)
def _GetPrimForIndex(self, modelIndex):
# type: (QtCore.QModelIndex) -> Optional[Usd.Prim]
"""Retrieve the prim for the input model index
External clients should use `UsdQt.roles.HierarchyPrimRole` to access
the prim for an index.
Parameters
----------
modelIndex : QtCore.QModelIndex
Returns
-------
Optional[Usd.Prim]
"""
if modelIndex.isValid():
proxy = modelIndex.internalPointer()
if type(proxy) is _HierarchyCache.Proxy and not proxy.expired:
return proxy.GetPrim()
def parent(self, modelIndex):
if not self._IsStageValid():
return QtCore.QModelIndex()
if not modelIndex.isValid():
return QtCore.QModelIndex()
proxy = modelIndex.internalPointer()
if self._index.IsRoot(proxy):
return QtCore.QModelIndex()
parentProxy = self._index.GetParent(proxy)
parentRow = self._index.GetRow(parentProxy)
return self.createIndex(parentRow, 0, parentProxy)
def data(self, modelIndex, role=QtCore.Qt.DisplayRole):
if not modelIndex.isValid():
return
if not self._IsStageValid():
return
if role == QtCore.Qt.DisplayRole:
prim = self._GetPrimForIndex(modelIndex)
return prim.GetName()
if role == roles.HierarchyPrimRole:
return self._GetPrimForIndex(modelIndex)
def index(self, row, column, parent=QtCore.QModelIndex()):
if not self._IsStageValid():
return QtCore.QModelIndex()
if not parent.isValid():
# We assume the root has already been registered.
root = self._index.GetRoot()
return self.createIndex(row, column, root)
parentProxy = parent.internalPointer()
child = self._index.GetChild(parentProxy, row)
return self.createIndex(row, column, child)
def columnCount(self, parent):
return 1
def rowCount(self, parent):
if not self._IsStageValid():
return 0
if not parent.isValid():
return 1
parentProxy = parent.internalPointer()
return self._index.GetChildCount(parentProxy)
def Debug(self):
self._index.PrintFullIndex()
class HierarchyStandardModel(HierarchyBaseModel):
"""Configurable model for common columns for displaying hierarchies"""
Name = "Name"
Type = "Type"
Kind = "Kind"
NormalColor = QtGui.QBrush(QtGui.QColor(227, 227, 227))
InactiveDarker = 200
ArcsIconPath = 'icons/arcs_2.xpm'
NoarcsIconPath = 'icons/noarcs_2.xpm'
def __init__(self, stage=None, columns=None, parent=None):
# type: (Optional[Usd.Stage], Optional[Union[List[str], Tuple[str]]], Optional[QtCore.QObject]) -> None
"""
Parameters
----------
stage : Optional[Usd.Stage]
columns : Optional[List[str]]
parent : Optional[QtCore.QObject]
"""
super(HierarchyStandardModel, self).__init__(
stage, Usd.TraverseInstanceProxies(
Usd.PrimIsDefined | ~Usd.PrimIsDefined), parent)
if not columns:
# By default show all possible columns.
self.columns = [HierarchyStandardModel.Name,
HierarchyStandardModel.Type,
HierarchyStandardModel.Kind]
else:
self.columns = columns
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
return self.columns[section]
return super(HierarchyStandardModel, self).headerData(
section, orientation, role)
def columnCount(self, parent):
return len(self.columns)
def data(self, modelIndex, role=QtCore.Qt.DisplayRole):
if not (modelIndex.isValid()):
return None
column = self.columns[modelIndex.column()]
if role == QtCore.Qt.ForegroundRole:
prim = self._GetPrimForIndex(modelIndex)
brush = HierarchyStandardModel.NormalColor
if not prim.IsActive():
brush = QtGui.QBrush(brush)
brush.setColor(brush.color().darker(
HierarchyStandardModel.InactiveDarker))
return brush
elif role == QtCore.Qt.DecorationRole:
if modelIndex.column() == 0:
prim = self._GetPrimForIndex(modelIndex)
if prim.HasAuthoredInherits() or \
prim.HasAuthoredReferences() or \
prim.HasVariantSets() or \
prim.HasPayload() or \
prim.HasAuthoredSpecializes():
return qtUtils.IconCache.Get(self.ArcsIconPath)
else:
return qtUtils.IconCache.Get(self.NoarcsIconPath)
elif role == QtCore.Qt.DisplayRole:
if column == HierarchyStandardModel.Name:
return super(HierarchyStandardModel, self).data(modelIndex, role)
elif column == HierarchyStandardModel.Type:
prim = self._GetPrimForIndex(modelIndex)
typeName = prim.GetTypeName()
return typeName if typeName else ""
elif column == HierarchyStandardModel.Kind:
prim = self._GetPrimForIndex(modelIndex)
kind = prim.GetMetadata('kind')
return kind if kind else ""
else:
raise Exception("shouldn't happen")
elif role == QtCore.Qt.ToolTipRole:
prim = self._GetPrimForIndex(modelIndex)
specifier = prim.GetSpecifier()
primType = prim.GetTypeName()
documentation = prim.GetDocumentation()
if specifier == Sdf.SpecifierDef:
toolTipString = "Defined %s"
elif specifier == Sdf.SpecifierOver:
toolTipString = "Undefined %s"
elif specifier == Sdf.SpecifierClass:
toolTipString = "Abstract %s"
else:
raise Exception("Unhandled specifier for tooltip.")
if not primType:
toolTipString = toolTipString % "Prim"
else:
toolTipString = toolTipString % primType
if documentation:
toolTipString = "%s\n%s" % (toolTipString, documentation)
if prim.IsInstanceProxy():
toolTipString = "Instance Proxy of %s" % toolTipString
if prim.IsInstanceable():
toolTipString = "Instance Root of %s" % toolTipString
return toolTipString
return super(HierarchyStandardModel, self).data(modelIndex, role)
class HierarchyStandardFilterModel(QtCore.QSortFilterProxyModel):
"""Set of standard filtering strategies for items in a hierarchy model"""
def __init__(self, showInactive=False, showUndefined=False,
showAbstract=False,
filterCachePredicate=(Usd.PrimIsDefined | ~Usd.PrimIsDefined),
parent=None):
# type: (bool, bool, bool, Usd.PrimFlags, Optional[QtCore.QObject]) -> None
"""
Parameters
----------
showInactive : bool
showUndefined : bool
showAbstract : bool
filterCachePredicate : Usd.PrimFlags
parent : Optional[QtCore.QObject]
"""
super(HierarchyStandardFilterModel, self).__init__(parent)
self._filterCachePredicate = filterCachePredicate
self._filterCache = PrimFilterCache()
self._filterCacheActive = False
self._filterAcrossArcs = True
self._showInactive = showInactive
self._showUndefined = showUndefined
self._showAbstract = showAbstract
def ClearFilter(self):
self._filterCacheActive = False
self.invalidateFilter()
def SetPathContainsFilter(self, substring):
self._filterCache.ApplyPathContainsFilter(self.sourceModel().GetRoot(),
substring,
self._filterCachePredicate)
self._filterCacheActive = True
self.invalidateFilter()
def TogglePrimInactive(self, value):
self._showInactive = bool(value)
self.invalidateFilter()
def TogglePrimUndefined(self, value):
self._showUndefined = bool(value)
self.invalidateFilter()
def TogglePrimAbstract(self, value):
self._showAbstract = bool(value)
self.invalidateFilter()
def ToggleFilterAcrossArcs(self, value):
self._filterAcrossArcs = bool(value)
self.invalidateFilter()
def _FilterAll(self, prim):
if prim.GetPath() == Sdf.Path('/'):
return True
if not self._showInactive and not prim.IsActive():
return False
if not self._showUndefined and not prim.GetSpecifier() \
in (Sdf.SpecifierDef, Sdf.SpecifierClass):
return False
if not self._showAbstract and not prim.GetSpecifier() \
in (Sdf.SpecifierDef, Sdf.SpecifierOver):
return False
return True
def filterAcceptsRow(self, sourceRow, sourceParent):
index = self.sourceModel().index(sourceRow, 0, sourceParent)
prim = index.data(role=roles.HierarchyPrimRole)
if not prim:
raise Exception("Retrieved invalid prim during filtering.")
if not self._FilterAll(prim):
return False
if self._filterAcrossArcs:
if not (prim.GetPrimIndex().rootNode.hasSpecs
or (prim.IsActive() and prim.IsDefined())):
return False
if self._filterCacheActive:
state = self._filterCache.GetState(prim.GetPath())
return state not in (PrimFilterCache.Reject,
PrimFilterCache.Untraversed)
return True
if __name__ == '__main__':
import sys
import os
from ._Qt import QtWidgets
app = QtWidgets.QApplication(sys.argv)
dir = os.path.split(__file__)[0]
path = os.path.join(
dir, 'testenv', 'testUsdQtHierarchyModel', 'simpleHierarchy.usda')
stage = Usd.Stage.Open(path)
model = HierarchyStandardModel(stage)
search = QtWidgets.QLineEdit()
tv = QtWidgets.QTreeView()
tv.setModel(model)
tv.show()
sys.exit(app.exec_())