Blender V5.0
anim_data_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_action.hh"
6
7#include "ANIM_action.hh"
8
9#include "DNA_action_types.h"
10#include "DNA_anim_types.h"
11
12#include "BLI_vector.hh"
13
14#include "testing/testing.h"
15
16namespace blender::bke::tests {
17
18TEST(anim_data, BKE_fcurves_id_cb_test)
19{
20 /* BKE_id_free() hits a code path that uses CLOG, which crashes if not initialized properly. */
21 CLG_init();
22 /* To make id_can_have_animdata() and friends work, the `id_types` array needs to be set up. */
24
25 Main *bmain = BKE_main_new();
26 Action *action = BKE_id_new<Action>(bmain, "ACÄnimåtië");
27 Object *cube = BKE_object_add_only_object(bmain, OB_EMPTY, "Küüübus");
28 Object *suzanne = BKE_object_add_only_object(bmain, OB_EMPTY, "OBSuzanne");
29
30 /* Create F-Curves for Cube. */
31 ASSERT_TRUE(animrig::assign_action(action, cube->id));
32 const FCurve *fcurve_cube_loc1 = animrig::action_fcurve_ensure(
33 bmain, *action, cube->id, {"location", 1});
34 const FCurve *fcurve_cube_scale2 = animrig::action_fcurve_ensure(
35 bmain, *action, cube->id, {"scale", 2});
36
37 /* Create F-Curves for Suzanne. */
38 ASSERT_TRUE(animrig::assign_action(action, suzanne->id));
39 const FCurve *fcurve_suzanne_loc0 = animrig::action_fcurve_ensure(
40 bmain, *action, suzanne->id, {"location", 0});
41 const FCurve *fcurve_suzanne_scale1 = animrig::action_fcurve_ensure(
42 bmain, *action, suzanne->id, {"scale", 1});
43
44 /* Test that BKE_fcurves_id_cb only reports F-Curves that are meant for that ID. */
45 Vector<ID *> reported_ids;
46 Vector<FCurve *> reported_fcurves;
47 const auto callback = [&reported_fcurves](ID *id, FCurve *fcurve) {
48 reported_ids.push_back(id);
49 reported_fcurves.push_back(fcurve);
50 };
51
52 BKE_fcurves_id_cb(&cube->id, callback);
53 EXPECT_EQ(1, reported_ids.size());
54 EXPECT_EQ(2, reported_fcurves.size());
55 EXPECT_EQ(&cube->id, reported_ids[0]);
56 EXPECT_EQ(fcurve_cube_loc1, reported_fcurves[0]);
57 EXPECT_EQ(fcurve_cube_scale2, reported_fcurves[1]);
58
59 /* Also test for Suzanne, as that's a different slot. The first slot could
60 * have been covered by legacy-compatible code. */
61 reported_ids.clear();
62 reported_fcurves.clear();
63
64 BKE_fcurves_id_cb(&cube->id, callback);
65 EXPECT_EQ(1, reported_ids.size());
66 EXPECT_EQ(2, reported_fcurves.size());
67 EXPECT_EQ(&suzanne->id, reported_ids[0]);
68 EXPECT_EQ(fcurve_suzanne_loc0, reported_fcurves[0]);
69 EXPECT_EQ(fcurve_suzanne_scale1, reported_fcurves[1]);
70
71 BKE_main_free(bmain);
72 CLG_exit();
73}
74
75} // namespace blender::bke::tests
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
void BKE_fcurves_id_cb(struct ID *id, blender::FunctionRef< void(ID *, FCurve *)> func)
void BKE_idtype_init()
Definition idtype.cc:121
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1514
Main * BKE_main_new()
Definition main.cc:89
void BKE_main_free(Main *bmain)
Definition main.cc:192
Object * BKE_object_add_only_object(Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void CLG_exit()
Definition clog.cc:880
void CLG_init()
Definition clog.cc:873
@ OB_EMPTY
int64_t size() const
bool assign_action(bAction *action, ID &animated_id)
FCurve & action_fcurve_ensure(Main *bmain, bAction &action, ID &animated_id, const FCurveDescriptor &fcurve_descriptor)
TEST(action_groups, ReconstructGroupsWithReordering)
Definition DNA_ID.h:414