Blender V5.0
blenkernel/intern/action_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string_utf8.h"
6
7#include "BKE_action.hh"
8
9#include "ANIM_action.hh"
10
11#include "DNA_action_types.h"
12#include "DNA_anim_types.h"
13
14#include "BLI_listbase.h"
15
16#include "testing/testing.h"
17
19
20TEST(action_groups, ReconstructGroupsWithReordering)
21{
22 /* Construct an Action with three groups. */
23 bAction action = {{nullptr}};
24 FCurve groupAcurve1 = {nullptr};
25 FCurve groupAcurve2 = {nullptr};
26 FCurve groupBcurve1 = {nullptr};
27 FCurve groupBcurve2 = {nullptr};
28 FCurve groupBcurve3 = {nullptr};
29 /* Group C has no curves intentionally. */
30 FCurve groupDcurve1 = {nullptr};
31 FCurve groupDcurve2 = {nullptr};
32
33 groupAcurve1.rna_path = (char *)"groupAcurve1";
34 groupAcurve2.rna_path = (char *)"groupAcurve2";
35 groupBcurve1.rna_path = (char *)"groupBcurve1";
36 groupBcurve2.rna_path = (char *)"groupBcurve2";
37 groupDcurve1.rna_path = (char *)"groupDcurve1";
38 groupBcurve3.rna_path = (char *)"groupBcurve3";
39 groupDcurve2.rna_path = (char *)"groupDcurve2";
40
41 BLI_addtail(&action.curves, &groupAcurve1);
42 BLI_addtail(&action.curves, &groupAcurve2);
43 BLI_addtail(&action.curves, &groupBcurve1);
44 BLI_addtail(&action.curves, &groupBcurve2);
45 BLI_addtail(&action.curves, &groupDcurve1);
46 BLI_addtail(&action.curves, &groupBcurve3); /* <-- The error that should be corrected. */
47 BLI_addtail(&action.curves, &groupDcurve2);
48
49 /* Introduce another error type, by changing some `prev` pointers. */
50 groupBcurve1.prev = nullptr;
51 groupBcurve3.prev = &groupBcurve2;
52 groupDcurve1.prev = &groupBcurve3;
53
54 bActionGroup groupA = {nullptr};
55 bActionGroup groupB = {nullptr};
56 bActionGroup groupC = {nullptr};
57 bActionGroup groupD = {nullptr};
58 STRNCPY_UTF8(groupA.name, "groupA");
59 STRNCPY_UTF8(groupB.name, "groupB");
60 STRNCPY_UTF8(groupC.name, "groupC");
61 STRNCPY_UTF8(groupD.name, "groupD");
62
63 BLI_addtail(&action.groups, &groupA);
64 BLI_addtail(&action.groups, &groupB);
65 BLI_addtail(&action.groups, &groupC);
66 BLI_addtail(&action.groups, &groupD);
67
68 groupAcurve1.grp = &groupA;
69 groupAcurve2.grp = &groupA;
70 groupBcurve1.grp = &groupB;
71 groupBcurve2.grp = &groupB;
72 groupBcurve3.grp = &groupB;
73 groupDcurve1.grp = &groupD;
74 groupDcurve2.grp = &groupD;
75
76 groupA.channels.first = &groupAcurve1;
77 groupA.channels.last = &groupAcurve2;
78 groupB.channels.first = &groupBcurve1;
79 groupB.channels.last = &groupBcurve3; /* The last channel in group B, after group C curve 1. */
80 groupD.channels.first = &groupDcurve1;
81 groupD.channels.last = &groupDcurve2;
82
83 EXPECT_EQ(groupA.channels.first, &groupAcurve1);
84 EXPECT_EQ(groupA.channels.last, &groupAcurve2);
85 EXPECT_EQ(groupB.channels.first, &groupBcurve1);
86 EXPECT_EQ(groupB.channels.last, &groupBcurve3);
87 EXPECT_EQ(groupC.channels.first, nullptr);
88 EXPECT_EQ(groupC.channels.last, nullptr);
89 EXPECT_EQ(groupD.channels.first, &groupDcurve1);
90 EXPECT_EQ(groupD.channels.last, &groupDcurve2);
91
93
94 EXPECT_EQ(action.curves.first, &groupAcurve1);
95 EXPECT_EQ(action.curves.last, &groupDcurve2);
96
97 EXPECT_EQ(groupA.prev, nullptr);
98 EXPECT_EQ(groupB.prev, &groupA);
99 EXPECT_EQ(groupC.prev, &groupB);
100 EXPECT_EQ(groupD.prev, &groupC);
101
102 EXPECT_EQ(groupA.next, &groupB);
103 EXPECT_EQ(groupB.next, &groupC);
104 EXPECT_EQ(groupC.next, &groupD);
105 EXPECT_EQ(groupD.next, nullptr);
106
107 EXPECT_EQ(groupA.channels.first, &groupAcurve1);
108 EXPECT_EQ(groupA.channels.last, &groupAcurve2);
109 EXPECT_EQ(groupB.channels.first, &groupBcurve1);
110 EXPECT_EQ(groupB.channels.last, &groupBcurve3);
111 EXPECT_EQ(groupC.channels.first, nullptr);
112 EXPECT_EQ(groupC.channels.last, nullptr);
113 EXPECT_EQ(groupD.channels.first, &groupDcurve1);
114 EXPECT_EQ(groupD.channels.last, &groupDcurve2);
115
116 EXPECT_EQ(groupAcurve1.prev, nullptr);
117 EXPECT_EQ(groupAcurve2.prev, &groupAcurve1);
118 EXPECT_EQ(groupBcurve1.prev, &groupAcurve2);
119 EXPECT_EQ(groupBcurve2.prev, &groupBcurve1);
120 EXPECT_EQ(groupBcurve3.prev, &groupBcurve2);
121 EXPECT_EQ(groupDcurve1.prev, &groupBcurve3);
122 EXPECT_EQ(groupDcurve2.prev, &groupDcurve1);
123
124 EXPECT_EQ(groupAcurve1.next, &groupAcurve2);
125 EXPECT_EQ(groupAcurve2.next, &groupBcurve1);
126 EXPECT_EQ(groupBcurve1.next, &groupBcurve2);
127 EXPECT_EQ(groupBcurve2.next, &groupBcurve3);
128 EXPECT_EQ(groupBcurve3.next, &groupDcurve1);
129 EXPECT_EQ(groupDcurve1.next, &groupDcurve2);
130 EXPECT_EQ(groupDcurve2.next, nullptr);
131}
132
133namespace {
134
135/* Allocate fcu->bezt, and also return a unique_ptr to it for easily freeing the memory. */
136std::unique_ptr<BezTriple[]> allocate_keyframes(FCurve *fcu, const size_t num_keyframes)
137{
138 auto bezt_uptr = std::make_unique<BezTriple[]>(num_keyframes);
139 fcu->bezt = bezt_uptr.get();
140 return bezt_uptr;
141}
142
143/* Append keyframe, assumes that fcu->bezt is allocated and has enough space. */
144void add_keyframe(FCurve *fcu, float x, float y)
145{
146 /* The insert_keyframe functions are in the editors, so we cannot link to those here. */
147 BezTriple the_keyframe = {};
148
149 /* Copied from insert_vert_fcurve() in `keyframing.cc`. */
150 the_keyframe.vec[0][0] = x - 1.0f;
151 the_keyframe.vec[0][1] = y;
152 the_keyframe.vec[1][0] = x;
153 the_keyframe.vec[1][1] = y;
154 the_keyframe.vec[2][0] = x + 1.0f;
155 the_keyframe.vec[2][1] = y;
156
157 memcpy(&fcu->bezt[fcu->totvert], &the_keyframe, sizeof(the_keyframe));
158 fcu->totvert++;
159}
160
161} // namespace
162
163TEST(action_assets, BKE_action_has_single_frame)
164{
165 /* No FCurves. */
166 {
167 const bAction empty = {{nullptr}};
168 EXPECT_FALSE(empty.wrap().has_single_frame())
169 << "Action without FCurves cannot have a single frame.";
170 }
171
172 /* One curve with one key. */
173 {
174 FCurve fcu = {nullptr};
175 std::unique_ptr<BezTriple[]> bezt = allocate_keyframes(&fcu, 1);
176 add_keyframe(&fcu, 1.0f, 2.0f);
177
178 bAction action = {{nullptr}};
179 BLI_addtail(&action.curves, &fcu);
180
181 EXPECT_TRUE(action.wrap().has_single_frame())
182 << "Action with one FCurve and one key should have single frame.";
183 }
184
185 /* Two curves with one key each. */
186 {
187 FCurve fcu1 = {nullptr};
188 FCurve fcu2 = {nullptr};
189 std::unique_ptr<BezTriple[]> bezt1 = allocate_keyframes(&fcu1, 1);
190 std::unique_ptr<BezTriple[]> bezt2 = allocate_keyframes(&fcu2, 1);
191 add_keyframe(&fcu1, 1.0f, 327.0f);
192 add_keyframe(&fcu2, 1.0f, 47.0f); /* Same X-coordinate as the other one. */
193
194 bAction action = {{nullptr}};
195 BLI_addtail(&action.curves, &fcu1);
196 BLI_addtail(&action.curves, &fcu2);
197
198 EXPECT_TRUE(action.wrap().has_single_frame())
199 << "Two FCurves with keys on the same frame should have single frame.";
200
201 /* Modify the 2nd curve so it's keyed on a different frame. */
202 fcu2.bezt[0].vec[1][0] = 2.0f;
203 EXPECT_FALSE(action.wrap().has_single_frame())
204 << "Two FCurves with keys on different frames should have animation.";
205 }
206
207 /* One curve with two keys. */
208 {
209 FCurve fcu = {nullptr};
210 std::unique_ptr<BezTriple[]> bezt = allocate_keyframes(&fcu, 2);
211 add_keyframe(&fcu, 1.0f, 2.0f);
212 add_keyframe(&fcu, 2.0f, 2.5f);
213
214 bAction action = {{nullptr}};
215 BLI_addtail(&action.curves, &fcu);
216
217 EXPECT_FALSE(action.wrap().has_single_frame())
218 << "Action with one FCurve and two keys must have animation.";
219 }
220}
221
222} // namespace blender::bke::tests
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
void BKE_action_groups_reconstruct(bAction *act)
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:111
#define STRNCPY_UTF8(dst, src)
TEST(action_groups, ReconstructGroupsWithReordering)
float vec[3][3]
struct FCurve * next
bActionGroup * grp
char * rna_path
BezTriple * bezt
struct FCurve * prev
unsigned int totvert
void * last
void * first
struct bActionGroup * prev
struct bActionGroup * next
ListBase curves
ListBase groups