-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharttest.py
290 lines (235 loc) · 9.27 KB
/
charttest.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
"""
C:/Users/trypt/PycharmProjects/charttest/charttest.py
"""
import weakref
import logging
from PySide2 import QtGui, QtWidgets, QtCore
from PySide2.QtCharts import QtCharts
from pxr import Usd, UsdSkel, Sdf, Gf
logging.basicConfig()
logger = logging.getLogger('usdchart')
logger.setLevel(logging.DEBUG)
def prim_iter(prim, inclusive=False):
if inclusive:
yield prim
for ch in prim.GetChildren():
yield ch
for ich in prim_iter(ch):
yield ich
def find_skeleton(prim):
for k in prim_iter(prim):
if k.GetTypeName() == 'Skeleton':
return k
def skel_to_treeitems(joints):
topo = UsdSkel.Topology(joints)
parents = topo.GetParentIndices()
joint_idx_dct = {}
result = {}
root = None
for i in range(len(joints)):
parent_idx = parents[i]
this_path = Sdf.Path(joints[i])
item = QtWidgets.QTreeWidgetItem([this_path.name])
if not root:
root = item
joint_idx_dct[i] = item
result[joints[i]] = item
parent_item = joint_idx_dct.get(parent_idx)
if parent_item:
parent_item.addChild(item)
return root,result
SERIES_ROLE = QtCore.Qt.UserRole + 1024
SERIES_IDX_ROLE = QtCore.Qt.UserRole + 1025
class JointSamples:
def __init__(self, joint_path, parent):
self.parent = weakref.proxy(parent)
self.name = Sdf.Path(joint_path).name
self._series = []
for att in ('.tx', '.ty', '.tz', '.rx', '.ry', '.rz', '.sx', '.sy', '.sz'):
series = QtCharts.QLineSeries()
series.setName(self.name+att)
series.setPointsVisible(True)
# series.setPointSize(4)
if 'x' in att:
series.setColor(QtGui.QColor('red'))
elif 'y' in att:
series.setColor(QtGui.QColor('green'))
else:
series.setColor(QtGui.QColor('blue'))
self._series.append(series)
def append(self, sample, trans, rot, scale):
children = self._series
children[0].append(sample, trans[0])
children[1].append(sample, trans[1])
children[2].append(sample, trans[2])
rot = Gf.Rotation(rot)
eul = rot.Decompose(Gf.Vec3d.XAxis(), Gf.Vec3d.YAxis(), Gf.Vec3d.ZAxis())
children[3].append(sample, eul[0])
children[4].append(sample, eul[1])
children[5].append(sample, eul[2])
children[6].append(sample, scale[0])
children[7].append(sample, scale[1])
children[8].append(sample, scale[2])
def add_translate(self, sample, trans):
self._series[0].append(sample, trans[0])
self._series[1].append(sample, trans[1])
self._series[2].append(sample, trans[2])
def add_rotate(self, sample, rot):
rot = Gf.Rotation(rot)
eul = rot.Decompose(Gf.Vec3d.XAxis(), Gf.Vec3d.YAxis(), Gf.Vec3d.ZAxis())
self._series[3].append(sample, eul[0])
self._series[4].append(sample, eul[1])
self._series[5].append(sample, eul[2])
def add_scale(self, sample, scale):
self._series[6].append(sample, scale[0])
self._series[7].append(sample, scale[1])
self._series[8].append(sample, scale[2])
def add_to_chart(self, chart):
if self.parent.view_translate():
for ch in self._series[0:3]:
chart.addSeries(ch)
if self.parent.view_rotate():
for ch in self._series[3:6]:
chart.addSeries(ch)
if self.parent.view_scale():
for ch in self._series[6:9]:
chart.addSeries(ch)
def rm_from_chart(self, chart):
for ch in self._series:
chart.removeSeries(ch)
def set_points_visible(self, val):
for ch in self._series:
ch.setPointsVisible(val)
class GraphWidget(QtWidgets.QWidget):
_instance = None
def __init__(self, parent=None):
super().__init__(parent)
self._prim_map = {}
self._last_sel = list()
self._series_map = {}
ly = QtWidgets.QVBoxLayout()
hly = QtWidgets.QHBoxLayout()
self._tr_only = QtWidgets.QCheckBox('Translate')
self._tr_only.setChecked(True)
self._tr_only.stateChanged.connect(self.on_view_cb_stateChanged)
self._rotate_only = QtWidgets.QCheckBox('Rotate')
self._rotate_only.setChecked(True)
self._rotate_only.stateChanged.connect(self.on_view_cb_stateChanged)
self._sc_only = QtWidgets.QCheckBox('Scale')
self._sc_only.setChecked(True)
self._sc_only.stateChanged.connect(self.on_view_cb_stateChanged)
hly.addWidget(self._tr_only)
hly.addWidget(self._rotate_only)
hly.addWidget(self._sc_only)
ly.addLayout(hly)
splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
self.pathList = QtWidgets.QTreeWidget()
self.pathList.setAlternatingRowColors(True)
self.pathList.setSelectionMode(self.pathList.ExtendedSelection)
self.chartView = QtCharts.QChartView()
self.chart = QtCharts.QChart()
self.chartView.setChart(self.chart)
splitter.addWidget(self.pathList)
splitter.addWidget(self.chartView)
ly.addWidget(splitter)
self.setLayout(ly)
self.pathList.itemSelectionChanged.connect(self.on_selection_changed)
self.setWindowTitle('Samples Viewer')
def view_translate(self):
return self._tr_only.isChecked()
def view_rotate(self):
return self._rotate_only.isChecked()
def view_scale(self):
return self._sc_only.isChecked()
def on_view_cb_stateChanged(self, _):
self.on_selection_changed()
def on_selection_changed(self):
for sel in self._last_sel:
smp = self._series_map.get(sel.data(0, SERIES_IDX_ROLE))
if smp:
smp.rm_from_chart(self.chart)
self._last_sel = self.pathList.selectedItems()
for sel in self._last_sel:
smp = self._series_map.get(sel.data(0, SERIES_IDX_ROLE))
if smp:
smp.add_to_chart(self.chart)
self.chart.createDefaultAxes()
def add_skel_and_anim(self, skel_prim, src):
# logger.info(' Skeleton: %s', skel_prim)
skel = UsdSkel.Skeleton(skel_prim)
skel_joints = skel.GetJointsAttr().Get()
root, joint_item_dct = skel_to_treeitems(skel_joints)
prim_item = QtWidgets.QTreeWidgetItem([skel_prim.GetName()])
prim_item.addChild(root)
self.pathList.addTopLevelItem(prim_item)
anim = UsdSkel.Animation(src)
anim_joints = anim.GetJointsAttr().Get()
tr = anim.GetTranslationsAttr()
rt = anim.GetRotationsAttr()
sc = anim.GetScalesAttr()
logger.info(' %s', tr.GetNumTimeSamples())
logger.info(' %s', rt.GetNumTimeSamples())
logger.info(' %s', sc.GetNumTimeSamples())
# assert tr.GetNumTimeSamples() == rt.GetNumTimeSamples() == sc.GetNumTimeSamples()
for attr,fnc in [(tr, JointSamples.add_translate,), (rt, JointSamples.add_rotate,), (sc, JointSamples.add_scale,)]:
samples = attr.GetTimeSamples()
for s in samples:
local = attr.Get(s)
first_sample = True
for i,joint in enumerate(anim_joints):
smp = self._series_map.get(i) or self._series_map.setdefault(i, JointSamples(joint, self))
fnc(smp, s, local[i])
if first_sample:
titem = joint_item_dct.get(joint)
if titem:
titem.setData(0, SERIES_IDX_ROLE, i)
first_sample = False
def add_skel_animation(self, stage):
"""Find and apply skel anim"""
for prim in stage.Traverse():
bnd = UsdSkel.BindingAPI(prim)
src = bnd.GetAnimationSource()
if src:
logger.info(' Found: %s %s', prim.GetPath(), src)
skel_prim = find_skeleton(prim)
if skel_prim:
self.add_skel_and_anim(skel_prim, src)
def set_stage(self, stage):
"""Pull all sampled and skel data
:param stage:
:return:
"""
self.chart = QtCharts.QChart()
self.pathList.clear()
self.add_skel_animation(stage)
# do we want static skels? not sure yet
# self.add_static_skel(stage)
# self.add_animated_prims(stage)
self.pathList.expandAll()
self.pathList.setIndentation(16)
self.chart.createDefaultAxes()
self.chartView.setChart(self.chart)
# def get_series_from_prim(self, prim):
# return list()
#
# def set_prim(self, prim):
# path = prim.GetPath()
# series = self._prim_map.get(path)
# if series is None:
# series = self.get_series_from_prim(prim)
# self._prim_map[path] = series
#
# for s in series:
# self.chart.addSeries(s)
def unset_prim(self, prim):
for series in self._prim_map.get(prim.GetPath(), list()):
self.chart.removeSeries(series)
@classmethod
def initUi(cls):
cls._instance = cls()
cls._instance.show()
cls._instance.raise_()
return cls._instance
if __name__ == 'builtins':
t = GraphWidget.initUi()
t.set_stage(usdviewApi.stage)