Blender V5.3
tree_display_libraries.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 "BLI_listbase.hh"
11
12#include "BKE_collection.hh"
13#include "BKE_library.hh"
14#include "BKE_main.hh"
15
17#include "DNA_space_types.h"
18
19#include "BLT_translation.hh"
20
21#include "../outliner_intern.hh"
22#include "common.hh"
23#include "tree_display.hh"
26
27namespace blender::ed::outliner {
28
29template<typename T> using List = ListBaseWrapper<T>;
30
35
36ListBaseT<TreeElement> TreeDisplayLibraries::build_tree(const TreeSourceData &source_data)
37{
38 ListBaseT<TreeElement> tree = {nullptr};
39
40 {
41 /* current file first - mainvar provides tselem with unique pointer - not used */
42 TreeElement *ten = add_library_contents(*source_data.bmain, tree, nullptr);
43 TreeStoreElem *tselem;
44
45 if (ten) {
46 tselem = TREESTORE(ten);
47 if (!tselem->used) {
48 tselem->flag &= ~TSE_CLOSED;
49 }
50 }
51 }
52
53 for (ID *id : List<ID>(source_data.bmain->libraries)) {
54 Library *lib = reinterpret_cast<Library *>(id);
55 TreeElement *ten = add_library_contents(*source_data.bmain, tree, lib);
56 /* Null-check matters, due to filtering there may not be a new element. */
57 if (ten) {
58 lib->id.newid = reinterpret_cast<ID *>(ten);
59 }
60 }
61
62 /* Make hierarchy.
63 *
64 * Note: `List<T>` template is similar to non-mutable ListBaseT iteration so we need to
65 * iterate over an actual copy of the original list here, to avoid missing some items. */
67 if (ten == tree.first_) {
68 /* First item is main, skip. */
69 continue;
70 }
71
72 TreeStoreElem *tselem = TREESTORE(ten);
73 Library *lib = id_cast<Library *>(tselem->id);
74 BLI_assert(!lib || (GS(lib->id.name) == ID_LI));
75 if (!lib || !(lib->runtime->parent || lib->archive_parent_library)) {
76 continue;
77 }
78
79 /* A library with a non-null `parent` is always strictly indirectly linked. */
80 TreeElement *parent = reinterpret_cast<TreeElement *>(
82 ->id.newid);
83 BLI_remlink(&tree, ten);
84 BLI_addtail(&parent->subtree, ten);
85 ten->parent = parent;
86 }
87 /* restore newid pointers */
88 for (ID *library_id : List<ID>(source_data.bmain->libraries)) {
89 library_id->newid = nullptr;
90 }
91
92 return tree;
93}
94
95TreeElement *TreeDisplayLibraries::add_library_contents(Main &mainvar,
96 ListBaseT<TreeElement> &lb,
97 Library *lib)
98{
99 const short filter_id_type = id_filter_get();
100
101 Vector<ListBaseT<ID> *> lbarray;
102 if (filter_id_type) {
104 }
105 else {
106 lbarray.extend(BKE_main_lists_get(mainvar));
107 }
108
109 TreeElement *tenlib = nullptr;
110 for (int a = 0; a < lbarray.size(); a++) {
111 if (!lbarray[a] || !lbarray[a]->first()) {
112 continue;
113 }
114
115 ID *id = lbarray[a]->first();
116 const bool is_library = (GS(id->name) == ID_LI) && (lib != nullptr);
117
118 /* Don't show deprecated types. */
119 if (ID_TYPE_IS_DEPRECATED(GS(id->name))) {
120 continue;
121 }
122
123 /* check if there's data in current lib */
124 for (ID *id_iter : List<ID>(lbarray[a])) {
125 if (id_iter->lib == lib) {
126 id = id_iter;
127 break;
128 }
129 }
130
131 /* We always want to create an entry for libraries, even if/when we have no more IDs from them.
132 * This invalid state is important to show to user as well. */
133 if (id != nullptr || is_library) {
134 if (!tenlib) {
135 /* Create library tree element on demand, depending if there are any data-blocks. */
136 if (lib) {
137 tenlib = add_id_element({.lb = &lb}, reinterpret_cast<ID *>(lib));
138 }
139 else {
140 tenlib = add_element<TreeElementIDBase>({.lb = &lb, .persistent_ptr = &mainvar});
141 tenlib->name = IFACE_("Current File");
142 }
143 }
144
145 /* Create data-block list parent element on demand. */
146 if (id != nullptr) {
147 TreeElement *ten;
148
149 if (filter_id_type) {
150 ten = tenlib;
151 }
152 else if (id->lib == lib) {
154 {.lb = &tenlib->subtree, .index = a, .persistent_ptr = lib});
155 ten->name = outliner_idcode_to_plural(GS(id->name));
156 }
157
158 for (ID *id : List<ID>(lbarray[a])) {
159 if (library_id_filter_poll(lib, id)) {
160 /* Shape Key isn't treated as ID in outliner, see #TreeElementShapeKeyBase. */
161 if (GS(id->name) == ID_KE) {
162 add_element<TreeElementShapeKeyBase>({.parent = ten}, *reinterpret_cast<Key *>(id));
163 }
164 else {
165 add_id_element({.parent = ten}, id);
166 }
167 }
168 }
169 }
170 }
171 }
172
173 return tenlib;
174}
175
176short TreeDisplayLibraries::id_filter_get() const
177{
180 }
181 return 0;
182}
183
184bool TreeDisplayLibraries::library_id_filter_poll(const Library *lib, ID *id) const
185{
186 if (id->lib != lib) {
187 return false;
188 }
189
190 if (id_filter_get() == ID_GR) {
191 /* Don't show child collections of non-scene master collection,
192 * they are already shown as children. */
193 Collection *collection = id_cast<Collection *>(id);
194 bool has_non_scene_parent = false;
195
196 for (CollectionParent *cparent : List<CollectionParent>(collection->runtime->parents)) {
197 if (!(cparent->collection->flag & COLLECTION_IS_MASTER)) {
198 has_non_scene_parent = true;
199 }
200 }
201
202 if (has_non_scene_parent) {
203 return false;
204 }
205 }
206
207 return true;
208}
209
210} // namespace blender::ed::outliner
#define IFACE_(msgid)
#define ID_TYPE_IS_DEPRECATED(id_type)
Definition DNA_ID.h:755
Object groups, one object can be in many groups at once.
int64_t size() const
void append(const T &value)
void extend(Span< T > array)
const T & first() const
TreeElement * add_id_element(const TreeElementAddParams &params, ID *id)
AbstractTreeDisplay(SpaceOutliner &space_outliner)
TreeElement * add_element(const TreeElementAddParams &params, Args &&...args)
TreeDisplayLibraries(SpaceOutliner &space_outliner)
ListBaseT< TreeElement > build_tree(const TreeSourceData &source_data) override
static DynamicLibrary lib
const char * outliner_idcode_to_plural(short idcode)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
Vector< T * > listbase_to_vector(ListBase &list)
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ID_Type GS(const char *name)
Definition DNA_ID.h:410
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
#define TREESTORE(a)
char name[258]
Definition DNA_ID.h:433
struct ID * newid
Definition DNA_ID.h:419
struct Library * archive_parent_library
Definition DNA_ID.h:575
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
ListBaseT< Library > libraries
Definition BKE_main.hh:387
eSpaceOutliner_Filter filter
eTreeStoreElem_Flag flag
The data to build the tree from.
Establish and manage Outliner trees for different display modes.