-
Notifications
You must be signed in to change notification settings - Fork 4
/
markerstreamer.py
156 lines (116 loc) · 5.25 KB
/
markerstreamer.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
import hashlib, os
from PySide2 import QtGui
from PySide2 import QtWidgets
import maya.cmds as cmds
import maya.api.OpenMaya as om
from mayaui import load_icon
from mayautil import MayaUtil
class MarkerStreamer:
def __init__(self, qtmrt, listWidget, textWidget):
self._qtm = qtmrt
self._listWidget = listWidget
self._textWidget = textWidget
self._markers = None
self._marker_groups = None
self._unit_conversion = 0.1
self._qtm.connectedChanged.connect(self._connected_changed)
self._connected_changed(self._qtm.connected)
def _connected_changed(self, connected):
self._up_axis = cmds.upAxis(q=True, axis=True)
if connected:
self._init()
self._update_ui()
else:
self._markers = None
self._marker_groups = None
self._listWidget.clear()
def _packet_received(self, packet):
_, markers = packet.get_3d_markers()
if self._up_axis == "y":
cmds.setAttr("mocapMarkers.rotateX", 90)
cmds.setAttr("mocapMarkers.rotateZ", 180)
for i, marker in enumerate(markers):
transformFn = self._markers[i]["transformFn"]
translation = om.MVector(
marker.x * self._unit_conversion,
marker.y * self._unit_conversion,
marker.z * self._unit_conversion)
transformFn.setTranslation(translation, om.MSpace.kTransform)
def _init(self):
self._qtm_settings = self._qtm.get_settings("3d")
if self._qtm_settings == None:
return
if self._marker_groups is None:
self._markers = []
self._marker_groups = {"mocapMarkers": []}
for i, label in enumerate(self._qtm_settings["The_3D"]["Label"]):
label["Index"] = i
self._markers.append(label)
self._marker_groups["mocapMarkers"].append(label)
def _update_ui(self):
self._listWidget.clear()
if self._marker_groups == None:
return
for group_name, marker_group in self._marker_groups.items():
group_icon = load_icon(
os.path.dirname(os.path.abspath(__file__)) + "/assets/transform.svg",
QtGui.QColor(255, 0, 0),
)
group_item = QtWidgets.QListWidgetItem(group_icon, group_name)
self._listWidget.addItem(group_item)
for label in marker_group:
red = int(label["RGBColor"]) >> 16
green = (int(label["RGBColor"]) >> 8) & 0x00FF
blue = int(label["RGBColor"]) & 0x0000FF
marker_color = QtGui.QColor(red, green, blue)
icon = load_icon(
os.path.dirname(os.path.abspath(__file__))
+ "/assets/marker_64x32.png",
marker_color,
)
item = QtWidgets.QListWidgetItem(icon, label["Name"])
self._listWidget.addItem(item)
def create(self):
modifier = om.MDagModifier()
if self._marker_groups is not None:
for group_name, marker_group in self._marker_groups.items():
parent = MayaUtil.get_node_by_name(group_name)
if parent is None:
parent = modifier.createNode("transform")
modifier.renameNode(parent, group_name)
modifier.doIt()
for i, marker in enumerate(marker_group):
locator = MayaUtil.get_node_by_name(marker["Name"])
if locator is None:
locator = modifier.createNode("locator")
modifier.renameNode(locator, marker["Name"])
modifier.reparentNode(locator, parent)
modifier.doIt()
transformFn = om.MFnTransform(locator)
self._markers[marker["Index"]]["locator"] = locator
self._markers[marker["Index"]]["transformFn"] = transformFn
def group_markers(self):
new_group = []
new_group_name = self._textWidget.text()
selected = self._listWidget.selectedItems()
for item in selected:
for group_name, marker_group in self._marker_groups.items():
# Remove marker from existing groups.
for i, marker in enumerate(marker_group):
if item.text() == marker["Name"]:
new_group.append(marker_group[i])
self._markers[marker["Index"]] = marker
del marker_group[i]
for group_name, marker_group in self._marker_groups.items():
if len(marker_group) == 0:
del self._marker_groups[group_name]
self._marker_groups[new_group_name + " temp"] = new_group
if new_group_name in self._marker_groups:
random_name = hashlib.md5(str(time.time())).hexdigest()
self._marker_groups[random_name] = self._marker_groups[new_group_name]
self._marker_groups[new_group_name] = self._marker_groups[
new_group_name + " temp"
]
del self._marker_groups[new_group_name + " temp"]
self._update_ui()
self.create()