Blender V5.3
keyframes_general_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
8
9#include "BLI_listbase.hh"
10#include "BLI_string.hh"
11#include "BLI_string_utf8.hh"
12
13#include "BKE_armature.hh"
14#include "BKE_fcurve.hh"
15#include "BKE_gtest_base.hh"
16#include "BKE_idtype.hh"
17#include "BKE_lib_id.hh"
18#include "BKE_main.hh"
19#include "BKE_object.hh"
20
21#include "DNA_anim_types.h"
22#include "DNA_object_types.h"
23
24#include "ED_keyframes_edit.hh"
25
26namespace blender {
27
28using namespace blender::animrig;
29
31
32namespace {
33
34/* std::unique_ptr for FCurve. */
35struct fcurve_deleter {
36 void operator()(FCurve *fcurve) const
37 {
38 /* If this F-Curve was registered as "bone", remove it from that registration as well. */
39 keyframe_copy_buffer->bone_fcurves.remove(fcurve);
40
41 BKE_fcurve_free(fcurve);
42 }
43};
44using FCurvePtr = std::unique_ptr<FCurve, fcurve_deleter>;
45
50FCurvePtr fake_fcurve(const char *rna_path, const int array_index)
51{
52 FCurve *fcurve = BKE_fcurve_create();
53
54 if (rna_path) {
55 fcurve->rna_path_set(rna_path);
56 }
57 fcurve->array_index = array_index;
58
59 return FCurvePtr(fcurve);
60}
61
69FCurvePtr fake_fcurve_in_buffer(const char *rna_path,
70 const int array_index,
71 const bool is_bone,
72 const slot_handle_t slot_handle = Slot::unassigned,
73 ID *owner_id = nullptr)
74{
75 FCurvePtr fcurve_ptr = fake_fcurve(rna_path, array_index);
76
77 if (is_bone) {
78 keyframe_copy_buffer->bone_fcurves.add(fcurve_ptr.get());
79 }
80
81 if (owner_id) {
82 keyframe_copy_buffer->slot_animated_ids.add_overwrite(slot_handle, owner_id);
83 }
84 return fcurve_ptr;
85}
86
87} // namespace
88
94struct keyframes_paste : public testing::Test {
95 static void SetUpTestSuite()
96 {
99 }
100
101 static void TearDownTestSuite()
102 {
105 }
106};
107
109{
110 EXPECT_EQ(std::nullopt, flip_names("whatever")) << "not a bone prefix";
111
112 EXPECT_EQ("pose.bones[\"head\"]", flip_names("pose.bones[\"head\"]"))
113 << "unflippable name should remain unchanged";
114 EXPECT_EQ("pose.bones[\"Arm_L\"]", flip_names("pose.bones[\"Arm_R\"]"))
115 << "flippable name should be flipped";
116
117 EXPECT_EQ("pose.bones[\"Arm_L\"].rotation_euler",
118 flip_names("pose.bones[\"Arm_R\"].rotation_euler"))
119 << "flippable name should be flipped";
120}
121
123{
124 constexpr slot_handle_t unassigned = Slot::unassigned;
125
126 { /* NULL RNA paths. */
128 FCurvePtr fcurve_target = fake_fcurve(nullptr, 0);
129 FCurvePtr fcurve_in_buffer = fake_fcurve_in_buffer(nullptr, 0, false);
130
131 /* Little wrapper for #pastebuf_match_path_full() to make it easier to see
132 * the differences between the test-cases. */
133 auto call = [&](const bool from_single, const bool to_single, const bool flip) {
135 nullptr, *fcurve_target, *fcurve_in_buffer, unassigned, from_single, to_single, flip);
136 };
137
138 /* This only matches when `to_single` is true. */
139 EXPECT_FALSE(call(false, false, false));
140 EXPECT_FALSE(call(false, false, true));
141 EXPECT_TRUE(call(false, true, false));
142 EXPECT_TRUE(call(false, true, true));
143 EXPECT_FALSE(call(true, false, false));
144 EXPECT_FALSE(call(true, false, true));
145 EXPECT_TRUE(call(true, true, false));
146 EXPECT_TRUE(call(true, true, true));
147 }
148
149 { /* Many to many, no flipping. */
151 FCurvePtr fcurve = fake_fcurve("location", 0);
152
154 *fcurve,
155 *fake_fcurve_in_buffer("location", 0, false),
156 unassigned,
157 false,
158 false,
159 false));
161 *fcurve,
162 *fake_fcurve_in_buffer("location", 1, false),
163 unassigned,
164 false,
165 false,
166 false))
167 << "array index mismatch";
169 *fcurve,
170 *fake_fcurve_in_buffer("rotation_euler", 0, false),
171 unassigned,
172 false,
173 false,
174 false))
175 << "rna path mismatch";
176 }
177
178 /* Many to many, Flipping bone names. */
179 {
181 const bool from_single = false;
182 const bool to_single = false;
183 const bool flip = true;
184
185 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
186
188 nullptr,
189 *fcurve,
190 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 0, true),
191 unassigned,
192 from_single,
193 to_single,
194 flip))
195 << "original path match, is bone";
197 nullptr,
198 *fcurve,
199 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 0, true),
200 unassigned,
201 from_single,
202 to_single,
203 flip))
204 << "flipped path match, is bone";
205
207 nullptr,
208 *fcurve,
209 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 0, false),
210 unassigned,
211 from_single,
212 to_single,
213 flip))
214 << "flipped path match, is NOT bone";
216 nullptr,
217 *fcurve,
218 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 0, false),
219 unassigned,
220 from_single,
221 to_single,
222 flip))
223 << "original path match, is NOT bone";
224
226 *fcurve,
227 *fake_fcurve_in_buffer("location", 0, false),
228 unassigned,
229 from_single,
230 to_single,
231 flip))
232 << "rna path mismatch";
233
235 nullptr,
236 *fcurve,
237 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 1, true),
238 unassigned,
239 from_single,
240 to_single,
241 flip))
242 << "flipped path match, but array index mismatch";
243 }
244
245 /* Many to single (so only array index matters), Flipping bone names requested (but won't happen
246 * because 'to single'). */
247 {
249 const bool from_single = false;
250 const bool to_single = true;
251 const bool flip = true;
252
253 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
254
256 nullptr,
257 *fcurve,
258 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 0, true),
259 unassigned,
260 from_single,
261 to_single,
262 flip))
263 << "original path match, is bone";
265 nullptr,
266 *fcurve,
267 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 0, true),
268 unassigned,
269 from_single,
270 to_single,
271 flip))
272 << "flipped path match, is bone";
273
275 *fcurve,
276 *fake_fcurve_in_buffer("location", 0, false),
277 unassigned,
278 from_single,
279 to_single,
280 flip))
281 << "rna path mismatch, ACI is NOT bone";
282
284 nullptr,
285 *fcurve,
286 *fake_fcurve_in_buffer("pose.bones[\"nose\"].rotation_euler", 0, true),
287 unassigned,
288 from_single,
289 to_single,
290 flip))
291 << "rna path mismatch, ACI is bone";
292
294 nullptr,
295 *fcurve,
296 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 1, true),
297 unassigned,
298 from_single,
299 to_single,
300 flip))
301 << "original path match, but array index mismatch";
302
304 nullptr,
305 *fcurve,
306 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 1, true),
307 unassigned,
308 from_single,
309 to_single,
310 flip))
311 << "flipped path match, but array index mismatch";
312 }
313
314 { /* Single (so array indices won't matter) to Many, Flipping bone names requested. */
316 const bool from_single = true;
317 const bool to_single = false;
318 const bool flip = true;
319
320 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
321
323 nullptr,
324 *fcurve,
325 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 0, true),
326 unassigned,
327 from_single,
328 to_single,
329 flip))
330 << "original path match, is bone";
332 nullptr,
333 *fcurve,
334 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 0, true),
335 unassigned,
336 from_single,
337 to_single,
338 flip))
339 << "flipped path match, is bone";
340
342 *fcurve,
343 *fake_fcurve_in_buffer("location", 0, false),
344 unassigned,
345 from_single,
346 to_single,
347 flip))
348 << "rna path mismatch, ACI is NOT bone";
349
351 nullptr,
352 *fcurve,
353 *fake_fcurve_in_buffer("pose.bones[\"nose\"].rotation_euler", 0, true),
354 unassigned,
355 from_single,
356 to_single,
357 flip))
358 << "rna path mismatch, ACI is bone";
359
361 nullptr,
362 *fcurve,
363 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 1, true),
364 unassigned,
365 from_single,
366 to_single,
367 flip))
368 << "original path match, but array index mismatch";
369
371 nullptr,
372 *fcurve,
373 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 1, true),
374 unassigned,
375 from_single,
376 to_single,
377 flip))
378 << "flipped path match, but array index mismatch";
379 }
380
381 {
382 /* Single (so array indices won't matter) to Many, NOT flipping bone names. */
384 const bool from_single = true;
385 const bool to_single = false;
386 const bool flip = false;
387
388 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
389
391 nullptr,
392 *fcurve,
393 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 0, true),
394 unassigned,
395 from_single,
396 to_single,
397 flip))
398 << "original path match, is bone";
400 nullptr,
401 *fcurve,
402 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 0, true),
403 unassigned,
404 from_single,
405 to_single,
406 flip))
407 << "flipped path match, is bone";
408
410 *fcurve,
411 *fake_fcurve_in_buffer("location", 0, false),
412 unassigned,
413 from_single,
414 to_single,
415 flip))
416 << "rna path mismatch, ACI is NOT bone";
417
419 nullptr,
420 *fcurve,
421 *fake_fcurve_in_buffer("pose.bones[\"nose\"].rotation_euler", 0, true),
422 unassigned,
423 from_single,
424 to_single,
425 flip))
426 << "rna path mismatch, ACI is bone";
427
429 nullptr,
430 *fcurve,
431 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 1, true),
432 unassigned,
433 from_single,
434 to_single,
435 flip))
436 << "original path match, but array index mismatch";
437
439 nullptr,
440 *fcurve,
441 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 1, true),
442 unassigned,
443 from_single,
444 to_single,
445 flip))
446 << "flipped path match, but array index mismatch";
447 }
448
449 /* Single to Single (so nothing should matter), Flipping bone names requested. */
450 {
452 const bool from_single = true;
453 const bool to_single = true;
454 const bool flip = true;
455
456 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
457
459 nullptr,
460 *fcurve,
461 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 0, true),
462 unassigned,
463 from_single,
464 to_single,
465 flip))
466 << "original path match, is bone";
468 nullptr,
469 *fcurve,
470 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 0, true),
471 unassigned,
472 from_single,
473 to_single,
474 flip))
475 << "flipped path match, is bone";
476
478 *fcurve,
479 *fake_fcurve_in_buffer("location", 0, false),
480 unassigned,
481 from_single,
482 to_single,
483 flip))
484 << "rna path mismatch, ACI is NOT bone";
485
487 nullptr,
488 *fcurve,
489 *fake_fcurve_in_buffer("pose.bones[\"nose\"].rotation_euler", 0, true),
490 unassigned,
491 from_single,
492 to_single,
493 flip))
494 << "rna path mismatch, ACI is bone";
495
497 nullptr,
498 *fcurve,
499 *fake_fcurve_in_buffer("pose.bones[\"hand.R\"].location", 1, true),
500 unassigned,
501 from_single,
502 to_single,
503 flip))
504 << "flipped path match, but array index mismatch";
505
507 nullptr,
508 *fcurve,
509 *fake_fcurve_in_buffer("pose.bones[\"hand.L\"].location", 1, true),
510 unassigned,
511 from_single,
512 to_single,
513 flip))
514 << "original path match, but array index mismatch";
515 }
516}
517
519{
520 constexpr slot_handle_t unassigned = Slot::unassigned;
521
522 Main *bmain = BKE_main_new();
523 ID *arm_ob_id;
524
525 { /* Set up an armature, to test matching on property names. */
526 bArmature *armature = BKE_armature_add(bmain, "Armature");
527 for (const auto &bone_name : {"hand.L", "hand.R", "middle"}) {
528 Bone *bone = MEM_new<Bone>(__func__);
529 STRNCPY_UTF8(bone->name, bone_name);
530 BLI_addtail(&armature->bonebase, bone);
531 }
532
533 Object *armature_object = BKE_object_add_only_object(bmain, OB_ARMATURE, "Armature");
534 armature_object->data = id_cast<ID *>(armature);
535 BKE_pose_ensure(bmain, armature_object, armature, false);
536
537 arm_ob_id = &armature_object->id;
538 }
539
540 /* Wrapper function to create an F-Curve in the copy buffer, animating the armature object. */
541 const auto fake_armob_fcurve =
542 [&](const char *rna_path, const int array_index, const bool is_bone) {
543 return fake_fcurve_in_buffer(rna_path, array_index, is_bone, unassigned, arm_ob_id);
544 };
545
546 { /* From Single Channel, so array indices are ignored. */
548 const bool from_single = true;
549 const bool to_single = false; /* Doesn't matter, function under test doesn't use this. */
550 const bool flip = false; /* Doesn't matter, function under test doesn't use this. */
551
552 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
553
555 bmain,
556 *fcurve,
557 *fake_armob_fcurve("pose.bones[\"hand.L\"].location", 0, true),
558 unassigned,
559 from_single,
560 to_single,
561 flip))
562 << "original path match, is bone";
563
565 bmain,
566 *fcurve,
567 *fake_armob_fcurve("pose.bones[\"hand.R\"].location", 0, true),
568 unassigned,
569 from_single,
570 to_single,
571 flip))
572 << "flipped path match, is bone";
573
575 bmain,
576 *fcurve,
577 *fake_armob_fcurve("pose.bones[\"hand.L\"].location", 2, true),
578 unassigned,
579 from_single,
580 to_single,
581 flip))
582 << "original path match, other array index";
583
585 bmain,
586 *fcurve,
587 *fake_armob_fcurve("pose.bones[\"hand.L\"].rotation_euler", 0, true),
588 unassigned,
589 from_single,
590 to_single,
591 flip))
592 << "same bone, other property";
593
594 /* Malformed paths shouldn't generate a match or crash Blender. */
596 *fcurve,
597 *fake_armob_fcurve("pose.bones[\"hand.", 0, true),
598 unassigned,
599 from_single,
600 to_single,
601 flip))
602 << "malformed rna path";
603
606 *fcurve,
607 *fake_armob_fcurve("location", 0, false),
608 unassigned,
609 from_single,
610 to_single,
611 flip))
612 << "other struct, same property name";
613
615 *fcurve,
616 *fake_armob_fcurve("location", 0, false),
617 unassigned,
618 from_single,
619 to_single,
620 flip))
621 << "other struct, same property name";
622
624 bmain,
625 *fcurve,
626 *fake_armob_fcurve("pose.bones[\"missing\"].location", 0, true),
627 unassigned,
628 from_single,
629 to_single,
630 flip))
631 << "nonexistent bone, but same property name";
632
633 /* This just tests the current functionality. This may not necessarily be
634 * correct / desired behavior. */
635 FCurvePtr fcurve_with_long_rna_path = fake_fcurve(
636 "pose.bones[\"hand.L\"].weirdly_long_location", 0);
638 bmain,
639 *fcurve,
640 *fake_armob_fcurve("pose.bones[\"hand.L\"].location", 0, true),
641 unassigned,
642 from_single,
643 to_single,
644 flip))
645 << "property name suffix-match";
646 }
647
648 { /* From Multiple Channels, so array indices matter. */
650 const bool from_single = false;
651 const bool to_single = false; /* Doesn't matter, function under test doesn't use this. */
652 const bool flip = false; /* Doesn't matter, function under test doesn't use this. */
653
654 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
655
657 bmain,
658 *fcurve,
659 *fake_armob_fcurve("pose.bones[\"hand.L\"].location", 0, true),
660 unassigned,
661 from_single,
662 to_single,
663 flip))
664 << "original path match, is bone";
665
667 bmain,
668 *fcurve,
669 *fake_armob_fcurve("pose.bones[\"hand.R\"].location", 0, true),
670 unassigned,
671 from_single,
672 to_single,
673 flip))
674 << "flipped path match, is bone";
675
677 bmain,
678 *fcurve,
679 *fake_armob_fcurve("pose.bones[\"hand.L\"].location", 2, true),
680 unassigned,
681 from_single,
682 to_single,
683 flip))
684 << "original path match, other array index";
685
687 bmain,
688 *fcurve,
689 *fake_armob_fcurve("pose.bones[\"hand.L\"].rotation_euler", 0, true),
690 unassigned,
691 from_single,
692 to_single,
693 flip))
694 << "same bone, other property";
695
698 *fcurve,
699 *fake_armob_fcurve("location", 0, false),
700 unassigned,
701 from_single,
702 to_single,
703 flip))
704 << "other struct, same property name";
705
707 *fcurve,
708 *fake_armob_fcurve("location", 0, false),
709 unassigned,
710 from_single,
711 to_single,
712 flip))
713 << "other struct, same property name";
714
716 bmain,
717 *fcurve,
718 *fake_armob_fcurve("pose.bones[\"missing\"].location", 0, true),
719 unassigned,
720 from_single,
721 to_single,
722 flip))
723 << "nonexistent bone, but same property name";
724
725 /* This just tests the current functionality. This may not necessarily be
726 * correct / desired behavior. */
727 FCurvePtr fcurve_with_long_rna_path = fake_fcurve(
728 "pose.bones[\"hand.L\"].weirdly_long_location", 0);
730 bmain,
731 *fcurve_with_long_rna_path,
732 *fake_armob_fcurve("pose.bones[\"hand.L\"].location", 0, true),
733 unassigned,
734 from_single,
735 to_single,
736 flip))
737 << "property name suffix-match";
738 }
739
740 { /* Resilience against deleted IDs. */
742 FCurvePtr fcurve = fake_fcurve("pose.bones[\"hand.L\"].location", 0);
743 Object *object_not_in_main = BKE_object_add_only_object(nullptr, OB_EMPTY, "non-main");
744
746 bmain,
747 *fcurve,
748 *fake_fcurve_in_buffer(
749 "pose.bones[\"hand.L\"].location", 0, true, unassigned, &object_not_in_main->id),
750 unassigned,
751 false,
752 false,
753 false))
754 << "copying from deleted ID";
755
756 BKE_id_free(nullptr, &object_not_in_main->id);
757 }
758
759 BKE_main_free(bmain);
760}
761
763{
764 constexpr slot_handle_t unassigned = Slot::unassigned;
766 FCurvePtr fcurve = fake_fcurve("some_prop", 1);
767
769 *fcurve,
770 *fake_fcurve_in_buffer("location", 1, false),
771 unassigned,
772 false,
773 false,
774 false));
776 *fcurve,
777 *fake_fcurve_in_buffer("location", 2, false),
778 unassigned,
779 false,
780 false,
781 false));
782}
783
784} // namespace ed::animation::tests
785} // namespace blender
General operations, lookup, etc. for blender objects.
#define STRNCPY_UTF8(dst, src)
Object is a sort of wrapper for general info.
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
static constexpr slot_handle_t unassigned
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
decltype(blender::ActionSlot::handle) slot_handle_t
void gtest_teardown()
void gtest_setup()
TEST_F(keyframes_paste, flip_names)
std::optional< std::string > flip_names(const StringRefNull rna_path)
KeyframeCopyBuffer * keyframe_copy_buffer
bool pastebuf_match_index_only(Main *, const FCurve &fcurve_to_match, const FCurve &fcurve_in_copy_buffer, animrig::slot_handle_t, const bool from_single, const bool, const bool)
bool pastebuf_match_path_property_and_component_length(Main *bmain, const FCurve &fcurve_to_match, const FCurve &fcurve_in_copy_buffer, const animrig::slot_handle_t slot_handle_in_copy_buffer, const bool from_single, const bool to_single, const bool flip)
bool pastebuf_match_path_property(Main *, const FCurve &fcurve_to_match, const FCurve &fcurve_in_copy_buffer, animrig::slot_handle_t, const bool from_single, const bool, const bool)
bool pastebuf_match_path_full(Main *, const FCurve &fcurve_to_match, const FCurve &fcurve_in_copy_buffer, animrig::slot_handle_t, const bool from_single, const bool to_single, const bool flip)
BMesh Iterators.
void ANIM_fcurves_copybuf_reset()
bArmature * BKE_armature_add(Main *bmain, const char *name)
void ANIM_fcurves_copybuf_free()
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
Main * BKE_main_new()
Definition main.cc:93
void BKE_id_free(Main *bmain, void *idv)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BKE_fcurve_free(FCurve *fcu)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
FCurve * BKE_fcurve_create()
ListBaseT< Bone > bonebase
Map< blender::animrig::slot_handle_t, ID * > slot_animated_ids