Blender V5.3
asset_filter.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
10
11#include "BKE_idtype.hh"
12
13#include "BLI_listbase.hh"
14
15#include "DNA_asset_types.h"
16
18#include "AS_asset_library.hh"
19
20#include "ED_asset_filter.hh"
21#include "ED_asset_list.hh"
22
23namespace blender::ed::asset {
24
27{
28 ID_Type asset_type = asset.get_id_type();
29 uint64_t asset_id_filter = BKE_idtype_idcode_to_idfilter(asset_type);
30
31 if (filter->id_types && (filter->id_types & asset_id_filter) == 0) {
32 return false;
33 }
34 /* Not very efficient (O(n^2)), could be improved quite a bit. */
35 for (const AssetTag &filter_tag : filter->tags) {
36 AssetMetaData &asset_data = asset.get_metadata();
37
38 AssetTag *matched_tag = static_cast<AssetTag *>(
39 BLI_findstring(&asset_data.tags, filter_tag.name, offsetof(AssetTag, name)));
40 if (matched_tag == nullptr) {
41 return false;
42 }
43 }
44
45 /* Successfully passed through all filters. */
46 return true;
47}
48
51 const AssetLibraryReference &library_ref,
52 const FunctionRef<bool(const asset_system::AssetRepresentation &)> is_asset_visible_fn)
53{
54 Set<StringRef> known_paths;
55
56 /* Collect paths containing assets. */
58 if (!is_asset_visible_fn(asset)) {
59 return true;
60 }
61
62 const AssetMetaData &meta_data = asset.get_metadata();
63 if (BLI_uuid_is_nil(meta_data.catalog_id)) {
64 return true;
65 }
66
67 const asset_system::AssetCatalog *catalog = library.catalog_service().find_catalog(
68 meta_data.catalog_id);
69 if (catalog == nullptr) {
70 return true;
71 }
72 known_paths.add(catalog->path.str());
73 return true;
74 });
75
76 /* Build catalog tree. */
78 const std::shared_ptr<const asset_system::AssetCatalogTree> full_tree =
79 library.catalog_service().catalog_tree();
80 full_tree->foreach_item([&](const asset_system::AssetCatalogTreeItem &item) {
81 if (!known_paths.contains(item.catalog_path().str())) {
82 return;
83 }
84
85 asset_system::AssetCatalog *catalog = library.catalog_service().find_catalog(
86 item.get_catalog_id());
87 if (catalog == nullptr) {
88 return;
89 }
90 filtered_tree.insert_item(*catalog);
91 });
92
93 return filtered_tree;
94}
95
97 const asset_system::AssetCatalogPath &full_path, const std::optional<StringRef> skip_prefix)
98{
99 const bool has_skip_prefix = skip_prefix && full_path.str().starts_with(*skip_prefix) &&
100 full_path.str()[skip_prefix->size()] ==
102
103 return has_skip_prefix ? StringRef(full_path.str()).drop_prefix(skip_prefix->size() + 1) :
104 full_path;
105}
106
108 const AssetLibraryReference &library_ref,
109 const bContext &C,
110 const AssetFilterSettings &filter_settings,
111 const FunctionRef<bool(const AssetMetaData &)> meta_data_filter,
112 const std::optional<StringRef> skip_prefix)
113{
115 assets_per_path;
117
118 list::storage_fetch(&library_ref, &C);
120 if (!library) {
121 return {};
122 }
123
124 const bool loading_finished = list::is_loaded(&library_ref);
125 const bool dirty = !loading_finished;
126
128 if (!filter_matches_asset(&filter_settings, asset)) {
129 return true;
130 }
131 const AssetMetaData &meta_data = asset.get_metadata();
132 if (meta_data_filter && !meta_data_filter(meta_data)) {
133 return true;
134 }
135
136 if (BLI_uuid_is_nil(meta_data.catalog_id)) {
137 unassigned_assets.append(&asset);
138 return true;
139 }
140
141 const asset_system::AssetCatalog *catalog = library->catalog_service().find_catalog(
142 meta_data.catalog_id);
143 if (catalog == nullptr) {
144 /* Also include assets with catalogs we're unable to find (e.g. the catalog was deleted) in
145 * the "Unassigned" list. */
146 unassigned_assets.append(&asset);
147 return true;
148 }
149
151 skip_prefix);
152
153 if (catalog_path.str().empty() ||
154 catalog_path.str() == std::string{asset_system::AssetCatalogPath::SEPARATOR})
155 {
156 /* Also include assets with an empty catalog path in the "Unassigned" list. Mostly relevant
157 * when assets are directly placed under the skipped prefix path. */
158 unassigned_assets.append(&asset);
159 return true;
160 }
161
162 assets_per_path.add(catalog_path, &asset);
163 return true;
164 });
165
166 asset_system::AssetCatalogTree catalogs_with_node_assets;
167 const std::shared_ptr<const asset_system::AssetCatalogTree> catalog_tree =
168 library->catalog_service().catalog_tree();
169 catalog_tree->foreach_item([&](const asset_system::AssetCatalogTreeItem &item) {
171 item.catalog_path(), skip_prefix);
172
173 if (assets_per_path.lookup(catalog_path).is_empty()) {
174 return;
175 }
176 asset_system::AssetCatalog *catalog = library->catalog_service().find_catalog(
177 item.get_catalog_id());
178 if (catalog == nullptr) {
179 return;
180 }
181 catalogs_with_node_assets.insert_item(*catalog, skip_prefix);
182 });
183
184 return {std::move(catalogs_with_node_assets),
185 std::move(assets_per_path),
186 std::move(unassigned_assets),
187 dirty};
188}
189
190} // namespace blender::ed::asset
Main runtime representation of an asset.
#define C
Definition RandGen.cpp:29
Span< Value > lookup(const Key &key) const
void add(const Key &key, const Value &value)
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRef()
void append(const T &value)
#define offsetof(t, d)
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
void iterate(const AssetLibraryReference &library_reference, AssetListIterFn fn)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
bool is_loaded(const AssetLibraryReference *library_reference)
bool filter_matches_asset(const AssetFilterSettings *filter, const asset_system::AssetRepresentation &asset)
static asset_system::AssetCatalogPath catalog_path_skipped_prefix(const asset_system::AssetCatalogPath &full_path, const std::optional< StringRef > skip_prefix)
AssetItemTree build_filtered_all_catalog_tree(const AssetLibraryReference &library_ref, const bContext &C, const AssetFilterSettings &filter_settings, FunctionRef< bool(const AssetMetaData &)> meta_data_filter={}, const std::optional< StringRef > skip_prefix=std::nullopt)
asset_system::AssetCatalogTree build_filtered_catalog_tree(const asset_system::AssetLibrary &library, const AssetLibraryReference &library_ref, FunctionRef< bool(const asset_system::AssetRepresentation &)> is_asset_visible_fn)
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:473
unsigned __int64 uint64_t
Definition BKE_text.h:91
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:72
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
ListBaseT< AssetTag > tags
User defined tag. Currently only used by assets, could be used more often at some point....