Blender V5.3
action_runtime.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
10
11#include "BKE_anim_data.hh"
12#include "BKE_global.hh"
13#include "BKE_lib_query.hh"
14#include "BKE_main.hh"
15#include "BKE_nla.hh"
16#include "BKE_node.hh"
17
18#include "BLI_listbase.hh"
19#include "BLI_set.hh"
20
21#include "ANIM_action.hh"
23
24#include "action_runtime.hh"
25
27
29{
30 /* Loop over all Actions and clear their slots' user cache. */
31 for (bAction &dna_action : bmain.actions) {
32 Action &action = dna_action.wrap();
33 for (Slot *slot : action.slots()) {
34 BLI_assert_msg(slot->runtime, "Slot::runtime should always be allocated");
35 slot->runtime->users.clear();
36 }
37 }
38
39 /* Mark all Slots as clear. This is a bit of a lie, because the code below still has to run.
40 * However, this is a necessity to make the `slot.users_add(*id)` call work without triggering
41 * an infinite recursion.
42 *
43 * The alternative would be to go around the `slot.users_add()` function and access the
44 * runtime directly, but this is IMO a bit cleaner. */
46
47 /* Visit any ID to see which Action+Slot it is using. Returns whether the ID
48 * was visited for the first time. */
49 Set<ID *> visited_ids;
50 auto visit_id = [&visited_ids](ID *id) -> bool {
51 BLI_assert(id);
52
53 if (!visited_ids.add(id)) {
54 return false;
55 }
56
57 foreach_action_slot_use(*id, [&](Action &action, slot_handle_t slot_handle) {
58 Slot *slot = action.slot_for_handle(slot_handle);
59 if (!slot) {
60 return true;
61 }
62 slot->users_add(*id);
63 return true;
64 });
65
66 return true;
67 };
68
69 /* Loop over all IDs to cache their slot usage. */
70 ListBaseT<ID> *ids_of_idtype;
71 ID *id;
72 FOREACH_MAIN_LISTBASE_BEGIN (&bmain, ids_of_idtype) {
73 /* Check whether this ID type can be animated. If not, just skip all IDs of this type. */
74 id = ids_of_idtype->first();
75 if (!id || !id_type_can_have_animdata(GS(id->name))) {
76 continue;
77 }
78
79 FOREACH_MAIN_LISTBASE_ID_BEGIN (ids_of_idtype, id) {
81
82 /* Process the ID itself. */
83 if (!visit_id(id)) {
84 continue;
85 }
86
87 /* Process embedded IDs, as these are not listed in bmain, but still can
88 * have their own Action+Slot. Unfortunately there is no generic looper
89 * for embedded IDs. At this moment the only animatable embedded ID is a
90 * node tree. */
91 bNodeTree *node_tree = bke::node_tree_from_id(id);
92 if (node_tree) {
93 visit_id(&node_tree->id);
94 }
95 }
97 }
99}
100
101} // namespace blender::animrig::internal
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:715
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:708
Internal C++ functions to deal with Actions, Slots, and their runtime data.
#define BLI_assert_msg(a, msg)
bool add(const Key &key)
Definition BLI_set.hh:248
Span< const Slot * > slots() const
Slot * slot_for_handle(slot_handle_t handle)
void users_add(ID &animated_id)
void rebuild_slot_user_cache(Main &bmain)
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)
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool id_type_can_have_animdata(short id_type)
Definition anim_data.cc:69
bool id_can_have_animdata(const ID *id)
Definition anim_data.cc:78
ID_Type GS(const char *name)
Definition DNA_ID.h:410
bool is_action_slot_to_id_map_dirty
Definition BKE_main.hh:342
ListBaseT< bAction > actions
Definition BKE_main.hh:408