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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578 | class LoadClip(plugin.NukeLoader):
"""Load clip into Nuke
Either it is image sequence or video file.
"""
log = Logger.get_logger(__name__)
product_types = {
"source",
"plate",
"render",
"prerender",
"review",
}
representations = {"*"}
extensions = set(
ext.lstrip(".") for ext in IMAGE_EXTENSIONS.union(VIDEO_EXTENSIONS)
)
settings_category = "nuke"
label = "Load Clip"
order = -20
icon = "file-video-o"
color = "white"
# Loaded from settings
representations_include = []
script_start = int(nuke.root()["first_frame"].value())
# option gui
options_defaults = {
"start_at_workfile": True,
"add_retime": True,
"deep_exr": False
}
node_name_template = "{class_name}_{ext}"
@classmethod
def get_options(cls, *args):
return [
qargparse.Boolean(
"start_at_workfile",
help="Load at workfile start frame",
default=cls.options_defaults["start_at_workfile"]
),
qargparse.Boolean(
"add_retime",
help="Load with retime",
default=cls.options_defaults["add_retime"]
),
qargparse.Boolean(
"deep_exr",
help="Read with deep exr",
default=cls.options_defaults["deep_exr"]
)
]
@classmethod
def get_representations(cls):
return cls.representations_include or cls.representations
def load(self, context, name, namespace, options):
"""Load asset via database."""
project_name = context["project"]["name"]
repre_entity = context["representation"]
version_entity = context["version"]
version_attributes = version_entity["attrib"]
version_data = version_entity["data"]
# reset container id so it is always unique for each instance
self.reset_container_id()
is_sequence = len(repre_entity["files"]) > 1
if is_sequence:
context["representation"] = (
self._representation_with_hash_in_frame(repre_entity)
)
filepath = self.filepath_from_context(context)
filepath = filepath.replace("\\", "/")
self.log.debug("_ filepath: {}".format(filepath))
start_at_workfile = options.get(
"start_at_workfile", self.options_defaults["start_at_workfile"])
add_retime = options.get(
"add_retime", self.options_defaults["add_retime"])
deep_exr = options.get(
"deep_exr", self.options_defaults["deep_exr"])
repre_id = repre_entity["id"]
self.log.debug(
"Representation id `{}` ".format(repre_id))
self.handle_start = version_attributes.get("handleStart", 0)
self.handle_end = version_attributes.get("handleEnd", 0)
first = version_attributes.get("frameStart")
last = version_attributes.get("frameEnd")
first -= self.handle_start
last += self.handle_end
if not is_sequence:
duration = last - first
first = 1
last = first + duration
# If a slate is present, the frame range is 1 frame longer for movies,
# but file sequences its the first frame that is 1 frame lower.
slate_frames = repre_entity["data"].get("slateFrames", 0)
extension = "." + repre_entity["context"]["ext"]
if extension in VIDEO_EXTENSIONS:
last += slate_frames
files_count = len(repre_entity["files"])
if extension in IMAGE_EXTENSIONS and files_count != 1:
first -= slate_frames
# Fallback to folder name when namespace is None
if namespace is None:
namespace = context["folder"]["name"]
if not filepath:
self.log.warning(
"Representation id `{}` is failing to load".format(repre_id))
return
read_name = self._get_node_name(context)
read_node = None
if deep_exr:
# Create the Loader with the filename path set
read_node = nuke.createNode(
"DeepRead",
"name {}".format(read_name),
inpanel=False
)
else:
# Create the Loader with the filename path set
read_node = nuke.createNode(
"Read",
"name {}".format(read_name),
inpanel=False
)
# to avoid multiple undo steps for rest of process
# we will switch off undo-ing
with viewer_update_and_undo_stop():
read_node["file"].setValue(filepath)
if read_node.Class() == "Read":
self.set_colorspace_to_node(
read_node,
filepath,
project_name,
version_entity,
repre_entity
)
self._set_range_to_node(
read_node, first, last, start_at_workfile, slate_frames
)
version_name = version_entity["version"]
if version_name < 0:
version_name = "hero"
data_imprint = {
"version": version_name,
}
# add attributes from the version to imprint metadata knob
for key in [
"frameStart",
"frameEnd",
"source",
"fps",
"handleStart",
"handleEnd",
]:
value = version_attributes.get(key, str(None))
if isinstance(value, str):
value = value.replace("\\", "/")
data_imprint[key] = value
if add_retime and version_data.get("retime"):
data_imprint["addRetime"] = True
read_node["tile_color"].setValue(int("0x4ecd25ff", 16))
container = containerise(
read_node,
name=name,
namespace=namespace,
context=context,
loader=self.__class__.__name__,
data=data_imprint)
if add_retime and version_data.get("retime"):
self._make_retimes(read_node, version_attributes, version_data)
self.set_as_member(read_node)
return container
def switch(self, container, context):
self.update(container, context)
def _representation_with_hash_in_frame(self, repre_entity):
"""Convert frame key value to padded hash
Args:
repre_entity (dict): Representation entity.
Returns:
dict: altered representation data
"""
new_repre_entity = deepcopy(repre_entity)
context = new_repre_entity["context"]
# Get the frame from the context and hash it
frame = context["frame"]
hashed_frame = "#" * len(str(frame))
# Replace the frame with the hash in the originalBasename
if (
"{originalBasename}" in new_repre_entity["attrib"]["template"]
):
origin_basename = context["originalBasename"]
context["originalBasename"] = origin_basename.replace(
frame, hashed_frame
)
# Replace the frame with the hash in the frame
new_repre_entity["context"]["frame"] = hashed_frame
return new_repre_entity
def update(self, container, context):
"""Update the Loader's path
Nuke automatically tries to reset some variables when changing
the loader's path to a new file. These automatic changes are to its
inputs:
"""
project_name = context["project"]["name"]
version_entity = context["version"]
repre_entity = context["representation"]
version_attributes = version_entity["attrib"]
version_data = version_entity["data"]
is_sequence = len(repre_entity["files"]) > 1
read_node = container["node"]
if is_sequence:
repre_entity = self._representation_with_hash_in_frame(
repre_entity
)
filepath = (
get_representation_path(repre_entity)
).replace("\\", "/")
self.log.debug("_ filepath: {}".format(filepath))
start_at_workfile = "start at" in read_node['frame_mode'].value()
add_retime = [
key for key in read_node.knobs().keys()
if "addRetime" in key
]
repre_id = repre_entity["id"]
self.handle_start = version_attributes.get("handleStart", 0)
self.handle_end = version_attributes.get("handleEnd", 0)
first = version_attributes.get("frameStart")
last = version_attributes.get("frameEnd")
first -= self.handle_start
last += self.handle_end
if not is_sequence:
duration = last - first
first = 1
last = first + duration
if not filepath:
self.log.warning(
"Representation id `{}` is failing to load".format(repre_id))
return
read_node["file"].setValue(filepath)
# to avoid multiple undo steps for rest of process
# we will switch off undo-ing
with viewer_update_and_undo_stop():
if read_node.Class() == "Read":
self.set_colorspace_to_node(
read_node,
filepath,
project_name,
version_entity,
repre_entity
)
self._set_range_to_node(read_node, first, last, start_at_workfile)
updated_dict = {
"representation": repre_entity["id"],
"frameStart": str(first),
"frameEnd": str(last),
"version": str(version_entity["version"]),
"source": version_attributes.get("source"),
"handleStart": str(self.handle_start),
"handleEnd": str(self.handle_end),
"fps": str(version_attributes.get("fps"))
}
last_version_entity = ayon_api.get_last_version_by_product_id(
project_name, version_entity["productId"], fields={"id"}
)
# change color of read_node
if version_entity["id"] == last_version_entity["id"]:
color_value = "0x4ecd25ff"
else:
color_value = "0xd84f20ff"
read_node["tile_color"].setValue(int(color_value, 16))
# Update the imprinted representation
update_container(read_node, updated_dict)
self.log.info(
"updated to version: {}".format(version_entity["version"])
)
if add_retime and version_data.get("retime"):
self._make_retimes(read_node, version_attributes, version_data)
else:
self.clear_members(read_node)
self.set_as_member(read_node)
def set_colorspace_to_node(
self,
read_node,
filepath,
project_name,
version_entity,
repre_entity,
):
"""Set colorspace to read node.
Sets colorspace with available names validation.
Args:
read_node (nuke.Node): The nuke's read node
filepath (str): File path.
project_name (str): Project name.
version_entity (dict): Version entity.
repre_entity (dict): Representation entity.
"""
used_colorspace = self._get_colorspace_data(
project_name, version_entity, repre_entity, filepath
)
if (
used_colorspace
and colorspace_exists_on_node(read_node, used_colorspace)
):
self.log.info(f"Used colorspace: {used_colorspace}")
read_node["colorspace"].setValue(used_colorspace)
else:
self.log.info("Colorspace not set...")
def remove(self, container):
read_node = container["node"]
assert read_node.Class() == "Read", "Must be Read"
with viewer_update_and_undo_stop():
members = self.get_members(read_node)
nuke.delete(read_node)
for member in members:
nuke.delete(member)
def _set_range_to_node(
self, read_node, first, last, start_at_workfile, slate_frames=0
):
read_node['origfirst'].setValue(int(first))
read_node['first'].setValue(int(first))
read_node['origlast'].setValue(int(last))
read_node['last'].setValue(int(last))
# set start frame depending on workfile or version
if start_at_workfile:
read_node['frame_mode'].setValue("start at")
start_frame = self.script_start - slate_frames
read_node['frame'].setValue(str(start_frame))
def _make_retimes(self, parent_node, version_attributes, version_data):
''' Create all retime and timewarping nodes with copied animation '''
speed = version_data.get('speed', 1)
time_warp_nodes = version_data.get('timewarps', [])
last_node = None
source_id = self.get_container_id(parent_node)
self.log.debug("__ source_id: {}".format(source_id))
self.log.debug("__ members: {}".format(
self.get_members(parent_node)))
dependent_nodes = self.clear_members(parent_node)
with maintained_selection():
parent_node['selected'].setValue(True)
if speed != 1:
rtn = nuke.createNode(
"Retime",
"speed {}".format(abs(speed))
)
rtn["before"].setValue("continue")
rtn["after"].setValue("continue")
rtn["reverse"].setValue(speed < 0)
rtn["input.first_lock"].setValue(True)
rtn["input.first"].setValue(
version_attributes["frameStart"]
)
rtn["input.last_lock"].setValue(True)
rtn["input.last"].setValue(
version_attributes["frameEnd"]
)
self.set_as_member(rtn)
last_node = rtn
if time_warp_nodes != []:
start_anim = self.script_start + (self.handle_start / speed)
for timewarp in time_warp_nodes:
twn = nuke.createNode(
timewarp["Class"],
"name {}".format(timewarp["name"])
)
if isinstance(timewarp["lookup"], list):
# if array for animation
twn["lookup"].setAnimated()
for i, value in enumerate(timewarp["lookup"]):
twn["lookup"].setValueAt(
(start_anim + i) + value,
(start_anim + i))
else:
# if static value `int`
twn["lookup"].setValue(timewarp["lookup"])
self.set_as_member(twn)
last_node = twn
if dependent_nodes:
# connect to original inputs
for i, n in enumerate(dependent_nodes):
last_node.setInput(i, n)
def _get_node_name(self, context):
folder_entity = context["folder"]
product_name = context["product"]["name"]
repre_entity = context["representation"]
folder_name = folder_entity["name"]
repre_cont = repre_entity["context"]
name_data = {
"folder": {
"name": folder_name,
},
"product": {
"name": product_name,
},
"asset": folder_name,
"subset": product_name,
"representation": repre_entity["name"],
"ext": repre_cont["representation"],
"id": repre_entity["id"],
"class_name": self.__class__.__name__
}
return self.node_name_template.format(**name_data)
def _get_colorspace_data(
self, project_name, version_entity, repre_entity, filepath
):
"""Get colorspace data from version and representation documents
Args:
project_name (str): Project name.
version_entity (dict): Version entity.
repre_entity (dict): Representation entity.
filepath (str): File path.
Returns:
Any[str,None]: colorspace name or None
"""
# Get colorspace from representation colorspaceData if colorspace is
# not found.
colorspace_data = repre_entity["data"].get("colorspaceData", {})
colorspace = colorspace_data.get("colorspace")
self.log.debug(
f"Colorspace from representation colorspaceData: {colorspace}"
)
if not colorspace:
# Get backward compatible colorspace key.
colorspace = repre_entity["data"].get("colorspace")
self.log.debug(
f"Colorspace from representation colorspace: {colorspace}"
)
# Get backward compatible version data key if colorspace is not found.
if not colorspace:
colorspace = version_entity["attrib"].get("colorSpace")
self.log.debug(
f"Colorspace from version colorspace: {colorspace}"
)
config_data = get_current_context_imageio_config_preset()
# check if any filerules are not applicable
new_parsed_colorspace = get_imageio_file_rules_colorspace_from_filepath( # noqa
filepath, "nuke", project_name, config_data=config_data
)
self.log.debug(f"Colorspace new filerules: {new_parsed_colorspace}")
# colorspace from `project_settings/nuke/imageio/regexInputs`
old_parsed_colorspace = get_imageio_input_colorspace(filepath)
self.log.debug(f"Colorspace old filerules: {old_parsed_colorspace}")
return (
new_parsed_colorspace
or old_parsed_colorspace
or colorspace
)
|