Blender V5.3
animdata.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#include "ANIM_action.hh"
9#include "ANIM_animdata.hh"
10
11#include "BKE_action.hh"
12#include "BKE_anim_data.hh"
13#include "BKE_fcurve.hh"
14#include "BKE_key.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_main.hh"
17#include "BKE_material.hh"
18#include "BKE_node.hh"
19
20#include "BLT_translation.hh"
21
22#include "BLI_listbase.hh"
23#include "BLI_string_utf8.hh"
24
25#include "DEG_depsgraph.hh"
27
28#include "DNA_anim_types.h"
29#include "DNA_key_types.h"
30#include "DNA_material_types.h"
31#include "DNA_particle_types.h"
32
33#include "RNA_access.hh"
34
35namespace blender::animrig {
36
37/* -------------------------------------------------------------------- */
40
41/* Find the users of the given ID within the objects of `bmain` and add non-duplicates to the end
42 * of `related_ids`. */
43static void add_object_data_users(const Main &bmain, const ID &id, Vector<ID *> &related_ids)
44{
45 if (ID_REAL_USERS(&id) != 1) {
46 /* Only find objects if this ID is only used once. */
47 return;
48 }
49
50 Object *ob;
51 ID *object_id;
52 FOREACH_MAIN_LISTBASE_ID_BEGIN (&bmain.objects, object_id) {
53 ob = reinterpret_cast<Object *>(object_id);
54 if (ob->data != &id) {
55 continue;
56 }
57 related_ids.append_non_duplicates(&ob->id);
58 }
60}
61
63{
64 Vector<ID *> related_ids({&id});
65
66 /* `related_ids` can grow during an iteration if the ID of the current iteration has associated
67 * code that defines relationships. */
68 for (int i = 0; i < related_ids.size(); i++) {
69 ID *related_id = related_ids[i];
70
71 if (related_id->flag & ID_FLAG_EMBEDDED_DATA) {
72 /* No matter the type of embedded ID, their owner can always be added to the related IDs. */
73
74 /* User counting is irrelevant for the logic here, because embedded IDs cannot be shared.
75 * Embedded IDs do exist (sometimes) with a non-zero user count, hence the assertion that the
76 * user count is not greater than 1. */
77 BLI_assert(ID_REAL_USERS(related_id) <= 1);
78 ID *owner_id = BKE_id_owner_get(related_id);
79 /* Embedded IDs should always have an owner. */
80 BLI_assert(owner_id != nullptr);
81 related_ids.append_non_duplicates(owner_id);
82 }
83
84 /* No action found on current ID, add related IDs to the ID Vector. */
85 switch (GS(related_id->name)) {
86 case ID_OB: {
87 Object *ob = reinterpret_cast<Object *>(related_id);
88 if (!ob->data) {
89 break;
90 }
91 ID *data = ob->data;
92 if (ID_REAL_USERS(data) == 1) {
93 related_ids.append_non_duplicates(data);
94 }
95 for (ParticleSystem &particle_system : ob->particlesystem) {
96 if (!particle_system.part) {
97 continue;
98 }
99 if (ID_REAL_USERS(&particle_system.part->id) != 1) {
100 continue;
101 }
102 related_ids.append_non_duplicates(&particle_system.part->id);
103 }
104 break;
105 }
106
107 case ID_KE: {
108 /* Shape-keys. */
109 Key *key = reinterpret_cast<Key *>(related_id);
110 /* Shape-keys are not embedded but there is currently no way to reuse them. */
111 BLI_assert(ID_REAL_USERS(related_id) == 1);
112 related_ids.append_non_duplicates(key->from);
113 break;
114 }
115
116 case ID_MA: {
117 /* Explicitly not relating materials and material users. */
118 Material *mat = reinterpret_cast<Material *>(related_id);
119 if (mat->nodetree && ID_REAL_USERS(&mat->nodetree->id) == 1) {
120 related_ids.append_non_duplicates(&mat->nodetree->id);
121 }
122 break;
123 }
124
125 case ID_PA: {
126 if (ID_REAL_USERS(related_id) != 1) {
127 continue;
128 }
129 Object *ob;
130 ID *object_id;
131 /* Find users of this particle setting. */
132 FOREACH_MAIN_LISTBASE_ID_BEGIN (&bmain.objects, object_id) {
133 ob = reinterpret_cast<Object *>(object_id);
134 bool object_uses_particle_settings = false;
135 for (ParticleSystem &particle_system : ob->particlesystem) {
136 if (!particle_system.part) {
137 continue;
138 }
139 if (&particle_system.part->id != related_id) {
140 continue;
141 }
142 object_uses_particle_settings = true;
143 break;
144 }
145 if (object_uses_particle_settings) {
146 related_ids.append_non_duplicates(&ob->id);
147 break;
148 }
149 }
151
152 break;
153 }
154
155 default: {
156 /* Just check if the ID is used as object data somewhere. */
157 add_object_data_users(bmain, *related_id, related_ids);
158 bNodeTree *node_tree = bke::node_tree_from_id(related_id);
159 if (node_tree && ID_REAL_USERS(&node_tree->id) == 1) {
160 related_ids.append_non_duplicates(&node_tree->id);
161 }
162
163 Key *key = BKE_key_from_id(related_id);
164 if (key) {
165 /* No check for multi user because the shape-key cannot be shared. */
166 BLI_assert(ID_REAL_USERS(&key->id) == 1);
167 related_ids.append_non_duplicates(&key->id);
168 }
169 break;
170 }
171 }
172 }
173
174 return related_ids;
175}
176
177/* Find an action on an ID that is related to the given ID. Related things are e.g. Object<->Data,
178 * Mesh<->Material and so on. */
179static bAction *find_related_action(Main &bmain, ID &id)
180{
181 Vector<ID *> related_ids = find_related_ids(bmain, id);
182
183 for (ID *related_id : related_ids) {
184 Action *action = get_action(*related_id);
185 if (action && BKE_id_is_editable(&bmain, &action->id)) {
186 /* Returning the first action found means highest priority has the action closest in the
187 * relationship graph. */
188 return action;
189 }
190 }
191
192 return nullptr;
193}
194
196{
198 if (adt == nullptr) {
199 printf("ERROR: data-block type is not animatable (ID = %s)\n", (id) ? (id->name) : "<None>");
200 return nullptr;
201 }
202
203 /* init action if none available yet */
204 /* TODO: need some wizardry to handle NLA stuff correct */
205 if (adt->action == nullptr) {
206 bAction *action = find_related_action(*bmain, *id);
207
208 if (action == nullptr) {
209 /* init action name from name of ID block */
210 char actname[sizeof(id->name) - 2];
211 if (id->flag & ID_FLAG_EMBEDDED_DATA) {
212 /* When the ID is embedded, use the name of the owner ID for clarity. */
213 ID *owner_id = BKE_id_owner_get(id);
214 /* If the ID is embedded it should have an owner. */
215 BLI_assert(owner_id != nullptr);
216 SNPRINTF_UTF8(actname, DATA_("%sAction"), owner_id->name + 2);
217 }
218 else if (GS(id->name) == ID_KE) {
219 Key *key = reinterpret_cast<Key *>(id);
220 SNPRINTF_UTF8(actname, DATA_("%sAction"), key->from->name + 2);
221 }
222 else {
223 SNPRINTF_UTF8(actname, DATA_("%sAction"), id->name + 2);
224 }
225
226 /* create action */
227 action = BKE_action_add(bmain, actname);
228
229 /* Decrement the default-1 user count, as assigning it will increase it again. */
230 BLI_assert(action->id.us == 1);
231 id_us_min(&action->id);
232 }
233
234 /* Assigning the Action should always work here. The only reason it wouldn't, is when a legacy
235 * Action of the wrong ID type is assigned, but since in this branch of the code we're only
236 * dealing with either new or layered Actions, this will never fail. */
237 const bool ok = animrig::assign_action(action, {*id, *adt});
238 BLI_assert_msg(ok, "Expecting Action assignment to work here");
240
241 /* Tag depsgraph to be rebuilt to include time dependency. */
243 }
244
246
247 /* return the action */
248 return adt->action;
249}
250
252{
253 /* - If no AnimData, we've got nowhere to remove the F-Curve from
254 * (this doesn't guarantee that the F-Curve is in there, but at least we tried).
255 * - If no F-Curve, there is nothing to remove
256 */
257 if (ELEM(nullptr, adt, fcu)) {
258 return;
259 }
260
261 const bool is_driver = fcu->driver != nullptr;
262 if (is_driver) {
263 BLI_remlink(&adt->drivers, fcu);
264 }
265 else if (adt->action) {
266 Action &action = adt->action->wrap();
267 action_fcurve_remove(action, *fcu);
268 /* Return early to avoid the call to BKE_fcurve_free because the fcu has already been freed
269 * by action_fcurve_remove. */
270 return;
271 }
272 else {
274 }
275
276 BKE_fcurve_free(fcu);
277}
278
280{
281 if (adt->action != nullptr) {
282 bAction *act = adt->action;
284 Action &action = act->wrap();
285 if (action.is_empty() && (adt->flag & ADT_NLA_EDIT_ON) == 0) {
286 id_us_min(&act->id);
287 adt->action = nullptr;
288 return true;
289 }
290 }
291
292 return false;
293}
294
296
299 const int array_index)
300{
301 BLI_assert(adt.action);
302 if (!adt.action) {
303 return nullptr;
304 }
305
306 const Action &action = adt.action->wrap();
307
308 const Slot *slot = action.slot_for_handle(adt.slot_handle);
309 if (!slot) {
310 /* No need to inspect anything if this ID does not have an Action Slot. */
311 return nullptr;
312 }
313
314 /* No check for the slot's ID type. Not only do we not have the actual ID
315 * to do this check, but also, since the Action and the slot have been
316 * assigned, just trust that it's valid. */
317
318 /* Iterate the layers top-down, as higher-up animation overrides (or at least can override)
319 * lower-down animation. */
320 for (int layer_idx = action.layer_array_num - 1; layer_idx >= 0; layer_idx--) {
321 const Layer *layer = action.layer(layer_idx);
322
323 /* TODO: refactor this into something nicer once we have different strip types. */
324 for (const Strip *strip : layer->strips()) {
325 switch (strip->type()) {
327 const StripKeyframeData &strip_data = strip->data<StripKeyframeData>(action);
328 const Channelbag *channelbag_for_slot = strip_data.channelbag_for_slot(*slot);
329 if (!channelbag_for_slot) {
330 continue;
331 }
332 const FCurve *fcu = channelbag_for_slot->fcurve_find({rna_path, array_index});
333 if (!fcu) {
334 continue;
335 }
336
337 /* This code assumes that there is only one strip, and that it's infinite. When that
338 * changes, this code needs to be expanded to check for strip boundaries. */
339 return fcu;
340 }
341 }
342 /* Explicit lack of 'default' clause, to get compiler warnings when strip types are added. */
343 }
344 }
345
346 return nullptr;
347}
348
350{
351 if (!adt || !adt->action) {
352 return {};
353 }
354 return fcurves_for_action_slot(adt->action->wrap(), adt->slot_handle);
355}
356
358{
359 if (!adt || !adt->action) {
360 return {};
361 }
362 return fcurves_for_action_slot(const_cast<const bAction *>(adt->action)->wrap(),
363 adt->slot_handle);
364}
365
366} // namespace blender::animrig
Functions and classes to work with Actions.
Functions to work with AnimData.
Blender kernel action and pose functionality.
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
General operations, lookup, etc. for materials.
#define SNPRINTF_UTF8(dst, format,...)
#define DATA_(msgid)
#define ID_REAL_USERS(id)
Definition DNA_ID.h:679
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
void append_non_duplicates(const T &value)
const Layer * layer(int64_t index) const
Slot * slot_for_handle(slot_handle_t handle)
const Channelbag * channelbag_for_slot(const Slot &slot) const
#define layer
bool action_fcurve_remove(Action &action, FCurve &fcu)
void animdata_fcurve_delete(AnimData *adt, FCurve *fcu)
Definition animdata.cc:251
Vector< ID * > find_related_ids(Main &bmain, ID &id)
Definition animdata.cc:62
bAction * id_action_ensure(Main *bmain, ID *id)
Definition animdata.cc:195
static void add_object_data_users(const Main &bmain, const ID &id, Vector< ID * > &related_ids)
Definition animdata.cc:43
Span< FCurve * > fcurves_for_action_slot(Action &action, slot_handle_t slot_handle)
Action * get_action(ID &animated_id)
bool animdata_remove_empty_action(AnimData *adt)
Definition animdata.cc:279
Span< FCurve * > fcurves_for_assigned_action(AnimData *adt)
Definition animdata.cc:349
bool assign_action(bAction *action, ID &animated_id)
static bAction * find_related_action(Main &bmain, ID &id)
Definition animdata.cc:179
const FCurve * fcurve_find_by_rna_path(const AnimData &adt, StringRefNull rna_path, int array_index)
Definition animdata.cc:297
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bAction * BKE_action_add(Main *bmain, const char name[])
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
Key * BKE_key_from_id(ID *id)
Definition key.cc:1291
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void BKE_fcurve_free(FCurve *fcu)
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
ID_Type GS(const char *name)
Definition DNA_ID.h:410
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2770
void DEG_relations_tag_update(Main *bmain)
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
void id_us_min(ID *id)
Definition lib_id.cc:441
eAnimData_Flag flag
ListBaseT< FCurve > drivers
ChannelDriver * driver
char name[258]
Definition DNA_ID.h:433
short flag
Definition DNA_ID.h:439
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< struct ParticleSystem > particlesystem