[Bf-blender-cvs] [8925d3b7bf9] master: UI/Assets: Initial Asset View UI template

Julian Eisel noreply at git.blender.org
Thu Jul 15 16:14:27 CEST 2021


Commit: 8925d3b7bf989e1db364d989fbd0c6cdb9a2acdb
Author: Julian Eisel
Date:   Tue Jul 13 18:07:57 2021 +0200
Branches: master
https://developer.blender.org/rB8925d3b7bf989e1db364d989fbd0c6cdb9a2acdb

UI/Assets: Initial Asset View UI template

The asset view UI template is a mini-version of the Asset Browser that
can be placed in regular layouts, regions or popups. At this point it's
made specifically for placement in vertical layouts, it can be made more
flexible in the future.
Generally the way this is implemented will likely change a lot still as
the asset system evolves.

The Pose Library add-on will use the asset view to display pose
libraries in the 3D View sidebar.

References:
* https://developer.blender.org/T86139
* https://code.blender.org/2021/06/asset-browser-project-update/#what-are-we-building
* https://code.blender.org/2021/05/pose-library-v2-0/#use-from-3d-viewport

Notes:
* Important limitation: Due to the early & WIP implementation of the
  asset list, all asset views showing the same library will show the
  same assets. That is despite the ID type filter option the template
  provides. The first asset view created will determine what's visible.
  Of course this should be made to work eventually.
* The template supports passing an activate and a drag operator name.
  The former is called when an asset is clicked on (e.g. to apply the
  asset) the latter when dragging (e.g. to .blend a pose asset). If no
  drag operator is set, regular asset drag & drop will be executed.
* The template returns the properties for both operators (see example
  below).
* The argument list for using the template is quite long, but we can't
  avoid that currently. The UI list design requires that we pass a
  number of RNA or custom properties to work with, that for the Pose
  Libraries should be registered at the Pose Library add-on level, not
  in core Blender.
* Idea is that Python scripts or add-ons that want to use the asset view
  can register custom properties, to hold data like the list of assets,
  and the active asset index. Maybe that will change in future and we
  can manage these internally.

As an example, the pose library add-on uses it like this:
```
activate_op_props, drag_op_props = layout.template_asset_view(
    "pose_assets",
    workspace,
    "active_asset_library",
    wm,
    "pose_assets",
    workspace,
    "active_pose_asset_index",
    filter_id_types={"filter_action"},
    activate_operator="poselib.apply_pose_asset",
    drag_operator="poselib.blend_pose_asset",
)
drag_op_props.release_confirm = True
drag_op_props.flipped = wm.poselib_flipped
activate_op_props.flipped = wm.poselib_flipped
```

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

M	source/blender/blenkernel/BKE_screen.h
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/CMakeLists.txt
M	source/blender/editors/interface/interface_intern.h
A	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/interface/interface_template_list.cc
M	source/blender/editors/screen/area.c
M	source/blender/makesrna/RNA_enum_types.h
M	source/blender/makesrna/intern/rna_ID.c
M	source/blender/makesrna/intern/rna_space.c
M	source/blender/makesrna/intern/rna_ui_api.c

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

diff --git a/source/blender/blenkernel/BKE_screen.h b/source/blender/blenkernel/BKE_screen.h
index fed155626ed..0b08bbfeff5 100644
--- a/source/blender/blenkernel/BKE_screen.h
+++ b/source/blender/blenkernel/BKE_screen.h
@@ -332,6 +332,9 @@ typedef void (*uiListFilterItemsFunc)(struct uiList *ui_list,
                                       struct PointerRNA *,
                                       const char *propname);
 
+/* Listen to notifiers. Only for lists defined in C. */
+typedef void (*uiListListener)(struct uiList *ui_list, wmRegionListenerParams *params);
+
 typedef struct uiListType {
   struct uiListType *next, *prev;
 
@@ -341,6 +344,9 @@ typedef struct uiListType {
   uiListDrawFilterFunc draw_filter;
   uiListFilterItemsFunc filter_items;
 
+  /* For lists defined in C only. */
+  uiListListener listener;
+
   /* RNA integration */
   ExtensionRNA rna_ext;
 } uiListType;
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index 020ca1f8f55..a25aac5803c 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -35,6 +35,7 @@ extern "C" {
 /* Struct Declarations */
 
 struct ARegion;
+struct AssetFilterSettings;
 struct AutoComplete;
 struct EnumPropertyItem;
 struct FileSelectParams;
@@ -2287,6 +2288,20 @@ int uiTemplateRecentFiles(struct uiLayout *layout, int rows);
 void uiTemplateFileSelectPath(uiLayout *layout,
                               struct bContext *C,
                               struct FileSelectParams *params);
+void uiTemplateAssetView(struct uiLayout *layout,
+                         struct bContext *C,
+                         const char *list_id,
+                         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,
+                         const char *activate_opname,
+                         struct PointerRNA *r_activate_op_properties,
+                         const char *drag_opname,
+                         struct PointerRNA *r_drag_op_properties);
 
 struct PointerRNA *UI_list_custom_activate_operator_set(struct uiList *ui_list,
                                                         const char *opname,
diff --git a/source/blender/editors/interface/CMakeLists.txt b/source/blender/editors/interface/CMakeLists.txt
index 29f868eebb0..39dd6143eb9 100644
--- a/source/blender/editors/interface/CMakeLists.txt
+++ b/source/blender/editors/interface/CMakeLists.txt
@@ -66,6 +66,7 @@ set(SRC
   interface_region_tooltip.c
   interface_regions.c
   interface_style.c
+  interface_template_asset_view.cc
   interface_template_list.cc
   interface_template_search_menu.c
   interface_template_search_operator.c
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h
index f3085c6b79b..a07f924e65b 100644
--- a/source/blender/editors/interface/interface_intern.h
+++ b/source/blender/editors/interface/interface_intern.h
@@ -1241,6 +1241,9 @@ void UI_OT_eyedropper_driver(struct wmOperatorType *ot);
 /* interface_eyedropper_gpencil_color.c */
 void UI_OT_eyedropper_gpencil_color(struct wmOperatorType *ot);
 
+/* interface_template_asset_view.cc */
+struct uiListType *UI_UL_asset_view(void);
+
 /**
  * For use with #ui_rna_collection_search_update_fn.
  */
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
new file mode 100644
index 00000000000..2860abb32a1
--- /dev/null
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -0,0 +1,272 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup edinterface
+ */
+
+#include "DNA_space_types.h"
+#include "DNA_userdef_types.h"
+
+#include "BKE_screen.h"
+
+#include "BLI_path_util.h"
+#include "BLI_string.h"
+#include "BLI_string_ref.hh"
+
+#include "BLO_readfile.h"
+
+#include "ED_asset.h"
+#include "ED_screen.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "RNA_access.h"
+
+#include "UI_interface.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "interface_intern.h"
+
+struct AssetViewListData {
+  AssetLibraryReference asset_library;
+  bScreen *screen;
+};
+
+static void asset_view_item_but_drag_set(uiBut *but,
+                                         AssetViewListData *list_data,
+                                         AssetHandle *asset_handle)
+{
+  ID *id = asset_handle->file_data->id;
+  if (id != NULL) {
+    UI_but_drag_set_id(but, id);
+    return;
+  }
+
+  const blender::StringRef asset_list_path = ED_assetlist_library_path(&list_data->asset_library);
+  char blend_path[FILE_MAX_LIBEXTRA];
+
+  char path[FILE_MAX_LIBEXTRA];
+  BLI_join_dirfile(path, sizeof(path), asset_list_path.data(), asset_handle->file_data->relpath);
+  if (BLO_library_path_explode(path, blend_path, nullptr, nullptr)) {
+    ImBuf *imbuf = ED_assetlist_asset_image_get(asset_handle);
+    UI_but_drag_set_asset(but,
+                          asset_handle->file_data->name,
+                          BLI_strdup(blend_path),
+                          asset_handle->file_data->blentype,
+                          FILE_ASSET_IMPORT_APPEND,
+                          asset_handle->file_data->preview_icon_id,
+                          imbuf,
+                          1.0f);
+  }
+}
+
+static void asset_view_draw_item(uiList *ui_list,
+                                 bContext *UNUSED(C),
+                                 uiLayout *layout,
+                                 PointerRNA *UNUSED(dataptr),
+                                 PointerRNA *itemptr,
+                                 int UNUSED(icon),
+                                 PointerRNA *UNUSED(active_dataptr),
+                                 const char *UNUSED(active_propname),
+                                 int UNUSED(index),
+                                 int UNUSED(flt_flag))
+{
+  AssetViewListData *list_data = (AssetViewListData *)ui_list->dyn_data->customdata;
+
+  BLI_assert(RNA_struct_is_a(itemptr->type, &RNA_AssetHandle));
+  AssetHandle *asset_handle = (AssetHandle *)itemptr->data;
+
+  uiLayoutSetContextPointer(layout, "asset_handle", itemptr);
+
+  uiBlock *block = uiLayoutGetBlock(layout);
+  /* TODO ED_fileselect_init_layout(). Share somehow? */
+  const float size_x = (96.0f / 20.0f) * UI_UNIT_X;
+  const float size_y = (96.0f / 20.0f) * UI_UNIT_Y;
+  uiBut *but = uiDefIconTextBut(block,
+                                UI_BTYPE_PREVIEW_TILE,
+                                0,
+                                asset_handle->file_data->preview_icon_id,
+                                asset_handle->file_data->name,
+                                0,
+                                0,
+                                size_x,
+                                size_y,
+                                nullptr,
+                                0,
+                                0,
+                                0,
+                                0,
+                                "");
+  ui_def_but_icon(but,
+                  asset_handle->file_data->preview_icon_id,
+                  /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */
+                  UI_HAS_ICON | UI_BUT_ICON_PREVIEW);
+  if (!ui_list->dyn_data->custom_drag_optype) {
+    asset_view_item_but_drag_set(but, list_data, asset_handle);
+  }
+}
+
+static void asset_view_listener(uiList *ui_list, wmRegionListenerParams *params)
+{
+  AssetViewListData *list_data = (AssetViewListData *)ui_list->dyn_data->customdata;
+  const wmNotifier *notifier = params->notifier;
+
+  switch (notifier->category) {
+    case NC_ID: {
+      if (ELEM(notifier->action, NA_RENAME)) {
+        ED_assetlist_storage_tag_main_data_dirty();
+      }
+      break;
+    }
+  }
+
+  if (ED_assetlist_listen(&list_data->asset_library, params->notifier)) {
+    ED_region_tag_redraw(params->region);
+  }
+}
+
+uiListType *UI_UL_asset_view()
+{
+  uiListType *list_type = (uiListType *)MEM_callocN(sizeof(*list_type), __func__);
+
+  BLI_strncpy(list_type->idname, "UI_UL_asset_view", sizeof(list_type->idname));
+  list_type->draw_item = asset_view_draw_item;
+  list_type->listener = asset_view_listener;
+
+  return list_type;
+}
+
+static void asset_view_template_refresh_asset_collection(
+    const AssetLibraryReference &asset_library,
+    PointerRNA &assets_dataptr,
+    const char *assets_propname)
+{
+  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_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list