Blender V5.3
anim_convert.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 "DNA_action_types.h"
10#include "DNA_anim_types.h"
11#include "DNA_object_types.h"
12
13#include "RNA_access.hh"
14#include "RNA_prototypes.hh"
15
17#include "BLI_math_vector_c.hh"
18#include "BLI_set.hh"
19
20#include "BKE_context.hh"
21#include "BKE_fcurve.hh"
22#include "BKE_lib_id.hh"
23
24#include "ANIM_action.hh"
26#include "ANIM_fcurve.hh"
27#include "ANIM_rna.hh"
28
29#include "DEG_depsgraph.hh"
30
31#include "WM_api.hh"
32
33#include "ED_anim_api.hh"
35
36namespace blender {
37
39{
40 int insert_index = 0;
41 for (FCurve *existing_fcurve : fcurves_) {
42 BLI_assert_msg(fcurve.array_index != existing_fcurve->array_index,
43 "An FCurve with that index was already inserted");
44 if (existing_fcurve->array_index > fcurve.array_index) {
45 break;
46 }
47 insert_index++;
48 }
49 fcurves_.insert(insert_index, &fcurve);
50}
51
53{
54 fcurves_.clear();
55}
56
58{
59 return fcurves_;
60}
61
63{
64 for (FCurve *fcurve : fcurves_) {
65 if (fcurve->array_index == array_index) {
66 return fcurve;
67 }
68 }
69 return nullptr;
70}
71
72/* Returns the ranges in which a rotation mode is active. Each entry denotes the starting point of
73 * the range and it ends with the next entry. If the FCurve has any keys, there will be at least
74 * one entry with the starting mode. */
76{
77 BLI_assert(fcurve.rna_path().endswith("rotation_mode"));
78 if (!fcurve.bezt) {
79 return {};
80 }
82 for (const int i : IndexRange(fcurve.totvert)) {
83 const BezTriple &key = fcurve.bezt[i];
84 const eRotationModes key_rotation_mode = eRotationModes(key.vec[1][1]);
85 if (changes.is_empty()) {
86 changes.append({0, key_rotation_mode});
87 continue;
88 }
89 if (changes.last().second == key_rotation_mode) {
90 continue;
91 }
92 changes.append({key.vec[1][0], key_rotation_mode});
93 }
94 return changes;
95}
96
102 Vector<const FCurve *> fcurves_;
103 Array<int> key_indices_;
104 Bounds<float> range_;
105
106 float get_next_frame() const
107 {
108 float next_frame = FLT_MAX;
109 for (const int i : fcurves_.index_range()) {
110 const FCurve *fcurve = fcurves_[i];
111 if (key_indices_[i] > fcurve->totvert - 1) {
112 continue;
113 }
114 const float key_frame = fcurve->bezt[key_indices_[i]].vec[1][0];
115 if (key_frame >= range_.max) {
116 continue;
117 }
118 if (key_frame < next_frame) {
119 next_frame = key_frame;
120 }
121 }
122 return next_frame;
123 }
124
125 public:
130 KeyframeIterator(Span<const FCurve *> fcurves, const Bounds<float> range) : range_(range)
131 {
132 for (const int i : fcurves.index_range()) {
133 if (!fcurves[i] || !fcurves[i]->bezt) {
134 continue;
135 }
136 fcurves_.append(fcurves[i]);
137 }
138
139 key_indices_.reinitialize(fcurves_.size());
140 bool frame_has_key;
141 for (const int i : fcurves_.index_range()) {
142 const FCurve *fcurve = fcurves_[i];
143 const int index = BKE_fcurve_bezt_binarysearch_index(
144 fcurve->bezt, range.min, fcurve->totvert, &frame_has_key);
145 key_indices_[i] = index;
146 }
147 }
148
149 bool can_advance() const
150 {
151 for (const int i : fcurves_.index_range()) {
152 const FCurve *fcurve = fcurves_[i];
153 if (key_indices_[i] > fcurve->totvert - 1) {
154 /* No more keys for that FCurve. */
155 continue;
156 }
157 const float key_frame = fcurve->bezt[key_indices_[i]].vec[1][0];
158 if (key_frame < range_.max) {
159 return true;
160 }
161 }
162 return false;
163 }
164
165 void advance()
166 {
167 float next_frame = get_next_frame();
168
169 if (next_frame == FLT_MAX) {
170 /* No more keys to step in the range. */
171 return;
172 }
173
174 for (const int i : fcurves_.index_range()) {
175 const FCurve *fcurve = fcurves_[i];
176 if (key_indices_[i] > fcurve->totvert - 1) {
177 continue;
178 }
179 const float key_frame = fcurve->bezt[key_indices_[i]].vec[1][0];
180 if (key_frame <= next_frame) {
181 /* Advance all FCurves that have a key at that frame. */
182 key_indices_[i]++;
183 }
184 }
185 }
186
191 float get_frame() const
192 {
193 float next_frame = get_next_frame();
194
195 if (next_frame == FLT_MAX) {
196 /* No more keys to step in the range. */
197 return 0;
198 }
199 return next_frame;
200 }
201
207 {
208 /* Always return some reasonable defaults. */
210 BezTriple *key = nullptr;
211 for (const int i : fcurves_.index_range()) {
212 const FCurve *fcurve = fcurves_[i];
213 if (key_indices_[i] > fcurve->totvert - 1) {
214 continue;
215 }
216 if (key && fcurve->bezt[key_indices_[i]].vec[1][0] >= key->vec[1][0]) {
217 continue;
218 }
219 key = &fcurve->bezt[key_indices_[i]];
220 }
221 if (key) {
222 settings.handle = eBezTriple_Handle(key->h1);
224 settings.keyframe_type = BEZKEYTYPE(key);
225 }
226 return settings;
227 }
228};
229
243static void convert_rotation_mode_range(const Span<const FCurve *> evaluation_buffer,
244 const Span<FCurve *> insertion_buffer,
245 const eRotationModes from_mode,
246 const eRotationModes to_mode,
247 const Bounds<float> range,
248 const ed::AnimTransformable &transformable,
249 const bool ensure_range_start_key)
250{
251 /* Filling the array with the current values to have good base values in case not every array
252 * index is keyed. */
253 ed::Rotation rotation_values = transformable.get_rotation_for_mode(from_mode);
254
255 KeyframeIterator key_iterator = KeyframeIterator(evaluation_buffer, range);
256 /* Generate the current rotation values respecting missing FCurves. */
257 for (const FCurve *fcurve : evaluation_buffer) {
258 if (!fcurve) {
259 continue;
260 }
261 rotation_values.values[fcurve->array_index] = evaluate_fcurve(fcurve, range.min);
262 }
263
264 /* Storing the previous rotation for euler angles larger than 180 degrees. */
265 ed::Rotation previous_conversion = rotation_values.converted_to_mode(to_mode);
266 const animrig::KeyframeSettings settings = key_iterator.get_keyframe_settings();
267
268 if (ensure_range_start_key && key_iterator.get_frame() > range.min) {
269 /* This case can happen if the rotation mode is keyed, but not any of the rotation channels.
270 * In that case the below loop would not insert a key into the range start which would result
271 * in a visual jump after the conversion. */
272 for (const int i : insertion_buffer.index_range()) {
273 FCurve *fcurve = insertion_buffer[i];
274 BLI_assert_msg(fcurve, "For insertion all FCurves are expected to be created before");
276 fcurve, {range.min, previous_conversion.values[i]}, settings, INSERTKEY_FAST);
277 }
278 }
279
280 while (key_iterator.can_advance()) {
281 const float frame = key_iterator.get_frame();
282 const animrig::KeyframeSettings settings = key_iterator.get_keyframe_settings();
283 key_iterator.advance();
284 /* Generate the current rotation values respecting missing FCurves. */
285 for (const FCurve *fcurve : evaluation_buffer) {
286 if (!fcurve) {
287 continue;
288 }
289 rotation_values.values[fcurve->array_index] = evaluate_fcurve(fcurve, frame);
290 }
291 ed::Rotation converted_rotation = rotation_values.converted_to_mode(to_mode,
292 &previous_conversion);
293 for (const int i : insertion_buffer.index_range()) {
294 FCurve *fcurve = insertion_buffer[i];
295 BLI_assert_msg(fcurve, "For insertion all FCurves are expected to be created before");
296 insert_vert_fcurve(fcurve, {frame, converted_rotation.values[i]}, settings, INSERTKEY_FAST);
297 }
298 previous_conversion = std::move(converted_rotation);
299 }
300}
301
302static void remove_rotation_fcurves(const ed::AnimTransformable &transformable,
303 RNAFCurveMap &fcu_map,
304 animrig::Channelbag &channelbag,
305 const eRotationModes rotation_mode)
306{
307 std::string rna_path = transformable.rna_path_to_rotation(rotation_mode);
308 SortedFCurveBuffer *rotation_fcurves = fcu_map.lookup_ptr(rna_path);
309 if (!rotation_fcurves) {
310 return;
311 }
312 /* Remove the FCurves that target the now no longer used rotation modes. */
313 for (FCurve *fcurve : rotation_fcurves->fcurves()) {
314 channelbag.fcurve_remove(*fcurve);
315 }
316 rotation_fcurves->clear();
317 fcu_map.remove(rna_path);
318}
319
321 RNAFCurveMap &fcu_map,
322 const eRotationModes to_mode,
323 const Span<std::pair<float, eRotationModes>> ranges,
324 const ed::AnimTransformable &transformable)
325{
326 const int insertion_buffer_count = to_mode > ROT_MODE_QUAT ? 3 : 4;
327 Array<FCurve *> insertion_buffer(insertion_buffer_count);
328
329 for (const int i : insertion_buffer.index_range()) {
330 /* Is needed to get correct FCurve colors. */
331 PropertySubType prop_subtype = PROP_EULER;
332 if (to_mode == ROT_MODE_QUAT) {
333 prop_subtype = PROP_QUATERNION;
334 }
335 else if (to_mode == ROT_MODE_AXISANGLE) {
336 prop_subtype = PROP_AXISANGLE;
337 }
339 {transformable.rna_path_to_rotation(to_mode),
340 i,
342 prop_subtype,
343 transformable.fcurve_group_name()});
344 insertion_buffer[i] = fcurve;
345 }
346
347 bool modified_keys = false;
348 for (const int i : ranges.index_range()) {
349 const std::pair<float, eRotationModes> &rotation_mode_range = ranges[i];
350 const eRotationModes from_mode = rotation_mode_range.second;
351
352 const std::string from_mode_rna_path = transformable.rna_path_to_rotation(from_mode);
353 const SortedFCurveBuffer *rotation_fcurves = fcu_map.lookup_ptr(from_mode_rna_path);
354 if (!rotation_fcurves) {
355 continue;
356 }
357
358 const int evaluation_buffer_count = from_mode > ROT_MODE_QUAT ? 3 : 4;
359 Array<FCurve *> evaluation_buffer(evaluation_buffer_count);
360 for (const int buffer_index : evaluation_buffer.index_range()) {
361 evaluation_buffer[buffer_index] = rotation_fcurves->get_fcurve_by_array_index(buffer_index);
362 }
363
364 Bounds<float> range(rotation_mode_range.first, FLT_MAX);
365 if (i + 1 < ranges.size()) {
366 range.max = ranges[i + 1].first;
367 }
368
370 evaluation_buffer, insertion_buffer, from_mode, to_mode, range, transformable, i > 0);
371
372 modified_keys = true;
373 }
374
375 if (!modified_keys) {
376 /* There were no rotation FCurves to read from. In that case don't insert the
377 * `insertion_buffer` FCurves into the channelbag. */
378 for (FCurve *fcurve : insertion_buffer) {
379 BKE_fcurve_free(fcurve);
380 }
381 return false;
382 }
383
384 /* Remove all old rotation FCurves. */
385 remove_rotation_fcurves(transformable, fcu_map, channelbag, ROT_MODE_QUAT);
386 remove_rotation_fcurves(transformable, fcu_map, channelbag, ROT_MODE_EUL);
387 remove_rotation_fcurves(transformable, fcu_map, channelbag, ROT_MODE_AXISANGLE);
388
389 for (FCurve *fcurve : insertion_buffer) {
390 channelbag.fcurve_append(*fcurve);
391 bActionGroup &grp = channelbag.channel_group_ensure(transformable.fcurve_group_name());
392 channelbag.fcurve_assign_to_channel_group(*fcurve, grp);
394 }
395 return true;
396}
397
399 ChannelbagFCurveMap &channelbag_fcurve_map,
400 const eRotationModes to_mode)
401{
402 bool modified_keys = false;
403
404 for (const auto &item : channelbag_fcurve_map.items()) {
405 animrig::Channelbag *channelbag = item.key;
406 RNAFCurveMap &fcu_map = item.value;
407 const std::string rotation_mode_path = transformable.rna_path_to_rotation_mode();
408 Vector<std::pair<float, eRotationModes>> rotation_mode_ranges;
409 FCurve *rotation_mode_fcurve = nullptr;
410 if (const SortedFCurveBuffer *rotation_mode_buffer = fcu_map.lookup_ptr(rotation_mode_path)) {
411 BLI_assert(rotation_mode_buffer->fcurves().size() == 1);
412 rotation_mode_fcurve = rotation_mode_buffer->fcurves()[0];
413 rotation_mode_ranges = get_rotation_mode_ranges(*rotation_mode_fcurve);
414 }
415 else {
416 /* Defaulting back to the struct value means that this can have unexpected results when
417 * dealing with action layers. The rotation mode can still be animated by a higher layer but
418 * that means we cannot know the correct rotation mode for the current layer. */
419 rotation_mode_ranges = {{0, transformable.get_rotation_mode()}};
420 }
421
422 modified_keys |= convert_rotation_mode_channelbag(
423 *channelbag, fcu_map, to_mode, rotation_mode_ranges, transformable);
424
425 if (rotation_mode_fcurve && rotation_mode_fcurve->bezt) {
426 for (const int i : IndexRange(rotation_mode_fcurve->totvert)) {
427 rotation_mode_fcurve->bezt[i].vec[1][1] = to_mode;
428 }
429 BKE_fcurve_handles_recalc(*rotation_mode_fcurve);
430 }
431 }
432
433 return modified_keys;
434}
435
437{
438 const int start_of_propname = rna_path.rfind(".") + 1;
439 return rna_path.substr(start_of_propname, rna_path.size()) == "rotation_mode";
440}
441
443 const animrig::slot_handle_t slot_handle)
444{
445 ChannelbagFCurveMap rotation_map;
446 for (animrig::Channelbag *channelbag : channelbags_for_action_slot(action, slot_handle)) {
447 RNAFCurveMap &curves = rotation_map.lookup_or_add(channelbag, {});
448 for (FCurve *fcurve : channelbag->fcurves()) {
449 StringRefNull rna_path = fcurve->rna_path();
450 if (!animrig::is_rotation_path(fcurve->rna_path_parsed()) &&
452 {
453 continue;
454 }
455 SortedFCurveBuffer &fcurve_buffer = curves.lookup_or_add(rna_path, {});
456 fcurve_buffer.insert_fcurve(*fcurve);
457 }
458 }
459 return rotation_map;
460}
461
462void bake_rotation_fcurves(const ChannelbagFCurveMap &channelbag_fcurve_map,
463 const ed::AnimTransformable &transformable)
464{
465 /* Need to bake on all potential FCurves to cover for an animated rotation mode. */
467 for (const eRotationModes rotation_mode : rotation_modes) {
468 std::string rotation_rna_path = transformable.rna_path_to_rotation(rotation_mode);
469
470 for (const RNAFCurveMap &rna_fcurve_map : channelbag_fcurve_map.values()) {
471 const SortedFCurveBuffer *fcurve_buffer = rna_fcurve_map.lookup_ptr(rotation_rna_path);
472 if (!fcurve_buffer) {
473 continue;
474 }
475 for (FCurve *fcurve : fcurve_buffer->fcurves()) {
476 if (!fcurve || !fcurve->bezt) {
477 continue;
478 }
479 const int2 range = {int(fcurve->bezt[0].vec[1][0]),
480 int(fcurve->bezt[fcurve->totvert - 1].vec[1][0])};
482 }
483 }
484 }
485}
486
488 ed::AnimTransformable &transformable,
489 const eRotationModes to_mode,
490 const bool bake)
491{
492 Main *bmain = CTX_data_main(&C);
493 if (!BKE_id_is_editable(bmain, transformable.owner_id())) {
494 return;
495 }
496 /* A map built per action to make it quicker to find the FCurves by RNA path. */
498
499 bool converted_actions = false;
501 *transformable.owner_id(),
502 [&](animrig::Action &action, const animrig::slot_handle_t slot_handle) {
503 if (!BKE_id_is_editable(bmain, &action.id)) {
504 return true;
505 }
506 if (!data_map.contains({&action, slot_handle})) {
507 ChannelbagFCurveMap fcurve_map = build_rotation_fcurve_map(action, slot_handle);
508 data_map.add({&action, slot_handle}, fcurve_map);
509 }
510 ChannelbagFCurveMap &channelbag_fcurve_map = data_map.lookup({&action, slot_handle});
511 if (bake) {
512 bake_rotation_fcurves(channelbag_fcurve_map, transformable);
513 }
514 converted_actions |= convert_rotation_keys(transformable, channelbag_fcurve_map, to_mode);
515 DEG_id_tag_update(&action.id, ID_RECALC_ANIMATION);
516 return true;
517 });
518
519 if (converted_actions) {
520 transformable.set_rotation_mode(to_mode);
521 ID *id = transformable.owner_id();
522 DEG_id_tag_update(id, ID_RECALC_GEOMETRY);
524 }
525 else {
526 ed::Rotation rotation = transformable.get_rotation();
527 transformable.set_rotation_mode(to_mode);
528 transformable.set_rotation(rotation.converted_to_mode(to_mode));
529 }
530}
531
532} // namespace blender
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
Functions to modify FCurves.
Helper functions for animation to interact with the RNA system.
#define BEZKEYTYPE(bezt)
Object is a sort of wrapper for general info.
Defines an abstraction around various structs to modify their transform properties via a unified API ...
#define C
Definition RandGen.cpp:29
#define NC_ANIMATION
Definition WM_types.hh:401
#define NA_ADDED
Definition WM_types.hh:636
#define ND_KEYFRAME
Definition WM_types.hh:537
#define BLI_assert_msg(a, msg)
IndexRange index_range() const
Definition BLI_array.hh:371
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange()=default
animrig::KeyframeSettings get_keyframe_settings() const
KeyframeIterator(Span< const FCurve * > fcurves, const Bounds< float > range)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
ValueIterator values() const &
Definition BLI_map.hh:884
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool remove(const Key &key)
Definition BLI_map.hh:368
bool contains(const Key &key) const
Definition BLI_map.hh:353
ItemIterator items() const &
Definition BLI_map.hh:902
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
void insert_fcurve(FCurve &fcurve)
Span< FCurve * > fcurves() const
FCurve * get_fcurve_by_array_index(int array_index) const
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
IndexRange index_range() const
bool fcurve_assign_to_channel_group(FCurve &fcurve, bActionGroup &to_group)
bool fcurve_remove(FCurve &fcurve_to_remove)
bActionGroup & channel_group_ensure(StringRefNull name)
Rotation get_rotation_for_mode(eRotationModes mode) const
std::string rna_path_to_rotation(eRotationModes rotation_mode) const
eRotationModes get_rotation_mode() const
std::string rna_path_to_rotation_mode() const
#define FLT_MAX
decltype(blender::ActionSlot::handle) slot_handle_t
bool foreach_action_slot_use(const ID &animated_id, FunctionRef< bool(Action &action, slot_handle_t slot_handle)> callback)
FCurve * create_fcurve_for_channel(const FCurveDescriptor &fcurve_descriptor)
bool is_rotation_path(ParsedRNAPathRef rna_path)
Definition anim_rna.cc:165
Vector< animrig::Channelbag * > channelbags_for_action_slot(Action &action, slot_handle_t slot_handle)
void bake_fcurve(FCurve *fcu, int2 range, float step, BakeCurveRemove remove_existing)
SingleKeyingResult insert_vert_fcurve(FCurve *fcu, const float2 position, const KeyframeSettings &settings, eInsertKeyFlags flag)
Main Key-framing API call.
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ BEZT_KEYTYPE_KEYFRAME
static void convert_rotation_mode_range(const Span< const FCurve * > evaluation_buffer, const Span< FCurve * > insertion_buffer, const eRotationModes from_mode, const eRotationModes to_mode, const Bounds< float > range, const ed::AnimTransformable &transformable, const bool ensure_range_start_key)
static void remove_rotation_fcurves(const ed::AnimTransformable &transformable, RNAFCurveMap &fcu_map, animrig::Channelbag &channelbag, const eRotationModes rotation_mode)
void convert_to_rotation_mode(bContext &C, ed::AnimTransformable &transformable, const eRotationModes to_mode, const bool bake)
ChannelbagFCurveMap build_rotation_fcurve_map(animrig::Action &action, const animrig::slot_handle_t slot_handle)
VecBase< int32_t, 2 > int2
void BKE_fcurve_handles_recalc(FCurve &fcu)
void DEG_id_tag_update(ID *id, unsigned int flags)
bool convert_rotation_keys(const ed::AnimTransformable &transformable, ChannelbagFCurveMap &channelbag_fcurve_map, const eRotationModes to_mode)
Map< std::string, SortedFCurveBuffer > RNAFCurveMap
static Vector< std::pair< float, eRotationModes > > get_rotation_mode_ranges(const FCurve &fcurve)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
Map< animrig::Channelbag *, RNAFCurveMap > ChannelbagFCurveMap
void BKE_fcurve_free(FCurve *fcu)
void bake_rotation_fcurves(const ChannelbagFCurveMap &channelbag_fcurve_map, const ed::AnimTransformable &transformable)
Main * CTX_data_main(const bContext *C)
@ PROP_QUATERNION
Definition RNA_types.hh:324
@ PROP_AXISANGLE
Definition RNA_types.hh:325
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
static bool is_rotation_mode_path(const StringRefNull rna_path)
static bool convert_rotation_mode_channelbag(animrig::Channelbag &channelbag, RNAFCurveMap &fcu_map, const eRotationModes to_mode, const Span< std::pair< float, eRotationModes > > ranges, const ed::AnimTransformable &transformable)
float evaluate_fcurve(const FCurve *fcu, float evaltime)
eBezTriple_Interpolation ipo
eBezTriple_Handle h1
unsigned int totvert
eBezTriple_Interpolation interpolation
eBezTriple_KeyframeType keyframe_type
Rotation converted_to_mode(eRotationModes mode, const Rotation *reference_euler=nullptr) const