Blender V4.3
blenkernel/intern/nla_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_listbase.h"
6
7#include "BKE_nla.hh"
8
9#include "DNA_anim_types.h"
10#include "DNA_nla_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "testing/testing.h"
15
16namespace blender::bke::tests {
17
19{
20 NlaStrip strip{};
21 strip.blendin = 4.0;
22 strip.blendout = 5.0;
23 strip.start = 1;
24 strip.end = 10;
25
26 /* Scaling a strip up doesn't affect the blend in/out value. */
27 strip.end = 20;
29 EXPECT_FLOAT_EQ(strip.blendin, 4.0);
30 EXPECT_FLOAT_EQ(strip.blendout, 5.0);
31
32 /* Scaling a strip down affects the blend-in value before the blend-out value. */
33 strip.end = 7;
35 EXPECT_FLOAT_EQ(strip.blendin, 1.0);
36 EXPECT_FLOAT_EQ(strip.blendout, 5.0);
37
38 /* Scaling a strip down to nothing updates the blend in/out values accordingly. */
39 strip.end = 1.1;
41 EXPECT_FLOAT_EQ(strip.blendin, 0.0);
42 EXPECT_FLOAT_EQ(strip.blendout, 0.1);
43}
44
46{
47 ListBase strips{};
48 NlaStrip strip1{};
49 strip1.start = 0;
50 strip1.end = 10;
51 strips.first = &strip1;
52
53 NlaStrip strip2{};
54 strip2.start = 5;
55 strip2.end = 10;
56
57 /* Can't add a null NLA strip to an NLA Track. */
58 EXPECT_FALSE(BKE_nlastrips_add_strip(&strips, nullptr));
59
60 /* Can't add an NLA strip to an NLA Track that overlaps another NLA strip. */
61 EXPECT_FALSE(BKE_nlastrips_add_strip(&strips, &strip2));
62
63 strip2.start = 15;
64 strip2.end = 20;
65 /* Can add an NLA strip to an NLA Track that doesn't overlaps another NLA strip. */
66 EXPECT_TRUE(BKE_nlastrips_add_strip(&strips, &strip2));
67}
68
70{
71 NlaStrip strip{};
72
73 strip.actstart = 2.0f;
74 strip.actend = 4.0f;
75 EXPECT_FLOAT_EQ(BKE_nla_clip_length_get_nonzero(&strip), 2.0f);
76
77 strip.actstart = 2.0f;
78 strip.actend = 3.0f;
79 EXPECT_FLOAT_EQ(BKE_nla_clip_length_get_nonzero(&strip), 1.0f);
80
81 strip.actstart = 2.0f;
82 strip.actend = 2.0625f;
83 EXPECT_FLOAT_EQ(BKE_nla_clip_length_get_nonzero(&strip), 0.0625f);
84
85 strip.actstart = 2.0f;
86 strip.actend = 2.0f;
87 EXPECT_FLOAT_EQ(BKE_nla_clip_length_get_nonzero(&strip), 1.0f);
88}
89
91{
92 float start, end;
93
94 start = 2.0f;
95 end = 4.0f;
97 EXPECT_FLOAT_EQ(end, 4.0f);
98
99 start = 2.0f;
100 end = 3.0f;
102 EXPECT_FLOAT_EQ(end, 3.0f);
103
104 start = 2.0f;
105 end = 2.0625f;
107 EXPECT_FLOAT_EQ(end, 2.0625f);
108
109 start = 2.0f;
110 end = 2.0f;
112 EXPECT_FLOAT_EQ(end, 3.0f);
113}
114
116{
117 NlaTrack track{};
118 ListBase strips{};
119 NlaStrip strip1{};
120 strip1.start = 0;
121 strip1.end = 10;
122
123 NlaStrip strip2{};
124 strip2.start = 11;
125 strip2.end = 20;
126
127 /* Add NLA strips to the NLATrack. */
128 BKE_nlastrips_add_strip(&strips, &strip1);
129 BKE_nlastrips_add_strip(&strips, &strip2);
130 track.strips = strips;
131
132 /* Ensure we have 2 strips in the track. */
133 EXPECT_EQ(2, BLI_listbase_count(&track.strips));
134
135 BKE_nlatrack_remove_strip(&track, &strip2);
136 EXPECT_EQ(1, BLI_listbase_count(&track.strips));
137 /* Ensure the correct strip was removed. */
138 EXPECT_EQ(-1, BLI_findindex(&track.strips, &strip2));
139}
140
142{
143 AnimData adt{};
144
145 /* Add NLA tracks to the Animation Data. */
146 NlaTrack *track1 = BKE_nlatrack_new_tail(&adt.nla_tracks, false);
147 NlaTrack *track2 = BKE_nlatrack_new_tail(&adt.nla_tracks, false);
148
149 /* Ensure we have 2 tracks in the track. */
150 EXPECT_EQ(2, BLI_listbase_count(&adt.nla_tracks));
151
152 BKE_nlatrack_remove_and_free(&adt.nla_tracks, track2, false);
153 EXPECT_EQ(1, BLI_listbase_count(&adt.nla_tracks));
154
155 /* Ensure the correct track was removed. */
156 EXPECT_EQ(-1, BLI_findindex(&adt.nla_tracks, track2));
157
158 /* Free the rest of the tracks, and ensure they are removed. */
159 BKE_nlatrack_remove_and_free(&adt.nla_tracks, track1, false);
160 EXPECT_EQ(0, BLI_listbase_count(&adt.nla_tracks));
161 EXPECT_EQ(-1, BLI_findindex(&adt.nla_tracks, track1));
162}
163
165{
166 AnimData adt{};
167 NlaTrack *trackB = BKE_nlatrack_new_tail(&adt.nla_tracks, false);
168 NlaTrack *trackA = BKE_nlatrack_new_tail(&adt.nla_tracks, false);
169
170 /* Expect that Track B was added before track A. */
171 EXPECT_EQ(1, BLI_findindex(&adt.nla_tracks, trackA));
172 EXPECT_EQ(0, BLI_findindex(&adt.nla_tracks, trackB));
173
174 /* Free the tracks. */
175 BKE_nlatrack_remove_and_free(&adt.nla_tracks, trackA, false);
176 BKE_nlatrack_remove_and_free(&adt.nla_tracks, trackB, false);
177}
178
180{
181 AnimData adt{};
182 NlaTrack *trackB = BKE_nlatrack_new_head(&adt.nla_tracks, false);
183 NlaTrack *trackA = BKE_nlatrack_new_head(&adt.nla_tracks, false);
184
185 /* Expect that Track A was added before track B. */
186 EXPECT_EQ(0, BLI_findindex(&adt.nla_tracks, trackA));
187 EXPECT_EQ(1, BLI_findindex(&adt.nla_tracks, trackB));
188
189 /* Free the tracks. */
190 BKE_nlatrack_remove_and_free(&adt.nla_tracks, trackA, false);
191 BKE_nlatrack_remove_and_free(&adt.nla_tracks, trackB, false);
192}
193
194} // namespace blender::bke::tests
void BKE_nla_clip_length_ensure_nonzero(const float *actstart, float *r_actend)
void BKE_nlatrack_remove_strip(NlaTrack *track, NlaStrip *strip)
bool BKE_nlastrips_add_strip(ListBase *strips, NlaStrip *strip)
NlaTrack * BKE_nlatrack_new_tail(ListBase *nla_tracks, const bool is_liboverride)
void BKE_nlastrip_recalculate_blend(NlaStrip *strip)
NlaTrack * BKE_nlatrack_new_head(ListBase *nla_tracks, bool is_liboverride)
float BKE_nla_clip_length_get_nonzero(const NlaStrip *strip)
void BKE_nlatrack_remove_and_free(ListBase *tracks, NlaTrack *nlt, bool do_id_user)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Read Guarded memory(de)allocation.
TEST(action_groups, ReconstructGroupsWithReordering)