Blender V5.3
keyframing_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "ANIM_action.hh"
6#include "ANIM_keyframing.hh"
7#include "ANIM_nla.hh"
8
9#include "BKE_action.hh"
10#include "BKE_anim_data.hh"
11#include "BKE_animsys.hh"
12#include "BKE_armature.hh"
13#include "BKE_fcurve.hh"
14#include "BKE_gtest_base.hh"
15#include "BKE_idtype.hh"
16#include "BKE_lib_id.hh"
17#include "BKE_main.hh"
18#include "BKE_material.hh"
19#include "BKE_mesh.hh"
20#include "BKE_nla.hh"
21#include "BKE_object.hh"
22
23#include "DNA_anim_types.h"
24#include "DNA_material_types.h"
25#include "DNA_object_types.h"
26
27#include "RNA_access.hh"
28#include "RNA_prototypes.hh"
29
30#include "BLI_listbase.hh"
31#include "BLI_string.hh"
32
33#include "testing/testing.h"
34
37 public:
39
40 /* For standard single-action testing. */
43
44 /* For pose bone single-action testing. */
48
49 /* For NLA testing. */
53
54 /* For action reuse testing. */
61
62 void SetUp() override
63 {
65
66 object = BKE_object_add_only_object(bmain, OB_EMPTY, "Empty");
68
69 Bone *bone = MEM_new<Bone>("BONE");
70 STRNCPY(bone->name, "Bone");
71
72 armature = BKE_armature_add(bmain, "Armature");
74
76 armature_object->data = id_cast<ID *>(armature);
79
82 cube_mesh = BKE_mesh_add(bmain, "cube_mesh");
84 /* Removing the implicit id user. Using BKE_mesh_assign_object increments the user count which
85 * would leave it at 2 otherwise. */
88 material = BKE_material_add(bmain, "material");
90
91 id_us_min(&material->id);
93
96 nla_action = BKE_id_new<bAction>(bmain, "NLAAction");
97 /* Set up an NLA system with a single NLA track with a single offset-in-time
98 * NLA strip, and make that strip active and in tweak mode. */
100 NlaTrack *track = BKE_nlatrack_new_head(&adt->nla_tracks, false);
101 ASSERT_NE(track, nullptr);
103 BKE_nlatrack_add_strip(track, strip, false);
104 ASSERT_NE(strip, nullptr);
105 ASSERT_TRUE(animrig::nla::assign_action(*strip, nla_action->wrap(), object_with_nla->id));
106 track->flag |= NLATRACK_ACTIVE;
107 strip->flag |= NLASTRIP_FLAG_ACTIVE;
108 strip->start = -10.0;
109 strip->end = 990.0;
110 strip->actstart = 0.0;
111 strip->actend = 1000.0;
112 strip->scale = 1.0;
114 }
115
116 void TearDown() override
117 {
119 }
120
122 {
123 Action &action = object.adt->action->wrap();
124 if (action.layer_array_num == 0) {
125 return nullptr;
126 }
127 Layer *layer = action.layer(0);
128 if (layer->strip_array_num == 0) {
129 return nullptr;
130 }
131 Strip *strip = layer->strip(0);
133 StripKeyframeData &strip_data = strip->data<animrig::StripKeyframeData>(action);
134 return strip_data.channelbag_for_slot(object.adt->slot_handle);
135 }
136};
137
138/* ------------------------------------------------------------
139 * Tests for `insert_keyframes()` with layered actions.
140 */
141
142/* Keying a non-array property. */
143TEST_F(KeyframingTest, insert_keyframes__non_array_property)
144{
145 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
146
147 /* First time should create:
148 * - AnimData
149 * - Action
150 * - Slot
151 * - Layer
152 * - Infinite KeyframeStrip
153 * - FCurve with a single key
154 */
155 object->empty_drawsize = 42.0;
156 const CombinedKeyingResult result_1 = insert_keyframes(bmain,
157 &object_rna_pointer,
158 std::nullopt,
159 {{"empty_display_size"}},
160 1.0,
161 anim_eval_context,
165 ASSERT_NE(nullptr, object->adt);
166 ASSERT_NE(nullptr, object->adt->action);
167 Action &action = object->adt->action->wrap();
168
169 /* The action has a slot, it's named properly, and it's correctly assigned
170 * to the object. */
171 ASSERT_EQ(1, action.slots().size());
172 Slot *slot = action.slot(0);
173 EXPECT_STREQ(object->id.name, slot->identifier);
174 EXPECT_STREQ(object->adt->last_slot_identifier, slot->identifier);
175 EXPECT_EQ(object->adt->slot_handle, slot->handle);
176
177 /* We have the default layer and strip. */
178 ASSERT_EQ(1, action.layers().size());
179 ASSERT_EQ(1, action.layer(0)->strips().size());
180 EXPECT_TRUE(strlen(action.layer(0)->name) > 0);
181 Strip *strip = action.layer(0)->strip(0);
182 ASSERT_TRUE(strip->is_infinite());
183 ASSERT_EQ(Strip::Type::Keyframe, strip->type());
184 StripKeyframeData *strip_data = &strip->data<StripKeyframeData>(action);
185 /* We have a channel bag for the slot. */
186 Channelbag *channelbag = strip_data->channelbag_for_slot(*slot);
187 ASSERT_NE(nullptr, channelbag);
188
189 /* The fcurves in the channel bag are what we expect. */
190 EXPECT_EQ(1, channelbag->fcurves().size());
191 const FCurve *fcurve = channelbag->fcurve_find({"empty_display_size", 0});
192 ASSERT_NE(nullptr, fcurve);
193 ASSERT_NE(nullptr, fcurve->bezt);
194 EXPECT_EQ(1, fcurve->totvert);
195 EXPECT_EQ(1.0, fcurve->bezt[0].vec[1][0]);
196 EXPECT_EQ(42.0, fcurve->bezt[0].vec[1][1]);
197
198 /* Second time inserting with a different value on the same frame should
199 * simply replace the key. */
200 object->empty_drawsize = 86.0;
201 const CombinedKeyingResult result_2 = insert_keyframes(bmain,
202 &object_rna_pointer,
203 std::nullopt,
204 {{"empty_display_size"}},
205 1.0,
206 anim_eval_context,
210 EXPECT_EQ(1, fcurve->totvert);
211 EXPECT_EQ(1.0, fcurve->bezt[0].vec[1][0]);
212 EXPECT_EQ(86.0, fcurve->bezt[0].vec[1][1]);
213
214 /* Third time inserting on a different time should add a second key. */
215 object->empty_drawsize = 7.0;
216 const CombinedKeyingResult result_3 = insert_keyframes(bmain,
217 &object_rna_pointer,
218 std::nullopt,
219 {{"empty_display_size"}},
220 10.0,
221 anim_eval_context,
225 EXPECT_EQ(2, fcurve->totvert);
226 EXPECT_EQ(1.0, fcurve->bezt[0].vec[1][0]);
227 EXPECT_EQ(86.0, fcurve->bezt[0].vec[1][1]);
228 EXPECT_EQ(10.0, fcurve->bezt[1].vec[1][0]);
229 EXPECT_EQ(7.0, fcurve->bezt[1].vec[1][1]);
230}
231
232TEST_F(KeyframingTest, insert_keyframes__action_reuse)
233{
234 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
235 CombinedKeyingResult result_ob;
236 result_ob = insert_keyframes(bmain,
237 &armature_object_rna_pointer,
238 std::nullopt,
239 {{"location"}},
240 10.0,
241 anim_eval_context,
244
245 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 3);
246 ASSERT_TRUE(armature_object->adt != nullptr);
247 ASSERT_TRUE(armature_object->adt->action != nullptr);
248
249 PointerRNA armature_rna_pointer = RNA_id_pointer_create(&armature->id);
250
251 result_ob = insert_keyframes(bmain,
252 &armature_rna_pointer,
253 std::nullopt,
254 {{"display_type"}},
255 10.0,
256 anim_eval_context,
259 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 1);
260 ASSERT_TRUE(armature->adt != nullptr);
261 ASSERT_TRUE(armature->adt->action != nullptr);
262
263 /* Action is expected to be reused between object and data. */
264 ASSERT_EQ(armature->adt->action, armature_object->adt->action);
265
266 Action &action = armature->adt->action->wrap();
267 /* Should have two slots now. */
268 ASSERT_EQ(action.slot_array_num, 2);
269 for (Slot *slot : action.slots()) {
270 ASSERT_TRUE(slot->idtype == ID_AR || slot->idtype == ID_OB);
271 }
272}
273
274TEST_F(KeyframingTest, insert_keyframes__action_reuse_material)
275{
276 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
277 CombinedKeyingResult result_ob;
278
279 result_ob = insert_keyframes(bmain,
280 &material_rna_pointer,
281 std::nullopt,
282 {{"pass_index"}},
283 1.0,
284 anim_eval_context,
287
288 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 1);
289 ASSERT_TRUE(material->adt != nullptr);
290 ASSERT_TRUE(material->adt->action != nullptr);
291
292 result_ob = insert_keyframes(bmain,
293 &cube_rna_pointer,
294 std::nullopt,
295 {{"location"}},
296 1.0,
297 anim_eval_context,
300
301 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 3);
302 ASSERT_TRUE(cube->adt != nullptr);
303 ASSERT_TRUE(cube->adt->action != nullptr);
304
305 /* Actions are not shared between object and material. */
306 ASSERT_NE(cube->adt->action, material->adt->action);
307
308 result_ob = insert_keyframes(bmain,
309 &cube_mesh_rna_pointer,
310 std::nullopt,
311 {{"remesh_voxel_size"}},
312 1.0,
313 anim_eval_context,
316
317 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 1);
318 ASSERT_TRUE(cube_mesh->adt != nullptr);
319 ASSERT_TRUE(cube_mesh->adt->action != nullptr);
320
321 /* Reuse between Object and object data. */
322 ASSERT_EQ(cube_mesh->adt->action, cube->adt->action);
323 /* Still no reuse from mesh to material. */
324 ASSERT_NE(cube_mesh->adt->action, material->adt->action);
325
326 Action &action = cube->adt->action->wrap();
327 /* Should have two slots now. */
328 ASSERT_EQ(action.slot_array_num, 2);
329
330 /* Material action should have only 1 slot. */
331 ASSERT_EQ(material->adt->action->wrap().slot_array_num, 1);
332
333 for (Slot *slot : action.slots()) {
334 ASSERT_TRUE(slot->idtype == ID_ME || slot->idtype == ID_OB);
335 ASSERT_NE(slot->idtype, ID_MA);
336 }
337}
338
339TEST_F(KeyframingTest, insert_keyframes__action_reuse_multiuser)
340{
341 Object *another_object = BKE_object_add_only_object(bmain, OB_MESH, "another_object");
342 PointerRNA another_object_rna_pointer = RNA_id_pointer_create(&another_object->id);
343 BKE_mesh_assign_object(bmain, another_object, cube_mesh);
344
345 ASSERT_EQ(ID_REFCOUNTING_USERS(&cube_mesh->id), 2);
346
347 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
348 CombinedKeyingResult result_ob;
349
350 result_ob = insert_keyframes(bmain,
351 &cube_rna_pointer,
352 std::nullopt,
353 {{"location"}},
354 1.0,
355 anim_eval_context,
358
359 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 3);
360 ASSERT_TRUE(cube->adt != nullptr);
361 ASSERT_TRUE(cube->adt->action != nullptr);
362
363 result_ob = insert_keyframes(bmain,
364 &cube_mesh_rna_pointer,
365 std::nullopt,
366 {{"remesh_voxel_size"}},
367 1.0,
368 anim_eval_context,
371
372 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 1);
373 ASSERT_TRUE(cube_mesh->adt != nullptr);
374 ASSERT_TRUE(cube_mesh->adt->action != nullptr);
375
376 /* When an ID is used more than once, the action should not be reused. */
377 ASSERT_NE(cube->adt->action, cube_mesh->adt->action);
378
379 result_ob = insert_keyframes(bmain,
380 &another_object_rna_pointer,
381 std::nullopt,
382 {{"location"}},
383 1.0,
384 anim_eval_context,
387
388 ASSERT_EQ(result_ob.get_count(SingleKeyingResult::SUCCESS), 3);
389 ASSERT_TRUE(another_object->adt != nullptr);
390 ASSERT_TRUE(another_object->adt->action != nullptr);
391
392 /* Given that those two objects are connected by a mesh (which due to this has two users) the
393 * action shouldn't be reused between them. */
394 ASSERT_NE(cube->adt->action, another_object->adt->action);
395}
396
397/* Keying a single element of an array property. */
398TEST_F(KeyframingTest, insert_keyframes__single_element)
399{
400 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
401
403 &object_rna_pointer,
404 std::nullopt,
405 {{"rotation_euler", std::nullopt, 0}},
406 1.0,
407 anim_eval_context,
410
412 ASSERT_NE(nullptr, object->adt);
413 ASSERT_NE(nullptr, object->adt->action);
414 Action &action = object->adt->action->wrap();
415 ASSERT_EQ(1, action.slots().size());
416 ASSERT_EQ(1, action.layers().size());
417 ASSERT_EQ(1, action.layer(0)->strips().size());
418 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
419 ASSERT_EQ(1, strip_data->channelbags().size());
420 Channelbag *channelbag = strip_data->channelbag(0);
421
422 EXPECT_EQ(1, channelbag->fcurves().size());
423 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 0}));
424}
425
426/* Keying all elements of an array property. */
427TEST_F(KeyframingTest, insert_keyframes__all_elements)
428{
429 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
430
432 &object_rna_pointer,
433 std::nullopt,
434 {{"rotation_euler"}},
435 1.0,
436 anim_eval_context,
439
441 ASSERT_NE(nullptr, object->adt);
442 ASSERT_NE(nullptr, object->adt->action);
443 Action &action = object->adt->action->wrap();
444 ASSERT_EQ(1, action.slots().size());
445 ASSERT_EQ(1, action.layers().size());
446 ASSERT_EQ(1, action.layer(0)->strips().size());
447 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
448 ASSERT_EQ(1, strip_data->channelbags().size());
449 Channelbag *channelbag = strip_data->channelbag(0);
450
451 EXPECT_EQ(3, channelbag->fcurves().size());
452 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 0}));
453 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 1}));
454 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 2}));
455}
456
457/* Keying a pose bone from its own RNA pointer. */
458TEST_F(KeyframingTest, insert_keyframes__pose_bone_rna_pointer)
459{
460 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
461 bPoseChannel *pchan = BKE_pose_channel_find_name(armature_object->pose, "Bone");
462 PointerRNA pose_bone_rna_pointer = RNA_pointer_create_discrete(
463 &armature_object->id, RNA_PoseBone, pchan);
464
466 &pose_bone_rna_pointer,
467 std::nullopt,
468 {{"rotation_euler", std::nullopt, 0}},
469 1.0,
470 anim_eval_context,
473
475 ASSERT_NE(nullptr, armature_object->adt);
476 ASSERT_NE(nullptr, armature_object->adt->action);
477 Action &action = armature_object->adt->action->wrap();
478 ASSERT_EQ(1, action.slots().size());
479 ASSERT_EQ(1, action.layers().size());
480 ASSERT_EQ(1, action.layer(0)->strips().size());
481 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
482 ASSERT_EQ(1, strip_data->channelbags().size());
483 Channelbag *channelbag = strip_data->channelbag(0);
484
485 EXPECT_EQ(1, channelbag->fcurves().size());
486 EXPECT_NE(nullptr, channelbag->fcurve_find({"pose.bones[\"Bone\"].rotation_euler", 0}));
487}
488
489/* Keying a pose bone from its owning ID's RNA pointer. */
490TEST_F(KeyframingTest, insert_keyframes__pose_bone_owner_id_pointer)
491{
492 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
493
495 bmain,
496 &armature_object_rna_pointer,
497 std::nullopt,
498 {{"pose.bones[\"Bone\"].rotation_euler", std::nullopt, 0}},
499 1.0,
500 anim_eval_context,
503
505 ASSERT_NE(nullptr, armature_object->adt);
506 ASSERT_NE(nullptr, armature_object->adt->action);
507 Action &action = armature_object->adt->action->wrap();
508 ASSERT_EQ(1, action.slots().size());
509 ASSERT_EQ(1, action.layers().size());
510 ASSERT_EQ(1, action.layer(0)->strips().size());
511 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
512 ASSERT_EQ(1, strip_data->channelbags().size());
513 Channelbag *channelbag = strip_data->channelbag(0);
514
515 EXPECT_EQ(1, channelbag->fcurves().size());
516 EXPECT_NE(nullptr, channelbag->fcurve_find({"pose.bones[\"Bone\"].rotation_euler", 0}));
517}
518
519/* Keying multiple elements of multiple properties at once. */
520TEST_F(KeyframingTest, insert_keyframes__multiple_properties)
521{
522 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
523
525 &object_rna_pointer,
526 std::nullopt,
527 {
528 {"empty_display_size"},
529 {"location"},
530 {"rotation_euler", std::nullopt, 0},
531 {"rotation_euler", std::nullopt, 2},
532 },
533 1.0,
534 anim_eval_context,
537
539 ASSERT_NE(nullptr, object->adt);
540 ASSERT_NE(nullptr, object->adt->action);
541 Action &action = object->adt->action->wrap();
542 ASSERT_EQ(1, action.slots().size());
543 ASSERT_EQ(1, action.layers().size());
544 ASSERT_EQ(1, action.layer(0)->strips().size());
545 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
546 ASSERT_EQ(1, strip_data->channelbags().size());
547 Channelbag *channelbag = strip_data->channelbag(0);
548
549 EXPECT_EQ(6, channelbag->fcurves().size());
550 EXPECT_NE(nullptr, channelbag->fcurve_find({"empty_display_size", 0}));
551 EXPECT_NE(nullptr, channelbag->fcurve_find({"location", 0}));
552 EXPECT_NE(nullptr, channelbag->fcurve_find({"location", 1}));
553 EXPECT_NE(nullptr, channelbag->fcurve_find({"location", 2}));
554 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 0}));
555 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 2}));
556}
557
558/* Keying more than one ID on the same action. */
559TEST_F(KeyframingTest, insert_keyframes__multiple_ids)
560{
561 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
562
563 /* First object should crate the action and get a slot and channel bag. */
564 const CombinedKeyingResult result_1 = insert_keyframes(bmain,
565 &object_rna_pointer,
566 std::nullopt,
567 {{"empty_display_size"}},
568 1.0,
569 anim_eval_context,
573 ASSERT_NE(nullptr, object->adt);
574 ASSERT_NE(nullptr, object->adt->action);
575 Action &action = object->adt->action->wrap();
576
577 /* The action has a slot and it's assigned to the first object. */
578 ASSERT_EQ(1, action.slots().size());
579 Slot *slot_1 = action.slot_for_handle(object->adt->slot_handle);
580 ASSERT_NE(nullptr, slot_1);
581 EXPECT_STREQ(object->id.name, slot_1->identifier);
582 EXPECT_STREQ(object->adt->last_slot_identifier, slot_1->identifier);
583
584 /* Get the keyframe strip. */
585 ASSERT_EQ(1, action.layers().size());
586 ASSERT_EQ(1, action.layer(0)->strips().size());
587 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
588
589 /* We have a single channel bag, and it's for the first object's slot. */
590 ASSERT_EQ(1, strip_data->channelbags().size());
591 Channelbag *channelbag_1 = strip_data->channelbag_for_slot(*slot_1);
592 ASSERT_NE(nullptr, channelbag_1);
593
594 /* Assign the action to the second object, with no slot. */
595 ASSERT_TRUE(assign_action(&action, armature_object->id));
596 ASSERT_EQ(assign_action_slot(nullptr, armature_object->id), ActionSlotAssignmentResult::OK);
597
598 /* Keying the second object should go into the same action, creating a new
599 * slot and channel bag. */
600 const CombinedKeyingResult result_2 = insert_keyframes(bmain,
601 &armature_object_rna_pointer,
602 std::nullopt,
603 {{"empty_display_size"}},
604 1.0,
605 anim_eval_context,
609
610 ASSERT_EQ(2, action.slots().size());
611 Slot *slot_2 = action.slot_for_handle(armature_object->adt->slot_handle);
612 ASSERT_NE(nullptr, slot_2);
613 EXPECT_STREQ(armature_object->id.name, slot_2->identifier);
614 EXPECT_STREQ(armature_object->adt->last_slot_identifier, slot_2->identifier);
615
616 ASSERT_EQ(2, strip_data->channelbags().size());
617 Channelbag *channelbag_2 = strip_data->channelbag_for_slot(*slot_2);
618 ASSERT_NE(nullptr, channelbag_2);
619}
620
621/* Keying with the "Only Insert Available" flag. */
622TEST_F(KeyframingTest, insert_keyframes__only_available)
623{
624 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
625
626 /* First attempt should fail, because there are no fcurves yet. */
627 const CombinedKeyingResult result_1 = insert_keyframes(bmain,
628 &object_rna_pointer,
629 std::nullopt,
630 {{"rotation_euler"}},
631 1.0,
632 anim_eval_context,
635
637
638 /* It's unclear why an AnimData should be created if keying fails
639 * here. It may even be undesirable. This check is just here to ensure no
640 * *unintentional* changes in behavior. */
641 ASSERT_NE(nullptr, object->adt);
642 /* No action is created when using the flag INSERTKEY_AVAILABLE on an
643 * object without an action. */
644 ASSERT_EQ(nullptr, object->adt->action);
645
646 /* Insert a key on two of the elements without using the flag so that there
647 * will be two fcurves. */
648 const CombinedKeyingResult result_2 = insert_keyframes(bmain,
649 &object_rna_pointer,
650 std::nullopt,
651 {
652 {"rotation_euler", std::nullopt, 0},
653 {"rotation_euler", std::nullopt, 2},
654 },
655 1.0,
656 anim_eval_context,
659
660 /* If an action is created, it should be the default action with one
661 * layer and an infinite keyframe strip. */
662 Action &action = object->adt->action->wrap();
663 ASSERT_EQ(1, action.slots().size());
664 ASSERT_EQ(1, action.layers().size());
665 ASSERT_EQ(1, action.layer(0)->strips().size());
666 EXPECT_EQ(object->adt->slot_handle, action.slot(0)->handle);
667 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
668
670 ASSERT_EQ(1, strip_data->channelbags().size());
671 Channelbag *channelbag = strip_data->channelbag(0);
672
673 /* Second attempt should succeed with two keys, because two of the elements
674 * now have fcurves. */
675 const CombinedKeyingResult result_3 = insert_keyframes(bmain,
676 &object_rna_pointer,
677 std::nullopt,
678 {{"rotation_euler"}},
679 1.0,
680 anim_eval_context,
683
685 EXPECT_EQ(2, channelbag->fcurves().size());
686 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 0}));
687 EXPECT_NE(nullptr, channelbag->fcurve_find({"rotation_euler", 2}));
688}
689
690/* Keying with the "Only Replace" flag. */
691TEST_F(KeyframingTest, insert_keyframes__only_replace)
692{
693 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
694
695 /* First attempt should fail, because there are no fcurves yet. */
696 object->rot[0] = 42.0;
697 object->rot[1] = 42.0;
698 object->rot[2] = 42.0;
699 const CombinedKeyingResult result_1 = insert_keyframes(bmain,
700 &object_rna_pointer,
701 std::nullopt,
702 {{"rotation_euler"}},
703 1.0,
704 anim_eval_context,
708
709 /* Insert a key for two of the elements so that there will be two fcurves with
710 * one key each. */
711 const CombinedKeyingResult result_2 = insert_keyframes(bmain,
712 &object_rna_pointer,
713 std::nullopt,
714 {
715 {"rotation_euler", std::nullopt, 0},
716 {"rotation_euler", std::nullopt, 2},
717 },
718 1.0,
719 anim_eval_context,
723 ASSERT_NE(nullptr, object->adt);
724 ASSERT_NE(nullptr, object->adt->action);
725 Action &action = object->adt->action->wrap();
726 ASSERT_EQ(1, action.slots().size());
727 ASSERT_EQ(1, action.layers().size());
728 ASSERT_EQ(1, action.layer(0)->strips().size());
729 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
730 ASSERT_EQ(1, strip_data->channelbags().size());
731 Channelbag *channelbag = strip_data->channelbag(0);
732
733 ASSERT_EQ(2, channelbag->fcurves().size());
734 const FCurve *fcurve_x = channelbag->fcurve_find({"rotation_euler", 0});
735 const FCurve *fcurve_z = channelbag->fcurve_find({"rotation_euler", 2});
736 EXPECT_EQ(1, fcurve_x->totvert);
737 EXPECT_EQ(1, fcurve_z->totvert);
738 EXPECT_EQ(1.0, fcurve_x->bezt[0].vec[1][0]);
739 EXPECT_EQ(42.0, fcurve_x->bezt[0].vec[1][1]);
740 EXPECT_EQ(1.0, fcurve_z->bezt[0].vec[1][0]);
741 EXPECT_EQ(42.0, fcurve_z->bezt[0].vec[1][1]);
742
743 /* Second attempt should also fail, because we insert on a different frame
744 * than the two keys we just created. */
745 object->rot[0] = 86.0;
746 object->rot[1] = 86.0;
747 object->rot[2] = 86.0;
748 const CombinedKeyingResult result_3 = insert_keyframes(bmain,
749 &object_rna_pointer,
750 std::nullopt,
751 {{"rotation_euler"}},
752 5.0,
753 anim_eval_context,
757 EXPECT_EQ(2, channelbag->fcurves().size());
758 EXPECT_EQ(1, fcurve_x->totvert);
759 EXPECT_EQ(1, fcurve_z->totvert);
760 EXPECT_EQ(1.0, fcurve_x->bezt[0].vec[1][0]);
761 EXPECT_EQ(42.0, fcurve_x->bezt[0].vec[1][1]);
762 EXPECT_EQ(1.0, fcurve_z->bezt[0].vec[1][0]);
763 EXPECT_EQ(42.0, fcurve_z->bezt[0].vec[1][1]);
764
765 /* The third attempt, keying on the original frame, should succeed and replace
766 * the existing key on each fcurve. */
767 const CombinedKeyingResult result_4 = insert_keyframes(bmain,
768 &object_rna_pointer,
769 std::nullopt,
770 {{"rotation_euler"}},
771 1.0,
772 anim_eval_context,
776 EXPECT_EQ(2, channelbag->fcurves().size());
777 EXPECT_EQ(1, fcurve_x->totvert);
778 EXPECT_EQ(1, fcurve_z->totvert);
779 EXPECT_EQ(1.0, fcurve_x->bezt[0].vec[1][0]);
780 EXPECT_EQ(86.0, fcurve_x->bezt[0].vec[1][1]);
781 EXPECT_EQ(1.0, fcurve_z->bezt[0].vec[1][0]);
782 EXPECT_EQ(86.0, fcurve_z->bezt[0].vec[1][1]);
783}
784
785/* Keying with the "Only Insert Needed" flag. */
786TEST_F(KeyframingTest, insert_keyframes__only_needed)
787{
788 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
789
790 /* First attempt should succeed, because there are no fcurves yet. */
791 const CombinedKeyingResult result_1 = insert_keyframes(bmain,
792 &object_rna_pointer,
793 std::nullopt,
794 {{"rotation_euler"}},
795 1.0,
796 anim_eval_context,
800 ASSERT_NE(nullptr, object->adt);
801 ASSERT_NE(nullptr, object->adt->action);
802 Action &action = object->adt->action->wrap();
803 ASSERT_EQ(1, action.slots().size());
804 ASSERT_EQ(1, action.layers().size());
805 ASSERT_EQ(1, action.layer(0)->strips().size());
806 StripKeyframeData *strip_data = &action.layer(0)->strip(0)->data<StripKeyframeData>(action);
807 ASSERT_EQ(1, strip_data->channelbags().size());
808 Channelbag *channelbag = strip_data->channelbag(0);
809
810 ASSERT_EQ(3, channelbag->fcurves().size());
811 const FCurve *fcurve_x = channelbag->fcurve_find({"rotation_euler", 0});
812 const FCurve *fcurve_y = channelbag->fcurve_find({"rotation_euler", 1});
813 const FCurve *fcurve_z = channelbag->fcurve_find({"rotation_euler", 2});
814 EXPECT_EQ(1, fcurve_x->totvert);
815 EXPECT_EQ(1, fcurve_y->totvert);
816 EXPECT_EQ(1, fcurve_z->totvert);
817
818 /* Second attempt should fail, because there is now an fcurve for the
819 * property, but its value matches the current property value. */
820 const CombinedKeyingResult result_2 = insert_keyframes(bmain,
821 &object_rna_pointer,
822 std::nullopt,
823 {{"rotation_euler"}},
824 10.0,
825 anim_eval_context,
829 EXPECT_EQ(3, channelbag->fcurves().size());
830 EXPECT_EQ(1, fcurve_x->totvert);
831 EXPECT_EQ(1, fcurve_y->totvert);
832 EXPECT_EQ(1, fcurve_z->totvert);
833
834 /* Third attempt should succeed on two elements, because we change the value
835 * of those elements to differ from the existing fcurves. */
836 object->rot[0] = 123.0;
837 object->rot[2] = 123.0;
838 const CombinedKeyingResult result_3 = insert_keyframes(bmain,
839 &object_rna_pointer,
840 std::nullopt,
841 {{"rotation_euler"}},
842 10.0,
843 anim_eval_context,
846
848 EXPECT_EQ(3, channelbag->fcurves().size());
849 EXPECT_EQ(2, fcurve_x->totvert);
850 EXPECT_EQ(1, fcurve_y->totvert);
851 EXPECT_EQ(2, fcurve_z->totvert);
852}
853
854/* Passing the frame number explicitly vs not. */
855TEST_F(KeyframingTest, insert_keyframes__optional_frame)
856{
857 /* If the frame number is not explicitly passed, the eval frame from the
858 * animation evaluation context should be used. */
859 AnimationEvalContext anim_eval_context = {nullptr, 5.0};
860
861 object->rotmode = ROT_MODE_XYZ;
862 const CombinedKeyingResult result_1 = insert_keyframes(bmain,
863 &object_rna_pointer,
864 std::nullopt,
865 {{"rotation_mode"}},
866 std::nullopt,
867 anim_eval_context,
871 Channelbag *channelbag = get_channelbag_in_first_layer(*object);
872 FCurve *fcurve = channelbag->fcurve_find({"rotation_mode", 0});
873 EXPECT_EQ(5.0, fcurve->bezt[0].vec[1][0]);
874 EXPECT_EQ(float(ROT_MODE_XYZ), fcurve->bezt[0].vec[1][1]);
875
876 /* If the frame number *is* explicitly passed, it should be used. */
877 object->rotmode = ROT_MODE_QUAT;
878 const CombinedKeyingResult result_2 = insert_keyframes(bmain,
879 &object_rna_pointer,
880 std::nullopt,
881 {{"rotation_mode"}},
882 10.0,
883 anim_eval_context,
887 EXPECT_EQ(5.0, fcurve->bezt[0].vec[1][0]);
888 EXPECT_EQ(float(ROT_MODE_XYZ), fcurve->bezt[0].vec[1][1]);
889 EXPECT_EQ(10.0, fcurve->bezt[1].vec[1][0]);
890 EXPECT_EQ(float(ROT_MODE_QUAT), fcurve->bezt[1].vec[1][1]);
891}
892
893/* Passing the channel group explicitly vs not. */
894TEST_F(KeyframingTest, insert_keyframes__optional_channel_group)
895{
896 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
897
898 /* If the channel group is not explicitly passed, the default should be used. */
899 const CombinedKeyingResult result_1 = insert_keyframes(
900 bmain,
901 &object_rna_pointer,
902 std::nullopt,
903 {{"location", std::nullopt, 0}, {"visible_shadow"}},
904 1.0,
905 anim_eval_context,
909
910 Channelbag *channelbag = get_channelbag_in_first_layer(*object);
911 /* Location X should get the default transform group. */
912 FCurve *fcurve_location_x = channelbag->fcurve_find({"location", 0});
913 ASSERT_NE(nullptr, fcurve_location_x->grp);
914 EXPECT_EQ(0, strcmp("Object Transforms", fcurve_location_x->grp->name));
915
916 /* Shadow visibility should get no group. */
917 FCurve *fcurve_visible_shadow = channelbag->fcurve_find({"visible_shadow", 0});
918 ASSERT_EQ(nullptr, fcurve_visible_shadow->grp);
919
920 /* If the channel group *is* explicitly passed, it should override the default. */
921 const CombinedKeyingResult result_2 = insert_keyframes(
922 bmain,
923 &object_rna_pointer,
924 "Foo",
925 {{"location", std::nullopt, 1}, {"hide_render"}},
926 1.0,
927 anim_eval_context,
931
932 /* Both location Y and render visibility should get the "Foo" group. */
933 FCurve *fcurve_location_y = channelbag->fcurve_find({"location", 1});
934 ASSERT_NE(nullptr, fcurve_location_y->grp);
935 EXPECT_EQ(0, strcmp("Foo", fcurve_location_y->grp->name));
936 FCurve *fcurve_hide_render = channelbag->fcurve_find({"hide_render", 0});
937 ASSERT_NE(nullptr, fcurve_hide_render->grp);
938 EXPECT_EQ(0, strcmp("Foo", fcurve_hide_render->grp->name));
939}
940
941/* Inserting a key into an NLA strip that has a time offset should remap the
942 * key's time to the local time of the strip. */
943TEST_F(KeyframingTest, insert_keyframes__nla_time_remapping)
944{
945 BKE_nla_tweakmode_enter({object_with_nla->id, *object_with_nla->adt});
946 AnimationEvalContext anim_eval_context = {nullptr, 1.0};
947
949 &object_with_nla_rna_pointer,
950 std::nullopt,
951 {{"location", std::nullopt, 0}},
952 1.0,
953 anim_eval_context,
956
958 Action &act = nla_action->wrap();
959 Layer *layer = act.layer(0);
960 Strip *strip = layer->strip(0);
962 StripKeyframeData &strip_data = strip->data<animrig::StripKeyframeData>(act);
963 Channelbag *channelbag = strip_data.channelbag_for_slot(nla_action->wrap().slots()[0]->handle);
964 EXPECT_EQ(1, channelbag->fcurve_array_num);
965 FCurve *fcurve = channelbag->fcurve_find({"location", 0});
966 ASSERT_NE(nullptr, fcurve);
967 ASSERT_NE(nullptr, fcurve->bezt);
968 EXPECT_EQ(1, fcurve->totvert);
969 EXPECT_EQ(11.0, fcurve->bezt[0].vec[1][0]);
970 BKE_nla_tweakmode_exit({object_with_nla->id, *object_with_nla->adt});
971}
972
973} // namespace blender::animrig::tests
Functions and classes to work with Actions.
Functions to insert, delete or modify keyframes.
Blender kernel action and pose functionality.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define ID_REFCOUNTING_USERS(id)
Definition DNA_ID.h:684
Object is a sort of wrapper for general info.
Span< const Slot * > slots() const
const Layer * layer(int64_t index) const
const Slot * slot(int64_t index) const
Span< const Layer * > layers() const
Slot * slot_for_handle(slot_handle_t handle)
Span< const FCurve * > fcurves() const
const FCurve * fcurve_find(const FCurveDescriptor &fcurve_descriptor) const
int get_count(const SingleKeyingResult result) const
Span< const Strip * > strips() const
const Strip * strip(int64_t index) const
const Channelbag * channelbag_for_slot(const Slot &slot) const
Span< const Channelbag * > channelbags() const
const Channelbag * channelbag(int64_t index) const
const T & data(const Action &owning_action) const
Channelbag * get_channelbag_in_first_layer(Object &object)
#define layer
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
bool assign_action(NlaStrip &strip, Action &action, ID &animated_id)
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
bool assign_action(bAction *action, ID &animated_id)
ActionSlotAssignmentResult assign_action_slot(Slot *slot_to_assign, ID &animated_id)
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.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ BEZT_KEYTYPE_KEYFRAME
NlaTrack * BKE_nlatrack_new_head(ListBaseT< NlaTrack > *nla_tracks, bool is_liboverride)
bArmature * BKE_armature_add(Main *bmain, const char *name)
char * STRNCPY(char(&dst)[N], const char *src)
PointerRNA RNA_id_pointer_create(ID *id)
@ NLASTRIP_FLAG_ACTIVE
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
bool BKE_nlatrack_add_strip(NlaTrack *nlt, NlaStrip *strip, bool is_liboverride)
Main * BKE_main_new()
Definition main.cc:93
void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
void BKE_nla_tweakmode_exit(OwnedAnimData owned_adt)
NlaStrip * BKE_nlastrip_new(bAction *act, ID &animated_id)
@ INSERTKEY_AVAILABLE
@ NLASTRIP_MODE_COMBINE
@ BKE_MAT_ASSIGN_OBDATA
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_mesh_assign_object(Main *bmain, Object *ob, Mesh *mesh)
Material * BKE_material_add(Main *bmain, const char *name)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
Mesh * BKE_mesh_add(Main *bmain, const char *name)
void id_us_min(ID *id)
Definition lib_id.cc:441
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
bool BKE_nla_tweakmode_enter(OwnedAnimData owned_adt)
ListBaseT< NlaTrack > nla_tracks
bActionGroup * grp
unsigned int totvert
eNlaStrip_Blend_Mode blendmode
eNlaStrip_Flag flag
eNlaTrack_Flag flag
struct AnimData * adt
ListBaseT< Bone > bonebase