Blender V5.3
otio_import_resolve.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11
12#include "BLI_function_ref.hh"
13#include "BLI_map.hh"
14#include "BLI_math_base.hh"
16#include "BLI_string_ref.hh"
17#include "BLI_vector.hh"
18#include "BLI_vector_set.hh"
19
20#include "DNA_scene_types.h"
21
22#include "SEQ_connect.hh"
23
24#include "opentimelineio/anyVector.h"
25#include "opentimelineio/clip.h"
26#include "opentimelineio/effect.h"
27
29#include "otio_metadata.hh"
30#include "otio_resolve.hh"
31
32namespace blender::io::otio {
33
34static const AnyDictionary *resolve_metadata(SerializableObjectWithMetadata &object)
35{
36 return find<AnyDictionary>(object.metadata(), "Resolve_OTIO");
37}
38
39static const AnyDictionary *metadata_effect(SerializableObjectWithMetadata &object)
40{
41 const AnyDictionary *resolve = resolve_metadata(object);
42 return resolve ? find<AnyDictionary>(*resolve, "Effects") : nullptr;
43}
44
45static const AnyDictionary *find_parameter(const AnyDictionary &effect, const std::string &id)
46{
47 const AnyVector *parameters = find<AnyVector>(effect, "Parameters");
48 if (!parameters) {
49 return nullptr;
50 }
51 for (const std::any &item : *parameters) {
52 const AnyDictionary *parameter = std::any_cast<AnyDictionary>(&item);
53 const std::string *name = parameter ? find<std::string>(*parameter, "Parameter ID") : nullptr;
54 if (name && *name == id) {
55 return parameter;
56 }
57 }
58 return nullptr;
59}
60
61static const std::any *parameter(const AnyDictionary &effect, const std::string &id)
62{
63 const AnyDictionary *parameter = find_parameter(effect, id);
64 return parameter ? find(*parameter, "Parameter Value") : nullptr;
65}
66
67static std::optional<double> number(const std::any *value)
68{
69 return value ? to_number(*value) : std::nullopt;
70}
71
72static Vector<double2> parameter_keyframes(const AnyDictionary &effect,
73 const std::string &id,
74 const int start_frame,
75 const FunctionRef<double(double)> convert)
76{
77 const AnyDictionary *parameter = find_parameter(effect, id);
78 const AnyDictionary *keys = parameter ? find<AnyDictionary>(*parameter, "Key Frames") : nullptr;
79 Vector<double2> keyframes;
80 if (!keys) {
81 return keyframes;
82 }
83 for (const auto &[frame, key] : *keys) {
84 const AnyDictionary *keyframe = std::any_cast<AnyDictionary>(&key);
85 if (const std::optional<double> value = number(keyframe ? find(*keyframe, "Value") : nullptr))
86 {
87 keyframes.append(
88 double2(start_frame + std::strtod(frame.c_str(), nullptr), convert(*value)));
89 }
90 }
91 std::sort(keyframes.begin(), keyframes.end(), [](const double2 &a, const double2 &b) {
92 return a.x < b.x;
93 });
94 return keyframes;
95}
96
97static std::any keyframes_value(const Span<double2> keyframes)
98{
99 AnyVector values;
100 for (const double2 &keyframe : keyframes) {
101 values.push_back(AnyVector{keyframe.x, keyframe.y});
102 }
103 AnyDictionary animation;
104 animation["keyframes"] = values;
105 return animation;
106}
107
108static std::optional<std::any> animated_number(const AnyDictionary &effect,
109 const std::string &id,
110 const int start_frame,
111 const FunctionRef<double(double)> convert)
112{
113 const Vector<double2> keyframes = parameter_keyframes(effect, id, start_frame, convert);
114 if (!keyframes.is_empty()) {
115 return keyframes_value(keyframes);
116 }
117 const std::optional<double> value = number(parameter(effect, id));
118 return value ? std::make_optional<std::any>(convert(*value)) : std::nullopt;
119}
120
121static double value_at(const Span<double2> keyframes, const double frame)
122{
123 for (const int i : keyframes.index_range().drop_front(1)) {
124 if (frame < keyframes[i].x) {
125 const double2 &a = keyframes[i - 1];
126 const double2 &b = keyframes[i];
127 return math::interpolate(a.y, b.y, std::clamp((frame - a.x) / (b.x - a.x), 0.0, 1.0));
128 }
129 }
130 return keyframes.last().y;
131}
132
134 const double start_frame,
135 const double end_frame,
136 const double fade_in,
137 const double fade_out)
138{
139 const double fade_in_end = start_frame + fade_in;
140 const double fade_out_start = end_frame - fade_out;
141 Vector<double2> faded;
142 if (fade_in > 0.0) {
143 faded.append(double2(start_frame, 0.0));
144 }
145 faded.append(double2(fade_in_end, value_at(keyframes, fade_in_end)));
146 for (const double2 &keyframe : keyframes) {
147 if (keyframe.x > fade_in_end && keyframe.x < fade_out_start) {
148 faded.append(keyframe);
149 }
150 }
151 faded.append(double2(fade_out_start, value_at(keyframes, fade_out_start)));
152 if (fade_out > 0.0) {
153 faded.append(double2(end_frame, 0.0));
154 }
155 return faded;
156}
157
158static void copy_number(AnyDictionary &properties,
159 const StringRef rna_path,
160 const AnyDictionary &effect,
161 const std::string &id,
162 const int start_frame,
163 const double factor)
164{
165 std::optional<std::any> value = animated_number(
166 effect, id, start_frame, [&](const double value) { return value * factor; });
167 if (!value) {
168 return;
169 }
170 const int64_t dot = rna_path.find('.');
171 if (dot == StringRef::not_found) {
172 properties[rna_path] = *value;
173 return;
174 }
175 ensure_dictionary(properties, rna_path.substr(0, dot))[rna_path.substr(dot + 1)] = *value;
176}
177
178static void add_effect_properties(const AnyDictionary &effect,
179 const Scene &scene,
180 const Strip &strip,
181 AnyDictionary &properties)
182{
183 const std::string *name = find<std::string>(effect, "Effect Name");
184 const bool *enabled = find<bool>(effect, "Enabled");
185 if (!name || (enabled && !*enabled)) {
186 return;
187 }
188
189 ResolveUnits units(scene, strip);
190 if (strip.data && strip.data->transform) {
191 units.fit_scale = float2(strip.data->transform->scale_x, strip.data->transform->scale_y);
192 }
193 const int start_frame = strip.left_handle();
194
195 for (const ResolveNumber &number : resolve_numbers()) {
196 if (number.effect == *name) {
197 copy_number(properties,
198 number.rna_path,
199 effect,
200 number.parameter,
201 start_frame,
202 units.to_blender(number.unit));
203 }
204 }
205
206 if (*name == "Transform") {
207 const AnyVector *anchor = std::any_cast<AnyVector>(
208 parameter(effect, "transformationAnchorPoint"));
209 if (anchor && anchor->size() == 2) {
210 const float2 offset(to_number((*anchor)[0]).value_or(0.0),
211 to_number((*anchor)[1]).value_or(0.0));
212 const float2 origin = 0.5f + offset * units.anchor_to_origin();
213 ensure_dictionary(properties, "transform")["origin"] = AnyVector{double(origin.x),
214 double(origin.y)};
215 }
216 if (const std::any *flip_x = parameter(effect, "transformationFlipX")) {
217 properties["use_flip_x"] = *flip_x;
218 }
219 if (const std::any *flip_y = parameter(effect, "transformationFlipY")) {
220 properties["use_flip_y"] = *flip_y;
221 }
222 }
223 else if (*name == "Composite") {
224 const std::any *mode = parameter(effect, "composite mode");
225 const int64_t *mode_value = mode ? std::any_cast<int64_t>(mode) : nullptr;
226 if (const char *blend_type = mode_value ? resolve_blend_type(*mode_value) : nullptr) {
227 properties["blend_type"] = std::string(blend_type);
228 }
229 }
230 else if (*name == "Fairlight Clip Volume and Fades") {
231 const auto to_gain = [](const double decibels) { return std::pow(10.0, decibels / 20.0); };
232 Vector<double2> volume = parameter_keyframes(effect, "volume", start_frame, to_gain);
233 if (volume.is_empty()) {
234 volume.append(
235 double2(start_frame, to_gain(number(parameter(effect, "volume")).value_or(0.0))));
236 }
237 const double fade_in = number(parameter(effect, "faderIn")).value_or(0.0);
238 const double fade_out = number(parameter(effect, "faderOut")).value_or(0.0);
239 if (fade_in > 0.0 || fade_out > 0.0) {
240 volume = add_fades(volume, start_frame, strip.right_handle(&scene), fade_in, fade_out);
241 }
242 properties["volume"] = volume.size() == 1 ? std::any(volume[0].y) : keyframes_value(volume);
243 }
244 else if (*name == "Solid Color") {
245 const std::any *color = parameter(effect, "color");
246 if (const std::string *hex = color ? std::any_cast<std::string>(color) : nullptr) {
247 properties["color"] = hex_to_color(*hex);
248 }
249 }
250 else if (*name == "Rich Text") {
251 if (const std::any *text = parameter(effect, "rich text")) {
252 properties["text"] = *text;
253 }
254 if (const std::any *position = parameter(effect, "position")) {
255 properties["location"] = *position;
256 }
257 }
258}
259
260std::optional<StripType> resolve_generator_type(const GeneratorReference &generator)
261{
262 const std::string &kind = generator.generator_kind();
263 if (kind == "Solid Color") {
264 return STRIP_TYPE_COLOR;
265 }
266 if (kind == "Text" || StringRef(kind).startswith("Rich")) {
267 return STRIP_TYPE_TEXT;
268 }
269 return std::nullopt;
270}
271
272static Vector<const AnyDictionary *> resolve_effects(SerializableObjectWithMetadata &object)
273{
275 if (auto *item = dynamic_cast<Item *>(&object)) {
276 for (const SerializableObject::Retainer<otio::Effect> &effect : item->effects()) {
277 if (const AnyDictionary *resolve = resolve_metadata(*effect)) {
278 effects.append(resolve);
279 }
280 }
281 }
282 if (auto *clip = dynamic_cast<Clip *>(&object)) {
283 if (auto *generator = dynamic_cast<GeneratorReference *>(clip->media_reference())) {
284 if (const AnyVector *list = find<AnyVector>(generator->parameters(), "Resolve_OTIO")) {
285 for (const std::any &item : *list) {
286 if (const AnyDictionary *effect = std::any_cast<AnyDictionary>(&item)) {
287 effects.append(effect);
288 }
289 }
290 }
291 }
292 }
293 if (const AnyDictionary *effect = metadata_effect(object)) {
294 effects.append(effect);
295 }
296 return effects;
297}
298
300{
301 for (const AnyDictionary *effect : resolve_effects(clip)) {
302 const int64_t *type = find<int64_t>(*effect, "Type");
303 if (type && *type == 74) {
304 return true;
305 }
306 }
307 return false;
308}
309
310std::optional<StripType> resolve_transition_type(Transition &transition)
311{
312 const AnyDictionary *effect = metadata_effect(transition);
313 const std::string *name = effect ? find<std::string>(*effect, "Effect Name") : nullptr;
314 if (name && *name == "Edge Wipe") {
315 return STRIP_TYPE_WIPE;
316 }
317 return std::nullopt;
318}
319
320std::optional<eSeqImageFitMethod> resolve_fit_method(Item &item)
321{
322 for (const AnyDictionary *effect : resolve_effects(item)) {
323 const std::string *name = find<std::string>(*effect, "Effect Name");
324 if (!name || *name != "Retime and Scaling") {
325 continue;
326 }
327 switch (int(number(parameter(*effect, "scale")).value_or(0.0))) {
328 case 1:
330 case 2:
331 return SEQ_SCALE_TO_FIT;
332 case 3:
333 return SEQ_SCALE_TO_FILL;
334 case 4:
335 return SEQ_STRETCH_TO_FILL;
336 }
337 }
338 return std::nullopt;
339}
340
341AnyDictionary resolve_strip_properties(const Scene &scene,
342 const Strip &strip,
343 SerializableObjectWithMetadata &object)
344{
345 AnyDictionary properties;
346 for (const AnyDictionary *effect : resolve_effects(object)) {
347 add_effect_properties(*effect, scene, strip, properties);
348 }
349 return properties;
350}
351
352bool resolve_track_locked(Track &track)
353{
354 const AnyDictionary *resolve = resolve_metadata(track);
355 const bool *locked = resolve ? find<bool>(*resolve, "Locked") : nullptr;
356 return locked && *locked;
357}
358
360 const Span<std::pair<Strip *, SerializableObjectWithMetadata *>> strips)
361{
363 for (const auto &[strip, object] : strips) {
364 const AnyDictionary *resolve = resolve_metadata(*object);
365 if (const int64_t *link_group = resolve ? find<int64_t>(*resolve, "Link Group ID") : nullptr) {
366 link_groups.lookup_or_add_default(*link_group).add(strip);
367 }
368 }
369 for (VectorSet<Strip *> &link_group : link_groups.values()) {
370 seq::connect(link_group);
371 }
372}
373
374} // namespace blender::io::otio
constexpr IndexRange drop_front(int64_t n) const
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
ValueIterator values() const &
Definition BLI_map.hh:884
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr StringRef()
int64_t size() const
void append(const T &value)
bool is_empty() const
const T & data(const Action &owning_action) const
static constexpr int64_t not_found
#define position
float dot(VecOp< float, D >, VecOp< float, D >) RET
bool resolve_track_locked(Track &track)
static const std::any * parameter(const AnyDictionary &effect, const std::string &id)
static const AnyDictionary * find_parameter(const AnyDictionary &effect, const std::string &id)
void resolve_connect_strips(const Span< std::pair< Strip *, SerializableObjectWithMetadata * > > strips)
static std::optional< std::any > animated_number(const AnyDictionary &effect, const std::string &id, const int start_frame, const FunctionRef< double(double)> convert)
const char * resolve_blend_type(const int64_t composite_mode)
static Vector< double2 > add_fades(const Span< double2 > keyframes, const double start_frame, const double end_frame, const double fade_in, const double fade_out)
static const AnyDictionary * metadata_effect(SerializableObjectWithMetadata &object)
AnyVector hex_to_color(const std::string &hex)
static std::any keyframes_value(const Span< double2 > keyframes)
AnyDictionary & ensure_dictionary(AnyDictionary &parent, const std::string &key)
static double value_at(const Span< double2 > keyframes, const double frame)
Span< ResolveNumber > resolve_numbers()
std::optional< eSeqImageFitMethod > resolve_fit_method(Item &item)
std::optional< double > to_number(const std::any &value)
static const AnyDictionary * resolve_metadata(SerializableObjectWithMetadata &object)
AnyDictionary resolve_strip_properties(const Scene &scene, const Strip &strip, SerializableObjectWithMetadata &object)
const std::any * find(const AnyDictionary &dictionary, const std::string &key)
static void add_effect_properties(const AnyDictionary &effect, const Scene &scene, const Strip &strip, AnyDictionary &properties)
static void copy_number(AnyDictionary &properties, const StringRef rna_path, const AnyDictionary &effect, const std::string &id, const int start_frame, const double factor)
std::optional< StripType > resolve_generator_type(const GeneratorReference &generator)
bool resolve_is_adjustment_clip(Clip &clip)
static Vector< const AnyDictionary * > resolve_effects(SerializableObjectWithMetadata &object)
static Vector< double2 > parameter_keyframes(const AnyDictionary &effect, const std::string &id, const int start_frame, const FunctionRef< double(double)> convert)
std::optional< StripType > resolve_transition_type(Transition &transition)
static std::optional< double > number(const std::any *value)
T interpolate(const T &a, const T &b, const FactorT &t)
void connect(Strip *strip1, Strip *strip2)
__int64 int64_t
Definition BKE_text.h:90
@ SEQ_USE_ORIGINAL_SIZE
@ SEQ_STRETCH_TO_FILL
VecBase< double, 2 > double2
VecBase< float, 2 > float2
double to_blender(ResolveUnit unit) const