[Bf-blender-cvs] [bd04c4aef2a] node-add-asset-menu: Also add items to existing menus

Hans Goudey noreply at git.blender.org
Thu Oct 6 21:38:24 CEST 2022


Commit: bd04c4aef2aa007d55084e049e8575354ea5344d
Author: Hans Goudey
Date:   Thu Oct 6 14:38:15 2022 -0500
Branches: node-add-asset-menu
https://developer.blender.org/rBbd04c4aef2aa007d55084e049e8575354ea5344d

Also add items to existing menus

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

M	release/scripts/startup/bl_ui/node_add_menu.py
M	source/blender/blenkernel/BKE_asset_catalog.hh
M	source/blender/blenkernel/intern/asset_catalog.cc
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/space_node/add_menu_assets.cc
M	source/blender/makesrna/intern/rna_ui_api.c

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

diff --git a/release/scripts/startup/bl_ui/node_add_menu.py b/release/scripts/startup/bl_ui/node_add_menu.py
index debed301904..d393dc04963 100644
--- a/release/scripts/startup/bl_ui/node_add_menu.py
+++ b/release/scripts/startup/bl_ui/node_add_menu.py
@@ -58,8 +58,8 @@ def draw_node_group_add_menu(context, layout):
                 ops.name = "node_tree"
                 ops.value = "bpy.data.node_groups[%r]" % group.name
 
-def draw_assets_for_catalog(layout, catalog):
-    layout.menu_contents("NODE_MT_node_add_catalog_assets")
+def draw_assets_for_catalog(layout, catalog_path):
+    layout.template_node_asset_menu_items(catalog_path=catalog_path)
 
 def draw_root_assets(layout):
     layout.menu_contents("NODE_MT_node_add_root_catalogs")
diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh
index 0ca2b27b5a0..18f0debfd6c 100644
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@ -357,6 +357,7 @@ class AssetCatalogTree {
   bool is_empty() const;
 
   AssetCatalogTreeItem *find_item(const AssetCatalogPath &path);
+  AssetCatalogTreeItem *find_root_item(const AssetCatalogPath &path);
 
  protected:
   /** Child tree items, ordered by their names. */
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index c69deac059e..b9fe7469ead 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -821,7 +821,22 @@ AssetCatalogTreeItem *AssetCatalogTree::find_item(const AssetCatalogPath &path)
   AssetCatalogTreeItem *result = nullptr;
   this->foreach_item([&](AssetCatalogTreeItem &item) {
     if (result) {
-      /* TODO: Add a way to stop iteration. */
+      /* There is no way to stop iteration. */
+      return;
+    }
+    if (item.catalog_path() == path) {
+      result = &item;
+    }
+  });
+  return result;
+}
+
+AssetCatalogTreeItem *AssetCatalogTree::find_root_item(const AssetCatalogPath &path)
+{
+  AssetCatalogTreeItem *result = nullptr;
+  this->foreach_root_item([&](AssetCatalogTreeItem &item) {
+    if (result) {
+      /* There is no way to stop iteration. */
       return;
     }
     if (item.catalog_path() == path) {
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index 2a1941f0d9e..9f378644b9a 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -2524,6 +2524,7 @@ void uiTemplateNodeView(uiLayout *layout,
                         struct bNodeTree *ntree,
                         struct bNode *node,
                         struct bNodeSocket *input);
+void uiTemplateNodeAssetMenuItems(uiLayout *layout, struct bContext *C, const char *catalog_path);
 void uiTemplateTextureUser(uiLayout *layout, struct bContext *C);
 /**
  * Button to quickly show texture in Properties Editor texture tab.
diff --git a/source/blender/editors/space_node/add_menu_assets.cc b/source/blender/editors/space_node/add_menu_assets.cc
index d20eb635bd3..8fd3bfcd1f6 100644
--- a/source/blender/editors/space_node/add_menu_assets.cc
+++ b/source/blender/editors/space_node/add_menu_assets.cc
@@ -215,4 +215,25 @@ MenuType add_root_catalogs_menu_type()
   return type;
 }
 
-}  // namespace blender::ed::space_node
\ No newline at end of file
+}  // namespace blender::ed::space_node
+
+/* Note: This is only necessary because Python can't set an asset catalog path context item. */
+void uiTemplateNodeAssetMenuItems(uiLayout *layout, bContext *C, const char *catalog_path)
+{
+  using namespace blender;
+  using namespace blender::ed::space_node;
+  bScreen &screen = *CTX_wm_screen(C);
+  SpaceNode &snode = *CTX_wm_space_node(C);
+  AssetItemTree &tree = *snode.runtime->assets_for_menu;
+  const bke::AssetCatalogTreeItem *item = tree.catalogs.find_root_item(catalog_path);
+  if (!item) {
+    return;
+  }
+  const bke::AssetCatalogPath &path = tree.full_catalog_per_tree_item.lookup(item);
+  PointerRNA path_ptr{
+      &screen.id, &RNA_AssetCatalogPath, const_cast<bke::AssetCatalogPath *>(&path)};
+  uiItemS(layout);
+  uiLayout *row = uiLayoutRow(layout, false);
+  uiLayoutSetContextPointer(row, "asset_catalog_path", &path_ptr);
+  uiItemMContents(row, "NODE_MT_node_add_catalog_assets");
+}
diff --git a/source/blender/makesrna/intern/rna_ui_api.c b/source/blender/makesrna/intern/rna_ui_api.c
index fc68e8421d7..eac29ac5e61 100644
--- a/source/blender/makesrna/intern/rna_ui_api.c
+++ b/source/blender/makesrna/intern/rna_ui_api.c
@@ -1785,6 +1785,10 @@ void RNA_api_ui_layout(StructRNA *srna)
   parm = RNA_def_pointer(func, "socket", "NodeSocket", "", "");
   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
 
+  func = RNA_def_function(srna, "template_node_asset_menu_items", "uiTemplateNodeAssetMenuItems");
+  RNA_def_function_flag(func, FUNC_USE_CONTEXT);
+  parm = RNA_def_string(func, "catalog_path", NULL, 0, "", "");
+
   func = RNA_def_function(srna, "template_texture_user", "uiTemplateTextureUser");
   RNA_def_function_flag(func, FUNC_USE_CONTEXT);



More information about the Bf-blender-cvs mailing list