Blender V4.3
asset_library.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
9#include <memory>
10
12#include "AS_asset_library.hh"
14
15#include "BKE_lib_remap.hh"
16#include "BKE_main.hh"
17#include "BKE_preferences.h"
18
19#include "BLI_fileops.h"
20#include "BLI_listbase.h"
21#include "BLI_path_utils.hh"
22#include "BLI_string.h"
23
24#include "DNA_userdef_types.h"
25
29#include "utils.hh"
30
31using namespace blender;
32using namespace blender::asset_system;
33
35
37{
38 /* NOTE: Can probably removed once #WITH_DESTROY_VIA_LOAD_HANDLER gets enabled by default. */
39
41}
42
44 const AssetLibraryReference &library_reference)
45{
47 return service->get_asset_library(bmain, library_reference);
48}
49
50AssetLibrary *AS_asset_library_load(const char *name, const char *library_dirpath)
51{
52 /* NOTE: Loading an asset library at this point only means loading the catalogs.
53 * Later on this should invoke reading of asset representations too. */
54
57 if (library_dirpath == nullptr || library_dirpath[0] == '\0') {
59 }
60 else {
61 lib = service->get_asset_library_on_disk_custom(name, library_dirpath);
62 }
63 return lib;
64}
65
71
73 const AssetLibraryReference &library_reference)
74{
76}
77
79 const blender::StringRefNull input_path)
80{
82 &U, input_path.c_str()))
83 {
84 return preferences_lib->dirpath;
85 }
86
87 char buffer[FILE_MAXDIR];
88 BLI_path_split_dir_part(input_path.c_str(), buffer, FILE_MAXDIR);
89 return buffer;
90}
91
96
98{
101 [mappings](AssetLibrary &library) { library.remap_ids_and_remove_invalid(mappings); }, true);
102}
103
105 char r_path_buffer[1090 /* FILE_MAX_LIBEXTRA */],
106 char **r_dir,
107 char **r_group,
108 char **r_name)
109{
111 std::optional<AssetLibraryService::ExplodedPath> exploded =
112 service->resolve_asset_weak_reference_to_exploded_path(*asset_reference);
113
114 if (!exploded) {
115 if (r_dir) {
116 *r_dir = nullptr;
117 }
118 if (r_group) {
119 *r_group = nullptr;
120 }
121 if (r_name) {
122 *r_name = nullptr;
123 }
124 r_path_buffer[0] = '\0';
125 return;
126 }
127
128 BLI_assert(!exploded->group_component.is_empty());
129 BLI_assert(!exploded->name_component.is_empty());
130
131 BLI_strncpy(r_path_buffer, exploded->full_path->c_str(), 1090 /* #FILE_MAX_LIBEXTRA. */);
132
133 if (!exploded->dir_component.is_empty()) {
134 r_path_buffer[exploded->dir_component.size()] = '\0';
135 r_path_buffer[exploded->dir_component.size() + 1 + exploded->group_component.size()] = '\0';
136
137 if (r_dir) {
138 *r_dir = r_path_buffer;
139 }
140 if (r_group) {
141 *r_group = r_path_buffer + exploded->dir_component.size() + 1;
142 }
143 if (r_name) {
144 *r_name = r_path_buffer + exploded->dir_component.size() + 1 +
145 exploded->group_component.size() + 1;
146 }
147 }
148 else {
149 r_path_buffer[exploded->group_component.size()] = '\0';
150
151 if (r_dir) {
152 *r_dir = nullptr;
153 }
154 if (r_group) {
155 *r_group = r_path_buffer;
156 }
157 if (r_name) {
158 *r_name = r_path_buffer + exploded->group_component.size() + 1;
159 }
160 }
161}
162
163namespace blender::asset_system {
164
166 : library_type_(library_type),
167 name_(name),
168 root_path_(std::make_shared<std::string>(utils::normalize_directory_path(root_path))),
169 catalog_service_(std::make_unique<AssetCatalogService>())
170{
171}
172
179
181 const bool include_all_library)
182{
184 service->foreach_loaded_asset_library(fn, include_all_library);
185}
186
188{
189 auto catalog_service = std::make_unique<AssetCatalogService>(root_path());
191 std::lock_guard lock{catalog_service_mutex_};
193}
194
199
201
202std::weak_ptr<AssetRepresentation> AssetLibrary::add_external_asset(
203 StringRef relative_asset_path,
204 StringRef name,
205 const int id_type,
206 std::unique_ptr<AssetMetaData> metadata)
207{
208 return asset_storage_.external_assets.lookup_key_or_add(std::make_shared<AssetRepresentation>(
209 relative_asset_path, name, id_type, std::move(metadata), *this));
210}
211
212std::weak_ptr<AssetRepresentation> AssetLibrary::add_local_id_asset(StringRef relative_asset_path,
213 ID &id)
214{
215 return asset_storage_.local_id_assets.lookup_key_or_add(
216 std::make_shared<AssetRepresentation>(relative_asset_path, id, *this));
217}
218
220{
221 if (asset_storage_.local_id_assets.remove_as(&asset)) {
222 return true;
223 }
224 return asset_storage_.external_assets.remove_as(&asset);
225}
226
228{
229 Set<AssetRepresentation *> removed_assets;
230
231 for (auto &asset_ptr : asset_storage_.local_id_assets) {
232 AssetRepresentation &asset = *asset_ptr;
233 BLI_assert(asset.is_local_id());
234
235 const IDRemapperApplyResult result = mappings.apply(&std::get<ID *>(asset.asset_),
237
238 /* Entirely remove assets whose ID is unset. We don't want assets with a null ID pointer. */
239 if (result == ID_REMAP_RESULT_SOURCE_UNASSIGNED) {
240 removed_assets.add(&asset);
241 }
242 }
243
244 for (AssetRepresentation *asset : removed_assets) {
245 this->remove_asset(*asset);
246 }
247}
248
249namespace {
250void asset_library_on_save_post(Main *bmain,
251 PointerRNA **pointers,
252 const int num_pointers,
253 void *arg)
254{
255 AssetLibrary *asset_lib = static_cast<AssetLibrary *>(arg);
256 asset_lib->on_blend_save_post(bmain, pointers, num_pointers);
257}
258
259} // namespace
260
262{
263 /* The callback system doesn't own `on_save_callback_store_`. */
265
266 on_save_callback_store_.func = asset_library_on_save_post;
268
270}
271
278
280 PointerRNA ** /*pointers*/,
281 const int /*num_pointers*/)
282{
284 this->catalog_service().write_to_disk(bmain->filepath);
285 }
286}
287
289 const AssetWeakReference &asset_reference)
290{
292 return service->resolve_asset_weak_reference_to_full_path(asset_reference);
293}
294
296{
297 if (BLI_uuid_is_nil(asset_data->catalog_id)) {
298 asset_data->catalog_simple_name[0] = '\0';
299 return;
300 }
301 const AssetCatalog *catalog = this->catalog_service().find_catalog(asset_data->catalog_id);
302 if (catalog == nullptr) {
303 /* No-op if the catalog cannot be found. This could be the kind of "the catalog definition file
304 * is corrupt/lost" scenario that the simple name is meant to help recover from. */
305 return;
306 }
307 STRNCPY(asset_data->catalog_simple_name, catalog->simple_name.c_str());
308}
309
311{
312 return library_type_;
313}
314
316{
317 return name_;
318}
319
321{
322 return *root_path_;
323}
324
326{
328 {
329 AssetLibraryReference library_ref{};
330 library_ref.custom_library_index = -1;
331 library_ref.type = ASSET_LIBRARY_ESSENTIALS;
332 result.append(library_ref);
333 }
334 int i;
335 LISTBASE_FOREACH_INDEX (const bUserAssetLibrary *, asset_library, &U.asset_libraries, i) {
336 if (!BLI_is_dir(asset_library->dirpath)) {
337 continue;
338 }
339 AssetLibraryReference library_ref{};
340 library_ref.custom_library_index = i;
341 library_ref.type = ASSET_LIBRARY_CUSTOM;
342 result.append(library_ref);
343 }
344
345 AssetLibraryReference library_ref{};
346 library_ref.custom_library_index = -1;
347 library_ref.type = ASSET_LIBRARY_LOCAL;
348 result.append(library_ref);
349 return result;
350}
351
353{
354 AssetLibraryReference all_library_ref{};
355 all_library_ref.custom_library_index = -1;
356 all_library_ref.type = ASSET_LIBRARY_ALL;
357 return all_library_ref;
358}
359
365
366} // namespace blender::asset_system
Main runtime representation of an asset.
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:75
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:82
@ BKE_CB_EVT_SAVE_POST
IDRemapperApplyResult
@ ID_REMAP_RESULT_SOURCE_UNASSIGNED
@ ID_REMAP_APPLY_DEFAULT
struct bUserAssetLibrary * BKE_preferences_asset_library_containing_path(const struct UserDef *userdef, const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
#define BLI_assert(a)
Definition BLI_assert.h:50
File and directory operations.
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:433
#define LISTBASE_FOREACH_INDEX(type, var, list, index_var)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
#define FILE_MAXDIR
#define STRNCPY(dst, src)
Definition BLI_string.h:593
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:79
eAssetLibraryType
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
@ ASSET_LIBRARY_ALL
void AS_asset_library_remap_ids(const bke::id::IDRemapper &mappings)
std::string AS_asset_library_find_suitable_root_path_from_main(const Main *bmain)
void AS_asset_libraries_exit()
AssetLibrary * AS_asset_library_load(const Main *bmain, const AssetLibraryReference &library_reference)
std::string AS_asset_library_find_suitable_root_path_from_path(const blender::StringRefNull input_path)
std::string AS_asset_library_root_path_from_library_ref(const AssetLibraryReference &library_reference)
bool AS_asset_library_has_any_unsaved_catalogs()
void AS_asset_full_path_explode_from_weak_ref(const AssetWeakReference *asset_reference, char r_path_buffer[1090], char **r_dir, char **r_group, char **r_name)
volatile int lock
unsigned int U
Definition btGjkEpa3.h:78
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr const char * c_str() const
AssetCatalog * find_catalog(CatalogID catalog_id) const
bool write_to_disk(const CatalogFilePath &blend_file_path)
void foreach_loaded_asset_library(FunctionRef< void(AssetLibrary &)> fn, bool include_all_library) const
AssetLibrary * get_asset_library(const Main *bmain, const AssetLibraryReference &library_reference)
std::optional< ExplodedPath > resolve_asset_weak_reference_to_exploded_path(const AssetWeakReference &asset_reference)
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
AssetLibrary * get_asset_library_on_disk_custom(StringRef name, StringRefNull root_path)
static std::string root_path_from_library_ref(const AssetLibraryReference &library_reference)
void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers)
eAssetLibraryType library_type() const
std::unique_ptr< AssetCatalogService > catalog_service_
void remap_ids_and_remove_invalid(const blender::bke::id::IDRemapper &mappings)
std::weak_ptr< AssetRepresentation > add_local_id_asset(StringRef relative_asset_path, ID &id)
AssetCatalogService & catalog_service() const
static void foreach_loaded(FunctionRef< void(AssetLibrary &)> fn, bool include_all_library)
AssetLibrary(eAssetLibraryType library_type, StringRef name="", StringRef root_path="")
bool remove_asset(AssetRepresentation &asset)
void refresh_catalog_simplename(AssetMetaData *asset_data)
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
std::weak_ptr< AssetRepresentation > add_external_asset(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata)
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
AssetLibraryReference all_library_reference()
void all_library_reload_catalogs_if_dirty()
Vector< AssetLibraryReference > all_valid_asset_library_refs()
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
char catalog_simple_name[64]
struct bUUID catalog_id
Definition DNA_ID.h:413
char filepath[1024]
Definition BKE_main.hh:136
void(* func)(Main *, PointerRNA **, int num_pointers, void *arg)
static DynamicLibrary lib