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 | class LoadEditorialPackage(load.LoaderPlugin):
"""Load editorial package to timeline.
Loading timeline from OTIO file included media sources
and timeline structure.
"""
product_types = {"editorial_pkg"}
representations = {"*"}
extensions = {"otio"}
label = "Load as Timeline"
order = -10
icon = "ei.align-left"
color = "orange"
def load(self, context, name, namespace, data):
files = get_representation_path(context["representation"])
search_folder_path = Path(files).parent / "resources"
if not search_folder_path.exists():
search_folder_path = Path(files).parent
project = lib.get_current_project()
media_pool = project.GetMediaPool()
folder_path = context["folder"]["path"]
# create versioned bin for editorial package
version_name = context["version"]["name"]
loaded_bin = lib.create_bin(f"{folder_path}/{name}/{version_name}")
# make timeline unique name based on folder path
folder_path_name = folder_path.replace("/", "_").lstrip("_")
loaded_timeline_name = (
f"{folder_path_name}_{name}_{version_name}_timeline")
import_options = {
"timelineName": loaded_timeline_name,
"importSourceClips": True,
"sourceClipsPath": search_folder_path.as_posix(),
}
# import timeline from otio file
timeline = media_pool.ImportTimelineFromFile(files, import_options)
# get timeline media pool item for metadata update
timeline_media_pool_item = lib.get_timeline_media_pool_item(
timeline, loaded_bin
)
# Update the metadata
clip_data = self._get_container_data(
context, data)
timeline_media_pool_item.SetMetadata(
constants.AYON_TAG_NAME, json.dumps(clip_data)
)
# set clip color based on random choice
clip_color = self.get_random_clip_color()
timeline_media_pool_item.SetClipColor(clip_color)
# TODO: there are two ways to import timeline resources (representation
# and resources folder) but Resolve seems to ignore any of this
# since it is importing sources automatically. But we might need
# to at least set some metadata to those loaded media pool items
print("Timeline imported: ", timeline)
def update(self, container, context):
"""Update the container with the latest version."""
# Get the latest version of the container data
timeline_media_pool_item = container["_item"]
clip_data = timeline_media_pool_item.GetMetadata(
constants.AYON_TAG_NAME)
clip_data = json.loads(clip_data)
clip_data["load"] = {}
# update publish key in publish container data to be False
if clip_data["publish"]["publish"] is True:
clip_data["publish"]["publish"] = False
timeline_media_pool_item.SetMetadata(
constants.AYON_TAG_NAME, json.dumps(clip_data))
self.load(
context,
context["product"]["name"],
container["namespace"],
container
)
def _get_container_data(
self,
context: dict,
data: dict
) -> dict:
"""Return metadata related to the representation and version."""
# add additional metadata from the version to imprint AYON knob
version_entity = context["version"]
for key in ("_item", "name"):
data.pop(key, None) # remove unnecessary key from the data if it exists
data = {
"load": data,
}
# add version attributes to the load data
data["load"].update(
version_entity["attrib"]
)
# add variables related to version context
data["load"].update(
{
"schema": "ayon:container-3.0",
"id": AVALON_CONTAINER_ID,
"loader": str(self.__class__.__name__),
"author": version_entity["data"]["author"],
"representation": context["representation"]["id"],
"version": version_entity["version"],
}
)
# add publish data for streamline publishing
data["publish"] = get_editorial_publish_data(
folder_path=context["folder"]["path"],
product_name=context["product"]["name"],
version=version_entity["version"],
task=context["representation"]["context"].get("task", {}).get(
"name"),
)
return data
def get_random_clip_color(self):
"""Return clip color."""
# list of all available davinci resolve clip colors
colors = [
"Orange",
"Apricot"
"Yellow",
"Lime",
"Olive",
"Green",
"Teal",
"Navy",
"Blue",
"Purple",
"Violet",
"Pink",
"Tan",
"Beige",
"Brown",
"Chocolate",
]
# return one of the colors based on random position
return random.choice(colors)
|