Blender V5.3
key_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_gtest_base.hh"
6#include "BKE_idtype.hh"
7#include "BKE_key.hh"
8#include "BKE_main.hh"
9#include "BKE_mesh.h"
10#include "BKE_mesh.hh"
11#include "BKE_object.hh"
12
13#include "DNA_object_types.h"
14
15#include "testing/testing.h"
16
17#include "MEM_guardedalloc.h"
18namespace blender {
19
21 public:
22 Main *bmain = nullptr;
23 Object *ob = nullptr;
24 Mesh *mesh = nullptr;
25
26 void SetUp() override
27 {
30 mesh = BKE_mesh_add(bmain, "Test Mesh");
31 ob->data = &mesh->id;
32
33 mesh->verts_num = 4;
35 /* Shapekeys don't affect edges and faces so they can be ignored here. */
37 {0, 0, 0},
38 {1, 0, 0},
39 {1, 1, 0},
40 {0, 1, 0},
41 };
42 mesh->vert_positions_for_write().copy_from(verts);
43 }
44
45 void TearDown() override
46 {
48 }
49};
50
51namespace bke::tests {
52
53/* Test that creating a shapekey from a mesh has the correct data. */
54TEST_F(ShapekeyTest, mesh_key_creation)
55{
56 Key *key = BKE_key_add(bmain, &mesh->id);
57 ASSERT_EQ(key->from, &mesh->id);
58 /* Assignment to the mesh does not happen automatically by adding it. */
59 mesh->key = key;
61 KeyBlock *base = BKE_keyblock_add(key, "base");
62 /* This should be set automatically after adding the first key. */
63 ASSERT_EQ(key->refkey, base);
64 /* The elemsize stores how many bytes one element has (vertex in this case). */
65 ASSERT_EQ(key->elemsize, sizeof(float[3]));
66 /* Adding the keyblock does not actually allocate any data for it. */
67 EXPECT_EQ(base->data, nullptr);
69 ASSERT_NE(base->data, nullptr);
70 EXPECT_EQ(base->totelem, 4);
71 float3 *data = reinterpret_cast<float3 *>(base->data);
72 Array<float3> expected = {
73 {0, 0, 0},
74 {1, 0, 0},
75 {1, 1, 0},
76 {0, 1, 0},
77 };
78 EXPECT_EQ_ARRAY(expected.data(), data, 4);
79}
80
81TEST_F(ShapekeyTest, mesh_key_evaluation_relative)
82{
83 Key *key = BKE_key_add(bmain, &mesh->id);
84 mesh->key = key;
85 key->type = KEY_RELATIVE;
86 KeyBlock *base = BKE_keyblock_add(key, "base");
88
89 KeyBlock *key1 = BKE_keyblock_add(key, "one");
91 float3 *key1_data = reinterpret_cast<float3 *>(key1->data);
92 key1_data[0] = {1, 1, 1};
93
94 key1->curval = 1.0;
95 int totelem = 0;
96 float3 *ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
97 ASSERT_NE(ob_eval, nullptr);
98 ASSERT_EQ(totelem, mesh->verts_num);
99 Array<float3> expected = {
100 {1, 1, 1},
101 {1, 0, 0},
102 {1, 1, 0},
103 {0, 1, 0},
104 };
105 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
106 MEM_delete(ob_eval);
107
108 KeyBlock *key2 = BKE_keyblock_add(key, "two");
110 float3 *key2_data = reinterpret_cast<float3 *>(key2->data);
111 key2_data[0] = {2, 2, 2};
112 key2_data[1] = {-1, -1, -1};
113
114 key2->curval = 1.0;
115 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
116 expected = {
117 {3, 3, 3},
118 {-1, -1, -1},
119 {1, 1, 0},
120 {0, 1, 0},
121 };
122 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
123 MEM_delete(ob_eval);
124
125 /* Blend in halfway. */
126 key2->curval = 0.5;
127 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
128 expected = {
129 {2, 2, 2},
130 {0, -0.5, -0.5},
131 {1, 1, 0},
132 {0, 1, 0},
133 };
134 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
135 MEM_delete(ob_eval);
136
137 /* Blend in double. */
138 key2->curval = 2.0;
139 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
140 expected = {
141 {5, 5, 5},
142 {-3, -2, -2},
143 {1, 1, 0},
144 {0, 1, 0},
145 };
146 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
147 MEM_delete(ob_eval);
148}
149
150TEST_F(ShapekeyTest, mesh_key_evaluation_absolute)
151{
152 Key *key = BKE_key_add(bmain, &mesh->id);
153 mesh->key = key;
154 key->type = KEY_NORMAL;
155 KeyBlock *base = BKE_keyblock_add(key, "base");
157
158 KeyBlock *key1 = BKE_keyblock_add(key, "one");
159 key1->pos = 1.0;
161 float3 *key1_data = reinterpret_cast<float3 *>(key1->data);
162 key1_data[1] = {1, 1, 1};
163
164 KeyBlock *key2 = BKE_keyblock_add(key, "two");
165 key2->pos = 2.0;
167 float3 *key2_data = reinterpret_cast<float3 *>(key2->data);
168 key2_data[1] = {0, 0, 0};
169 key2_data[2] = {5, 5, 5};
170
171 /* At 0 this should be the base. */
172 key->ctime = 0.0;
173 int totelem = 0;
174 float3 *ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
175 Array<float3> expected = {
176 {0, 0, 0},
177 {1, 0, 0},
178 {1, 1, 0},
179 {0, 1, 0},
180 };
181 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
182 MEM_delete(ob_eval);
183
184 /* At 1 this should be at key1. */
185 key->ctime = 100.0;
186 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
187 expected = {
188 {0, 0, 0},
189 {1, 1, 1},
190 {1, 1, 0},
191 {0, 1, 0},
192 };
193 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
194 MEM_delete(ob_eval);
195
196 /* At 2 this should be at key2. */
197 key->ctime = 200.0;
198 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
199 expected = {
200 {0, 0, 0},
201 {0, 0, 0},
202 {5, 5, 5},
203 {0, 1, 0},
204 };
205 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
206 MEM_delete(ob_eval);
207
208 /* This should be a linear blend between key1 and key2; */
209 key->ctime = 150.0;
210 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
211 expected = {
212 {0, 0, 0},
213 {0.5, 0.5, 0.5},
214 {3, 3, 2.5},
215 {0, 1, 0},
216 };
217 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
218 EXPECT_NEAR(3.0, 0.5 * key1_data[2][0] + 0.5 * key2_data[2][0], 0.001);
219 MEM_delete(ob_eval);
220}
221
222/* For historical reasons, keyblocks can end up having an element count that does not match the
223 * element count of the source data (unequal vertex count in the case of meshes). This is not
224 * supported in relative evaluation, so that should ignore the shape key, regardless of its weight.
225 */
226TEST_F(ShapekeyTest, mesh_key_evaluation_relative_uneqal_element_count)
227{
228 Key *key = BKE_key_add(bmain, &mesh->id);
229 mesh->key = key;
230 key->type = KEY_RELATIVE;
231 KeyBlock *base = BKE_keyblock_add(key, "base");
233
234 KeyBlock *key1 = BKE_keyblock_add(key, "one");
235 ASSERT_EQ(mesh->verts_num, 4);
236 ASSERT_EQ(key->elemsize, sizeof(float[3]));
237 /* The mesh has 4 vertices, but this shapekey will only have 3. */
238 constexpr int SHAPEKEY_VERTEX_COUNT = 3;
239 float3 *key1_data = reinterpret_cast<float3 *>(
240 MEM_new_array_uninitialized(size_t(SHAPEKEY_VERTEX_COUNT), size_t(key->elemsize), __func__));
241 key1->data = key1_data;
242 key1->totelem = SHAPEKEY_VERTEX_COUNT;
243 key1_data[1] = {5, 5, 5};
244
245 key1->curval = 1.0;
246 int totelem = 0;
247 float3 *ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
248 /* Despite having the key at full influence, it should do nothing since the element count does
249 * not match. */
250 Array<float3> expected = {
251 {0, 0, 0},
252 {1, 0, 0},
253 {1, 1, 0},
254 {0, 1, 0},
255 };
256 EXPECT_EQ_ARRAY(expected.data(), ob_eval, 4);
257 MEM_delete(ob_eval);
258}
259
260/* Same as mesh_key_evaluation_relative_uneqal_element_count but with absolute shapekeys. This is
261 * somewhat supported but there is no known way to get Blender into such a state. */
262TEST_F(ShapekeyTest, mesh_key_evaluation_absolute_uneqal_element_count)
263{
264 Key *key = BKE_key_add(bmain, &mesh->id);
265 mesh->key = key;
266 key->type = KEY_NORMAL;
267 KeyBlock *base = BKE_keyblock_add(key, "base");
269 float3 *base_data = reinterpret_cast<float3 *>(base->data);
270
271 KeyBlock *key1 = BKE_keyblock_add(key, "one");
272 ASSERT_EQ(mesh->verts_num, 4);
273 ASSERT_EQ(key->elemsize, 12);
274 /* The mesh has 4 vertices, but this shapekey will only have 2. */
275 float3 *key1_data = reinterpret_cast<float3 *>(
276 MEM_new_array_uninitialized(size_t(2), size_t(key->elemsize), __func__));
277 key1->data = key1_data;
278 key1->totelem = 2;
279 key1_data[0] = {1, 0, 0};
280 key1_data[1] = {2, 0, 0};
281
282 key1->pos = 1.0;
283 key->ctime = 100.0;
284
285 int totelem = 0;
286 float3 *ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
287 ASSERT_EQ(totelem, 4);
288 /* Vertices are skipped in this case. */
289 Array<float3> expected = {
290 {1, 0, 0},
291 {1, 0, 0},
292 {2, 0, 0},
293 {2, 0, 0},
294 };
295 EXPECT_NEAR_ARRAY_ND(expected.data(), ob_eval, 4, 3, 0.001);
296 MEM_delete(ob_eval);
297
298 /* Causing blending with the base key. */
299 key->ctime = 95.0;
300
301 totelem = 0;
302 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
303 ASSERT_EQ(totelem, 4);
304 expected = {
305 {0.95, 0, 0},
306 {1, 0, 0},
307 {1.95, 0.05, 0},
308 {1.9, 0.05, 0},
309 };
310 EXPECT_NEAR_ARRAY_ND(expected.data(), ob_eval, 4, 3, 0.001);
311 MEM_delete(ob_eval);
312
313 MEM_delete(key1_data);
314
315 key->ctime = 100.0;
316 /* Testing with more vertices than the mesh. */
317 key1_data = reinterpret_cast<float3 *>(
318 MEM_new_array_uninitialized(size_t(8), size_t(key->elemsize), __func__));
319 key1->data = key1_data;
320 key1->totelem = 8;
321 key1_data[0] = {1, 0, 0};
322 key1_data[1] = {2, 0, 0};
323 key1_data[2] = {3, 0, 0};
324 key1_data[3] = {4, 0, 0};
325 key1_data[4] = {5, 0, 0};
326 key1_data[5] = {6, 0, 0};
327 key1_data[6] = {7, 0, 0};
328 key1_data[7] = {8, 0, 0};
329
330 totelem = 0;
331 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
332 ASSERT_EQ(totelem, 4);
333 /* The evaluation ignores any vertices that are extra. */
334 expected = {
335 {1, 0, 0},
336 {3, 0, 0},
337 {5, 0, 0},
338 {7, 0, 0},
339 };
340 EXPECT_NEAR_ARRAY_ND(expected.data(), ob_eval, 4, 3, 0.001);
341 MEM_delete(ob_eval);
342
343 /* Causing blending with the base key. */
344 key->ctime = 95.0;
345
346 totelem = 0;
347 ob_eval = reinterpret_cast<float3 *>(BKE_key_evaluate_object(ob, &totelem));
348 ASSERT_EQ(totelem, 4);
349 expected = {
350 {0.95, 0, 0},
351 {2.9, 0, 0},
352 {4.8, 0.05, 0},
353 {6.65, 0.05, 0},
354 };
355 EXPECT_NEAR_ARRAY_ND(expected.data(), ob_eval, 4, 3, 0.001);
356 /* The values should be a linear interpolation between base and key1 at 95%. */
357 EXPECT_NEAR(6.65, 0.95 * key1_data[6][0] + 0.05 * base_data[3][0], 0.001);
358 MEM_delete(ob_eval);
359}
360
361} // namespace bke::tests
362} // namespace blender
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
const T * data() const
Definition BLI_array.hh:323
void SetUp() override
Definition key_test.cc:26
void TearDown() override
Definition key_test.cc:45
#define EXPECT_NEAR(result, expect, threshold)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
void mesh_ensure_required_data_layers(Mesh &mesh)
BMesh Iterators.
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Main * BKE_main_new()
Definition main.cc:93
Key * BKE_key_add(Main *bmain, ID *id)
Definition key.cc:228
BMesh const char void * data
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
KeyBlock * BKE_keyblock_add(Key *key, const char *name)
Definition key.cc:1322
float * BKE_key_evaluate_object(Object *ob, int *r_totelem)
Definition key.cc:1137
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
Mesh * BKE_mesh_add(Main *bmain, const char *name)
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
Definition key.cc:1681
KeyBlock * refkey
ShapekeyContainerType type