[Bf-blender-cvs] [02ccd5931a0] ui-asset-view-template: Use custom collection property to pass the asset list to UI lists

Julian Eisel noreply at git.blender.org
Wed Mar 17 20:51:12 CET 2021


Commit: 02ccd5931a0f97ae3711d21aea7a50edd62579c9
Author: Julian Eisel
Date:   Wed Mar 17 20:46:47 2021 +0100
Branches: ui-asset-view-template
https://developer.blender.org/rB02ccd5931a0f97ae3711d21aea7a50edd62579c9

Use custom collection property to pass the asset list to UI lists

`UIList` is designed to use collection properties. I previously did a
whole bunch of hacks to use it without. This isn't acceptable for
master, instead a proper collection property should be provided.

Idea is to let scripts register a custom collection property, that the
asset view template can fill with the latest asset list contents and
pass that to the UI list.

Longer term I'd prefer if the UI code would support other containers,
and not rely so much on RNA and custom/ID properties. I have ideas and
plans for this, but meanwhile, this will do to remove the hacks.

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

M	release/scripts/startup/bl_ui/__init__.py
M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/interface/interface_templates.c
M	source/blender/makesdna/DNA_asset_types.h
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/makesrna.c
M	source/blender/makesrna/intern/rna_asset.c
M	source/blender/makesrna/intern/rna_ui_api.c

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

diff --git a/release/scripts/startup/bl_ui/__init__.py b/release/scripts/startup/bl_ui/__init__.py
index 2fd7eac79e1..aac972714ef 100644
--- a/release/scripts/startup/bl_ui/__init__.py
+++ b/release/scripts/startup/bl_ui/__init__.py
@@ -117,11 +117,16 @@ def register():
             register_class(cls)
 
     from bpy.props import (
+        CollectionProperty,
         EnumProperty,
         IntProperty,
         StringProperty,
     )
-    from bpy.types import WorkSpace, WindowManager
+    from bpy.types import (
+        AssetHandle,
+        WindowManager,
+        WorkSpace,
+    )
 
     # space_userprefs.py
     def addon_filter_items(_self, _context):
@@ -171,6 +176,11 @@ def register():
         # TODO explain which list the index belongs to, or how it can be used to get the pose.
         description="Per workspace index of the active pose asset"
     )
+    # Register for window-manager. This is a global property that shouldn't be
+    # written to files.
+    WindowManager.pose_assets = CollectionProperty(
+        type=AssetHandle
+    )
     # done...
 
 
diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 95c6a59ea7c..4179de75a06 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -7015,9 +7015,10 @@ class VIEW3D_PT_asset_testing(Panel):
     def draw(self, context):
         layout = self.layout
 
+        wm = context.window_manager
         workspace = context.workspace
 
-        layout.template_asset_view(workspace, "active_asset_library", workspace, "active_pose_asset_index")
+        layout.template_asset_view(workspace, "active_asset_library", wm, "pose_assets", workspace, "active_pose_asset_index")
 
 
 # Grease Pencil Object - Multiframe falloff tools
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index e44dd762977..d34cd92280e 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -2163,17 +2163,8 @@ void uiTemplateList(uiLayout *layout,
                     int columns,
                     bool sort_reverse,
                     bool sort_lock);
-struct TemplateListIterData;
-typedef void (*uiTemplateListItemIterFn)(struct TemplateListIterData *iter_data,
-                                         struct PointerRNA *itemptr);
-typedef void (*uiTemplateListItemsIterFn)(struct PointerRNA *dataptr,
-                                          struct PropertyRNA *prop,
-                                          struct TemplateListIterData *iter_data,
-                                          uiTemplateListItemIterFn fn,
-                                          void *customdata);
 struct uiList *uiTemplateList_ex(uiLayout *layout,
                                  struct bContext *C,
-                                 uiTemplateListItemsIterFn iter_items,
                                  const char *listtype_name,
                                  const char *list_id,
                                  struct PointerRNA *dataptr,
@@ -2237,6 +2228,8 @@ void uiTemplateAssetView(struct uiLayout *layout,
                          struct bContext *C,
                          struct PointerRNA *asset_library_dataptr,
                          const char *asset_library_propname,
+                         struct PointerRNA *assets_dataptr,
+                         const char *assets_propname,
                          struct PointerRNA *active_dataptr,
                          const char *active_propname,
                          const struct AssetFilterSettings *filter_settings);
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
index 8470a6904e2..6bea3ce126e 100644
--- a/source/blender/editors/interface/interface_template_asset_view.cc
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -87,11 +87,16 @@ static void asset_view_draw_item(uiList *ui_list,
 {
   AssetViewListData *list_data = (AssetViewListData *)ui_list->dyn_data->customdata;
 
-  PropertyRNA *nameprop = RNA_struct_name_property(itemptr->type);
+  BLI_assert(RNA_struct_is_a(itemptr->type, &RNA_AssetHandle));
+
+  PropertyRNA *file_data_prop = RNA_struct_find_property(itemptr, "file_data");
+  PointerRNA fileptr = RNA_property_pointer_get(itemptr, file_data_prop);
+  FileDirEntry *file = (FileDirEntry *)fileptr.data;
+
+  PropertyRNA *nameprop = RNA_struct_name_property(fileptr.type);
   char str[MAX_NAME];
-  RNA_property_string_get(itemptr, nameprop, str);
+  RNA_property_string_get(&fileptr, nameprop, str);
 
-  FileDirEntry *file = (FileDirEntry *)itemptr->data;
   uiBlock *block = uiLayoutGetBlock(layout);
   /* TODO ED_fileselect_init_layout(). Share somehow? */
   float size_x = (96.0f / 20.0f) * UI_UNIT_X;
@@ -140,17 +145,38 @@ void ED_uilisttypes_ui()
   WM_uilisttype_add(UI_UL_asset_view());
 }
 
-static void asset_view_template_list_item_iter_fn(PointerRNA *UNUSED(dataptr),
-                                                  PropertyRNA *UNUSED(prop),
-                                                  TemplateListIterData *iter_data,
-                                                  uiTemplateListItemIterFn fn,
-                                                  void *customdata)
+static void asset_view_template_refresh_asset_collection(
+    const AssetLibraryReference &asset_library,
+    PointerRNA &assets_dataptr,
+    const char *assets_propname)
 {
-  AssetViewListData *asset_iter_data = (AssetViewListData *)customdata;
-  ED_assetlist_iterate(&asset_iter_data->asset_library, [&](FileDirEntry &file) {
-    PointerRNA itemptr;
-    RNA_pointer_create(&asset_iter_data->screen->id, &RNA_FileSelectEntry, &file, &itemptr);
-    fn(iter_data, &itemptr);
+  PropertyRNA *assets_prop = RNA_struct_find_property(&assets_dataptr, assets_propname);
+  if (!assets_prop) {
+    RNA_warning("Asset collection not found");
+    return;
+  }
+  if (!RNA_struct_is_a(RNA_property_pointer_type(&assets_dataptr, assets_prop),
+                       &RNA_AssetHandle)) {
+    RNA_warning("Expected a collection property for AssetHandle items");
+    return;
+  }
+
+  RNA_property_collection_clear(&assets_dataptr, assets_prop);
+
+  ED_assetlist_iterate(&asset_library, [&](FileDirEntry &file) {
+    PointerRNA itemptr, fileptr;
+    RNA_property_collection_add(&assets_dataptr, assets_prop, &itemptr);
+
+    RNA_pointer_create(nullptr, &RNA_FileSelectEntry, &file, &fileptr);
+    RNA_pointer_set(&itemptr, "file_data", fileptr);
+
+    /* Copy name from file to asset-handle name ID-property. */
+    char name[MAX_NAME];
+    PropertyRNA *file_name_prop = RNA_struct_name_property(fileptr.type);
+    RNA_property_string_get(&fileptr, file_name_prop, name);
+    PropertyRNA *asset_name_prop = RNA_struct_name_property(&RNA_AssetHandle);
+    RNA_property_string_set(&itemptr, asset_name_prop, name);
+
     return true;
   });
 }
@@ -159,6 +185,8 @@ void uiTemplateAssetView(uiLayout *layout,
                          bContext *C,
                          PointerRNA *asset_library_dataptr,
                          const char *asset_library_propname,
+                         PointerRNA *assets_dataptr,
+                         const char *assets_propname,
                          PointerRNA *active_dataptr,
                          const char *active_propname,
                          const AssetFilterSettings *filter_settings)
@@ -174,21 +202,21 @@ void uiTemplateAssetView(uiLayout *layout,
   ED_assetlist_fetch(&asset_library, filter_settings, C);
   ED_assetlist_ensure_previews_job(&asset_library, C);
 
+  asset_view_template_refresh_asset_collection(asset_library, *assets_dataptr, assets_propname);
+
   AssetViewListData *list_data = (AssetViewListData *)MEM_mallocN(sizeof(*list_data),
                                                                   "AssetViewListData");
   list_data->asset_library = asset_library;
   list_data->screen = CTX_wm_screen(C);
 
-  PointerRNA rna_nullptr = PointerRNA_NULL;
   /* TODO can we have some kind of model-view API to handle referencing, filtering and lazy loading
    * (of previews) of the items? */
   uiList *list = uiTemplateList_ex(col,
                                    C,
-                                   asset_view_template_list_item_iter_fn,
                                    "UI_UL_asset_view",
                                    "asset_view",
-                                   &rna_nullptr,
-                                   "",
+                                   assets_dataptr,
+                                   assets_propname,
                                    active_dataptr,
                                    active_propname,
                                    nullptr,
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index a6a1deb80bd..3865a1140cc 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -5817,6 +5817,9 @@ typedef struct {
   int active_item_idx;
 } TemplateListInputData;
 
+/**
+ * Internal wrapper for a single item in the list (well, actually stored as a vector).
+ */
 typedef struct {
   PointerRNA item;
   int org_idx;
@@ -5849,24 +5852,6 @@ typedef struct {
   int end_idx;      /* Index of last item to display + 1. */
 } TemplateListVisualInfo;
 
-/**
- * For internal use only, but external users that set the iterator-callback via #uiTemplate
- */
-typedef struct TemplateListIterData {
-  uiListDyn *dyn_data;
-
-  _uilist_item *items_ptr;
-
-  int filter_exclude;
-  bool order_reverse;
-  bool activei_mapping_pending;
-  int activei;
-
-  /* Just for the callback */
-  int current_iter_idx;
-  int reorder_idx;
-} TemplateListIterData;
-
 /**
  * Validate input parameters and initialize \a r_data from that. Plus find the list-type and return
  * it in \a r_list_type.
@@ -5939,44 +5924,43 @@ static bool ui_template_list_data_retrieve(const char *listtype_name,
   return true;
 }
 
-sta

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list