[Bf-blender-cvs] [5a94ab9ac27] ui-asset-view-template: Allow getting asset library and active asset handle via context

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


Commit: 5a94ab9ac270e1dd97ad6772fdb82859cea6cce2
Author: Julian Eisel
Date:   Wed Mar 24 12:58:17 2021 +0100
Branches: ui-asset-view-template
https://developer.blender.org/rB5a94ab9ac270e1dd97ad6772fdb82859cea6cce2

Allow getting asset library and active asset handle via context

`CTX_wm_asset_library()` returns the active asset library from the workspace,
or the the Asset Browser one while inside that. The active asset handle is set
by the Asset Browser and the asset view UI template so that operators executed
via the template can access it.

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

M	source/blender/blenkernel/BKE_context.h
M	source/blender/blenkernel/intern/context.c
M	source/blender/editors/asset/asset_list.cc
M	source/blender/editors/include/ED_asset.h
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/interface/interface_templates.c
M	source/blender/editors/screen/screen_context.c
M	source/blender/editors/space_file/space_file.c
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_asset.c

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

diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h
index 3d30188e517..3bb4ad60007 100644
--- a/source/blender/blenkernel/BKE_context.h
+++ b/source/blender/blenkernel/BKE_context.h
@@ -340,6 +340,8 @@ int CTX_data_visible_gpencil_layers(const bContext *C, ListBase *list);
 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);
+
 bool CTX_wm_interface_locked(const bContext *C);
 
 /* Gets pointer to the dependency graph.
diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c
index cbf7a4483c0..741db29d49b 100644
--- a/source/blender/blenkernel/intern/context.c
+++ b/source/blender/blenkernel/intern/context.c
@@ -1398,6 +1398,11 @@ int CTX_data_editable_gpencil_strokes(const bContext *C, ListBase *list)
   return ctx_data_collection_get(C, "editable_gpencil_strokes", list);
 }
 
+const AssetLibraryReference *CTX_wm_asset_library(const bContext *C)
+{
+  return ctx_data_pointer_get(C, "asset_library");
+}
+
 Depsgraph *CTX_data_depsgraph_pointer(const bContext *C)
 {
   Main *bmain = CTX_data_main(C);
diff --git a/source/blender/editors/asset/asset_list.cc b/source/blender/editors/asset/asset_list.cc
index 9499d487eb7..903c4fdbf17 100644
--- a/source/blender/editors/asset/asset_list.cc
+++ b/source/blender/editors/asset/asset_list.cc
@@ -472,14 +472,14 @@ void ED_assetlist_iterate(const AssetLibraryReference *library_reference, AssetL
   }
 }
 
-ImBuf *ED_assetlist_asset_image_get(const FileDirEntry *file)
+ImBuf *ED_assetlist_asset_image_get(const AssetHandle *asset_handle)
 {
-  ImBuf *imbuf = filelist_file_getimage(file);
+  ImBuf *imbuf = filelist_file_getimage(asset_handle->file_data);
   if (imbuf) {
     return imbuf;
   }
 
-  return filelist_geticon_image_ex(file);
+  return filelist_geticon_image_ex(asset_handle->file_data);
 }
 
 const char *ED_assetlist_library_path(const AssetLibraryReference *library_reference)
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h
index 4fbd6aac931..45ef7c42386 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/include/ED_asset.h
@@ -48,8 +48,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);
 
-struct FileDirEntry;
-struct ImBuf *ED_assetlist_asset_image_get(const struct FileDirEntry *file);
+struct ImBuf *ED_assetlist_asset_image_get(const AssetHandle *asset_handle);
 const char *ED_assetlist_library_path(const struct AssetLibraryReference *library_reference);
 
 bool ED_assetlist_listen(const struct AssetLibraryReference *library_reference,
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
index 53ada395707..ff91b344c38 100644
--- a/source/blender/editors/interface/interface_template_asset_view.cc
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -49,9 +49,9 @@ struct AssetViewListData {
 
 static void asset_view_item_but_drag_set(uiBut *but,
                                          AssetViewListData *list_data,
-                                         FileDirEntry *file)
+                                         AssetHandle *asset_handle)
 {
-  if (ID *id = file->id) {
+  if (ID *id = asset_handle->file_data->id) {
     UI_but_drag_set_id(but, id);
   }
   else {
@@ -60,14 +60,14 @@ static void asset_view_item_but_drag_set(uiBut *but,
     char blend_path[FILE_MAX_LIBEXTRA];
 
     char path[FILE_MAX_LIBEXTRA];
-    BLI_join_dirfile(path, sizeof(path), asset_list_path.data(), file->relpath);
+    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(file);
+      ImBuf *imbuf = ED_assetlist_asset_image_get(asset_handle);
       UI_but_drag_set_asset(but,
-                            file->name,
+                            asset_handle->file_data->name,
                             BLI_strdup(blend_path),
-                            file->blentype,
-                            file->preview_icon_id,
+                            asset_handle->file_data->blentype,
+                            asset_handle->file_data->preview_icon_id,
                             imbuf,
                             1.0f);
     }
@@ -88,14 +88,9 @@ static void asset_view_draw_item(uiList *ui_list,
   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;
 
-  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(&fileptr, nameprop, str);
+  uiLayoutSetContextPointer(layout, "asset_handle", itemptr);
 
   uiBlock *block = uiLayoutGetBlock(layout);
   /* TODO ED_fileselect_init_layout(). Share somehow? */
@@ -104,8 +99,8 @@ static void asset_view_draw_item(uiList *ui_list,
   uiBut *but = uiDefIconTextBut(block,
                                 UI_BTYPE_PREVIEW_TILE,
                                 0,
-                                file->preview_icon_id,
-                                file->name,
+                                asset_handle->file_data->preview_icon_id,
+                                asset_handle->file_data->name,
                                 0,
                                 0,
                                 size_x,
@@ -117,10 +112,10 @@ static void asset_view_draw_item(uiList *ui_list,
                                 0,
                                 "");
   ui_def_but_icon(but,
-                  file->preview_icon_id,
+                  asset_handle->file_data->preview_icon_id,
                   /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */
                   UI_HAS_ICON | UI_BUT_ICON_PREVIEW);
-  asset_view_item_but_drag_set(but, list_data, file);
+  asset_view_item_but_drag_set(but, list_data, asset_handle);
 }
 
 static void asset_view_listener(uiList *ui_list, wmRegionListenerParams *params)
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index 6f318bd5789..c6ca097f295 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -6572,6 +6572,10 @@ static void ui_template_list_layout_draw(bContext *C,
                                  org_i,
                                  flt_flag);
 
+          /* Items should be able to set context pointers for the layout. But the list-row button
+           * swallows events, so it needs the context storage too for handlers to see it. */
+          but->context = uiLayoutGetContextStore(col);
+
           /* If we are "drawing" active item, set all labels as active. */
           if (i == items->active_item_idx) {
             ui_layout_list_set_labels_active(col);
diff --git a/source/blender/editors/screen/screen_context.c b/source/blender/editors/screen/screen_context.c
index 627a67358f2..fb1641025c8 100644
--- a/source/blender/editors/screen/screen_context.c
+++ b/source/blender/editors/screen/screen_context.c
@@ -36,6 +36,7 @@
 #include "DNA_sequence_types.h"
 #include "DNA_space_types.h"
 #include "DNA_windowmanager_types.h"
+#include "DNA_workspace_types.h"
 
 #include "BLI_ghash.h"
 #include "BLI_listbase.h"
@@ -111,6 +112,7 @@ const char *screen_context_dir[] = {
     "selected_editable_fcurves",
     "active_editable_fcurve",
     "selected_editable_keyframes",
+    "asset_library",
     NULL,
 };
 
@@ -1024,6 +1026,14 @@ static eContextResult screen_ctx_selected_editable_keyframes(const bContext *C,
   return CTX_RESULT_NO_DATA;
 }
 
+static eContextResult screen_ctx_asset_library(const bContext *C, bContextDataResult *result)
+{
+  WorkSpace *workspace = CTX_wm_workspace(C);
+  CTX_data_pointer_set(
+      result, &workspace->id, &RNA_AssetLibraryReference, &workspace->active_asset_library);
+  return CTX_RESULT_OK;
+}
+
 /* Registry of context callback functions. */
 
 typedef eContextResult (*context_callback)(const bContext *C, bContextDataResult *result);
@@ -1098,6 +1108,7 @@ static void ensure_ed_screen_context_functions(void)
   register_context_function("selected_visible_fcurves", screen_ctx_selected_visible_fcurves);
   register_context_function("active_editable_fcurve", screen_ctx_active_editable_fcurve);
   register_context_function("selected_editable_keyframes", screen_ctx_selected_editable_keyframes);
+  register_context_function("asset_library", screen_ctx_asset_library);
 }
 
 /* Entry point for the screen context. */
diff --git a/source/blender/editors/space_file/space_file.c b/source/blender/editors/space_file/space_file.c
index cf308f7e28f..fc15d2e6a3d 100644
--- a/source/blender/editors/space_file/space_file.c
+++ b/source/blender/editors/space_file/space_file.c
@@ -847,7 +847,13 @@ static void file_space_subtype_item_extend(bContext *UNUSED(C),
   }
 }
 
-static const char *file_context_dir[] = {"active_file", "id", NULL};
+static const char *file_context_dir[] = {
+    "active_file",
+    "active_handle",
+    "active_library",
+    "id",
+    NULL,
+};
 
 static int /*eContextResult*/ file_context(const bContext *C,
                                            const char *member,
@@ -878,6 +884,34 @@ static int /*eContextResult*/ file_context(const bContext *C,
     CTX_data_pointer_set(result, &screen->id, &RNA_FileSelectEntry, file);
     return CTX_RESULT_OK;
   }
+  /* TODO this kind of handle is very weak. We need something proper that doesn't depend on file
+   * data. */
+  if (CTX_data_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list