diff options
Diffstat (limited to 'source/blender/blenkernel/intern')
5 files changed, 106 insertions, 1 deletions
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc index 38c712d7e75..62d03b2d79b 100644 --- a/source/blender/blenkernel/intern/asset_catalog.cc +++ b/source/blender/blenkernel/intern/asset_catalog.cc @@ -9,6 +9,7 @@ #include "BKE_asset_catalog.hh" #include "BKE_asset_library.h" +#include "BKE_asset_library.hh" #include "BLI_fileops.hh" #include "BLI_path_util.h" @@ -787,6 +788,41 @@ void AssetCatalogTree::foreach_root_item(const ItemIterFn callback) } } +bool AssetCatalogTree::is_empty() const +{ + return root_items_.empty(); +} + +AssetCatalogTreeItem *AssetCatalogTree::find_item(const AssetCatalogPath &path) +{ + AssetCatalogTreeItem *result = nullptr; + this->foreach_item([&](AssetCatalogTreeItem &item) { + if (result) { + /* There is no way to stop iteration. */ + return; + } + if (item.catalog_path() == path) { + result = &item; + } + }); + return result; +} + +AssetCatalogTreeItem *AssetCatalogTree::find_root_item(const AssetCatalogPath &path) +{ + AssetCatalogTreeItem *result = nullptr; + this->foreach_root_item([&](AssetCatalogTreeItem &item) { + if (result) { + /* There is no way to stop iteration. */ + return; + } + if (item.catalog_path() == path) { + result = &item; + } + }); + return result; +} + /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ diff --git a/source/blender/blenkernel/intern/asset_catalog_path.cc b/source/blender/blenkernel/intern/asset_catalog_path.cc index 669adb7adf4..9c653c1a137 100644 --- a/source/blender/blenkernel/intern/asset_catalog_path.cc +++ b/source/blender/blenkernel/intern/asset_catalog_path.cc @@ -12,7 +12,7 @@ namespace blender::bke { const char AssetCatalogPath::SEPARATOR = '/'; -AssetCatalogPath::AssetCatalogPath(const std::string &path) : path_(path) +AssetCatalogPath::AssetCatalogPath(std::string path) : path_(std::move(path)) { } diff --git a/source/blender/blenkernel/intern/asset_library.cc b/source/blender/blenkernel/intern/asset_library.cc index 968873cbcbe..b8420af1168 100644 --- a/source/blender/blenkernel/intern/asset_library.cc +++ b/source/blender/blenkernel/intern/asset_library.cc @@ -10,6 +10,7 @@ #include "BKE_main.h" #include "BKE_preferences.h" +#include "BLI_fileops.h" #include "BLI_path_util.h" #include "DNA_asset_types.h" @@ -19,6 +20,13 @@ bool blender::bke::AssetLibrary::save_catalogs_when_file_is_saved = true; +blender::bke::AssetLibrary *BKE_asset_library_load(const Main *bmain, + const AssetLibraryReference &library_reference) +{ + blender::bke::AssetLibraryService *service = blender::bke::AssetLibraryService::get(); + return service->get_asset_library(bmain, library_reference); +} + /** * Loading an asset library at this point only means loading the catalogs. Later on this should * invoke reading of asset representations too. @@ -172,4 +180,26 @@ void AssetLibrary::refresh_catalog_simplename(struct AssetMetaData *asset_data) } STRNCPY(asset_data->catalog_simple_name, catalog->simple_name.c_str()); } + +Vector<AssetLibraryReference> all_valid_asset_library_refs() +{ + Vector<AssetLibraryReference> result; + int i; + LISTBASE_FOREACH_INDEX (const bUserAssetLibrary *, asset_library, &U.asset_libraries, i) { + if (!BLI_is_dir(asset_library->path)) { + continue; + } + AssetLibraryReference library_ref{}; + library_ref.custom_library_index = i; + library_ref.type = ASSET_LIBRARY_CUSTOM; + result.append(library_ref); + } + + AssetLibraryReference library_ref{}; + library_ref.custom_library_index = -1; + library_ref.type = ASSET_LIBRARY_LOCAL; + result.append(library_ref); + return result; +} + } // namespace blender::bke diff --git a/source/blender/blenkernel/intern/asset_library_service.cc b/source/blender/blenkernel/intern/asset_library_service.cc index a4f97234042..cd8de7908bf 100644 --- a/source/blender/blenkernel/intern/asset_library_service.cc +++ b/source/blender/blenkernel/intern/asset_library_service.cc @@ -7,11 +7,15 @@ #include "asset_library_service.hh" #include "BKE_blender.h" +#include "BKE_preferences.h" #include "BLI_fileops.h" /* For PATH_MAX (at least on Windows). */ #include "BLI_path_util.h" #include "BLI_string_ref.hh" +#include "DNA_asset_types.h" +#include "DNA_userdef_types.h" + #include "CLG_log.h" static CLG_LogRef LOG = {"bke.asset_service"}; @@ -38,6 +42,38 @@ void AssetLibraryService::destroy() instance_.reset(); } +AssetLibrary *AssetLibraryService::get_asset_library( + const Main *bmain, const AssetLibraryReference &library_reference) +{ + if (library_reference.type == ASSET_LIBRARY_LOCAL) { + /* For the "Current File" library we get the asset library root path based on main. */ + char root_path[FILE_MAX]; + if (bmain) { + BKE_asset_library_find_suitable_root_path_from_main(bmain, root_path); + } + else { + root_path[0] = '\0'; + } + + if (root_path[0] == '\0') { + /* File wasn't saved yet. */ + return get_asset_library_current_file(); + } + + return get_asset_library_on_disk(root_path); + } + if (library_reference.type == ASSET_LIBRARY_CUSTOM) { + bUserAssetLibrary *user_library = BKE_preferences_asset_library_find_from_index( + &U, library_reference.custom_library_index); + + if (user_library) { + return get_asset_library_on_disk(user_library->path); + } + } + + return nullptr; +} + namespace { std::string normalize_directory_path(StringRefNull directory) { diff --git a/source/blender/blenkernel/intern/asset_library_service.hh b/source/blender/blenkernel/intern/asset_library_service.hh index 277fb9db6cc..c22c6b182ce 100644 --- a/source/blender/blenkernel/intern/asset_library_service.hh +++ b/source/blender/blenkernel/intern/asset_library_service.hh @@ -44,6 +44,9 @@ class AssetLibraryService { /** Destroy the AssetLibraryService singleton. It will be reallocated by #get() if necessary. */ static void destroy(); + AssetLibrary *get_asset_library(const Main *bmain, + const AssetLibraryReference &library_reference); + /** * Get the given asset library. Opens it (i.e. creates a new AssetLibrary instance) if necessary. */ |