forked from shotgunsoftware/tk-hiero-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiero_resolve_custom_strings.py
88 lines (72 loc) · 3 KB
/
hiero_resolve_custom_strings.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
# Copyright (c) 2014 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.
import sgtk
from tank.util import shotgun
from tank import Hook
class HieroResolveCustomStrings(Hook):
"""
This class implements a hook that is used to resolve custom tokens into
their concrete value when paths are being processed during the export.
"""
# Cache of shots that have already been pulled from shotgun
_sg_lookup_cache = {}
def execute(self, task, keyword, **kwargs):
"""
The default implementation of the custom resolver simply looks up
the keyword from the Shotgun Shot entity dictionary. For example,
to pull the shot code, you would simply specify 'code'. To pull
the sequence code you would use 'sg_sequence.Sequence.code'.
:param task: The export task being processed.
:param str keyword: The keyword token that needs to be resolved.
:returns: The resolved keyword value to be replaced into the
associated string.
:rtype: str
"""
shot_code = task._item.name()
if keyword == "{Projectcode}":
# Getting shotgun credentials
sg = shotgun.get_sg_connection()
# Finding current project name
current_engine = sgtk.platform.current_engine()
current_context = current_engine.context
projectname = current_context.project["name"]
projectcode = sg.find_one(
"Project",
[["name", "is", projectname]],
["sg_projectcode"],
)
return projectcode.get("sg_projectcode")
# grab the shot from the cache, or the get_shot hook if not cached
sg_shot = self._sg_lookup_cache.get(shot_code)
if sg_shot is None:
fields = [
ctf["keyword"]
for ctf in self.parent.get_setting("custom_template_fields")
]
sg_shot = self.parent.execute_hook(
"hook_get_shot",
task=task,
item=task._item,
data=self.parent.preprocess_data,
fields=fields,
upload_thumbnail=False,
)
self._sg_lookup_cache[shot_code] = sg_shot
if sg_shot is None:
raise RuntimeError(
"Could not find shot for custom resolver: %s" % keyword
)
# strip off the leading and trailing curly brackets
keyword = keyword[1:-1]
result = sg_shot.get(keyword, "")
self.parent.log_debug(
"Custom resolver: %s[%s] -> %s" % (shot_code, keyword, result)
)
return result