Blender V5.3
tree_display_override_library_hierarchies.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 "DNA_space_types.h"
10
11#include "BLI_function_ref.hh"
12#include "BLI_ghash.hh"
13#include "BLI_listbase.hh"
14#include "BLI_map.hh"
15
16#include "BLI_set.hh"
17
18#include "BLT_translation.hh"
19
20#include "BKE_lib_override.hh"
21#include "BKE_lib_query.hh"
22#include "BKE_main.hh"
23
24#include "../outliner_intern.hh"
25#include "common.hh"
26#include "tree_display.hh"
29
30namespace blender::ed::outliner {
31
33
39
41 const TreeSourceData &source_data)
42{
43 ListBaseT<TreeElement> tree = {nullptr};
44
45 /* First step: Build "Current File" hierarchy. */
47 {.lb = &tree, .index = -1, .persistent_ptr = source_data.bmain});
48 current_file_te->name = IFACE_("Current File");
50 {
51 build_hierarchy_for_lib_or_main(source_data.bmain, *current_file_te);
52
53 /* Add dummy child if there's nothing to display. */
54 if (current_file_te->subtree.is_empty()) {
55 TreeElement *dummy_te = add_element<TreeElementIDBase>({.parent = current_file_te});
56 dummy_te->name = IFACE_("No Library Overrides");
57 }
58 }
59
60 /* Second step: Build hierarchies for external libraries. */
61 for (Library *lib = source_data.bmain->libraries.first(); lib;
62 lib = static_cast<Library *>(lib->id.next))
63 {
64 TreeElement *tenlib = add_id_element({.lb = &tree}, reinterpret_cast<ID *>(lib));
65 build_hierarchy_for_lib_or_main(source_data.bmain, *tenlib, lib);
66 }
67
68 /* Remove top level library elements again that don't contain any overrides. */
69 for (TreeElement &top_level_te : tree.items_mutable()) {
70 if (&top_level_te == current_file_te) {
71 continue;
72 }
73
74 if (top_level_te.subtree.is_empty()) {
75 outliner_free_tree_element(&top_level_te, &tree);
76 }
77 }
78
79 return tree;
80}
81
83{
84 return true;
85}
86
87/* -------------------------------------------------------------------- */
90
92 AbstractTreeDisplay &tree_display_;
93 SpaceOutliner &space_outliner_;
94 Main &bmain_;
95 MainIDRelations &id_relations_;
96
97 struct HierarchyBuildData {
98 const ID &override_root_id_;
99 /* The ancestor IDs leading to the current ID, to avoid IDs recursing into themselves. Changes
100 * with every level of recursion. */
101 Set<const ID *> parent_ids;
102 /* The IDs that were already added to #parent_te, to avoid duplicates. Entirely new set with
103 * every level of recursion. */
104 Set<const ID *> sibling_ids;
105 };
106
107 public:
109 SpaceOutliner &space_outliner,
110 Main &bmain,
111 MainIDRelations &id_relations)
112 : tree_display_(tree_display),
113 space_outliner_(space_outliner),
114 bmain_(bmain),
115 id_relations_(id_relations)
116 {
117 }
118
119 void build_hierarchy_for_ID(ID &root_id, TreeElement &te_to_expand);
120
121 private:
122 void build_hierarchy_for_ID_recursive(const ID &parent_id,
123 HierarchyBuildData &build_data,
124 TreeElement &te_to_expand);
125};
126
127ListBaseT<TreeElement> TreeDisplayOverrideLibraryHierarchies::build_hierarchy_for_lib_or_main(
128 Main *bmain, TreeElement &parent_te, Library *lib)
129{
130 ListBaseT<TreeElement> tree = {nullptr};
131
132 /* Ensure #Main.relations contains the latest mapping of relations. Must be freed before
133 * returning. */
135
136 OverrideIDHierarchyBuilder builder(*this, space_outliner_, *bmain, *bmain->relations);
137
138 /* Keep track over which ID base elements were already added, and expand them once added. */
139 Map<ID_Type, TreeElement *> id_base_te_map;
140 /* Index for the ID base elements ("Objects", "Materials", etc). */
141 int base_index = 0;
142
143 ID *iter_id;
144 FOREACH_MAIN_ID_BEGIN (bmain, iter_id) {
146 continue;
147 }
148 if (iter_id->lib != lib) {
149 continue;
150 }
151
152 TreeElement *new_base_te = id_base_te_map.lookup_or_add_cb(GS(iter_id->name), [&]() {
153 TreeElement *new_te = add_element<TreeElementIDBase>(
154 {.parent = &parent_te,
155 .index = base_index++,
156 .owner_id = reinterpret_cast<ID *>(lib),
157 .persistent_ptr = bmain});
158 new_te->name = outliner_idcode_to_plural(GS(iter_id->name));
159 return new_te;
160 });
161
162 TreeElement *new_id_te = add_id_element({.parent = new_base_te, .expand = false}, iter_id);
163
164 builder.build_hierarchy_for_ID(*iter_id, *new_id_te);
165 }
167
169
170 return tree;
171}
172
174 TreeElement &te_to_expand)
175{
176 HierarchyBuildData build_data{override_root_id};
177 build_hierarchy_for_ID_recursive(override_root_id, build_data, te_to_expand);
178}
179
184/* Helpers (defined below). */
185static void foreach_natural_hierarchy_child(const MainIDRelations &id_relations,
186 const ID &parent_id,
188static bool id_is_in_override_hierarchy(const Main &bmain,
189 const ID &id,
190 const ID &relationship_parent_id,
191 const ID &override_root_id);
192
193void OverrideIDHierarchyBuilder::build_hierarchy_for_ID_recursive(const ID &parent_id,
194 HierarchyBuildData &build_data,
195 TreeElement &te_to_expand)
196{
197 /* In case this isn't added to the parents yet (does nothing if already there). */
198 build_data.parent_ids.add(&parent_id);
199
200 foreach_natural_hierarchy_child(id_relations_, parent_id, [&](ID &id) {
201 /* Some IDs can use themselves, early abort. */
202 if (&id == &parent_id) {
203 return FOREACH_CONTINUE;
204 }
205 if (!id_is_in_override_hierarchy(bmain_, id, parent_id, build_data.override_root_id_)) {
206 return FOREACH_CONTINUE;
207 }
208
209 /* Avoid endless recursion: If there is an ancestor for this ID already, it recurses into
210 * itself. */
211 if (build_data.parent_ids.lookup_key_default(&id, nullptr)) {
212 return FOREACH_CONTINUE;
213 }
214
215 /* Avoid duplicates: If there is a sibling for this ID already, the same ID is just used
216 * multiple times by the same parent. */
217 if (build_data.sibling_ids.lookup_key_default(&id, nullptr)) {
218 return FOREACH_CONTINUE;
219 }
220
221 /* We only want to add children whose parent isn't collapsed. Otherwise, in complex scenes with
222 * thousands of relationships, the building can slow down tremendously. Tag the parent to allow
223 * un-collapsing, but don't actually add the children. */
224 if (!TSELEM_OPEN(TREESTORE(&te_to_expand), &space_outliner_)) {
225 te_to_expand.flag |= TE_PRETEND_HAS_CHILDREN;
226 return FOREACH_BREAK;
227 }
228
229 /* Shape Key isn't treated as ID in outliner, see #TreeElementShapeKeyBase. */
230 TreeElement *new_te;
231 if (GS(id.name) == ID_KE) {
232 new_te = tree_display_.add_element<TreeElementShapeKeyBase>(
233 {.parent = &te_to_expand, .expand = false}, *reinterpret_cast<Key *>(&id));
234 }
235 else {
236 new_te = tree_display_.add_id_element({.parent = &te_to_expand, .expand = false}, &id);
237 }
238
239 build_data.sibling_ids.add(&id);
240
241 /* Recurse into this ID. */
242 HierarchyBuildData child_build_data{build_data.override_root_id_};
243 child_build_data.parent_ids = build_data.parent_ids;
244 child_build_data.parent_ids.add(&id);
245 child_build_data.sibling_ids.reserve(10);
246 build_hierarchy_for_ID_recursive(id, child_build_data, *new_te);
247
248 return FOREACH_CONTINUE;
249 });
250}
251
253
254/* -------------------------------------------------------------------- */
257
272static void foreach_natural_hierarchy_child(const MainIDRelations &id_relations,
273 const ID &parent_id,
275{
276 const MainIDRelationsEntry *relations_of_id = id_relations.relations_from_pointers->lookup(
277 &parent_id);
278
279 /* Iterate over all IDs used by the parent ID (e.g. the child-collections of a collection). */
280 for (MainIDRelationsEntryItem *to_id_entry = relations_of_id->to_ids; to_id_entry;
281 to_id_entry = to_id_entry->next)
282 {
283 /* An ID pointed to (used) by the ID to recurse into. */
284 ID &target_id = *to_id_entry->id_pointer.to;
285
286 /* Don't walk up the hierarchy, e.g. ignore pointers to parent collections. */
287 if (to_id_entry->usage_flag & IDWALK_CB_LOOPBACK) {
288 continue;
289 }
290
291 /* Special case for objects: Process the parent object instead of the child object. Below the
292 * parent will add the child objects then. */
293 if (GS(target_id.name) == ID_OB) {
294 const Object &potential_child_ob = reinterpret_cast<const Object &>(target_id);
295 if (potential_child_ob.parent) {
296 if (fn(potential_child_ob.parent->id) == FOREACH_BREAK) {
297 return;
298 }
299 continue;
300 }
301 }
302
303 if (fn(target_id) == FOREACH_BREAK) {
304 return;
305 }
306 }
307
308 /* If the ID is an object, find and iterate over any child objects. */
309 if (GS(parent_id.name) == ID_OB) {
310 for (MainIDRelationsEntryItem *from_id_entry = relations_of_id->from_ids; from_id_entry;
311 from_id_entry = from_id_entry->next)
312 {
313 ID &potential_child_id = *from_id_entry->id_pointer.from;
314
315 if (GS(potential_child_id.name) != ID_OB) {
316 continue;
317 }
318
319 const Object &potential_child_ob = reinterpret_cast<Object &>(potential_child_id);
320 if (!potential_child_ob.parent || &potential_child_ob.parent->id != &parent_id) {
321 continue;
322 }
323
324 if (fn(potential_child_id) == FOREACH_BREAK) {
325 return;
326 }
327 }
328 }
329}
330
331static bool id_is_in_override_hierarchy(const Main &bmain,
332 const ID &id,
333 const ID &relationship_parent_id,
334 const ID &override_root_id)
335{
336 /* If #id is an embedded ID, this will be set to the owner, which is a real ID and contains the
337 * override data. So queries of override data should be done via this, but the actual tree
338 * element we add is the embedded ID. */
339 const ID *real_override_id = &id;
340
342 /* In many cases, `relationship_parent_id` is the owner, but not always (e.g. there can be
343 * drivers directly between an object and a shape-key). */
344 BKE_lib_override_library_get(const_cast<Main *>(&bmain),
345 const_cast<ID *>(&id),
346 const_cast<ID *>(&relationship_parent_id),
347 const_cast<ID **>(&real_override_id));
348 }
349
350 if (!ID_IS_OVERRIDE_LIBRARY(real_override_id)) {
351 return false;
352 }
353 /* Is this ID part of the same override hierarchy? */
354 if (real_override_id->override_library->hierarchy_root != &override_root_id) {
355 return false;
356 }
357
358 return true;
359}
360
362
363} // namespace blender::ed::outliner
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define IFACE_(msgid)
#define ID_IS_OVERRIDE_LIBRARY_VIRTUAL(_id)
Definition DNA_ID.h:733
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define ID_IS_OVERRIDE_LIBRARY_HIERARCHY_ROOT(_id)
Definition DNA_ID.h:739
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
TreeElement * add_id_element(const TreeElementAddParams &params, ID *id)
AbstractTreeDisplay(SpaceOutliner &space_outliner)
TreeElement * add_element(const TreeElementAddParams &params, Args &&...args)
static void uncollapse_by_default(TreeElement *legacy_te)
OverrideIDHierarchyBuilder(AbstractTreeDisplay &tree_display, SpaceOutliner &space_outliner, Main &bmain, MainIDRelations &id_relations)
ListBaseT< TreeElement > build_tree(const TreeSourceData &source_data) override
static DynamicLibrary lib
const char * outliner_idcode_to_plural(short idcode)
static void foreach_natural_hierarchy_child(const MainIDRelations &id_relations, const ID &parent_id, FunctionRef< ForeachChildReturn(ID &)> fn)
static bool id_is_in_override_hierarchy(const Main &bmain, const ID &id, const ID &relationship_parent_id, const ID &override_root_id)
void outliner_free_tree_element(TreeElement *element, ListBaseT< TreeElement > *parent_subtree)
void BKE_main_relations_free(Main *bmain)
Definition main.cc:760
void BKE_main_relations_create(Main *bmain, short flag)
Definition main.cc:723
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ IDWALK_CB_LOOPBACK
IDOverrideLibrary * BKE_lib_override_library_get(Main *bmain, ID *id, ID *owner_id_hint, ID **r_owner_id)
#define TREESTORE(a)
#define TSELEM_OPEN(telm, sv)
struct ID * hierarchy_root
Definition DNA_ID.h:352
char name[258]
Definition DNA_ID.h:433
struct Library * lib
Definition DNA_ID.h:421
IDOverrideLibrary * override_library
Definition DNA_ID.h:495
MainIDRelationsEntryItem * next
Definition BKE_main.hh:139
MainIDRelationsEntryItem * from_ids
Definition BKE_main.hh:156
MainIDRelationsEntryItem * to_ids
Definition BKE_main.hh:158
Map< const ID *, MainIDRelationsEntry * > * relations_from_pointers
Definition BKE_main.hh:211
MainIDRelations * relations
Definition BKE_main.hh:437
ListBaseT< Library > libraries
Definition BKE_main.hh:387
struct Object * parent
The data to build the tree from.
Establish and manage Outliner trees for different display modes.