Blender V5.3
keyframes_keylist_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "ANIM_action.hh"
8#include "ANIM_fcurve.hh"
9
10#include "ED_anim_api.hh"
12
13#include "DNA_anim_types.h"
14#include "DNA_curve_types.h"
15
16#include "MEM_guardedalloc.h"
17
18#include "BKE_action.hh"
19#include "BKE_armature.hh"
20#include "BKE_fcurve.hh"
21#include "BKE_global.hh"
22#include "BKE_gtest_base.hh"
23#include "BKE_idtype.hh"
24#include "BKE_lib_id.hh"
25#include "BKE_main.hh"
26#include "BKE_object.hh"
27
28#include "BLI_listbase.hh"
29#include "BLI_string_utf8.hh"
30
31#include <functional>
32#include <optional>
33
35
36const float KEYLIST_NEAR_ERROR = 0.1;
37const float FRAME_STEP = 0.005;
38
39/* Build FCurve with keys on frames 10, 20, and 30. */
40static void build_fcurve(FCurve &fcurve)
41{
42 fcurve.totvert = 3;
43 fcurve.bezt = MEM_new_array_zeroed<BezTriple>(fcurve.totvert, "BezTriples");
44 fcurve.bezt[0].vec[1][0] = 10.0f;
45 fcurve.bezt[0].vec[1][1] = 1.0f;
46 fcurve.bezt[1].vec[1][0] = 20.0f;
47 fcurve.bezt[1].vec[1][1] = 2.0f;
48 fcurve.bezt[2].vec[1][0] = 30.0f;
49 fcurve.bezt[2].vec[1][1] = 1.0f;
50}
51
53{
54 FCurve *fcurve = BKE_fcurve_create();
55 build_fcurve(*fcurve);
56
57 AnimKeylist *keylist = ED_keylist_create();
58 fcurve_to_keylist(nullptr, fcurve, keylist, 0, {-FLT_MAX, FLT_MAX}, false);
59 BKE_fcurve_free(fcurve);
60
62 return keylist;
63}
64
65static void assert_act_key_column(const ActKeyColumn *column,
66 const std::optional<float> expected_frame)
67{
68 if (expected_frame.has_value()) {
69 ASSERT_NE(column, nullptr) << "Expected a frame to be found at " << *expected_frame;
70 EXPECT_NEAR(column->cfra, *expected_frame, KEYLIST_NEAR_ERROR);
71 }
72 else {
73 EXPECT_EQ(column, nullptr) << "Expected no frame to be found, but found " << column->cfra;
74 }
75}
76
77using KeylistFindFunction = std::function<const ActKeyColumn *(const AnimKeylist *, float)>;
78
79static void check_keylist_find_range(const AnimKeylist *keylist,
80 KeylistFindFunction keylist_find_func,
81 const float frame_from,
82 const float frame_to,
83 const std::optional<float> expected_frame)
84{
85 float cfra = frame_from;
86 for (; cfra < frame_to; cfra += FRAME_STEP) {
87 const ActKeyColumn *found = keylist_find_func(keylist, cfra);
88 assert_act_key_column(found, expected_frame);
89 }
90}
91
92static void check_keylist_find_next_range(const AnimKeylist *keylist,
93 const float frame_from,
94 const float frame_to,
95 const std::optional<float> expected_frame)
96{
97 check_keylist_find_range(keylist, ED_keylist_find_next, frame_from, frame_to, expected_frame);
98}
99
101
103{
104 AnimKeylist *keylist = create_test_keylist();
105
106 check_keylist_find_next_range(keylist, 0.0f, 9.99f, 10.0f);
107 check_keylist_find_next_range(keylist, 10.0f, 19.99f, 20.0f);
108 check_keylist_find_next_range(keylist, 20.0f, 29.99f, 30.0f);
109 check_keylist_find_next_range(keylist, 30.0f, 39.99f, std::nullopt);
110
111 ED_keylist_free(keylist);
112}
113
115 const float frame_from,
116 const float frame_to,
117 const std::optional<float> expected_frame)
118{
119 check_keylist_find_range(keylist, ED_keylist_find_prev, frame_from, frame_to, expected_frame);
120}
121
123{
124 AnimKeylist *keylist = create_test_keylist();
125
126 check_keylist_find_prev_range(keylist, 0.0f, 10.00f, std::nullopt);
127 check_keylist_find_prev_range(keylist, 10.01f, 20.00f, 10.0f);
128 check_keylist_find_prev_range(keylist, 20.01f, 30.00f, 20.0f);
129 check_keylist_find_prev_range(keylist, 30.01f, 49.99f, 30.0f);
130
131 ED_keylist_free(keylist);
132}
133
135 const float frame_from,
136 const float frame_to,
137 const std::optional<float> expected_frame)
138{
139 check_keylist_find_range(keylist, ED_keylist_find_exact, frame_from, frame_to, expected_frame);
140}
141
142TEST_F(KeyListTest, find_exact)
143{
144 AnimKeylist *keylist = create_test_keylist();
145
146 check_keylist_find_exact_range(keylist, 0.0f, 9.99f, std::nullopt);
147 check_keylist_find_exact_range(keylist, 9.9901f, 10.01f, 10.0f);
148 check_keylist_find_exact_range(keylist, 10.01f, 19.99f, std::nullopt);
149 check_keylist_find_exact_range(keylist, 19.9901f, 20.01f, 20.0f);
150 check_keylist_find_exact_range(keylist, 20.01f, 29.99f, std::nullopt);
151 check_keylist_find_exact_range(keylist, 29.9901f, 30.01f, 30.0f);
152 check_keylist_find_exact_range(keylist, 30.01f, 49.99f, std::nullopt);
153
154 ED_keylist_free(keylist);
155}
156
157TEST_F(KeyListTest, find_closest)
158{
159 AnimKeylist *keylist = create_test_keylist();
160
161 {
162 const ActKeyColumn *closest = ED_keylist_find_closest(keylist, -1);
163 EXPECT_EQ(closest->cfra, 10.0);
164 }
165
166 {
167 const ActKeyColumn *closest = ED_keylist_find_closest(keylist, 10);
168 EXPECT_EQ(closest->cfra, 10.0);
169 }
170
171 {
172 const ActKeyColumn *closest = ED_keylist_find_closest(keylist, 14.999);
173 EXPECT_EQ(closest->cfra, 10.0);
174 }
175 {
176 /* When the distance between key columns is equal, the previous column is chosen */
177 const ActKeyColumn *closest = ED_keylist_find_closest(keylist, 15);
178 EXPECT_EQ(closest->cfra, 10.0);
179 }
180 {
181 const ActKeyColumn *closest = ED_keylist_find_closest(keylist, 15.001);
182 EXPECT_EQ(closest->cfra, 20.0);
183 }
184 {
185 const ActKeyColumn *closest = ED_keylist_find_closest(keylist, 30.001);
186 EXPECT_EQ(closest->cfra, 30.0);
187 }
188 ED_keylist_free(keylist);
189}
190
192 public:
200
202 bAnimContext ac = {nullptr};
203
204 void SetUp() override
205 {
207 G_MAIN = bmain; /* For BKE_animdata_free(). */
208
209 action = &BKE_id_new<bAction>(bmain, "ACÄnimåtië")->wrap();
211
212 armature_data = BKE_armature_add(bmain, "ARArmature");
213 bone1 = MEM_new<Bone>("KeylistSummaryTest");
214 bone2 = MEM_new<Bone>("KeylistSummaryTest");
215 STRNCPY_UTF8(bone1->name, "Bone.001");
216 STRNCPY_UTF8(bone2->name, "Bone.002");
220
222 armature->data = id_cast<ID *>(armature_data);
224
225 /*
226 * Fill in the common bits for the mock bAnimContext, for an Action editor.
227 *
228 * Tests should fill in:
229 * - ac.obact
230 * - ac.active_action_user (= &ac.obact.id)
231 */
233 ac.bmain = bmain;
235 ac.data = action;
237 ac.sl = reinterpret_cast<SpaceLink *>(&saction);
238 ac.ads = &saction.ads;
240 }
241
242 void TearDown() override
243 {
244 ac.obact = nullptr;
245 ac.active_action = nullptr;
246 ac.active_action_user = nullptr;
247
249 G_MAIN = nullptr;
250 }
251};
252
253TEST_F(KeylistSummaryTest, slot_summary_simple)
254{
255 /* Test that a key summary is generated correctly for a slot that's animating
256 * an object's transforms. */
257
258 using namespace blender::animrig;
259
260 Slot &slot_cube = action->slot_add_for_id(cube->id);
261 ASSERT_EQ(ActionSlotAssignmentResult::OK, assign_action_and_slot(action, &slot_cube, cube->id));
262 Channelbag &channelbag = action_channelbag_ensure(*action, cube->id);
263
264 FCurve &loc_x = channelbag.fcurve_ensure(bmain, {"location", 0});
265 FCurve &loc_y = channelbag.fcurve_ensure(bmain, {"location", 1});
266 FCurve &loc_z = channelbag.fcurve_ensure(bmain, {"location", 2});
267
268 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&loc_x, {1.0, 0.0}, {}, {}));
269 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&loc_x, {2.0, 1.0}, {}, {}));
270 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&loc_y, {2.0, 2.0}, {}, {}));
271 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&loc_y, {3.0, 3.0}, {}, {}));
272 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&loc_z, {2.0, 4.0}, {}, {}));
273 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&loc_z, {5.0, 5.0}, {}, {}));
274
275 /* Generate slot summary keylist. */
276 AnimKeylist *keylist = ED_keylist_create();
277 ac.obact = cube;
278 ac.active_action_user = &cube->id;
280 &ac, &cube->id, *action, slot_cube.handle, keylist, 0, {0.0, 6.0});
282
283 const ActKeyColumn *col_0 = ED_keylist_find_exact(keylist, 0.0);
284 const ActKeyColumn *col_1 = ED_keylist_find_exact(keylist, 1.0);
285 const ActKeyColumn *col_2 = ED_keylist_find_exact(keylist, 2.0);
286 const ActKeyColumn *col_3 = ED_keylist_find_exact(keylist, 3.0);
287 const ActKeyColumn *col_4 = ED_keylist_find_exact(keylist, 4.0);
288 const ActKeyColumn *col_5 = ED_keylist_find_exact(keylist, 5.0);
289 const ActKeyColumn *col_6 = ED_keylist_find_exact(keylist, 6.0);
290
291 /* Check that we only have columns at the frames with keys. */
292 EXPECT_EQ(nullptr, col_0);
293 EXPECT_NE(nullptr, col_1);
294 EXPECT_NE(nullptr, col_2);
295 EXPECT_NE(nullptr, col_3);
296 EXPECT_EQ(nullptr, col_4);
297 EXPECT_NE(nullptr, col_5);
298 EXPECT_EQ(nullptr, col_6);
299
300 /* Check that the right number of keys are indicated in each column. */
301 EXPECT_EQ(1, col_1->totkey);
302 EXPECT_EQ(3, col_2->totkey);
303 EXPECT_EQ(1, col_3->totkey);
304 EXPECT_EQ(1, col_5->totkey);
305
306 ED_keylist_free(keylist);
307}
308
309TEST_F(KeylistSummaryTest, slot_summary_bone_selection)
310{
311 /* Test that a key summary is generated correctly, excluding keys for
312 * unselected bones when filter-by-selection is on. */
313
314 using namespace blender::animrig;
315
316 Slot &slot_armature = action->slot_add_for_id(armature->id);
318 assign_action_and_slot(action, &slot_armature, armature->id));
319 Channelbag &channelbag = action_channelbag_ensure(*action, armature->id);
320
321 FCurve &bone1_loc_x = channelbag.fcurve_ensure(
322 bmain, {"pose.bones[\"Bone.001\"].location", 0, {}, {}, "Bone.001"});
323 FCurve &bone2_loc_x = channelbag.fcurve_ensure(
324 bmain, {"pose.bones[\"Bone.002\"].location", 0, {}, {}, "Bone.002"});
325
326 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&bone1_loc_x, {1.0, 0.0}, {}, {}));
327 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&bone1_loc_x, {2.0, 1.0}, {}, {}));
328 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&bone2_loc_x, {2.0, 2.0}, {}, {}));
329 ASSERT_EQ(SingleKeyingResult::SUCCESS, insert_vert_fcurve(&bone2_loc_x, {3.0, 3.0}, {}, {}));
330
331 /* Select only Bone.001. */
332 bPoseChannel *pose_bone1 = BKE_pose_channel_find_name(armature->pose, bone1->name);
333 ASSERT_NE(pose_bone1, nullptr);
334 pose_bone1->flag |= POSE_SELECTED;
335 bPoseChannel *pose_bone2 = BKE_pose_channel_find_name(armature->pose, bone2->name);
336 pose_bone2->flag &= ~POSE_SELECTED;
337
338 /* Generate slot summary keylist. */
339 AnimKeylist *keylist = ED_keylist_create();
340 saction.ads.filterflag = ADS_FILTER_ONLYSEL; /* Filter by selection. */
341 ac.obact = armature;
342 ac.active_action_user = &armature->id;
343 ac.filters.flag = eDopeSheet_FilterFlag(saction.ads.filterflag);
345 &ac, &armature->id, *action, slot_armature.handle, keylist, 0, {0.0, 6.0});
347
348 const ActKeyColumn *col_1 = ED_keylist_find_exact(keylist, 1.0);
349 const ActKeyColumn *col_2 = ED_keylist_find_exact(keylist, 2.0);
350 const ActKeyColumn *col_3 = ED_keylist_find_exact(keylist, 3.0);
351
352 /* Check that we only have columns at the frames with keys for Bone.001. */
353 EXPECT_NE(nullptr, col_1);
354 EXPECT_NE(nullptr, col_2);
355 EXPECT_EQ(nullptr, col_3);
356
357 /* Check that the right number of keys are indicated in each column. */
358 EXPECT_EQ(1, col_1->totkey);
359 EXPECT_EQ(1, col_2->totkey);
360
361 ED_keylist_free(keylist);
362}
363
364} // namespace blender::editor::animation::tests
Functions and classes to work with Actions.
Functions to modify FCurves.
Blender kernel action and pose functionality.
#define G_MAIN
General operations, lookup, etc. for blender objects.
#define STRNCPY_UTF8(dst, src)
Read Blender memory allocation and freeing.
bool closest(btVector3 &v)
FCurve & fcurve_ensure(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
#define FLT_MAX
#define EXPECT_NE(result, expect)
#define EXPECT_NEAR(result, expect, threshold)
ActionSlotAssignmentResult assign_action_and_slot(Action *action, Slot *slot_to_assign, ID &animated_id)
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.
static void check_keylist_find_next_range(const AnimKeylist *keylist, const float frame_from, const float frame_to, const std::optional< float > expected_frame)
static void check_keylist_find_exact_range(const AnimKeylist *keylist, const float frame_from, const float frame_to, const std::optional< float > expected_frame)
static void assert_act_key_column(const ActKeyColumn *column, const std::optional< float > expected_frame)
static void build_fcurve(FCurve &fcurve)
static void check_keylist_find_prev_range(const AnimKeylist *keylist, const float frame_from, const float frame_to, const std::optional< float > expected_frame)
std::function< const ActKeyColumn *(const AnimKeylist *, float)> KeylistFindFunction
static void check_keylist_find_range(const AnimKeylist *keylist, KeylistFindFunction keylist_find_func, const float frame_from, const float frame_to, const std::optional< float > expected_frame)
bArmature * BKE_armature_add(Main *bmain, const char *name)
@ ANIMCONT_ACTION
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
Main * BKE_main_new()
Definition main.cc:93
const ActKeyColumn * ED_keylist_find_prev(const AnimKeylist *keylist, const float cfra)
const ActKeyColumn * ED_keylist_find_exact(const AnimKeylist *keylist, const float cfra)
const ActKeyColumn * ED_keylist_find_next(const AnimKeylist *keylist, const float cfra)
void fcurve_to_keylist(AnimData *adt, FCurve *fcu, AnimKeylist *keylist, const int saction_flag, float2 range, const bool use_nla_remapping)
void ED_keylist_prepare_for_direct_access(AnimKeylist *keylist)
void action_slot_summary_to_keylist(bAnimContext *ac, ID *animated_id, animrig::Action &action, const animrig::slot_handle_t slot_handle, AnimKeylist *keylist, const int saction_flag, float2 range)
AnimKeylist * ED_keylist_create()
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
void BKE_armature_bone_hash_make(bArmature *arm)
void ED_keylist_free(AnimKeylist *keylist)
const ActKeyColumn * ED_keylist_find_closest(const AnimKeylist *keylist, const float cfra)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
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)
FCurve * BKE_fcurve_create()
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
unsigned int totvert
eAnimCont_Types datatype
ListBaseT< Bone > bonebase
eDopeSheet_FilterFlag filterflag