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 | class BackgroundLoader(load.LoaderPlugin):
"""Load images
Stores the imported product in a container named after the product.
"""
product_types = {"background"}
representations = {"json"}
def load(self, context, name=None, namespace=None, data=None):
path = self.filepath_from_context(context)
with open(path) as json_file:
data = json.load(json_file)
layers = list()
for child in data['children']:
if child.get("filename"):
layers.append(child["filename"])
else:
for layer in child['children']:
if layer.get("filename"):
layers.append(layer["filename"])
bg_folder = os.path.dirname(path)
product_name = context["product"]["name"]
# read_node_name += "_{}".format(uuid.uuid4())
container_nodes = []
for layer in sorted(layers):
file_to_import = [
os.path.join(bg_folder, layer).replace("\\", "/")
]
read_node = harmony.send(
{
"function": copy_files + import_files,
"args": ["Top", file_to_import, layer, 1]
}
)["result"]
container_nodes.append(read_node)
return harmony.containerise(
product_name,
namespace,
product_name,
context,
self.__class__.__name__,
nodes=container_nodes
)
def update(self, container, context):
repre_entity = context["representation"]
path = get_representation_path(repre_entity)
with open(path) as json_file:
data = json.load(json_file)
layers = list()
for child in data['children']:
if child.get("filename"):
print(child["filename"])
layers.append(child["filename"])
else:
for layer in child['children']:
if layer.get("filename"):
print(layer["filename"])
layers.append(layer["filename"])
bg_folder = os.path.dirname(path)
print(container)
is_latest = is_representation_from_latest(repre_entity)
for layer in sorted(layers):
file_to_import = [
os.path.join(bg_folder, layer).replace("\\", "/")
]
print(20 * "#")
print(f"FILE TO REPLACE: {file_to_import}")
print(f"LAYER: {layer}")
node = harmony.find_node_by_name(layer, "READ")
print(f"{node}")
if node in container['nodes']:
harmony.send(
{
"function": copy_files + replace_files,
"args": [file_to_import, node, 1]
}
)
else:
read_node = harmony.send(
{
"function": copy_files + import_files,
"args": ["Top", file_to_import, layer, 1]
}
)["result"]
container['nodes'].append(read_node)
# Colour node.
sig = harmony.signature("set_color")
func = """function %s(args){
for( var i =0; i <= args[0].length - 1; ++i)
{
var red_color = new ColorRGBA(255, 0, 0, 255);
var green_color = new ColorRGBA(0, 255, 0, 255);
if (args[1] == "red"){
node.setColor(args[0], red_color);
}
if (args[1] == "green"){
node.setColor(args[0], green_color);
}
}
}
%s
""" % (sig, sig)
if is_latest:
harmony.send({"function": func, "args": [node, "green"]})
else:
harmony.send({"function": func, "args": [node, "red"]})
harmony.imprint(
container['name'],
{
"representation": repre_entity["id"],
"nodes": container["nodes"]
}
)
def remove(self, container):
for node in container.get("nodes"):
func = """function deleteNode(_node)
{
node.deleteNode(_node, true, true);
}
deleteNode
"""
harmony.send(
{"function": func, "args": [node]}
)
harmony.imprint(container['name'], {}, remove=True)
def switch(self, container, context):
self.update(container, context)
|