[Bf-blender-cvs] [35e54b52e6e] master: Assets: "All" asset library

Julian Eisel noreply at git.blender.org
Tue Jan 10 15:42:05 CET 2023


Commit: 35e54b52e6ecbb0465f1a28e0915769160b7bf86
Author: Julian Eisel
Date:   Tue Jan 10 15:27:28 2023 +0100
Branches: master
https://developer.blender.org/rB35e54b52e6ecbb0465f1a28e0915769160b7bf86

Assets: "All" asset library

Adds a new built-in asset library that contains all other asset
libraries visible in the asset library selector menu. This also means
all their asset catalogs will be displayed as a single merged tree. The
asset catalogs are not editable, since this would require support for
writing multiple catalog definition files, which isn't there yet.

Often it's not relevant where an asset comes from. Users just want to be
able to get an asset quickly, comparable to how people use a search
engine to browse images or the web itself, instead of first going to a
dedicated platform. They don't want to bother with first choosing where
they want the result to come from.
This especially is needed for the Asset Shelf (T102879) that is being
developed for the brush assets project (T101895). With this, users will
have access to all their brushes efficiently from the 3D view, without
much browsing.

Did an informal review of the asset system bits with Sybren.

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

M	source/blender/asset_system/AS_asset_catalog.hh
M	source/blender/asset_system/AS_asset_library.hh
M	source/blender/asset_system/intern/asset_catalog.cc
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
M	source/blender/editors/asset/ED_asset_catalog.h
M	source/blender/editors/asset/ED_asset_catalog.hh
M	source/blender/editors/asset/ED_asset_library.h
M	source/blender/editors/asset/ED_asset_list.hh
M	source/blender/editors/asset/intern/asset_catalog.cc
M	source/blender/editors/asset/intern/asset_library_reference_enum.cc
M	source/blender/editors/asset/intern/asset_list.cc
M	source/blender/editors/asset/intern/asset_ops.cc
M	source/blender/editors/space_file/asset_catalog_tree_view.cc
M	source/blender/editors/space_file/filelist.cc
M	source/blender/editors/space_file/filesel.c
M	source/blender/makesdna/DNA_asset_defaults.h
M	source/blender/makesdna/DNA_asset_types.h
M	source/blender/makesdna/DNA_space_types.h

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

diff --git a/source/blender/asset_system/AS_asset_catalog.hh b/source/blender/asset_system/AS_asset_catalog.hh
index 87b9425c8c6..8f818c6a768 100644
--- a/source/blender/asset_system/AS_asset_catalog.hh
+++ b/source/blender/asset_system/AS_asset_catalog.hh
@@ -45,11 +45,17 @@ class AssetCatalogService {
   Vector<std::unique_ptr<AssetCatalogCollection>> undo_snapshots_;
   Vector<std::unique_ptr<AssetCatalogCollection>> redo_snapshots_;
 
+  const bool is_read_only_ = false;
+
  public:
   static const CatalogFilePath DEFAULT_CATALOG_FILENAME;
 
+  struct read_only_tag {
+  };
+
  public:
   AssetCatalogService();
+  explicit AssetCatalogService(read_only_tag);
   explicit AssetCatalogService(const CatalogFilePath &asset_library_root);
 
   /**
@@ -62,11 +68,24 @@ class AssetCatalogService {
   void tag_has_unsaved_changes(AssetCatalog *edited_catalog);
   bool has_unsaved_changes() const;
 
+  /**
+   * Check if this is a read-only service meaning the user shouldn't be able to do edits. This is
+   * not enforced by internal catalog code, the catalog service user is responsible for it. For
+   * example the UI should disallow edits.
+   */
+  bool is_read_only() const;
+
   /** Load asset catalog definitions from the files found in the asset library. */
   void load_from_disk();
   /** Load asset catalog definitions from the given file or directory. */
   void load_from_disk(const CatalogFilePath &file_or_directory_path);
 
+  /**
+   * Duplicate the catalogs from \a other_service into this one. Does not rebuild the tree, this
+   * needs to be done by the caller (call #rebuild_tree()!).
+   */
+  void add_from_existing(const AssetCatalogService &other_service);
+
   /**
    * Write the catalog definitions to disk.
    *
@@ -105,6 +124,15 @@ class AssetCatalogService {
    */
   void reload_catalogs();
 
+  /**
+   * Make sure the tree is updated to the latest collection of catalogs stored in this service.
+   * Does not depend on a CDF file being available so this can be called on a service that stores
+   * catalogs that are not stored in a CDF.
+   * Most API functions that modify catalog data will trigger this, unless otherwise specified (for
+   * batch operations).
+   */
+  void rebuild_tree();
+
   /** Return catalog with the given ID. Return nullptr if not found. */
   AssetCatalog *find_catalog(CatalogID catalog_id) const;
 
@@ -222,7 +250,6 @@ class AssetCatalogService {
       const CatalogFilePath &blend_file_path);
 
   std::unique_ptr<AssetCatalogTree> read_into_tree();
-  void rebuild_tree();
 
   /**
    * For every catalog, ensure that its parent path also has a known catalog.
@@ -270,6 +297,11 @@ class AssetCatalogCollection {
   AssetCatalogCollection(AssetCatalogCollection &&other) noexcept = default;
 
   std::unique_ptr<AssetCatalogCollection> deep_copy() const;
+  /**
+   * Copy the catalogs from \a other and append them to this collection. Copies no other data
+   * otherwise.
+   */
+  void add_catalogs_from_existing(const AssetCatalogCollection &other);
 
  protected:
   static OwningAssetCatalogMap copy_catalog_map(const OwningAssetCatalogMap &orig);
diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh
index f272ed6ff51..b3b7d421724 100644
--- a/source/blender/asset_system/AS_asset_library.hh
+++ b/source/blender/asset_system/AS_asset_library.hh
@@ -56,6 +56,8 @@ class AssetLibrary {
    */
   std::unique_ptr<AssetStorage> asset_storage_;
 
+  std::function<void(AssetLibrary &self)> on_refresh_;
+
   bCallbackFuncStore on_save_callback_store_{};
 
  public:
@@ -65,6 +67,8 @@ class AssetLibrary {
 
   std::unique_ptr<AssetCatalogService> catalog_service;
 
+  friend class AssetLibraryService;
+
  public:
   /**
    * \param root_path: If this is an asset library on disk, the top-level directory path.
@@ -72,6 +76,16 @@ class AssetLibrary {
   AssetLibrary(StringRef root_path = "");
   ~AssetLibrary();
 
+  /**
+   * Execute \a fn for every asset library that is loaded. The asset library is passed to the
+   * \a fn call.
+   *
+   * \param skip_all_library: When true, the \a fn will also be executed for the "All" asset
+   *                          library. This is just a combination of the other ones, so usually
+   *                          iterating over it is redundant.
+   */
+  static void foreach_loaded(FunctionRef<void(AssetLibrary &)> fn, bool include_all_library);
+
   void load_catalogs();
 
   /** Load catalogs that have changed on disk. */
@@ -128,9 +142,6 @@ class AssetLibrary {
   AssetIdentifier asset_identifier_from_library(StringRef relative_asset_path);
 
   StringRefNull root_path() const;
-
- private:
-  std::optional<int> find_asset_index(const AssetRepresentation &asset);
 };
 
 Vector<AssetLibraryReference> all_valid_asset_library_refs();
@@ -138,12 +149,22 @@ Vector<AssetLibraryReference> all_valid_asset_library_refs();
 }  // namespace blender::asset_system
 
 /**
+ * Load the data for an asset library, but not the asset representations themselves (loading these
+ * is currently not done in the asset system).
+ *
+ * For the "All" asset library (#ASSET_LIBRARY_ALL), every other known asset library will be
+ * loaded as well. So a call to #AssetLibrary::foreach_loaded() can be expected to iterate over all
+ * libraries.
+ *
  * \warning Catalogs are reloaded, invalidating catalog pointers. Do not store catalog pointers,
  *          store CatalogIDs instead and lookup the catalog where needed.
  */
 blender::asset_system::AssetLibrary *AS_asset_library_load(
     const Main *bmain, const AssetLibraryReference &library_reference);
 
+std::string AS_asset_library_root_path_from_library_ref(
+    const AssetLibraryReference &library_reference);
+
 /**
  * Try to find an appropriate location for an asset library root from a file or directory path.
  * Does not check if \a input_path exists.
diff --git a/source/blender/asset_system/intern/asset_catalog.cc b/source/blender/asset_system/intern/asset_catalog.cc
index c295f2de16e..6fd01ab14f7 100644
--- a/source/blender/asset_system/intern/asset_catalog.cc
+++ b/source/blender/asset_system/intern/asset_catalog.cc
@@ -43,6 +43,11 @@ AssetCatalogService::AssetCatalogService()
 {
 }
 
+AssetCatalogService::AssetCatalogService(read_only_tag) : AssetCatalogService()
+{
+  const_cast<bool &>(is_read_only_) = true;
+}
+
 AssetCatalogService::AssetCatalogService(const CatalogFilePath &asset_library_root)
     : AssetCatalogService()
 {
@@ -51,6 +56,8 @@ AssetCatalogService::AssetCatalogService(const CatalogFilePath &asset_library_ro
 
 void AssetCatalogService::tag_has_unsaved_changes(AssetCatalog *edited_catalog)
 {
+  BLI_assert(!is_read_only_);
+
   if (edited_catalog) {
     edited_catalog->flags.has_unsaved_changes = true;
   }
@@ -85,6 +92,11 @@ bool AssetCatalogService::has_unsaved_changes() const
   return catalog_collection_->has_unsaved_changes_;
 }
 
+bool AssetCatalogService::is_read_only() const
+{
+  return is_read_only_;
+}
+
 void AssetCatalogService::tag_all_catalogs_as_unsaved_changes()
 {
   for (auto &catalog : catalog_collection_->catalogs_.values()) {
@@ -322,6 +334,11 @@ void AssetCatalogService::load_from_disk(const CatalogFilePath &file_or_director
   rebuild_tree();
 }
 
+void AssetCatalogService::add_from_existing(const AssetCatalogService &other_service)
+{
+  catalog_collection_->add_catalogs_from_existing(*other_service.catalog_collection_);
+}
+
 void AssetCatalogService::load_directory_recursive(const CatalogFilePath &directory_path)
 {
   /* TODO(@sybren): implement proper multi-file support. For now, just load
@@ -452,6 +469,8 @@ bool AssetCatalogService::is_catalog_known_with_unsaved_changes(const CatalogID
 
 bool AssetCatalogService::write_to_disk(const CatalogFilePath &blend_file_path)
 {
+  BLI_assert(!is_read_only_);
+
   if (!write_to_disk_ex(blend_file_path)) {
     return false;
   }
@@ -625,6 +644,7 @@ void AssetCatalogService::undo()
 
 void AssetCatalogService::redo()
 {
+  BLI_assert(!is_read_only_);
   BLI_assert_msg(is_redo_possbile(), "Redo stack is empty");
 
   undo_snapshots_.append(std::move(catalog_collection_));
@@ -634,6 +654,7 @@ void AssetCatalogService::redo()
 
 void AssetCatalogService::undo_push()
 {
+  BLI_assert(!is_read_only_);
   std::unique_ptr<AssetCatalogCollection> snapshot = catalog_collection_->deep_copy();
   undo_snapshots_.append(std::move(snapshot));
   redo_snapshots_.clear();
@@ -657,15 +678,24 @@ std::unique_ptr<AssetCatalogCollection> AssetCatalogCollection::deep_copy() cons
   return copy;
 }
 
-OwningAssetCatalogMap AssetCatalogCollection::copy_catalog_map(const OwningAssetCatalogMap &orig)
+static void copy_catalog_map_into_existing(const OwningAssetCatalogMap &source,
+                                           OwningAssetCatalogMap &dest)
 {
-  OwningAssetCatalogMap copy;
-
-  for (const auto &orig_catalog_uptr : orig.values()) {
+  for (const auto &orig_catalog_uptr : source.values()) {
     auto copy_catalog_uptr = std::make_unique<AssetCatalog>(*orig_catalog_uptr);
-    copy.add_new(copy_catalog_uptr->catalog_id, std::move(copy_catalog_uptr));
+    dest.add_new(copy_catalog_uptr->catalog_id, std::move(copy_catalog_uptr));
   }
+}
+
+void AssetCatalogCollection::add_catalogs_from_existing(const AssetCatalogCollection &other)
+{
+  copy_catalog_map_into_existing(other.catalogs_, catalogs_);
+}
 
+OwningAssetCatalogMap AssetCatalogCollection::copy_catalog_map(const OwningAssetCatalogMap &orig)
+{
+  OwningAssetCatalogMap copy;
+  copy_catalog_map_into_existing(orig, copy);
   return copy;
 }
 
diff --git a/source/blender/asset_system/intern/asset_library.cc b/source/blender/asset_system/intern/asset_library.cc
index 9a6b73ef501..2379e738e37 100644
--- a/source/blender/asset_system/intern/asset_library.cc
+++ b/source/blender/asset_system/intern/asset_library.cc
@@ -65,6 +65,12 @@ bool AS_asset_library_has_any_unsaved_catalogs()
   return service->has_any_unsaved_catalogs();
 }
 
+std::string AS_asset_library_root_path_from_library_ref(
+    const AssetLibraryReference &library_reference)
+{
+  return AssetLibraryService::root_path_from_library_ref(library_reference);
+}
+
 std::string AS_asset_library_find_suitable_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list