-
Notifications
You must be signed in to change notification settings - Fork 0
/
shotgrid_review.py
executable file
·578 lines (473 loc) · 18 KB
/
shotgrid_review.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
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
"""ShotGrid Review
Netherlands Filmacademy 2022
Will use Nuke to automatically create a slate
with Netherlands Filmacademy design, transcode it
and upload to ShotGrid
"""
import nuke
import shotgun_api3
import os
import re
from datetime import datetime
class ShotGridReview(object):
"""Creates slate provided by publish data, transcodes and
uploads to ShotGrid.
Args:
publish_id (int): associated publish id
first_frame (int): first frame from frame sequence
last_frame (int): first frame from frame sequence
sequence_path (str): path to frame sequence
slate_path (str): path to render slate
shotgrid_site (str): url for ShotGrid site
script_name (str): API name for script on ShotGrid
script_key (str): API key for script on ShotGrid
fps (float, optional): fps used by project. Defaults to 25.0.
company (str, optional): company name to add to slate. Defaults to "ShotGrid".
colorspace_idt (str, optional): _description_.
Defaults to "ACES - ACEScg".
colorspace_odt (str, optional): _description_.
Defaults to "Output - sRGB".
"""
def __init__(
self,
publish_id,
first_frame,
last_frame,
sequence_path,
slate_path,
shotgrid_site,
script_name,
script_key,
fps=25.0,
company="ShotGrid",
colorspace_idt="ACES - ACEScg",
colorspace_odt="Output - sRGB",
):
# Get script directory to add gizmo
script_directory = os.path.dirname(os.path.realpath(__file__))
gizmo_directory = os.path.join(script_directory, "gizmo")
gizmo_directory = gizmo_directory.replace(os.sep, "/")
# gizmo_directory = "//nfa-vfxim-deadline.ahk.nl/deadline/custom/plugins/ShotGridReview/gizmo"
nuke.pluginAddPath(gizmo_directory)
# Setting connection to ShotGrid with API
self.sg = shotgun_api3.Shotgun(
shotgrid_site, script_name=script_name, api_key=script_key
)
# Get frame sequences by path
sequence = self.__validate_sequence(sequence_path)
# If sequence is found, proceed
if sequence:
read = self.__setup_script(
sequence_path=sequence_path,
first_frame=first_frame,
last_frame=last_frame,
fps=fps,
sequence=sequence,
colorspace_idt=colorspace_idt,
)
# Get publish data
publish_data = self.__get_publish_data(publish_id)
# Create slate
slate = self.__setup_slate(
read_node=read,
publish_data=publish_data,
company=company,
first_frame=first_frame,
last_frame=last_frame,
fps=fps,
colorspace_idt=colorspace_idt,
colorspace_odt=colorspace_odt,
)
# Create write node
write = self.__setup_write(
slate_node=slate,
slate_path=slate_path,
colorspace_odt=colorspace_odt,
fps=fps,
)
# Render slate
self.__render_slate(
write_node=write,
first_frame=first_frame,
last_frame=last_frame,
)
# Upload slate to ShotGrid
self.__upload_to_shotgrid(
publish_data=publish_data,
slate_path=slate_path,
sequence_path=sequence_path,
first_frame=first_frame,
last_frame=last_frame,
fps=fps,
colorspace_odt=colorspace_odt,
)
def __validate_sequence(
self,
sequence_path,
):
"""Check if sequence is existing
Args:
sequence_path (str): sequence to check
Returns:
str or False: if validated returns sequence containing frame list
"""
sequence_directory = os.path.dirname(sequence_path)
sequence_filename = os.path.basename(sequence_path)
sequences = self.__get_frame_sequences(sequence_directory)
for sequence in sequences:
filename = os.path.basename(sequence[0])
if "1001" in filename:
print("Found incorrectly filename, fixing frame padding")
filename = filename.replace("1001", "%04d")
if sequence_filename == filename:
return sequence
raise Exception("No frame sequence found")
@staticmethod
def __setup_script(
sequence_path,
first_frame,
last_frame,
fps,
sequence,
colorspace_idt,
):
"""Creates Nuke script with read node and correct settings
Args:
sequence_path (str): path to file sequence
first_frame (str): first frame for sequence
last_frame (str): last frame for sequence
fps (str): fps used for project
sequence (list): list containing both sequence path and frame list
colorspace_idt (str): colorspace used by read node
Returns:
attribute: created read node
"""
# Setup Nuke script
nuke.root().knob("first_frame").setValue(first_frame)
nuke.root().knob("last_frame").setValue(last_frame)
nuke.root().knob("fps").setValue(fps)
nuke.root().knob("colorManagement").setValue("OCIO")
nuke.root().knob("OCIO_config").setValue(1)
print("Setup script completed")
# Setup read node
read = nuke.createNode("Read")
read.knob("file").setValue(sequence_path)
first_sequence_frame = int(min(sequence[1]))
last_sequence_frame = int(max(sequence[1]))
# Set found frame range by sequence find function
read.knob("first").setValue(first_sequence_frame)
read.knob("origfirst").setValue(first_sequence_frame)
read.knob("last").setValue(last_sequence_frame)
read.knob("origlast").setValue(last_sequence_frame)
read.knob("colorspace").setValue(colorspace_idt)
read.knob("on_error").setValue("checkerboard")
print("Created read node")
# Return created read node
return read
def __get_publish_data(
self,
publish_id,
):
"""Search ShotGrid database for associated publish data
Args:
publish_id (int): id of publish
Returns:
dict: containing all publish data
E.g.:
{
"type": "PublishedFile",
"id": 42421,
"created_by": {
"id": 1,
"name": "Example User",
"type": "HumanUser",
},
"code": "iwr_pri_pri_0030_scene_main_v014.%04d.exr",
"task": {"id": 24136, "name": "comp", "type": "Task"},
"project": {"id": 2602, "name": "it_will_rain",
"type": "Project"},
"entity": {"id": 7193, "name": "pri_0030", "type": "Shot"},
"description": "Integrated DMP",
"version_number": 14,
}
"""
# Create the filter to search on ShotGrid
# for publishes with the same file name
filters = [
["id", "is", publish_id],
]
columns = [
"created_by",
"code",
"task",
"project",
"entity",
"description",
"version_number",
]
# Search on ShotGrid
publish = self.sg.find_one("PublishedFile", filters, columns)
print("Got publish data")
return publish
@staticmethod
def __setup_slate(
read_node,
publish_data,
company,
first_frame,
last_frame,
fps,
colorspace_idt,
colorspace_odt,
):
"""Setup slate with correct parameters
Args:
read_node (attribute): read node to connect slate to
publish_data (dict): dictionary containing publish data
company (str): name used for company knob
first_frame (int): first frame by frame sequence
last_frame (int): last frame by frame sequence
fps (float): fps used by sequence
colorspace_idt (str): colorspace for idt
colorspace_odt (str): colorspace for odt
Returns:
attribute: created slate node
"""
# Create slate node
slate = nuke.createNode("nfaSlate")
# Get project name
project_name = publish_data.get("project")
project_name = project_name.get("name")
slate.knob("project").setValue(project_name)
# Set company name
slate.knob("company").setValue(company)
# Get file name from publish data
submission_name = publish_data.get("code")
slate.knob("file").setValue(submission_name)
# Create frame list
frame_list = "%s - %s (%s)" % (
first_frame,
last_frame,
str(int(last_frame) - int(first_frame)),
)
slate.knob("frameList").setValue(frame_list)
# Get current time
date = datetime.now()
date = date.strftime("%d/%m/%Y %H:%M")
slate.knob("date").setValue(date)
# Get artist name
artist = publish_data.get("created_by")
artist = artist.get("name")
slate.knob("artist").setValue(artist)
task = publish_data.get("task")
task = task.get("name")
slate.knob("task").setValue(task)
# Get version number
version = publish_data.get("version_number")
version = "v%03d" % version
slate.knob("version").setValue(version)
# Set FPS
slate.knob("fps").setValue(fps)
# Set colorspace
slate.knob("colorspaceIDT").setValue(colorspace_idt)
slate.knob("colorspaceODT").setValue(colorspace_odt)
# Get description
description = publish_data.get("description")
slate.knob("description").setValue(description)
# Set read node as input for slate node
slate.setInput(0, read_node)
# Return created node
return slate
@staticmethod
def __setup_write(
slate_node,
slate_path,
colorspace_odt,
fps,
):
"""Create write node with correct settings
Args:
slate_node (attribute): node to connect write node to
slate_path (str): path to render slate to
colorspace_odt (str): output device transform used to render slate
fps (float): fps used for rendering slate
Returns:
attribute: created write node
"""
# Create write node
write = nuke.createNode("Write")
# Set write node settings
write.knob("file").setValue(slate_path)
write.knob("colorspace").setValue(colorspace_odt)
# Set input
write.setInput(0, slate_node)
# Create directories
slate_directory = os.path.dirname(slate_path)
if not os.path.isdir(slate_directory):
print("Slate directory doesn't exist, creating one")
os.makedirs(slate_directory)
return write
@staticmethod
def __render_slate(
write_node,
first_frame,
last_frame,
):
"""Render specified write node
Args:
write_node (attribute): write node to render
first_frame (int): first frame from sequence, will add one in front
for slate
last_frame (int): last frame from sequence
"""
try:
nuke.execute(write_node, first_frame - 1, last_frame)
print("Rendering complete")
except Exception as error:
print("Could not render because %s" % str(error))
def __upload_to_shotgrid(
self,
publish_data,
slate_path,
sequence_path,
first_frame,
last_frame,
fps,
colorspace_odt,
):
"""When slate has been created, upload to ShotGrid
writh provided data and publish data.
Args:
publish_data (dict): dictionary containing all associated
publish data
slate_path (str): path to rendered slate
first_frame (int): first frame from sequence
last_frame (int): last frame from sequence
fps (float): project fps
colorspace_odt (str): colorspace slate is rendered in
"""
project = publish_data.get("project")
submission_name = publish_data.get("code")
description = publish_data.get("description")
entity = publish_data.get("entity")
task = publish_data.get("task")
task_id = task.get("id")
user = publish_data.get("created_by")
publish_id = publish_data.get("id")
data = {
"project": project,
"code": submission_name,
"description": description,
"sg_colorspace": colorspace_odt,
"sg_path_to_movie": slate_path,
"sg_path_to_frames": sequence_path,
"sg_status_list": "rev",
"sg_first_frame": first_frame,
"sg_last_frame": last_frame,
"sg_uploaded_movie_frame_rate": fps,
"frame_range": "%s-%s" % (first_frame, last_frame),
"sg_movie_has_slate": True,
"entity": entity,
"sg_task": task,
"user": user,
"published_files": [{"type": "PublishedFile", "id": publish_id}],
}
# Create Version
version = self.sg.create("Version", data).get("id")
# Upload to ShotGrid
self.sg.upload("Version", version, slate_path, "sg_uploaded_movie")
print("Uploaded to ShotGrid")
# Update task status to review
self.sg.update("Task", task_id, {"sg_status_list": "rev"})
@staticmethod
def __get_frame_sequences(
folder,
extensions=None,
frame_spec=None,
):
"""
Copied from the publisher plugin, and customized to return file
sequences with frame lists instead of filenames
Given a folder, inspect the contained files to find what appear to be
files with frame numbers.
:param folder: The path to a folder potentially containing a
sequence of
files.
:param extensions: A list of file extensions to retrieve paths for.
If not supplied, the extension will be ignored.
:param frame_spec: A string to use to represent the frame number in the
return sequence path.
:return: A list of tuples for each identified frame sequence. The first
item in the tuple is a sequence path with the frame number replaced
with the supplied frame specification. If no frame spec is
supplied,
a python string format spec will be returned with the padding found
in the file.
Example::
get_frame_sequences(
"/path/to/the/folder",
["exr", "jpg"],
frame_spec="{FRAME}"
)
[
(
"/path/to/the/supplied/folder/key_light1.{FRAME}.exr",
[<frame_1_framenumber>, <frame_2_framenumber>, ...]
),
(
"/path/to/the/supplied/folder/fill_light1.{FRAME}.jpg",
[<frame_1_framenumber>, <frame_2_framenumber>, ...]
)
]
"""
FRAME_REGEX = re.compile(r"(.*)([._-])(\d+)\.([^.]+)$", re.IGNORECASE)
# list of already processed file names
processed_names = {}
# examine the files in the folder
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
if os.path.isdir(file_path):
# ignore subfolders
continue
# see if there is a frame number
frame_pattern_match = re.search(FRAME_REGEX, filename)
if not frame_pattern_match:
# no frame number detected. carry on.
continue
prefix = frame_pattern_match.group(1)
frame_sep = frame_pattern_match.group(2)
frame_str = frame_pattern_match.group(3)
extension = frame_pattern_match.group(4) or ""
# filename without a frame number.
file_no_frame = "%s.%s" % (prefix, extension)
if file_no_frame in processed_names:
# already processed this sequence. add the framenumber to the
# list, later we can use this to determine the framerange
processed_names[file_no_frame]["frame_list"].append(frame_str)
continue
if extensions and extension not in extensions:
# not one of the extensions supplied
continue
# make sure we maintain the same padding
if not frame_spec:
padding = len(frame_str)
frame_spec = "%%0%dd" % (padding,)
seq_filename = "%s%s%s" % (prefix, frame_sep, frame_spec)
if extension:
seq_filename = "%s.%s" % (seq_filename, extension)
# build the path in the same folder
seq_path = os.path.join(folder, seq_filename)
# remember each seq path identified and a
# list of files matching the
# seq pattern
processed_names[file_no_frame] = {
"sequence_path": seq_path,
"frame_list": [frame_str],
}
# build the final list of sequence paths to return
frame_sequences = []
for file_no_frame in processed_names:
seq_info = processed_names[file_no_frame]
seq_path = seq_info["sequence_path"]
frame_sequences.append((seq_path, seq_info["frame_list"]))
return frame_sequences