[Bf-blender-cvs] [1efc94bb2f7] master: Asset System: New core type to represent assets (`AssetRepresenation`)

Julian Eisel noreply at git.blender.org
Wed Nov 9 19:32:07 CET 2022


Commit: 1efc94bb2f7b0321935ce6564b23d5d92ee41594
Author: Julian Eisel
Date:   Wed Nov 9 18:34:31 2022 +0100
Branches: master
https://developer.blender.org/rB1efc94bb2f7b0321935ce6564b23d5d92ee41594

Asset System: New core type to represent assets (`AssetRepresenation`)

Introduces a new `AssetRepresentation` type, as a runtime only container
to hold asset information. It is supposed to become _the_ main way to
represent and refer to assets in the asset system, see T87235. It can
store things like the asset name, asset traits, preview and other asset
metadata.

Technical documentation:
https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Representation.

By introducing a proper asset representation type, we do an important
step away from the previous, non-optimal representation of assets as
files in the file browser backend, and towards the asset system as
backend. It should replace the temporary & hacky `AssetHandle` design in
the near future. Note that the loading of asset data still happens
through the file browser backend, check the linked to Wiki page for more
information on that.

As a side-effect, asset metadata isn't stored in file browser file
entries when browsing with link/append anymore. Don't think this was
ever used, but scripts may have accessed this. Can be brought back if
there's a need for it.

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

M	source/blender/blenkernel/BKE_asset.h
M	source/blender/blenkernel/BKE_asset_library.h
M	source/blender/blenkernel/BKE_asset_library.hh
A	source/blender/blenkernel/BKE_asset_representation.hh
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/asset.cc
M	source/blender/blenkernel/intern/asset_catalog.cc
M	source/blender/blenkernel/intern/asset_library.cc
M	source/blender/blenkernel/intern/asset_library_service.cc
M	source/blender/blenkernel/intern/asset_library_service.hh
M	source/blender/blenkernel/intern/asset_library_service_test.cc
A	source/blender/blenkernel/intern/asset_representation.cc
M	source/blender/blenkernel/intern/context.c
M	source/blender/blenkernel/intern/lib_id_delete.c
M	source/blender/editors/asset/intern/asset_handle.cc
M	source/blender/editors/asset/intern/asset_temp_id_consumer.cc
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_drag.cc
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/space_file/asset_catalog_tree_view.cc
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/file_indexer.cc
M	source/blender/editors/space_file/file_intern.h
M	source/blender/editors/space_file/filelist.cc
M	source/blender/editors/space_file/space_file.c
M	source/blender/makesdna/DNA_asset_types.h
M	source/blender/makesdna/DNA_space_types.h
M	source/blender/makesrna/intern/rna_space.c
M	source/blender/windowmanager/WM_api.h
M	source/blender/windowmanager/intern/wm_dragdrop.cc

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

diff --git a/source/blender/blenkernel/BKE_asset.h b/source/blender/blenkernel/BKE_asset.h
index 81b520a1db0..bcbe19c0f3e 100644
--- a/source/blender/blenkernel/BKE_asset.h
+++ b/source/blender/blenkernel/BKE_asset.h
@@ -23,6 +23,9 @@ struct ID;
 struct IDProperty;
 struct PreviewImage;
 
+/** C handle for #bke::AssetRepresentation. */
+typedef struct AssetRepresentation AssetRepresentation;
+
 typedef void (*PreSaveFn)(void *asset_ptr, struct AssetMetaData *asset_data);
 
 typedef struct AssetTypeInfo {
@@ -68,6 +71,22 @@ struct PreviewImage *BKE_asset_metadata_preview_get_from_id(const struct AssetMe
 void BKE_asset_metadata_write(struct BlendWriter *writer, struct AssetMetaData *asset_data);
 void BKE_asset_metadata_read(struct BlendDataReader *reader, struct AssetMetaData *asset_data);
 
+const char *BKE_asset_representation_name_get(const AssetRepresentation *asset)
+    ATTR_WARN_UNUSED_RESULT;
+AssetMetaData *BKE_asset_representation_metadata_get(const AssetRepresentation *asset)
+    ATTR_WARN_UNUSED_RESULT;
+bool BKE_asset_representation_is_local_id(const AssetRepresentation *asset)
+    ATTR_WARN_UNUSED_RESULT;
+
 #ifdef __cplusplus
 }
 #endif
+
+#ifdef __cplusplus
+
+#  include <memory>
+
+[[nodiscard]] std::unique_ptr<AssetMetaData> BKE_asset_metadata_move_to_unique_ptr(
+    AssetMetaData *asset_data);
+
+#endif
diff --git a/source/blender/blenkernel/BKE_asset_library.h b/source/blender/blenkernel/BKE_asset_library.h
index 824bc24203d..fc648ff6976 100644
--- a/source/blender/blenkernel/BKE_asset_library.h
+++ b/source/blender/blenkernel/BKE_asset_library.h
@@ -6,6 +6,7 @@
 
 #pragma once
 
+struct IDRemapper;
 struct Main;
 
 #ifdef __cplusplus
@@ -24,41 +25,6 @@ typedef struct AssetLibrary AssetLibrary;
  */
 struct AssetLibrary *BKE_asset_library_load(const char *library_path);
 
-/**
- * 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.
- *
- * The design is made to find an appropriate asset library path from a .blend file path, but
- * technically works with any file or directory as \a input_path.
- * Design is:
- * * If \a input_path lies within a known asset library path (i.e. an asset library registered in
- *   the Preferences), return the asset library path.
- * * Otherwise, if \a input_path has a parent path, return the parent path (e.g. to use the
- *   directory a .blend file is in as asset library root).
- * * If \a input_path is empty or doesn't have a parent path (e.g. because a .blend wasn't saved
- *   yet), there is no suitable path. The caller has to decide how to handle this case.
- *
- * \param r_library_path: The returned asset library path with a trailing slash, or an empty string
- *                        if no suitable path is found. Assumed to be a buffer of at least
- *                        #FILE_MAXDIR bytes.
- *
- * \return True if the function could find a valid, that is, a non-empty path to return in \a
- *         r_library_path.
- */
-bool BKE_asset_library_find_suitable_root_path_from_path(
-    const char *input_path, char r_library_path[768 /* FILE_MAXDIR */]);
-/**
- * Uses the current location on disk of the file represented by \a bmain as input to
- * #BKE_asset_library_find_suitable_root_path_from_path(). Refer to it for a design
- * description.
- *
- * \return True if the function could find a valid, that is, a non-empty path to return in \a
- *         r_library_path. If \a bmain wasn't saved into a file yet, the return value will be
- *         false.
- */
-bool BKE_asset_library_find_suitable_root_path_from_main(
-    const struct Main *bmain, char r_library_path[768 /* FILE_MAXDIR */]);
-
 /** Look up the asset's catalog and copy its simple name into #asset_data. */
 void BKE_asset_library_refresh_catalog_simplename(struct AssetLibrary *asset_library,
                                                   struct AssetMetaData *asset_data);
@@ -66,6 +32,10 @@ void BKE_asset_library_refresh_catalog_simplename(struct AssetLibrary *asset_lib
 /** Return whether any loaded AssetLibrary has unsaved changes to its catalogs. */
 bool BKE_asset_library_has_any_unsaved_catalogs(void);
 
+/** An asset library can include local IDs (IDs in the current file). Their pointers need to be
+ * remapped on change (or assets removed as IDs gets removed). */
+void BKE_asset_library_remap_ids(struct IDRemapper *mappings);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/BKE_asset_library.hh b/source/blender/blenkernel/BKE_asset_library.hh
index 2058df71f6a..b6a11a6b3f4 100644
--- a/source/blender/blenkernel/BKE_asset_library.hh
+++ b/source/blender/blenkernel/BKE_asset_library.hh
@@ -12,6 +12,9 @@
 
 #include "DNA_asset_types.h"
 
+#include "BLI_string_ref.hh"
+#include "BLI_vector.hh"
+
 #include "BKE_asset_library.h"
 
 #include "BKE_asset_catalog.hh"
@@ -19,27 +22,50 @@
 
 #include <memory>
 
+struct AssetLibraryReference;
+struct Main;
+
 namespace blender::bke {
 
+struct AssetRepresentation;
+
 /**
  * AssetLibrary provides access to an asset library's data.
- * For now this is only for catalogs, later this can be expanded to indexes/caches/more.
+ *
+ * The asset library contains catalogs and storage for asset representations. It could be extended
+ * to also include asset indexes and more.
  */
 struct AssetLibrary {
   /* Controlled by #ED_asset_catalogs_set_save_catalogs_when_file_is_saved,
    * for managing the "Save Catalog Changes" in the quit-confirmation dialog box. */
   static bool save_catalogs_when_file_is_saved;
 
+  /** The directory representing the root of this library. */
+  std::string root_path;
+
   std::unique_ptr<AssetCatalogService> catalog_service;
 
   AssetLibrary();
   ~AssetLibrary();
 
-  void load(StringRefNull library_root_directory);
+  void load_catalogs(StringRefNull library_root_directory);
 
   /** Load catalogs that have changed on disk. */
   void refresh();
 
+  /**
+   * Create a representation of an asset to be considered part of this library. Once the
+   * representation is not needed anymore, it must be freed using #remove_asset(), or there will be
+   * leaking that's only cleared when the library storage is destructed (typically on exit or
+   * loading a different file).
+   */
+  AssetRepresentation &add_external_asset(StringRef name, std::unique_ptr<AssetMetaData> metadata);
+  AssetRepresentation &add_local_id_asset(ID &id);
+  /** Remove an asset from the library that was added using #add_external_asset() or
+   * #add_local_id_asset().
+   * \return True on success, false if the asset couldn't be found inside the library. */
+  bool remove_asset(AssetRepresentation &asset);
+
   /**
    * Update `catalog_simple_name` by looking up the asset's catalog by its ID.
    *
@@ -53,8 +79,27 @@ struct AssetLibrary {
 
   void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers);
 
+  void remap_ids(struct IDRemapper &mappings);
+
  private:
   bCallbackFuncStore on_save_callback_store_{};
+
+  /** 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
+   * have to be loaded externally and added to this storage via #add_external_asset() or
+   * #add_local_id_asset(). So this really is arbitrary storage as far as #AssetLibrary is
+   * concerned (allowing the API user to manage partial library storage and partial loading, so
+   * only relevant parts of a library are kept in memory).
+   *
+   * For now, multiple parts of Blender just keep adding their own assets to this storage. E.g.
+   * multiple asset browsers might load multiple representations for the same asset into this.
+   * Currently there is just no way to properly identify assets, or keep track of which assets are
+   * already in memory and which not. Neither do we keep track of how many parts of Blender are
+   * using an asset or an asset library, which is needed to know when assets can be freed.
+   */
+  Vector<std::unique_ptr<AssetRepresentation>> asset_storage_;
+
+  std::optional<int> find_asset_index(const AssetRepresentation &asset);
 };
 
 Vector<AssetLibraryReference> all_valid_asset_library_refs();
@@ -64,6 +109,40 @@ Vector<AssetLibraryReference> all_valid_asset_library_refs();
 blender::bke::AssetLibrary *BKE_asset_library_load(const Main *bmain,
                                                    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.
+ *
+ * The design is made to find an appropriate asset library path from a .blend file path, but
+ * technically works with any file or directory as \a input_path.
+ * Design is:
+ * * If \a input_path lies within a known asset library path (i.e. an asset library registered in
+ *   the Preferences), return the asset library path.
+ * * Otherwise, if \a input_path has a parent path, return the parent path (e.g. to use the
+ *   directory a .blend file is in as asset library root).
+ * * If \a input_path is empty or doesn't have a parent path (e.g. because a .blend wasn't saved
+ *   yet), there is no suitable path. The caller has to decide how to handle this case.
+ *
+ * \param r_library_path: The returned asset library path with a trailing slash, or an empty string
+ *                        if no suitable path is found. Assumed to be a buffer of at least
+ *                        #FILE_MAXDIR bytes.
+ *
+ * \return True if the function could find a valid, that is, a non-empty path to return in \a
+ *         r_library_path.
+ */
+std::string BKE_asset_library_find_suitable_root_path_from_path(blender::StringRefNull input_path);
+
+/**
+ * Uses the current location on disk of the file represented by \a bmain as input to
+ * #BKE_asset_library_find_suitable_root_path_from_path(). Refer to it for a design
+ * description.
+ *
+ * \return True if the function could find a valid, that is, a non-empty path to return in \a
+ *         r_library_path. If \a bmain wasn't saved into a file yet, the return value will be
+ *         false.

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list