-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.py
292 lines (254 loc) · 9.8 KB
/
app.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
# Copyright (c) 2013 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.
"""
Update the Hiero export to be Tank/Shotgun aware
"""
import re
import os
import sys
import shutil
import tempfile
import traceback
import sgtk
from sgtk.platform.qt import QtCore
from sgtk.platform import Application
from sgtk import TankError
import hiero.ui
import hiero.core
import hiero.exporters
from hiero.exporters import FnExternalRender
from hiero.exporters import FnNukeShotExporter
# do not use tk import here, hiero needs the classes to be in their
# standard namespace, hack to get the right path in sys.path
sys.path.append(os.path.join(os.path.dirname(__file__), "python"))
from tk_hiero_export import (
ShotgunShotProcessor,
ShotgunShotProcessorUI,
ShotgunShotUpdater,
ShotgunTranscodePreset,
ShotgunNukeShotPreset,
ShotgunAudioPreset,
ShotgunShotUpdaterPreset,
ShotgunTranscodeExporter,
ShotgunNukeShotExporter,
ShotgunAudioExporter,
ShotgunShotProcessorPreset,
ShotgunTranscodeExporterUI,
ShotgunNukeShotExporterUI,
ShotgunAudioExporterUI,
ShotgunHieroObjectBase,
)
sys.path.pop()
# list keywords Hiero is using in its export substitution
HIERO_SUBSTITUTION_KEYWORDS = [
"clip",
"day",
"DD",
"event",
"ext",
"filebase",
"fileext",
"filehead",
"filename",
"filepadding",
"fullbinpath",
"fullday",
"fullmonth",
"MM",
"month",
"project",
"projectroot",
"sequence",
"shot",
"tk_version",
"track",
"user",
"version",
"YY",
"YYYY",
]
class HieroExport(Application):
def init_app(self):
# let the shot exporter know when the first shot is being run
self.first_shot = False
self._register_exporter()
@property
def context_change_allowed(self):
"""
Specifies that context changes are allowed.
"""
return True
def get_default_encoder_name(self):
"""Returns the default encoder for use in quicktime generation.
The value returned is dependent on the platform and the version of
Hiero/NukeStudio being used.
The "mov32" encoder does not work in newer versions of Hiero/NukeStudio
(10.0v2 or greater) where there is no dependency on the Qt desktop
application.
:return: The name of the default encoder to use for this platform
and version of Hiero/NukeStudio
:rtype: str
"""
if sgtk.util.is_linux():
encoder_name = "mov64"
else:
encoder_name = "mov32"
try:
import nuke
if nuke.NUKE_VERSION_MAJOR >= 10 and nuke.NUKE_VERSION_RELEASE > 1:
# newer version of nuke without access to desktop Qt
encoder_name = "mov64"
except ImportError:
# can't import nuke. older version of Hiero
pass
return encoder_name
def get_nuke_version_tuple(self):
"""
Returns a tuple of the nuke version for comparing against using python's
handy tuple comparison.
Usage example::
# see if the current version is >= Nuke 10.5v1
if app.get_nuke_version_tuple() >= (10, 5, 1):
...
"""
import nuke
return (
nuke.NUKE_VERSION_MAJOR,
nuke.NUKE_VERSION_MINOR,
nuke.NUKE_VERSION_RELEASE,
)
def _register_exporter(self):
"""
Set up this app with the hiero exporter frameworks
"""
# register our app with the base class that all custom hiero objects derive from.
ShotgunHieroObjectBase.setApp(self)
hiero.core.taskRegistry.registerTask(
ShotgunShotUpdaterPreset, ShotgunShotUpdater
)
hiero.core.taskRegistry.registerTask(
ShotgunTranscodePreset, ShotgunTranscodeExporter
)
hiero.core.taskRegistry.registerTask(
ShotgunNukeShotPreset, ShotgunNukeShotExporter
)
hiero.core.taskRegistry.registerTask(ShotgunAudioPreset, ShotgunAudioExporter)
hiero.core.taskRegistry.registerProcessor(
ShotgunShotProcessorPreset, ShotgunShotProcessor
)
hiero.ui.taskUIRegistry.registerTaskUI(
ShotgunTranscodePreset, ShotgunTranscodeExporterUI
)
hiero.ui.taskUIRegistry.registerTaskUI(
ShotgunNukeShotPreset, ShotgunNukeShotExporterUI
)
hiero.ui.taskUIRegistry.registerTaskUI(
ShotgunAudioPreset, ShotgunAudioExporterUI
)
hiero.ui.taskUIRegistry.registerProcessorUI(
ShotgunShotProcessorPreset, ShotgunShotProcessorUI
)
# Add our default preset
self._old_AddDefaultPresets_fn = hiero.core.taskRegistry._defaultPresets
hiero.core.taskRegistry.setDefaultPresets(self._add_default_presets)
def _add_default_presets(self, overwrite):
"""
Hiero std method to add new exporter presets.
Passed in to hiero.core.taskRegistry.setDefaultPresets() as a function pointer.
"""
# add all built-in defaults
self._old_AddDefaultPresets_fn(overwrite)
# Add Shotgun template
name = "Basic PTR Shot"
localpresets = [
preset.name() for preset in hiero.core.taskRegistry.localPresets()
]
# only add the preset if it is not already there - or if a reset to defaults is requested.
if overwrite or name not in localpresets:
# grab all our path templates
plate_template = self.get_template("template_plate_path")
script_template = self.get_template("template_nuke_script_path")
render_template = self.get_template("template_render_path")
# call the hook to translate them into hiero paths, using hiero keywords
plate_hiero_str = self.execute_hook(
"hook_translate_template", template=plate_template, output_type="plate"
)
self.log_debug("Translated %s --> %s" % (plate_template, plate_hiero_str))
script_hiero_str = self.execute_hook(
"hook_translate_template",
template=script_template,
output_type="script",
)
self.log_debug("Translated %s --> %s" % (script_template, script_hiero_str))
render_hiero_str = self.execute_hook(
"hook_translate_template",
template=render_template,
output_type="render",
)
self.log_debug("Translated %s --> %s" % (render_template, render_hiero_str))
# check so that no unknown keywords exist in the templates after translation
self._validate_hiero_export_template(plate_hiero_str)
self._validate_hiero_export_template(script_hiero_str)
self._validate_hiero_export_template(render_hiero_str)
# and set the default properties to be based off of those templates
# Set the quicktime defaults per our hook
file_type, file_options = self.execute_hook(
"hook_get_quicktime_settings", for_shotgun=False
)
properties = {
"exportTemplate": (
(
script_hiero_str,
ShotgunNukeShotPreset("", {"readPaths": [], "writePaths": []}),
),
(
render_hiero_str,
FnExternalRender.NukeRenderPreset(
"", {"file_type": "dpx", "dpx": {"datatype": "10 bit"}}
),
),
(
plate_hiero_str,
ShotgunTranscodePreset(
"", {"file_type": file_type, file_type: file_options}
),
),
)
}
preset = ShotgunShotProcessorPreset(name, properties)
hiero.core.taskRegistry.removeProcessorPreset(name)
hiero.core.taskRegistry.addProcessorPreset(name, preset)
def _validate_hiero_export_template(self, template_str):
"""
Validate that a template_str only contains Hiero substitution keywords or custom
keywords created via the resolve_custom_strings hook.
"""
# build list of valid tokens
custom_substitution_keywords = [
x["keyword"] for x in self.get_setting("custom_template_fields")
]
valid_substitution_keywords = (
HIERO_SUBSTITUTION_KEYWORDS + custom_substitution_keywords
)
hiero_resolver_tokens = ["{%s}" % x for x in valid_substitution_keywords]
# replace all tokens we know about in the template
for x in hiero_resolver_tokens:
template_str = template_str.replace(x, "")
# find any remaining {xyz} tokens in the template
regex = r"(?<={)[a-zA-Z_ 0-9]+(?=})"
key_names = re.findall(regex, template_str)
if len(key_names) > 0:
raise TankError(
"The configuration template '%s' contains keywords %s which are "
"not recognized by Hiero. Either remove them from the sgtk template "
"or adjust the hook that converts a template to a hiero export "
"path to convert these fields into fixed strings or hiero "
"substitution tokens." % (template_str, ",".join(key_names))
)