Blender V5.3
action_iterators.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 "ANIM_action.hh"
11
12#include "BLI_assert.hh"
13#include "BLI_listbase.hh"
14
15#include "BKE_anim_data.hh"
16#include "BKE_armature.hh"
17#include "BKE_nla.hh"
18
20#include "DNA_object_types.h"
21
22#include "RNA_access.hh"
23#include "RNA_prototypes.hh"
24
26
27namespace blender::animrig {
28
29void foreach_fcurve_in_action(Action &action, FunctionRef<void(FCurve &fcurve)> callback)
30{
31 for (Layer *layer : action.layers()) {
32 for (Strip *strip : layer->strips()) {
33 if (strip->type() != Strip::Type::Keyframe) {
34 continue;
35 }
36 for (Channelbag *bag : strip->data<StripKeyframeData>(action).channelbags()) {
37 for (FCurve *fcu : bag->fcurves()) {
38 callback(*fcu);
39 }
40 }
41 }
42 }
43}
44
46 slot_handle_t handle,
47 FunctionRef<void(FCurve &fcurve)> callback)
48{
49 /* Once layers can be locked, this needs to be checked here. */
51 for (Layer *layer : action.layers()) {
52 for (Strip *strip : layer->strips()) {
53 if (strip->type() != Strip::Type::Keyframe) {
54 continue;
55 }
56 for (Channelbag *bag : strip->data<StripKeyframeData>(action).channelbags()) {
57 if (bag->slot_handle != handle) {
58 continue;
59 }
60 for (FCurve *fcu : bag->fcurves()) {
61 BLI_assert(fcu != nullptr);
62 if (fcu->flag & FCURVE_PROTECTED) {
63 continue;
64 }
65 callback(*fcu);
66 }
67 }
68 }
69 }
70}
71
73 slot_handle_t handle,
74 FunctionRef<void(FCurve &fcurve)> callback)
75{
76 for (Layer *layer : action.layers()) {
77 for (Strip *strip : layer->strips()) {
78 if (strip->type() != Strip::Type::Keyframe) {
79 continue;
80 }
81 for (Channelbag *bag : strip->data<StripKeyframeData>(action).channelbags()) {
82 if (bag->slot_handle != handle) {
83 continue;
84 }
85 for (FCurve *fcu : bag->fcurves()) {
86 BLI_assert(fcu != nullptr);
87 callback(*fcu);
88 }
89 }
90 }
91 }
92}
93
94bool foreach_action_slot_use(const ID &animated_id,
95 FunctionRef<bool(Action &action, slot_handle_t slot_handle)> callback)
96{
97
98 const auto forward_to_callback = [&](ID & /* animated_id */,
99 bAction *&action_ptr_ref,
100 const slot_handle_t &slot_handle_ref,
101 char * /*last_slot_identifier*/) -> bool {
102 if (!action_ptr_ref) {
103 return true;
104 }
105 return callback(action_ptr_ref->wrap(), slot_handle_ref);
106 };
107
108 return foreach_action_slot_use_with_references(const_cast<ID &>(animated_id),
109 forward_to_callback);
110}
111
113 ID &animated_id,
114 FunctionRef<bool(ID &animated_id,
115 bAction *&action_ptr_ref,
116 slot_handle_t &slot_handle_ref,
117 char *last_slot_identifier)> callback)
118{
119 AnimData *adt = BKE_animdata_from_id(&animated_id);
120
121 if (adt) {
122 if (adt->action) {
123 /* Direct assignment. */
124 if (!callback(animated_id, adt->action, adt->slot_handle, adt->last_slot_identifier)) {
125 return false;
126 }
127 }
128
129 /* NLA strips. */
130 const bool looped_until_last_strip = bke::nla::foreach_strip_adt(*adt, [&](NlaStrip *strip) {
131 if (strip->act) {
132 if (!callback(
133 animated_id, strip->act, strip->action_slot_handle, strip->last_slot_identifier))
134 {
135 return false;
136 }
137 }
138 return true;
139 });
140 if (!looped_until_last_strip) {
141 return false;
142 }
143 }
144
145 /* The rest of the code deals with constraints, so only relevant when this is an Object. */
146 if (GS(animated_id.name) != ID_OB) {
147 return true;
148 }
149
150 /* Cannot be `const` because of the BKE_pose_ensure() call below. */
151 Object &object = reinterpret_cast<Object &>(animated_id);
152
159 auto visit_constraint = [&](const bConstraint &constraint) -> bool {
160 if (constraint.type != CONSTRAINT_TYPE_ACTION) {
161 return true;
162 }
163 bActionConstraint *constraint_data = static_cast<bActionConstraint *>(constraint.data);
164 if (!constraint_data->act) {
165 return true;
166 }
167 return callback(animated_id,
168 constraint_data->act,
169 constraint_data->action_slot_handle,
170 constraint_data->last_slot_identifier);
171 };
172
173 /* Visit Object constraints. */
174 for (bConstraint &con : object.constraints) {
175 if (!visit_constraint(con)) {
176 return false;
177 }
178 }
179
180 /* Visit Pose Bone constraints. */
181 if (object.type == OB_ARMATURE) {
182 bArmature *arm = id_cast<bArmature *>(object.data);
183 BKE_pose_ensure(nullptr, &object, arm, /*do_id_user=*/DEG_is_original(&object));
184 for (bPoseChannel &pchan : object.pose->chanbase) {
185 for (bConstraint &con : pchan.constraints) {
186 if (!visit_constraint(con)) {
187 return false;
188 }
189 }
190 }
191 }
192
193 return true;
194}
195
197 FunctionRef<bool(ID &animated_id,
198 bAction *action,
199 PointerRNA &action_slot_ptr,
200 PropertyRNA &action_slot_prop,
201 char *last_slot_identifier)> callback)
202{
203 /* This function has to copy the logic of #foreach_action_slot_use_with_references(),
204 * as it needs to know where exactly those pointers came from. */
205
206 AnimData *adt = BKE_animdata_from_id(&animated_id);
207
208 if (adt) {
209 if (adt->action) {
210 /* Direct assignment. */
211 PointerRNA ptr = RNA_pointer_create_discrete(&animated_id, RNA_AnimData, adt);
212 PropertyRNA *prop = RNA_struct_find_property(&ptr, "action_slot");
213 if (!callback(animated_id, adt->action, ptr, *prop, adt->last_slot_identifier)) {
214 return false;
215 }
216 }
217
218 /* NLA strips. */
219 const bool looped_until_last_strip = bke::nla::foreach_strip_adt(*adt, [&](NlaStrip *strip) {
220 if (strip->act) {
221 PointerRNA ptr = RNA_pointer_create_discrete(&animated_id, RNA_NlaStrip, strip);
222 PropertyRNA *prop = RNA_struct_find_property(&ptr, "action_slot");
223
224 if (!callback(animated_id, strip->act, ptr, *prop, strip->last_slot_identifier)) {
225 return false;
226 }
227 }
228 return true;
229 });
230 if (!looped_until_last_strip) {
231 return false;
232 }
233 }
234
235 /* The rest of the code deals with constraints, so only relevant when this is an Object. */
236 if (GS(animated_id.name) != ID_OB) {
237 return true;
238 }
239
240 /* Cannot be `const` because of the BKE_pose_ensure() call below. */
241 Object &object = reinterpret_cast<Object &>(animated_id);
242
249 auto visit_constraint = [&](bConstraint &constraint) -> bool {
250 if (constraint.type != CONSTRAINT_TYPE_ACTION) {
251 return true;
252 }
253 bActionConstraint *constraint_data = static_cast<bActionConstraint *>(constraint.data);
254 if (!constraint_data->act) {
255 return true;
256 }
257
258 PointerRNA ptr = RNA_pointer_create_discrete(&animated_id, RNA_ActionConstraint, &constraint);
259 PropertyRNA *prop = RNA_struct_find_property(&ptr, "action_slot");
260
261 return callback(
262 animated_id, constraint_data->act, ptr, *prop, constraint_data->last_slot_identifier);
263 };
264
265 /* Visit Object constraints. */
266 for (bConstraint &con : object.constraints) {
267 if (!visit_constraint(con)) {
268 return false;
269 }
270 }
271
272 /* Visit Pose Bone constraints. */
273 if (object.type == OB_ARMATURE) {
274 bArmature *arm = id_cast<bArmature *>(object.data);
275 BKE_pose_ensure(nullptr, &object, arm, /*do_id_user=*/DEG_is_original(&object));
276
277 for (bPoseChannel &pchan : object.pose->chanbase) {
278 for (bConstraint &con : pchan.constraints) {
279 if (!visit_constraint(con)) {
280 return false;
281 }
282 }
283 }
284 }
285
286 return true;
287}
288
289} // namespace blender::animrig
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
Object is a sort of wrapper for general info.
btSequentialImpulseConstraintSolverMt int btPersistentManifold int btTypedConstraint ** constraints
Span< const Layer * > layers() const
Span< const Channelbag * > channelbags() const
#define layer
void foreach_fcurve_in_action_slot(Action &action, slot_handle_t handle, FunctionRef< void(FCurve &fcurve)> callback)
void assert_baklava_phase_1_invariants(const Action &action)
decltype(blender::ActionSlot::handle) slot_handle_t
bool foreach_action_slot_use(const ID &animated_id, FunctionRef< bool(Action &action, slot_handle_t slot_handle)> callback)
void foreach_fcurve_in_action(Action &action, FunctionRef< void(FCurve &fcurve)> callback)
bool foreach_action_slot_use_with_rna(ID &animated_id, FunctionRef< bool(ID &animated_id, bAction *action, PointerRNA &action_slot_owner_ptr, PropertyRNA &action_slot_prop, char *last_slot_identifier)> callback)
void foreach_fcurve_in_action_slot_editable(Action &action, slot_handle_t handle, FunctionRef< void(FCurve &fcurve)> callback)
bool foreach_action_slot_use_with_references(ID &animated_id, FunctionRef< bool(ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *last_slot_identifier)> callback)
bool foreach_strip_adt(const AnimData &adt, FunctionRef< bool(NlaStrip *)> callback)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
bool DEG_is_original(const T *id)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
BMesh const char void * data
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
char last_slot_identifier[258]
char name[258]
Definition DNA_ID.h:433
char last_slot_identifier[258]