Blender V5.3
transformable.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10
11#include "BLI_math_matrix.hh"
12#include "BLI_math_matrix_c.hh"
14#include "BLI_math_vector_c.hh"
15#include "BLI_string.hh"
16
17#include "DNA_action_types.h"
18#include "DNA_object_types.h"
19
20#include "BKE_action.hh"
21#include "BKE_armature.hh"
22#include "BKE_context.hh"
23#include "BKE_object.hh"
24
25#include "ANIM_rna.hh"
26
27#include "RNA_access.hh"
28#include "RNA_prototypes.hh"
29
31
33
34namespace blender::ed {
35
40static bool is_axis_mutable(const int index, const AxisMutable axis_flag)
41{
42 /* AxisMutable happens to be set up in such a way that X, Y and Z correspond to bits 0, 1
43 * and 2. The AXIS_MUTABLE_ALL case has all these bits set. */
44 return axis_flag & (1 << index);
45}
46
48{
49 TransformFloats copy(value.size());
50 for (const int i : value.index_range()) {
51 copy[i] = *(value[i]);
52 }
53 return copy;
54}
55
57 MutableSpan<float> target,
58 const AxisMutable axis_flag)
59{
60 BLI_assert(target.size() == value.size());
61 for (const int i : value.index_range()) {
62 if (!is_axis_mutable(i, axis_flag)) {
63 continue;
64 }
65 target[i] = value[i];
66 }
67}
68
73 const float target,
74 const float factor,
75 const AxisMutable axis_flag)
76{
77 for (const int i : values.index_range()) {
78 if (!is_axis_mutable(i, axis_flag)) {
79 continue;
80 }
81 values[i] += factor * (target - values[i]);
82 }
83}
84
90 const Span<float> target,
91 const float factor,
92 const AxisMutable axis_flag)
93{
94 BLI_assert(values.size() == target.size());
95 for (const int i : values.index_range()) {
96 if (!is_axis_mutable(i, axis_flag)) {
97 continue;
98 }
99 values[i] += factor * (target[i] - values[i]);
100 }
101}
102
103Array<float> property_interpolated(const Span<float> a, const Span<float> b, const float factor)
104{
105 BLI_assert(a.size() == b.size());
106 Array<float> interpolated(a.size());
107 for (const int i : a.index_range()) {
108 interpolated[i] = interpf(b[i], a[i], factor);
109 }
110 return interpolated;
111}
112
114{
115 Vector<ed::AnimTransformable> transformables;
116 Vector<PointerRNA> pointers;
117 switch (CTX_data_mode_enum(&C)) {
118 case CTX_MODE_OBJECT: {
119 CTX_data_selected_objects(&C, &pointers);
120 for (PointerRNA &ptr : pointers) {
121 transformables.append(ed::AnimTransformable(*id_cast<Object *>(ptr.owner_id)));
122 }
123 break;
124 }
125 case CTX_MODE_POSE: {
126 CTX_data_selected_pose_bones(&C, &pointers);
127 for (PointerRNA &ptr : pointers) {
128 transformables.append(
129 {*id_cast<Object *>(ptr.owner_id), *static_cast<bPoseChannel *>(ptr.data)});
130 }
131 break;
132 }
133
134 default:
135 break;
136 }
137 return transformables;
138}
139
140/* Since there can be more than one representation of rotation data, they are stored in an array.
141 * The enum is the index into that array. */
146 /* Not a rotation mode, always keep last. */
148};
149
151 const Rotation *reference_euler) const
152{
153 if (mode == this->mode) {
154 return *this;
155 }
156
157 float4 quat;
158 switch (this->mode) {
159 case ROT_MODE_QUAT:
160 copy_qt_qt(quat, this->values.data());
161 break;
162
164 axis_angle_to_quat(quat, &this->values[1], this->values[0]);
165 break;
166
167 default:
168 BLI_assert(this->mode <= ROT_MODE_ZYX);
169 eulO_to_quat(quat, this->values.data(), this->mode);
170 break;
171 }
172
173 normalize_qt(quat);
174
175 Rotation converted;
176 converted.mode = mode;
177 switch (mode) {
178 case ROT_MODE_QUAT:
179 converted.values.reinitialize(4);
180 copy_qt_qt(converted.values.data(), quat);
181 break;
182
184 converted.values.reinitialize(4);
185 quat_to_axis_angle(&converted.values[1], &converted.values[0], quat);
186 break;
187
188 default:
190 converted.values.reinitialize(3);
191 if (reference_euler) {
192 BLI_assert(reference_euler->mode >= ROT_MODE_EUL);
194 converted.values.data(), reference_euler->values.data(), mode, quat);
195 }
196 else {
197 quat_to_eulO(converted.values.data(), mode, quat);
198 }
199 break;
200 }
201 return converted;
202}
203
205{
206 switch (mode) {
207 /* Both Axis-Angle and Quaternion have their x-y-z values on array index 1, 2 and 3. Since we
208 * are mirroring on the y-z plane we need to invert those two axes. */
210 case ROT_MODE_QUAT:
211 values[2] *= -1;
212 values[3] *= -1;
213 break;
214
215 default:
217 values[1] *= -1;
218 values[2] *= -1;
219 break;
220 }
221}
222
224{
225 switch (mode) {
226 case ROT_MODE_QUAT:
227 return {{1, 0, 0, 0}, mode};
229 return {{0, 0, 1, 0}, mode};
230 default:
231 BLI_assert(mode <= ROT_MODE_ZYX);
232 return {{0, 0, 0}, mode};
233 }
234}
235
236static void interpolate_axis_angle(const float a_angle,
237 const float3 &a_axis,
238 const float b_angle,
239 const float3 &b_axis,
240 const float factor,
241 float *r_angle,
242 float r_axis[3])
243{
244 float4 a_quat, b_quat;
245 axis_angle_to_quat(a_quat, a_axis, a_angle);
246 axis_angle_to_quat(b_quat, b_axis, b_angle);
247 float4 interpolated_quat;
248 interp_qt_qtqt(interpolated_quat, a_quat, b_quat, factor);
249 quat_to_axis_angle(r_axis, r_angle, interpolated_quat);
250}
251
252Rotation rotation_interpolated(const Rotation &a, const Rotation &b, const float factor)
253{
254 /* Only different from `b` if the rotation mode does not match `a`. */
255 const Rotation b_aligned = b.converted_to_mode(a.mode);
256 Rotation interpolated;
257 interpolated.mode = a.mode;
258 interpolated.values.reinitialize(a.values.size());
259 switch (a.mode) {
260 case ROT_MODE_QUAT:
261 interp_qt_qtqt(interpolated.values.data(), a.values.data(), b_aligned.values.data(), factor);
262 break;
263
264 case ROT_MODE_AXISANGLE: {
266 &a.values[1],
267 b_aligned.values[0],
268 &b_aligned.values[1],
269 factor,
270 &interpolated.values[0],
271 &interpolated.values[1]);
272 break;
273 }
274
275 default:
276 /* Should axis angle use a different interpolation mode? */
277 for (const int i : interpolated.values.index_range()) {
278 interpolated.values[i] = interpf(b_aligned.values[i], a.values[i], factor);
279 }
280 break;
281 }
282
283 return interpolated;
284}
285
287 Array<TransformFloatPtrs> &rotations, float *euler, float *quat, float *axis, float *angle)
288{
290 rotations[ROT_IDX_EULER] = TransformFloatPtrs(3);
291 for (const int i : IndexRange(3)) {
292 rotations[ROT_IDX_EULER][i] = &euler[i];
293 }
294
296 for (const int i : IndexRange(4)) {
297 rotations[ROT_IDX_QUATERNION][i] = &quat[i];
298 }
299
301 for (const int i : IndexRange(3)) {
302 rotations[ROT_IDX_AXIS_ANGLE][i + 1] = &axis[i];
303 }
304 rotations[ROT_IDX_AXIS_ANGLE][0] = angle;
305}
306
308 : type_(AnimTransformable::Type::POSE_BONE),
309 owner_id_(&owner_id.id),
310 data_(&pchan),
311 fcurve_group_name_(pchan.name),
312 name_(pchan.name),
313 location_({pchan.loc, 3}),
314 rotation_mode_(&pchan.rotmode),
315 scale_({pchan.scale, 3})
316{
317 build_rotations_array(rotations_, pchan.eul, pchan.quat, pchan.rotAxis, &pchan.rotAngle);
318 rna_path_from_id_ = blender::animrig::get_pose_bone_rna_path(pchan);
319}
320
322 : type_(AnimTransformable::Type::OBJECT),
323 owner_id_(&obj.id),
324 data_(&obj),
325 fcurve_group_name_("Object Transforms"),
326 name_(&obj.id.name[2]),
327 location_({obj.loc, 3}),
328 rotation_mode_(reinterpret_cast<eRotationModes *>(&obj.rotmode)),
329 scale_({obj.scale, 3})
330{
331 build_rotations_array(rotations_, obj.rot, obj.quat, obj.rotAxis, &obj.rotAngle);
332 rna_path_from_id_ = "";
333}
334
336{
337 BLI_assert(type_ == Type::POSE_BONE);
338 return static_cast<bPoseChannel *>(data_);
339}
340
342{
343 BLI_assert(type_ == Type::OBJECT);
344 return static_cast<Object *>(data_);
345}
346
348{
349 return rna_path_from_id_;
350}
351
353{
354 /* Note that this assumes the property name for the underlying struct. If we add support for a
355 * struct where this doesn't match, the property names have to be moved to the constructor. */
356 StringRefNull property_name;
357 switch (prop_type) {
359 property_name = "location";
360 break;
362 property_name = blender::animrig::get_rotation_mode_path(*rotation_mode_);
363 break;
365 property_name = "scale";
366 break;
367 }
368 return this->rna_path_to_property(property_name);
369}
370
371std::string AnimTransformable::rna_path_to_property(const StringRef property_name) const
372{
373 if (rna_path_from_id_.empty()) {
374 return property_name;
375 }
376 return fmt::format("{}.{}", rna_path_from_id_, property_name);
377}
378
379std::string AnimTransformable::rna_path_to_rotation(const eRotationModes rotation_mode) const
380{
381 StringRefNull property_name = blender::animrig::get_rotation_mode_path(rotation_mode);
382 return this->rna_path_to_property(property_name);
383}
384
386{
387 return this->rna_path_to_property("rotation_mode");
388}
389
391{
392 switch (prop_type) {
394 return location_.as_span();
395
397 const TransformFloatPtrs *rotation_array = get_rotation_array_from_mode(*rotation_mode_);
398 return copy_pointers_to_values(*rotation_array);
399 }
401 return scale_.as_span();
402 }
403
405 return {};
406}
407
409 const Span<float> values,
410 const AxisMutable axis_flag)
411{
412 switch (prop_type) {
414 copy_span_into_mutable_span(values, location_, axis_flag);
415 break;
416
418 const TransformFloatPtrs *rotation_array = get_rotation_array_from_mode(*rotation_mode_);
419 BLI_assert(rotation_array->size() == values.size());
420 if (rotation_array->size() > values.size()) {
421 /* Trying to set a rotation with different mode. Use `set_rotation` instead. */
423 return;
424 }
425 /* Axis flags don't work with quaternion rotations. */
426 BLI_assert((axis_flag == AXIS_MUTABLE_ALL) || (*rotation_mode_ != ROT_MODE_QUAT));
427 for (const int i : rotation_array->index_range()) {
428 if (!is_axis_mutable(i, axis_flag)) {
429 continue;
430 }
431 *(*rotation_array)[i] = values[i];
432 }
433 break;
434 }
436 copy_span_into_mutable_span(values, scale_, axis_flag);
437 break;
438 }
439}
440
442 const Span<float> target,
443 const float factor,
444 const AxisMutable axis_flag)
445{
446 switch (prop_type) {
448 blend_linear(location_, target, factor, axis_flag);
449 break;
450
452 const TransformFloatPtrs *rotation_array = get_rotation_array_from_mode(*rotation_mode_);
453 if (rotation_array->size() != target.size()) {
454 /* This doesn't catch all invalid cases. Differing euler rotation order or quaternion/axis
455 * angle will still have the same array size but blending will create bogus data. */
456 BLI_assert_msg(false, "Cannot do blending with differing rotation modes");
457 return;
458 }
459 Rotation rotation;
460 /* Assuming the rotation mode. See docstring of function. */
461 rotation.mode = *rotation_mode_;
462 rotation.values = target;
463 blend_rotation_to(rotation, factor, axis_flag);
464 break;
465 }
467 blend_linear(scale_, target, factor, axis_flag);
468 break;
469 }
470}
471
473 const float target,
474 const float factor,
475 const AxisMutable axis_flag)
476{
477 switch (prop_type) {
479 blend_linear(location_, target, factor, axis_flag);
480 break;
481
483 BLI_assert(*rotation_mode_ != ROT_MODE_QUAT);
484 const TransformFloatPtrs *rotation_array = get_rotation_array_from_mode(*rotation_mode_);
485 Rotation rotation;
486 /* Assuming the rotation mode. See docstring of function. */
487 rotation.mode = *rotation_mode_;
488 rotation.values.reinitialize(rotation_array->size());
489 rotation.values.fill(target);
490 blend_rotation_to(rotation, factor, axis_flag);
491 break;
492 }
494 blend_linear(scale_, target, factor, axis_flag);
495 break;
496 }
497}
498
499const TransformFloatPtrs *AnimTransformable::get_rotation_array_from_mode(
500 const eRotationModes mode) const
501{
502 const TransformFloatPtrs *rotations_array = nullptr;
503 switch (mode) {
504 case ROT_MODE_QUAT:
505 rotations_array = &rotations_[ROT_IDX_QUATERNION];
506 break;
508 rotations_array = &rotations_[ROT_IDX_AXIS_ANGLE];
509 break;
510 default:
511 BLI_assert(mode <= ROT_MODE_ZYX);
512 rotations_array = &rotations_[ROT_IDX_EULER];
513 break;
514 }
515 return rotations_array;
516}
517
519{
520 return get_rotation_for_mode(*rotation_mode_);
521}
522
524{
525 Rotation rotation;
526 rotation.mode = mode;
527 const Array<float *> *rotations_array = get_rotation_array_from_mode(mode);
528 BLI_assert(rotations_array != nullptr);
529 rotation.values = copy_pointers_to_values(*rotations_array);
530 return rotation;
531}
532
534{
535 const TransformFloatPtrs *rotations_array = get_rotation_array_from_mode(*rotation_mode_);
536 BLI_assert(rotations_array != nullptr);
537 if (rotation.mode == *rotation_mode_) {
538 /* Easy case, can just copy the values. */
539 for (const int i : rotations_array->index_range()) {
540 *(*rotations_array)[i] = rotation.values[i];
541 }
542 return;
543 }
544
545 Rotation rot_in_correct_mode = rotation.converted_to_mode(*rotation_mode_);
546 for (const int i : rotations_array->index_range()) {
547 *(*rotations_array)[i] = rot_in_correct_mode.values[i];
548 }
549}
550
552{
553 return *rotation_mode_;
554}
555
557{
558 *rotation_mode_ = mode;
559}
560
562 const float factor,
563 const AxisMutable axis_flag)
564{
565 /* If `target` matches the `current_mode`, the function will return `target` unmodified. */
566 Rotation compatible_rotation = target.converted_to_mode(*rotation_mode_);
567
568 const TransformFloatPtrs *rotations_array = get_rotation_array_from_mode(*rotation_mode_);
569 BLI_assert(rotations_array != nullptr);
570
572 switch (*rotation_mode_) {
573 case ROT_MODE_QUAT: {
574 float4 current_quat;
575 for (const int i : IndexRange(4)) {
576 current_quat[i] = *((*rotations_array)[i]);
577 }
578 normalize_qt(current_quat);
579 normalize_qt(compatible_rotation.values.data());
580 result.reinitialize(4);
581 /* We are not using the axis flag here. Not sure how that would work with quaternions. */
582 interp_qt_qtqt(result.data(), current_quat, compatible_rotation.values.data(), factor);
583 break;
584 }
585 case ROT_MODE_AXISANGLE: {
586 result.reinitialize(4);
587 for (const int i : IndexRange(4)) {
588 result[i] = *((*rotations_array)[i]);
589 }
591 &result[1],
592 compatible_rotation.values[0],
593 &compatible_rotation.values[1],
594 factor,
595 &result[0],
596 &result[1]);
597 break;
598 }
599 default: {
600 BLI_assert(*rotation_mode_ <= ROT_MODE_ZYX);
601 result.reinitialize(3);
602 for (const int i : IndexRange(3)) {
603 result[i] = *((*rotations_array)[i]);
604 }
605 blend_linear(result, compatible_rotation.values, factor, axis_flag);
606 break;
607 }
608 }
609
610 BLI_assert(result.size() == rotations_array->size());
611 for (const int i : result.index_range()) {
612 *(*rotations_array)[i] = result[i];
613 }
614}
615
616float4x4 AnimTransformable::get_world_space(const Depsgraph &depsgraph) const
617{
618 ID *evaluated_id = DEG_get_evaluated_id(&depsgraph, this->owner_id_);
619 BLI_assert(evaluated_id);
620 switch (this->type_) {
622 Object *ob_eval = id_cast<Object *>(evaluated_id);
623 bPoseChannel *pose_bone_eval = BKE_pose_channel_find_name(ob_eval->pose, this->name_.data());
624 if (!pose_bone_eval) {
626 break;
627 }
628 return ob_eval->object_to_world() * float4x4(pose_bone_eval->pose_mat);
629 }
631 Object *ob_eval = id_cast<Object *>(evaluated_id);
632 return ob_eval->object_to_world();
633 }
634 }
635
637 return float4x4::identity();
638}
639
641 const float4x4 &world_matrix) const
642{
643 ID *evaluated_id = DEG_get_evaluated_id(&depsgraph, this->owner_id_);
644 BLI_assert(evaluated_id);
645 switch (this->type_) {
647 Object *ob_eval = id_cast<Object *>(evaluated_id);
648 bPoseChannel *pose_bone_eval = BKE_pose_channel_find_name(ob_eval->pose, this->name_.data());
649 if (!pose_bone_eval) {
651 break;
652 }
653 Bone *bone = pose_bone_eval->bone_get(*ob_eval);
654 float4x4 object_local = ob_eval->world_to_object() * world_matrix;
655 float4x4 bone_local = float4x4::identity();
656 /* The function docstring tells me I (Christoph) cannot use this function the way I am using
657 * it here. But it works. Either I am missing an edge case, or the description is wrong. */
658 BKE_armature_mat_pose_to_bone({pose_bone_eval, bone}, object_local.ptr(), bone_local.ptr());
659 return bone_local;
660 }
661
663 Object *ob_eval = id_cast<Object *>(evaluated_id);
664 float4x4 parent_matrix = float4x4::identity();
665 if (ob_eval->parent) {
666 parent_matrix = math::invert(float4x4(ob_eval->parentinv)) *
667 ob_eval->parent->world_to_object();
668 }
669
670 float4x4 delta_matrix = BKE_object_delta_matrix_get(*ob_eval);
671 return math::invert(delta_matrix) * parent_matrix * world_matrix;
672 }
673 }
674
676 return float4x4::identity();
677}
678
679} // namespace blender::ed
Helper functions for animation to interact with the RNA system.
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Defines an abstraction around various structs to modify their transform properties via a unified API ...
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
int64_t size() const
Definition BLI_array.hh:267
const T * data() const
Definition BLI_array.hh:323
IndexRange index_range() const
Definition BLI_array.hh:371
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
Rotation get_rotation_for_mode(eRotationModes mode) const
float4x4 get_world_space(const Depsgraph &depsgraph) const
void blend_property_to(PropertyType prop_type, Span< float > target, float factor, AxisMutable axis_flag)
void set_rotation(const Rotation &rotation)
std::string rna_path_to_property(PropertyType prop_type) const
void blend_rotation_to(const Rotation &target, float factor, AxisMutable axis_flag)
StringRefNull rna_path() const
void set_rotation_mode(eRotationModes mode)
std::string rna_path_to_rotation(eRotationModes rotation_mode) const
eRotationModes get_rotation_mode() const
TransformFloats get_property(PropertyType prop_type) const
std::string rna_path_to_rotation_mode() const
AnimTransformable(Object &owner_id, bPoseChannel &pchan)
float4x4 world_to_local(const Depsgraph &depsgraph, const float4x4 &world_matrix) const
void set_property(PropertyType prop_type, Span< float > values, AxisMutable axis_flag)
StringRefNull get_rotation_mode_path(eRotationModes rotation_mode)
Definition anim_rna.cc:109
std::string get_pose_bone_rna_path(const bPoseChannel &pose_bone)
Definition anim_rna.cc:85
Rotation identity_rotation(const eRotationModes mode)
Array< float *, 4 > TransformFloatPtrs
static TransformFloats copy_pointers_to_values(const Span< float * > value)
static void build_rotations_array(Array< TransformFloatPtrs > &rotations, float *euler, float *quat, float *axis, float *angle)
Rotation rotation_interpolated(const Rotation &a, const Rotation &b, const float factor)
static void interpolate_axis_angle(const float a_angle, const float3 &a_axis, const float b_angle, const float3 &b_axis, const float factor, float *r_angle, float r_axis[3])
static void copy_span_into_mutable_span(const Span< float > value, MutableSpan< float > target, const AxisMutable axis_flag)
static void blend_linear(MutableSpan< float > values, const float target, const float factor, const AxisMutable axis_flag)
static bool is_axis_mutable(const int index, const AxisMutable axis_flag)
Array< float > property_interpolated(const Span< float > a, const Span< float > b, const float factor)
Array< float, 4 > TransformFloats
Vector< ed::AnimTransformable > selected_transformables_from_context(bContext &C)
CartesianBasis invert(const CartesianBasis &basis)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
float normalize_qt(float q[4])
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
ID * DEG_get_evaluated_id(const Depsgraph *depsgraph, ID *id)
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
MatBase< float, 4, 4 > float4x4
@ CTX_MODE_OBJECT
void quat_to_compatible_eulO(float eul[3], const float oldrot[3], short order, const float quat[4])
VecBase< float, 4 > float4
void BKE_armature_mat_pose_to_bone(bke::PChanBoneConst pchanbone, const float inmat[4][4], float outmat[4][4])
void copy_qt_qt(float q[4], const float a[4])
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
float4x4 BKE_object_delta_matrix_get(const Object &obj)
Definition object.cc:3150
void quat_to_eulO(float e[3], short order, const float q[4])
unsigned char uint8_t
Definition BKE_text.h:79
void quat_to_axis_angle(float axis[3], float *angle, const float q[4])
bool CTX_data_selected_pose_bones(const bContext *C, Vector< PointerRNA > *list)
void eulO_to_quat(float q[4], const float e[3], short order)
MINLINE float interpf(float target, float origin, float t)
VecBase< float, 3 > float3
bool CTX_data_selected_objects(const bContext *C, Vector< PointerRNA > *list)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
const c_style_mat & ptr() const
struct Object * parent
float parentinv[4][4]
struct bPose * pose
Rotation converted_to_mode(eRotationModes mode, const Rotation *reference_euler=nullptr) const