[Bf-blender-cvs] [d51212c4f05] temp-asset-library-all: Integrate "All" library better with the asset system

Julian Eisel noreply at git.blender.org
Tue Nov 29 00:30:38 CET 2022


Commit: d51212c4f05e38851580f7e9e75d174695dc1b82
Author: Julian Eisel
Date:   Mon Nov 28 19:37:00 2022 +0100
Branches: temp-asset-library-all
https://developer.blender.org/rBd51212c4f05e38851580f7e9e75d174695dc1b82

Integrate "All" library better with the asset system

Now it actually loads data from all asset libraries when this is
selected. The asset representations still need to be loaded by the file
browser backend, this won't change for now.

This adds the concept of nested asset libraries, which I'd prefer to
keep as implementation detail and not expose in the API. But for now
it's needed (for the asset representation loading by the file browser
backend).

===================================================================

M	source/blender/asset_system/AS_asset_library.hh
M	source/blender/asset_system/CMakeLists.txt
M	source/blender/asset_system/intern/asset_library.cc
M	source/blender/asset_system/intern/asset_library_service.cc
M	source/blender/asset_system/intern/asset_library_service.hh
A	source/blender/asset_system/intern/utils.cc
A	source/blender/asset_system/intern/utils.hh
M	source/blender/editors/space_file/filelist.cc

===================================================================

diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh
index ece3426731c..3bbed9603a5 100644
--- a/source/blender/asset_system/AS_asset_library.hh
+++ b/source/blender/asset_system/AS_asset_library.hh
@@ -34,7 +34,9 @@ class AssetStorage;
  * to also include asset indexes and more.
  */
 class AssetLibrary {
-  bCallbackFuncStore on_save_callback_store_{};
+  /** If this is an asset library on disk, the top-level directory path. Normalized using
+   * #normalize_directory_path().*/
+  std::string root_path_;
 
   /** Storage for assets (better said their representations) that are considered to be part of this
    * library. Assets are not automatically loaded into this when loading an asset library. Assets
@@ -51,6 +53,12 @@ class AssetLibrary {
    */
   std::unique_ptr<AssetStorage> asset_storage_;
 
+  /** In some cases an asset library is a combination of multiple other ones, these are then
+   * referenced here. "All" asset library only currently. */
+  Vector<AssetLibrary *> nested_libs_; /* Non-owning pointers. */
+
+  bCallbackFuncStore on_save_callback_store_{};
+
  public:
   /* Controlled by #ED_asset_catalogs_set_save_catalogs_when_file_is_saved,
    * for managing the "Save Catalog Changes" in the quit-confirmation dialog box. */
@@ -58,11 +66,16 @@ class AssetLibrary {
 
   std::unique_ptr<AssetCatalogService> catalog_service;
 
+  friend class AssetLibraryService;
+
  public:
-  AssetLibrary();
+  /**
+   * \param root_path: If this is an asset library on disk, the top-level directory path.
+   */
+  AssetLibrary(StringRef root_path = "");
   ~AssetLibrary();
 
-  void load_catalogs(StringRefNull library_root_directory);
+  void load_catalogs();
 
   /** Load catalogs that have changed on disk. */
   void refresh();
@@ -85,6 +98,11 @@ class AssetLibrary {
    *         case when the reference is dangling). */
   bool remove_asset(AssetRepresentation &asset);
 
+  /** In some cases an asset library is a combination of multiple other ones ("All" asset library
+   * only currently). Iterate over the contained asset libraries, executing \a fn for each of them.
+   */
+  void foreach_nested(FunctionRef<void(AssetLibrary &nested_library)> fn);
+
   /**
    * Remap ID pointers for local ID assets, see #BKE_lib_remap.h. When an ID pointer would be
    * mapped to null (typically when an ID gets removed), the asset is removed, because we don't
@@ -105,6 +123,8 @@ class AssetLibrary {
 
   void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers);
 
+  StringRefNull root_path() const;
+
  private:
   std::optional<int> find_asset_index(const AssetRepresentation &asset);
 };
diff --git a/source/blender/asset_system/CMakeLists.txt b/source/blender/asset_system/CMakeLists.txt
index d00c3c72e3b..c9ef11d33f4 100644
--- a/source/blender/asset_system/CMakeLists.txt
+++ b/source/blender/asset_system/CMakeLists.txt
@@ -21,6 +21,7 @@ set(SRC
   intern/asset_library_service.cc
   intern/asset_representation.cc
   intern/asset_storage.cc
+  intern/utils.cc
 
   AS_asset_catalog.hh
   AS_asset_catalog_path.hh
@@ -29,6 +30,7 @@ set(SRC
   AS_asset_representation.hh
   intern/asset_library_service.hh
   intern/asset_storage.hh
+  intern/utils.hh
 
   AS_asset_library.h
   AS_asset_representation.h
diff --git a/source/blender/asset_system/intern/asset_library.cc b/source/blender/asset_system/intern/asset_library.cc
index eebbc8db837..79c3c55029c 100644
--- a/source/blender/asset_system/intern/asset_library.cc
+++ b/source/blender/asset_system/intern/asset_library.cc
@@ -22,6 +22,7 @@
 
 #include "asset_library_service.hh"
 #include "asset_storage.hh"
+#include "utils.hh"
 
 using namespace blender;
 using namespace blender::asset_system;
@@ -126,8 +127,9 @@ void AS_asset_library_remap_ids(const IDRemapper *mappings)
 
 namespace blender::asset_system {
 
-AssetLibrary::AssetLibrary()
-    : asset_storage_(std::make_unique<AssetStorage>()),
+AssetLibrary::AssetLibrary(StringRef root_path)
+    : root_path_(utils::normalize_directory_path(root_path)),
+      asset_storage_(std::make_unique<AssetStorage>()),
       catalog_service(std::make_unique<AssetCatalogService>())
 {
 }
@@ -139,9 +141,9 @@ AssetLibrary::~AssetLibrary()
   }
 }
 
-void AssetLibrary::load_catalogs(StringRefNull library_root_directory)
+void AssetLibrary::load_catalogs()
 {
-  auto catalog_service = std::make_unique<AssetCatalogService>(library_root_directory);
+  auto catalog_service = std::make_unique<AssetCatalogService>(root_path_);
   catalog_service->load_from_disk();
   this->catalog_service = std::move(catalog_service);
 }
@@ -167,6 +169,18 @@ bool AssetLibrary::remove_asset(AssetRepresentation &asset)
   return asset_storage_->remove_asset(asset);
 }
 
+void AssetLibrary::foreach_nested(FunctionRef<void(AssetLibrary &)> fn)
+{
+  for (AssetLibrary *library : nested_libs_) {
+    if (!library) {
+      BLI_assert_unreachable();
+      continue;
+    }
+
+    fn(*library);
+  }
+}
+
 void AssetLibrary::remap_ids_and_remove_invalid(const IDRemapper &mappings)
 {
   asset_storage_->remap_ids_and_remove_invalid(mappings);
@@ -215,6 +229,11 @@ void AssetLibrary::on_blend_save_post(struct Main *main,
   }
 }
 
+StringRefNull AssetLibrary::root_path() const
+{
+  return root_path_;
+}
+
 void AssetLibrary::refresh_catalog_simplename(struct AssetMetaData *asset_data)
 {
   if (BLI_uuid_is_nil(asset_data->catalog_id)) {
diff --git a/source/blender/asset_system/intern/asset_library_service.cc b/source/blender/asset_system/intern/asset_library_service.cc
index c40e6059f66..8a00ebe2a08 100644
--- a/source/blender/asset_system/intern/asset_library_service.cc
+++ b/source/blender/asset_system/intern/asset_library_service.cc
@@ -4,9 +4,6 @@
  * \ingroup asset_system
  */
 
-#include "asset_library_service.hh"
-#include "AS_asset_library.hh"
-
 #include "BKE_blender.h"
 #include "BKE_preferences.h"
 
@@ -19,6 +16,10 @@
 
 #include "CLG_log.h"
 
+#include "AS_asset_library.hh"
+#include "asset_library_service.hh"
+#include "utils.hh"
+
 /* When enabled, use a pre file load handler (#BKE_CB_EVT_LOAD_PRE) callback to destroy the asset
  * library service. Without this an explicit call from the file loading code is needed to do this,
  * which is not as nice.
@@ -57,69 +58,58 @@ void AssetLibraryService::destroy()
 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. */
-    std::string root_path = bmain ? AS_asset_library_find_suitable_root_path_from_main(bmain) : "";
-
-    if (root_path.empty()) {
-      /* File wasn't saved yet. */
-      return get_asset_library_current_file();
-    }
-
-    return get_asset_library_on_disk(root_path);
-  }
-
-  /* TODO */
-  if (library_reference.type == ASSET_LIBRARY_ALL) {
-    return get_asset_library_current_file();
-  }
-
-  if (library_reference.type == ASSET_LIBRARY_CUSTOM) {
-    std::string root_path = root_path_from_library_ref(library_reference);
-
-    if (!root_path.empty()) {
+  const eAssetLibraryType type = eAssetLibraryType(library_reference.type);
+
+  switch (type) {
+    case ASSET_LIBRARY_LOCAL: {
+      /* For the "Current File" library  we get the asset library root path based on main. */
+      std::string root_path = bmain ? AS_asset_library_find_suitable_root_path_from_main(bmain) :
+                                      "";
+
+      if (root_path.empty()) {
+        /* File wasn't saved yet. */
+        return get_asset_library_current_file();
+      }
       return get_asset_library_on_disk(root_path);
     }
+    case ASSET_LIBRARY_ALL:
+      return get_asset_library_all(bmain);
+    case ASSET_LIBRARY_CUSTOM: {
+      std::string root_path = root_path_from_library_ref(library_reference);
+
+      if (!root_path.empty()) {
+        return get_asset_library_on_disk(root_path);
+      }
+    } break;
   }
 
   return nullptr;
 }
 
-namespace {
-std::string normalize_directory_path(StringRefNull directory)
+AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull root_path)
 {
-
-  char dir_normalized[PATH_MAX];
-  STRNCPY(dir_normalized, directory.c_str());
-  BLI_path_normalize_dir(nullptr, dir_normalized, sizeof(dir_normalized));
-  return std::string(dir_normalized);
-}
-}  // namespace
-
-AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull top_level_directory)
-{
-  BLI_assert_msg(!top_level_directory.is_empty(),
+  BLI_assert_msg(!root_path.is_empty(),
                  "top level directory must be given for on-disk asset library");
 
-  std::string top_dir_trailing_slash = normalize_directory_path(top_level_directory);
+  std::string normalized_root_path = utils::normalize_directory_path(root_path);
 
   std::unique_ptr<AssetLibrary> *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(
-      top_dir_trailing_slash);
+      normalized_root_path);
   if (lib_uptr_ptr != nullptr) {
-    CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", top_dir_trailing_slash.c_str());
+    CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", normalized_root_path.c_str());
     AssetLibrary *lib = lib_uptr_ptr->get();
     lib->refresh();
     return lib;
   }
 
-  std::unique_ptr lib_uptr = std::make_unique<AssetLibrary>();
+  std::unique_ptr lib_uptr = std::make_unique<AssetLibrary>(normalized_root_path);
   AssetLibrary *lib = lib_uptr.get();
 
   lib->on_blend_save_handler_register();
-  lib->load_catalogs(top_dir_trailing_slash);
+  lib->load_catalogs();
 
-  on_disk_libraries_.add_new(top_dir_trailing_slash, std::move(lib_uptr));
-  CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_dir_trailing_slash.c_str());
+  on_disk_libraries_.add_new(normalized_root_path, std::move(lib_uptr));
+  CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", normalized_root_path.c_str());
   return lib;
 }
 
@@ -138,6 +128,30 @@ AssetLibrary *AssetLibraryService::get_asset_library_current_file()
   return lib;
 }
 
+AssetLibrary *AssetLibraryService::get_asset_library_all(const Main *bmain)
+{
+  if (all_library_) {
+    CLOG_INFO(&LOG, 2, "get all lib (cached)");
+  

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list