Blender V5.3
animrig/intern/keyframing.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 <cmath>
10#include <string>
11
12#include <fmt/format.h>
13
14#include "ANIM_action.hh"
16#include "ANIM_animdata.hh"
17#include "ANIM_fcurve.hh"
18#include "ANIM_keyframing.hh"
19#include "ANIM_rna.hh"
20#include "ANIM_visualkey.hh"
21
22#include "BKE_action.hh"
23#include "BKE_anim_data.hh"
24#include "BKE_animsys.hh"
25#include "BKE_fcurve.hh"
26#include "BKE_idtype.hh"
27#include "BKE_lib_id.hh"
28#include "BKE_nla.hh"
29#include "BKE_report.hh"
30
31#include "DNA_scene_types.h"
32
33#include "BLI_math_base_c.hh"
34#include "BLI_task.hh"
35#include "BLI_utildefines.hh"
36#include "BLT_translation.hh"
37
38#include "DEG_depsgraph.hh"
40#include "DNA_anim_types.h"
41#include "MEM_guardedalloc.h"
42#include "RNA_access.hh"
43#include "RNA_path.hh"
44#include "RNA_prototypes.hh"
45
46#include "WM_types.hh"
47
48namespace blender::animrig {
49
51{
52 switch (result) {
54 BKE_reportf(reports, RPT_INFO, "Successfully inserted a key.");
55 break;
57 BKE_reportf(reports, RPT_ERROR, "Keyframe insertion failed for an unknown reason.");
58 break;
60 BKE_reportf(reports, RPT_ERROR, "Failed to create the F-Curve.");
61 break;
63 BKE_reportf(reports, RPT_ERROR, "The F-Curve is not keyable. It may be locked or sampled.");
64 break;
67 reports, RPT_ERROR, "Due to the setting 'Only Insert Needed' no keyframe was inserted.");
68 break;
70 BKE_reportf(reports, RPT_ERROR, "Due to the NLA stack setup, no key was inserted.");
71 break;
74 reports, RPT_ERROR, "Inserting key has been skipped because the ID cannot be edited.");
75 break;
78 reports, RPT_ERROR, "Inserting key has been skipped because the ID cannot be keyed.");
79 break;
81 BKE_reportf(reports, RPT_ERROR, "No valid layer. Cannot insert key.");
82 break;
84 BKE_reportf(reports, RPT_ERROR, "No valid strip. Cannot insert key.");
85 break;
87 BKE_reportf(reports, RPT_ERROR, "No valid slot. Cannot insert key.");
88 break;
90 BKE_reportf(reports, RPT_ERROR, "Invalid RNA path. Cannot insert key.");
91 break;
93 break;
94 }
95}
96
98{
99 result_counter.fill(0);
100}
101
103{
104 result_counter[int(result)] += count;
105}
106
108{
109 for (int i = 0; i < result_counter.size(); i++) {
110 result_counter[i] += other.result_counter[i];
111 }
112}
113
115{
116 return result_counter[int(result)];
117}
118
120{
121 /* For loop starts at 1 to skip the SUCCESS flag. Assumes that SUCCESS is 0 and the rest of the
122 * enum are sequential values. */
123 static_assert(int(SingleKeyingResult::SUCCESS) == 0);
124 for (int i = 1; i < result_counter.size(); i++) {
125 if (result_counter[i] > 0) {
126 return true;
127 }
128 }
129 return false;
130}
131
133{
134 if (!this->has_errors() && this->get_count(SingleKeyingResult::SUCCESS) == 0) {
136 reports, RPT_WARNING, "No keys have been inserted and no errors have been reported.");
137 return;
138 }
139
140 Vector<std::string> errors;
142 const int error_count = this->get_count(SingleKeyingResult::UNKNOWN_FAILURE);
143 errors.append(fmt::format(
144 fmt::runtime(RPT_("There were {:d} keying failures for unknown reasons.")), error_count));
145 }
146
148 const int error_count = this->get_count(SingleKeyingResult::CANNOT_CREATE_FCURVE);
149 errors.append(fmt::format(
150 fmt::runtime(RPT_("Could not create {:d} F-Curve(s). This can happen when only "
151 "inserting to available F-Curves.")),
152 error_count));
153 }
154
156 const int error_count = this->get_count(SingleKeyingResult::FCURVE_NOT_KEYFRAMEABLE);
157 errors.append(
158 fmt::format(fmt::runtime(RPT_(
159 "{:d} F-Curve(s) are not keyframeable. They might be locked or sampled.")),
160 error_count));
161 }
162
164 const int error_count = this->get_count(SingleKeyingResult::NO_KEY_NEEDED);
165 errors.append(fmt::format(
166 fmt::runtime(RPT_(
167 "Due to the setting 'Only Insert Needed', {:d} keyframe(s) have not been inserted.")),
168 error_count));
169 }
170
173 errors.append(fmt::format(
174 fmt::runtime(RPT_("Due to the NLA stack setup, {:d} keyframe(s) have not been inserted.")),
175 error_count));
176 }
177
179 const int error_count = this->get_count(SingleKeyingResult::ID_NOT_EDITABLE);
180 errors.append(fmt::format(
181 fmt::runtime(RPT_("Inserting keys on {:d} data-block(s) has been skipped because "
182 "they are not editable.")),
183 error_count));
184 }
185
187 const int error_count = this->get_count(SingleKeyingResult::ID_NOT_ANIMATABLE);
188 errors.append(fmt::format(
189 fmt::runtime(RPT_("Inserting keys on {:d} data-block(s) has been skipped because "
190 "they cannot be animated.")),
191 error_count));
192 }
193
195 const int error_count = this->get_count(SingleKeyingResult::CANNOT_RESOLVE_PATH);
196 errors.append(fmt::format(
197 fmt::runtime(RPT_("Inserting keys on {:d} data-block(s) has been skipped because "
198 "the RNA path wasn't valid for them.")),
199 error_count));
200 }
201
203 const int error_count = this->get_count(SingleKeyingResult::NO_VALID_LAYER);
204 errors.append(fmt::format(
205 fmt::runtime(RPT_("Inserting keys on {:d} data-block(s) has been skipped because "
206 "there were no layers that could accept the keys.")),
207 error_count));
208 }
209
211 const int error_count = this->get_count(SingleKeyingResult::NO_VALID_STRIP);
212 errors.append(fmt::format(
213 fmt::runtime(RPT_("Inserting keys on {:d} data-block(s) has been skipped because "
214 "there were no strips that could accept the keys.")),
215 error_count));
216 }
217
219 const int error_count = this->get_count(SingleKeyingResult::NO_VALID_SLOT);
220 errors.append(fmt::format(
221 fmt::runtime(RPT_("Inserting keys on {:d} data-block(s) has been skipped because "
222 "of missing action slots.")),
223 error_count));
224 }
225
226 if (errors.is_empty()) {
227 BKE_report(reports, RPT_WARNING, "Encountered unhandled error during keyframing");
228 return;
229 }
230
231 if (errors.size() == 1) {
232 BKE_report(reports, report_level, errors[0].c_str());
233 return;
234 }
235
236 std::string error_message = RPT_("Inserting keyframes failed:");
237 for (const std::string &error : errors) {
238 error_message.append(fmt::format("\n- {}", error));
239 }
240 BKE_report(reports, report_level, error_message.c_str());
241}
242
243std::optional<StringRefNull> default_channel_group_for_path(const PointerRNA *animated_struct,
244 const StringRef prop_rna_path)
245{
246 if (animated_struct->type == RNA_PoseBone) {
247 bPoseChannel *pose_channel = static_cast<bPoseChannel *>(animated_struct->data);
248 return pose_channel->name;
249 }
250
251 if (animated_struct->type == RNA_Object) {
252 if (prop_rna_path.find("location") != StringRef::not_found ||
253 prop_rna_path.find("rotation") != StringRef::not_found ||
254 prop_rna_path.find("scale") != StringRef::not_found)
255 {
256 /* NOTE: Keep this label in sync with the "ID" case in
257 * _keyingsets_utils.py :: get_transform_generators_base_info()
258 */
259 return "Object Transforms";
260 }
261 }
262
263 return std::nullopt;
264}
265
267{
268 /* First clear out all the flags that should be updated by this function, before setting just the
269 * ones suitable for this property type. */
271 fcu->flag |= fcurve_flags_for_property_type(prop_type);
272}
273
274bool is_keying_flag(const Scene *scene, const eKeying_Flag flag)
275{
276 if (scene) {
277 return (scene->toolsettings->keying_flag & flag) || (U.keying_flag & flag);
278 }
279 return U.keying_flag & flag;
280}
281
283{
285
286 /* Visual keying. */
288 flag |= INSERTKEY_MATRIX;
289 }
290
291 /* Cycle-aware keyframe insertion - preserve cycle period and flow. */
293 flag |= INSERTKEY_CYCLE_AWARE;
294 }
295
297 flag |= INSERTKEY_NEEDED;
298 }
299
300 return flag;
301}
302
310static bool assigned_action_has_keyframe_at(AnimData &adt, const float frame)
311{
312 if (adt.action == nullptr) {
313 return false;
314 }
315
316 if (adt.action->flag & ACT_MUTED) {
317 return false;
318 }
319
321 /* 1024 is a common value for memory bandwidth limited tasks. The number isn't critical: 512
322 * works fine here, but 128 and 4096 seem to work equally well in testing. */
324 fcurves.index_range(),
325 512,
326 false,
327 [&](const IndexRange range, const bool is_found) {
328 if (is_found) {
329 return true;
330 }
331 for (FCurve *fcu : fcurves.slice(range)) {
332 if (fcurve_frame_has_keyframe(fcu, frame)) {
333 return true;
334 }
335 }
336 return false;
337 },
338 std::logical_or<bool>());
339}
340
341/* Checks whether an Object has a keyframe for a given frame. */
342static bool object_frame_has_keyframe(Object *ob, const float frame)
343{
344 if (ob == nullptr) {
345 return false;
346 }
347
348 /* Check its own animation data - specifically, the action it contains. */
349 if ((ob->adt) && (ob->adt->action)) {
350 /* #41525 - When the active action is a NLA strip being edited,
351 * we need to correct the frame number to "look inside" the
352 * remapped action
353 */
354 const float ob_frame = BKE_nla_tweakedit_remap(ob->adt, frame, NLATIME_CONVERT_UNMAP);
355
356 if (assigned_action_has_keyframe_at(*ob->adt, ob_frame)) {
357 return true;
358 }
359 }
360
361 /* nothing found */
362 return false;
363}
364
365bool id_frame_has_keyframe(ID *id, float frame)
366{
367 if (id == nullptr) {
368 return false;
369 }
370
371 /* Perform special checks for 'macro' types. */
372 switch (GS(id->name)) {
373 case ID_OB:
374 return object_frame_has_keyframe(id_cast<Object *>(id), frame);
375
376 default: {
378
379 /* only check keyframes in active action */
380 if (adt) {
381 return assigned_action_has_keyframe_at(*adt, frame);
382 }
383 break;
384 }
385 }
386
387 return false;
388}
389
391{
392 return (insert_key_flags & (INSERTKEY_REPLACE | INSERTKEY_AVAILABLE)) == 0;
393}
394
395Vector<float> get_property_values(PointerRNA *ptr, PropertyRNA *prop, const bool visual_key)
396{
397 Vector<float> values;
398
399 if (visual_key && visualkey_can_use(ptr, prop)) {
400 /* Visual-keying is only available for object data-blocks and pose-channels,
401 * as it works by key-framing using a value extracted from the final matrix
402 * instead of using the kt system to extract a value. */
403 values = visualkey_get_values(ptr, prop);
404 }
405 else {
406 values = get_rna_values(ptr, prop);
407 }
408 return values;
409}
410
411static float nla_time_remap(float time,
412 const AnimationEvalContext *anim_eval_context,
413 PointerRNA *id_ptr,
414 AnimData *adt,
415 bAction *act,
416 ListBaseT<NlaKeyframingContext> *nla_cache,
417 NlaKeyframingContext **r_nla_context)
418{
419 if (adt && adt->action == act) {
421 nla_cache, id_ptr, adt, anim_eval_context);
422
423 const float remapped_frame = BKE_nla_tweakedit_remap(adt, time, NLATIME_CONVERT_UNMAP);
424 return remapped_frame;
425 }
426
427 *r_nla_context = nullptr;
428 return time;
429}
430
432 PropertyRNA &prop,
433 FCurve &fcu,
434 const float fcurve_frame,
435 const eBezTriple_KeyframeType keytype,
436 const eInsertKeyFlags flag)
437{
438 if (!ptr) {
441 }
442
443 if (!BKE_fcurve_is_keyframable(fcu)) {
445 }
446
447 /* Update F-Curve flags to ensure proper behavior for property type. */
449
450 const bool visual_keyframing = flag & INSERTKEY_MATRIX;
451 Vector<float> values = get_property_values(&ptr, &prop, visual_keyframing);
452
453 const int index = fcu.array_index;
454 if (index < 0 || index >= values.size()) {
455 /* Can only happen if the FCurve and PropertyRNA do not match which
456 * should never be the case. */
459 }
460
462 settings.keyframe_type = keytype;
463
464 return insert_vert_fcurve(&fcu, {fcurve_frame, values[index]}, settings, flag);
465}
466
467/* ************************************************** */
468/* KEYFRAME DELETION */
469
470/* Main Keyframing API call:
471 * Use this when validation of necessary animation data isn't necessary as it
472 * already exists. It will delete a keyframe at the current frame.
473 *
474 * The flag argument is used for special settings that alter the behavior of
475 * the keyframe deletion. These include the quick refresh options.
476 */
477
478static void deg_tag_after_keyframe_delete(Main *bmain, ID *id, AnimData *adt)
479{
480 if (adt->action == nullptr) {
481 /* In the case last f-curve was removed need to inform dependency graph
482 * about relations update, since it needs to get rid of animation operation
483 * for this data-block. */
486 }
487 else {
489 }
490}
491
492int delete_keyframe(Main *bmain, ReportList *reports, ID *id, const RNAPath &rna_path, float cfra)
493{
495
496 if (ELEM(nullptr, id, adt)) {
497 BKE_report(reports, RPT_ERROR, "No ID block and/or AnimData to delete keyframe from");
498 return 0;
499 }
500
501 PointerRNA ptr;
502 PropertyRNA *prop;
504 if (RNA_path_resolve_property(&id_ptr, rna_path.path.c_str(), &ptr, &prop) == false) {
506 reports,
507 RPT_ERROR,
508 "Could not delete keyframe, as RNA path is invalid for the given ID (ID = %s, path = %s)",
509 id->name,
510 rna_path.path.c_str());
511 return 0;
512 }
513
514 if (!adt->action) {
515 BKE_reportf(reports, RPT_ERROR, "No action to delete keyframes from for ID = %s", id->name);
516 return 0;
517 }
518 bAction *act = adt->action;
520 int array_index = rna_path.index.value_or(0);
521 int array_index_max = array_index + 1;
522
523 if (!rna_path.index.has_value()) {
524 array_index_max = RNA_property_array_length(&ptr, prop);
525 /* For single properties, increase max_index so that the property itself gets included,
526 * but don't do this for standard arrays since that can cause corruption issues
527 * (extra unused curves).
528 */
529 if (array_index_max == array_index) {
530 array_index_max++;
531 }
532 }
533
534 Action &action = act->wrap();
535 Vector<FCurve *> modified_fcurves;
536 /* Just being defensive in the face of the NLA shenanigans above. This
537 * probably isn't necessary, but it doesn't hurt. */
538 BLI_assert(adt->action == act && action.slot_for_handle(adt->slot_handle) != nullptr);
539
540 Span<FCurve *> fcurves = fcurves_for_action_slot(action, adt->slot_handle);
541 /* This loop's clause is copied from the pre-existing code for legacy
542 * actions below, to ensure behavioral consistency between the two code
543 * paths. In the future when legacy actions are removed, we can restructure
544 * it to be clearer. */
545 for (; array_index < array_index_max; array_index++) {
546 FCurve *fcurve = fcurve_find(fcurves, {rna_path.path, array_index});
547 if (fcurve == nullptr) {
548 continue;
549 }
550 if (fcurve_delete_keyframe_at_time(fcurve, cfra)) {
551 modified_fcurves.append(fcurve);
552 }
553 }
554
555 if (!modified_fcurves.is_empty()) {
556 for (FCurve *fcurve : modified_fcurves) {
557 if (BKE_fcurve_is_empty(fcurve)) {
558 animdata_fcurve_delete(adt, fcurve);
559 }
560 }
561 deg_tag_after_keyframe_delete(bmain, id, adt);
562 }
563
564 return modified_fcurves.size();
565}
566
567/* ************************************************** */
568/* KEYFRAME CLEAR */
569
570int clear_keyframe(Main *bmain, ReportList *reports, ID *id, const RNAPath &rna_path)
571{
573
574 if (ELEM(nullptr, id, adt)) {
575 BKE_report(reports, RPT_ERROR, "No ID block and/or AnimData to delete keyframe from");
576 return 0;
577 }
578
579 PointerRNA ptr;
580 PropertyRNA *prop;
582 if (RNA_path_resolve_property(&id_ptr, rna_path.path.c_str(), &ptr, &prop) == false) {
584 reports,
585 RPT_ERROR,
586 "Could not clear keyframe, as RNA path is invalid for the given ID (ID = %s, path = %s)",
587 id->name,
588 rna_path.path.c_str());
589 return 0;
590 }
591
592 if (!adt->action) {
593 BKE_reportf(reports, RPT_ERROR, "No action to delete keyframes from for ID = %s", id->name);
594 return 0;
595 }
596 bAction *act = adt->action;
597
598 Action &action = act->wrap();
599 int key_count = 0;
600
601 if (adt->slot_handle) {
602 Vector<FCurve *> fcurves;
604 if (rna_path.index.has_value() && rna_path.index.value() != fcurve.array_index) {
605 return;
606 }
607 if (rna_path.path != fcurve.rna_path()) {
608 return;
609 }
610 fcurves.append(&fcurve);
611 });
612
613 for (FCurve *fcu : fcurves) {
614 if (action_fcurve_remove(action, *fcu)) {
615 key_count++;
616 }
617 }
618 }
619
620 if (key_count) {
621 deg_tag_after_keyframe_delete(bmain, id, adt);
622 }
623
624 return key_count;
625}
626
631
633 Action &action,
634 Layer &layer,
635 const Slot &slot,
636 const std::string &rna_path,
637 PropertyRNA *prop,
638 const std::optional<StringRefNull> channel_group,
639 const KeyInsertData &key_data,
640 const KeyframeSettings &key_settings,
641 const eInsertKeyFlags insert_key_flags)
642{
644 BLI_assert(layer.strips().size() == 1);
645
646 const bool do_cyclic = (insert_key_flags & INSERTKEY_CYCLE_AWARE) && action.is_cyclic();
647
648 const PropertyType prop_type = RNA_property_type(prop);
649 const PropertySubType prop_subtype = RNA_property_subtype(prop);
650
651 Strip *strip = layer.strip(0);
652 return strip->data<StripKeyframeData>(action).keyframe_insert(
653 bmain,
654 slot,
655 {rna_path, key_data.array_index, prop_type, prop_subtype, channel_group},
656 key_data.position,
657 key_settings,
658 insert_key_flags,
659 do_cyclic ? std::optional(action.get_frame_range()) : std::nullopt);
660}
661
662static std::pair<Layer *, Slot *> prep_action_layer_for_keying(Action &action, ID &animated_id)
663{
665 ELEM(get_action(animated_id), &action, nullptr),
666 "The animated ID should not be using another Action than the one passed to this function");
667
668 Slot *slot = assign_action_ensure_slot_for_keying(action, animated_id);
670 slot,
671 "The conditions that would cause this Slot assignment to fail (such as the ID not being "
672 "animatible) should have been caught and handled by higher-level functions.");
673
674 action.layer_keystrip_ensure();
675
676 /* TODO: we currently assume this will always successfully find a layer.
677 * However, that may not be true in the future when we implement features like
678 * layer locking: if layers already exist, but they are all locked, then the
679 * default layer won't be added by the line above, but there also won't be any
680 * layers we can insert keys into. */
682 BLI_assert(layer != nullptr);
683
684 return std::make_pair(layer, slot);
685}
686
688 Main *bmain,
689 Action &action,
690 Layer &layer,
691 const Slot &slot,
692 PropertyRNA *prop,
693 const std::optional<StringRefNull> channel_group,
694 const std::string &rna_path,
695 const float frame,
696 const Span<float> values,
697 const eInsertKeyFlags insert_key_flags,
698 const KeyframeSettings &key_settings,
699 const BitSpan keying_mask)
700{
701 BLI_assert(bmain != nullptr);
702
703 int property_array_index = 0;
704 CombinedKeyingResult combined_result;
705 for (float value : values) {
706 if (!keying_mask[property_array_index]) {
708 property_array_index++;
709 continue;
710 }
711 const KeyInsertData key_data = {{frame, value}, property_array_index};
713 action,
714 layer,
715 slot,
716 rna_path,
717 prop,
718 channel_group,
719 key_data,
720 key_settings,
721 insert_key_flags);
722
723 combined_result.add(result);
724 property_array_index++;
725 }
726 return combined_result;
727}
728
730 PointerRNA *struct_pointer,
731 const std::optional<StringRefNull> channel_group,
732 const Span<RNAPath> rna_paths,
733 const std::optional<float> scene_frame,
734 const AnimationEvalContext &anim_eval_context,
735 const eBezTriple_KeyframeType key_type,
736 const eInsertKeyFlags insert_key_flags)
737
738{
739 ID *id = struct_pointer->owner_id;
740 PointerRNA id_pointer = RNA_id_pointer_create(id);
741 CombinedKeyingResult combined_result;
742
743 if (!BKE_id_is_editable(bmain, id)) {
744 combined_result.add(SingleKeyingResult::ID_NOT_EDITABLE, rna_paths.size());
745 return combined_result;
746 }
747
748 /* Init animdata if none available yet. */
750 if (adt == nullptr) {
752 return combined_result;
753 }
754
755 if ((adt->action == nullptr) && (insert_key_flags & INSERTKEY_AVAILABLE)) {
756 combined_result.add(SingleKeyingResult::CANNOT_CREATE_FCURVE, rna_paths.size());
757 return combined_result;
758 }
759
760 if (const bAction *action = adt->action) {
761 if (ID_IS_LINKED(action) || ID_IS_OVERRIDE_LIBRARY(action)) {
762 combined_result.add(SingleKeyingResult::ID_NOT_EDITABLE, rna_paths.size());
763 return combined_result;
764 }
765 }
766
767 bAction *dna_action = id_action_ensure(bmain, id);
768 BLI_assert(dna_action != nullptr);
769 Action &action = dna_action->wrap();
770
772 (insert_key_flags & INSERTKEY_NO_USERPREF) == 0);
773 key_settings.keyframe_type = key_type;
774
775 /* NOTE: keyframing functions can deal with the nla_context being a nullptr. */
776 ListBaseT<NlaKeyframingContext> nla_cache = {nullptr, nullptr};
777 NlaKeyframingContext *nla_context = nullptr;
778 const float nla_frame = nla_time_remap(scene_frame.value_or(anim_eval_context.eval_time),
779 &anim_eval_context,
780 &id_pointer,
781 adt,
782 dna_action,
783 &nla_cache,
784 &nla_context);
785 const bool visual_keyframing = insert_key_flags & INSERTKEY_MATRIX;
786
787 auto [layer, slot] = prep_action_layer_for_keying(action, *struct_pointer->owner_id);
788 for (const RNAPath &rna_path : rna_paths) {
789 PointerRNA ptr;
790 PropertyRNA *prop = nullptr;
791 const bool path_resolved = RNA_path_resolve_property(
792 struct_pointer, rna_path.path.c_str(), &ptr, &prop);
793 if (!path_resolved) {
795 continue;
796 }
797
798 Vector<float> rna_values = get_property_values(&ptr, prop, visual_keyframing);
799 BitVector<> rna_values_mask(rna_values.size(), false);
800 bool force_all;
801
802 /* NOTE: this function call is complex with interesting/non-obvious effects.
803 * Please see its documentation for details. */
805 &ptr,
806 prop,
807 rna_values.as_mutable_span(),
808 rna_path.index.value_or(-1),
809 &anim_eval_context,
810 &force_all,
811 rna_values_mask);
812
813 std::optional<std::string> rna_path_id_to_prop = RNA_path_from_ID_to_property(&ptr, prop);
814 if (!rna_path_id_to_prop.has_value()) {
815 /* In the case of nested RNA properties the path cannot be reconstructed in all cases. There
816 * may be a system in place in the future, see #122427. */
817 if (struct_pointer->data != id) {
818 continue;
819 }
820 /* However if the struct pointer happens to be an ID pointer we can use the path that was
821 * passed in. This fixes issues like #132195. */
822 rna_path_id_to_prop = rna_path.path;
823 }
824
825 /* Handle the `force_all` condition mentioned above, ensuring the
826 * "all-or-nothing" behavior if needed.
827 *
828 * TODO: this currently doesn't account for the "Only Insert Available"
829 * flag, which also needs to be accounted for to actually ensure
830 * all-or-nothing behavior. This is because the function this part of the
831 * code originally came from (see #122053) also didn't account for it.
832 * Presumably that was an oversight, and should be addressed. But for now
833 * we're faithfully reproducing the original behavior.
834 */
835 eInsertKeyFlags insert_key_flags_adjusted = insert_key_flags;
836 if (force_all && (insert_key_flags & (INSERTKEY_REPLACE | INSERTKEY_AVAILABLE))) {
837 /* Determine if at least one element would succeed getting keyed. */
838 bool at_least_one_would_succeed = false;
839 for (int i = 0; i < rna_values.size(); i++) {
840 const FCurve *fcu = fcurve_find_in_action(dna_action, {*rna_path_id_to_prop, i});
841 if (!fcu) {
842 continue;
843 }
844
845 /* We found an fcurve, and "Only Replace" is not on, so a key insertion
846 * would succeed according to the two flags we're accounting for. */
847 if (!(insert_key_flags & INSERTKEY_REPLACE)) {
848 at_least_one_would_succeed = true;
849 break;
850 }
851
852 /* "Only Replace" *is* on, so a key insertion would succeed only if we
853 * actually replace an existing keyframe. */
854 bool replace;
855 BKE_fcurve_bezt_binarysearch_index(fcu->bezt, nla_frame, fcu->totvert, &replace);
856 if (replace) {
857 at_least_one_would_succeed = true;
858 break;
859 }
860 }
861
862 /* If at least one would succeed, then we disable all keying flags that
863 * would prevent the other elements from getting keyed as well. */
864 if (at_least_one_would_succeed) {
865 insert_key_flags_adjusted &= ~(INSERTKEY_REPLACE | INSERTKEY_AVAILABLE);
866 }
867 }
868
870
871 const std::optional<StringRefNull> this_rna_path_channel_group =
872 channel_group.has_value() ? channel_group :
873 default_channel_group_for_path(&ptr, *rna_path_id_to_prop);
874
876 action,
877 *layer,
878 *slot,
879 prop,
880 this_rna_path_channel_group,
881 *rna_path_id_to_prop,
882 nla_frame,
883 rna_values,
884 insert_key_flags,
885 key_settings,
886 rna_values_mask);
887
888 combined_result.merge(result);
889 }
890
892
893 if (combined_result.get_count(SingleKeyingResult::SUCCESS) > 0) {
894 /* NOTE: this is NOT using ID_RECALC_ANIMATION on purpose, because that would be quite annoying
895 * in the following case:
896 *
897 * - Key Cube's loc/rot/scale.
898 * - Go to another frame.
899 * - Translate, rotate, and scale the cube.
900 * - Hover over the loc/rot/scale properties and one by one press 'I' to
901 * insert a key there.
902 *
903 * If ID_RECALC_ANIMATION were used, keying the location would immediately cause a flush of the
904 * animation data, popping the rotation and scale back to their animated values. */
906
907 /* TODO: it's not entirely clear why the action we got wouldn't be the same
908 * as the action in AnimData. Further, it's not clear why it would need to
909 * be tagged for a depsgraph update regardless. This code is here because it
910 * was part of the function this one was refactored from, but at some point
911 * this should be investigated and either documented or removed. */
912 if (!ELEM(adt->action, nullptr, dna_action)) {
914 }
915 }
916
917 return combined_result;
918}
919
920} // namespace blender::animrig
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
Functions to work with AnimData.
Functions to modify FCurves.
Functions to insert, delete or modify keyframes.
Helper functions for animation to interact with the RNA system.
Functions to work with the visual keying system.
Blender kernel action and pose functionality.
#define RPT_(msgid)
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
void append(const T &value)
bool is_empty() const
MutableSpan< T > as_mutable_span()
float2 get_frame_range() const ATTR_WARN_UNUSED_RESULT
bool is_cyclic() const ATTR_WARN_UNUSED_RESULT
Slot * slot_for_handle(slot_handle_t handle)
void merge(const CombinedKeyingResult &other)
int get_count(const SingleKeyingResult result) const
void generate_reports(ReportList *reports, eReportType report_level=RPT_ERROR)
void add(SingleKeyingResult result, int count=1)
const T & data(const Action &owning_action) const
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
#define layer
bool action_fcurve_remove(Action &action, FCurve &fcu)
void assert_baklava_phase_1_invariants(const Action &action)
std::optional< StringRefNull > default_channel_group_for_path(const PointerRNA *animated_struct, StringRef prop_rna_path)
void animdata_fcurve_delete(AnimData *adt, FCurve *fcu)
Definition animdata.cc:251
KeyframeSettings get_keyframe_settings(bool from_userprefs)
Slot * assign_action_ensure_slot_for_keying(Action &action, ID &animated_id)
static bool object_frame_has_keyframe(Object *ob, const float frame)
bAction * id_action_ensure(Main *bmain, ID *id)
Definition animdata.cc:195
eFCurve_Flags fcurve_flags_for_property_type(PropertyType prop_type)
Vector< float > visualkey_get_values(PointerRNA *ptr, PropertyRNA *prop)
Definition visualkey.cc:197
bool visualkey_can_use(PointerRNA *ptr, PropertyRNA *prop)
Definition visualkey.cc:37
void update_autoflags_fcurve_direct(FCurve *fcu, PropertyType prop_type)
Vector< float > get_property_values(PointerRNA *ptr, PropertyRNA *prop, bool visual_key)
Vector< float > get_rna_values(PointerRNA *ptr, PropertyRNA *prop)
Definition anim_rna.cc:26
int clear_keyframe(Main *bmain, ReportList *reports, ID *id, const RNAPath &rna_path)
int delete_keyframe(Main *bmain, ReportList *reports, ID *id, const RNAPath &rna_path, float cfra)
Main Delete Key-Framing API call.
bool fcurve_delete_keyframe_at_time(FCurve *fcurve, float time)
bool id_frame_has_keyframe(ID *id, float frame)
static bool assigned_action_has_keyframe_at(AnimData &adt, const float frame)
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)
const FCurve * fcurve_find(Span< const FCurve * > fcurves, const FCurveDescriptor &fcurve_descriptor)
Action * get_action(ID &animated_id)
SingleKeyingResult insert_keyframe_direct(PointerRNA &ptr, PropertyRNA &prop, FCurve &fcu, float fcurve_frame, eBezTriple_KeyframeType keytype, eInsertKeyFlags flag)
Secondary Insert Key-framing API call.
static SingleKeyingResult insert_key_layer(Main *bmain, Action &action, Layer &layer, const Slot &slot, const std::string &rna_path, PropertyRNA *prop, const std::optional< StringRefNull > channel_group, const KeyInsertData &key_data, const KeyframeSettings &key_settings, const eInsertKeyFlags insert_key_flags)
static CombinedKeyingResult insert_key_layered_action(Main *bmain, Action &action, Layer &layer, const Slot &slot, PropertyRNA *prop, const std::optional< StringRefNull > channel_group, const std::string &rna_path, const float frame, const Span< float > values, const eInsertKeyFlags insert_key_flags, const KeyframeSettings &key_settings, const BitSpan keying_mask)
static float nla_time_remap(float time, const AnimationEvalContext *anim_eval_context, PointerRNA *id_ptr, AnimData *adt, bAction *act, ListBaseT< NlaKeyframingContext > *nla_cache, NlaKeyframingContext **r_nla_context)
Span< FCurve * > fcurves_for_assigned_action(AnimData *adt)
Definition animdata.cc:349
bool is_keying_flag(const Scene *scene, eKeying_Flag flag)
static std::pair< Layer *, Slot * > prep_action_layer_for_keying(Action &action, ID &animated_id)
bool key_insertion_may_create_fcurve(eInsertKeyFlags insert_key_flags)
FCurve * fcurve_find_in_action(bAction *act, const FCurveDescriptor &fcurve_descriptor)
void foreach_fcurve_in_action_slot_editable(Action &action, slot_handle_t handle, FunctionRef< void(FCurve &fcurve)> callback)
static void deg_tag_after_keyframe_delete(Main *bmain, ID *id, AnimData *adt)
void generate_single_keying_result_report(const SingleKeyingResult result, ReportList *reports)
eInsertKeyFlags get_keyframing_flags(Scene *scene)
CombinedKeyingResult insert_keyframes(Main *bmain, PointerRNA *struct_pointer, std::optional< StringRefNull > channel_group, const Span< RNAPath > rna_paths, std::optional< float > scene_frame, const AnimationEvalContext &anim_eval_context, eBezTriple_KeyframeType key_type, eInsertKeyFlags insert_key_flags)
Main key-frame insertion API.
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
UserDef U
Definition blender.cc:58
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ RPT_WARNING
Definition BKE_report.hh:40
float BKE_nla_tweakedit_remap(AnimData *adt, float cframe, eNlaTime_ConvertModes mode)
void BKE_animsys_nla_remap_keyframe_values(NlaKeyframingContext *context, PointerRNA *prop_ptr, PropertyRNA *prop, const MutableSpan< float > values, int index, const AnimationEvalContext *anim_eval_context, bool *r_force_all, BitVector<> &r_values_mask)
Definition anim_sys.cc:3524
bool BKE_fcurve_is_keyframable(const FCurve &fcu)
void BKE_animsys_free_nla_keyframing_context_cache(ListBaseT< NlaKeyframingContext > *cache)
Definition anim_sys.cc:3648
PointerRNA RNA_id_pointer_create(ID *id)
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1237
@ NLATIME_CONVERT_UNMAP
Definition BKE_nla.hh:560
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
PropertyType RNA_property_type(PropertyRNA *prop)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
@ INSERTKEY_AVAILABLE
@ INSERTKEY_CYCLE_AWARE
@ INSERTKEY_NO_USERPREF
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
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
@ FCURVE_DISCRETE_VALUES
PropertySubType RNA_property_subtype(PropertyRNA *prop)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
NlaKeyframingContext * BKE_animsys_get_nla_keyframing_context(ListBaseT< NlaKeyframingContext > *cache, PointerRNA *ptr, AnimData *adt, const AnimationEvalContext *anim_eval_context)
Definition anim_sys.cc:3478
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void DEG_relations_tag_update(Main *bmain)
int BKE_fcurve_bezt_binarysearch_index(const BezTriple array[], float frame, int arraylen, bool *r_replace)
bool BKE_fcurve_is_empty(const FCurve *fcu)
@ MANUALKEY_FLAG_INSERTNEEDED
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
eFCurve_Flags flag
unsigned int totvert
struct AnimData * adt
StructRNA * type
Definition RNA_types.hh:63
struct ToolSettings * toolsettings
eBezTriple_KeyframeType keyframe_type