-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathin_gdtf.py
300 lines (253 loc) · 9.52 KB
/
in_gdtf.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
# Copyright (C) 2024 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 os
import shutil
import traceback
from threading import Timer
import bpy
from bpy_extras.io_utils import ImportHelper
if bpy.app.version >= (4, 2):
from bpy_extras.io_utils import poll_file_object_drop
from bpy.props import (
BoolProperty,
CollectionProperty,
FloatVectorProperty,
IntProperty,
StringProperty,
)
from bpy.types import PropertyGroup
from .i18n import DMX_Lang
from .panels import profiles as Profiles
from .util import create_unique_fixture_name
from .gdtf_file import DMX_GDTF_File
from .logging_setup import DMX_Log
_ = DMX_Lang._
def createDMXcollection():
dmx = bpy.context.scene.dmx
if not dmx.collection:
bpy.context.scene.dmx.new()
class DMX_Break_Import(PropertyGroup):
def ensure_universe_exists(self, context):
dmx = bpy.context.scene.dmx
dmx.ensureUniverseExists(self.universe)
dmx_break: IntProperty(
name="DMX Break",
description="DMX entry point",
default=0,
min=0,
max=511,
)
universe: IntProperty(
name="Fixture > Universe",
description="Fixture DMX Universe",
default=0,
min=0,
max=511,
update=ensure_universe_exists,
)
address: IntProperty(
name="Fixture > Address", description="Fixture DMX Address", default=1, min=1
) # no max for now
channels_count: IntProperty(
name="Number of channels",
description="Number of DMX channels",
default=0,
min=0,
) # no max for now
class DMX_OT_Import_GDTF(bpy.types.Operator, ImportHelper):
"""Import GDTF"""
bl_idname = "dmx.import_gdtf_into_scene"
bl_label = "Import GDTF (.gdtf)"
bl_options = {"PRESET", "UNDO"}
filename_ext = ".gdtf"
filter_glob: StringProperty(default="*.gdtf", options={"HIDDEN"})
files: CollectionProperty(
type=bpy.types.OperatorFileListElement, options={"HIDDEN", "SKIP_SAVE"}
)
directory: StringProperty(subtype="DIR_PATH")
patch: BoolProperty(
name="Patch into the scene",
description="Patch fixture into the scene",
default=False,
)
universe: IntProperty(
name=_("Universe"), description=_("DMX Universe"), default=0, min=0, max=511
)
address: IntProperty(
name=_("Address"), description=_("DMX Address"), default=1, min=1, max=512
)
dmx_breaks: CollectionProperty(name="DMX Break", type=DMX_Break_Import)
gel_color: FloatVectorProperty(
name=_("Gel Color"),
subtype="COLOR",
size=4,
min=0.0,
max=1.0,
default=(1.0, 1.0, 1.0, 1.0),
)
display_beams: BoolProperty(
name=_("Display beams"),
description=_("Display beam projection and cone"),
# update = onDisplayBeams,
default=True,
)
add_target: BoolProperty(
name=_("Add Target"),
description=_("Add target for beam to follow"),
# update = onAddTarget,
default=True,
)
increment_address: BoolProperty(
name=_("Increment DMX address"),
description=_("Increment DMX address"),
default=True,
)
increment_fixture_id: BoolProperty(
name=_("Increment Fixture ID"),
description=_("Increment Fixture ID if numeric"),
default=True,
)
fixture_id: StringProperty(
name=_("Fixture ID"),
description=_(
"The Fixture ID is an identifier of this fixture that can be used to activate / select them for programming."
),
default="",
)
units: IntProperty(
name=_("Units"),
description=_("How many units of this light to add"),
default=1,
min=1,
max=1024,
)
def draw(self, context):
dmx = context.scene.dmx
if not dmx.collection:
Timer(0.5, createDMXcollection, ()).start()
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False
layout.prop(self, "patch")
box = layout.column().box()
box.enabled = self.patch
box.prop(self, "units")
box.prop(self, "universe")
box.prop(self, "address")
box.prop(self, "fixture_id")
box.prop(self, "gel_color")
box.prop(self, "display_beams")
box.prop(self, "add_target")
box.prop(self, "increment_address")
box.prop(self, "increment_fixture_id")
def execute(self, context):
dmx = bpy.context.scene.dmx
folder_path = dmx.get_addon_path()
folder_path = os.path.join(folder_path, "assets", "profiles")
dmx = context.scene.dmx
address = self.address
universe = self.universe
fixture_id = self.fixture_id
for file in self.files:
if not file.name:
continue
file_path = os.path.join(self.directory, file.name)
DMX_Log.log.info(f"Importing GDTF Profile: {file_path}")
try:
shutil.copy(file_path, folder_path)
DMX_GDTF_File.add_to_data(file.name)
except shutil.SameFileError:
DMX_Log.log.debug(
"Importing file which already existed in the profiles folder"
)
if self.patch:
try:
profile = DMX_GDTF_File.load_gdtf_profile(file.name)
dmx_mode = profile.dmx_modes[0]
self.dmx_breaks.clear()
for idx, dmx_break in enumerate(dmx_mode.dmx_breaks):
new_break = self.dmx_breaks.add()
new_break.dmx_break = dmx_break.dmx_break
new_break.address = address
new_break.universe = universe
new_break.channels_count = dmx_break.channels_count
if not self.dmx_breaks:
new_break = self.dmx_breaks.add()
new_break.dmx_break = 0
new_break.universe = 0
new_break.address = 0
new_break.channels_count = 0
for count in range(1, 1 + self.units):
new_name = f"{profile.name} {count}"
new_name = create_unique_fixture_name(new_name)
dmx.addFixture(
new_name,
file.name,
self.dmx_breaks,
dmx_mode.name,
self.gel_color,
self.display_beams,
self.add_target,
fixture_id=fixture_id,
)
fixture = dmx.fixtures[-1]
DMX_Log.log.debug(f"Added fixture {fixture}")
if not fixture:
continue
if self.increment_fixture_id:
if fixture_id.isnumeric():
fixture_id = str(int(fixture_id) + 1)
if self.increment_address:
# This will only increment correctly the address of the first break
# Other breaks will have to be adjusted in the Fixtures list after import
if (address + self.dmx_breaks[0].channels_count) > 512:
universe += 1
address = 1
dmx.ensureUniverseExists(universe)
else:
address += self.dmx_breaks[0].channels_count
except Exception as e:
traceback.print_exception(e)
Profiles.DMX_Fixtures_Local_Profile.loadLocal()
DMX_GDTF_File.get_manufacturers_list()
return {"FINISHED"}
if bpy.app.version >= (4, 1):
class DMX_IO_FH_GDTF(bpy.types.FileHandler):
bl_idname = "IO_FH_gdtf"
bl_label = "GDTF"
bl_import_operator = "dmx.import_gdtf_into_scene"
bl_file_extensions = ".gdtf"
@classmethod
def poll_drop(cls, context):
if bpy.app.version >= (4, 2):
return poll_file_object_drop(context)
def menu_func_import(self, context):
self.layout.operator(
DMX_OT_Import_GDTF.bl_idname,
text="General Device Type Format (.gdtf) into BlenderDMX",
)
def register():
bpy.utils.register_class(DMX_Break_Import)
bpy.utils.register_class(DMX_OT_Import_GDTF)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
if bpy.app.version >= (4, 1):
bpy.utils.register_class(DMX_IO_FH_GDTF)
def unregister():
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
if bpy.app.version >= (4, 1):
bpy.utils.unregister_class(DMX_IO_FH_GDTF)
bpy.utils.unregister_class(DMX_OT_Import_GDTF)