Blender V5.3
anim_filter_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_fcurve.hh"
7
8#include "BKE_action.hh"
9#include "BKE_anim_data.hh"
10#include "BKE_global.hh"
11#include "BKE_gtest_base.hh"
12#include "BKE_idtype.hh"
13#include "BKE_lib_id.hh"
14#include "BKE_main.hh"
15#include "BKE_object.hh"
16
17#include "DNA_anim_types.h"
18#include "DNA_object_types.h"
19#include "DNA_space_types.h"
20
21#include "ED_anim_api.hh"
22
23#include "BLI_listbase.hh"
24
25#include "testing/testing.h"
26
29 public:
34
35 void SetUp() override
36 {
38 G_MAIN = bmain; /* For BKE_animdata_free(). */
39
40 action = &BKE_id_new<bAction>(bmain, "ACÄnimåtië")->wrap();
43 }
44
45 void TearDown() override
46 {
48 G_MAIN = nullptr;
49 }
50};
51
52TEST_F(ActionFilterTest, slots_expanded_or_not)
53{
54 Slot &slot_cube = action->slot_add();
55 Slot &slot_suzanne = action->slot_add();
56 ASSERT_TRUE(assign_action(action, cube->id));
57 ASSERT_TRUE(assign_action(action, suzanne->id));
58 ASSERT_EQ(assign_action_slot(&slot_cube, cube->id), ActionSlotAssignmentResult::OK);
59 ASSERT_EQ(assign_action_slot(&slot_suzanne, suzanne->id), ActionSlotAssignmentResult::OK);
60
61 Layer &layer = action->layer_add("Kübus layer");
62 Strip &key_strip = layer.strip_add(*action, Strip::Type::Keyframe);
63 StripKeyframeData &strip_data = key_strip.data<StripKeyframeData>(*action);
64
65 /* Create multiple FCurves for multiple Slots. */
66 const KeyframeSettings settings = get_keyframe_settings(false);
67 ASSERT_EQ(
69 strip_data.keyframe_insert(bmain, slot_cube, {"location", 0}, {1.0f, 0.25f}, settings));
70 ASSERT_EQ(
72 strip_data.keyframe_insert(bmain, slot_cube, {"location", 1}, {1.0f, 0.25f}, settings));
73 ASSERT_EQ(
75 strip_data.keyframe_insert(bmain, slot_suzanne, {"location", 0}, {1.0f, 0.25f}, settings));
76 ASSERT_EQ(
78 strip_data.keyframe_insert(bmain, slot_suzanne, {"location", 1}, {1.0f, 0.25f}, settings));
79
80 Channelbag *cube_channelbag = strip_data.channelbag_for_slot(slot_cube);
81 ASSERT_NE(nullptr, cube_channelbag);
82 FCurve *fcu_cube_loc_x = cube_channelbag->fcurve_find({"location", 0});
83 FCurve *fcu_cube_loc_y = cube_channelbag->fcurve_find({"location", 1});
84 ASSERT_NE(nullptr, fcu_cube_loc_x);
85 ASSERT_NE(nullptr, fcu_cube_loc_y);
86
87 /* Mock an bAnimContext for the Animation editor, with the above Animation showing. */
88 SpaceAction saction = {};
90
91 bAnimContext ac = {nullptr};
92 ac.bmain = bmain;
94 ac.data = action;
96 ac.sl = reinterpret_cast<SpaceLink *>(&saction);
97 ac.obact = cube;
98 ac.active_action = action;
99 ac.active_action_user = &cube->id;
100 ac.ads = &saction.ads;
101
102 { /* Test with collapsed slots. */
103 slot_cube.set_expanded(false);
104 slot_suzanne.set_expanded(false);
105
106 /* This should produce 2 slots and no FCurves. */
107 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
111 const int num_entries = ANIM_animdata_filter(
112 &ac, &anim_data, filter, ac.data, eAnimCont_Types(ac.datatype));
113 EXPECT_EQ(2, num_entries);
114 EXPECT_EQ(2, anim_data.count());
115
116 ASSERT_GE(num_entries, 1)
117 << "Missing 1st ANIMTYPE_ACTION_SLOT entry, stopping to prevent crash";
118 const bAnimListElem *first_ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 0));
121 EXPECT_EQ(&cube->id, first_ale->id) << "id should be the animated ID (" << cube->id.name
122 << ") but is (" << first_ale->id->name << ")";
123 EXPECT_EQ(cube->adt, first_ale->adt) << "adt should be the animated ID's animation data";
124 EXPECT_EQ(&action->id, first_ale->fcurve_owner_id) << "fcurve_owner_id should be the Action";
125 EXPECT_EQ(&action->id, first_ale->key_data) << "key_data should be the Action";
126 EXPECT_EQ(&slot_cube, first_ale->data);
127 EXPECT_EQ(slot_cube.slot_flags, first_ale->flag);
128
129 ASSERT_GE(num_entries, 2)
130 << "Missing 2nd ANIMTYPE_ACTION_SLOT entry, stopping to prevent crash";
131 const bAnimListElem *second_ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 1));
132 EXPECT_EQ(ANIMTYPE_ACTION_SLOT, second_ale->type);
133 EXPECT_EQ(&slot_suzanne, second_ale->data);
134 /* Assume the rest is set correctly, as it's the same code as tested above. */
135
136 ANIM_animdata_freelist(&anim_data);
137 }
138
139 { /* Test with one expanded and one collapsed slot. */
140 slot_cube.set_expanded(true);
141 slot_suzanne.set_expanded(false);
142
143 /* This should produce 2 slots and 2 FCurves. */
144 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
148 const int num_entries = ANIM_animdata_filter(
149 &ac, &anim_data, filter, ac.data, eAnimCont_Types(ac.datatype));
150 EXPECT_EQ(4, num_entries);
151 EXPECT_EQ(4, anim_data.count());
152
153 /* First should be Cube slot. */
154 ASSERT_GE(num_entries, 1) << "Missing 1st ale, stopping to prevent crash";
155 const bAnimListElem *ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 0));
157 EXPECT_EQ(&slot_cube, ale->data);
158
159 /* After that the Cube's FCurves. */
160 ASSERT_GE(num_entries, 2) << "Missing 2nd ale, stopping to prevent crash";
161 ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 1));
163 EXPECT_EQ(fcu_cube_loc_x, ale->data);
164 EXPECT_EQ(slot_cube.handle, ale->slot_handle);
165
166 ASSERT_GE(num_entries, 3) << "Missing 3rd ale, stopping to prevent crash";
167 ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 2));
169 EXPECT_EQ(fcu_cube_loc_y, ale->data);
170 EXPECT_EQ(slot_cube.handle, ale->slot_handle);
171
172 /* And finally the Suzanne slot. */
173 ASSERT_GE(num_entries, 4) << "Missing 4th ale, stopping to prevent crash";
174 ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 3));
176 EXPECT_EQ(&slot_suzanne, ale->data);
177
178 ANIM_animdata_freelist(&anim_data);
179 }
180
181 { /* Test one expanded and one collapsed slot, and one Slot and one FCurve selected. */
182 slot_cube.set_expanded(true);
183 slot_cube.set_selected(false);
184 slot_suzanne.set_expanded(false);
185 slot_suzanne.set_selected(true);
186
187 fcu_cube_loc_x->flag &= ~FCURVE_SELECTED;
188 fcu_cube_loc_y->flag |= FCURVE_SELECTED;
189
190 /* This should produce 1 slot and 1 FCurve. */
191 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
195 const int num_entries = ANIM_animdata_filter(
196 &ac, &anim_data, filter, ac.data, eAnimCont_Types(ac.datatype));
197 EXPECT_EQ(2, num_entries);
198 EXPECT_EQ(2, anim_data.count());
199
200 /* First should be Cube's selected FCurve. */
201 const bAnimListElem *ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 0));
203 EXPECT_EQ(fcu_cube_loc_y, ale->data);
204
205 /* Second the Suzanne slot, as that's the only selected slot. */
206 ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 1));
208 EXPECT_EQ(&slot_suzanne, ale->data);
209
210 ANIM_animdata_freelist(&anim_data);
211 }
212}
213
214TEST_F(ActionFilterTest, layered_action_active_fcurves)
215{
216 Slot &slot_cube = action->slot_add();
217 /* The Action+Slot has to be assigned to what the bAnimContext thinks is the active Object.
218 * See the BLI_assert_msg() call in the ANIMCONT_ACTION case of ANIM_animdata_filter(). */
219 ASSERT_EQ(assign_action_and_slot(action, &slot_cube, cube->id), ActionSlotAssignmentResult::OK);
220
221 Layer &layer = action->layer_add("Kübus layer");
222 Strip &key_strip = layer.strip_add(*action, Strip::Type::Keyframe);
223 StripKeyframeData &strip_data = key_strip.data<StripKeyframeData>(*action);
224
225 /* Create multiple FCurves. */
226 const KeyframeSettings settings = get_keyframe_settings(false);
227 ASSERT_EQ(
229 strip_data.keyframe_insert(bmain, slot_cube, {"location", 0}, {1.0f, 0.25f}, settings));
230 ASSERT_EQ(
232 strip_data.keyframe_insert(bmain, slot_cube, {"location", 1}, {1.0f, 0.25f}, settings));
233
234 /* Set one F-Curve as the active one, and the other as inactive. The latter is necessary because
235 * by default the first curve is automatically marked active, but that's too trivial a test case
236 * (it's too easy to mistakenly just return the first-seen F-Curve). */
237 Channelbag *cube_channelbag = strip_data.channelbag_for_slot(slot_cube);
238 ASSERT_NE(nullptr, cube_channelbag);
239 FCurve *fcurve_active = cube_channelbag->fcurve_find({"location", 1});
240 fcurve_active->flag |= FCURVE_ACTIVE;
241 FCurve *fcurve_other = cube_channelbag->fcurve_find({"location", 0});
242 fcurve_other->flag &= ~FCURVE_ACTIVE;
243
244 /* Mock an bAnimContext for the Action editor. */
245 SpaceAction saction = {};
247
248 bAnimContext ac = {nullptr};
249 ac.bmain = bmain;
251 ac.data = action;
253 ac.sl = reinterpret_cast<SpaceLink *>(&saction);
254 ac.obact = cube;
255 ac.active_action = action;
256 ac.active_action_user = &cube->id;
257 ac.ads = &saction.ads;
258
259 {
260 /* This should produce just the active F-Curve. */
261 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
264 const int num_entries = ANIM_animdata_filter(
265 &ac, &anim_data, filter, ac.data, eAnimCont_Types(ac.datatype));
266 EXPECT_EQ(1, num_entries);
267 EXPECT_EQ(1, anim_data.count());
268
269 const bAnimListElem *first_ale = static_cast<bAnimListElem *>(BLI_findlink(&anim_data, 0));
270 EXPECT_EQ(ANIMTYPE_FCURVE, first_ale->type);
271 EXPECT_EQ(ALE_FCURVE, first_ale->datatype);
272 EXPECT_EQ(fcurve_active, first_ale->data);
273
274 ANIM_animdata_freelist(&anim_data);
275 }
276}
277
278} // namespace blender::animrig::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.
Object is a sort of wrapper for general info.
const FCurve * fcurve_find(const FCurveDescriptor &fcurve_descriptor) const
const Channelbag * channelbag_for_slot(const Slot &slot) const
SingleKeyingResult keyframe_insert(Main *bmain, const Slot &slot, const FCurveDescriptor &fcurve_descriptor, float2 time_value, const KeyframeSettings &settings, eInsertKeyFlags insert_key_flags=INSERTKEY_NOFLAGS, std::optional< float2 > cycle_range=std::nullopt)
const T & data(const Action &owning_action) const
#define layer
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
KeyframeSettings get_keyframe_settings(bool from_userprefs)
ActionSlotAssignmentResult assign_action_and_slot(Action *action, Slot *slot_to_assign, ID &animated_id)
bool assign_action(bAction *action, ID &animated_id)
ActionSlotAssignmentResult assign_action_slot(Slot *slot_to_assign, ID &animated_id)
@ ANIMCONT_ACTION
Main * BKE_main_new()
Definition main.cc:93
@ ANIMTYPE_FCURVE
@ ANIMTYPE_ACTION_SLOT
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
@ ALE_ACTION_SLOT
size_t ANIM_animdata_filter(bAnimContext *ac, ListBaseT< bAnimListElem > *anim_data, const eAnimFilter_Flags filter_mode, void *data, const eAnimCont_Types datatype)
@ ANIMFILTER_FCURVESONLY
@ ANIMFILTER_FOREDIT
@ ANIMFILTER_LIST_VISIBLE
@ ANIMFILTER_DATA_VISIBLE
@ ANIMFILTER_ACTIVE
@ ANIMFILTER_NODUPLIS
@ ANIMFILTER_LIST_CHANNELS
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void ANIM_animdata_freelist(ListBaseT< bAnimListElem > *anim_data)
Definition anim_deps.cc:472
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
eFCurve_Flags flag
char name[258]
Definition DNA_ID.h:433
eAnimCont_Types datatype
eAnim_ChannelType type
eDopeSheet_FilterFlag filterflag