Blender V4.5
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
8
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 "runtime_library.hh"
30#include "utils.hh"
31
32using namespace blender;
33using namespace blender::asset_system;
34
36
38{
39 /* NOTE: Can probably removed once #WITH_DESTROY_VIA_LOAD_HANDLER gets enabled by default. */
40
42}
43
45 const AssetLibraryReference &library_reference)
46{
48 return service->get_asset_library(bmain, library_reference);
49}
50
51AssetLibrary *AS_asset_library_load_from_directory(const char *name, const char *library_dirpath)
52{
53 /* NOTE: Loading an asset library at this point only means loading the catalogs.
54 * Later on this should invoke reading of asset representations too. */
55
58 if (library_dirpath == nullptr || library_dirpath[0] == '\0') {
60 }
61 else {
62 lib = service->get_asset_library_on_disk_custom(name, library_dirpath);
63 }
64 return lib;
65}
66
72
74 const AssetLibraryReference &library_reference)
75{
77}
78
80 const blender::StringRefNull input_path)
81{
83 &U, input_path.c_str()))
84 {
85 return preferences_lib->dirpath;
86 }
87
88 char buffer[FILE_MAXDIR];
89 BLI_path_split_dir_part(input_path.c_str(), buffer, FILE_MAXDIR);
90 return buffer;
91}
92
97
99{
102 [mappings](AssetLibrary &library) { library.remap_ids_and_remove_invalid(mappings); }, true);
103}
104
106 char r_path_buffer[1090 /* FILE_MAX_LIBEXTRA */],
107 char **r_dir,
108 char **r_group,
109 char **r_name)
110{
112 std::optional<AssetLibraryService::ExplodedPath> exploded =
113 service->resolve_asset_weak_reference_to_exploded_path(*asset_reference);
114
115 if (!exploded) {
116 if (r_dir) {
117 *r_dir = nullptr;
118 }
119 if (r_group) {
120 *r_group = nullptr;
121 }
122 if (r_name) {
123 *r_name = nullptr;
124 }
125 r_path_buffer[0] = '\0';
126 return;
127 }
128
129 BLI_assert(!exploded->group_component.is_empty());
130 BLI_assert(!exploded->name_component.is_empty());
131
132 BLI_strncpy(r_path_buffer, exploded->full_path->c_str(), 1090 /* #FILE_MAX_LIBEXTRA. */);
133
134 if (!exploded->dir_component.is_empty()) {
135 r_path_buffer[exploded->dir_component.size()] = '\0';
136 r_path_buffer[exploded->dir_component.size() + 1 + exploded->group_component.size()] = '\0';
137
138 if (r_dir) {
139 *r_dir = r_path_buffer;
140 }
141 if (r_group) {
142 *r_group = r_path_buffer + exploded->dir_component.size() + 1;
143 }
144 if (r_name) {
145 *r_name = r_path_buffer + exploded->dir_component.size() + 1 +
146 exploded->group_component.size() + 1;
147 }
148 }
149 else {
150 r_path_buffer[exploded->group_component.size()] = '\0';
151
152 if (r_dir) {
153 *r_dir = nullptr;
154 }
155 if (r_group) {
156 *r_group = r_path_buffer;
157 }
158 if (r_name) {
159 *r_name = r_path_buffer + exploded->group_component.size() + 1;
160 }
161 }
162}
163
164namespace blender::asset_system {
165
167 : library_type_(library_type),
168 name_(name),
169 root_path_(std::make_shared<std::string>(utils::normalize_directory_path(root_path))),
170 catalog_service_(std::make_unique<AssetCatalogService>(*root_path_))
171{
172}
173
180
182 const bool include_all_library)
183{
185 service->foreach_loaded_asset_library(fn, include_all_library);
186}
187
189{
190 {
191 std::lock_guard lock{catalog_service_mutex_};
192 /* Should never actually be the case, catalog service gets allocated with the asset library. */
193 if (catalog_service_ == nullptr) {
194 auto catalog_service = std::make_unique<AssetCatalogService>(root_path());
195 catalog_service->load_from_disk();
197 return;
198 }
199 }
200
201 /* The catalog service was created before without being associated with a definition file. */
202 if (catalog_service_->get_catalog_definition_file() == nullptr) {
203 catalog_service_->load_from_disk();
204 }
205 else {
206 this->refresh_catalogs();
207 }
208}
209
214
216
217std::weak_ptr<AssetRepresentation> AssetLibrary::add_external_asset(
218 StringRef relative_asset_path,
220 const int id_type,
221 std::unique_ptr<AssetMetaData> metadata)
222{
223 return asset_storage_.external_assets.lookup_key_or_add(std::make_shared<AssetRepresentation>(
224 relative_asset_path, name, id_type, std::move(metadata), *this));
225}
226
227std::weak_ptr<AssetRepresentation> AssetLibrary::add_local_id_asset(StringRef relative_asset_path,
228 ID &id)
229{
230 return asset_storage_.local_id_assets.lookup_key_or_add(
231 std::make_shared<AssetRepresentation>(relative_asset_path, id, *this));
232}
233
235{
236 /* Make sure this is forwarded to the library actually owning the asset if needed. For example
237 * the "All Libraries" library doesn't own the assets itself. */
238 if (&asset.owner_asset_library_ != this) {
239 return asset.owner_asset_library_.remove_asset(asset);
240 }
241
242 BLI_assert(asset_storage_.local_id_assets.contains_as(&asset) ||
243 asset_storage_.external_assets.contains_as(&asset));
244
245 if (asset_storage_.local_id_assets.remove_as(&asset)) {
246 return true;
247 }
248 return asset_storage_.external_assets.remove_as(&asset);
249}
250
252{
253 Set<AssetRepresentation *> removed_assets;
254
255 for (const auto &asset_ptr : asset_storage_.local_id_assets) {
256 AssetRepresentation &asset = *asset_ptr;
257 BLI_assert(asset.is_local_id());
258
259 const IDRemapperApplyResult result = mappings.apply(&std::get<ID *>(asset.asset_),
261
262 /* Entirely remove assets whose ID is unset. We don't want assets with a null ID pointer. */
264 removed_assets.add(&asset);
265 }
266 }
267
268 for (AssetRepresentation *asset : removed_assets) {
269 this->remove_asset(*asset);
270 }
271}
272
273namespace {
274void asset_library_on_save_post(Main *bmain,
275 PointerRNA **pointers,
276 const int num_pointers,
277 void *arg)
278{
279 AssetLibrary *asset_lib = static_cast<AssetLibrary *>(arg);
280
281 /* Transform 'runtime' current file library into 'on-disk' current file library. */
282 if (asset_lib->library_type() == ASSET_LIBRARY_LOCAL && asset_lib->root_path().is_empty()) {
283 BLI_assert(dynamic_cast<RuntimeAssetLibrary *>(asset_lib) != nullptr);
284
285 if (AssetLibrary *on_disk_lib =
287 {
288 /* Allow undoing to the state before merging in catalogs from disk. */
289 on_disk_lib->catalog_service().undo_push();
290
291 /* Force refresh to merge on-disk catalogs with the ones stolen from the runtime library. */
293 ASSET_LIBRARY_LOCAL, on_disk_lib->root_path());
294 BLI_assert(asset_lib == on_disk_lib);
295 }
296 }
297
298 asset_lib->on_blend_save_post(bmain, pointers, num_pointers);
299}
300
301} // namespace
302
304{
305 /* The callback system doesn't own `on_save_callback_store_`. */
306 on_save_callback_store_.alloc = false;
307
308 on_save_callback_store_.func = asset_library_on_save_post;
309 on_save_callback_store_.arg = this;
310
312}
313
320
322 PointerRNA ** /*pointers*/,
323 const int /*num_pointers*/)
324{
326 this->catalog_service().write_to_disk(bmain->filepath);
327 }
328}
329
331 const AssetWeakReference &asset_reference)
332{
334 return service->resolve_asset_weak_reference_to_full_path(asset_reference);
335}
336
338{
339 if (BLI_uuid_is_nil(asset_data->catalog_id)) {
340 asset_data->catalog_simple_name[0] = '\0';
341 return;
342 }
343 const AssetCatalog *catalog = this->catalog_service().find_catalog(asset_data->catalog_id);
344 if (catalog == nullptr) {
345 /* No-op if the catalog cannot be found. This could be the kind of "the catalog definition file
346 * is corrupt/lost" scenario that the simple name is meant to help recover from. */
347 return;
348 }
349 STRNCPY(asset_data->catalog_simple_name, catalog->simple_name.c_str());
350}
351
353{
354 return library_type_;
355}
356
358{
359 return name_;
360}
361
363{
364 return *root_path_;
365}
366
368{
370 {
371 AssetLibraryReference library_ref{};
372 library_ref.custom_library_index = -1;
373 library_ref.type = ASSET_LIBRARY_ESSENTIALS;
374 result.append(library_ref);
375 }
376 int i;
377 LISTBASE_FOREACH_INDEX (const bUserAssetLibrary *, asset_library, &U.asset_libraries, i) {
378 if (!BLI_is_dir(asset_library->dirpath)) {
379 continue;
380 }
381 AssetLibraryReference library_ref{};
382 library_ref.custom_library_index = i;
383 library_ref.type = ASSET_LIBRARY_CUSTOM;
384 result.append(library_ref);
385 }
386
387 AssetLibraryReference library_ref{};
388 library_ref.custom_library_index = -1;
389 library_ref.type = ASSET_LIBRARY_LOCAL;
390 result.append(library_ref);
391 return result;
392}
393
395{
396 AssetLibraryReference all_library_ref{};
397 all_library_ref.custom_library_index = -1;
398 all_library_ref.type = ASSET_LIBRARY_ALL;
399 return all_library_ref;
400}
401
403{
404 AssetLibraryReference library_ref{};
405 library_ref.custom_library_index = -1;
406 library_ref.type = ASSET_LIBRARY_LOCAL;
407 return library_ref;
408}
409
415
416} // namespace blender::asset_system
Main runtime representation of an asset.
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:74
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition callbacks.cc:81
@ 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:46
File and directory operations.
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:456
#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
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
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)
AssetLibrary * AS_asset_library_load_from_directory(const char *name, const char *library_dirpath)
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
#define U
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr bool is_empty() const
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_on_disk_builtin(eAssetLibraryType type, StringRefNull root_path)
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)
static AssetLibrary * move_runtime_current_file_into_on_disk_library(const Main &bmain)
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()
AssetLibraryReference current_file_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:404
char filepath[1024]
Definition BKE_main.hh:155
i
Definition text_draw.cc:230
static DynamicLibrary lib