Blender V5.3
transformable_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLI_listbase.hh"
6#include "BLI_math_base_c.hh"
7#include "BLI_string.hh"
8
9#include "BKE_action.hh"
10#include "BKE_armature.hh"
11#include "BKE_idtype.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_main.hh"
14#include "BKE_object.hh"
15
16#include "DNA_object_types.h"
17
18#include "RNA_access.hh"
19#include "RNA_define.hh"
20#include "RNA_prototypes.hh"
21
23
24#include "CLG_log.h"
25#include "testing/testing.h"
26
28
29class TransformableTest : public testing::Test {
30 public:
34
36
37 static void SetUpTestSuite()
38 {
39 /* BKE_id_free() hits a code path that uses CLOG, which crashes if not initialized properly. */
40 CLG_init();
42 RNA_init();
43 }
44
45 static void TearDownTestSuite()
46 {
47 CLG_exit();
48 RNA_exit();
49 }
50
51 void SetUp() override
52 {
54
55 Bone *bone = MEM_new<Bone>("BONE");
56 STRNCPY(bone->name, "Bone");
57
58 armature = BKE_armature_add(bmain, "Armature");
60
62 armature_object->data = id_cast<ID *>(armature);
65 ASSERT_NE(pose_bone, nullptr);
66 }
67
68 void TearDown() override
69 {
71 }
72};
73
74TEST_F(TransformableTest, transformable_get_values)
75{
76 AnimTransformable transformable(*armature_object, *pose_bone);
77 EXPECT_STREQ(transformable.rna_path().c_str(), "pose.bones[\"Bone\"]");
78
80 Array<float> expected = {0, 0, 0};
81 EXPECT_EQ(expected, location);
82
83 pose_bone->loc[0] = 1;
84
85 expected = {0, 0, 0};
86 /* The returned values are a copy, changing the underlying data does not modify the array. */
87 EXPECT_EQ(expected, location);
88
90 expected = {1, 0, 0};
91 EXPECT_EQ(expected, location);
92
93 Array<float> rotation_values = transformable.get_property(
95 EXPECT_EQ(pose_bone->rotmode, ROT_MODE_QUAT);
96 EXPECT_EQ(transformable.get_rotation_mode(), pose_bone->rotmode);
97 EXPECT_EQ(rotation_values.size(), 4);
98}
99
100TEST_F(TransformableTest, transformable_rotation)
101{
102 AnimTransformable transformable(*armature_object, *pose_bone);
103 Rotation rotation = transformable.get_rotation();
104 /* The rotation is always returned in the mode of the Transformable. */
105 EXPECT_EQ(rotation.mode, transformable.get_rotation_mode());
106 EXPECT_EQ(rotation.mode, ROT_MODE_QUAT);
107 Array<float> expected = {1, 0, 0, 0};
108 EXPECT_EQ(expected, rotation.values);
109
110 pose_bone->rotmode = ROT_MODE_XYZ;
111 pose_bone->eul[0] = 3.14;
112 transformable.set_rotation(rotation);
113 /* Even though the rotation is a quaternion, setting it to the Transformable that is a bone
114 * with xyz euler still works. The rotation is converted to the correct mode of the
115 * Transformable. */
116 expected = {0, 0, 0};
117 EXPECT_NEAR_SPAN(expected.as_span(), Span<float>(pose_bone->eul, 3), 0.001);
118}
119
120TEST_F(TransformableTest, transformable_blend_to)
121{
122 AnimTransformable transformable(*armature_object, *pose_bone);
123 transformable.blend_property_to(
125 Array<float> expected = {0, 0, 0};
126 /* A blend factor of 0 keeps the current values. */
127 EXPECT_NEAR_SPAN(expected.as_span(),
129 0.001);
130
131 transformable.blend_property_to(
133 expected = {0.1f, 0, 0};
134 /* Blending linearly to 1. */
135 EXPECT_NEAR_SPAN(expected.as_span(),
137 0.001);
138
139 transformable.blend_property_to(
141 expected = {1.0f, 0, 0};
142 /* Blending linearly to 1. */
143 EXPECT_NEAR_SPAN(expected.as_span(),
145 0.001);
146}
147
148TEST_F(TransformableTest, transformable_blend_rotation_to)
149{
150 AnimTransformable transformable(*armature_object, *pose_bone);
151 /* There is a special function for rotations that does spherical interpolation for
152 * quaternions. */
153 EXPECT_EQ(pose_bone->rotmode, ROT_MODE_QUAT);
154 /* A 90 degree rotation on X. */
155 Rotation rot_90_x = {{0.707107f, 0.707107f, 0, 0}, ROT_MODE_QUAT};
156 transformable.blend_rotation_to(rot_90_x, 0.5f, AXIS_MUTABLE_ALL);
157 Rotation current_rotation = transformable.get_rotation();
158 EXPECT_NEAR(current_rotation.values[0], 0.92387f, 0.001);
159 EXPECT_NEAR(current_rotation.values[1], 0.38268f, 0.001);
160
161 /* Checking that the result is different from linear interpolation. */
162 EXPECT_NE(current_rotation.values[0], interpf(0.707107f, 1.0f, 0.5f));
163 EXPECT_NE(current_rotation.values[1], interpf(0.707107f, 0.0f, 0.5f));
164
166 /* Using the generic blend function assumes that the given values are in the rotation mode that
167 * the object is currently in. As long as that is the case it will work as expected. */
168 transformable.blend_property_to(
170 EXPECT_NEAR(current_rotation.values[0], 0.92387f, 0.001);
171 EXPECT_NEAR(current_rotation.values[1], 0.38268f, 0.001);
172}
173
174TEST_F(TransformableTest, transformable_axis_constraints)
175{
176 /* It is possible to only set and blend certain axes. This is a feature of the pose slide code
177 * and had to be added to transformables. */
178 AnimTransformable transformable(*armature_object, *pose_bone);
179
181 Array<float> expected = {1, 0, 0};
182 EXPECT_NEAR_SPAN(expected.as_span(),
184 0.001);
185
187 {2, 2, 2},
189 expected = {2, 2, 0};
190 EXPECT_NEAR_SPAN(expected.as_span(),
192 0.001);
193
195 {3, 3, 3},
196 1.0f,
198 expected = {2, 3, 3};
199 EXPECT_NEAR_SPAN(expected.as_span(),
201 0.001);
202}
203
204} // namespace blender::ed::tests
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
void CLG_exit()
Definition clog.cc:877
void CLG_init()
Definition clog.cc:870
Object is a sort of wrapper for general info.
Defines an abstraction around various structs to modify their transform properties via a unified API ...
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
constexpr const char * c_str() const
void blend_property_to(PropertyType prop_type, Span< float > target, float factor, AxisMutable axis_flag)
void set_rotation(const Rotation &rotation)
void blend_rotation_to(const Rotation &target, float factor, AxisMutable axis_flag)
StringRefNull rna_path() const
eRotationModes get_rotation_mode() const
TransformFloats get_property(PropertyType prop_type) const
void set_property(PropertyType prop_type, Span< float > values, AxisMutable axis_flag)
#define EXPECT_NE(result, expect)
#define EXPECT_NEAR(result, expect, threshold)
TEST_F(TransformableTest, transformable_get_values)
Rotation identity_rotation(const eRotationModes mode)
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)
Main * BKE_main_new()
Definition main.cc:93
void RNA_init()
Definition rna_access.cc:88
void BKE_idtype_init()
Definition idtype.cc:207
void RNA_exit()
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
MINLINE float interpf(float target, float origin, float t)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
struct bPose * pose
ListBaseT< Bone > bonebase