-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtracker.py
178 lines (153 loc) · 5.95 KB
/
tracker.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
# Copyright vanous
#
# This file is part of BlenderDMX.
#
# BlenderDMX is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# BlenderDMX is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
import uuid
import bpy
from bpy.props import (
BoolProperty,
CollectionProperty,
EnumProperty,
IntProperty,
PointerProperty,
StringProperty,
)
from bpy.types import Collection, Object, PropertyGroup
from .i18n import DMX_Lang
from .network import DMX_Network
from .psn import DMX_PSN
_ = DMX_Lang._
class DMX_Tracker_Object(PropertyGroup):
object: PointerProperty(name="Tracker > Object", type=Object)
class DMX_Tracker(PropertyGroup):
def onPsnEnable(self, context):
if self.enabled:
DMX_PSN.enable(self)
else:
DMX_PSN.disable(self)
enabled: BoolProperty(
name=_("Enable PSN Input"),
description=_("Enables PosiStageNet input"),
default=False,
update=onPsnEnable,
)
ip_address: EnumProperty(
name=_("IPv4 Address for PSN signal"),
description=_("The network card/interface to listen for PSN data"),
items=DMX_Network.cards,
)
ip_port: IntProperty(name=_("PSN Target port"), description=_(""), default=56565)
uuid: StringProperty(
name="UUID",
description="Unique ID, used for identification",
default=str(uuid.uuid4()),
)
# Blender RNA #
collection: PointerProperty(name="Tracker > Collection", type=Collection)
objects: CollectionProperty(name="Tracker > Objects", type=DMX_Tracker_Object)
@staticmethod
def add_tracker():
dmx = bpy.context.scene.dmx
new_tracker = dmx.trackers.add()
new_tracker["position"] = [
[],
] * 10 # hardcoded to 10 slots
new_tracker.uuid = str(uuid.uuid4())
new_id = len(dmx.trackers)
new_tracker.name = generate_tracker_name(new_id)
target = bpy.data.objects.new(
name=f"{new_tracker.name} Tracker", object_data=None
)
target["uuid"] = new_tracker.uuid
bpy.ops.collection.create(name=new_tracker.name)
new_tracker.collection = bpy.data.collections[new_tracker.name]
for c in new_tracker.collection.objects:
new_tracker.collection.objects.unlink(c)
for c in new_tracker.collection.children:
new_tracker.collection.children.unlink(c)
tracker_object = new_tracker.objects.add()
new_tracker.collection.objects.link(target)
tracker_object.name = new_tracker.name
tracker_object.object = target
target.empty_display_size = 0.2
target.empty_display_type = "ARROWS"
target.location = (0, 0, 0)
bpy.context.scene.dmx.collection.children.link(new_tracker.collection)
@staticmethod
def remove_tracker(uuid):
dmx = bpy.context.scene.dmx
tracker_idx = DMX_Tracker.get_tracker_idx(uuid)
tracker = DMX_Tracker.get_tracker(uuid)
tracker.enabled = False
for fixture in dmx.fixtures:
for obj in fixture.objects:
if obj.name == "Target":
for constraint in obj.object.constraints:
if constraint.target is not None:
if constraint.target.get("uuid", None) == uuid:
obj.object.constraints.remove(constraint)
if tracker is not None:
if tracker.collection is not None:
if tracker.collection.objects is not None:
for obj in tracker.collection.objects:
bpy.data.objects.remove(obj)
if tracker.objects is not None:
for obj in tracker.objects:
if obj.object:
bpy.data.objects.remove(obj.object)
if tracker.collection is not None:
bpy.data.collections.remove(tracker.collection)
if tracker_idx is not None:
dmx.trackers.remove(tracker_idx)
@staticmethod
def get_tracker_idx(uuid):
dmx = bpy.context.scene.dmx
for idx, tracker in enumerate(dmx.trackers):
if tracker.uuid == uuid:
return idx
@staticmethod
def get_tracker(uuid):
dmx = bpy.context.scene.dmx
for tracker in dmx.trackers:
if tracker.uuid == uuid:
return tracker
def render(self, current_frame=None):
data = DMX_PSN.get_data(self.uuid)
for idx, slot_data in enumerate(data):
if idx > 10: # hardcoded number of PSN slots
return
if list(self["position"][idx]) == list(slot_data):
return
x, y, z = slot_data
self["position"][idx] = list(data)
for obj_idx, obj in enumerate(self.collection.objects):
if obj_idx == idx:
if x is not None:
obj.location.x = x
if y is not None:
obj.location.y = y
if z is not None:
obj.location.z = z
if current_frame:
obj.keyframe_insert(data_path="location", frame=current_frame)
def generate_tracker_name(new_id):
dmx = bpy.context.scene.dmx
while True:
name = f"PSN Server {new_id:>03}"
if name in dmx.trackers or name in dmx.collection.children:
new_id += 1
else:
break
return name