Blender V5.3
pose_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors, Joshua Leung.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_listbase.hh"
15#include "BLI_math_vector_c.hh"
16#include "BLI_string.hh"
17#include "BLI_vector.hh"
18
19#include "DNA_anim_types.h"
20#include "DNA_armature_types.h"
21#include "DNA_object_types.h"
22
23#include "BKE_action.hh"
24#include "BKE_anim_data.hh"
25#include "BKE_idprop.hh"
26#include "BKE_layer.hh"
27#include "BKE_object.hh"
28
29#include "BKE_context.hh"
30
31#include "DEG_depsgraph.hh"
32
33#include "RNA_access.hh"
34#include "RNA_path.hh"
35#include "RNA_prototypes.hh"
36
37#include "WM_api.hh"
38#include "WM_types.hh"
39
40#include "ED_anim_api.hh"
42#include "ED_armature.hh"
43#include "ED_keyframing.hh"
44#include "ED_object.hh"
45
46#include "ANIM_action.hh"
48#include "ANIM_keyframing.hh"
49#include "ANIM_keyingsets.hh"
50#include "ANIM_rna.hh"
51
52#include "armature_intern.hh"
53
54namespace blender {
55
56/* *********************************************** */
57/* Contents of this File:
58 *
59 * This file contains methods shared between Pose Slide and Pose Lib;
60 * primarily the functions in question concern Animato <-> Pose
61 * convenience functions, such as applying/getting pose values
62 * and/or inserting keyframes for these.
63 */
64/* *********************************************** */
65/* FCurves <-> PoseChannels Links */
66
72 PointerRNA &ptr,
73 Vector<FCurve *> &r_curves)
74{
76 if (!adt || !adt->action) {
77 return eAction_TransformFlags(0);
78 }
79 animrig::Action &action = adt->action->wrap();
80
81 short flags = 0;
82
83 /* Get the basic path to the properties of interest. */
84 const std::optional<std::string> path_to_struct = RNA_path_from_ID_to_struct(&ptr);
85 StringRef base_path;
86 if (RNA_struct_is_ID(ptr.type)) {
87 base_path = "";
88 }
89 else {
90 if (!path_to_struct.has_value()) {
92 return eAction_TransformFlags(0);
93 }
94 base_path = path_to_struct.value();
95 }
96
98 const StringRefNull fcurve_path = fcurve.rna_path();
99 if (!base_path.is_empty() && !fcurve_path.startswith(base_path)) {
100 return;
101 }
102
103 StringRef property_name;
104 if (base_path.is_empty()) {
105 property_name = fcurve_path;
106 }
107 else {
108 /* Normal properties are separated by a dot, custom properties don't have that. */
109 if (fcurve_path[base_path.size()] == '.') {
110 property_name = fcurve_path.substr(base_path.size() + 1);
111 }
112 else {
113 property_name = fcurve_path.substr(base_path.size());
114 }
115 }
116
117 if (property_name == "location") {
118 flags |= ACT_TRANS_LOC;
119 r_curves.append(&fcurve);
120 return;
121 }
122
123 if (property_name == "scale") {
124 flags |= ACT_TRANS_SCALE;
125 r_curves.append(&fcurve);
126 return;
127 }
128
129 if (property_name == "rotation_euler" || property_name == "rotation_quaternion" ||
130 property_name == "rotation_axis_angle")
131 {
132 flags |= ACT_TRANS_ROT;
133 r_curves.append(&fcurve);
134 return;
135 }
136
137 if (property_name.startswith("bbone_")) {
138 flags |= ACT_TRANS_BBONE;
139 r_curves.append(&fcurve);
140 return;
141 }
142
143 /* Custom properties only. */
144 if (property_name.startswith("[\"")) {
145 flags |= ACT_TRANS_PROP;
146 r_curves.append(&fcurve);
147 return;
148 }
149 });
150
151 /* return flags found */
152 return eAction_TransformFlags(flags);
153}
154
160 const StringRef property_name,
161 Vector<PropertySnapshot> &snapshots)
162{
163
164 PropertyRNA *prop = RNA_struct_find_property(&ptr, property_name.data());
165 if (!prop) {
166 return;
167 }
168 Array<float> property_values = animrig::rna_property_get_as_float(ptr, *prop);
169 if (property_values.size() == 0) {
170 /* Unsupported property type. */
171 return;
172 }
173 snapshots.append({prop, std::move(property_values)});
174}
175
176static void store_starting_transform(SlideSubject &slide_subject,
177 ed::AnimTransformable &transformable)
178{
179 slide_subject.old_loc = transformable.get_property(
181 slide_subject.old_rot = transformable.get_rotation();
183}
184
188static void store_id_properties(SlideSubject &slide_subject,
189 PointerRNA &ptr,
190 IDProperty *id_properties,
191 IDProperty *system_properties)
192{
193 if (id_properties) {
194 for (const IDProperty &id_prop : id_properties->data.group) {
195 char name_escaped[MAX_IDPROP_NAME * 2];
196 BLI_str_escape(name_escaped, id_prop.name, sizeof(name_escaped));
197 std::string property_name_with_brackets = fmt::format("[\"{}\"]", name_escaped);
198 store_property_snapshot(ptr, property_name_with_brackets, slide_subject.properties);
199 }
200 }
201 if (system_properties) {
202 for (const IDProperty &id_prop : system_properties->data.group) {
203 store_property_snapshot(ptr, id_prop.name, slide_subject.system_properties);
204 }
205 }
206}
207
208/* helper for slide_subjects_get() -> get the relevant F-Curves per PoseChannel */
209static void pchan_to_slide_subject(ListBaseT<SlideSubject> &slide_subjects,
210 Object &ob,
211 bPoseChannel &pchan)
212{
213 PointerRNA bone_ptr = RNA_pointer_create_discrete(&ob.id, RNA_PoseBone, &pchan);
216 ob.id, bone_ptr, curves);
217
218 if (!transFlags) {
219 return;
220 }
221
222 SlideSubject *slide_subject = MEM_new<SlideSubject>("SlideSubject");
223 BLI_addtail(&slide_subjects, slide_subject);
224 slide_subject->fcurves = curves;
225
226 ed::AnimTransformable *transformable = MEM_new<ed::AnimTransformable>(
227 "transformable_pose_bone", ob, pchan);
228 slide_subject->transformable = transformable;
229
230 /* Set pchan's transform flags. */
231 slide_subject->transform_flag = transFlags;
232
233 store_starting_transform(*slide_subject, *transformable);
234
235 slide_subject->ptr = bone_ptr;
236
237 if (transFlags & ACT_TRANS_BBONE) {
238 store_property_snapshot(bone_ptr, "bbone_rollin", slide_subject->additional_properties);
239 store_property_snapshot(bone_ptr, "bbone_rollout", slide_subject->additional_properties);
240 store_property_snapshot(bone_ptr, "bbone_curveinx", slide_subject->additional_properties);
241 store_property_snapshot(bone_ptr, "bbone_curveoutx", slide_subject->additional_properties);
242 store_property_snapshot(bone_ptr, "bbone_curveinz", slide_subject->additional_properties);
243 store_property_snapshot(bone_ptr, "bbone_curveoutz", slide_subject->additional_properties);
244 store_property_snapshot(bone_ptr, "bbone_easein", slide_subject->additional_properties);
245 store_property_snapshot(bone_ptr, "bbone_easeout", slide_subject->additional_properties);
246 store_property_snapshot(bone_ptr, "bbone_scalein", slide_subject->additional_properties);
247 store_property_snapshot(bone_ptr, "bbone_scaleout", slide_subject->additional_properties);
248 }
249
250 /* Make copy of custom properties. */
251 if (transFlags & ACT_TRANS_PROP) {
252 store_id_properties(*slide_subject, bone_ptr, pchan.prop, pchan.system_properties);
253 }
254}
255
257{
259 if (!ELEM(nullptr, ob, ob->data, ob->adt, ob->adt->action)) {
260 return ob;
261 }
262 return nullptr;
263}
264
265static void get_pose_bones_for_slide(bContext *C, ListBaseT<SlideSubject> &slide_subjects)
266{
267 /* For each Pose-Channel which gets affected, get the F-Curves for that channel
268 * and set the relevant transform flags... */
269 Object *prev_ob, *ob_pose_armature;
270
271 prev_ob = nullptr;
272 ob_pose_armature = nullptr;
273 /* Used to avoid duplicates when using mirroring. */
274 Set<bPoseChannel *> inserted_bones;
275 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, selected_pose_bones, Object *, ob) {
276 BLI_assert(pchan != nullptr);
277 if (ob != prev_ob) {
278 prev_ob = ob;
279 ob_pose_armature = animated_armature_ob_get(ob);
280 }
281
282 if (ob_pose_armature == nullptr) {
283 continue;
284 }
285 if (!ob_pose_armature->adt || !ob_pose_armature->adt->action) {
286 /* No action means no FCurves. */
287 continue;
288 }
289
290 if (!inserted_bones.add(pchan)) {
291 continue;
292 }
293 pchan_to_slide_subject(slide_subjects, *ob_pose_armature, *pchan);
294
295 if (ob_pose_armature->pose->flag & POSE_MIRROR_EDIT) {
296 bPoseChannel *pchan_mirror = BKE_pose_channel_get_mirrored(ob->pose, pchan->name);
297 if (pchan_mirror && inserted_bones.add(pchan_mirror)) {
298 pchan_to_slide_subject(slide_subjects, *ob_pose_armature, *pchan_mirror);
299 }
300 }
301 }
303
304 /* If no PoseChannels were found, try a second pass, doing visible ones instead.
305 * i.e. if nothing selected, do whole pose.
306 */
307 if (slide_subjects.is_empty()) {
308 prev_ob = nullptr;
309 ob_pose_armature = nullptr;
310 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, visible_pose_bones, Object *, ob) {
311 BLI_assert(pchan != nullptr);
312 if (ob != prev_ob) {
313 prev_ob = ob;
314 ob_pose_armature = animated_armature_ob_get(ob);
315 }
316
317 if (ob_pose_armature == nullptr) {
318 continue;
319 }
320 if (!ob_pose_armature->adt || !ob_pose_armature->adt->action) {
321 /* No action means no FCurves. */
322 continue;
323 }
324
325 pchan_to_slide_subject(slide_subjects, *ob_pose_armature, *pchan);
326 }
328 }
329}
330
331static void get_objects_for_slide(bContext *C, ListBaseT<SlideSubject> &slider_data)
332{
333 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
334 PointerRNA object_ptr = RNA_pointer_create_discrete(&ob->id, RNA_Object, ob);
335
338 ob->id, object_ptr, curves);
339
340 if (!transFlags) {
341 continue;
342 }
343
344 SlideSubject *slide_subject = MEM_new<SlideSubject>("TransformableFCurveLink");
345 BLI_addtail(&slider_data, slide_subject);
346 slide_subject->fcurves = curves;
347
348 ed::AnimTransformable *transformable = MEM_new<ed::AnimTransformable>("transformable_object",
349 *ob);
350 slide_subject->transformable = transformable;
351 slide_subject->transform_flag = transFlags;
352
353 store_starting_transform(*slide_subject, *transformable);
354 slide_subject->ptr = object_ptr;
355
356 if (transFlags & ACT_TRANS_PROP) {
357 store_id_properties(*slide_subject, object_ptr, ob->id.properties, ob->id.system_properties);
358 }
359 }
361}
362
363void slide_subjects_get(bContext *C, ListBaseT<SlideSubject> *r_transformable_list)
364{
365 BLI_assert(r_transformable_list != nullptr);
367 switch (mode) {
368 case CTX_MODE_POSE:
370 get_pose_bones_for_slide(C, *r_transformable_list);
371 break;
372 case CTX_MODE_OBJECT:
373 get_objects_for_slide(C, *r_transformable_list);
374 break;
375
376 default:
377 /* Not implemented. */
379 break;
380 }
381}
382
383void slide_subjects_free(ListBaseT<SlideSubject> *slide_subjects)
384{
385 SlideSubject *slide_subject, *pfln = nullptr;
386
387 /* free the temp pchan links and their data */
388 for (slide_subject = slide_subjects->first(); slide_subject; slide_subject = pfln) {
389 pfln = slide_subject->next;
390
391 MEM_delete(slide_subject->transformable);
392
393 /* We cannot use BLI_freelinkN because that casts the SlideSubject to a C-style
394 * struct causing MEM_delete to do a C-style delete and not deallocate the Vector. */
395 BLI_remlink(slide_subjects, slide_subject);
396 MEM_delete(slide_subject);
397 }
398}
399
400/* ------------------------- */
401
402void slide_subjects_refresh(bContext *C, const SlideSubject &slide_subject)
403{
404 ID *id = slide_subject.ptr.owner_id;
407 switch (slide_subject.transformable->type()) {
409 WM_event_add_notifier(C, NC_OBJECT | ND_POSE, id_cast<Object *>(id));
410 break;
412 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, id_cast<Object *>(id));
413 break;
414 }
415
417 if (adt && adt->action) {
419 }
420}
421
422void slide_subjects_reset(ListBaseT<SlideSubject> *slide_subjects)
423{
424 /* Iterate over each transformable affected, restoring all channels to their original values. */
425 for (SlideSubject &slide_subject : *slide_subjects) {
426 ed::AnimTransformable *transformable = slide_subject.transformable;
427
428 /* just copy all the values over regardless of whether they changed or not */
430 slide_subject.old_loc,
432 transformable->set_rotation(slide_subject.old_rot);
433 transformable->set_property(
435
436 for (PropertySnapshot &extra_prop : slide_subject.additional_properties) {
438 slide_subject.ptr, *extra_prop.property, extra_prop.values);
439 }
440
441 for (PropertySnapshot &custom_prop : slide_subject.properties) {
443 slide_subject.ptr, *custom_prop.property, custom_prop.values);
444 }
445 for (PropertySnapshot &custom_prop : slide_subject.system_properties) {
447 slide_subject.ptr, *custom_prop.property, custom_prop.values);
448 }
449 }
450}
451
453 Scene *scene,
454 const ListBaseT<SlideSubject> *slide_subjects)
455{
456 bool anything_to_key = false;
457 for (SlideSubject &slide_subject : *slide_subjects) {
458 if (!animrig::autokeyframe_cfra_can_key(scene, slide_subject.ptr.owner_id)) {
459 continue;
460 }
461 anything_to_key = true;
462 break;
463 }
464 /* If there is nothing to key, return before deselecting any keys. */
465 if (!anything_to_key) {
466 return;
467 }
468
470
471 /* Insert keyframes as necessary if auto-key-framing. */
472 for (SlideSubject &slide_subject : *slide_subjects) {
473 PointerRNA &ptr = slide_subject.ptr;
474 if (!animrig::autokeyframe_cfra_can_key(scene, slide_subject.ptr.owner_id)) {
475 continue;
476 }
477
478 Vector<RNAPath> paths;
479 /* The transform flags tell us which properties have keys. Properties without keys cannot pose
480 * slide, so should not be auto keyed. */
481 if (slide_subject.transform_flag & ACT_TRANS_LOC) {
482 paths.append({"location"});
483 }
484 if (slide_subject.transform_flag & ACT_TRANS_ROT) {
485 paths.append(
486 {animrig::get_rotation_mode_path(slide_subject.transformable->get_rotation_mode())});
487 }
488 if (slide_subject.transform_flag & ACT_TRANS_SCALE) {
489 paths.append({"scale"});
490 }
491
492 /* No need to check the transform_flag here, because those vectors are only filled if the flag
493 * was set in the first place.*/
494 for (const PropertySnapshot &snapshot : slide_subject.additional_properties) {
495 paths.append({RNA_property_identifier(snapshot.property)});
496 }
497 for (const PropertySnapshot &snapshot : slide_subject.properties) {
498 char name_escaped[MAX_IDPROP_NAME * 2];
500 name_escaped, RNA_property_identifier(snapshot.property), sizeof(name_escaped));
501 paths.append({fmt::format("[\"{}\"]", name_escaped)});
502 }
503 for (const PropertySnapshot &snapshot : slide_subject.system_properties) {
504 paths.append({RNA_property_identifier(snapshot.property)});
505 }
506
507 switch (slide_subject.transformable->type()) {
510 scene,
511 id_cast<Object *>(ptr.owner_id),
512 static_cast<bPoseChannel *>(ptr.data),
513 paths,
514 false);
515 break;
517 animrig::autokeyframe_object(C, scene, id_cast<Object *>(ptr.owner_id), paths);
518 break;
519 }
520 }
521
522 Vector<Object *> objects;
523 for (SlideSubject &slide_subject : *slide_subjects) {
524 ID *owner_id = slide_subject.transformable->owner_id();
525 if (GS(owner_id->name) != ID_OB) {
526 continue;
527 }
528 objects.append(id_cast<Object *>(owner_id));
529 }
530 /* This includes all motion paths for bones. Could be more fine grained in the future to avoid
531 * needless updates to data that was not changed. */
532 ed::object::motion_paths_recalc(C, scene, objects);
534}
535
536/* *********************************************** */
537
538} // namespace blender
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
Functions to insert, delete or modify keyframes.
Functionality to interact with keying sets.
Helper functions for animation to interact with the RNA system.
Blender kernel action and pose functionality.
#define CTX_DATA_BEGIN_WITH_ID(C, Type, instance, member, Type_id, instance_id)
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
General operations, lookup, etc. for blender objects.
#define MAX_IDPROP_NAME
Definition DNA_ID.h:174
Object is a sort of wrapper for general info.
Defines an abstraction around various structs to modify their transform properties via a unified API ...
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_ANIMATION
Definition WM_types.hh:401
#define NA_ADDED
Definition WM_types.hh:636
#define ND_POSE
Definition WM_types.hh:482
#define ND_TRANSFORM
Definition WM_types.hh:475
#define ND_KEYFRAME
Definition WM_types.hh:537
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_assert_unreachable()
#define ELEM(...)
int64_t size() const
Definition BLI_array.hh:267
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
constexpr bool is_empty() const
constexpr const char * data() const
void set_rotation(const Rotation &rotation)
TransformFloats get_property(PropertyType prop_type) const
void set_property(PropertyType prop_type, Span< float > values, AxisMutable axis_flag)
void foreach_fcurve_in_action_slot(Action &action, slot_handle_t handle, FunctionRef< void(FCurve &fcurve)> callback)
bool autokeyframe_cfra_can_key(const Scene *scene, ID *id)
StringRefNull get_rotation_mode_path(eRotationModes rotation_mode)
Definition anim_rna.cc:109
void rna_property_set_as_float(PointerRNA &ptr, PropertyRNA &prop, Span< float > values)
Definition anim_rna.cc:301
void autokeyframe_object(bContext *C, const Scene *scene, Object *ob, Span< RNAPath > rna_paths)
Array< float > rna_property_get_as_float(PointerRNA &ptr, PropertyRNA &prop)
Definition anim_rna.cc:253
void autokeyframe_pose_channel(bContext *C, Scene *scene, Object *ob, bPoseChannel *pose_channel, Span< RNAPath > rna_paths, short targetless_ik)
void motion_paths_recalc(bContext *C, Scene *scene, const Span< Object * > objects)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void get_pose_bones_for_slide(bContext *C, ListBaseT< SlideSubject > &slide_subjects)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void slide_subjects_free(ListBaseT< SlideSubject > *slide_subjects)
const char * RNA_property_identifier(const PropertyRNA *prop)
std::optional< std::string > RNA_path_from_ID_to_struct(const PointerRNA *ptr)
Definition rna_path.cc:1078
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
static void store_property_snapshot(PointerRNA &ptr, const StringRef property_name, Vector< PropertySnapshot > &snapshots)
@ CTX_MODE_OBJECT
@ CTX_MODE_PAINT_WEIGHT
void slide_subjects_reset(ListBaseT< SlideSubject > *slide_subjects)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
static eAction_TransformFlags get_item_transform_flags_and_fcurves(ID &id, PointerRNA &ptr, Vector< FCurve * > &r_curves)
Definition pose_utils.cc:71
bPoseChannel * BKE_pose_channel_get_mirrored(const bPose *pose, const char *name) ATTR_WARN_UNUSED_RESULT
static Object * animated_armature_ob_get(Object *ob_)
void slide_subjects_get(bContext *C, ListBaseT< SlideSubject > *r_transformable_list)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
Object * BKE_object_pose_armature_get(Object *ob)
Definition object.cc:2507
void ANIM_deselect_keys_in_animation_editors(bContext *C)
Definition anim_deps.cc:487
void slide_subjects_autokey(bContext *C, Scene *scene, const ListBaseT< SlideSubject > *slide_subjects)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void slide_subjects_refresh(bContext *C, const SlideSubject &slide_subject)
bool RNA_struct_is_ID(const StructRNA *type)
static void store_id_properties(SlideSubject &slide_subject, PointerRNA &ptr, IDProperty *id_properties, IDProperty *system_properties)
static void pchan_to_slide_subject(ListBaseT< SlideSubject > &slide_subjects, Object &ob, bPoseChannel &pchan)
static void get_objects_for_slide(bContext *C, ListBaseT< SlideSubject > &slider_data)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static void store_starting_transform(SlideSubject &slide_subject, ed::AnimTransformable &transformable)
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
IDPropertyData data
Definition DNA_ID.h:157
char name[258]
Definition DNA_ID.h:433
struct AnimData * adt
struct bPose * pose
StructRNA * type
Definition RNA_types.hh:63
eAction_TransformFlags transform_flag
Vector< PropertySnapshot > additional_properties
Vector< PropertySnapshot > system_properties
ed::TransformFloats old_loc
Vector< PropertySnapshot > properties
ed::AnimTransformable * transformable
Vector< FCurve * > fcurves
ed::TransformFloats old_scale