forked from ynput/ayon-shotgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrate_shotgrid_publish.py
210 lines (181 loc) · 7.97 KB
/
integrate_shotgrid_publish.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
import os
import re
import platform
import pyblish.api
from openpype.pipeline import KnownPublishError
from openpype.pipeline.publish import get_publish_repre_path
class IntegrateShotgridPublish(pyblish.api.InstancePlugin):
"""
Create published Files from representations and add it to version. If
representation is tagged as shotgrid review, it will add it in
path to movie for a movie file or path to frame for an image sequence.
"""
order = pyblish.api.IntegratorOrder + 0.499
label = "Shotgrid Published Files"
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
sg_session = instance.context.data.get("shotgridSession")
sg_version = instance.data.get("shotgridVersion")
if not sg_version:
return
for representation in instance.data.get("representations", []):
if "shotgridreview" not in representation.get("tags", []):
self.log.debug(
"No 'shotgridreview' tag on representation '%s', skipping.",
representation.get("name")
)
continue
local_path = get_publish_repre_path(
instance, representation, False
)
sg_project = instance.data.get("shotgridProject")
sg_entity = instance.data.get("shotgridEntity")
sg_task = instance.data.get("shotgridTask")
code = os.path.basename(local_path)
# Extract and remove version number from code so Publishedfile versions are
# grouped together. More info about this on:
# https://developer.shotgridsoftware.com/tk-core/_modules/tank/util/shotgun/publish_creation.html
version_number = 0
match = re.search("_v(\d+)", code)
if match:
version_number = int(match.group(1))
# Remove version from name
code = re.sub("_v\d+", "", code)
# Remove frames from name (i.e., filename.1001.exr -> filename.exr)
code = re.sub("\.\d+", "", code)
query_filters = [
["project", "is", sg_project],
["entity", "is", sg_entity],
["version", "is", sg_version],
["code", "is", code],
]
if sg_task:
query_filters.append(["task", "is", sg_task])
sg_published_file = sg_session.find_one(
"PublishedFile",
query_filters
)
sg_local_storage = sg_session.find_one(
"LocalStorage",
filters=[],
fields=["mac_path", "windows_path", "linux_path"]
)
if not sg_local_storage:
KnownPublishError(
"Unable to find a Local Storage in Shotgrid."
"Enable them in Site Preferences > Local Management:"
"https://help.autodesk.com/view/SGSUB/ENU/?guid=SG_Administrator_ar_data_management_ar_linking_local_files_html"
)
self.log.debug("Using the Local Storage: {sg_local_storage}")
try:
if platform.system() == "Windows":
_, file_partial_path = local_path.split(
sg_local_storage["windows_path"]
)
file_partial_path = file_partial_path.replace("\\", "/")
elif platform.system() == "Linux":
_, file_partial_path = local_path.split(
sg_local_storage["linux_path"]
)
elif platform.system() == "Darwin":
_, file_partial_path = local_path.split(
sg_local_storage["mac_path"]
)
file_partial_path = file_partial_path.lstrip("/")
except ValueError:
raise KnownPublishError(
f"Filepath {local_path} doesn't match the "
f"Shotgrid Local Storage {sg_local_storage}"
"Enable them in Site Preferences > Local Management:"
"https://help.autodesk.com/view/SGSUB/ENU/?guid=SG_Administrator_ar_data_management_ar_linking_local_files_html"
)
published_file_data = {
"project": sg_project,
"code": code,
"entity": sg_entity,
"version": sg_version,
"path": {
"local_storage": sg_local_storage,
"relative_path": file_partial_path
},
# Add file type and version number fields
"published_file_type": self._find_published_file_type(
instance, local_path, representation
),
"version_number": version_number,
}
if sg_task:
published_file_data["task"] = sg_task
if not sg_published_file:
try:
sg_published_file = sg_session.create(
"PublishedFile",
published_file_data
)
except Exception as e:
self.log.error(
"Unable to create PublishedFile with data: {}".format(
published_file_data
)
)
raise e
self.log.info(
"Created Shotgrid PublishedFile: {}".format(sg_published_file)
)
else:
sg_session.update(
sg_published_file["type"],
sg_published_file["id"],
published_file_data,
)
self.log.info(
"Update Shotgrid PublishedFile: {}".format(sg_published_file)
)
if instance.data["family"] == "image":
sg_session.upload_thumbnail(
sg_published_file["type"],
sg_published_file["id"],
local_path
)
instance.data["shotgridPublishedFile"] = sg_published_file
def _find_published_file_type(self, instance, filepath, representation):
"""Given a filepath infer what type of published file type it is."""
_, ext = os.path.splitext(filepath)
published_file_type = "Unknown"
if ext in [".exr", ".jpg", ".jpeg", ".png", ".dpx", ".tif", ".tiff"]:
is_sequence = len(representation["files"]) > 1
if is_sequence:
published_file_type = "Rendered Image"
else:
published_file_type = "Image"
elif ext in [".mov", ".mp4"]:
published_file_type = "Movie"
elif ext == ".abc":
published_file_type = "Alembic Cache"
elif ext in [".bgeo", ".sc", ".gz"]:
published_file_type = "Bgeo Geo"
elif ext in [".ma", ".mb"]:
published_file_type = "Maya Scene"
elif ext == ".nk":
published_file_type = "Nuke Script"
elif ext == ".hip":
published_file_type = "Houdini Scene"
elif ext in [".hda"]:
published_file_type = "HDA"
elif ext in [".fbx"]:
published_file_type = "FBX Geo"
filters = [["code", "is", published_file_type]]
sg_session = instance.context.data.get("shotgridSession")
sg_published_file_type = sg_session.find_one(
"PublishedFileType", filters=filters
)
if not sg_published_file_type:
# Create a published file type on the fly
sg_published_file_type = sg_session.create(
"PublishedFileType", {"code": published_file_type}
)
return sg_published_file_type