[Bf-blender-cvs] [8750f355b32] asset-browser-poselib: Expose more asset info and utilities in the Python API

Julian Eisel noreply at git.blender.org
Fri Apr 2 19:19:57 CEST 2021


Commit: 8750f355b32bb50c8b71b87a3153071b9ef4cbe7
Author: Julian Eisel
Date:   Fri Apr 2 19:11:56 2021 +0200
Branches: asset-browser-poselib
https://developer.blender.org/rB8750f355b32bb50c8b71b87a3153071b9ef4cbe7

Expose more asset info and utilities in the Python API

Needed so the pose library add-on can keep its operators work outside of
the Asset Browser context, namely with the asset view template.

An issue here is that we can't expose an asset handle from the Asset
Browser, because there is not permanently saved one whose pointer we can
pass around. This is planned to be addressed, but for now Python will
have to use the file wrapped by the asset-handle directly, via
`context.asset_file_handle`.

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

M	source/blender/editors/asset/asset_edit.cc
M	source/blender/editors/include/ED_asset.h
M	source/blender/makesrna/intern/rna_asset.c
M	source/blender/makesrna/intern/rna_context.c
M	source/blender/makesrna/intern/rna_space.c

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

diff --git a/source/blender/editors/asset/asset_edit.cc b/source/blender/editors/asset/asset_edit.cc
index 48bd1188471..57832aa1e20 100644
--- a/source/blender/editors/asset/asset_edit.cc
+++ b/source/blender/editors/asset/asset_edit.cc
@@ -141,6 +141,26 @@ AssetLibraryReference ED_asset_library_reference_from_enum_value(int value)
   return library;
 }
 
+const char *ED_asset_handle_get_name(const AssetHandle *asset)
+{
+  return asset->file_data->name;
+}
+
+void ED_asset_handle_get_full_library_path(const bContext *C,
+                                           const AssetLibraryReference *asset_library,
+                                           const AssetHandle *asset,
+                                           char r_full_lib_path[FILE_MAX_LIBEXTRA])
+{
+  *r_full_lib_path = '\0';
+
+  std::string asset_path = ED_assetlist_asset_filepath_get(C, *asset_library, *asset);
+  if (asset_path.empty()) {
+    return;
+  }
+
+  BLO_library_path_explode(asset_path.c_str(), r_full_lib_path, nullptr, nullptr);
+}
+
 class AssetTemporaryIDConsumer : NonCopyable, NonMovable {
   const AssetHandle &handle_;
   TempLibraryContext *temp_lib_context_ = nullptr;
@@ -167,15 +187,9 @@ class AssetTemporaryIDConsumer : NonCopyable, NonMovable {
                 Main &bmain,
                 ReportList &reports)
   {
-    std::string asset_path = ED_assetlist_asset_filepath_get(C, asset_library, handle_);
-    if (asset_path.empty()) {
-      return nullptr;
-    }
-
+    const char *asset_name = ED_asset_handle_get_name(&handle_);
     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);
+    ED_asset_handle_get_full_library_path(C, &asset_library, &handle_, blend_file_path);
 
     temp_lib_context_ = BLO_library_temp_load_id(
         &bmain, blend_file_path, id_type, asset_name, &reports);
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h
index aae48d583cc..08994f859f3 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/include/ED_asset.h
@@ -44,6 +44,12 @@ 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);
 
+const char *ED_asset_handle_get_name(const AssetHandle *asset);
+void ED_asset_handle_get_full_library_path(const struct bContext *C,
+                                           const AssetLibraryReference *asset_library,
+                                           const AssetHandle *asset,
+                                           char r_full_lib_path[]);
+
 AssetTempIDConsumer *ED_asset_temp_id_consumer_create(const AssetHandle *handle);
 void ED_asset_temp_id_consumer_free(AssetTempIDConsumer **consumer);
 struct ID *ED_asset_temp_id_consumer_ensure_local_id(AssetTempIDConsumer *consumer,
diff --git a/source/blender/makesrna/intern/rna_asset.c b/source/blender/makesrna/intern/rna_asset.c
index 397c033d494..f2f20ea7b2e 100644
--- a/source/blender/makesrna/intern/rna_asset.c
+++ b/source/blender/makesrna/intern/rna_asset.c
@@ -25,6 +25,7 @@
 
 #include "DNA_asset_types.h"
 #include "DNA_defs.h"
+#include "DNA_space_types.h"
 
 #include "rna_internal.h"
 
@@ -134,7 +135,25 @@ static void rna_AssetMetaData_active_tag_range(
 static PointerRNA rna_AssetHandle_file_data_get(PointerRNA *ptr)
 {
   AssetHandle *asset_handle = ptr->data;
-  return rna_pointer_inherit_refine(ptr, &RNA_AssetHandle, asset_handle->file_data);
+  return rna_pointer_inherit_refine(ptr, &RNA_FileSelectEntry, asset_handle->file_data);
+}
+
+static void rna_AssetHandle_get_full_library_path(
+    // AssetHandle *asset,
+    bContext *C,
+    FileDirEntry *asset_file,
+    AssetLibraryReference *library,
+    char r_result[FILE_MAX_LIBEXTRA])
+{
+  AssetHandle asset = {.file_data = asset_file};
+  ED_asset_handle_get_full_library_path(C, library, &asset, r_result);
+}
+
+static PointerRNA rna_AssetHandle_local_id_get(PointerRNA *ptr)
+{
+  const AssetHandle *asset = ptr->data;
+  ID *id = ED_assetlist_asset_local_id_get(asset);
+  return rna_pointer_inherit_refine(ptr, &RNA_ID, id);
 }
 
 static void rna_AssetHandle_file_data_set(PointerRNA *ptr,
@@ -325,6 +344,30 @@ static void rna_def_asset_data(BlenderRNA *brna)
   RNA_def_property_ui_text(prop, "Active Tag", "Index of the tag set for editing");
 }
 
+static void rna_def_asset_handle_api(StructRNA *srna)
+{
+  FunctionRNA *func;
+  PropertyRNA *parm;
+
+  func = RNA_def_function(srna, "get_full_library_path", "rna_AssetHandle_get_full_library_path");
+  RNA_def_function_flag(func, FUNC_USE_CONTEXT);
+  /* TODO temporarily static function, for until .py can receive the asset handle from context
+   * properly. `asset_file_handle` should go away too then. */
+  RNA_def_function_flag(func, FUNC_NO_SELF);
+  parm = RNA_def_pointer(func, "asset_file_handle", "FileSelectEntry", "", "");
+  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+  parm = RNA_def_pointer(func,
+                         "asset_library",
+                         "AssetLibraryReference",
+                         "",
+                         "The asset library containing the given asset, only valid if the asset "
+                         "library is external (i.e. not the \"Current File\" one");
+  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+  parm = RNA_def_string(func, "result", NULL, FILE_MAX_LIBEXTRA, "result", "");
+  RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
+  RNA_def_function_output(func, parm);
+}
+
 static void rna_def_asset_handle(BlenderRNA *brna)
 {
   StructRNA *srna;
@@ -333,12 +376,24 @@ static void rna_def_asset_handle(BlenderRNA *brna)
   srna = RNA_def_struct(brna, "AssetHandle", "PropertyGroup");
   RNA_def_struct_ui_text(srna, "Asset Handle", "Reference to some asset");
 
+  /* TODO why is this editable? There probably shouldn't be a setter. */
   prop = RNA_def_property(srna, "file_data", PROP_POINTER, PROP_NONE);
   RNA_def_property_flag(prop, PROP_EDITABLE);
   RNA_def_property_struct_type(prop, "FileSelectEntry");
   RNA_def_property_pointer_funcs(
       prop, "rna_AssetHandle_file_data_get", "rna_AssetHandle_file_data_set", NULL, NULL);
   RNA_def_property_ui_text(prop, "File Entry", "File data used to refer to the asset");
+
+  prop = RNA_def_property(srna, "local_id", PROP_POINTER, PROP_NONE);
+  RNA_def_property_struct_type(prop, "ID");
+  RNA_def_property_pointer_funcs(prop, "rna_AssetHandle_local_id_get", NULL, NULL, NULL);
+  RNA_def_property_ui_text(prop,
+                           "",
+                           "The local data-block this asset represents; only valid if that is a "
+                           "data-block in this file");
+  RNA_def_property_flag(prop, PROP_HIDDEN);
+
+  rna_def_asset_handle_api(srna);
 }
 
 static void rna_def_asset_library_reference(BlenderRNA *brna)
diff --git a/source/blender/makesrna/intern/rna_context.c b/source/blender/makesrna/intern/rna_context.c
index e6dceb5af72..d14232a1a6b 100644
--- a/source/blender/makesrna/intern/rna_context.c
+++ b/source/blender/makesrna/intern/rna_context.c
@@ -58,6 +58,8 @@ const EnumPropertyItem rna_enum_context_mode_items[] = {
 
 #ifdef RNA_RUNTIME
 
+#  include "DNA_asset_types.h"
+
 #  ifdef WITH_PYTHON
 #    include "BPY_extern.h"
 #  endif
@@ -134,6 +136,20 @@ static PointerRNA rna_Context_gizmo_group_get(PointerRNA *ptr)
   return newptr;
 }
 
+static PointerRNA rna_Context_asset_file_handle_get(PointerRNA *ptr)
+{
+  bContext *C = (bContext *)ptr->data;
+  bool is_handle_valid;
+  AssetHandle asset_handle = CTX_wm_asset_handle(C, &is_handle_valid);
+  if (!is_handle_valid) {
+    return PointerRNA_NULL;
+  }
+
+  PointerRNA newptr;
+  RNA_pointer_create(NULL, &RNA_FileSelectEntry, asset_handle.file_data, &newptr);
+  return newptr;
+}
+
 static PointerRNA rna_Context_main_get(PointerRNA *ptr)
 {
   bContext *C = (bContext *)ptr->data;
@@ -281,6 +297,17 @@ void RNA_def_context(BlenderRNA *brna)
   RNA_def_property_struct_type(prop, "GizmoGroup");
   RNA_def_property_pointer_funcs(prop, "rna_Context_gizmo_group_get", NULL, NULL, NULL);
 
+  /* TODO can't expose AssetHandle, since there is no permanent storage to it (so we can't
+   * return a pointer). Instead provide the FileDirEntry pointer it wraps. */
+  prop = RNA_def_property(srna, "asset_file_handle", PROP_POINTER, PROP_NONE);
+  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+  RNA_def_property_struct_type(prop, "FileSelectEntry");
+  RNA_def_property_pointer_funcs(prop, "rna_Context_asset_file_handle_get", NULL, NULL, NULL);
+  RNA_def_property_ui_text(prop,
+                           "",
+                           "The file of an active asset. Avoid using this, it will be replaced by "
+                           "a proper AssetHandle design");
+
   /* Data */
   prop = RNA_def_property(srna, "blend_data", PROP_POINTER, PROP_NONE);
   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
diff --git a/source/blender/makesrna/intern/rna_space.c b/source/blender/makesrna/intern/rna_space.c
index cf22ff95e74..6f98a73d5aa 100644
--- a/source/blender/makesrna/intern/rna_space.c
+++ b/source/blender/makesrna/intern/rna_space.c
@@ -512,6 +512,8 @@ static const EnumPropertyItem rna_enum_curve_display_handle_items[] = {
 #  include "BLI_path_util.h"
 #  include "BLI_string.h"
 
+#  include "BLO_readfile.h"
+
 #  include "BKE_anim_data.h"
 #  include "BKE_brush.h"
 #  include "BKE_colortools.h"
@@ -2675,6 +2677,18 @@ static int rna_FileBrowser_FileSelectEntry_name_length(PointerRNA *ptr)
   return (int)strlen(entry->name);
 }
 
+static int rna_FileBrowser_FileSelectEntry_id_type_get(PointerRNA *ptr)
+{
+  const FileDirEntry *entry = ptr->data;
+  return entry->blentype;
+}
+
+static PointerRNA rna_FileBrowser_FileSelectEntry_local_id_get(PointerRNA *ptr)
+{
+  const FileDirEntry *entry = ptr->data;
+  return rna_pointer_inherit_refine(ptr, &RNA_ID, entry->id);
+}
+
 static int rna_FileBrowser_FileSelectEntry_preview_icon_id_get(PointerRNA *ptr)
 {
   const FileDirEntry *entry = pt

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list