Blender V4.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
10
11#include "BKE_idtype.hh"
12
13#include "BLI_listbase.h"
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 LISTBASE_FOREACH (const AssetTag *, filter_tag, &filter->tags) {
36 AssetMetaData &asset_data = asset.get_metadata();
37
38 AssetTag *matched_tag = (AssetTag *)BLI_findstring(
39 &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
50 const asset_system::AssetLibrary &library,
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. */
57 list::iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
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
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 asset_system::AssetCatalogTree &full_tree = library.catalog_service().catalog_tree();
79 full_tree.foreach_item([&](const asset_system::AssetCatalogTreeItem &item) {
80 if (!known_paths.contains(item.catalog_path().str())) {
81 return;
82 }
83
85 item.get_catalog_id());
86 if (catalog == nullptr) {
87 return;
88 }
89 filtered_tree.insert_item(*catalog);
90 });
91
92 return filtered_tree;
93}
94
96 const AssetLibraryReference &library_ref,
97 const bContext &C,
98 const AssetFilterSettings &filter_settings,
99 const FunctionRef<bool(const AssetMetaData &)> meta_data_filter)
100{
102 assets_per_path;
104
105 list::storage_fetch(&library_ref, &C);
107 if (!library) {
108 return {};
109 }
110
111 const bool loading_finished = list::is_loaded(&library_ref);
112 const bool dirty = !loading_finished;
113
114 list::iterate(library_ref, [&](asset_system::AssetRepresentation &asset) {
115 if (!filter_matches_asset(&filter_settings, asset)) {
116 return true;
117 }
118 const AssetMetaData &meta_data = asset.get_metadata();
119 if (meta_data_filter && !meta_data_filter(meta_data)) {
120 return true;
121 }
122
123 if (BLI_uuid_is_nil(meta_data.catalog_id)) {
124 unassigned_assets.append(&asset);
125 return true;
126 }
127
128 const asset_system::AssetCatalog *catalog = library->catalog_service().find_catalog(
129 meta_data.catalog_id);
130 if (catalog == nullptr) {
131 /* Also include assets with catalogs we're unable to find (e.g. the catalog was deleted) in
132 * the "Unassigned" list. */
133 unassigned_assets.append(&asset);
134 return true;
135 }
136 assets_per_path.add(catalog->path, &asset);
137 return true;
138 });
139
140 asset_system::AssetCatalogTree catalogs_with_node_assets;
141 const asset_system::AssetCatalogTree &catalog_tree = library->catalog_service().catalog_tree();
142 catalog_tree.foreach_item([&](const asset_system::AssetCatalogTreeItem &item) {
143 if (assets_per_path.lookup(item.catalog_path()).is_empty()) {
144 return;
145 }
147 item.get_catalog_id());
148 if (catalog == nullptr) {
149 return;
150 }
151 catalogs_with_node_assets.insert_item(*catalog);
152 });
153
154 return {std::move(catalogs_with_node_assets),
155 std::move(assets_per_path),
156 std::move(unassigned_assets),
157 dirty};
158}
159
160} // namespace blender::ed::asset
Main runtime representation of an asset.
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:369
void * BLI_findstring(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define LISTBASE_FOREACH(type, var, list)
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:79
ID_Type
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:291
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
AssetCatalog * find_catalog(CatalogID catalog_id) const
void foreach_item(ItemIterFn callback) const
AssetCatalogService & catalog_service() const
#define offsetof(t, d)
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
void iterate(const AssetLibraryReference &library_reference, AssetListHandleIterFn fn, FunctionRef< bool(asset_system::AssetRepresentation &)> prefilter_fn=nullptr)
bool is_loaded(const AssetLibraryReference *library_reference)
AssetItemTree build_filtered_all_catalog_tree(const AssetLibraryReference &library_ref, const bContext &C, const AssetFilterSettings &filter_settings, FunctionRef< bool(const AssetMetaData &)> meta_data_filter={})
asset_system::AssetCatalogTree build_filtered_catalog_tree(const asset_system::AssetLibrary &library, const AssetLibraryReference &library_ref, blender::FunctionRef< bool(const asset_system::AssetRepresentation &)> is_asset_visible_fn)
bool filter_matches_asset(const AssetFilterSettings *filter, const blender::asset_system::AssetRepresentation &asset)
unsigned __int64 uint64_t
Definition stdint.h:90
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
struct bUUID catalog_id
User defined tag. Currently only used by assets, could be used more often at some point....