Blender V5.3
pose.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
8
9#include "BLI_listbase.hh"
11
12#include "BKE_action.hh"
13#include "BKE_animsys.hh"
14#include "BKE_armature.hh"
15#include "BKE_fcurve.hh"
16#include "BKE_object.hh"
17
18#include "DNA_anim_types.h"
19#include "DNA_object_types.h"
20
21#include "RNA_access.hh"
22#include "RNA_prototypes.hh"
23
24#include "ANIM_action.hh"
25#include "ANIM_pose.hh"
26#include "ANIM_rna.hh"
27
28namespace blender::animrig {
29
30namespace {
31
32using ActionApplier =
33 FunctionRef<void(PointerRNA *, bAction *, slot_handle_t, const AnimationEvalContext *)>;
34
35void pose_apply_restore_fcurves(const Span<FCurve *> fcurves)
36{
37 for (FCurve *fcu : fcurves) {
38 fcu->flag &= ~FCURVE_DISABLED;
39 }
40}
41
42/* Returns a vector of all FCurves on which the fcurve flag was modified. */
43Vector<FCurve *> pose_apply_disable_fcurves_for_unselected_bones(
44 bAction *action, const slot_handle_t slot_handle, const bke::BoneNameSet &selected_bone_names)
45{
46 Vector<FCurve *> modified_fcurves;
47 auto disable_unselected_fcurve = [&](FCurve *fcu, const char *bone_name) {
48 const bool is_bone_selected = selected_bone_names.contains(bone_name);
49 if (!is_bone_selected) {
50 if (!(fcu->flag & FCURVE_DISABLED)) {
51 /* FCurve is not yet disabled, we need to reset that later. */
52 modified_fcurves.append(fcu);
53 }
54 fcu->flag |= FCURVE_DISABLED;
55 }
56 };
57 bke::BKE_action_find_fcurves_with_bones(action, slot_handle, disable_unselected_fcurve);
58 return modified_fcurves;
59}
60
61void pose_apply(Object *ob,
62 bAction *action,
63 const slot_handle_t slot_handle,
64 const AnimationEvalContext *anim_eval_context,
65 ActionApplier applier)
66{
67 bPose *pose = ob->pose;
68 if (pose == nullptr) {
69 return;
70 }
71
72 if (action->wrap().slot_array_num == 0) {
73 return;
74 }
75
76 const bke::BoneNameSet selected_bone_names = bke::BKE_pose_channel_find_selected_names(ob);
77
78 /* Mute all FCurves that are not associated with selected bones. This separates the concept of
79 * bone selection from the FCurve evaluation code. */
80 Vector<FCurve *> modified_fcurves = pose_apply_disable_fcurves_for_unselected_bones(
81 action, slot_handle, selected_bone_names);
82
83 /* Apply the Action. */
84 PointerRNA pose_owner_ptr = RNA_id_pointer_create(&ob->id);
85
86 applier(&pose_owner_ptr, action, slot_handle, anim_eval_context);
87
88 pose_apply_restore_fcurves(modified_fcurves);
89}
90
91} // namespace
92
93static bool is_fcurve_evaluatable(const FCurve &fcu)
94{
95 if (fcu.flag & (FCURVE_MUTED | FCURVE_DISABLED)) {
96 return false;
97 }
98 if (fcu.grp != nullptr && (fcu.grp->flag & AGRP_MUTED)) {
99 return false;
100 }
101 if (BKE_fcurve_is_empty(&fcu)) {
102 return false;
103 }
104 return true;
105}
106
111 PathResolvedRNA &anim_rna,
112 const AnimationEvalContext *anim_eval_context,
113 const float blend_factor)
114{
115 const float fcurve_value = calculate_fcurve(&anim_rna, &fcu, anim_eval_context);
116
117 float current_value;
118 float value_to_write;
119 if (!BKE_animsys_read_from_rna_path(&anim_rna, &current_value)) {
120 /* Unable to read the current value for blending, so just apply the FCurve value instead. */
121 return fcurve_value;
122 }
123
124 value_to_write = (1 - blend_factor) * current_value + blend_factor * fcurve_value;
125
126 switch (RNA_property_type(anim_rna.prop)) {
127 case PROP_BOOLEAN: /* Without this, anything less than 1.0 is converted to 'False' by
128 * ANIMSYS_FLOAT_AS_BOOL(). This is probably not desirable for blends,
129 * where anything above a 50% blend should act more like the FCurve than
130 * like the current value. */
131 case PROP_INT:
132 case PROP_ENUM:
133 value_to_write = roundf(value_to_write);
134 break;
135 /* All other types are just handled as float, and value_to_write is already correct. */
136 default:
137 break;
138 }
139 return value_to_write;
140}
141
142static bool rotation_mode_is_euler(const eRotationModes rotation_mode)
143{
144 return rotation_mode >= ROT_MODE_EUL;
145}
146
153 PropertyRNA *prop,
154 const Span<FCurve *> quat_fcurves,
155 const AnimationEvalContext *anim_eval_context,
156 float r_quaternion[4])
157{
158 BLI_assert(quat_fcurves.size() <= 4);
159
160 /* Initialize r_quaternion to the unit quaternion so that half-keyed quaternions at least have
161 * *some* value in there. */
162 r_quaternion[0] = 1.0f;
163 r_quaternion[1] = 0.0f;
164 r_quaternion[2] = 0.0f;
165 r_quaternion[3] = 0.0f;
166 PathResolvedRNA quat_rna;
167 quat_rna.ptr = ptr;
168 quat_rna.prop = prop;
169 for (FCurve *quat_curve_fcu : quat_fcurves) {
170 const int array_index = quat_curve_fcu->array_index;
171 quat_rna.prop_index = array_index;
172 r_quaternion[array_index] = calculate_fcurve(&quat_rna, quat_curve_fcu, anim_eval_context);
173 }
174
175 if (quat_fcurves.size() < 4) {
176 /* This quaternion was incompletely keyed, so the result is a mixture of the unit quaternion
177 * and values from FCurves. This means that it's almost certainly no longer of unit length. */
178 normalize_qt(r_quaternion);
179 }
180}
181
187 PropertyRNA *prop,
188 const Span<FCurve *> quaternion_fcurves,
189 const AnimationEvalContext *anim_eval_context,
190 const float blend_factor)
191{
192 BLI_assert(quaternion_fcurves.size() <= 4);
193
194 float current_quat[4];
195 RNA_property_float_get_array(&ptr, prop, current_quat);
196
197 float target_quat[4];
199 ptr, prop, quaternion_fcurves, anim_eval_context, target_quat);
200
201 float blended_quat[4];
202 interp_qt_qtqt(blended_quat, current_quat, target_quat, blend_factor);
203
204 RNA_property_float_set_array(&ptr, prop, blended_quat);
205}
206
207static void blend_rotation(PointerRNA &ptr,
208 PropertyRNA *prop,
209 const Span<FCurve *> rotation_fcurves,
210 const eRotationModes fcurve_rotation_mode,
211 const AnimationEvalContext *anim_eval_context,
212 const float blend_factor)
213{
214 if (fcurve_rotation_mode == ROT_MODE_QUAT) {
215 animsys_blend_fcurves_quaternion(ptr, prop, rotation_fcurves, anim_eval_context, blend_factor);
216 return;
217 }
218
219 PathResolvedRNA anim_rna;
220 anim_rna.ptr = ptr;
221 anim_rna.prop = prop;
222 for (FCurve *fcurve : rotation_fcurves) {
223 anim_rna.prop_index = fcurve->array_index;
224 const float value_to_write = get_fcurve_blend_value(
225 *fcurve, anim_rna, anim_eval_context, blend_factor);
226 BKE_animsys_write_to_rna_path(&anim_rna, value_to_write);
227 }
228}
229
238 const Span<FCurve *> rotation_fcurves,
239 const eRotationModes fcurve_rotation_mode,
240 const float eval_time,
241 const float blend_factor)
242{
243 /* The rotation data is 0 initialized for reasonable defaults in case some indices have no
244 * FCurves associated with them. */
245 float4 fcurve_rotation_values(0.0);
246 if (fcurve_rotation_mode == ROT_MODE_QUAT) {
247 /* Default W value for quaternions. */
248 fcurve_rotation_values[0] = 1.0;
249 }
250
251 for (FCurve *fcurve : rotation_fcurves) {
252 BLI_assert_msg(fcurve->array_index >= 0 && fcurve->array_index < 4,
253 "Rotation properties have at most 4 components.");
254 fcurve_rotation_values[fcurve->array_index] = evaluate_fcurve(fcurve, eval_time);
255 }
256
257 /* Converting to quaternion simplifies blending below. */
258 float4 fcurve_quat;
259 switch (fcurve_rotation_mode) {
260 case ROT_MODE_QUAT: {
261 copy_qt_qt(fcurve_quat, fcurve_rotation_values);
262 break;
263 }
264 case ROT_MODE_EUL: {
265 /* TODO: determine the rotation order for euler angles. This has to be stored at the
266 * point of pose creation. */
267 eulO_to_quat(fcurve_quat, fcurve_rotation_values, ROT_MODE_XYZ);
268 break;
269 }
270 case ROT_MODE_AXISANGLE: {
271 axis_angle_to_quat(fcurve_quat, &fcurve_rotation_values[1], fcurve_rotation_values[0]);
272 break;
273 }
274 default: {
276 }
277 }
278
279 float4 interp_quat;
280 if (ptr.type == RNA_PoseBone) {
281 bPoseChannel *pose_bone = static_cast<bPoseChannel *>(ptr.data);
282 const float4 quat = BKE_pchan_rot_to_quat(*pose_bone);
283 interp_qt_qtqt(interp_quat, quat, fcurve_quat, blend_factor);
284 BKE_pchan_quat_to_rot(*pose_bone, interp_quat);
285 }
286 else if (ptr.type == RNA_Object) {
287 Object *object = static_cast<Object *>(ptr.data);
288 const float4 quat = BKE_object_rot_to_quat(*object);
289 interp_qt_qtqt(interp_quat, quat, fcurve_quat, blend_factor);
290 BKE_object_quat_to_rot(*object, interp_quat);
291 }
292 else {
294 }
295}
296
297/* LERP between current value (blend_factor=0.0) and the value from the FCurve (blend_factor=1.0)
298 */
300 const Span<FCurve *> fcurves,
301 const AnimationEvalContext *anim_eval_context,
302 const float blend_factor)
303{
304 /* Rotations are a special case since the rotation mode of the pose may not match with the
305 * current rotation mode of the `ptr`. Also quaternions need to be handled together. */
306 Map<ParsedRNAPathRef, Vector<FCurve *>> rotation_fcurve_map;
307 for (FCurve *fcurve : fcurves) {
308 ParsedRNAPathRef rna_path = fcurve->rna_path_parsed();
309
310 if (!is_fcurve_evaluatable(*fcurve)) {
311 continue;
312 }
313
315 continue;
316 }
317
318 Vector<FCurve *> &rotation_fcurves = rotation_fcurve_map.lookup_or_add_default(rna_path);
319 rotation_fcurves.append(fcurve);
320 }
321
322 for (const auto &[rna_path, rotation_fcurves] : rotation_fcurve_map.items()) {
323 PointerRNA resolved_ptr;
324 PropertyRNA *resolved_prop;
325 if (!RNA_path_resolve_property(&ptr, rna_path, &resolved_ptr, &resolved_prop)) {
326 continue;
327 }
328
329 std::optional<eRotationModes> ptr_rotation_mode_opt = get_rotation_mode_from_rna_pointer(
330 resolved_ptr);
332 ptr_rotation_mode_opt.has_value(),
333 "We have an FCurve on a rotation property, the RNA data should have a rotation order.");
334 const eRotationModes ptr_rotation_mode = ptr_rotation_mode_opt.value();
335
336 const std::optional<eRotationModes> fcurve_rotation_mode_opt = get_rotation_mode_from_path(
337 rna_path);
338 BLI_assert(fcurve_rotation_mode_opt.has_value());
339 const eRotationModes fcurve_rotation_mode = fcurve_rotation_mode_opt.value();
340
341 /* The check for Euler rotation mode means we will *not* do any conversion if both modes are
342 * euler. Since we *cannot* know the exact euler mode of the stored FCurves we have to assume
343 * they are the same as the ptr. */
344 if (fcurve_rotation_mode == ptr_rotation_mode ||
345 (rotation_mode_is_euler(fcurve_rotation_mode) &&
346 rotation_mode_is_euler(ptr_rotation_mode)))
347 {
348 /* Easy case, animation mode of fcurves and of `resolved_ptr` are matching. Data can just
349 * be applied. The reason to have this separate is because in this case euler angles > 180
350 * degrees are preserved. The other path uses a conversion to a quaternion which loses that
351 * information. */
352 blend_rotation(resolved_ptr,
353 resolved_prop,
354 rotation_fcurves,
355 fcurve_rotation_mode,
356 anim_eval_context,
357 blend_factor);
358 }
359 else {
361 rotation_fcurves,
362 fcurve_rotation_mode,
363 anim_eval_context->eval_time,
364 blend_factor);
365 }
366 }
367
368 for (FCurve *fcu : fcurves) {
369 if (!is_fcurve_evaluatable(*fcu)) {
370 continue;
371 }
372
373 const ParsedRNAPathRef rna_path = fcu->rna_path_parsed();
374 if (rotation_fcurve_map.contains(rna_path)) {
375 continue;
376 }
377
378 PathResolvedRNA anim_rna;
379 if (!BKE_animsys_rna_path_resolve(&ptr, rna_path, fcu->array_index, &anim_rna)) {
380 continue;
381 }
382
383 const float value_to_write = get_fcurve_blend_value(
384 *fcu, anim_rna, anim_eval_context, blend_factor);
385 BKE_animsys_write_to_rna_path(&anim_rna, value_to_write);
386 }
387}
388
390 Action &act,
391 const int32_t action_slot_handle,
392 const AnimationEvalContext *anim_eval_context,
393 const float blend_factor)
394{
395 Vector<FCurve *> fcurves = fcurves_for_action_slot(act, action_slot_handle);
396 animsys_blend_in_fcurves(ptr, fcurves, anim_eval_context, blend_factor);
397}
398
400 bAction *action,
401 const int32_t slot_handle,
402 const AnimationEvalContext *anim_eval_context)
403{
404 PointerRNA pose_owner_ptr = RNA_id_pointer_create(&ob->id);
405 animsys_evaluate_action(&pose_owner_ptr, action, slot_handle, anim_eval_context, false);
406}
407
409 bAction *action,
410 const int32_t slot_handle,
411 const AnimationEvalContext *anim_eval_context,
412 const float blend_factor)
413{
414 auto evaluate_and_blend = [blend_factor](PointerRNA *ptr,
415 bAction *act,
416 const int32_t slot_handle,
417 const AnimationEvalContext *anim_eval_context) {
418 blend_in_action(*ptr, act->wrap(), slot_handle, anim_eval_context, blend_factor);
419 };
420
421 pose_apply(ob, action, slot_handle, anim_eval_context, evaluate_and_blend);
422}
423
425 bAction *action,
426 slot_handle_t slot_handle,
427 const AnimationEvalContext *anim_eval_context,
428 const float blend_factor)
429{
430 PointerRNA pose_owner_ptr = RNA_id_pointer_create(&ob->id);
431 blend_in_action(pose_owner_ptr, action->wrap(), slot_handle, anim_eval_context, blend_factor);
432}
433
435{
436 for (const Object *obj : objects) {
437 if (!obj->pose) {
438 continue;
439 }
440 for (bPoseChannel &pose_bone : obj->pose->chanbase) {
441 if (pose_bone.flag & POSE_SELECTED) {
442 return true;
443 }
444 }
445 }
446 return false;
447}
448
450 Action &pose_action,
451 const AnimationEvalContext *anim_eval_context,
452 const float blend_factor)
453{
454 if (any_bone_selected(objects)) {
455 for (Object *object : objects) {
456 Slot &slot = get_best_pose_slot_for_id(object->id, pose_action);
457 pose_apply_action_blend(object, &pose_action, slot.handle, anim_eval_context, blend_factor);
458 }
459 }
460 else {
461 /* In the case of nothing selected, act as if all is selected. This is a convenience feature
462 * for the artists so they don't have to be specific in their selection all the time. */
463 for (Object *object : objects) {
464 Slot &slot = get_best_pose_slot_for_id(object->id, pose_action);
466 object, &pose_action, slot.handle, anim_eval_context, blend_factor);
467 }
468 }
469}
470
472{
473 BLI_assert_msg(pose_data.slot_array_num > 0,
474 "Actions without slots have no data. This should have been caught earlier.");
475
476 Slot *slot = generic_slot_for_autoassign(id, pose_data, "");
477 if (slot == nullptr) {
478 slot = pose_data.slot(0);
479 }
480
481 return *slot;
482}
483
484} // namespace blender::animrig
Functions and classes to work with Actions.
Functions to work with animation poses.
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.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
bool contains(const Key &key) const
Definition BLI_map.hh:353
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
const Slot * slot(int64_t index) const
#define roundf(x)
static void blend_rotation(PointerRNA &ptr, PropertyRNA *prop, const Span< FCurve * > rotation_fcurves, const eRotationModes fcurve_rotation_mode, const AnimationEvalContext *anim_eval_context, const float blend_factor)
Definition pose.cc:207
decltype(blender::ActionSlot::handle) slot_handle_t
std::optional< eRotationModes > get_rotation_mode_from_path(ParsedRNAPathRef rna_path)
Definition anim_rna.cc:121
void pose_apply_action_all_bones(Object *ob, bAction *action, slot_handle_t slot_handle, const AnimationEvalContext *anim_eval_context)
bool is_rotation_path(ParsedRNAPathRef rna_path)
Definition anim_rna.cc:165
void pose_apply_action(Span< Object * > objects, Action &pose_action, const AnimationEvalContext *anim_eval_context, float blend_factor)
static bool is_fcurve_evaluatable(const FCurve *fcu)
Definition evaluation.cc:99
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
static void animsys_blend_fcurves_quaternion(PointerRNA &ptr, PropertyRNA *prop, const Span< FCurve * > quaternion_fcurves, const AnimationEvalContext *anim_eval_context, const float blend_factor)
Definition pose.cc:186
std::optional< eRotationModes > get_rotation_mode_from_rna_pointer(const PointerRNA &ptr)
Definition anim_rna.cc:152
Span< FCurve * > fcurves_for_action_slot(Action &action, slot_handle_t slot_handle)
static float get_fcurve_blend_value(FCurve &fcu, PathResolvedRNA &anim_rna, const AnimationEvalContext *anim_eval_context, const float blend_factor)
Definition pose.cc:110
static void animsys_blend_in_fcurves(PointerRNA &ptr, const Span< FCurve * > fcurves, const AnimationEvalContext *anim_eval_context, const float blend_factor)
Definition pose.cc:299
bool any_bone_selected(Span< const Object * > objects)
Slot & get_best_pose_slot_for_id(const ID &id, Action &pose_data)
Definition pose.cc:471
static void blend_rotation_with_conversion(PointerRNA &ptr, const Span< FCurve * > rotation_fcurves, const eRotationModes fcurve_rotation_mode, const float eval_time, const float blend_factor)
Definition pose.cc:237
static void blend_in_action(PointerRNA &ptr, Action &act, const int32_t action_slot_handle, const AnimationEvalContext *anim_eval_context, const float blend_factor)
Definition pose.cc:389
void pose_apply_action_blend(Object *ob, bAction *action, slot_handle_t slot_handle, const AnimationEvalContext *anim_eval_context, float blend_factor)
static bool rotation_mode_is_euler(const eRotationModes rotation_mode)
Definition pose.cc:142
Slot * generic_slot_for_autoassign(const ID &animated_id, Action &action, StringRefNull last_slot_identifier)
static void animsys_quaternion_evaluate_fcurves(PointerRNA &ptr, PropertyRNA *prop, const Span< FCurve * > quat_fcurves, const AnimationEvalContext *anim_eval_context, float r_quaternion[4])
Definition pose.cc:152
Set< std::string > BoneNameSet
void BKE_action_find_fcurves_with_bones(bAction *action, animrig::slot_handle_t slot_handle, FoundFCurveCallback callback)
BoneNameSet BKE_pose_channel_find_selected_names(const Object *object)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
float calculate_fcurve(PathResolvedRNA *anim_rna, FCurve *fcu, const AnimationEvalContext *anim_eval_context)
PointerRNA RNA_id_pointer_create(ID *id)
void animsys_evaluate_action(PointerRNA *ptr, bAction *act, int32_t action_slot_handle, const AnimationEvalContext *anim_eval_context, bool flush_to_original)
Definition anim_sys.cc:686
float normalize_qt(float q[4])
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
void BKE_pchan_quat_to_rot(bPoseChannel &pchan, const float4 &quat)
Span< rna_path::Item > ParsedRNAPathRef
Definition RNA_path.hh:113
VecBase< float, 4 > float4
PropertyType RNA_property_type(PropertyRNA *prop)
float4 BKE_object_rot_to_quat(const Object &ob)
Definition object.cc:2994
@ PROP_BOOLEAN
Definition RNA_types.hh:210
void copy_qt_qt(float q[4], const float a[4])
float4 BKE_pchan_rot_to_quat(const bPoseChannel &pchan)
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
void BKE_object_quat_to_rot(Object &ob, const float4 &quat)
Definition object.cc:3010
bool BKE_animsys_read_from_rna_path(PathResolvedRNA *anim_rna, float *r_value)
Definition anim_sys.cc:400
bool BKE_animsys_write_to_rna_path(PathResolvedRNA *anim_rna, float value, bool force_write=false)
Definition anim_sys.cc:461
void eulO_to_quat(float q[4], const float e[3], short order)
signed int int32_t
Definition BKE_text.h:78
bool BKE_animsys_rna_path_resolve(PointerRNA *ptr, ParsedRNAPathRef rna_path, int array_index, PathResolvedRNA *r_result)
Definition anim_sys.cc:356
bool BKE_fcurve_is_empty(const FCurve *fcu)
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
float evaluate_fcurve(const FCurve *fcu, float evaltime)
bActionGroup * grp
eFCurve_Flags flag
StructRNA * type
Definition RNA_types.hh:63