Blender V5.3
animrig/intern/action.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 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_array_utils.hh"
12#include "DNA_scene_types.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_map.hh"
16#include "BLI_math_base_c.hh"
17#include "BLI_string.hh"
18#include "BLI_string_utf8.hh"
19#include "BLI_string_utils.hh"
20#include "BLI_utildefines.hh"
21
22#include "BKE_action.hh"
23#include "BKE_anim_data.hh"
24#include "BKE_fcurve.hh"
25#include "BKE_lib_id.hh"
26#include "BKE_library.hh"
27#include "BKE_main.hh"
28#include "BKE_nla.hh"
29#include "BKE_report.hh"
30
31#include "RNA_access.hh"
32#include "RNA_path.hh"
33#include "RNA_prototypes.hh"
34
35#include "MEM_guardedalloc.h"
36
37#include "BLT_translation.hh"
38
39#include "DEG_depsgraph.hh"
41
42#include "ANIM_action.hh"
44#include "ANIM_action_legacy.hh"
45#include "ANIM_animdata.hh"
46#include "ANIM_fcurve.hh"
47
48#include "CLG_log.h"
49
50#include "action_runtime.hh"
51
52#include <cstdio>
53#include <cstring>
54
55namespace blender {
56
57static CLG_LogRef LOG = {"anim.action"};
58
59namespace animrig {
60
61namespace {
69constexpr const char *slot_default_name = "Slot";
70
74constexpr const char *slot_untyped_prefix = "XX";
75
76constexpr const char *layer_default_name = "Layer";
77
78} // namespace
79
81{
82 ActionLayer *layer = MEM_new<ActionLayer>(__func__);
83 return layer->wrap();
84}
85
86/* Copied from source/blender/blenkernel/intern/grease_pencil.cc.
87 * Keep an eye on DNA_array_utils.hh; we may want to move these functions in there. */
88template<typename T> static void grow_array(T **array, int *num, const int add_num)
89{
90 BLI_assert(add_num > 0);
91 const int new_array_num = *num + add_num;
92 T *new_array = MEM_new_array_zeroed<T>(new_array_num, "animrig::action/grow_array");
93
94 uninitialized_relocate_n(*array, *num, new_array);
95 MEM_SAFE_DELETE(*array);
96
97 *array = new_array;
98 *num = new_array_num;
99}
100
101template<typename T> static void grow_array_and_append(T **array, int *num, T item)
102{
103 grow_array(array, num, 1);
104 (*array)[*num - 1] = item;
105}
106
107template<typename T>
108static void grow_array_and_insert(T **array, int *num, const int index, T item)
109{
110 BLI_assert(index >= 0 && index <= *num);
111 const int new_array_num = *num + 1;
112 T *new_array = MEM_new_array_zeroed<T>(new_array_num, __func__);
113
115 new_array[index] = item;
116 uninitialized_relocate_n(*array + index, *num - index, new_array + index + 1);
117
118 MEM_SAFE_DELETE(*array);
119
120 *array = new_array;
121 *num = new_array_num;
122}
123
124template<typename T> static void shrink_array(T **array, int *num, const int shrink_num)
125{
126 BLI_assert(shrink_num > 0);
127 const int new_array_num = *num - shrink_num;
128 if (new_array_num == 0) {
129 MEM_delete(*array);
130 *array = nullptr;
131 *num = 0;
132 return;
133 }
134
135 T *new_array = MEM_new_array_zeroed<T>(new_array_num, __func__);
136
137 uninitialized_move_n(*array, new_array_num, new_array);
138 MEM_delete(*array);
139
140 *array = new_array;
141 *num = new_array_num;
142}
143
144template<typename T> static void shrink_array_and_remove(T **array, int *num, const int index)
145{
146 BLI_assert(index >= 0 && index < *num);
147 const int new_array_num = *num - 1;
148 T *new_array = MEM_new_array_zeroed<T>(new_array_num, __func__);
149
150 uninitialized_move_n(*array, index, new_array);
151 uninitialized_move_n(*array + index + 1, *num - index - 1, new_array + index);
152 MEM_delete(*array);
153
154 *array = new_array;
155 *num = new_array_num;
156}
157
163template<typename T> static void shrink_array_and_swap_remove(T **array, int *num, const int index)
164{
165 BLI_assert(index >= 0 && index < *num);
166 const int new_array_num = *num - 1;
167 T *new_array = MEM_new_array_zeroed<T>(new_array_num, __func__);
168
169 uninitialized_move_n(*array, index, new_array);
170 if (index < new_array_num) {
171 new_array[index] = (*array)[new_array_num];
172 uninitialized_move_n(*array + index + 1, *num - index - 2, new_array + index + 1);
173 }
174 MEM_delete(*array);
175
176 *array = new_array;
177 *num = new_array_num;
178}
179
189template<typename T>
191 T *array, const int num, const int range_start, const int range_end, const int to)
192{
193 BLI_assert(range_start <= range_end);
194 BLI_assert(range_end <= num);
195 BLI_assert(to <= num + range_start - range_end);
197
198 if (ELEM(range_start, range_end, to)) {
199 return;
200 }
201
202 if (to < range_start) {
203 T *start = array + to;
204 T *mid = array + range_start;
205 T *end = array + range_end;
206 std::rotate(start, mid, end);
207 }
208 else {
209 T *start = array + range_start;
210 T *mid = array + range_end;
211 T *end = array + to + range_end - range_start;
212 std::rotate(start, mid, end);
213 }
214}
215
216/* ----- Action implementation ----------- */
217
219{
220 /* The check for emptiness has to include the check for an empty `groups` ListBaseT because of
221 * the animation filtering code. With the functions `rearrange_action_channels` and
222 * `join_groups_action_temp` the ownership of FCurves is temporarily transferred to the `groups`
223 * ListBaseT leaving `curves` potentially empty. */
224 return this->layer_array_num == 0 && this->slot_array_num == 0 && this->curves.is_empty() &&
225 this->groups.is_empty();
226}
227
229{
230 return Span<const Layer *>{reinterpret_cast<Layer **>(this->layer_array), this->layer_array_num};
231}
233{
234 return Span<Layer *>{reinterpret_cast<Layer **>(this->layer_array), this->layer_array_num};
235}
236const Layer *Action::layer(const int64_t index) const
237{
238 return &this->layer_array[index]->wrap();
239}
241{
242 return &this->layer_array[index]->wrap();
243}
244
245Layer &Action::layer_add(const std::optional<StringRefNull> name)
246{
247 Layer &new_layer = ActionLayer_alloc();
248 if (name.has_value()) {
249 STRNCPY_UTF8(new_layer.name, name.value().c_str());
250 }
251 else {
252 STRNCPY_UTF8(new_layer.name, DATA_(layer_default_name));
253 }
254
256 &this->layer_array, &this->layer_array_num, &new_layer);
257 this->layer_active_index = this->layer_array_num - 1;
258
259 /* If this is the first layer in this Action, it means that it could have been
260 * used as a legacy Action before. As a result, this->idroot may be non-zero
261 * while it should be zero for layered Actions.
262 *
263 * And since setting this to 0 when it is already supposed to be 0 is fine,
264 * there is no check for whether this is actually the first layer. */
265 this->idroot = 0;
266
267 return new_layer;
268}
269
270static void layer_ptr_destructor(ActionLayer **dna_layer_ptr)
271{
272 Layer &layer = (*dna_layer_ptr)->wrap();
273 MEM_delete(&layer);
274};
275
276bool Action::layer_remove(Layer &layer_to_remove)
277{
278 const int64_t layer_index = this->find_layer_index(layer_to_remove);
279 if (layer_index < 0) {
280 return false;
281 }
282
284 &this->layer_array_num,
285 &this->layer_active_index,
286 layer_index,
288 return true;
289}
290
292{
293 /* Ensure a layer. */
294 Layer *layer;
295 if (this->layers().is_empty()) {
296 layer = &this->layer_add(DATA_(layer_default_name));
297 }
298 else {
299 layer = this->layer(0);
300 }
301
302 /* Ensure a keyframe Strip. */
303 if (layer->strips().is_empty()) {
304 layer->strip_add(*this, Strip::Type::Keyframe);
305 }
306
307 /* Within the limits of Baklava Phase 1, the above code should not have
308 * created more than one layer, or more than one strip on the layer. And if a
309 * layer + strip already existed, that must have been a keyframe strip. */
311}
312
314{
315 for (const int64_t layer_index : this->layers().index_range()) {
316 const Layer *visit_layer = this->layer(layer_index);
317 if (visit_layer == &layer) {
318 return layer_index;
319 }
320 }
321 return -1;
322}
323
325{
326 for (const int64_t slot_index : this->slots().index_range()) {
327 const Slot *visit_slot = this->slot(slot_index);
328 if (visit_slot == &slot) {
329 return slot_index;
330 }
331 }
332 return -1;
333}
334
336{
337 return Span<Slot *>{reinterpret_cast<Slot **>(this->slot_array), this->slot_array_num};
338}
340{
341 return Span<Slot *>{reinterpret_cast<Slot **>(this->slot_array), this->slot_array_num};
342}
343const Slot *Action::slot(const int64_t index) const
344{
345 return &this->slot_array[index]->wrap();
346}
348{
349 return &this->slot_array[index]->wrap();
350}
351
353{
354 const Slot *slot = const_cast<const Action *>(this)->slot_for_handle(handle);
355 return const_cast<Slot *>(slot);
356}
357
359{
360 if (handle == Slot::unassigned) {
361 return nullptr;
362 }
363
364 /* TODO: implement hash-map lookup. */
365 for (const Slot *slot : slots()) {
366 if (slot->handle == handle) {
367 return slot;
368 }
369 }
370 return nullptr;
371}
372
373static void slot_identifier_ensure_unique(Action &action, Slot &slot)
374{
375 auto check_name_is_used = [&](const StringRef name) -> bool {
376 for (const Slot *slot_iter : action.slots()) {
377 if (slot_iter == &slot) {
378 /* Don't compare against the slot that's being renamed. */
379 continue;
380 }
381 if (slot_iter->identifier == name) {
382 return true;
383 }
384 }
385 return false;
386 };
387
388 BLI_uniquename_cb(check_name_is_used, "", '.', slot.identifier, sizeof(slot.identifier));
389}
390
392{
393 this->slot_display_name_define(slot, new_display_name);
394 this->slot_identifier_propagate(bmain, slot);
395}
396
398{
399 BLI_assert_msg(StringRef(new_display_name).size() >= 1,
400 "Action Slot display names must not be empty");
402 "Action Slot's existing identifier lacks the two-character type prefix, which "
403 "would make the display name copy meaningless due to early null termination.");
404
405 BLI_strncpy_utf8(slot.identifier + 2, new_display_name.c_str(), ARRAY_SIZE(slot.identifier) - 2);
407}
408
415
416void Action::slot_identifier_set(Main &bmain, Slot &slot, const StringRefNull new_identifier)
417{
418 /* TODO: maybe this function should only set the 'identifier without prefix' aka the 'display
419 * name'. That way only `this->id_type` is responsible for the prefix. I (Sybren) think that's
420 * easier to determine when the code is a bit more mature, and we can see what the majority of
421 * the calls to this function actually do/need. */
422
423 this->slot_identifier_define(slot, new_identifier);
424 this->slot_identifier_propagate(bmain, slot);
425}
426
428{
430 StringRef(new_identifier).size() >= Slot::identifier_length_min,
431 "Action Slot identifiers must be large enough for a 2-letter ID code + the display name");
432 STRNCPY_UTF8(slot.identifier, new_identifier.c_str());
434}
435
437{
438 /* Just loop over all animatable IDs in the main database. */
439 ListBaseT<ID> *lb;
440 ID *id;
441 FOREACH_MAIN_LISTBASE_BEGIN (&bmain, lb) {
443 if (!id_can_have_animdata(id)) {
444 /* This ID type cannot have any animation, so ignore all and continue to
445 * the next ID type. */
446 break;
447 }
448
450 if (!adt || adt->action != this) {
451 /* Not animated by this Action. */
452 continue;
453 }
454 if (adt->slot_handle != slot.handle) {
455 /* Not animated by this Slot. */
456 continue;
457 }
458
459 /* Ensure the Slot identifier on the AnimData is correct. */
461 }
463 }
465}
466
468{
469 for (Slot *slot : slots()) {
470 if (STREQ(slot->identifier, slot_identifier.c_str())) {
471 return slot;
472 }
473 }
474 return nullptr;
475}
476
477Slot &Action::slot_allocate()
478{
479 Slot &slot = *MEM_new<Slot>(__func__);
480 this->last_slot_handle++;
481 BLI_assert_msg(this->last_slot_handle > 0, "Action Slot handle overflow");
482 slot.handle = this->last_slot_handle;
483
484 /* Set the default flags. These cannot be set via the 'DNA defaults' system,
485 * as that would require knowing which bit corresponds with which flag. That's
486 * only known to the C++ wrapper code. */
487 slot.set_expanded(true);
488 return slot;
489}
490
492{
493 Slot &slot = this->slot_allocate();
494
495 /* Assign the default name and the 'untyped' identifier prefix. */
496 STRNCPY_UTF8(slot.identifier, slot_untyped_prefix);
497 BLI_strncpy_utf8(slot.identifier + 2, DATA_(slot_default_name), ARRAY_SIZE(slot.identifier) - 2);
498
499 /* Append the Slot to the Action. */
501
503
504 /* If this is the first slot in this Action, it means that it could have
505 * been used as a legacy Action before. As a result, this->idroot may be
506 * non-zero while it should be zero for layered Actions.
507 *
508 * And since setting this to 0 when it is already supposed to be 0 is fine,
509 * there is no check for whether this is actually the first layer. */
510 this->idroot = 0;
511
512 return slot;
513}
514
516{
517 Slot &slot = this->slot_add();
518
519 slot.idtype = idtype;
521 BLI_strncpy_utf8(slot.identifier + 2, DATA_(slot_default_name), ARRAY_SIZE(slot.identifier) - 2);
523
524 /* No need to call anim.slot_identifier_propagate() as nothing will be using
525 * this brand new Slot yet. */
526
527 return slot;
528}
529
530Slot &Action::slot_add_for_id(const ID &animated_id)
531{
532 Slot &slot = this->slot_add();
533 slot.idtype = animated_id.id_type();
534
535 /* Determine the identifier for this slot, prioritizing transparent
536 * auto-selection when toggling between Actions. That's why the last-used slot
537 * identifier is used here, and the ID name only as fallback. */
538 const AnimData *adt = BKE_animdata_from_id(&animated_id);
539 const StringRefNull last_slot_identifier = adt ? adt->last_slot_identifier : "";
540
541 StringRefNull slot_identifier = last_slot_identifier;
542 if (slot_identifier.is_empty()) {
543 slot_identifier = animated_id.name;
544 }
545
546 this->slot_identifier_define(slot, slot_identifier);
547 /* No need to call anim.slot_identifier_propagate() as nothing will be using
548 * this brand new Slot yet. */
549
550 /* The last-used slot might have had a different ID type through some quirk (changes to linked
551 * data, for example). So better ensure that the identifier prefix is correct on this new slot,
552 * instead of relying for 100% on the old one. */
554
555 return slot;
556}
557
558static void slot_ptr_destructor(ActionSlot **dna_slot_ptr)
559{
560 Slot &slot = (*dna_slot_ptr)->wrap();
561 MEM_delete(&slot);
562};
563
564bool Action::slot_remove(Slot &slot_to_remove)
565{
566 /* Check that this slot belongs to this Action. */
567 const int64_t slot_index = this->find_slot_index(slot_to_remove);
568 if (slot_index < 0) {
569 return false;
570 }
571
572 /* Remove the slot's data from each keyframe strip. */
573 for (StripKeyframeData *strip_data : this->strip_keyframe_data()) {
574 strip_data->slot_data_remove(slot_to_remove.handle);
575 }
576
577 /* Don't bother un-assigning this slot from its users. The slot handle will
578 * not be reused by a new slot anyway. */
579
580 /* Remove the actual slot. */
582 &this->slot_array, &this->slot_array_num, nullptr, slot_index, slot_ptr_destructor);
583 return true;
584}
585
586void Action::slot_move_to_index(Slot &slot, const int to_slot_index)
587{
588 BLI_assert(this->slots().index_range().contains(to_slot_index));
589
590 const int from_slot_index = this->slots().first_index_try(&slot);
591 BLI_assert_msg(from_slot_index >= 0, "Slot not in this action.");
592
594 this->slot_array, this->slot_array_num, from_slot_index, from_slot_index + 1, to_slot_index);
595}
596
598{
599 for (Slot *slot : slots()) {
600 slot->set_active(slot->handle == slot_handle);
601 }
602}
603
605{
606 for (Slot *slot : slots()) {
607 if (slot->is_active()) {
608 return slot;
609 }
610 }
611 return nullptr;
612}
613
614bool Action::is_slot_animated(const slot_handle_t slot_handle) const
615{
616 if (slot_handle == Slot::unassigned) {
617 return false;
618 }
619
620 Span<const FCurve *> fcurves = fcurves_for_action_slot(*this, slot_handle);
621 return !fcurves.is_empty();
622}
623
633
635{
636 BLI_assert(index >= 0 && index < this->strip_keyframe_data_array_num);
637
638 /* Make sure the data isn't being used anywhere. */
639 for (const Layer *layer : this->layers()) {
640 for (const Strip *strip : layer->strips()) {
641 if (strip->type() == Strip::Type::Keyframe && strip->data_index == index) {
642 return;
643 }
644 }
645 }
646
647 /* Free the item to be removed. */
648 MEM_delete<StripKeyframeData>(
649 static_cast<StripKeyframeData *>(this->strip_keyframe_data_array[index]));
650
651 /* Remove the item, swapping in the item at the end of the array. */
653 &this->strip_keyframe_data_array, &this->strip_keyframe_data_array_num, index);
654
655 /* Update strips that pointed at the swapped-in item.
656 *
657 * Note that we don't special-case the corner-case where the removed data was
658 * at the end of the array, but it ends up not mattering because then
659 * `old_index == index`. */
660 const int old_index = this->strip_keyframe_data_array_num;
661 for (Layer *layer : this->layers()) {
662 for (Strip *strip : layer->strips()) {
663 if (strip->type() == Strip::Type::Keyframe && strip->data_index == old_index) {
664 strip->data_index = index;
665 }
666 }
667 }
668}
669
671{
672 /* The reinterpret cast is needed because `strip_keyframe_data_array` is for
673 * pointers to the C type `ActionStripKeyframeData`, but we want the C++
674 * wrapper type `StripKeyframeData`. */
676 reinterpret_cast<StripKeyframeData **>(this->strip_keyframe_data_array),
678}
680{
681 /* The reinterpret cast is needed because `strip_keyframe_data_array` is for
682 * pointers to the C type `ActionStripKeyframeData`, but we want the C++
683 * wrapper type `StripKeyframeData`. */
685 reinterpret_cast<StripKeyframeData **>(this->strip_keyframe_data_array),
687}
688
690{
692
693 if (this->layers().is_empty()) {
694 return nullptr;
695 }
696
697 return this->layer(0);
698}
699
700void Action::slot_identifier_ensure_prefix(Slot &slot)
701{
704}
705
706void Action::slot_setup_for_id(Slot &slot, const ID &animated_id)
707{
708 if (!ID_IS_EDITABLE(this) || ID_IS_OVERRIDE_LIBRARY(this)) {
709 /* Do not write to linked data. For now, also avoid changing the slot identifier on an
710 * override. Actions cannot have library overrides at the moment, and when they do, this should
711 * actually get designed. For now, it's better to avoid editing data than editing too much. */
712 return;
713 }
714
715 if (slot.has_idtype()) {
716 BLI_assert(slot.idtype == GS(animated_id.name));
717 return;
718 }
719
720 slot.idtype = animated_id.id_type();
721 this->slot_identifier_ensure_prefix(slot);
722}
723
724bool Action::has_keyframes(const slot_handle_t action_slot_handle) const
725{
726 for (const FCurve *fcu : fcurves_for_action_slot(*this, action_slot_handle)) {
727 if (fcu->totvert) {
728 return true;
729 }
730 }
731 return false;
732}
733
735{
736 bool found_key = false;
737 float found_key_frame = 0.0f;
738
739 for (const FCurve *fcu : legacy::fcurves_all(this)) {
740 switch (fcu->totvert) {
741 case 0:
742 /* No keys, so impossible to come to a conclusion on this curve alone. */
743 continue;
744 case 1:
745 /* Single key, which is the complex case, so handle below. */
746 break;
747 default:
748 /* Multiple keys, so there is animation. */
749 return false;
750 }
751
752 const float this_key_frame = fcu->bezt != nullptr ? fcu->bezt[0].vec[1][0] :
753 fcu->fpt[0].vec[0];
754 if (!found_key) {
755 found_key = true;
756 found_key_frame = this_key_frame;
757 continue;
758 }
759
760 /* The graph editor rounds to 1/1000th of a frame, so it's not necessary to be really precise
761 * with these comparisons. */
762 if (!compare_ff(found_key_frame, this_key_frame, 0.001f)) {
763 /* This key differs from the already-found key, so this Action represents animation. */
764 return false;
765 }
766 }
767
768 /* There is only a single frame if we found at least one key. */
769 return found_key;
770}
771
773{
774 return (this->flag & ACT_FRAME_RANGE) && (this->flag & ACT_CYCLIC);
775}
776
778static float2 get_frame_range_of_fcurves(Span<const FCurve *> fcurves, bool include_modifiers);
779
781{
782 if (this->flag & ACT_FRAME_RANGE) {
783 return {this->frame_start, this->frame_end};
784 }
785
787 return get_frame_range_of_fcurves(all_fcurves, false);
788}
789
791{
792 if (this->flag & ACT_FRAME_RANGE) {
793 return {this->frame_start, this->frame_end};
794 }
795
796 Span<const FCurve *> fcurves_to_consider = fcurves_for_action_slot(*this, slot_handle);
797 return get_frame_range_of_fcurves(fcurves_to_consider, false);
798}
799
800float2 Action::get_frame_range_of_keys(const bool include_modifiers) const
801{
802 return get_frame_range_of_fcurves(legacy::fcurves_all(this), include_modifiers);
803}
804
806 const bool include_modifiers)
807{
808 float min = 999999999.0f, max = -999999999.0f;
809 bool foundvert = false, foundmod = false;
810
811 for (const FCurve *fcu : fcurves) {
812 /* if curve has keyframes, consider them first */
813 if (fcu->totvert) {
814 float nmin, nmax;
815
816 /* get extents for this curve
817 * - no "selected only", since this is often used in the backend
818 * - no "minimum length" (we will apply this later), otherwise
819 * single-keyframe curves will increase the overall length by
820 * a phantom frame (#50354)
821 */
822 BKE_fcurve_calc_range(fcu, &nmin, &nmax, false);
823
824 /* compare to the running tally */
825 min = min_ff(min, nmin);
826 max = max_ff(max, nmax);
827
828 foundvert = true;
829 }
830
831 /* if include_modifiers is enabled, need to consider modifiers too
832 * - only really care about the last modifier
833 */
834 if ((include_modifiers) && (fcu->modifiers.last())) {
835 FModifier *fcm = fcu->modifiers.last();
836
837 /* only use the maximum sensible limits of the modifiers if they are more extreme */
838 switch (fcm->type) {
839 case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
840 {
841 FMod_Limits *fmd = static_cast<FMod_Limits *>(fcm->data);
842
843 if (fmd->flag & FCM_LIMIT_XMIN) {
844 min = min_ff(min, fmd->rect.xmin);
845 }
846 if (fmd->flag & FCM_LIMIT_XMAX) {
847 max = max_ff(max, fmd->rect.xmax);
848 }
849 break;
850 }
851 case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
852 {
853 FMod_Cycles *fmd = static_cast<FMod_Cycles *>(fcm->data);
854
855 if (fmd->before_mode != FCM_EXTRAPOLATE_NONE) {
856 min = MINAFRAMEF;
857 }
858 if (fmd->after_mode != FCM_EXTRAPOLATE_NONE) {
859 max = MAXFRAMEF;
860 }
861 break;
862 }
863 /* TODO: function modifier may need some special limits */
864
865 default: /* all other standard modifiers are on the infinite range... */
866 min = MINAFRAMEF;
867 max = MAXFRAMEF;
868 break;
869 }
870
871 foundmod = true;
872 }
873 }
874
875 if (foundvert || foundmod) {
877 }
878
879 return float2{0.0f, 0.0f};
880}
881
882/* ----- ActionLayer implementation ----------- */
883
885{
886 ActionLayer *copy = MEM_new<ActionLayer>(allocation_name.c_str());
887 *copy = *reinterpret_cast<const ActionLayer *>(this);
888
889 /* Make a shallow copy of the Strips, without copying their data. */
891 allocation_name.c_str());
892 for (int i : this->strips().index_range()) {
893 Strip *strip_copy = MEM_new<Strip>(allocation_name.c_str(), *this->strip(i));
894 copy->strip_array[i] = strip_copy;
895 }
896
897 return &copy->wrap();
898}
899
901{
902 for (Strip *strip : this->strips()) {
903 MEM_delete(strip);
904 }
905 MEM_SAFE_DELETE(this->strip_array);
906 this->strip_array_num = 0;
907}
908
910{
911 return Span<Strip *>{reinterpret_cast<Strip **>(this->strip_array), this->strip_array_num};
912}
914{
915 return Span<Strip *>{reinterpret_cast<Strip **>(this->strip_array), this->strip_array_num};
916}
917const Strip *Layer::strip(const int64_t index) const
918{
919 return &this->strip_array[index]->wrap();
920}
922{
923 return &this->strip_array[index]->wrap();
924}
925
926Strip &Layer::strip_add(Action &owning_action, const Strip::Type strip_type)
927{
928 Strip &strip = Strip::create(owning_action, strip_type);
929
930 /* Add the new strip to the strip array. */
932 &this->strip_array, &this->strip_array_num, &strip);
933
934 return strip;
935}
936
937static void strip_ptr_destructor(ActionStrip **dna_strip_ptr)
938{
939 Strip &strip = (*dna_strip_ptr)->wrap();
940 MEM_delete(&strip);
941};
942
943bool Layer::strip_remove(Action &owning_action, Strip &strip)
944{
945 const int64_t strip_index = this->find_strip_index(strip);
946 if (strip_index < 0) {
947 return false;
948 }
949
950 const Strip::Type strip_type = strip.type();
951 const int data_index = strip.data_index;
952
954 &this->strip_array, &this->strip_array_num, nullptr, strip_index, strip_ptr_destructor);
955
956 /* It's important that we do this *after* removing the strip itself
957 * (immediately above), because otherwise the strip will be found as a
958 * still-existing user of the strip data and thus the strip data won't be
959 * removed even if this strip was the last user. */
960 switch (strip_type) {
962 owning_action.strip_keyframe_data_remove_if_unused(data_index);
963 break;
964 }
965
966 return true;
967}
968
970{
971 for (const int64_t strip_index : this->strips().index_range()) {
972 const Strip *visit_strip = this->strip(strip_index);
973 if (visit_strip == &strip) {
974 return strip_index;
975 }
976 }
977 return -1;
978}
979
980/* ----- ActionSlot implementation ----------- */
981
983{
984 /* Zero-initialize the DNA struct. 'this' is a C++ class, and shouldn't be `memset` like this. */
985 _DNA_internal_memzero(this, sizeof(ActionSlot));
986 this->runtime = MEM_new<SlotRuntime>(__func__);
987}
988
989Slot::Slot(const Slot &other) : ActionSlot(other)
990{
991 this->runtime = MEM_new<SlotRuntime>(__func__);
992}
993
995{
996 MEM_delete(this->runtime);
997}
998
1000{
1001 BLI_assert(!this->runtime);
1002 this->runtime = MEM_new<SlotRuntime>(__func__);
1003}
1004
1005bool Slot::is_suitable_for(const ID &animated_id) const
1006{
1007 if (!this->has_idtype()) {
1008 /* Without specific ID type set, this Slot can animate any ID. */
1009 return true;
1010 }
1011
1012 /* Check that the ID type is compatible with this slot. */
1013 const int animated_idtype = animated_id.id_type();
1014 return this->idtype == animated_idtype;
1015}
1016
1018{
1019 return this->idtype != 0;
1020}
1021
1023{
1024 return static_cast<Slot::Flags>(this->slot_flags);
1025}
1027{
1028 return this->slot_flags & uint8_t(Flags::Expanded);
1029}
1030void Slot::set_expanded(const bool expanded)
1031{
1032 if (expanded) {
1034 }
1035 else {
1037 }
1038}
1039
1041{
1042 return this->slot_flags & uint8_t(Flags::Selected);
1043}
1044void Slot::set_selected(const bool selected)
1045{
1046 if (selected) {
1048 }
1049 else {
1051 }
1052}
1053
1055{
1056 return this->slot_flags & uint8_t(Flags::Active);
1057}
1058void Slot::set_active(const bool active)
1059{
1060 if (active) {
1062 }
1063 else {
1065 }
1066}
1067
1069{
1072 }
1073 BLI_assert(this->runtime);
1074 return this->runtime->users.as_span();
1075}
1076
1078{
1079 BLI_assert_msg(this->runtime, "Slot::runtime should always be allocated");
1080 return this->runtime->users;
1081}
1082
1083void Slot::users_add(ID &animated_id)
1084{
1085 BLI_assert(this->runtime);
1086 this->runtime->users.append_non_duplicates(&animated_id);
1087}
1088
1089void Slot::users_remove(ID &animated_id)
1090{
1091 BLI_assert(this->runtime);
1092 Vector<ID *> &users = this->runtime->users;
1093
1094 /* Even though users_add() ensures that there are no duplicates, there's still things like
1095 * pointer swapping etc. that can happen via the foreach-id looping code. That means that the
1096 * entries in the user map are not 100% under control of the user_add() and user_remove()
1097 * function, and thus we cannot assume that there are no duplicates. */
1098 users.remove_if([&](const ID *user) { return user == &animated_id; });
1099}
1100
1102{
1103 bmain.is_action_slot_to_id_map_dirty = true;
1104}
1105
1106std::string Slot::idtype_string() const
1107{
1108 if (!this->has_idtype()) {
1109 return slot_untyped_prefix;
1110 }
1111
1112 char name[3] = {0};
1113 *reinterpret_cast<short *>(name) = this->idtype;
1114 return name;
1115}
1116
1118{
1120 BLI_assert(identifier.size() >= 2);
1121
1122 return identifier.substr(0, 2);
1123}
1124
1126{
1128
1129 /* Avoid accessing an uninitialized part of the string accidentally. */
1130 if (this->identifier[0] == '\0' || this->identifier[1] == '\0') {
1131 return "";
1132 }
1133 return this->identifier + 2;
1134}
1135
1137{
1139
1140 if (StringRef(this->identifier).size() < 2) {
1141 /* The code below would overwrite the trailing 0-byte. */
1142 this->identifier[2] = '\0';
1143 }
1144
1145 if (!this->has_idtype()) {
1146 /* A zero idtype is not going to convert to a two-character string, so we
1147 * need to explicitly assign the default prefix. */
1148 this->identifier[0] = slot_untyped_prefix[0];
1149 this->identifier[1] = slot_untyped_prefix[1];
1150 return;
1151 }
1152
1153 *reinterpret_cast<short *>(this->identifier) = this->idtype;
1154}
1155
1156/* ----- Functions ----------- */
1157
1159{
1160 bAction *dna_action = BKE_action_add(&bmain, name.c_str());
1161 BLI_assert(dna_action->id.us == 1);
1162 id_us_min(&dna_action->id);
1163 return dna_action->wrap();
1164}
1165
1166bool assign_action(bAction *action, ID &animated_id)
1167{
1168 AnimData *adt = BKE_animdata_ensure_id(&animated_id);
1169 if (!adt) {
1170 return false;
1171 }
1172 return assign_action(action, {animated_id, *adt});
1173}
1174
1175bool assign_action(bAction *action, const OwnedAnimData owned_adt)
1176{
1177 if (!BKE_animdata_action_editable(&owned_adt.adt)) {
1178 /* Cannot remove, otherwise things turn to custard. */
1179 BKE_report(nullptr, RPT_ERROR, "Cannot change action, as it is still being edited in NLA");
1180 return false;
1181 }
1182
1183 return generic_assign_action(owned_adt.owner_id,
1184 action,
1185 owned_adt.adt.action,
1186 owned_adt.adt.slot_handle,
1187 owned_adt.adt.last_slot_identifier);
1188}
1189
1190bool assign_tmpaction(bAction *action, const OwnedAnimData owned_adt)
1191{
1192 return generic_assign_action(owned_adt.owner_id,
1193 action,
1194 owned_adt.adt.tmpact,
1195 owned_adt.adt.tmp_slot_handle,
1196 owned_adt.adt.tmp_last_slot_identifier);
1197}
1198
1199bool unassign_action(ID &animated_id)
1200{
1201 return assign_action(nullptr, animated_id);
1202}
1203
1205{
1206 return assign_action(nullptr, owned_adt);
1207}
1208
1210{
1211 AnimData *adt = BKE_animdata_from_id(&animated_id);
1212 Slot *slot;
1213
1214 /* Find a suitable slot, but be stricter about when to allow searching by name
1215 * than generic_slot_for_autoassign(...). */
1216 if (adt && adt->action == &action) {
1217 /* The slot handle is only valid when this action is already assigned.
1218 * Otherwise it's meaningless. */
1219 slot = action.slot_for_handle(adt->slot_handle);
1220
1221 /* If this Action is already assigned, a search by name is inappropriate, as it might
1222 * re-assign an intentionally-unassigned slot. */
1223 }
1224 else {
1225 /* In this case a by-name search is ok, so defer to generic_slot_for_autoassign(). */
1226 slot = generic_slot_for_autoassign(animated_id, action, adt ? adt->last_slot_identifier : "");
1227 }
1228
1229 /* As a last resort, if there is only one slot and it has no ID type yet, use that. This is what
1230 * gets created for the backwards compatibility RNA API, for example to allow
1231 * `action.fcurves.new()`. Key insertion should use that slot as well. */
1232 if (!slot && action.slots().size() == 1) {
1233 Slot *first_slot = action.slot(0);
1234 if (!first_slot->has_idtype()) {
1235 slot = first_slot;
1236 }
1237 }
1238
1239 /* If no suitable slot was found, create a new one. */
1240 if (!slot || !slot->is_suitable_for(animated_id)) {
1241 slot = &action.slot_add_for_id(animated_id);
1242 }
1243
1244 /* Only try to assign the Action to the ID if it is not already assigned.
1245 * Assignment can fail when the ID is in NLA Tweak mode. */
1246 const bool is_correct_action = adt && adt->action == &action;
1247 if (!is_correct_action && !assign_action(&action, animated_id)) {
1248 return nullptr;
1249 }
1250
1251 const bool is_correct_slot = adt && adt->slot_handle == slot->handle;
1252 if (!is_correct_slot && assign_action_slot(slot, animated_id) != ActionSlotAssignmentResult::OK)
1253 {
1254 /* This should never happen, as a few lines above a new slot is created for
1255 * this ID if the found one wasn't deemed suitable. */
1257 return nullptr;
1258 }
1259
1260 return slot;
1261}
1262
1263static bool is_id_using_action_slot(const ID &animated_id,
1264 const Action &action,
1265 const slot_handle_t slot_handle)
1266{
1267 auto visit_action_use = [&](const Action &used_action, slot_handle_t used_slot_handle) -> bool {
1268 const bool is_used = (&used_action == &action && used_slot_handle == slot_handle);
1269 return !is_used; /* Stop searching when we found a use of this Action+Slot. */
1270 };
1271
1272 const bool looped_until_end = foreach_action_slot_use(animated_id, visit_action_use);
1273 return !looped_until_end;
1274}
1275
1276bool generic_assign_action(ID &animated_id,
1277 bAction *action_to_assign,
1278 bAction *&action_ptr_ref,
1279 slot_handle_t &slot_handle_ref,
1280 char *slot_identifier)
1281{
1282 BLI_assert(slot_identifier);
1283
1284 /* Un-assign any previously-assigned Action first. */
1285 if (action_ptr_ref) {
1286 /* Un-assign the slot. This will always succeed, so no need to check the result. */
1287 if (slot_handle_ref != Slot::unassigned) {
1289 nullptr, animated_id, action_ptr_ref, slot_handle_ref, slot_identifier);
1292 }
1293
1294 /* Un-assign the Action itself. */
1295 id_us_min(&action_ptr_ref->id);
1296 action_ptr_ref = nullptr;
1297 }
1298
1299 if (!action_to_assign) {
1300 /* Un-assigning was the point, so the work is done. */
1301 return true;
1302 }
1303
1304 /* Assign the new Action. */
1305 action_ptr_ref = action_to_assign;
1306 id_us_plus(&action_ptr_ref->id);
1307
1308 /* Auto-assign a slot. */
1309 Slot *slot = generic_slot_for_autoassign(animated_id, action_ptr_ref->wrap(), slot_identifier);
1311 slot, animated_id, action_ptr_ref, slot_handle_ref, slot_identifier);
1314
1315 return true;
1316}
1317
1319 Action &action,
1320 const StringRefNull last_slot_identifier)
1321{
1322 /* The slot-finding code in assign_action_ensure_slot_for_keying() is very
1323 * similar to the code here (differences are documented there). It is very
1324 * likely that changes in the logic here should be applied there as well. */
1325
1326 /* Try the slot identifier, if it is set. */
1327 if (!last_slot_identifier.is_empty()) {
1328 /* If the last-used slot identifier was 'untyped', i.e. started with XX, see if something more
1329 * specific to this ID type exists.
1330 *
1331 * If there is any choice in the matter, the more specific slot is chosen. In other words, in
1332 * this case:
1333 *
1334 * - last_slot_identifier = `XXSlot`
1335 * - both `XXSlot` and `OBSlot` exist on the Action (where `OB` represents the ID type of
1336 * `animated_id`).
1337 *
1338 * the `OBSlot` should be chosen. This means that `XXSlot` NOT being auto-assigned if there is
1339 * an alternative. Since untyped slots are bound on assignment, this design keeps the Action
1340 * as-is, which means that the `XXSlot` remains untyped and thus the user is free to assign
1341 * this to another ID type if desired. */
1342
1343 const bool last_used_identifier_is_typed = last_slot_identifier.substr(0, 2) !=
1344 slot_untyped_prefix;
1345 if (!last_used_identifier_is_typed) {
1346 const std::string with_idtype_prefix = StringRef(animated_id.name, 2) +
1347 last_slot_identifier.substr(2);
1348 Slot *slot = action.slot_find_by_identifier(with_idtype_prefix);
1349 if (slot && slot->is_suitable_for(animated_id)) {
1350 return slot;
1351 }
1352 }
1353
1354 /* See if the actual last-used slot identifier can be matched. */
1355 Slot *slot = action.slot_find_by_identifier(last_slot_identifier);
1356 if (slot && slot->is_suitable_for(animated_id)) {
1357 return slot;
1358 }
1359
1360 /* If the last-used slot identifier was IDSomething, and XXSomething exists (where ID = the
1361 * ID code of the animated ID), fall back to the XX. If slot `IDSomething` existed, the code
1362 * above would have already returned it. */
1363 if (last_used_identifier_is_typed) {
1364 const std::string with_untyped_prefix = StringRef(slot_untyped_prefix) +
1365 last_slot_identifier.substr(2);
1366 Slot *slot = action.slot_find_by_identifier(with_untyped_prefix);
1367 if (slot && slot->is_suitable_for(animated_id)) {
1368 return slot;
1369 }
1370 }
1371 }
1372
1373 /* Search for the ID name (which includes the ID type). */
1374 {
1375 Slot *slot = action.slot_find_by_identifier(animated_id.name);
1376 if (slot && slot->is_suitable_for(animated_id)) {
1377 return slot;
1378 }
1379 }
1380
1381 /* If there is only one slot, and it is not specific to any ID type, use that.
1382 *
1383 * This should only trigger in some special cases, like legacy Actions that were converted to
1384 * slotted Actions by the versioning code, where the legacy Action was never assigned to anything
1385 * (and thus had idroot = 0).
1386 *
1387 * This might seem overly specific, and for convenience of automatically auto-assigning a slot,
1388 * it might be tempting to remove the "slot->has_idtype()" check. However, that would make the
1389 * following workflow significantly more cumbersome:
1390 *
1391 * - Animate `Cube`. This creates `CubeAction` with a single slot `OBCube`.
1392 * - Assign `CubeAction` to `Suzanne`, with the intent of animating both `Cube` and `Suzanne`
1393 * with the same Action.
1394 * - This should **not** auto-assign the `OBCube` slot to `Suzanne`, as that will overwrite any
1395 * property of `Suzanne` with the animated values for the `OBCube` slot.
1396 *
1397 * Recovering from this will be hard, as an undo will revert both the overwriting of properties
1398 * and the assignment of the Action. */
1399 if (action.slots().size() == 1) {
1400 Slot *slot = action.slot(0);
1401 if (!slot->has_idtype()) {
1402 return slot;
1403 }
1404 }
1405
1406 return nullptr;
1407}
1408
1410 ID &animated_id,
1411 bAction *&action_ptr_ref,
1412 slot_handle_t &slot_handle_ref,
1413 char *slot_identifier)
1414{
1415 BLI_assert(slot_identifier);
1416 if (!action_ptr_ref) {
1417 /* No action assigned yet, so no way to assign a slot. */
1419 }
1420
1421 Action &action = action_ptr_ref->wrap();
1422
1423 /* Check that the slot can actually be assigned. */
1424 if (slot_to_assign) {
1425 if (!action.slots().contains(slot_to_assign)) {
1427 }
1428
1429 if (!slot_to_assign->is_suitable_for(animated_id)) {
1431 }
1432 }
1433
1434 Slot *slot_to_unassign = action.slot_for_handle(slot_handle_ref);
1435
1436 /* If there was a previously-assigned slot, unassign it first. */
1437 slot_handle_ref = Slot::unassigned;
1438 if (slot_to_unassign) {
1439 /* Make sure that the stored Slot identifier is up to date. The slot identifier might have
1440 * changed in a way that wasn't copied into the ADT yet (for example when the
1441 * Action is linked from another file), so better copy the identifier to be sure
1442 * that it can be transparently reassigned later.
1443 *
1444 * TODO: Replace this with a BLI_assert() that the identifier is as expected, and "simply"
1445 * ensure this identifier is always correct. */
1446 BLI_strncpy_utf8(slot_identifier, slot_to_unassign->identifier, Slot::identifier_length_max);
1447
1448 /* If this was the last use of this slot, remove this ID from its users. */
1449 if (!is_id_using_action_slot(animated_id, action, slot_to_unassign->handle)) {
1450 slot_to_unassign->users_remove(animated_id);
1451 }
1452 }
1453
1454 if (!slot_to_assign) {
1456 }
1457
1458 action.slot_setup_for_id(*slot_to_assign, animated_id);
1459 slot_handle_ref = slot_to_assign->handle;
1460 BLI_strncpy_utf8(slot_identifier, slot_to_assign->identifier, Slot::identifier_length_max);
1461 slot_to_assign->users_add(animated_id);
1462
1464}
1465
1467 ID &animated_id,
1468 bAction *&action_ptr_ref,
1469 slot_handle_t &slot_handle_ref,
1470 char *slot_identifier)
1471{
1472 if (slot_handle_to_assign == Slot::unassigned && !action_ptr_ref) {
1473 /* No Action assigned, so no slot was used anyway. Just blindly assign the
1474 * 'unassigned' handle. */
1475 slot_handle_ref = Slot::unassigned;
1477 }
1478
1479 if (!action_ptr_ref) {
1480 /* No Action to verify the slot handle is valid. As the slot handle will be
1481 * completely ignored when re-assigning an Action, better to refuse setting
1482 * it altogether. This will make bugs more obvious. */
1484 }
1485
1486 Slot *slot = action_ptr_ref->wrap().slot_for_handle(slot_handle_to_assign);
1488 slot, animated_id, action_ptr_ref, slot_handle_ref, slot_identifier);
1489}
1490
1492{
1493 AnimData *adt = BKE_animdata_from_id(&animated_id);
1494 if (!adt) {
1496 }
1497
1499 slot_to_assign, animated_id, adt->action, adt->slot_handle, adt->last_slot_identifier);
1500}
1501
1503 Slot *slot_to_assign,
1504 ID &animated_id)
1505{
1506 if (!assign_action(action, animated_id)) {
1508 }
1509 return assign_action_slot(slot_to_assign, animated_id);
1510}
1511
1513 const slot_handle_t slot_handle,
1514 const OwnedAnimData owned_adt)
1515{
1516 if (!assign_tmpaction(action, owned_adt)) {
1518 }
1519 return generic_assign_action_slot_handle(slot_handle,
1520 owned_adt.owner_id,
1521 owned_adt.adt.tmpact,
1522 owned_adt.adt.tmp_slot_handle,
1523 owned_adt.adt.tmp_last_slot_identifier);
1524}
1525
1526Action *get_action(ID &animated_id)
1527{
1528 AnimData *adt = BKE_animdata_from_id(&animated_id);
1529 if (!adt) {
1530 return nullptr;
1531 }
1532 if (!adt->action) {
1533 return nullptr;
1534 }
1535 return &adt->action->wrap();
1536}
1537
1538std::optional<std::pair<Action *, Slot *>> get_action_slot_pair(ID &animated_id)
1539{
1540 AnimData *adt = BKE_animdata_from_id(&animated_id);
1541 if (!adt || !adt->action) {
1542 /* Not animated by any Action. */
1543 return std::nullopt;
1544 }
1545
1546 Action &action = adt->action->wrap();
1547 Slot *slot = action.slot_for_handle(adt->slot_handle);
1548 if (!slot) {
1549 /* Will not receive any animation from this Action. */
1550 return std::nullopt;
1551 }
1552
1553 return std::make_pair(&action, slot);
1554}
1555
1556/* ----- ActionStrip implementation ----------- */
1557
1559{
1560 /* Create the strip. */
1561 ActionStrip *strip = MEM_new<ActionStrip>(__func__);
1562 strip->strip_type = int8_t(type);
1563
1564 /* Create the strip's data on the owning Action. */
1565 switch (type) {
1566 case Strip::Type::Keyframe: {
1567 StripKeyframeData *strip_data = MEM_new<StripKeyframeData>(__func__);
1568 strip->data_index = owning_action.strip_keyframe_data_append(strip_data);
1569 break;
1570 }
1571 }
1572
1573 /* This can happen if someone forgets to add a strip type in the `switch`
1574 * above, or if someone is evil and passes an invalid strip type to this
1575 * function. */
1576 BLI_assert_msg(strip->data_index != -1, "Newly created strip has no data.");
1577
1578 return strip->wrap();
1579}
1580
1582{
1583 return this->frame_start == -std::numeric_limits<float>::infinity() &&
1584 this->frame_end == std::numeric_limits<float>::infinity();
1585}
1586
1587bool Strip::contains_frame(const float frame_time) const
1588{
1589 return this->frame_start <= frame_time && frame_time <= this->frame_end;
1590}
1591
1592bool Strip::is_last_frame(const float frame_time) const
1593{
1594 /* Maybe this needs a more advanced equality check. Implement that when
1595 * we have an actual example case that breaks. */
1596 return this->frame_end == frame_time;
1597}
1598
1599void Strip::resize(const float frame_start, const float frame_end)
1600{
1602 BLI_assert_msg(frame_start < std::numeric_limits<float>::infinity(),
1603 "only the end frame can be at positive infinity");
1604 BLI_assert_msg(frame_end > -std::numeric_limits<float>::infinity(),
1605 "only the start frame can be at negative infinity");
1606 this->frame_start = frame_start;
1607 this->frame_end = frame_end;
1608}
1609
1610template<>
1612{
1614
1615 return *owning_action.strip_keyframe_data()[this->data_index];
1616}
1618{
1620
1621 return *owning_action.strip_keyframe_data()[this->data_index];
1622}
1623
1624/* ----- ActionStripKeyframeData implementation ----------- */
1625
1628{
1630 __func__);
1631 Span<const Channelbag *> channelbags_src = other.channelbags();
1632 for (int i : channelbags_src.index_range()) {
1633 this->channelbag_array[i] = MEM_new<animrig::Channelbag>(__func__, *other.channelbag(i));
1634 }
1635}
1636
1638{
1639 for (Channelbag *channelbag_for_slot : this->channelbags()) {
1640 MEM_delete(channelbag_for_slot);
1641 }
1642 MEM_SAFE_DELETE(this->channelbag_array);
1643 this->channelbag_array_num = 0;
1644}
1645
1657{
1658 return &this->channelbag_array[index]->wrap();
1659}
1661{
1662 return &this->channelbag_array[index]->wrap();
1663}
1665{
1666 for (const Channelbag *channels : this->channelbags()) {
1667 if (channels->slot_handle == slot_handle) {
1668 return channels;
1669 }
1670 }
1671 return nullptr;
1672}
1674{
1675 for (int64_t index = 0; index < this->channelbag_array_num; index++) {
1676 if (this->channelbag(index) == &channelbag) {
1677 return index;
1678 }
1679 }
1680 return -1;
1681}
1683{
1684 const auto *const_this = const_cast<const StripKeyframeData *>(this);
1685 const auto *const_channels = const_this->channelbag_for_slot(slot_handle);
1686 return const_cast<Channelbag *>(const_channels);
1687}
1689{
1690 return this->channelbag_for_slot(slot.handle);
1691}
1693{
1694 return this->channelbag_for_slot(slot.handle);
1695}
1696
1701
1703{
1704 BLI_assert_msg(channelbag_for_slot(slot_handle) == nullptr,
1705 "Cannot add channelbag for already-registered slot");
1706 BLI_assert_msg(slot_handle != Slot::unassigned, "Cannot add channelbag for 'unassigned' slot");
1707
1708 Channelbag &channels = MEM_new<ActionChannelbag>(__func__)->wrap();
1709 channels.slot_handle = slot_handle;
1710
1712 &this->channelbag_array, &this->channelbag_array_num, &channels);
1713
1714 return channels;
1715}
1716
1721
1723{
1724 Channelbag *channelbag = this->channelbag_for_slot(slot_handle);
1725 if (channelbag != nullptr) {
1726 return *channelbag;
1727 }
1728 return this->channelbag_for_slot_add(slot_handle);
1729}
1730
1731static void channelbag_ptr_destructor(ActionChannelbag **dna_channelbag_ptr)
1732{
1733 Channelbag &channelbag = (*dna_channelbag_ptr)->wrap();
1734 MEM_delete(&channelbag);
1735};
1736
1738{
1739 const int64_t channelbag_index = this->find_channelbag_index(channelbag_to_remove);
1740 if (channelbag_index < 0) {
1741 return false;
1742 }
1743
1745 &this->channelbag_array_num,
1746 nullptr,
1747 channelbag_index,
1749
1750 return true;
1751}
1752
1754{
1755 Channelbag *channelbag = this->channelbag_for_slot(slot_handle);
1756 if (!channelbag) {
1757 return;
1758 }
1760}
1761
1763 const slot_handle_t target_slot_handle)
1764{
1765 BLI_assert(!this->channelbag_for_slot(target_slot_handle));
1766
1767 const Channelbag *source_cbag = this->channelbag_for_slot(source_slot_handle);
1768 if (!source_cbag) {
1769 return;
1770 }
1771
1772 Channelbag &target_cbag = *MEM_new<animrig::Channelbag>(__func__, *source_cbag);
1773 target_cbag.slot_handle = target_slot_handle;
1774
1776 &this->channelbag_array, &this->channelbag_array_num, &target_cbag);
1777}
1778
1779const FCurve *Channelbag::fcurve_find(const FCurveDescriptor &fcurve_descriptor) const
1780{
1781 return animrig::fcurve_find(this->fcurves(), fcurve_descriptor);
1782}
1783
1785{
1786 /* Intermediate variable needed to disambiguate const/non-const overloads. */
1787 Span<FCurve *> fcurves = this->fcurves();
1788 return animrig::fcurve_find(fcurves, fcurve_descriptor);
1789}
1790
1791FCurve &Channelbag::fcurve_ensure(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
1792{
1793 if (FCurve *existing_fcurve = this->fcurve_find(fcurve_descriptor)) {
1794 return *existing_fcurve;
1795 }
1796 return this->fcurve_create(bmain, fcurve_descriptor);
1797}
1798
1800{
1801 if (this->fcurve_find(fcurve_descriptor)) {
1802 return nullptr;
1803 }
1804 return &this->fcurve_create(bmain, fcurve_descriptor);
1805}
1806
1807FCurve &Channelbag::fcurve_create(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
1808{
1809 FCurve *new_fcurve = create_fcurve_for_channel(fcurve_descriptor);
1810
1811 if (this->fcurve_array_num == 0) {
1812 new_fcurve->flag |= FCURVE_ACTIVE; /* First curve is added active. */
1813 }
1814
1815 bActionGroup *group = fcurve_descriptor.channel_group.has_value() ?
1816 &this->channel_group_ensure(*fcurve_descriptor.channel_group) :
1817 nullptr;
1818 const int insert_index = group ? group->fcurve_range_start + group->fcurve_range_length :
1819 this->fcurve_array_num;
1820 BLI_assert(insert_index <= this->fcurve_array_num);
1821
1822 grow_array_and_insert(&this->fcurve_array, &this->fcurve_array_num, insert_index, new_fcurve);
1823 if (group) {
1824 group->fcurve_range_length += 1;
1825 this->restore_channel_group_invariants();
1826 }
1827
1828 if (bmain) {
1830 }
1831
1832 return *new_fcurve;
1833}
1834
1836 Span<FCurveDescriptor> fcurve_descriptors)
1837{
1838 const int prev_fcurve_num = this->fcurve_array_num;
1839 const int add_fcurve_num = int(fcurve_descriptors.size());
1840 const bool make_first_active = prev_fcurve_num == 0;
1841
1842 /* Figure out which path+index combinations already exist. */
1843 struct CurvePathIndex {
1845 int array_index;
1846 bool operator==(const CurvePathIndex &o) const
1847 {
1848 /* Check indices first, cheaper than a string comparison. */
1849 return this->array_index == o.array_index && this->rna_path == o.rna_path;
1850 }
1851 uint64_t hash() const
1852 {
1853 return get_default_hash(this->rna_path, this->array_index);
1854 }
1855 };
1856 Set<CurvePathIndex> unique_curves;
1857 unique_curves.reserve(prev_fcurve_num);
1858 for (FCurve *fcurve : this->fcurves()) {
1859 CurvePathIndex path_index;
1860 path_index.rna_path = fcurve->rna_path();
1861 path_index.array_index = fcurve->array_index;
1862 unique_curves.add(path_index);
1863 }
1864
1865 /* Grow curves array with enough space for new curves. */
1866 grow_array(&this->fcurve_array, &this->fcurve_array_num, add_fcurve_num);
1867
1868 /* Add the new curves. */
1869 Vector<FCurve *> new_fcurves;
1870 new_fcurves.resize(add_fcurve_num);
1871 int curve_index = prev_fcurve_num;
1872 for (int i = 0; i < add_fcurve_num; i++) {
1873 const FCurveDescriptor &desc = fcurve_descriptors[i];
1874
1875 CurvePathIndex path_index;
1876 path_index.rna_path = desc.rna_path;
1877 path_index.array_index = desc.array_index;
1878 if (desc.rna_path.is_empty() || !unique_curves.add(path_index)) {
1879 /* Empty input path, or such curve already exists. */
1880 new_fcurves[i] = nullptr;
1881 continue;
1882 }
1883
1885 new_fcurves[i] = fcurve;
1886
1887 this->fcurve_array[curve_index] = fcurve;
1888 if (desc.channel_group.has_value()) {
1889 bActionGroup *group = &this->channel_group_ensure(*desc.channel_group);
1890 const int insert_index = group->fcurve_range_start + group->fcurve_range_length;
1891 BLI_assert(insert_index <= this->fcurve_array_num);
1892 /* Insert curve into proper array place at the end of the group. Note: this can
1893 * still lead to quadratic complexity, in practice was not found to be an issue yet. */
1895 this->fcurve_array, this->fcurve_array_num, curve_index, curve_index + 1, insert_index);
1896 group->fcurve_range_length++;
1897
1898 /* Update curve start ranges of the following groups. */
1899 int index = this->channel_group_find_index(group);
1900 BLI_assert(index >= 0 && index < this->group_array_num);
1901 for (index = index + 1; index < this->group_array_num; index++) {
1903 }
1904 }
1905 curve_index++;
1906 }
1907
1908 if (this->fcurve_array_num != curve_index) {
1909 /* Some curves were not created, resize to final amount. */
1911 &this->fcurve_array, &this->fcurve_array_num, this->fcurve_array_num - curve_index);
1912 }
1913
1914 if (make_first_active) {
1915 /* Set first created curve as active. */
1916 for (FCurve *fcurve : new_fcurves) {
1917 if (fcurve != nullptr) {
1919 break;
1920 }
1921 }
1922 }
1923
1924 this->restore_channel_group_invariants();
1925 if (bmain) {
1927 }
1928 return new_fcurves;
1929}
1930
1932 const StringRefNull new_path,
1933 const int new_array_index,
1934 const StringRef new_group_name)
1935{
1936 FCurve *new_fcurve = this->fcurve_find({new_path, new_array_index});
1937 if (new_fcurve) {
1938 MEM_delete(new_fcurve->bezt);
1939 new_fcurve->bezt = MEM_dupalloc(old_fcurve.bezt);
1940 MEM_delete(new_fcurve->fpt);
1941 new_fcurve->fpt = MEM_dupalloc(old_fcurve.fpt);
1942 new_fcurve->totvert = old_fcurve.totvert;
1943 }
1944 else {
1945 new_fcurve = BKE_fcurve_copy(&old_fcurve);
1946 new_fcurve->rna_path_set(new_path);
1947 new_fcurve->array_index = new_array_index;
1948 this->fcurve_append(*new_fcurve);
1949 }
1950 bActionGroup &agrp = this->channel_group_ensure(new_group_name.data());
1951 this->fcurve_assign_to_channel_group(*new_fcurve, agrp);
1952 return *new_fcurve;
1953}
1954
1956{
1957 /* Appended F-Curves don't belong to any group yet, so better make sure their
1958 * group pointer reflects that. */
1959 fcurve.grp = nullptr;
1960
1962}
1963
1964static void fcurve_ptr_destructor(FCurve **fcurve_ptr)
1965{
1966 BKE_fcurve_free(*fcurve_ptr);
1967};
1968
1969bool Channelbag::fcurve_remove(FCurve &fcurve_to_remove)
1970{
1971 if (!this->fcurve_detach(fcurve_to_remove)) {
1972 return false;
1973 }
1974 BKE_fcurve_free(&fcurve_to_remove);
1975 return true;
1976}
1977
1979{
1980 /* Grab the pointer before it's detached, so we can free it after. */
1981 FCurve *fcurve_to_remove = this->fcurve(fcurve_index);
1982
1983 this->fcurve_detach_by_index(fcurve_index);
1984
1985 BKE_fcurve_free(fcurve_to_remove);
1986}
1987
1988static void fcurve_ptr_noop_destructor(FCurve ** /*fcurve_ptr*/) {}
1989
1990bool Channelbag::fcurve_detach(FCurve &fcurve_to_detach)
1991{
1992 const int64_t fcurve_index = this->fcurves().first_index_try(&fcurve_to_detach);
1993 if (fcurve_index < 0) {
1994 return false;
1995 }
1996 this->fcurve_detach_by_index(fcurve_index);
1997 return true;
1998}
1999
2001{
2002 BLI_assert(fcurve_index >= 0);
2003 BLI_assert(fcurve_index < this->fcurve_array_num);
2004
2005 const int group_index = this->channel_group_containing_index(fcurve_index);
2006 if (group_index != -1) {
2007 bActionGroup *group = this->channel_group(group_index);
2008
2009 group->fcurve_range_length -= 1;
2010 if (group->fcurve_range_length <= 0) {
2011 const int group_index = this->channel_groups().first_index_try(group);
2012 this->channel_group_remove_raw(group_index);
2013 }
2014 }
2015
2017 &this->fcurve_array_num,
2018 nullptr,
2019 fcurve_index,
2021
2022 this->restore_channel_group_invariants();
2023
2024 /* As an optimization, this function could call `DEG_relations_tag_update(bmain)` to prune any
2025 * relationships that are now no longer necessary. This is not needed for correctness of the
2026 * depsgraph evaluation results though. */
2027}
2028
2030{
2031 BLI_assert(to_fcurve_index >= 0 && to_fcurve_index < this->fcurves().size());
2032
2033 const int fcurve_index = this->fcurves().first_index_try(&fcurve);
2034 BLI_assert_msg(fcurve_index >= 0, "FCurve not in this channel bag.");
2035
2037 this->fcurve_array, this->fcurve_array_num, fcurve_index, fcurve_index + 1, to_fcurve_index);
2038
2039 this->restore_channel_group_invariants();
2040}
2041
2043{
2045
2046 /* Since all F-Curves are gone, the groups are all empty. */
2047 for (bActionGroup *group : channel_groups()) {
2048 group->fcurve_range_start = 0;
2049 group->fcurve_range_length = 0;
2050 }
2051}
2052
2054{
2055 /* #BKE_fcurve_get_cycle_type() only looks at the first modifier to see if it's a Cycle modifier,
2056 * so if we're going to add one, better make sure it's the first one.
2057 *
2058 * BUT: #add_fmodifier() only allows adding a Cycle modifier when there are none yet, so that's
2059 * all that we need to check for here.
2060 */
2061 if (!fcurve.modifiers.is_empty()) {
2062 return;
2063 }
2064
2066}
2067
2078static void cyclic_keying_ensure_cycle_range_exists(FCurve &fcurve, const float2 cycle_range)
2079{
2080 /* This is basically a copy of the legacy function `make_new_fcurve_cyclic()`
2081 * in `keyframing.cc`, except that it's limited to only one thing (ensuring
2082 * two keys exist to make cycling possible). Creating the F-Curve modifier is
2083 * the responsibility of another function. */
2084
2085 if (fcurve.totvert != 1 || fcurve.bezt == nullptr) {
2086 return;
2087 }
2088
2089 const float period = cycle_range[1] - cycle_range[0];
2090 if (period < 0.1f) {
2091 return;
2092 }
2093
2094 /* Move the one existing keyframe into the cycle range. */
2095 const float frame_offset = fcurve.bezt[0].vec[1][0] - cycle_range[0];
2096 const float fix = floorf(frame_offset / period) * period;
2097
2098 fcurve.bezt[0].vec[0][0] -= fix;
2099 fcurve.bezt[0].vec[1][0] -= fix;
2100 fcurve.bezt[0].vec[2][0] -= fix;
2101
2102 /* Reallocate the array to make space for the 2nd point. */
2103 fcurve.totvert++;
2104 fcurve.bezt = static_cast<BezTriple *>(
2105 MEM_realloc_uninitialized(fcurve.bezt, sizeof(BezTriple) * fcurve.totvert));
2106
2107 /* Duplicate and offset the keyframe. */
2108 fcurve.bezt[1] = fcurve.bezt[0];
2109 fcurve.bezt[1].vec[0][0] += period;
2110 fcurve.bezt[1].vec[1][0] += period;
2111 fcurve.bezt[1].vec[2][0] += period;
2112}
2113
2115 const Slot &slot,
2116 const FCurveDescriptor &fcurve_descriptor,
2117 const float2 time_value,
2118 const KeyframeSettings &settings,
2119 const eInsertKeyFlags insert_key_flags,
2120 const std::optional<float2> cycle_range)
2121{
2122 /* Get the fcurve, or create one if it doesn't exist and the keying flags
2123 * allow. */
2124 FCurve *fcurve = nullptr;
2125 if (key_insertion_may_create_fcurve(insert_key_flags)) {
2126 fcurve = &this->channelbag_for_slot_ensure(slot).fcurve_ensure(bmain, fcurve_descriptor);
2127 }
2128 else {
2129 Channelbag *channels = this->channelbag_for_slot(slot);
2130 if (channels != nullptr) {
2131 fcurve = channels->fcurve_find(fcurve_descriptor);
2132 }
2133 }
2134
2135 if (!fcurve) {
2136 CLOG_WARN(&LOG,
2137 "FCurve %s[%d] for slot %s was not created due to either the Only Insert "
2138 "Available setting or Replace keyframing mode.\n",
2139 fcurve_descriptor.rna_path.c_str(),
2140 fcurve_descriptor.array_index,
2141 slot.identifier);
2143 }
2144
2145 if (!BKE_fcurve_is_keyframable(*fcurve)) {
2146 /* TODO: handle this properly, in a way that can be communicated to the user. */
2147 CLOG_WARN(&LOG,
2148 "FCurve %s[%d] for slot %s doesn't allow inserting keys.\n",
2149 fcurve_descriptor.rna_path.c_str(),
2150 fcurve_descriptor.array_index,
2151 slot.identifier);
2153 }
2154
2155 if (cycle_range && (*cycle_range)[0] < (*cycle_range)[1]) {
2156 /* Cyclic keying consists of three things:
2157 * - Ensure there is a Cycle modifier on the F-Curve.
2158 * - Ensure the start and end of the cycle have explicit keys, so that the
2159 * cycle modifier knows how to cycle (it doesn't look at the Action, and
2160 * as long as the period is correct, the first/last keys don't have to
2161 * align with the Action start/end).
2162 * - Offset the key to insert so that it falls within the cycle range.
2163 */
2165 cyclic_keying_ensure_cycle_range_exists(*fcurve, *cycle_range);
2166 /* Offsetting the key doesn't have to happen here, as insert_vert_fcurve()
2167 * takes care of that. */
2168 }
2169
2170 const SingleKeyingResult insert_vert_result = insert_vert_fcurve(
2171 fcurve, time_value, settings, insert_key_flags);
2172
2173 if (insert_vert_result != SingleKeyingResult::SUCCESS) {
2174 CLOG_WARN(&LOG,
2175 "Could not insert key into FCurve %s[%d] for slot %s.\n",
2176 fcurve_descriptor.rna_path.c_str(),
2177 fcurve_descriptor.array_index,
2178 slot.identifier);
2179 return insert_vert_result;
2180 }
2181
2182 if (fcurve_descriptor.prop_type) {
2183 update_autoflags_fcurve_direct(fcurve, *fcurve_descriptor.prop_type);
2184 }
2185
2187}
2188
2189/* ActionChannelbag implementation. */
2190
2192{
2193 this->slot_handle = other.slot_handle;
2194
2195 this->fcurve_array_num = other.fcurve_array_num;
2197 for (int i = 0; i < other.fcurve_array_num; i++) {
2198 const FCurve *fcu_src = other.fcurve_array[i];
2199 this->fcurve_array[i] = BKE_fcurve_copy(fcu_src);
2200 }
2201
2202 this->group_array_num = other.group_array_num;
2204 for (int i = 0; i < other.group_array_num; i++) {
2205 const bActionGroup *group_src = other.group_array[i];
2206 this->group_array[i] = MEM_dupalloc(group_src);
2207 this->group_array[i]->channelbag = this;
2208 }
2209
2210 /* BKE_fcurve_copy() resets the FCurve's group pointer. Which is good, because the groups are
2211 * duplicated too. This sets the group pointers to the correct values. */
2212 this->restore_channel_group_invariants();
2213}
2214
2216{
2217 for (FCurve *fcu : this->fcurves()) {
2218 BKE_fcurve_free(fcu);
2219 }
2220 MEM_SAFE_DELETE(this->fcurve_array);
2221 this->fcurve_array_num = 0;
2222
2223 for (bActionGroup *group : this->channel_groups()) {
2224 MEM_SAFE_DELETE(group);
2225 }
2226 MEM_SAFE_DELETE(this->group_array);
2227 this->group_array_num = 0;
2228}
2229
2239{
2240 return this->fcurve_array[index];
2241}
2243{
2244 return this->fcurve_array[index];
2245}
2246
2256{
2257 BLI_assert(index < this->group_array_num);
2258 return this->group_array[index];
2259}
2261{
2262 BLI_assert(index < this->group_array_num);
2263 return this->group_array[index];
2264}
2265
2267{
2268 for (const bActionGroup *group : this->channel_groups()) {
2269 if (name == StringRef{group->name}) {
2270 return group;
2271 }
2272 }
2273
2274 return nullptr;
2275}
2276
2278{
2279 for (int i = 0; i < this->group_array_num; i++) {
2280 if (this->group_array[i] == group) {
2281 return i;
2282 }
2283 }
2284 return -1;
2285}
2286
2288{
2289 /* Intermediate variable needed to disambiguate const/non-const overloads. */
2290 Span<bActionGroup *> groups = this->channel_groups();
2291 for (bActionGroup *group : groups) {
2292 if (name == StringRef{group->name}) {
2293 return group;
2294 }
2295 }
2296
2297 return nullptr;
2298}
2299
2300int Channelbag::channel_group_containing_index(const int fcurve_array_index)
2301{
2302 int i = 0;
2303 for (const bActionGroup *group : this->channel_groups()) {
2304 if (fcurve_array_index >= group->fcurve_range_start &&
2305 fcurve_array_index < (group->fcurve_range_start + group->fcurve_range_length))
2306 {
2307 return i;
2308 }
2309 i++;
2310 }
2311
2312 return -1;
2313}
2314
2316{
2317 bActionGroup *new_group = MEM_new<bActionGroup>(__func__);
2318
2319 /* Find the end fcurve index of the current channel groups, to be used as the
2320 * start of the new channel group. */
2321 int fcurve_index = 0;
2322 const int length = this->channel_groups().size();
2323 if (length > 0) {
2324 const bActionGroup *last = this->channel_group(length - 1);
2325 fcurve_index = last->fcurve_range_start + last->fcurve_range_length;
2326 }
2327 new_group->fcurve_range_start = fcurve_index;
2328
2329 new_group->channelbag = this;
2330
2331 /* Make it selected. */
2332 new_group->flag = AGRP_SELECTED;
2333
2334 /* Ensure it has a unique name.
2335 *
2336 * Note that this only happens here (upon creation). The user can later rename
2337 * groups to have duplicate names. This is stupid, but it's how the legacy
2338 * system worked, and at the time of writing this code we're just trying to
2339 * match that system's behavior, even when it's goofy. */
2340 std::string unique_name = BLI_uniquename_cb(
2341 [&](const StringRef name) {
2342 for (const bActionGroup *group : this->channel_groups()) {
2343 if (STREQ(group->name, name.data())) {
2344 return true;
2345 }
2346 }
2347 return false;
2348 },
2349 '.',
2350 name[0] == '\0' ? DATA_("Group") : name);
2351
2352 STRNCPY_UTF8(new_group->name, unique_name.c_str());
2353
2354 grow_array_and_append(&this->group_array, &this->group_array_num, new_group);
2355
2356 return *new_group;
2357}
2358
2360{
2361 bActionGroup *group = this->channel_group_find(name);
2362 if (group) {
2363 return *group;
2364 }
2365
2366 return this->channel_group_create(name);
2367}
2368
2370{
2371 const int group_index = this->channel_groups().first_index_try(&group);
2372 if (group_index == -1) {
2373 return false;
2374 }
2375
2376 /* Move the group's fcurves to just past the end of where the grouped
2377 * fcurves will be after this group is removed. */
2378 const bActionGroup *last_group = this->channel_groups().last();
2379 BLI_assert(last_group != nullptr);
2380 const int to_index = last_group->fcurve_range_start + last_group->fcurve_range_length -
2381 group.fcurve_range_length;
2383 this->fcurve_array_num,
2384 group.fcurve_range_start,
2386 to_index);
2387
2388 this->channel_group_remove_raw(group_index);
2389 this->restore_channel_group_invariants();
2390
2391 return true;
2392}
2393
2394void Channelbag::channel_group_move_to_index(bActionGroup &group, const int to_group_index)
2395{
2396 BLI_assert(to_group_index >= 0 && to_group_index < this->channel_groups().size());
2397
2398 const int group_index = this->channel_groups().first_index_try(&group);
2399 BLI_assert_msg(group_index >= 0, "Group not in this channel bag.");
2400
2401 /* Shallow copy, to track which fcurves should be moved in the second step. */
2402 const bActionGroup pre_move_group = group;
2403
2404 /* First we move the group to its new position. The call to
2405 * `restore_channel_group_invariants()` is necessary to update the group's
2406 * fcurve range (as well as the ranges of the other groups) to match its new
2407 * position in the group array. */
2409 this->group_array, this->group_array_num, group_index, group_index + 1, to_group_index);
2410 this->restore_channel_group_invariants();
2411
2412 /* Move the fcurves that were part of `group` (as recorded in `pre_move_group`)
2413 * to their new positions (now in `group`) so that they're part of `group` again. */
2415 this->fcurve_array_num,
2416 pre_move_group.fcurve_range_start,
2417 pre_move_group.fcurve_range_start + pre_move_group.fcurve_range_length,
2418 group.fcurve_range_start);
2419 this->restore_channel_group_invariants();
2420}
2421
2422void Channelbag::channel_group_remove_raw(const int group_index)
2423{
2424 BLI_assert(group_index >= 0 && group_index < this->channel_groups().size());
2425
2426 MEM_SAFE_DELETE(this->group_array[group_index]);
2427 shrink_array_and_remove(&this->group_array, &this->group_array_num, group_index);
2428}
2429
2430void Channelbag::restore_channel_group_invariants()
2431{
2432 /* Shift channel groups. */
2433 {
2434 int start_index = 0;
2435 for (bActionGroup *group : this->channel_groups()) {
2436 group->fcurve_range_start = start_index;
2437 start_index += group->fcurve_range_length;
2438 }
2439
2440 /* Double-check that this didn't push any of the groups off the end of the
2441 * fcurve array. */
2442 BLI_assert(start_index <= this->fcurve_array_num);
2443 }
2444
2445 /* Recompute fcurves' group pointers. */
2446 {
2447 for (FCurve *fcurve : this->fcurves()) {
2448 fcurve->grp = nullptr;
2449 }
2450 for (bActionGroup *group : this->channel_groups()) {
2451 for (FCurve *fcurve : group->wrap().fcurves()) {
2452 fcurve->grp = group;
2453 }
2454 }
2455 }
2456}
2457
2459{
2460 if (this->fcurve_range_length == 0) {
2461 return {};
2462 }
2463
2464 return this->channelbag->wrap().fcurves().slice(this->fcurve_range_start,
2465 this->fcurve_range_length);
2466}
2467
2469{
2470 if (this->fcurve_range_length == 0) {
2471 return {};
2472 }
2473
2474 return this->channelbag->wrap().fcurves().slice(this->fcurve_range_start,
2475 this->fcurve_range_length);
2476}
2477
2478/* Utility function implementations. */
2479
2481 const slot_handle_t slot_handle)
2482{
2484
2485 if (slot_handle == Slot::unassigned) {
2486 return nullptr;
2487 }
2488
2489 for (const animrig::Layer *layer : action.layers()) {
2490 for (const animrig::Strip *strip : layer->strips()) {
2491 switch (strip->type()) {
2493 const animrig::StripKeyframeData &strip_data = strip->data<animrig::StripKeyframeData>(
2494 action);
2495 const animrig::Channelbag *bag = strip_data.channelbag_for_slot(slot_handle);
2496 if (bag) {
2497 return bag;
2498 }
2499 }
2500 }
2501 }
2502 }
2503
2504 return nullptr;
2505}
2506
2508{
2509 if (slot_handle == Slot::unassigned) {
2510 return {};
2511 }
2512
2513 /* To avoid adding the same channelbag multiple times which can happen with strip instances. */
2514 Set<Channelbag *> visited_channelbags;
2515 Vector<Channelbag *> channelbags;
2516 for (animrig::Layer *layer : action.layers()) {
2517 for (animrig::Strip *strip : layer->strips()) {
2518 switch (strip->type()) {
2520 animrig::StripKeyframeData &strip_data = strip->data<animrig::StripKeyframeData>(action);
2521 animrig::Channelbag *bag = strip_data.channelbag_for_slot(slot_handle);
2522 if (!bag) {
2523 continue;
2524 }
2525 if (!visited_channelbags.add(bag)) {
2526 continue;
2527 }
2528 channelbags.append(bag);
2529 }
2530 }
2531 }
2532 }
2533
2534 return channelbags;
2535}
2536
2538{
2540 const_cast<const Action &>(action), slot_handle);
2541 return const_cast<animrig::Channelbag *>(const_bag);
2542}
2543
2545{
2547 animrig::Channelbag *bag = channelbag_for_action_slot(action, slot_handle);
2548 if (!bag) {
2549 return {};
2550 }
2551 return bag->fcurves();
2552}
2553
2555{
2557 const animrig::Channelbag *bag = channelbag_for_action_slot(action, slot_handle);
2558 if (!bag) {
2559 return {};
2560 }
2561 return bag->fcurves();
2562}
2563
2565{
2566 if (act == nullptr) {
2567 return nullptr;
2568 }
2569
2570 Action &action = act->wrap();
2572 Layer *layer = action.layer(0);
2573 if (!layer) {
2574 return nullptr;
2575 }
2576 Strip *strip = layer->strip(0);
2577 if (!strip) {
2578 return nullptr;
2579 }
2580
2581 StripKeyframeData &strip_data = strip->data<StripKeyframeData>(action);
2582
2583 for (Channelbag *channelbag : strip_data.channelbags()) {
2584 FCurve *fcu = channelbag->fcurve_find(fcurve_descriptor);
2585 if (fcu) {
2586 return fcu;
2587 }
2588 }
2589
2590 return nullptr;
2591}
2592
2594{
2595 return fcurve_find_in_action_slot(adt.action, adt.slot_handle, fcurve_descriptor);
2596}
2597
2599 const slot_handle_t slot_handle,
2600 const FCurveDescriptor &fcurve_descriptor)
2601{
2602 if (act == nullptr) {
2603 return nullptr;
2604 }
2605
2606 Action &action = act->wrap();
2607 Channelbag *cbag = channelbag_for_action_slot(action, slot_handle);
2608 if (!cbag) {
2609 return nullptr;
2610 }
2611 return cbag->fcurve_find(fcurve_descriptor);
2612}
2613
2615 const StringRefNull collection_rna_path,
2616 const StringRefNull data_name)
2617{
2618 BLI_assert(!collection_rna_path.is_empty());
2619
2620 const size_t quoted_name_size = data_name.size() + 1;
2621 char *quoted_name = static_cast<char *>(alloca(quoted_name_size));
2622
2623 const StringRefNull rna_path = fcurve.rna_path();
2624 if (rna_path.is_empty()) {
2625 return false;
2626 }
2627 /* Skipping names longer than `quoted_name_size` is OK since we're after an exact match. */
2629 rna_path.c_str(), collection_rna_path.c_str(), quoted_name, quoted_name_size))
2630 {
2631 return false;
2632 }
2633 if (quoted_name != data_name) {
2634 return false;
2635 }
2636
2637 return true;
2638}
2639
2641 const slot_handle_t slot_handle,
2642 FunctionRef<bool(const FCurve &fcurve)> predicate)
2643{
2644 BLI_assert(act);
2645
2646 Vector<FCurve *> found;
2647
2648 foreach_fcurve_in_action_slot(act->wrap(), slot_handle, [&](FCurve &fcurve) {
2649 if (predicate(fcurve)) {
2650 found.append(&fcurve);
2651 }
2652 });
2653
2654 return found;
2655}
2656
2658 FunctionRef<bool(const FCurve &fcurve)> predicate)
2659{
2660 Vector<FCurve *> found;
2661
2662 for (FCurve *fcurve : fcurves) {
2663 if (predicate(*fcurve)) {
2664 found.append(fcurve);
2665 }
2666 }
2667
2668 return found;
2669}
2670
2672 FunctionRef<bool(const FCurve &fcurve)> predicate)
2673{
2674 Vector<FCurve *> found;
2675
2676 for (FCurve &fcurve : fcurves) {
2677 if (predicate(fcurve)) {
2678 found.append(&fcurve);
2679 }
2680 }
2681
2682 return found;
2683}
2684
2686 bAction *act,
2687 PointerRNA *ptr,
2688 const FCurveDescriptor &fcurve_descriptor)
2689{
2690 if (act == nullptr) {
2691 return nullptr;
2692 }
2693
2694 /* NOTE: for layered actions we require the following:
2695 *
2696 * - `ptr` is non-null.
2697 * - `ptr` has an `owner_id` that already uses `act`.
2698 *
2699 * This isn't for any principled reason, but rather is because adding
2700 * support for layered actions to this function was a fix to make Follow
2701 * Path animation work properly with layered actions (see PR #124353), and
2702 * those are the requirements the Follow Path code conveniently met.
2703 * Moreover those requirements were also already met by the other call sites
2704 * that potentially call this function with layered actions.
2705 *
2706 * Trying to puzzle out what "should" happen when these requirements don't
2707 * hold, or if this is even the best place to handle the layered action
2708 * cases at all, was leading to discussion of larger changes than made sense
2709 * to tackle at that point. */
2710 BLI_assert(ptr != nullptr);
2711 if (ptr == nullptr || ptr->owner_id == nullptr) {
2712 return nullptr;
2713 }
2714
2715 return &action_fcurve_ensure(bmain, *act, *ptr->owner_id, fcurve_descriptor);
2716}
2717
2719{
2720 Action &action = dna_action.wrap();
2721 BLI_assert(get_action(animated_id) == &action);
2722
2723 /* Ensure the id has an assigned slot. */
2724 Slot *slot = assign_action_ensure_slot_for_keying(action, animated_id);
2725 /* A nullptr here means the ID type is not animatable. But since the Action is already assigned,
2726 * it is certain that the ID is actually animatable. */
2727 BLI_assert(slot);
2728
2729 action.layer_keystrip_ensure();
2730
2732 StripKeyframeData &strip_data = action.layer(0)->strip(0)->data<StripKeyframeData>(action);
2733
2734 return strip_data.channelbag_for_slot_ensure(*slot);
2735}
2736
2738 bAction &dna_action,
2739 ID &animated_id,
2740 const FCurveDescriptor &fcurve_descriptor)
2741{
2742 Channelbag &channelbag = action_channelbag_ensure(dna_action, animated_id);
2743 return channelbag.fcurve_ensure(bmain, fcurve_descriptor);
2744}
2745
2747{
2748 if (action_fcurve_detach(action, fcu)) {
2749 BKE_fcurve_free(&fcu);
2750 return true;
2751 }
2752
2753 return false;
2754}
2755
2756bool action_fcurve_detach(Action &action, FCurve &fcurve_to_detach)
2757{
2758 for (Layer *layer : action.layers()) {
2759 for (Strip *strip : layer->strips()) {
2760 if (!(strip->type() == Strip::Type::Keyframe)) {
2761 continue;
2762 }
2763 StripKeyframeData &strip_data = strip->data<StripKeyframeData>(action);
2764 for (Channelbag *bag : strip_data.channelbags()) {
2765 const bool is_detached = bag->fcurve_detach(fcurve_to_detach);
2766 if (is_detached) {
2767 return true;
2768 }
2769 }
2770 }
2771 }
2772 return false;
2773}
2774
2776 const slot_handle_t action_slot,
2777 FCurve &fcurve_to_attach,
2778 std::optional<StringRefNull> group_name)
2779{
2780 Slot *slot = action.slot_for_handle(action_slot);
2781 BLI_assert(slot);
2782 if (!slot) {
2783 printf("Cannot find slot handle %d on Action %s, unable to attach F-Curve %s[%d] to it!\n",
2784 action_slot,
2785 action.id.name + 2,
2786 fcurve_to_attach.rna_path().c_str(),
2787 fcurve_to_attach.array_index);
2788 return;
2789 }
2790
2791 action.layer_keystrip_ensure();
2792 StripKeyframeData &strip_data = action.layer(0)->strip(0)->data<StripKeyframeData>(action);
2793 Channelbag &cbag = strip_data.channelbag_for_slot_ensure(*slot);
2794 cbag.fcurve_append(fcurve_to_attach);
2795
2796 if (group_name) {
2797 bActionGroup &group = cbag.channel_group_ensure(*group_name);
2798 cbag.fcurve_assign_to_channel_group(fcurve_to_attach, group);
2799 }
2800}
2801
2803 const slot_handle_t action_slot_dst,
2804 Action &action_src,
2805 FCurve &fcurve)
2806{
2807 /* Store the group name locally, as the group will be removed if this was its
2808 * last F-Curve. */
2809 std::optional<std::string> group_name;
2810 if (fcurve.grp) {
2811 group_name = fcurve.grp->name;
2812 }
2813
2814 const bool is_detached = action_fcurve_detach(action_src, fcurve);
2815 BLI_assert(is_detached);
2816 UNUSED_VARS_NDEBUG(is_detached);
2817
2818 action_fcurve_attach(action_dst, action_slot_dst, fcurve, group_name);
2819}
2820
2821void channelbag_fcurves_move(Channelbag &channelbag_dst, Channelbag &channelbag_src)
2822{
2823 while (!channelbag_src.fcurves().is_empty()) {
2824 FCurve &fcurve = *channelbag_src.fcurve(0);
2825
2826 /* Store the group name locally, as the group will be removed if this was its
2827 * last F-Curve. */
2828 std::optional<std::string> group_name;
2829 if (fcurve.grp) {
2830 group_name = fcurve.grp->name;
2831 }
2832
2833 const bool is_detached = channelbag_src.fcurve_detach(fcurve);
2834 BLI_assert(is_detached);
2835 UNUSED_VARS_NDEBUG(is_detached);
2836
2837 channelbag_dst.fcurve_append(fcurve);
2838
2839 if (group_name) {
2840 bActionGroup &group = channelbag_dst.channel_group_ensure(*group_name);
2841 channelbag_dst.fcurve_assign_to_channel_group(fcurve, group);
2842 }
2843 }
2844}
2845
2847{
2848 if (this->channel_groups().first_index_try(&to_group) == -1) {
2849 return false;
2850 }
2851
2852 const int fcurve_index = this->fcurves().first_index_try(&fcurve);
2853 if (fcurve_index == -1) {
2854 return false;
2855 }
2856
2857 if (fcurve.grp == &to_group) {
2858 return true;
2859 }
2860
2861 /* Remove fcurve from old group, if it belongs to one. */
2862 if (fcurve.grp != nullptr) {
2864 if (fcurve.grp->fcurve_range_length == 0) {
2865 const int group_index = this->channel_groups().first_index_try(fcurve.grp);
2866 this->channel_group_remove_raw(group_index);
2867 }
2868 this->restore_channel_group_invariants();
2869 }
2870
2872 this->fcurve_array_num,
2873 fcurve_index,
2874 fcurve_index + 1,
2875 to_group.fcurve_range_start + to_group.fcurve_range_length);
2876 to_group.fcurve_range_length++;
2877
2878 this->restore_channel_group_invariants();
2879
2880 return true;
2881}
2882
2884{
2885 const int fcurve_index = this->fcurves().first_index_try(&fcurve);
2886 if (fcurve_index == -1) {
2887 return false;
2888 }
2889
2890 if (fcurve.grp == nullptr) {
2891 return true;
2892 }
2893
2894 bActionGroup *old_group = fcurve.grp;
2895
2897 this->fcurve_array_num,
2898 fcurve_index,
2899 fcurve_index + 1,
2900 this->fcurve_array_num - 1);
2901
2902 old_group->fcurve_range_length--;
2903 if (old_group->fcurve_range_length == 0) {
2904 const int old_group_index = this->channel_groups().first_index_try(old_group);
2905 this->channel_group_remove_raw(old_group_index);
2906 }
2907
2908 this->restore_channel_group_invariants();
2909
2910 return true;
2911}
2912
2914 Action &action,
2915 const slot_handle_t slot_handle,
2916 ID *primary_id)
2917{
2918 Slot *slot = action.slot_for_handle(slot_handle);
2919 if (slot == nullptr) {
2920 return nullptr;
2921 }
2922
2923 Span<ID *> users = slot->users(bmain);
2924 if (users.size() == 1) {
2925 /* We only do this for `users.size() == 1` and not `users.size() >= 1`
2926 * because when there's more than one user it's ambiguous which user we
2927 * should return, and that would be unpredictable for end users of Blender.
2928 * We also expect that to be a corner case anyway. So instead we let that
2929 * case either get disambiguated by the primary ID in the case below, or
2930 * return null. */
2931 return users[0];
2932 }
2933 if (users.contains(primary_id)) {
2934 return primary_id;
2935 }
2936
2937 return nullptr;
2938}
2939
2940ID *action_slot_get_id_best_guess(Main &bmain, Slot &slot, ID *primary_id)
2941{
2942 Span<ID *> users = slot.users(bmain);
2943 if (users.is_empty()) {
2944 return nullptr;
2945 }
2946 if (users.contains(primary_id)) {
2947 return primary_id;
2948 }
2949 return users[0];
2950}
2951
2953{
2954 const Action &action = dna_action.wrap();
2955 if (action.slot_array_num == 0) {
2956 return Slot::unassigned;
2957 }
2958 return action.slot_array[0]->handle;
2959}
2960
2962{
2963 if (action.layers().is_empty()) {
2964 return;
2965 }
2966 BLI_assert(action.layers().size() == 1);
2967
2969}
2970
2972{
2973 if (layer.strips().is_empty()) {
2974 return;
2975 }
2976 BLI_assert(layer.strips().size() == 1);
2977
2979}
2980
2982{
2983 UNUSED_VARS_NDEBUG(strip);
2985 BLI_assert(strip.is_infinite());
2986 BLI_assert(strip.frame_offset == 0.0);
2987}
2988
2994static void clone_slot(const Slot &from, Slot &to)
2995{
2996 SlotRuntime *runtime = to.runtime;
2997 slot_handle_t handle = to.handle;
2998 *reinterpret_cast<ActionSlot *>(&to) = *reinterpret_cast<const ActionSlot *>(&from);
2999 to.runtime = runtime;
3000 to.handle = handle;
3001}
3002
3003void move_slot(Main &bmain, Slot &source_slot, Action &from_action, Action &to_action)
3004{
3005 BLI_assert(from_action.slots().contains(&source_slot));
3006 BLI_assert(&from_action != &to_action);
3007
3008 /* No merging of strips or layers is handled. All data is put into the assumed single strip. */
3011
3012 Slot &target_slot = to_action.slot_add();
3013 clone_slot(source_slot, target_slot);
3014 slot_identifier_ensure_unique(to_action, target_slot);
3015
3016 if (!from_action.layers().is_empty() && !from_action.layer(0)->strips().is_empty()) {
3017 StripKeyframeData &from_strip_data = from_action.layer(0)->strip(0)->data<StripKeyframeData>(
3018 from_action);
3019 Channelbag *channelbag = from_strip_data.channelbag_for_slot(source_slot.handle);
3020 /* It's perfectly fine for a slot to not have a channelbag on each keyframe strip. */
3021 if (channelbag) {
3022 /* Only create the layer & keyframe strip if there is a channelbag to move
3023 * into it. Otherwise it's better to keep the Action lean, and defer their
3024 * creation when keys are inserted. */
3025 to_action.layer_keystrip_ensure();
3026 StripKeyframeData &to_strip_data = to_action.layer(0)->strip(0)->data<StripKeyframeData>(
3027 to_action);
3028 channelbag->slot_handle = target_slot.handle;
3030 &to_strip_data.channelbag_array, &to_strip_data.channelbag_array_num, channelbag);
3031 const int index = from_strip_data.find_channelbag_index(*channelbag);
3033 &from_strip_data.channelbag_array, &from_strip_data.channelbag_array_num, index);
3034 }
3035 }
3036
3037 /* Reassign all users of `source_slot` to the action `to_action` and the slot `target_slot`. */
3038 for (ID *user : source_slot.users(bmain)) {
3039 const auto assign_other_action = [&](ID & /* animated_id */,
3040 bAction *&action_ptr_ref,
3041 slot_handle_t &slot_handle_ref,
3042 char *slot_identifier) -> bool {
3043 /* Only reassign if the reference is actually from the same action. Could be from a different
3044 * action when using the NLA or action constraints. */
3045 if (action_ptr_ref != &from_action) {
3046 return true;
3047 }
3048
3049 { /* Assign the Action. */
3050 const bool assign_ok = generic_assign_action(
3051 *user, &to_action, action_ptr_ref, slot_handle_ref, slot_identifier);
3052 BLI_assert_msg(assign_ok, "Expecting slotted Actions to always be assignable");
3053 UNUSED_VARS_NDEBUG(assign_ok);
3054 }
3055 { /* Assign the Slot. */
3057 &target_slot, *user, action_ptr_ref, slot_handle_ref, slot_identifier);
3060 }
3061
3062 /* TODO: move the tagging of animated IDs into generic_assign_action() and
3063 * generic_assign_action_slot(), as that's closer to the modification of
3064 * the animated ID.
3065 *
3066 * This line was added here for now, to fix #136388 with minimal impact on
3067 * other code, so that the fix can be easily back-ported to Blender 4.4. */
3069 return true;
3070 };
3071 foreach_action_slot_use_with_references(*user, assign_other_action);
3072 }
3073
3074 from_action.slot_remove(source_slot);
3075}
3076
3077Slot &duplicate_slot(Action &action, const Slot &slot)
3078{
3079 BLI_assert(action.slots().contains(const_cast<Slot *>(&slot)));
3080
3081 /* Duplicate the slot itself. */
3082 Slot &cloned_slot = action.slot_add();
3083 clone_slot(slot, cloned_slot);
3084 slot_identifier_ensure_unique(action, cloned_slot);
3085
3086 /* Duplicate each Channelbag for the source slot. */
3087 for (int i = 0; i < action.strip_keyframe_data_array_num; i++) {
3088 StripKeyframeData &strip_data = action.strip_keyframe_data_array[i]->wrap();
3089 strip_data.slot_data_duplicate(slot.handle, cloned_slot.handle);
3090 }
3091
3092 /* The ID has changed, and so it needs to be re-evaluated. Animation does not
3093 * have to be flushed since nothing is using this slot yet. */
3095
3096 return cloned_slot;
3097}
3098
3099} // namespace animrig
3100} // namespace blender
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
Functions for backward compatibility with the legacy Action API.
Functions to work with AnimData.
Functions to modify FCurves.
Blender kernel action and pose functionality.
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:715
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:708
#define STRNCPY_UTF8(dst, src)
#define DATA_(msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define MAXFRAMEF
#define MINAFRAMEF
Read Blender memory allocation and freeing.
Internal C++ functions to deal with Actions, Slots, and their runtime data.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define MEM_realloc_uninitialized(vmemh, len)
btScalar max
void reserve(const int64_t n)
Definition BLI_set.hh:656
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
constexpr const char * c_str() const
constexpr StringRef()
int64_t remove_if(Predicate &&predicate)
void append(const T &value)
void resize(const int64_t new_size)
void slot_active_set(slot_handle_t slot_handle)
Slot & slot_add_for_id(const ID &animated_id)
float2 get_frame_range_of_slot(slot_handle_t slot_handle) const ATTR_WARN_UNUSED_RESULT
Span< const Slot * > slots() const
void slot_display_name_define(Slot &slot, StringRefNull new_display_name)
float2 get_frame_range() const ATTR_WARN_UNUSED_RESULT
bool is_cyclic() const ATTR_WARN_UNUSED_RESULT
int strip_keyframe_data_append(StripKeyframeData *strip_data)
void slot_identifier_propagate(Main &bmain, const Slot &slot)
const Layer * layer(int64_t index) const
void slot_identifier_set(Main &bmain, Slot &slot, StringRefNull new_identifier)
int64_t find_slot_index(const Slot &slot) const
const Slot * slot(int64_t index) const
void slot_idtype_define(Slot &slot, ID_Type idtype)
void slot_move_to_index(Slot &slot, int to_slot_index)
bool has_keyframes(slot_handle_t action_slot_handle) const ATTR_WARN_UNUSED_RESULT
void slot_display_name_set(Main &bmain, Slot &slot, StringRefNull new_display_name)
void strip_keyframe_data_remove_if_unused(int index)
int64_t find_layer_index(const Layer &layer) const
Span< const Layer * > layers() const
Slot & slot_add_for_id_type(ID_Type idtype)
bool layer_remove(Layer &layer_to_remove)
Slot * slot_find_by_identifier(StringRefNull slot_identifier)
float2 get_frame_range_of_keys(bool include_modifiers) const ATTR_WARN_UNUSED_RESULT
void slot_identifier_define(Slot &slot, StringRefNull new_identifier)
bool slot_remove(Slot &slot_to_remove)
Slot * slot_for_handle(slot_handle_t handle)
bool has_single_frame() const ATTR_WARN_UNUSED_RESULT
Span< const StripKeyframeData * > strip_keyframe_data() const
bool is_slot_animated(slot_handle_t slot_handle) const
void slot_setup_for_id(Slot &slot, const ID &animated_id)
Layer & layer_add(std::optional< StringRefNull > name)
bool channel_group_remove(bActionGroup &group)
const FCurve * fcurve(int64_t index) const
void fcurve_remove_by_index(int64_t fcurve_index)
bool fcurve_detach(FCurve &fcurve_to_detach)
void fcurve_move_to_index(FCurve &fcurve, int to_fcurve_index)
bool fcurve_assign_to_channel_group(FCurve &fcurve, bActionGroup &to_group)
FCurve & fcurve_clone(const FCurve &old_fcurve, StringRefNull new_path, int new_array_index, StringRef new_group_name)
Span< const bActionGroup * > channel_groups() const
bActionGroup & channel_group_create(StringRefNull name)
bool fcurve_remove(FCurve &fcurve_to_remove)
const bActionGroup * channel_group(int64_t index) const
FCurve & fcurve_create(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
const bActionGroup * channel_group_find(StringRef name) const
Span< const FCurve * > fcurves() const
Vector< FCurve * > fcurve_create_many(Main *bmain, Span< FCurveDescriptor > fcurve_descriptors)
void fcurve_detach_by_index(int64_t fcurve_index)
int channel_group_find_index(const bActionGroup *group) const
FCurve & fcurve_ensure(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
FCurve * fcurve_create_unique(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
const FCurve * fcurve_find(const FCurveDescriptor &fcurve_descriptor) const
void channel_group_move_to_index(bActionGroup &group, int to_group_index)
int channel_group_containing_index(int fcurve_array_index)
bActionGroup & channel_group_ensure(StringRefNull name)
bool strip_remove(Action &owning_action, Strip &strip)
Span< const Strip * > strips() const
Layer * duplicate_with_shallow_strip_copies(StringRefNull allocation_name) const
const Strip * strip(int64_t index) const
Strip & strip_add(Action &owning_action, Strip::Type strip_type)
int64_t find_strip_index(const Strip &strip) const
static void users_invalidate(Main &bmain)
void users_add(ID &animated_id)
bool is_suitable_for(const ID &animated_id) const
std::string idtype_string() const
static constexpr int identifier_length_min
static constexpr int identifier_length_max
Span< ID * > users(Main &bmain) const
static constexpr slot_handle_t unassigned
StringRefNull identifier_without_prefix() const
void users_remove(ID &animated_id)
const Channelbag * channelbag_for_slot(const Slot &slot) const
int64_t find_channelbag_index(const Channelbag &channelbag) const
void slot_data_remove(slot_handle_t slot_handle)
void slot_data_duplicate(slot_handle_t source_slot_handle, slot_handle_t target_slot_handle)
static constexpr Strip::Type TYPE
Channelbag & channelbag_for_slot_add(const Slot &slot)
Span< const Channelbag * > channelbags() const
SingleKeyingResult keyframe_insert(Main *bmain, const Slot &slot, const FCurveDescriptor &fcurve_descriptor, float2 time_value, const KeyframeSettings &settings, eInsertKeyFlags insert_key_flags=INSERTKEY_NOFLAGS, std::optional< float2 > cycle_range=std::nullopt)
Channelbag & channelbag_for_slot_ensure(const Slot &slot)
bool channelbag_remove(Channelbag &channelbag_to_remove)
const Channelbag * channelbag(int64_t index) const
bool contains_frame(float frame_time) const
void resize(float frame_start, float frame_end)
static Strip & create(Action &owning_action, const Strip::Type type)
bool is_last_frame(float frame_time) const
const T & data(const Action &owning_action) const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
#define layer
float length(VecOp< float, D >) RET
#define T
void rebuild_slot_user_cache(Main &bmain)
Vector< const FCurve * > fcurves_all(const bAction *action)
void action_fcurve_attach(Action &action, slot_handle_t action_slot, FCurve &fcurve_to_attach, std::optional< StringRefNull > group_name)
slot_handle_t first_slot_handle(const bAction &dna_action)
void foreach_fcurve_in_action_slot(Action &action, slot_handle_t handle, FunctionRef< void(FCurve &fcurve)> callback)
bool action_fcurve_remove(Action &action, FCurve &fcu)
static void shrink_array_and_swap_remove(T **array, int *num, const int index)
void assert_baklava_phase_1_invariants(const Action &action)
static void strip_ptr_destructor(ActionStrip **dna_strip_ptr)
bool fcurve_matches_collection_path(const FCurve &fcurve, StringRefNull collection_rna_path, StringRefNull data_name)
decltype(blender::ActionSlot::handle) slot_handle_t
static bool is_id_using_action_slot(const ID &animated_id, const Action &action, const slot_handle_t slot_handle)
static animrig::Layer & ActionLayer_alloc()
void channelbag_fcurves_move(Channelbag &channelbag_dst, Channelbag &channelbag_src)
static void slot_ptr_destructor(ActionSlot **dna_slot_ptr)
static void array_shift_range(T *array, const int num, const int range_start, const int range_end, const int to)
Vector< FCurve * > fcurves_in_action_slot_filtered(bAction *act, slot_handle_t slot_handle, FunctionRef< bool(const FCurve &fcurve)> predicate)
Slot * assign_action_ensure_slot_for_keying(Action &action, ID &animated_id)
bool foreach_action_slot_use(const ID &animated_id, FunctionRef< bool(Action &action, slot_handle_t slot_handle)> callback)
FCurve * action_fcurve_ensure_ex(Main *bmain, bAction *act, PointerRNA *ptr, const FCurveDescriptor &fcurve_descriptor)
FCurve * create_fcurve_for_channel(const FCurveDescriptor &fcurve_descriptor)
void action_fcurve_move(Action &action_dst, slot_handle_t action_slot_dst, Action &action_src, FCurve &fcurve)
void update_autoflags_fcurve_direct(FCurve *fcu, PropertyType prop_type)
Action & action_add(Main &bmain, StringRefNull name)
const animrig::Channelbag * channelbag_for_action_slot(const Action &action, slot_handle_t slot_handle)
ActionSlotAssignmentResult assign_tmpaction_and_slot_handle(bAction *action, slot_handle_t slot_handle, OwnedAnimData owned_adt)
static void layer_ptr_destructor(ActionLayer **dna_layer_ptr)
static void cyclic_keying_ensure_modifier(FCurve &fcurve)
Slot & duplicate_slot(Action &action, const Slot &slot)
static void grow_array_and_append(T **array, int *num, T item)
Vector< FCurve * > fcurves_in_span_filtered(Span< FCurve * > fcurves, FunctionRef< bool(const FCurve &fcurve)> predicate)
bool generic_assign_action(ID &animated_id, bAction *action_to_assign, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
static void clone_slot(const Slot &from, Slot &to)
static float2 get_frame_range_of_fcurves(Span< const FCurve * > fcurves, bool include_modifiers)
static void cyclic_keying_ensure_cycle_range_exists(FCurve &fcurve, const float2 cycle_range)
ID * action_slot_get_id_best_guess(Main &bmain, Slot &slot, ID *primary_id)
ActionSlotAssignmentResult generic_assign_action_slot(Slot *slot_to_assign, ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
ActionSlotAssignmentResult assign_action_and_slot(Action *action, Slot *slot_to_assign, ID &animated_id)
Vector< FCurve * > fcurves_in_listbase_filtered(ListBaseT< FCurve > fcurves, FunctionRef< bool(const FCurve &fcurve)> predicate)
Vector< animrig::Channelbag * > channelbags_for_action_slot(Action &action, slot_handle_t slot_handle)
Channelbag & action_channelbag_ensure(bAction &dna_action, ID &animated_id)
SingleKeyingResult insert_vert_fcurve(FCurve *fcu, const float2 position, const KeyframeSettings &settings, eInsertKeyFlags flag)
Main Key-framing API call.
Span< FCurve * > fcurves_for_action_slot(Action &action, slot_handle_t slot_handle)
ID * action_slot_get_id_for_keying(Main &bmain, Action &action, slot_handle_t slot_handle, ID *primary_id)
static void shrink_array_and_remove(T **array, int *num, const int index)
static void grow_array_and_insert(T **array, int *num, const int index, T item)
const FCurve * fcurve_find(Span< const FCurve * > fcurves, const FCurveDescriptor &fcurve_descriptor)
Action * get_action(ID &animated_id)
bool assign_tmpaction(bAction *action, OwnedAnimData owned_adt)
static void fcurve_ptr_noop_destructor(FCurve **)
static void fcurve_ptr_destructor(FCurve **fcurve_ptr)
bool unassign_action(ID &animated_id)
bool action_fcurve_detach(Action &action, FCurve &fcurve_to_detach)
bool assign_action(bAction *action, ID &animated_id)
FCurve * fcurve_find_in_assigned_slot(AnimData &adt, const FCurveDescriptor &fcurve_descriptor)
std::optional< std::pair< Action *, Slot * > > get_action_slot_pair(ID &animated_id)
static void grow_array(T **array, int *num, const int add_num)
FCurve & action_fcurve_ensure(Main *bmain, bAction &action, ID &animated_id, const FCurveDescriptor &fcurve_descriptor)
bool key_insertion_may_create_fcurve(eInsertKeyFlags insert_key_flags)
FCurve * fcurve_find_in_action(bAction *act, const FCurveDescriptor &fcurve_descriptor)
static void shrink_array(T **array, int *num, const int shrink_num)
static void channelbag_ptr_destructor(ActionChannelbag **dna_channelbag_ptr)
ActionSlotAssignmentResult generic_assign_action_slot_handle(slot_handle_t slot_handle_to_assign, ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
ActionSlotAssignmentResult assign_action_slot(Slot *slot_to_assign, ID &animated_id)
bool foreach_action_slot_use_with_references(ID &animated_id, FunctionRef< bool(ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *last_slot_identifier)> callback)
void move_slot(Main &bmain, Slot &slot, Action &from_action, Action &to_action)
Slot * generic_slot_for_autoassign(const ID &animated_id, Action &action, StringRefNull last_slot_identifier)
static void slot_identifier_ensure_unique(Action &action, Slot &slot)
FCurve * fcurve_find_in_action_slot(bAction *act, slot_handle_t slot_handle, const FCurveDescriptor &fcurve_descriptor)
bool operator==(const Error &left, const Error &right)
void remove_index(T **items, int *items_num, int *active_index, const int index, void(*destruct_item)(T *))
void clear(T **items, int *items_num, int *active_index, void(*destruct_item)(T *))
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool BKE_animdata_action_editable(const AnimData *adt)
Definition anim_data.cc:164
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
bool BKE_fcurve_calc_range(const FCurve *fcu, float *r_min, float *r_max, bool selected_keys_only)
bool bool BLI_str_quoted_substr(const char *__restrict str, const char *__restrict prefix, char *result, size_t result_maxncpy)
bool BKE_fcurve_is_keyframable(const FCurve &fcu)
void id_us_plus(ID *id)
Definition lib_id.cc:433
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bAction * BKE_action_add(Main *bmain, const char name[])
FModifier * add_fmodifier(ListBaseT< FModifier > *modifiers, int type, FCurve *owner_fcu)
bool id_can_have_animdata(const ID *id)
Definition anim_data.cc:78
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
@ FMODIFIER_TYPE_LIMITS
@ FMODIFIER_TYPE_CYCLES
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
@ FCM_EXTRAPOLATE_NONE
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
unsigned char uint8_t
Definition BKE_text.h:79
FCurve * BKE_fcurve_copy(const FCurve *fcu)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
ATTR_WARN_UNUSED_RESULT const size_t num
void BKE_fcurve_free(FCurve *fcu)
void _DNA_internal_memzero(void *dst, size_t size)
Definition dna_utils.cc:271
MINLINE float max_ff(float a, float b)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static CLG_LogRef LOG
Definition logging.cpp:12
void DEG_relations_tag_update(Main *bmain)
void uninitialized_relocate_n(T *src, int64_t n, T *dst)
void id_us_min(ID *id)
Definition lib_id.cc:441
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
MINLINE int compare_ff(float a, float b, float max_diff)
signed char int8_t
Definition BKE_text.h:76
void uninitialized_move_n(T *src, int64_t n, T *dst)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define hash
Definition noise_c.cc:156
#define floorf
#define min(a, b)
Definition sort.cc:38
struct bActionGroup ** group_array
struct ActionStrip ** strip_array
animrig::SlotRuntime * runtime
struct ActionChannelbag ** channelbag_array
char tmp_last_slot_identifier[258]
char last_slot_identifier[258]
bActionGroup * grp
eFCurve_Flags flag
ListBaseT< FModifier > modifiers
unsigned int totvert
eFMod_Cycling_Modes after_mode
eFMod_Cycling_Modes before_mode
eFMod_Limit_Flags flag
eFModifier_Types type
char name[258]
Definition DNA_ID.h:433
bool is_action_slot_to_id_map_dirty
Definition BKE_main.hh:342
std::optional< StringRefNull > channel_group
std::optional< PropertyType > prop_type
struct ActionChannelbag * channelbag
struct ActionSlot ** slot_array
struct ActionStripKeyframeData ** strip_keyframe_data_array
ListBaseT< bActionGroup > groups
struct ActionLayer ** layer_array