[Bf-blender-cvs] [b090c18ce46] temp-asset-representation: Store ID in asset representation directly

Julian Eisel noreply at git.blender.org
Tue Nov 8 18:52:07 CET 2022


Commit: b090c18ce46697c97a4e1eaba60bb92a4212431d
Author: Julian Eisel
Date:   Tue Nov 8 18:43:25 2022 +0100
Branches: temp-asset-representation
https://developer.blender.org/rBb090c18ce46697c97a4e1eaba60bb92a4212431d

Store ID in asset representation directly

This should make some things more convenient in future, since we can
directly access the ID if an asset represents one, without having to
look it up. But also, we need to do ID remapping either way, which
currently requires the ID pointer to identify the ID, not other ID data
(like the contained asset metadata).

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

M	source/blender/blenkernel/BKE_asset_library.hh
M	source/blender/blenkernel/BKE_asset_representation.hh
M	source/blender/blenkernel/intern/asset_library.cc
M	source/blender/blenkernel/intern/asset_representation.cc

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

diff --git a/source/blender/blenkernel/BKE_asset_library.hh b/source/blender/blenkernel/BKE_asset_library.hh
index 6402799b3a1..3358e37231e 100644
--- a/source/blender/blenkernel/BKE_asset_library.hh
+++ b/source/blender/blenkernel/BKE_asset_library.hh
@@ -60,7 +60,7 @@ struct AssetLibrary {
    * loading a different file).
    */
   AssetRepresentation &add_external_asset(std::unique_ptr<AssetMetaData> metadata);
-  AssetRepresentation &add_local_id_asset(const ID &id);
+  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. */
diff --git a/source/blender/blenkernel/BKE_asset_representation.hh b/source/blender/blenkernel/BKE_asset_representation.hh
index 35aecba51e5..0b81f7b05b1 100644
--- a/source/blender/blenkernel/BKE_asset_representation.hh
+++ b/source/blender/blenkernel/BKE_asset_representation.hh
@@ -23,15 +23,15 @@ namespace blender::bke {
 class AssetRepresentation {
   /** Null if the asset represents a local ID, in which case the ID owns the metadata. */
   std::unique_ptr<AssetMetaData> metadata_ = nullptr;
-  /** If the asset representation was constructed from a local ID, this points to the editable
-   * asset metadata of the ID. */
-  AssetMetaData *local_id_metadata_ = nullptr; /* Non-owning. */
+  /** If this asset represents an ID in the current file, this references the ID directly. This
+   * means the represented asset is a fully local, and editable entity. */
+  ID *local_asset_id_ = nullptr; /* Non-owning. */
 
  public:
   explicit AssetRepresentation(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(const ID &id);
+  explicit AssetRepresentation(ID &id);
 
   AssetMetaData &get_metadata() const;
   /** Returns if this asset is stored inside this current file, and as such fully editable. */
diff --git a/source/blender/blenkernel/intern/asset_library.cc b/source/blender/blenkernel/intern/asset_library.cc
index edf804f70bc..826c24647a2 100644
--- a/source/blender/blenkernel/intern/asset_library.cc
+++ b/source/blender/blenkernel/intern/asset_library.cc
@@ -130,7 +130,7 @@ AssetRepresentation &AssetLibrary::add_external_asset(std::unique_ptr<AssetMetaD
   return *asset_storage_.last();
 }
 
-AssetRepresentation &AssetLibrary::add_local_id_asset(const ID &id)
+AssetRepresentation &AssetLibrary::add_local_id_asset(ID &id)
 {
   asset_storage_.append(std::make_unique<AssetRepresentation>(id));
   return *asset_storage_.last();
diff --git a/source/blender/blenkernel/intern/asset_representation.cc b/source/blender/blenkernel/intern/asset_representation.cc
index 7123d7aba55..97f37edfa43 100644
--- a/source/blender/blenkernel/intern/asset_representation.cc
+++ b/source/blender/blenkernel/intern/asset_representation.cc
@@ -4,6 +4,8 @@
  * \ingroup bke
  */
 
+#include <stdexcept>
+
 #include "DNA_ID.h"
 #include "DNA_asset_types.h"
 
@@ -17,18 +19,21 @@ AssetRepresentation::AssetRepresentation(std::unique_ptr<AssetMetaData> metadata
 {
 }
 
-AssetRepresentation::AssetRepresentation(const ID &id) : local_id_metadata_(id.asset_data)
+AssetRepresentation::AssetRepresentation(ID &id) : local_asset_id_(&id)
 {
+  if (!id.asset_data) {
+    throw std::invalid_argument("Passed ID is not an asset");
+  }
 }
 
 AssetMetaData &AssetRepresentation::get_metadata() const
 {
-  return local_id_metadata_ ? *local_id_metadata_ : *metadata_;
+  return local_asset_id_ ? *local_asset_id_->asset_data : *metadata_;
 }
 
 bool AssetRepresentation::is_local_id() const
 {
-  return local_id_metadata_ != nullptr;
+  return local_asset_id_ != nullptr;
 }
 
 }  // namespace blender::bke



More information about the Bf-blender-cvs mailing list