Blender V5.3
pose_backup.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_pose_backup.h"
10
11#include <cstring>
12
13#include "BLI_listbase.hh"
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_action_types.h"
18#include "DNA_armature_types.h"
19#include "DNA_object_types.h"
20
21#include "BKE_action.hh"
22#include "BKE_armature.hh"
23#include "BKE_idprop.hh"
24#include "BKE_object_types.hh"
25
26#include "ANIM_action.hh"
27#include "ANIM_pose.hh"
28
29namespace blender {
30
31using namespace blender::bke;
32
33/* simple struct for storing backup info for one pose channel */
36
37 bPoseChannel *pchan; /* Pose channel this backup is for. */
38
39 bPoseChannel olddata; /* Backup of pose channel. */
40 IDProperty *oldprops; /* Backup copy (needs freeing) of pose channel's ID properties. */
41 /* Backup copy (needs freeing) of pose channel's system IDProperties. */
43 const Object *owner; /* The object to which this pose channel belongs. */
44};
45
46struct PoseBackup {
48 ListBaseT<PoseChannelBackup> backups;
49};
50
59static void pose_backup_create(const Object *ob,
60 bAction *action,
61 const BoneNameSet &selected_bone_names,
62 PoseBackup &pose_backup)
63{
64 BoneNameSet backed_up_bone_names;
65 const bool is_bone_selection_relevant = pose_backup.is_bone_selection_relevant;
66 /* Make a backup of the given pose channel. */
67 auto store_animated_pchans = [&](const FCurve * /*unused*/, const char *bone_name) {
68 if (backed_up_bone_names.contains(bone_name)) {
69 /* Only backup each bone once. */
70 return;
71 }
72
73 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
74 if (pchan == nullptr) {
75 /* FCurve targets non-existent bone. */
76 return;
77 }
78
79 if (is_bone_selection_relevant && !selected_bone_names.contains(bone_name)) {
80 return;
81 }
82
83 PoseChannelBackup *chan_bak = MEM_new<PoseChannelBackup>("PoseChannelBackup");
84 chan_bak->pchan = pchan;
85 chan_bak->olddata = dna::shallow_copy(*chan_bak->pchan);
86 chan_bak->owner = ob;
87
88 if (pchan->prop) {
89 chan_bak->oldprops = IDP_CopyProperty(pchan->prop);
90 }
91 if (pchan->system_properties) {
93 }
94
95 BLI_addtail(&pose_backup.backups, chan_bak);
96 backed_up_bone_names.add_new(bone_name);
97 };
98
99 animrig::Slot &slot = animrig::get_best_pose_slot_for_id(ob->id, action->wrap());
100 /* Call `store_animated_pchans()` for each FCurve that targets a bone. */
101 BKE_action_find_fcurves_with_bones(action, slot.handle, store_animated_pchans);
102}
103
105{
106 Set<bPoseChannel *> selected_bones;
107 bool all_bones_selected = true;
108
109 for (Object *obj : objects) {
110 /* Iterate over the selected bones to fill the set of bone names. */
111 for (bPoseChannel &pose_bone : obj->pose->chanbase) {
112 if (pose_bone.flag & POSE_SELECTED) {
113 selected_bones.add(&pose_bone);
114 }
115 else {
116 all_bones_selected = false;
117 }
118 }
119 }
120
121 /* If all bones are selected, act as if none are. */
122 if (all_bones_selected) {
123 return {};
124 }
125
126 return selected_bones;
127}
128
130{
131 PoseBackup *pose_backup = MEM_new_zeroed<PoseBackup>(__func__);
132 pose_backup->backups = {nullptr, nullptr};
133 pose_backup->is_bone_selection_relevant = false;
134 for (Object *ob : objects) {
135 pose_backup_create(ob, const_cast<bAction *>(action), BoneNameSet(), *pose_backup);
136 }
137 return pose_backup;
138}
139
141{
142 PoseBackup *pose_backup = MEM_new_zeroed<PoseBackup>(__func__);
143 pose_backup->backups = {nullptr, nullptr};
145 pose_backup->is_bone_selection_relevant = !selected_bones.is_empty();
146
147 for (Object *ob : objects) {
148 const BoneNameSet selected_bone_names = BKE_pose_channel_find_selected_names(ob);
149 pose_backup_create(ob, const_cast<bAction *>(action), selected_bone_names, *pose_backup);
150 }
151
152 return pose_backup;
153}
154
156{
157 return pose_backup->is_bone_selection_relevant;
158}
159
161{
162 for (PoseChannelBackup &chan_bak : pbd->backups) {
163 *chan_bak.pchan = dna::shallow_copy(chan_bak.olddata);
164
165 if (chan_bak.oldprops) {
166 IDP_SyncGroupValues(chan_bak.pchan->prop, chan_bak.oldprops);
167 }
168 if (chan_bak.old_system_properties) {
170 }
171
172 /* TODO: constraints settings aren't restored yet,
173 * even though these could change (though not that likely) */
174 }
175}
176
178{
179 if (!pbd) {
180 /* Can happen if initialization was aborted. */
181 return;
182 }
183 for (PoseChannelBackup &chan_bak : pbd->backups.items_mutable()) {
184 if (chan_bak.oldprops) {
185 IDP_FreeProperty(chan_bak.oldprops);
186 }
187 if (chan_bak.old_system_properties) {
188 IDP_FreeProperty(chan_bak.old_system_properties);
189 }
190 BLI_freelinkN(&pbd->backups, &chan_bak);
191 }
192 MEM_delete(pbd);
193}
194
196{
198 PoseBackup *pose_backup = BKE_pose_backup_create_all_bones({ob}, action);
199 ob->runtime->pose_backup = pose_backup;
200}
201
203{
204 if (ob->runtime->pose_backup == nullptr) {
205 return false;
206 }
208 return true;
209}
210
212{
213 if (ob->runtime->pose_backup == nullptr) {
214 return;
215 }
216
218 ob->runtime->pose_backup = nullptr;
219}
220
221} // namespace blender
Functions and classes to work with Actions.
Functions to work with animation poses.
Blender kernel action and pose functionality.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:614
void add_new(const Key &key)
Definition BLI_set.hh:233
Slot & get_best_pose_slot_for_id(const ID &id, Action &pose_data)
Definition pose.cc:471
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)
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
BMesh Iterators.
void BKE_pose_backup_restore(const struct PoseBackup *pbd)
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
static void pose_backup_create(const Object *ob, bAction *action, const BoneNameSet &selected_bone_names, PoseBackup &pose_backup)
static Set< bPoseChannel * > armature_find_selected_pose_bones(Span< Object * > objects)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool BKE_pose_backup_restore_on_object(struct Object *ob)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void BKE_pose_backup_create_on_object(struct Object *ob, const struct bAction *action)
void IDP_SyncGroupValues(IDProperty *dest, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:655
struct PoseBackup * BKE_pose_backup_create_selected_bones(Span< Object * > objects, const struct bAction *action) ATTR_WARN_UNUSED_RESULT
struct PoseBackup * BKE_pose_backup_create_all_bones(Span< Object * > objects, const struct bAction *action) ATTR_WARN_UNUSED_RESULT
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_pose_backup_clear(struct Object *ob)
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void BKE_pose_backup_free(struct PoseBackup *pbd)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
bool BKE_pose_backup_is_selection_relevant(const struct PoseBackup *pose_backup)
bke::ObjectRuntime * runtime
struct bPose * pose
ListBaseT< PoseChannelBackup > backups
PoseChannelBackup * prev
PoseChannelBackup * next
IDProperty * old_system_properties