[Bf-blender-cvs] [792eb1381ea] temp-asset-browser-poselib-merge: Assets: AssetHandle type as temporary design to reference assets

Julian Eisel noreply at git.blender.org
Wed Jul 14 22:53:08 CEST 2021


Commit: 792eb1381ea4c823768b6b440dd8a9c402ffbbdc
Author: Julian Eisel
Date:   Thu Jul 8 23:20:26 2021 +0200
Branches: temp-asset-browser-poselib-merge
https://developer.blender.org/rB792eb1381ea4c823768b6b440dd8a9c402ffbbdc

Assets: AssetHandle type as temporary design to reference assets

With temporary I mean that this is not intended to be part of the
eventual asset system design. For that we are planning to have an
`AssetRepresentation` instead, see T87235. Once the `AssetList` is
implemented (see T88184), that would be the owner of the asset
representations.

However for the upcoming asset system, asset browser, asset view and
pose library commits we need some kind of asset handle to pass around.
That is what this commit introduces.
Idea is a handle to wrap the `FileDirEntry` representing the asset, and
an API to access its data (currently very small, will be extended in
further commits). So the fact that an asset is currently a file
internally is abstracted away. However: We have to expose it as file in
the Python API, because we can't return the asset-handle directly there,
for reasons explained in the code. So the active asset file is exposed
as `bpy.context.asset_file_handle`.

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

M	source/blender/blenkernel/BKE_context.h
M	source/blender/blenkernel/intern/context.c
M	source/blender/editors/asset/asset_edit.cc
M	source/blender/editors/include/ED_asset.h
M	source/blender/makesdna/DNA_asset_types.h
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_asset.c
M	source/blender/makesrna/intern/rna_context.c

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

diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h
index 416947e0b62..8917580689d 100644
--- a/source/blender/blenkernel/BKE_context.h
+++ b/source/blender/blenkernel/BKE_context.h
@@ -23,6 +23,9 @@
  * \ingroup bke
  */
 
+/* XXX temporary, until AssetHandle is designed properly and queries can return a pointer to it. */
+#include "DNA_asset_types.h"
+
 #include "DNA_listBase.h"
 #include "DNA_object_enums.h"
 #include "RNA_types.h"
@@ -358,6 +361,7 @@ int CTX_data_editable_gpencil_layers(const bContext *C, ListBase *list);
 int CTX_data_editable_gpencil_strokes(const bContext *C, ListBase *list);
 
 const struct AssetLibraryReference *CTX_wm_asset_library(const bContext *C);
+struct AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid);
 
 bool CTX_wm_interface_locked(const bContext *C);
 
diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c
index 4c91a3f3387..dced945bea0 100644
--- a/source/blender/blenkernel/intern/context.c
+++ b/source/blender/blenkernel/intern/context.c
@@ -1453,6 +1453,31 @@ const AssetLibraryReference *CTX_wm_asset_library(const bContext *C)
   return ctx_data_pointer_get(C, "asset_library");
 }
 
+AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid)
+{
+  AssetHandle *asset_handle_p =
+      (AssetHandle *)CTX_data_pointer_get_type(C, "asset_handle", &RNA_AssetHandle).data;
+  if (asset_handle_p) {
+    *r_is_valid = true;
+    return *asset_handle_p;
+  }
+
+  /* If the asset handle was not found in context directly, try if there's an active file with
+   * asset data there instead. Not nice to have this here, would be better to have this in
+   * `ED_asset.h`, but we can't include that in BKE. Even better would be not needing this at all
+   * and being able to have editors return this in the usual `context` callback. But that would
+   * require returning a non-owning pointer, which we don't have in the Asset Browser (yet). */
+  FileDirEntry *file =
+      (FileDirEntry *)CTX_data_pointer_get_type(C, "active_file", &RNA_FileSelectEntry).data;
+  if (file && file->asset_data) {
+    *r_is_valid = true;
+    return (AssetHandle){.file_data = file};
+  }
+
+  *r_is_valid = false;
+  return (AssetHandle){0};
+}
+
 Depsgraph *CTX_data_depsgraph_pointer(const bContext *C)
 {
   Main *bmain = CTX_data_main(C);
diff --git a/source/blender/editors/asset/asset_edit.cc b/source/blender/editors/asset/asset_edit.cc
index 5ae07a54b11..949dc1890ad 100644
--- a/source/blender/editors/asset/asset_edit.cc
+++ b/source/blender/editors/asset/asset_edit.cc
@@ -23,6 +23,8 @@
 #include "BKE_lib_id.h"
 
 #include "DNA_ID.h"
+#include "DNA_asset_types.h"
+#include "DNA_space_types.h"
 
 #include "UI_interface_icons.h"
 
@@ -118,3 +120,8 @@ 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;
+}
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h
index a2ef0755fbd..da0809f2e33 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/include/ED_asset.h
@@ -35,6 +35,8 @@ 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_operatortypes_asset(void);
 
 #ifdef __cplusplus
diff --git a/source/blender/makesdna/DNA_asset_types.h b/source/blender/makesdna/DNA_asset_types.h
index 5acdc4ee862..9b17bca2ab6 100644
--- a/source/blender/makesdna/DNA_asset_types.h
+++ b/source/blender/makesdna/DNA_asset_types.h
@@ -97,6 +97,17 @@ typedef struct AssetLibraryReference {
   int custom_library_index;
 } AssetLibraryReference;
 
+/**
+ * Not part of the core design, we should try to get rid of it. Only needed to wrap FileDirEntry
+ * into a type with PropertyGroup as base, so we can have an RNA collection of #AssetHandle's to
+ * pass to the UI.
+ */
+#
+#
+typedef struct AssetHandle {
+  struct FileDirEntry *file_data;
+} AssetHandle;
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h
index 52bf80ee45b..97615016016 100644
--- a/source/blender/makesrna/RNA_access.h
+++ b/source/blender/makesrna/RNA_access.h
@@ -71,6 +71,7 @@ extern StructRNA RNA_ArrayGpencilModifier;
 extern StructRNA RNA_ArrayModifier;
 extern StructRNA RNA_Attribute;
 extern StructRNA RNA_AttributeGroup;
+extern StructRNA RNA_AssetHandle;
 extern StructRNA RNA_AssetLibraryReference;
 extern StructRNA RNA_AssetMetaData;
 extern StructRNA RNA_AssetTag;
diff --git a/source/blender/makesrna/intern/rna_asset.c b/source/blender/makesrna/intern/rna_asset.c
index 54cc87b4eea..82430d3e174 100644
--- a/source/blender/makesrna/intern/rna_asset.c
+++ b/source/blender/makesrna/intern/rna_asset.c
@@ -125,6 +125,20 @@ static void rna_AssetMetaData_active_tag_range(
   *max = *softmax = MAX2(asset_data->tot_tags - 1, 0);
 }
 
+static PointerRNA rna_AssetHandle_file_data_get(PointerRNA *ptr)
+{
+  AssetHandle *asset_handle = ptr->data;
+  return rna_pointer_inherit_refine(ptr, &RNA_FileSelectEntry, asset_handle->file_data);
+}
+
+static void rna_AssetHandle_file_data_set(PointerRNA *ptr,
+                                          PointerRNA value,
+                                          struct ReportList *UNUSED(reports))
+{
+  AssetHandle *asset_handle = ptr->data;
+  asset_handle->file_data = value.data;
+}
+
 int rna_asset_library_reference_get(const AssetLibraryReference *library)
 {
   return ED_asset_library_reference_to_enum_value(library);
@@ -278,6 +292,23 @@ 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(BlenderRNA *brna)
+{
+  StructRNA *srna;
+  PropertyRNA *prop;
+
+  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");
+}
+
 static void rna_def_asset_library_reference(BlenderRNA *brna)
 {
   StructRNA *srna = RNA_def_struct(brna, "AssetLibraryReference", NULL);
@@ -306,6 +337,7 @@ void RNA_def_asset(BlenderRNA *brna)
   rna_def_asset_tag(brna);
   rna_def_asset_data(brna);
   rna_def_asset_library_reference(brna);
+  rna_def_asset_handle(brna);
 
   RNA_define_animate_sdna(true);
 }
diff --git a/source/blender/makesrna/intern/rna_context.c b/source/blender/makesrna/intern/rna_context.c
index 4079406e64b..9da08de2168 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);



More information about the Bf-blender-cvs mailing list