Blender V5.3
otio_export_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 <cmath>
10
11#include <fmt/format.h>
12
13#include "BKE_fcurve.hh"
14
15#include "BLI_function_ref.hh"
16#include "BLI_math_color_c.hh"
17
18#include "DNA_anim_types.h"
19#include "DNA_scene_types.h"
20
21#include "RNA_access.hh"
22#include "RNA_path.hh"
23#include "RNA_prototypes.hh"
24
25#include "SEQ_connect.hh"
26#include "SEQ_iterator.hh"
27#include "SEQ_transform.hh"
28
29#include "opentimelineio/anyVector.h"
30#include "opentimelineio/effect.h"
31
33#include "otio_resolve.hh"
34
35namespace blender::io::otio {
36
37static AnyDictionary &resolve_metadata(SerializableObjectWithMetadata &object)
38{
39 return ensure_dictionary(object.metadata(), "Resolve_OTIO");
40}
41
42static AnyDictionary resolve_parameter(const std::string &id,
43 const std::any &value,
44 const std::string &variant_type)
45{
46 AnyDictionary parameter;
47 parameter["Parameter ID"] = id;
48 parameter["Parameter Value"] = value;
49 parameter["Variant Type"] = variant_type;
50 return parameter;
51}
52
53static AnyDictionary resolve_effect(const std::string &effect_name,
54 const int64_t type,
55 const AnyVector &parameters,
56 const std::string &name = "")
57{
58 AnyDictionary effect;
59 effect["Effect Name"] = effect_name;
60 effect["Enabled"] = true;
61 effect["Name"] = name.empty() ? effect_name : name;
62 effect["Parameters"] = parameters;
63 effect["Type"] = type;
64 return effect;
65}
66
67static double rna_number(PointerRNA &ptr, PropertyRNA *prop)
68{
69 switch (RNA_property_type(prop)) {
70 case PROP_INT:
71 return RNA_property_int_get(&ptr, prop);
72 case PROP_FLOAT:
73 return RNA_property_float_get(&ptr, prop);
74 default:
75 return 0.0;
76 }
77}
78
79static AnyDictionary number_parameter(PointerRNA &strip_ptr,
80 const Strip &strip,
81 const std::string &id,
82 const char *rna_path,
83 const FunctionRef<double(double)> convert)
84{
85 PointerRNA ptr;
86 PropertyRNA *prop;
87 double value = 0.0;
88 AnyDictionary keyframes;
89 if (RNA_path_resolve_property(&strip_ptr, rna_path, &ptr, &prop)) {
90 value = rna_number(ptr, prop);
91 const FCurve *fcurve = BKE_fcurve_find_by_rna(
92 &ptr, prop, 0, nullptr, nullptr, nullptr, nullptr);
93 if (fcurve && fcurve->bezt) {
94 for (const BezTriple &bezt : Span(fcurve->bezt, fcurve->totvert)) {
95 AnyDictionary keyframe;
96 keyframe["Value"] = convert(bezt.vec[1][1]);
97 keyframe["Variant Type"] = std::string("Double");
98 keyframes[std::to_string(int(std::round(bezt.vec[1][0] - strip.left_handle())))] =
99 keyframe;
100 }
101 }
102 }
103 AnyDictionary parameter = resolve_parameter(id, convert(value), "Double");
104 parameter["Key Frames"] = keyframes;
105 return parameter;
106}
107
108static AnyVector number_parameters(PointerRNA &strip_ptr,
109 const Strip &strip,
110 const ResolveUnits &units,
111 const StringRef effect)
112{
113 AnyVector parameters;
114 for (const ResolveNumber &number : resolve_numbers()) {
115 if (number.effect == effect) {
116 const double scale = units.to_blender(number.unit);
117 parameters.push_back(number_parameter(
118 strip_ptr, strip, number.parameter, number.rna_path.c_str(), [&](const double value) {
119 return value / scale;
120 }));
121 }
122 }
123 return parameters;
124}
125
126static PointerRNA strip_pointer(const Scene &scene, const Strip &strip)
127{
129 const_cast<ID *>(&scene.id), RNA_Strip, const_cast<Strip *>(&strip));
130}
131
132static AnyVector strip_effects(const Scene &scene, const Strip &strip)
133{
134 PointerRNA ptr = strip_pointer(scene, strip);
135 const ResolveUnits units(scene, strip);
136
137 if (strip.type == STRIP_TYPE_SOUND) {
138 const auto to_decibels = [](const double gain) {
139 return std::max(20.0 * std::log10(gain), -100.0);
140 };
141 return {
142 resolve_effect("Fairlight Clip Volume and Fades",
143 62,
144 {number_parameter(ptr, strip, "volume", "volume", to_decibels)},
145 "Volume"),
146 resolve_effect("Fairlight Clip Pan",
147 72,
148 number_parameters(ptr, strip, units, "Fairlight Clip Pan"),
149 "Pan"),
150 };
151 }
152
153 AnyVector effects;
154 if (strip.type == STRIP_TYPE_ADJUSTMENT) {
155 effects.push_back(resolve_effect("Effect", 74, {}));
156 }
157 if (strip.data && strip.data->transform) {
158 AnyVector parameters = number_parameters(ptr, strip, units, "Transform");
159 const float2 anchor = (seq::image_transform_origin_get(&scene, &strip) - 0.5f) /
160 units.anchor_to_origin();
161 parameters.push_back(resolve_parameter(
162 "transformationAnchorPoint", AnyVector{double(anchor.x), double(anchor.y)}, "POINTF"));
163 parameters.push_back(
164 resolve_parameter("transformationFlipX", bool(strip.flag & SEQ_FLIPX), "Bool"));
165 parameters.push_back(
166 resolve_parameter("transformationFlipY", bool(strip.flag & SEQ_FLIPY), "Bool"));
167 effects.push_back(resolve_effect("Transform", 2, parameters));
168 }
169 if (strip.data && strip.data->crop) {
170 effects.push_back(
171 resolve_effect("Cropping", 3, number_parameters(ptr, strip, units, "Cropping")));
172 }
173
174 AnyVector composite = number_parameters(ptr, strip, units, "Composite");
175 const char *blend_type = "";
177 nullptr, &ptr, RNA_struct_find_property(&ptr, "blend_type"), strip.blend_mode, &blend_type);
178 if (const std::optional<int64_t> mode = resolve_composite_mode(blend_type)) {
179 composite.push_back(resolve_parameter("composite mode", *mode, "UInt"));
180 }
181 effects.push_back(resolve_effect("Composite", 1, composite));
182
184 effects.push_back(resolve_effect(
185 "Retime and Scaling", 22, {resolve_parameter("scale", int64_t(1), "UInt")}));
186 }
187 return effects;
188}
189
190static std::string color_hex(const float rgb[3])
191{
192 uchar color[3];
194 return fmt::format("#{:02x}{:02x}{:02x}", color[0], color[1], color[2]);
195}
196
197static AnyDictionary title_parameter(const TextVars &data)
198{
199 std::string html = fmt::format(
200 "<span style=\"font-size:{}pt; color:{};\">", data.text_size, color_hex(data.color));
201 for (const char c : StringRef(data.text_ptr ? data.text_ptr : "")) {
202 switch (c) {
203 case '&':
204 html += "&amp;";
205 break;
206 case '<':
207 html += "&lt;";
208 break;
209 case '>':
210 html += "&gt;";
211 break;
212 case '\n':
213 html += "<br>";
214 break;
215 default:
216 html += c;
217 }
218 }
219 html += "</span>";
220
221 AnyDictionary parameter;
222 parameter["Parameter ID"] = std::string("title blob");
223 parameter["Title HTML"] = html;
224 return parameter;
225}
226
227GeneratorReference *resolve_generator_reference(const Strip &strip,
228 const TimeRange &available_range)
229{
230 std::string kind;
231 AnyDictionary effect;
232 if (strip.type == STRIP_TYPE_COLOR) {
233 const SolidColorVars *data = static_cast<const SolidColorVars *>(strip.effectdata);
234 kind = "Solid Color";
235 effect = resolve_effect("Solid Color",
236 5,
237 {resolve_parameter("color", color_hex(data->col), "Color")},
238 "Generator");
239 }
240 else if (strip.type == STRIP_TYPE_TEXT) {
241 const TextVars *data = static_cast<const TextVars *>(strip.effectdata);
242 kind = "Rich";
243 effect = resolve_effect(
244 "Rich Text",
245 24,
248 "position", AnyVector{double(data->loc[0]), double(data->loc[1])}, "POINTF")});
249 }
250 else {
251 return nullptr;
252 }
253
254 AnyDictionary metadata;
255 AnyDictionary generator_metadata;
256 generator_metadata["Generator Type"] = kind;
257 metadata["Resolve_OTIO"] = generator_metadata;
258 AnyDictionary parameters;
259 parameters["Resolve_OTIO"] = AnyVector{effect};
260 return new GeneratorReference(strip.name + 2, kind, available_range, parameters, metadata);
261}
262
263/* Resolve only preserves links between video and audio of the same clip. Other linked
264 * clips (e.g. several color generators) come back unlinked on import, even if Resolve exported
265 * them itself. But still write every connection in case Resolve learns to read them later. */
266Map<const Strip *, int64_t> resolve_link_groups(const ListBaseT<Strip> &seqbase)
267{
268 Map<const Strip *, int64_t> link_groups;
269 int64_t link_group = 0;
270 for (Strip *strip : seq::query_all_strips_recursive(&seqbase)) {
271 if (!seq::is_strip_connected(strip) || link_groups.contains(strip)) {
272 continue;
273 }
274 link_group++;
275 link_groups.add(strip, link_group);
276 for (Strip *connected : seq::connected_strips_get(strip)) {
277 link_groups.add(connected, link_group);
278 }
279 }
280 return link_groups;
281}
282
283void resolve_add_clip_data(const Scene &scene,
284 const Strip &strip,
285 const Map<const Strip *, int64_t> &link_groups,
286 Clip &clip)
287{
288 for (const std::any &effect : strip_effects(scene, strip)) {
289 auto *otio_effect = new otio::Effect("", "Resolve Effect");
290 otio_effect->metadata()["Resolve_OTIO"] = effect;
291 clip.effects().push_back(otio_effect);
292 }
293 if (const int64_t *link_group = link_groups.lookup_ptr(&strip)) {
294 resolve_metadata(clip)["Link Group ID"] = *link_group;
295 }
296}
297
299 const Strip &transition,
300 Transition &otio_transition)
301{
302 if (transition.type != STRIP_TYPE_WIPE) {
303 return;
304 }
305 PointerRNA ptr = strip_pointer(scene, transition);
306 AnyDictionary &resolve = resolve_metadata(otio_transition);
307 resolve["Effects"] = resolve_effect(
308 "Edge Wipe",
309 13,
310 number_parameters(ptr, transition, ResolveUnits(scene, transition), "Edge Wipe"));
311 resolve["Transition Type"] = std::string("Edge Wipe");
312}
313
314void resolve_add_track_data(const SeqTimelineChannel *channel, Track &track)
315{
316 resolve_metadata(track)["Locked"] = bool(channel && channel->is_locked());
317}
318
319void resolve_add_timeline_metadata(Timeline &timeline)
320{
321 resolve_metadata(timeline)["Resolve OTIO Meta Version"] = std::string("1.0");
322}
323
324} // namespace blender::io::otio
#define ELEM(...)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr Span()=default
constexpr StringRef()
const T & data(const Action &owning_action) const
float3x3 scale(float3x3 mat, float2 scale)
static std::string color_hex(const float rgb[3])
void resolve_add_clip_data(const Scene &scene, const Strip &strip, const Map< const Strip *, int64_t > &link_groups, Clip &clip)
static double rna_number(PointerRNA &ptr, PropertyRNA *prop)
static PointerRNA strip_pointer(const Scene &scene, const Strip &strip)
static AnyDictionary title_parameter(const TextVars &data)
AnyDictionary & ensure_dictionary(AnyDictionary &parent, const std::string &key)
GeneratorReference * resolve_generator_reference(const Strip &strip, const TimeRange &available_range)
Span< ResolveNumber > resolve_numbers()
static AnyDictionary resolve_effect(const std::string &effect_name, const int64_t type, const AnyVector &parameters, const std::string &name="")
static AnyDictionary & resolve_metadata(SerializableObjectWithMetadata &object)
static AnyVector number_parameters(PointerRNA &strip_ptr, const Strip &strip, const ResolveUnits &units, const StringRef effect)
static AnyVector strip_effects(const Scene &scene, const Strip &strip)
void resolve_add_transition_data(const Scene &scene, const Strip &transition, Transition &otio_transition)
Map< const Strip *, int64_t > resolve_link_groups(const ListBaseT< Strip > &seqbase)
void resolve_add_track_data(const SeqTimelineChannel *channel, Track &track)
std::optional< int64_t > resolve_composite_mode(const StringRef blend_type)
static AnyDictionary resolve_parameter(const std::string &id, const std::any &value, const std::string &variant_type)
void resolve_add_timeline_metadata(Timeline &timeline)
static AnyDictionary number_parameter(PointerRNA &strip_ptr, const Strip &strip, const std::string &id, const char *rna_path, const FunctionRef< double(double)> convert)
bool is_strip_connected(const Strip *strip)
float2 image_transform_origin_get(const Scene *scene, const Strip *strip)
VectorSet< Strip * > query_all_strips_recursive(const ListBaseT< Strip > *seqbase)
Definition iterator.cc:123
VectorSet< Strip * > connected_strips_get(const Strip *strip)
__int64 int64_t
Definition BKE_text.h:90
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **r_identifier)
FCurve * BKE_fcurve_find_by_rna(PointerRNA *ptr, PropertyRNA *prop, int rnaindex, AnimData **r_adt, bAction **r_action, bool *r_driven, bool *r_special)
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
PropertyType RNA_property_type(PropertyRNA *prop)
VecBase< float, 2 > float2
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
unsigned int totvert
ResolveUnits(const Scene &scene, const Strip &strip)
double to_blender(ResolveUnit unit) const