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 | class IntegrateShotgridVersion(pyblish.api.InstancePlugin):
"""Integrate Shotgrid Version"""
order = pyblish.api.IntegratorOrder + 0.497
label = "Shotgrid Version"
# Dictionary of SG fields we want to update that map to other fields in the
# AYON entity
fields_to_add = {
"comment": (str, "description"),
"productType": (str, "sg_version_type"),
}
def process(self, instance):
# Skip execution if instance is marked to be processed in the farm
if instance.data.get("farm"):
self.log.info(
"Instance is marked to be processed on farm. Skipping")
return
context = instance.context
# Dictionary that holds all the data we want to set/update on
# the corresponding SG version
data_to_update = {}
intent = context.data.get("intent")
if intent:
data_to_update["sg_status_list"] = intent["value"]
# find thumbnail path
thumbnail_path = instance.data.get("thumbnailPath")
found_reviewable = False
for representation in instance.data.get("representations", []):
self.log.debug(pformat(representation))
if "shotgridreview" not in representation.get("tags", []):
continue
local_path = get_publish_repre_path(
instance, representation, False
)
if f".{representation['ext']}" in VIDEO_EXTENSIONS:
found_reviewable = True
data_to_update["sg_path_to_movie"] = local_path
if (
"slate" in instance.data["families"]
and "slate-frame" in representation["tags"]
):
data_to_update["sg_movie_has_slate"] = True
elif f".{representation['ext']}" in IMAGE_EXTENSIONS:
found_reviewable = True
# Replace the frame number with '%04d'
path_to_frame = re.sub(r"\.\d+\.", ".%04d.", local_path)
data_to_update.update({
"sg_path_to_movie": path_to_frame,
"sg_path_to_frames": path_to_frame,
})
if "slate" in instance.data["families"]:
data_to_update["sg_frames_have_slate"] = True
if not found_reviewable and thumbnail_path is not None:
# create a thumbnail data to update
found_reviewable = True
data_to_update.update({
"sg_path_to_movie": thumbnail_path,
"sg_path_to_frames": thumbnail_path,
})
# If there's no data to set/update, skip creation of SG version
if not found_reviewable:
self.log.info(
"No data to integrate to SG for product name "
f"'{instance.data['productName']}', skipping "
"version creation."
)
return
sg_session = context.data["shotgridSession"]
# TODO: Use path template solver to build version code from settings
anatomy_data = instance.data.get("anatomyData", {})
version_name_tokens = [
anatomy_data["folder"]["name"],
instance.data["productName"],
]
if instance.data["shotgridTask"]:
version_name_tokens.append(
instance.data["shotgridTask"]["content"]
)
version_name_tokens.append(
"v{:03}".format(int(anatomy_data["version"]))
)
version_name = "_".join(version_name_tokens)
self.log.info(
f"Integrating Shotgrid version with code: {version_name}")
sg_version = self._find_existing_version(version_name, instance)
if not sg_version:
sg_version = self._create_version(version_name, instance)
self.log.info(f"Create Shotgrid version: {sg_version}")
else:
self.log.info(f"Use existing Shotgrid version: {sg_version}")
# Upload movie to version
path_to_movie = data_to_update.get("sg_path_to_movie")
if path_to_movie:
self.log.info(
"Upload review: {} for version shotgrid {}".format(
path_to_movie, sg_version.get("id")
)
)
sg_session.upload(
"Version",
sg_version.get("id"),
path_to_movie,
field_name="sg_uploaded_movie",
)
# Update frame start/end on the version
frame_start = instance.data.get(
"frameStart", context.data.get("frameStart"))
handle_start = instance.data.get(
"handleStart", context.data.get("handleStart"))
if frame_start is not None and handle_start is not None:
frame_start = int(frame_start)
handle_start = int(handle_start)
data_to_update["sg_first_frame"] = frame_start - handle_start
frame_end = instance.data.get(
"frameEnd", context.data.get("frameEnd"))
handle_end = instance.data.get(
"handleEnd", context.data.get("handleEnd"))
if frame_end is not None and handle_end is not None:
frame_end = int(frame_end)
handle_end = int(handle_end)
data_to_update["sg_last_frame"] = frame_end + handle_end
# Add a few extra fields from AY to SG version
for ay_field, sg_field in self.fields_to_add.items():
field_value = (
instance.data.get(ay_field) or context.data.get(ay_field))
if field_value:
# Break sg_field tuple into whatever type of data it
# is and its name
type_, field_name = sg_field
data_to_update[field_name] = type_(field_value)
# Add version objectId to "sg_ayon_id" so we can keep a link
# between both
version_id = instance.data.get("versionEntity", {}).get("id")
if not version_id:
self.log.warning(
"Instance doesn't have a 'versionEntity' to extract the id."
)
version_id = "-"
data_to_update["sg_ayon_id"] = str(version_id)
self.log.info(f"Updating Shotgrid version with {data_to_update}")
sg_session.update("Version", sg_version["id"], data_to_update)
instance.data["shotgridVersion"] = sg_version
self.log.debug(f"Shotgrid version: {sg_version}")
def _find_existing_version(self, version_name, instance):
"""Find if a Version already exists in ShotGrid.
Args:
version_name(str): The full version name, `code` field in SG.
instance (pyblish.Instance): The version's Instance.
Returns:
dict/None: A ShotGrid Version or None if there is none.
"""
filters = [
["project", "is", instance.data.get("shotgridProject")],
["entity", "is", instance.data.get("shotgridEntity")],
["code", "is", version_name],
]
if instance.data.get("shotgridTask"):
filters.append(
["sg_task", "is", instance.data.get("shotgridTask")])
return instance.context.data["shotgridSession"].find_one(
"Version",
filters
)
def _create_version(self, version_name, instance):
"""Create a Shotgrid Version
Args:
version_name(str): The full version name, `code` field in SG.
instance (pyblish.Instance): The version's Instance.
Returns:
dict: The newly created Shotgrid Version.
"""
filters = [['login', 'is', instance.context.data["shotgridUser"]]]
try:
user_id = instance.context.data["shotgridSession"].find(
'HumanUser', filters)[0]["id"]
except IndexError as e:
raise ValueError(
f"User {instance.context.data['shotgridUser']} not found in "
"Shotgrid."
) from e
version_data = {
"project": instance.data.get("shotgridProject"),
"entity": instance.data.get("shotgridEntity"),
"code": version_name,
"user": {'type': 'HumanUser', 'id': user_id},
}
if instance.data.get("shotgridTask"):
version_data["sg_task"] = instance.data["shotgridTask"]
return instance.context.data["shotgridSession"].create(
"Version",
version_data
)
|