Blender V5.3
pose_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 "BKE_pose.hh"
6#include "BLI_listbase.hh"
9#include "BLI_string.hh"
10
11#include "BKE_action.hh"
12#include "BKE_anim_data.hh"
13#include "BKE_animsys.hh"
14#include "BKE_armature.hh"
15#include "BKE_gtest_base.hh"
16#include "BKE_idtype.hh"
17#include "BKE_lib_id.hh"
18#include "BKE_main.hh"
19#include "BKE_object.hh"
20
21#include "DEG_depsgraph.hh"
22
23#include "DNA_object_types.h"
24
25#include "ANIM_action.hh"
26#include "ANIM_pose.hh"
27
28#include "testing/testing.h"
29
30namespace blender {
31
33 "Properties not stored in the pose are expected to not be modified.";
34
35namespace animrig::tests {
36
38 public:
46
47 void SetUp() override
48 {
50 pose_action = BKE_id_new<Action>(bmain, "pose_data");
51 Layer &layer = pose_action->layer_add("first_layer");
52 Strip &strip = layer.strip_add(*pose_action, Strip::Type::Keyframe);
53 keyframe_data = &strip.data<StripKeyframeData>(*pose_action);
54
58
59 bArmature *armature = BKE_armature_add(bmain, "ArmatureA");
60 obj_armature_a->data = id_cast<ID *>(armature);
61
62 Bone *bone = MEM_new<Bone>("BONE");
63 STRNCPY(bone->name, "BoneA");
64 BLI_addtail(&armature->bonebase, bone);
65
66 bone = MEM_new<Bone>("BONE");
67 STRNCPY(bone->name, "BoneB");
68 BLI_addtail(&armature->bonebase, bone);
69
70 BKE_pose_ensure(bmain, obj_armature_a, armature, false);
71
72 armature = BKE_armature_add(bmain, "ArmatureB");
73 obj_armature_b->data = id_cast<ID *>(armature);
74
75 bone = MEM_new<Bone>("BONE");
76 STRNCPY(bone->name, "BoneA");
77 BLI_addtail(&armature->bonebase, bone);
78
79 bone = MEM_new<Bone>("BONE");
80 STRNCPY(bone->name, "BoneB");
81 BLI_addtail(&armature->bonebase, bone);
82
83 BKE_pose_ensure(bmain, obj_armature_b, armature, false);
84 }
85
86 void TearDown() override
87 {
89 }
90};
91
92TEST_F(PoseTest, get_best_slot)
93{
94 Slot &first_slot = pose_action->slot_add();
95 Slot &second_slot = pose_action->slot_add_for_id(obj_empty->id);
96
97 EXPECT_EQ(&get_best_pose_slot_for_id(obj_empty->id, *pose_action), &second_slot);
98 EXPECT_EQ(&get_best_pose_slot_for_id(obj_armature_a->id, *pose_action), &first_slot);
99}
100
101TEST_F(PoseTest, apply_action_object)
102{
103 /* Since pose bones live on the object, the code is already set up to handle objects
104 * transforms, even though the name suggests it only applies to bones. */
105 Slot &first_slot = pose_action->slot_add();
106 EXPECT_EQ(obj_empty->loc[0], 0.0f);
107 keyframe_data->keyframe_insert(bmain, first_slot, {"location", 0}, {1, 10}, key_settings);
108 AnimationEvalContext eval_context = {nullptr, 1.0f};
109 animrig::pose_apply_action_all_bones(obj_empty, pose_action, first_slot.handle, &eval_context);
110 EXPECT_EQ(obj_empty->loc[0], 10.0f);
111}
112
113TEST_F(PoseTest, apply_action_all_bones_single_slot)
114{
115 Slot &first_slot = pose_action->slot_add();
116
117 keyframe_data->keyframe_insert(
118 bmain, first_slot, {"pose.bones[\"BoneA\"].location", 0}, {1, 10}, key_settings);
119 keyframe_data->keyframe_insert(
120 bmain, first_slot, {"pose.bones[\"BoneB\"].location", 1}, {1, 5}, key_settings);
121
122 bPoseChannel *bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
123 bPoseChannel *bone_b = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneB");
124
125 bone_a->loc[1] = 1.0;
126 bone_a->loc[2] = 2.0;
127
128 AnimationEvalContext eval_context = {nullptr, 1.0f};
130 obj_armature_a, pose_action, first_slot.handle, &eval_context);
131 EXPECT_EQ(bone_a->loc[0], 10.0);
132 EXPECT_EQ(bone_b->loc[1], 5.0);
133
134 EXPECT_EQ(bone_a->loc[1], 1.0) << msg_unexpected_modification;
135 EXPECT_EQ(bone_a->loc[2], 2.0);
136}
137
138TEST_F(PoseTest, apply_action_all_bones_multiple_slots)
139{
140 Slot &slot_a = pose_action->slot_add_for_id(obj_armature_a->id);
141 Slot &slot_b = pose_action->slot_add_for_id(obj_armature_b->id);
142
143 keyframe_data->keyframe_insert(
144 bmain, slot_a, {"pose.bones[\"BoneA\"].location", 0}, {1, 5}, key_settings);
145 keyframe_data->keyframe_insert(
146 bmain, slot_a, {"pose.bones[\"BoneB\"].location", 0}, {1, 5}, key_settings);
147
148 keyframe_data->keyframe_insert(
149 bmain, slot_b, {"pose.bones[\"BoneA\"].location", 1}, {1, 10}, key_settings);
150 keyframe_data->keyframe_insert(
151 bmain, slot_b, {"pose.bones[\"BoneB\"].location", 1}, {1, 10}, key_settings);
152
153 bPoseChannel *arm_a_bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
154 bPoseChannel *arm_a_bone_b = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneB");
155
156 bPoseChannel *arm_b_bone_a = BKE_pose_channel_find_name(obj_armature_b->pose, "BoneA");
157 bPoseChannel *arm_b_bone_b = BKE_pose_channel_find_name(obj_armature_b->pose, "BoneB");
158
159 AnimationEvalContext eval_context = {nullptr, 1.0f};
160 animrig::pose_apply_action_all_bones(obj_armature_a, pose_action, slot_a.handle, &eval_context);
161
162 EXPECT_EQ(arm_a_bone_a->loc[0], 5.0);
163 EXPECT_EQ(arm_a_bone_a->loc[1], 0.0) << msg_unexpected_modification;
164 EXPECT_EQ(arm_a_bone_a->loc[2], 0.0) << msg_unexpected_modification;
165
166 EXPECT_EQ(arm_a_bone_b->loc[0], 5.0);
167
168 EXPECT_EQ(arm_b_bone_a->loc[1], 0.0) << "Other armature should not be affected yet.";
169
170 animrig::pose_apply_action_all_bones(obj_armature_b, pose_action, slot_b.handle, &eval_context);
171
172 EXPECT_EQ(arm_b_bone_b->loc[0], 0.0) << msg_unexpected_modification;
173 EXPECT_EQ(arm_b_bone_b->loc[1], 10.0);
174 EXPECT_EQ(arm_b_bone_b->loc[2], 0.0) << msg_unexpected_modification;
175
176 EXPECT_EQ(arm_a_bone_a->loc[0], 5.0) << "Other armature should not be affected.";
177
178 /* Any slot can be applied, even if it hasn't been added for the ID. */
179 animrig::pose_apply_action_all_bones(obj_armature_a, pose_action, slot_b.handle, &eval_context);
180
181 EXPECT_EQ(arm_b_bone_b->loc[1], arm_b_bone_a->loc[1])
182 << "Applying the same pose should result in the same values.";
183}
184
185TEST_F(PoseTest, apply_action_blend_single_slot)
186{
187 Slot &first_slot = pose_action->slot_add();
188 keyframe_data->keyframe_insert(
189 bmain, first_slot, {"pose.bones[\"BoneA\"].location", 0}, {1, 10}, key_settings);
190 keyframe_data->keyframe_insert(
191 bmain, first_slot, {"pose.bones[\"BoneB\"].location", 1}, {1, 5}, key_settings);
192
193 bPoseChannel *bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
194 bPoseChannel *bone_b = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneB");
195
196 bone_a->loc[0] = 0.0;
197 bone_b->loc[1] = 0.0;
198
199 AnimationEvalContext eval_context = {nullptr, 1.0f};
201 obj_armature_a, pose_action, first_slot.handle, &eval_context, 1.0);
202
203 EXPECT_NEAR(bone_a->loc[0], 10.0, 0.001);
204 EXPECT_NEAR(bone_b->loc[1], 5.0, 0.001);
205
206 bone_a->loc[0] = 0.0;
207 bone_b->loc[1] = 0.0;
208
210 obj_armature_a, pose_action, first_slot.handle, &eval_context, 0.5);
211
212 EXPECT_NEAR(bone_a->loc[0], 5.0, 0.001);
213 EXPECT_NEAR(bone_b->loc[1], 2.5, 0.001);
214
215 bone_a->loc[0] = 0.0;
216 bone_b->loc[1] = 0.0;
217
218 bone_a->flag |= POSE_SELECTED;
219 bone_b->flag &= ~POSE_SELECTED;
220
221 /* This should only affect the selected bone. */
223 obj_armature_a, pose_action, first_slot.handle, &eval_context, 0.5);
224
225 EXPECT_NEAR(bone_a->loc[0], 5.0, 0.001);
226 EXPECT_NEAR(bone_b->loc[1], 0.0, 0.001);
227}
228
229TEST_F(PoseTest, apply_action_multiple_objects)
230{
231 Slot &slot_a = pose_action->slot_add_for_id(obj_armature_a->id);
232 Slot &slot_b = pose_action->slot_add_for_id(obj_armature_b->id);
233
234 keyframe_data->keyframe_insert(
235 bmain, slot_a, {"pose.bones[\"BoneA\"].location", 0}, {1, 5}, key_settings);
236 keyframe_data->keyframe_insert(
237 bmain, slot_a, {"pose.bones[\"BoneB\"].location", 0}, {1, 5}, key_settings);
238
239 keyframe_data->keyframe_insert(
240 bmain, slot_b, {"pose.bones[\"BoneA\"].location", 1}, {1, 10}, key_settings);
241 keyframe_data->keyframe_insert(
242 bmain, slot_b, {"pose.bones[\"BoneB\"].location", 1}, {1, 10}, key_settings);
243
244 bPoseChannel *arm_a_bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
245 bPoseChannel *arm_a_bone_b = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneB");
246
247 bPoseChannel *arm_b_bone_a = BKE_pose_channel_find_name(obj_armature_b->pose, "BoneA");
248 bPoseChannel *arm_b_bone_b = BKE_pose_channel_find_name(obj_armature_b->pose, "BoneB");
249
250 Vector<bPoseChannel *> all_bones = {arm_a_bone_a, arm_a_bone_b, arm_b_bone_a, arm_b_bone_b};
251
252 for (bPoseChannel *pose_bone : all_bones) {
253 pose_bone->flag &= ~POSE_SELECTED;
254 pose_bone->loc[0] = 0.0;
255 pose_bone->loc[1] = 0.0;
256 }
257
258 AnimationEvalContext eval_context = {nullptr, 1.0f};
259 animrig::pose_apply_action({obj_armature_a, obj_armature_b}, *pose_action, &eval_context, 1.0);
260
261 /* No bones are selected, this should affect all bones. */
262 EXPECT_NEAR(arm_a_bone_a->loc[0], 5, 0.001);
263 EXPECT_NEAR(arm_a_bone_b->loc[0], 5, 0.001);
264 EXPECT_NEAR(arm_b_bone_a->loc[1], 10, 0.001);
265 EXPECT_NEAR(arm_b_bone_b->loc[1], 10, 0.001);
266
267 for (bPoseChannel *pose_bone : all_bones) {
268 pose_bone->loc[0] = 0.0;
269 pose_bone->loc[1] = 0.0;
270 }
271
272 arm_a_bone_a->flag |= POSE_SELECTED;
273
274 animrig::pose_apply_action({obj_armature_a, obj_armature_b}, *pose_action, &eval_context, 1.0);
275
276 /* Only the one selected bone should be affected. */
277 EXPECT_NEAR(arm_a_bone_a->loc[0], 5, 0.001);
278 EXPECT_NEAR(arm_a_bone_b->loc[0], 0, 0.001);
279 EXPECT_NEAR(arm_b_bone_a->loc[1], 0, 0.001);
280 EXPECT_NEAR(arm_b_bone_b->loc[1], 0, 0.001);
281
282 for (bPoseChannel *pose_bone : all_bones) {
283 pose_bone->loc[0] = 0.0;
284 pose_bone->loc[1] = 0.0;
285 }
286
287 arm_a_bone_a->flag |= POSE_SELECTED;
288 arm_b_bone_a->flag |= POSE_SELECTED;
289
290 animrig::pose_apply_action({obj_armature_a, obj_armature_b}, *pose_action, &eval_context, 1.0);
291
292 /* Only the two selected bones from different armatures should be affected. */
293 EXPECT_NEAR(arm_a_bone_a->loc[0], 5, 0.001);
294 EXPECT_NEAR(arm_a_bone_b->loc[0], 0, 0.001);
295 EXPECT_NEAR(arm_b_bone_a->loc[1], 10, 0.001);
296 EXPECT_NEAR(arm_b_bone_b->loc[1], 0, 0.001);
297
298 for (bPoseChannel *pose_bone : all_bones) {
299 pose_bone->loc[0] = 0.0;
300 pose_bone->loc[1] = 0.0;
301 }
302
303 animrig::pose_apply_action({obj_armature_a, obj_armature_b}, *pose_action, &eval_context, 0.5);
304
305 /* Blending half way. */
306 EXPECT_NEAR(arm_a_bone_a->loc[0], 2.5, 0.001);
307 EXPECT_NEAR(arm_a_bone_b->loc[0], 0, 0.001);
308 EXPECT_NEAR(arm_b_bone_a->loc[1], 5, 0.001);
309 EXPECT_NEAR(arm_b_bone_b->loc[1], 0, 0.001);
310
311 for (bPoseChannel *pose_bone : all_bones) {
312 pose_bone->loc[0] = 0.0;
313 pose_bone->loc[1] = 0.0;
314 }
315
316 arm_a_bone_a->flag |= POSE_SELECTED;
317 arm_a_bone_b->flag |= POSE_SELECTED;
318 arm_b_bone_a->flag |= POSE_SELECTED;
319
320 animrig::pose_apply_action({obj_armature_a, obj_armature_b}, *pose_action, &eval_context, 1.0);
321
322 EXPECT_NEAR(arm_a_bone_a->loc[0], 5, 0.001);
323 EXPECT_NEAR(arm_a_bone_b->loc[0], 5, 0.001);
324 EXPECT_NEAR(arm_b_bone_a->loc[1], 10, 0.001);
325 EXPECT_NEAR(arm_b_bone_b->loc[1], 0, 0.001);
326}
327
328TEST_F(PoseTest, apply_action_multiple_objects_single_slot)
329{
330 Slot &slot_a = pose_action->slot_add_for_id(obj_armature_a->id);
331
332 keyframe_data->keyframe_insert(
333 bmain, slot_a, {"pose.bones[\"BoneA\"].location", 0}, {1, 5}, key_settings);
334 keyframe_data->keyframe_insert(
335 bmain, slot_a, {"pose.bones[\"BoneB\"].location", 0}, {1, 5}, key_settings);
336
337 bPoseChannel *arm_a_bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
338 bPoseChannel *arm_a_bone_b = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneB");
339
340 bPoseChannel *arm_b_bone_a = BKE_pose_channel_find_name(obj_armature_b->pose, "BoneA");
341 bPoseChannel *arm_b_bone_b = BKE_pose_channel_find_name(obj_armature_b->pose, "BoneB");
342
343 Vector<bPoseChannel *> all_bones = {arm_a_bone_a, arm_a_bone_b, arm_b_bone_a, arm_b_bone_b};
344
345 for (bPoseChannel *pose_bone : all_bones) {
346 pose_bone->flag &= ~POSE_SELECTED;
347 pose_bone->loc[0] = 0.0;
348 pose_bone->loc[1] = 0.0;
349 }
350
351 AnimationEvalContext eval_context = {nullptr, 1.0f};
352 animrig::pose_apply_action({obj_armature_a, obj_armature_b}, *pose_action, &eval_context, 1.0);
353
354 /* No bones are selected, this should affect all bones. Armature B has no slot, it should fall
355 * back to slot 0. */
356 EXPECT_NEAR(arm_a_bone_a->loc[0], 5, 0.001);
357 EXPECT_NEAR(arm_a_bone_b->loc[0], 5, 0.001);
358 EXPECT_NEAR(arm_b_bone_a->loc[0], 5, 0.001);
359 EXPECT_NEAR(arm_b_bone_b->loc[0], 5, 0.001);
360}
361
363{
364 pose_bone.eul[0] = 0;
365 pose_bone.eul[1] = 0;
366 pose_bone.eul[2] = 0;
367
368 pose_bone.quat[0] = 1;
369 pose_bone.quat[1] = 0;
370 pose_bone.quat[2] = 0;
371 pose_bone.quat[3] = 0;
372
373 pose_bone.rotAngle = 0;
374 pose_bone.rotAxis[0] = 0;
375 pose_bone.rotAxis[1] = 0;
376 pose_bone.rotAxis[2] = 0;
377}
378
379TEST_F(PoseTest, apply_action_differing_rotation_mode_from_euler)
380{
381 /* When the pose has a different rotation mode than the data it is being applied to, the system
382 * should convert the rotation. */
383 Slot &slot_a = pose_action->slot_add_for_id(obj_armature_a->id);
384
385 keyframe_data->keyframe_insert(
386 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_euler", 0}, {1, 3.14}, key_settings);
387 keyframe_data->keyframe_insert(
388 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_euler", 1}, {1, 1}, key_settings);
389 keyframe_data->keyframe_insert(
390 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_euler", 2}, {1, 0}, key_settings);
391
392 bPoseChannel *bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
393 const bke::PChanBone pchanbone_a{bone_a, bone_a->bone_get(*obj_armature_a)};
394 AnimationEvalContext eval_context = {nullptr, 1.0f};
395
396 /* First check that applying works if the rotation mode matches. */
397 bone_a->rotmode = ROT_MODE_XYZ;
398 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
399 EXPECT_NEAR(bone_a->eul[0], 3.14, 0.001);
400 EXPECT_NEAR(bone_a->eul[1], 1, 0.001);
401 EXPECT_NEAR(bone_a->eul[2], 0, 0.001);
402
403 BKE_pchan_calc_mat(pchanbone_a);
404 float expected_matrix[4][4];
405 copy_m4_m4(expected_matrix, bone_a->chan_mat);
406
407 /* Check that other rotation modes work the same as applying euler directly. */
408 bone_a->rotmode = ROT_MODE_QUAT;
409 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
410 BKE_pchan_calc_mat(pchanbone_a);
411 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
412
413 bone_a->rotmode = ROT_MODE_AXISANGLE;
414 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
415 BKE_pchan_calc_mat(pchanbone_a);
416 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
417
418 /* Not doing blend testing here since the rotation matrix will not align. Component wise
419 * interpolation of euler angles and matrix interpolation is expected to yield different
420 * results. */
421}
422
423TEST_F(PoseTest, apply_action_differing_rotation_mode_from_quaternion)
424{
425 /* When the pose has a different rotation mode than the data it is being applied to, the system
426 * should convert the rotation. */
427 Slot &slot_a = pose_action->slot_add_for_id(obj_armature_a->id);
428
429 float quaternion[4] = {0.877, 0.11, -0.483, -0.164};
430 /* We have to have a normalized quaternion otherwise the resulting matrix will be off between
431 * different rotation modes. */
432 normalize_qt(quaternion);
433 keyframe_data->keyframe_insert(bmain,
434 slot_a,
435 {"pose.bones[\"BoneA\"].rotation_quaternion", 0},
436 {1, quaternion[0]},
437 key_settings);
438 keyframe_data->keyframe_insert(bmain,
439 slot_a,
440 {"pose.bones[\"BoneA\"].rotation_quaternion", 1},
441 {1, quaternion[1]},
442 key_settings);
443 keyframe_data->keyframe_insert(bmain,
444 slot_a,
445 {"pose.bones[\"BoneA\"].rotation_quaternion", 2},
446 {1, quaternion[2]},
447 key_settings);
448 keyframe_data->keyframe_insert(bmain,
449 slot_a,
450 {"pose.bones[\"BoneA\"].rotation_quaternion", 3},
451 {1, quaternion[3]},
452 key_settings);
453
454 bPoseChannel *bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
455 const bke::PChanBone pchanbone_a{bone_a, bone_a->bone_get(*obj_armature_a)};
456 AnimationEvalContext eval_context = {nullptr, 1.0f};
457
458 /* First check that applying works if the rotation mode matches. */
459 bone_a->rotmode = ROT_MODE_QUAT;
460 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
461 EXPECT_NEAR(bone_a->quat[0], quaternion[0], 0.001);
462 EXPECT_NEAR(bone_a->quat[1], quaternion[1], 0.001);
463 EXPECT_NEAR(bone_a->quat[2], quaternion[2], 0.001);
464 EXPECT_NEAR(bone_a->quat[3], quaternion[3], 0.001);
465
466 BKE_pchan_calc_mat(pchanbone_a);
467 float expected_matrix[4][4];
468 copy_m4_m4(expected_matrix, bone_a->chan_mat);
469
470 /* Check that other rotation modes work the same as applying quaternion directly. */
471 bone_a->rotmode = ROT_MODE_XYZ;
472 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
473 BKE_pchan_calc_mat(pchanbone_a);
474 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
475
476 bone_a->rotmode = ROT_MODE_AXISANGLE;
477 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
478 BKE_pchan_calc_mat(pchanbone_a);
479 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
480
482
483 /* Also test with blend factor other than 1. */
484 bone_a->rotmode = ROT_MODE_QUAT;
485 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 0.7);
486 BKE_pchan_calc_mat(pchanbone_a);
487 copy_m4_m4(expected_matrix, bone_a->chan_mat);
488
489 bone_a->rotmode = ROT_MODE_AXISANGLE;
490 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 0.7);
491 BKE_pchan_calc_mat(pchanbone_a);
492 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
493
494 bone_a->rotmode = ROT_MODE_XYZ;
495 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 0.7);
496 BKE_pchan_calc_mat(pchanbone_a);
497 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
498}
499
500TEST_F(PoseTest, apply_action_differing_rotation_mode_from_axisangle)
501{
502 /* When the pose has a different rotation mode than the data it is being applied to, the system
503 * should convert the rotation. */
504 Slot &slot_a = pose_action->slot_add_for_id(obj_armature_a->id);
505
506 keyframe_data->keyframe_insert(
507 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_axis_angle", 0}, {1, 0.66}, key_settings);
508 keyframe_data->keyframe_insert(
509 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_axis_angle", 1}, {1, -0.3}, key_settings);
510 keyframe_data->keyframe_insert(
511 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_axis_angle", 2}, {1, 0.86}, key_settings);
512 keyframe_data->keyframe_insert(
513 bmain, slot_a, {"pose.bones[\"BoneA\"].rotation_axis_angle", 3}, {1, -0.42}, key_settings);
514
515 bPoseChannel *bone_a = BKE_pose_channel_find_name(obj_armature_a->pose, "BoneA");
516 const bke::PChanBone pchanbone_a{bone_a, bone_a->bone_get(*obj_armature_a)};
517 AnimationEvalContext eval_context = {nullptr, 1.0f};
518
519 /* First check that applying works if the rotation mode matches. */
520 bone_a->rotmode = ROT_MODE_AXISANGLE;
521 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
522 EXPECT_NEAR(bone_a->rotAngle, 0.66, 0.001);
523 EXPECT_NEAR(bone_a->rotAxis[0], -0.3, 0.001);
524 EXPECT_NEAR(bone_a->rotAxis[1], 0.86, 0.001);
525 EXPECT_NEAR(bone_a->rotAxis[2], -0.42, 0.001);
526
527 BKE_pchan_calc_mat(pchanbone_a);
528 float expected_matrix[4][4];
529 copy_m4_m4(expected_matrix, bone_a->chan_mat);
530
531 /* Check that other rotation modes work the same as applying quaternion directly. */
532 bone_a->rotmode = ROT_MODE_XYZ;
533 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
534 BKE_pchan_calc_mat(pchanbone_a);
535 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
536
537 bone_a->rotmode = ROT_MODE_QUAT;
538 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 1.0);
539 BKE_pchan_calc_mat(pchanbone_a);
540 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
541
543
544 /* Also test with blend factor other than 1. */
545 bone_a->rotmode = ROT_MODE_AXISANGLE;
546 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 0.7);
547 BKE_pchan_calc_mat(pchanbone_a);
548 copy_m4_m4(expected_matrix, bone_a->chan_mat);
549
550 bone_a->rotmode = ROT_MODE_QUAT;
551 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 0.7);
552 BKE_pchan_calc_mat(pchanbone_a);
553 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
554
555 bone_a->rotmode = ROT_MODE_XYZ;
556 animrig::pose_apply_action({obj_armature_a}, *pose_action, &eval_context, 0.7);
557 BKE_pchan_calc_mat(pchanbone_a);
558 EXPECT_M4_NEAR(expected_matrix, bone_a->chan_mat, 0.001);
559}
560
561} // namespace animrig::tests
562} // namespace blender
Functions and classes to work with Actions.
Functions to work with animation poses.
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Layer & layer_add(std::optional< StringRefNull > name)
const T & data(const Action &owning_action) const
const animrig::KeyframeSettings key_settings
Definition pose_test.cc:45
StripKeyframeData * keyframe_data
Definition pose_test.cc:44
#define layer
#define EXPECT_NEAR(result, expect, threshold)
static void reset_pose_bone_rotations(bPoseChannel &pose_bone)
Definition pose_test.cc:362
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
void pose_apply_action_all_bones(Object *ob, bAction *action, slot_handle_t slot_handle, const AnimationEvalContext *anim_eval_context)
void pose_apply_action(Span< Object * > objects, Action &pose_action, const AnimationEvalContext *anim_eval_context, float blend_factor)
void pose_apply_action_blend_all_bones(Object *ob, bAction *action, slot_handle_t slot_handle, const AnimationEvalContext *anim_eval_context, float blend_factor)
Definition pose.cc:424
Slot & get_best_pose_slot_for_id(const ID &id, Action &pose_data)
Definition pose.cc:471
void pose_apply_action_blend(Object *ob, bAction *action, slot_handle_t slot_handle, const AnimationEvalContext *anim_eval_context, float blend_factor)
PChanBoneT< bPoseChannel, Bone > PChanBone
Definition BKE_pose.hh:88
BMesh Iterators.
@ BEZT_KEYTYPE_KEYFRAME
void BKE_pchan_calc_mat(bke::PChanBone pchanbone)
bArmature * BKE_armature_add(Main *bmain, const char *name)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
float normalize_qt(float q[4])
Main * BKE_main_new()
Definition main.cc:93
constexpr char msg_unexpected_modification[]
Definition pose_test.cc:32
void BKE_main_free(Main *bmain)
Definition main.cc:202
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:118
void copy_m4_m4(float m1[4][4], const float m2[4][4])
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
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
ListBaseT< Bone > bonebase