[Bf-blender-cvs] [10383566b80] ui-asset-view-template: Add utilities to temporarily append asset IDs

Julian Eisel noreply at git.blender.org
Wed Mar 24 14:36:09 CET 2021


Commit: 10383566b80004857bae8f87d4eb820f95214a25
Author: Julian Eisel
Date:   Wed Mar 24 13:29:56 2021 +0100
Branches: ui-asset-view-template
https://developer.blender.org/rB10383566b80004857bae8f87d4eb820f95214a25

Add utilities to temporarily append asset IDs

Uses the new `BLO_library_temp_xxx` functions, but deals with all the asset
specific file path building. This is a reasonable utility for the asset system
to have, it will probably be needed by more asset types than IDs.

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

M	source/blender/editors/asset/CMakeLists.txt
M	source/blender/editors/asset/asset_edit.cc
M	source/blender/editors/asset/asset_list.cc
M	source/blender/editors/include/ED_asset.h

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

diff --git a/source/blender/editors/asset/CMakeLists.txt b/source/blender/editors/asset/CMakeLists.txt
index ec917aa625a..e49b38ca01a 100644
--- a/source/blender/editors/asset/CMakeLists.txt
+++ b/source/blender/editors/asset/CMakeLists.txt
@@ -19,6 +19,7 @@ set(INC
     ../include
     ../../blenkernel
     ../../blenlib
+    ../../blenloader
     ../../makesdna
     ../../makesrna
     ../../windowmanager
diff --git a/source/blender/editors/asset/asset_edit.cc b/source/blender/editors/asset/asset_edit.cc
index 5681807e0aa..8fdfad7848b 100644
--- a/source/blender/editors/asset/asset_edit.cc
+++ b/source/blender/editors/asset/asset_edit.cc
@@ -18,12 +18,23 @@
  * \ingroup edasset
  */
 
+#include <memory>
+#include <string>
+
 #include "BKE_asset.h"
 #include "BKE_context.h"
 #include "BKE_lib_id.h"
+#include "BKE_report.h"
+
+#include "BLI_utility_mixins.hh"
+
+#include "BLO_readfile.h"
 
 #include "DNA_ID.h"
 #include "DNA_asset_types.h"
+#include "DNA_space_types.h"
+
+#include "MEM_guardedalloc.h"
 
 #include "UI_interface_icons.h"
 
@@ -31,6 +42,8 @@
 
 #include "ED_asset.h"
 
+using namespace blender;
+
 bool ED_asset_mark_id(const bContext *C, ID *id)
 {
   if (id->asset_data) {
@@ -125,3 +138,82 @@ AssetLibraryReference ED_asset_library_reference_from_enum_value(int value)
   }
   return library;
 }
+
+class AssetTemporaryIDConsumer : NonCopyable, NonMovable {
+  const AssetHandle &handle_;
+  TempLibraryContext *temp_lib_context_ = nullptr;
+
+ public:
+  AssetTemporaryIDConsumer(const AssetHandle &handle) : handle_(handle)
+  {
+  }
+  ~AssetTemporaryIDConsumer()
+  {
+    if (temp_lib_context_) {
+      BLO_library_temp_free(temp_lib_context_);
+    }
+  }
+
+  ID *get_local_id()
+  {
+    return ED_assetlist_asset_local_id_get(&handle_);
+  }
+
+  ID *import_id(const AssetLibraryReference &asset_library,
+                ID_Type id_type,
+                Main &bmain,
+                ReportList &reports)
+  {
+    std::string asset_path = ED_assetlist_asset_filepath_get(asset_library, handle_);
+    if (asset_path.empty()) {
+      return nullptr;
+    }
+
+    char blend_file_path[FILE_MAX_LIBEXTRA];
+    char *group = NULL;
+    char *asset_name = NULL;
+    BLO_library_path_explode(asset_path.c_str(), blend_file_path, &group, &asset_name);
+
+    temp_lib_context_ = BLO_library_temp_load_id(
+        &bmain, blend_file_path, id_type, asset_name, &reports);
+
+    if (temp_lib_context_ == nullptr || temp_lib_context_->temp_id == nullptr) {
+      BKE_reportf(&reports, RPT_ERROR, "Unable to load %s from %s", asset_name, blend_file_path);
+      return nullptr;
+    }
+
+    BLI_assert(GS(temp_lib_context_->temp_id->name) == id_type);
+    return temp_lib_context_->temp_id;
+  }
+};
+
+AssetTempIDConsumer *ED_asset_temporary_id_consumer_create(const AssetHandle *handle)
+{
+  if (!handle) {
+    return nullptr;
+  }
+  return reinterpret_cast<AssetTempIDConsumer *>(
+      OBJECT_GUARDED_NEW(AssetTemporaryIDConsumer, *handle));
+}
+
+void ED_asset_temporary_id_consumer_free(AssetTempIDConsumer **consumer)
+{
+  OBJECT_GUARDED_SAFE_DELETE(*consumer, AssetTemporaryIDConsumer);
+}
+
+ID *ED_asset_temporary_id_consumer_get_id(AssetTempIDConsumer *consumer_,
+                                          const AssetLibraryReference *asset_library,
+                                          ID_Type id_type,
+                                          Main *bmain,
+                                          ReportList *reports)
+{
+  if (!(consumer_ && asset_library && bmain && reports)) {
+    return nullptr;
+  }
+  AssetTemporaryIDConsumer *consumer = reinterpret_cast<AssetTemporaryIDConsumer *>(consumer_);
+
+  if (ID *local_id = consumer->get_local_id()) {
+    return local_id;
+  }
+  return consumer->import_id(*asset_library, id_type, *bmain, *reports);
+}
diff --git a/source/blender/editors/asset/asset_list.cc b/source/blender/editors/asset/asset_list.cc
index 903c4fdbf17..c418ef919e7 100644
--- a/source/blender/editors/asset/asset_list.cc
+++ b/source/blender/editors/asset/asset_list.cc
@@ -24,13 +24,16 @@
  */
 
 #include <optional>
+#include <string>
 
+#include "BKE_asset.h"
 #include "BKE_context.h"
 #include "BKE_screen.h"
 
 #include "BLI_function_ref.hh"
 #include "BLI_hash.hh"
 #include "BLI_map.hh"
+#include "BLI_path_util.h"
 #include "BLI_utility_mixins.hh"
 
 #include "DNA_asset_types.h"
@@ -153,7 +156,7 @@ class PreviewTimer : NonCopyable {
 class AssetList : NonCopyable {
   FileListWrapper filelist_;
   AssetLibraryReference library_ref_;
-  PreviewTimer previews_timer;
+  PreviewTimer previews_timer_;
 
  public:
   AssetList() = delete;
@@ -277,11 +280,11 @@ void AssetList::ensurePreviewsJob(bContext *C)
   {
     const bool previews_running = filelist_cache_previews_running(files);
     if (previews_running) {
-      previews_timer.ensureRunning(C);
+      previews_timer_.ensureRunning(C);
     }
     else {
       /* Preview is not running, no need to keep generating update events! */
-      previews_timer.stop(C);
+      previews_timer_.stop(C);
     }
   }
 }
@@ -472,6 +475,29 @@ void ED_assetlist_iterate(const AssetLibraryReference *library_reference, AssetL
   }
 }
 
+std::string ED_assetlist_asset_filepath_get(const AssetLibraryReference &library_reference,
+                                            const AssetHandle &asset_handle)
+{
+  if (asset_handle.file_data->id || !asset_handle.file_data->asset_data) {
+    return nullptr;
+  }
+  const char *library_path = ED_assetlist_library_path(&library_reference);
+  if (!library_path) {
+    return nullptr;
+  }
+  const char *asset_relpath = asset_handle.file_data->relpath;
+
+  char path[FILE_MAX_LIBEXTRA];
+  BLI_join_dirfile(path, sizeof(path), library_path, asset_relpath);
+
+  return path;
+}
+
+ID *ED_assetlist_asset_local_id_get(const AssetHandle *asset_handle)
+{
+  return asset_handle->file_data->asset_data ? asset_handle->file_data->id : nullptr;
+}
+
 ImBuf *ED_assetlist_asset_image_get(const AssetHandle *asset_handle)
 {
   ImBuf *imbuf = filelist_file_getimage(asset_handle->file_data);
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h
index 45ef7c42386..69ff22a6e80 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/include/ED_asset.h
@@ -20,6 +20,7 @@
 
 #pragma once
 
+#include "DNA_ID_enums.h"
 #include "DNA_asset_types.h"
 
 #ifdef __cplusplus
@@ -29,8 +30,12 @@ extern "C" {
 struct AssetFilterSettings;
 struct AssetLibraryReference;
 struct bContext;
+struct Main;
+struct ReportList;
 struct wmNotifier;
 
+typedef struct AssetTempIDConsumer AssetTempIDConsumer;
+
 bool ED_asset_mark_id(const struct bContext *C, struct ID *id);
 bool ED_asset_clear_id(struct ID *id);
 
@@ -39,6 +44,14 @@ bool ED_asset_can_make_single_from_context(const struct bContext *C);
 int ED_asset_library_reference_to_enum_value(const struct AssetLibraryReference *library);
 struct AssetLibraryReference ED_asset_library_reference_from_enum_value(int value);
 
+AssetTempIDConsumer *ED_asset_temporary_id_consumer_create(const AssetHandle *handle);
+void ED_asset_temporary_id_consumer_free(AssetTempIDConsumer **consumer);
+struct ID *ED_asset_temporary_id_consumer_get_id(AssetTempIDConsumer *consumer_,
+                                                 const AssetLibraryReference *asset_library,
+                                                 ID_Type id_type,
+                                                 struct Main *bmain,
+                                                 struct ReportList *reports);
+
 void ED_assetlist_storage_fetch(const struct AssetLibraryReference *library_reference,
                                 const struct AssetFilterSettings *filter_settings,
                                 const struct bContext *C);
@@ -48,6 +61,7 @@ void ED_assetlist_storage_tag_main_data_dirty(void);
 void ED_assetlist_storage_id_remap(struct ID *id_old, struct ID *id_new);
 void ED_assetlist_storage_exit(void);
 
+ID *ED_assetlist_asset_local_id_get(const AssetHandle *asset_handle);
 struct ImBuf *ED_assetlist_asset_image_get(const AssetHandle *asset_handle);
 const char *ED_assetlist_library_path(const struct AssetLibraryReference *library_reference);
 
@@ -62,6 +76,9 @@ void ED_operatortypes_asset(void);
 
 /* TODO move to C++ asset-list header? */
 #ifdef __cplusplus
+std::string ED_assetlist_asset_filepath_get(const AssetLibraryReference &library_reference,
+                                            const AssetHandle &asset_handle);
+
 #  include "BLI_function_ref.hh"
 /* Can return false to stop iterating. */
 using AssetListIterFn = blender::FunctionRef<bool(FileDirEntry &)>;



More information about the Bf-blender-cvs mailing list