[Bf-blender-cvs] [f68da703a5d] master: Asset system: Initial asset identifier type

Julian Eisel noreply at git.blender.org
Wed Nov 30 19:44:53 CET 2022


Commit: f68da703a5d2731231e0faa5d33eef87d65eedf1
Author: Julian Eisel
Date:   Wed Nov 30 18:12:42 2022 +0100
Branches: master
https://developer.blender.org/rBf68da703a5d2731231e0faa5d33eef87d65eedf1

Asset system: Initial asset identifier type

No user visible changes expected.

`AssetIdentifier` holds information to uniquely identify and locate an
asset. More information:
https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Identifier

For the start this is tied quite a bit to file paths, so that external
assets are assumed to be in the file system.

This is needed to support an "All" asset library (see T102879), which
would contain assets from different locations. Currently the location of
an asset is queried via the file browser backend, which however requires
a common root location. It also moves us further away from the file
browser towards the asset system (see T87235) and allows us to remove
some hacks (see following commit).

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

A	source/blender/asset_system/AS_asset_identifier.hh
M	source/blender/asset_system/AS_asset_library.hh
M	source/blender/asset_system/AS_asset_representation.hh
M	source/blender/asset_system/CMakeLists.txt
A	source/blender/asset_system/intern/asset_identifier.cc
M	source/blender/asset_system/intern/asset_library.cc
M	source/blender/asset_system/intern/asset_representation.cc
M	source/blender/asset_system/intern/asset_storage.cc
M	source/blender/asset_system/intern/asset_storage.hh
M	source/blender/editors/space_file/filelist.cc

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

diff --git a/source/blender/asset_system/AS_asset_identifier.hh b/source/blender/asset_system/AS_asset_identifier.hh
new file mode 100644
index 00000000000..33b7f71becc
--- /dev/null
+++ b/source/blender/asset_system/AS_asset_identifier.hh
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup asset_system
+ *
+ * \brief Information to uniquely identify and locate an asset.
+ *
+ * https://wiki.blender.org/wiki/Source/Architecture/Asset_System/Back_End#Asset_Identifier
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+namespace blender::asset_system {
+
+class AssetIdentifier {
+  std::shared_ptr<std::string> library_root_path_;
+  std::string relative_asset_path_;
+
+ public:
+  AssetIdentifier(std::shared_ptr<std::string> library_root_path, std::string relative_asset_path);
+  AssetIdentifier(AssetIdentifier &&) = default;
+  AssetIdentifier(const AssetIdentifier &) = default;
+
+  std::string full_path() const;
+};
+
+}  // namespace blender::asset_system
diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh
index 7df6e6d9f51..9f9b1b80343 100644
--- a/source/blender/asset_system/AS_asset_library.hh
+++ b/source/blender/asset_system/AS_asset_library.hh
@@ -24,6 +24,7 @@ struct Main;
 
 namespace blender::asset_system {
 
+class AssetIdentifier;
 class AssetRepresentation;
 class AssetStorage;
 
@@ -35,8 +36,10 @@ class AssetStorage;
  */
 class AssetLibrary {
   /** If this is an asset library on disk, the top-level directory path. Normalized using
-   * #normalize_directory_path().*/
-  std::string root_path_;
+   * #normalize_directory_path(). Shared pointer so assets can safely point to it, and don't have
+   * to hold a copy (which is the size of `std::string` + the allocated buffer, if no short string
+   * optimization is used). With thousands of assets this might make a reasonable difference. */
+  std::shared_ptr<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
@@ -79,10 +82,16 @@ class AssetLibrary {
    * 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).
+   *
+   * \param relative_asset_path: The path of the asset relative to the asset library root. With
+   *                             this the asset must be uniquely identifiable within the asset
+   *                             library.
    */
-  AssetRepresentation &add_external_asset(StringRef name, std::unique_ptr<AssetMetaData> metadata);
+  AssetRepresentation &add_external_asset(StringRef relative_asset_path,
+                                          StringRef name,
+                                          std::unique_ptr<AssetMetaData> metadata);
   /** See #AssetLibrary::add_external_asset(). */
-  AssetRepresentation &add_local_id_asset(ID &id);
+  AssetRepresentation &add_local_id_asset(StringRef relative_asset_path, ID &id);
   /** Remove an asset from the library that was added using #add_external_asset() or
    * #add_local_id_asset(). Can usually be expected to be constant time complexity (worst case may
    * differ).
@@ -112,6 +121,12 @@ class AssetLibrary {
 
   void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers);
 
+  /**
+   * Create an asset identifier from the root path of this asset library and the given relative
+   * asset path (relative to the asset library root directory).
+   */
+  AssetIdentifier derive_asset_identifier(StringRef relative_asset_path);
+
   StringRefNull root_path() const;
 
  private:
diff --git a/source/blender/asset_system/AS_asset_representation.hh b/source/blender/asset_system/AS_asset_representation.hh
index 853222c8dc7..65f7e08f395 100644
--- a/source/blender/asset_system/AS_asset_representation.hh
+++ b/source/blender/asset_system/AS_asset_representation.hh
@@ -16,21 +16,23 @@
 
 #include "BLI_string_ref.hh"
 
+#include "AS_asset_identifier.hh"
+
 struct AssetMetaData;
 struct ID;
 
 namespace blender::asset_system {
 
 class AssetRepresentation {
-  struct ExternalAsset {
-    std::string name;
-    std::unique_ptr<AssetMetaData> metadata_ = nullptr;
-  };
-
+  AssetIdentifier identifier_;
   /** Indicate if this is a local or external asset, and as such, which of the union members below
    * should be used. */
   const bool is_local_id_ = false;
 
+  struct ExternalAsset {
+    std::string name;
+    std::unique_ptr<AssetMetaData> metadata_ = nullptr;
+  };
   union {
     ExternalAsset external_asset_;
     ID *local_asset_id_ = nullptr; /* Non-owning. */
@@ -40,10 +42,12 @@ class AssetRepresentation {
 
  public:
   /** Constructs an asset representation for an external ID. The asset will not be editable. */
-  explicit AssetRepresentation(StringRef name, std::unique_ptr<AssetMetaData> metadata);
+  AssetRepresentation(AssetIdentifier &&identifier,
+                      StringRef name,
+                      std::unique_ptr<AssetMetaData> metadata);
   /** Constructs an asset representation for an ID stored in the current file. This makes the asset
    * local and fully editable. */
-  explicit AssetRepresentation(ID &id);
+  AssetRepresentation(AssetIdentifier &&identifier, ID &id);
   AssetRepresentation(AssetRepresentation &&other);
   /* Non-copyable type. */
   AssetRepresentation(const AssetRepresentation &other) = delete;
@@ -55,6 +59,8 @@ class AssetRepresentation {
   /* Non-copyable type. */
   AssetRepresentation &operator=(const AssetRepresentation &other) = delete;
 
+  const AssetIdentifier &get_identifier() const;
+
   StringRefNull get_name() const;
   AssetMetaData &get_metadata() const;
   /** Returns if this asset is stored inside this current file, and as such fully editable. */
@@ -62,3 +68,8 @@ class AssetRepresentation {
 };
 
 }  // namespace blender::asset_system
+
+/* C-Handle */
+struct AssetRepresentation;
+
+const std::string AS_asset_representation_full_path_get(const ::AssetRepresentation *asset);
diff --git a/source/blender/asset_system/CMakeLists.txt b/source/blender/asset_system/CMakeLists.txt
index c9ef11d33f4..de1f41667d5 100644
--- a/source/blender/asset_system/CMakeLists.txt
+++ b/source/blender/asset_system/CMakeLists.txt
@@ -17,6 +17,7 @@ set(SRC
   intern/asset_catalog.cc
   intern/asset_catalog_path.cc
   intern/asset_catalog_tree.cc
+  intern/asset_identifier.cc
   intern/asset_library.cc
   intern/asset_library_service.cc
   intern/asset_representation.cc
@@ -26,6 +27,7 @@ set(SRC
   AS_asset_catalog.hh
   AS_asset_catalog_path.hh
   AS_asset_catalog_tree.hh
+  AS_asset_identifier.hh
   AS_asset_library.hh
   AS_asset_representation.hh
   intern/asset_library_service.hh
diff --git a/source/blender/asset_system/intern/asset_identifier.cc b/source/blender/asset_system/intern/asset_identifier.cc
new file mode 100644
index 00000000000..94a15103b52
--- /dev/null
+++ b/source/blender/asset_system/intern/asset_identifier.cc
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup asset_system
+ */
+
+#include "BLI_path_util.h"
+#include <iostream>
+
+#include "AS_asset_identifier.hh"
+
+namespace blender::asset_system {
+
+AssetIdentifier::AssetIdentifier(std::shared_ptr<std::string> library_root_path,
+                                 std::string relative_asset_path)
+    : library_root_path_(library_root_path), relative_asset_path_(relative_asset_path)
+{
+}
+
+std::string AssetIdentifier::full_path() const
+{
+  char path[FILE_MAX];
+  BLI_path_join(path, sizeof(path), library_root_path_->c_str(), relative_asset_path_.c_str());
+  return path;
+}
+
+}  // namespace blender::asset_system
diff --git a/source/blender/asset_system/intern/asset_library.cc b/source/blender/asset_system/intern/asset_library.cc
index 7bdffe9abad..2f3b56d226a 100644
--- a/source/blender/asset_system/intern/asset_library.cc
+++ b/source/blender/asset_system/intern/asset_library.cc
@@ -7,6 +7,7 @@
 #include <memory>
 
 #include "AS_asset_catalog_tree.hh"
+#include "AS_asset_identifier.hh"
 #include "AS_asset_library.h"
 #include "AS_asset_library.hh"
 #include "AS_asset_representation.hh"
@@ -122,7 +123,7 @@ void AS_asset_library_remap_ids(const IDRemapper *mappings)
 namespace blender::asset_system {
 
 AssetLibrary::AssetLibrary(StringRef root_path)
-    : root_path_(utils::normalize_directory_path(root_path)),
+    : root_path_(std::make_shared<std::string>(utils::normalize_directory_path(root_path))),
       asset_storage_(std::make_unique<AssetStorage>()),
       catalog_service(std::make_unique<AssetCatalogService>())
 {
@@ -147,15 +148,18 @@ void AssetLibrary::refresh()
   this->catalog_service->reload_catalogs();
 }
 
-AssetRepresentation &AssetLibrary::add_external_asset(StringRef name,
+AssetRepresentation &AssetLibrary::add_external_asset(StringRef relative_asset_path,
+                                                      StringRef name,
                                                       std::unique_ptr<AssetMetaData> metadata)
 {
-  return asset_storage_->add_external_asset(name, std::move(metadata));
+  AssetIdentifier identifier = derive_asset_identifier(relative_asset_path);
+  return asset_storage_->add_external_asset(std::move(identifier), name, std::move(metadata));
 }
 
-AssetRepresentation &AssetLibrary::add_local_id_asset(ID &id)
+AssetRepresentation &AssetLibrary::add_local_id_asset(StringRef relative_asset_path, ID &id)
 {
-  return asset_storage_->add_local_id_asset(id);
+  AssetIdentifier identifier = derive_asset_identifier(relative_asset_path);
+  return asset_storage_->add_local_id_asset(std::move(identifier), id);
 }
 
 bool AssetLibrary::remove_asset(AssetRepresentation &asset)
@@ -211,6 +215,11 @@ void AssetLibrary::on_blend_save_post(struct Main *main,
   }
 }
 
+AssetIdentifier AssetLibrary::derive_asset_identifier(StringRef relative_asset_path)
+{
+  return AssetIdentifier(root_path_, relative_asset_path);
+}
+
 void AssetLibrary::refresh_catalog_simplename(

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list