Refactor: Use new context string support for node asset menus

Since 2fbf206491 it's possible to store named strings in context, rather
than having to wrap them in RNA pointers. Here this can avoid some
boilerplate code.

After this the `AssetCatalogPath` RNA struct should be unused. Might
be better to remove it in 5.0 only though, since it's part of the API.
Added to #110461.

Pull Request: https://projects.blender.org/blender/blender/pulls/122331
This commit is contained in:
Julian Eisel
2024-05-27 20:02:49 +02:00
committed by Julian Eisel
parent b37d121e0a
commit 95886fdfe2
7 changed files with 40 additions and 140 deletions

View File

@@ -137,35 +137,12 @@ const asset_system::AssetRepresentation *operator_asset_reference_props_get_asse
return find_asset_from_weak_ref(C, weak_ref, reports);
}
PointerRNA persistent_catalog_path_rna_pointer(const bScreen &owner_screen,
const asset_system::AssetLibrary &library,
const asset_system::AssetCatalogTreeItem &item)
{
const asset_system::AssetCatalog *catalog = library.catalog_service().find_catalog_by_path(
item.catalog_path());
if (!catalog) {
return PointerRNA_NULL;
}
const asset_system::AssetCatalogPath &path = catalog->path;
return {&const_cast<ID &>(owner_screen.id),
&RNA_AssetCatalogPath,
const_cast<asset_system::AssetCatalogPath *>(&path)};
}
void draw_menu_for_catalog(const bScreen &owner_screen,
const asset_system::AssetLibrary &library,
const asset_system::AssetCatalogTreeItem &item,
void draw_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item,
const StringRefNull menu_name,
uiLayout &layout)
{
PointerRNA path_ptr = asset::persistent_catalog_path_rna_pointer(owner_screen, library, item);
if (path_ptr.data == nullptr) {
return;
}
uiLayout *col = uiLayoutColumn(&layout, false);
uiLayoutSetContextPointer(col, "asset_catalog_path", &path_ptr);
uiLayoutSetContextString(col, "asset_catalog_path", item.catalog_path().c_str());
uiItemM(col, menu_name.c_str(), IFACE_(item.get_name().c_str()), ICON_NONE);
}

View File

@@ -1075,7 +1075,6 @@ static Set<std::string> get_builtin_menus(const ObjectType object_type, const eO
static void catalog_assets_draw(const bContext *C, Menu *menu)
{
bScreen &screen = *CTX_wm_screen(C);
const Object *active_object = CTX_data_active_object(C);
if (!active_object) {
return;
@@ -1084,13 +1083,14 @@ static void catalog_assets_draw(const bContext *C, Menu *menu)
if (!tree) {
return;
}
const PointerRNA menu_path_ptr = CTX_data_pointer_get(C, "asset_catalog_path");
if (RNA_pointer_is_null(&menu_path_ptr)) {
const std::optional<StringRefNull> menu_path = CTX_data_string_get(C, "asset_catalog_path");
if (!menu_path) {
return;
}
const auto &menu_path = *static_cast<const asset_system::AssetCatalogPath *>(menu_path_ptr.data);
const Span<asset_system::AssetRepresentation *> assets = tree->assets_per_path.lookup(menu_path);
const asset_system::AssetCatalogTreeItem *catalog_item = tree->catalogs.find_item(menu_path);
const Span<asset_system::AssetRepresentation *> assets = tree->assets_per_path.lookup(
menu_path->data());
const asset_system::AssetCatalogTreeItem *catalog_item = tree->catalogs.find_item(
menu_path->data());
BLI_assert(catalog_item != nullptr);
uiLayout *layout = menu->layout;
@@ -1131,8 +1131,7 @@ static void catalog_assets_draw(const bContext *C, Menu *menu)
uiItemS(layout);
add_separator = false;
}
asset::draw_menu_for_catalog(
screen, *all_library, item, "GEO_MT_node_operator_catalog_assets", *layout);
asset::draw_menu_for_catalog(item, "GEO_MT_node_operator_catalog_assets", *layout);
});
}
@@ -1253,7 +1252,6 @@ void ui_template_node_operator_asset_menu_items(uiLayout &layout,
const bContext &C,
const StringRef catalog_path)
{
bScreen &screen = *CTX_wm_screen(&C);
const Object *active_object = CTX_data_active_object(&C);
if (!active_object) {
return;
@@ -1271,18 +1269,13 @@ void ui_template_node_operator_asset_menu_items(uiLayout &layout,
if (!all_library) {
return;
}
PointerRNA path_ptr = asset::persistent_catalog_path_rna_pointer(screen, *all_library, *item);
if (path_ptr.data == nullptr) {
return;
}
uiLayout *col = uiLayoutColumn(&layout, false);
uiLayoutSetContextPointer(col, "asset_catalog_path", &path_ptr);
uiLayoutSetContextString(col, "asset_catalog_path", item->catalog_path().str());
uiItemMContents(col, "GEO_MT_node_operator_catalog_assets");
}
void ui_template_node_operator_asset_root_items(uiLayout &layout, const bContext &C)
{
bScreen &screen = *CTX_wm_screen(&C);
const Object *active_object = CTX_data_active_object(&C);
if (!active_object) {
return;
@@ -1295,19 +1288,14 @@ void ui_template_node_operator_asset_root_items(uiLayout &layout, const bContext
*tree = build_catalog_tree(C, *active_object);
}
if (asset_system::AssetLibrary *all_library = asset::list::library_get_once_available(
asset_system::all_library_reference()))
{
const Set<std::string> builtin_menus = get_builtin_menus(ObjectType(active_object->type),
eObjectMode(active_object->mode));
const Set<std::string> builtin_menus = get_builtin_menus(ObjectType(active_object->type),
eObjectMode(active_object->mode));
tree->catalogs.foreach_root_item([&](const asset_system::AssetCatalogTreeItem &item) {
if (!builtin_menus.contains_as(item.catalog_path().str())) {
asset::draw_menu_for_catalog(
screen, *all_library, item, "GEO_MT_node_operator_catalog_assets", layout);
}
});
}
tree->catalogs.foreach_root_item([&](const asset_system::AssetCatalogTreeItem &item) {
if (!builtin_menus.contains_as(item.catalog_path().str())) {
asset::draw_menu_for_catalog(item, "GEO_MT_node_operator_catalog_assets", layout);
}
});
if (!tree->unassigned_assets.is_empty() || unassigned_local_poll(C)) {
uiItemM(&layout, "GEO_MT_node_operator_unassigned", "", ICON_FILE_HIDDEN);

View File

@@ -25,18 +25,7 @@ class AssetRepresentation;
namespace blender::ed::asset {
/**
* Some code needs to pass catalog paths to context and for this they need persistent pointers to
* the paths. Rather than keeping some local path storage, get a pointer into the asset system
* directly, which is persistent until the library is reloaded and can safely be held by context.
*/
PointerRNA persistent_catalog_path_rna_pointer(const bScreen &owner_screen,
const asset_system::AssetLibrary &library,
const asset_system::AssetCatalogTreeItem &item);
void draw_menu_for_catalog(const bScreen &owner_screen,
const asset_system::AssetLibrary &library,
const asset_system::AssetCatalogTreeItem &item,
void draw_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item,
StringRefNull menu_name,
uiLayout &layout);

View File

@@ -596,8 +596,6 @@ void data_xform_by_mat4(XFormObjectData *xod, const float mat[4][4]);
void data_xform_restore(XFormObjectData *xod);
void data_xform_tag_update(XFormObjectData *xod);
void ui_template_modifier_asset_menu_items(uiLayout &layout,
const bContext &C,
StringRef catalog_path);
void ui_template_modifier_asset_menu_items(uiLayout &layout, StringRef catalog_path);
} // namespace blender::ed::object

View File

@@ -78,18 +78,16 @@ static asset::AssetItemTree *get_static_item_tree()
static void catalog_assets_draw(const bContext *C, Menu *menu)
{
bScreen &screen = *CTX_wm_screen(C);
asset::AssetItemTree &tree = *get_static_item_tree();
const PointerRNA menu_path_ptr = CTX_data_pointer_get(C, "asset_catalog_path");
if (RNA_pointer_is_null(&menu_path_ptr)) {
const std::optional<StringRefNull> menu_path = CTX_data_string_get(C, "asset_catalog_path");
if (!menu_path) {
return;
}
const asset_system::AssetCatalogPath &menu_path =
*static_cast<const asset_system::AssetCatalogPath *>(menu_path_ptr.data);
const Span<asset_system::AssetRepresentation *> assets = tree.assets_per_path.lookup(menu_path);
const asset_system::AssetCatalogTreeItem *catalog_item = tree.catalogs.find_item(menu_path);
const Span<asset_system::AssetRepresentation *> assets = tree.assets_per_path.lookup(
menu_path->data());
const asset_system::AssetCatalogTreeItem *catalog_item = tree.catalogs.find_item(
menu_path->data());
BLI_assert(catalog_item != nullptr);
if (assets.is_empty() && !catalog_item->has_children()) {
@@ -113,15 +111,8 @@ static void catalog_assets_draw(const bContext *C, Menu *menu)
asset::operator_asset_reference_props_set(*asset, props_ptr);
}
asset_system::AssetLibrary *all_library = asset::list::library_get_once_available(
asset_system::all_library_reference());
if (!all_library) {
return;
}
catalog_item->foreach_child([&](const asset_system::AssetCatalogTreeItem &item) {
asset::draw_menu_for_catalog(
screen, *all_library, item, "OBJECT_MT_add_modifier_catalog_assets", *layout);
asset::draw_menu_for_catalog(item, "OBJECT_MT_add_modifier_catalog_assets", *layout);
});
}
@@ -202,7 +193,6 @@ static void root_catalogs_draw(const bContext *C, Menu *menu)
if (!object) {
return;
}
bScreen &screen = *CTX_wm_screen(C);
uiLayout *layout = menu->layout;
const bool loading_finished = all_loading_finished();
@@ -239,16 +229,9 @@ static void root_catalogs_draw(const bContext *C, Menu *menu)
return menus;
}();
asset_system::AssetLibrary *all_library = asset::list::library_get_once_available(
asset_system::all_library_reference());
if (!all_library) {
return;
}
tree.catalogs.foreach_root_item([&](const asset_system::AssetCatalogTreeItem &item) {
if (!all_builtin_menus.contains(item.get_name())) {
asset::draw_menu_for_catalog(
screen, *all_library, item, "OBJECT_MT_add_modifier_catalog_assets", *layout);
asset::draw_menu_for_catalog(item, "OBJECT_MT_add_modifier_catalog_assets", *layout);
}
});
@@ -420,11 +403,8 @@ void object_modifier_add_asset_register()
WM_operatortype_append(OBJECT_OT_modifier_add_node_group);
}
void ui_template_modifier_asset_menu_items(uiLayout &layout,
const bContext &C,
const StringRef catalog_path)
void ui_template_modifier_asset_menu_items(uiLayout &layout, const StringRef catalog_path)
{
bScreen &screen = *CTX_wm_screen(&C);
asset::AssetItemTree &tree = *get_static_item_tree();
const asset_system::AssetCatalogTreeItem *item = tree.catalogs.find_root_item(catalog_path);
if (!item) {
@@ -435,13 +415,9 @@ void ui_template_modifier_asset_menu_items(uiLayout &layout,
if (!all_library) {
return;
}
PointerRNA path_ptr = asset::persistent_catalog_path_rna_pointer(screen, *all_library, *item);
if (path_ptr.data == nullptr) {
return;
}
uiItemS(&layout);
uiLayout *col = uiLayoutColumn(&layout, false);
uiLayoutSetContextPointer(col, "asset_catalog_path", &path_ptr);
uiLayoutSetContextString(col, "asset_catalog_path", item->catalog_path().str());
uiItemMContents(col, "OBJECT_MT_add_modifier_catalog_assets");
}

View File

@@ -145,7 +145,6 @@ static Set<StringRef> get_builtin_menus(const int tree_type)
static void node_add_catalog_assets_draw(const bContext *C, Menu *menu)
{
bScreen &screen = *CTX_wm_screen(C);
SpaceNode &snode = *CTX_wm_space_node(C);
const bNodeTree *edit_tree = snode.edittree;
if (!edit_tree) {
@@ -158,15 +157,15 @@ static void node_add_catalog_assets_draw(const bContext *C, Menu *menu)
}
asset::AssetItemTree &tree = *snode.runtime->assets_for_menu;
const PointerRNA menu_path_ptr = CTX_data_pointer_get(C, "asset_catalog_path");
if (RNA_pointer_is_null(&menu_path_ptr)) {
const std::optional<blender::StringRefNull> menu_path = CTX_data_string_get(
C, "asset_catalog_path");
if (!menu_path) {
return;
}
const asset_system::AssetCatalogPath &menu_path =
*static_cast<const asset_system::AssetCatalogPath *>(menu_path_ptr.data);
const Span<asset_system::AssetRepresentation *> assets = tree.assets_per_path.lookup(menu_path);
const asset_system::AssetCatalogTreeItem *catalog_item = tree.catalogs.find_item(menu_path);
const Span<asset_system::AssetRepresentation *> assets = tree.assets_per_path.lookup(
menu_path->c_str());
const asset_system::AssetCatalogTreeItem *catalog_item = tree.catalogs.find_item(
menu_path->c_str());
BLI_assert(catalog_item != nullptr);
if (assets.is_empty() && !catalog_item->has_children()) {
@@ -193,12 +192,6 @@ static void node_add_catalog_assets_draw(const bContext *C, Menu *menu)
asset::operator_asset_reference_props_set(*asset, op_ptr);
}
asset_system::AssetLibrary *all_library = asset::list::library_get_once_available(
asset_system::all_library_reference());
if (!all_library) {
return;
}
const Set<StringRef> all_builtin_menus = get_builtin_menus(edit_tree->type);
catalog_item->foreach_child([&](const asset_system::AssetCatalogTreeItem &item) {
@@ -209,8 +202,7 @@ static void node_add_catalog_assets_draw(const bContext *C, Menu *menu)
uiItemS(layout);
add_separator = false;
}
asset::draw_menu_for_catalog(
screen, *all_library, item, "NODE_MT_node_add_catalog_assets", *layout);
asset::draw_menu_for_catalog(item, "NODE_MT_node_add_catalog_assets", *layout);
});
}
@@ -243,7 +235,6 @@ static void node_add_unassigned_assets_draw(const bContext *C, Menu *menu)
static void add_root_catalogs_draw(const bContext *C, Menu *menu)
{
bScreen &screen = *CTX_wm_screen(C);
SpaceNode &snode = *CTX_wm_space_node(C);
uiLayout *layout = menu->layout;
const bNodeTree *edit_tree = snode.edittree;
@@ -269,16 +260,9 @@ static void add_root_catalogs_draw(const bContext *C, Menu *menu)
const Set<StringRef> all_builtin_menus = get_builtin_menus(edit_tree->type);
asset_system::AssetLibrary *all_library = asset::list::library_get_once_available(
asset_system::all_library_reference());
if (!all_library) {
return;
}
tree.catalogs.foreach_root_item([&](const asset_system::AssetCatalogTreeItem &item) {
if (!all_builtin_menus.contains_as(item.catalog_path().str())) {
asset::draw_menu_for_catalog(
screen, *all_library, item, "NODE_MT_node_add_catalog_assets", *layout);
asset::draw_menu_for_catalog(item, "NODE_MT_node_add_catalog_assets", *layout);
}
});
@@ -327,7 +311,6 @@ void ui_template_node_asset_menu_items(uiLayout &layout,
const bContext &C,
const StringRef catalog_path)
{
bScreen &screen = *CTX_wm_screen(&C);
SpaceNode &snode = *CTX_wm_space_node(&C);
if (snode.runtime->assets_for_menu == nullptr) {
return;
@@ -337,17 +320,8 @@ void ui_template_node_asset_menu_items(uiLayout &layout,
if (!item) {
return;
}
asset_system::AssetLibrary *all_library = asset::list::library_get_once_available(
asset_system::all_library_reference());
if (!all_library) {
return;
}
PointerRNA path_ptr = asset::persistent_catalog_path_rna_pointer(screen, *all_library, *item);
if (path_ptr.data == nullptr) {
return;
}
uiLayout *col = uiLayoutColumn(&layout, false);
uiLayoutSetContextPointer(col, "asset_catalog_path", &path_ptr);
uiLayoutSetContextString(col, "asset_catalog_path", item->catalog_path().str());
uiItemMContents(col, "NODE_MT_node_add_catalog_assets");
}

View File

@@ -850,11 +850,10 @@ static void rna_uiLayout_template_node_operator_asset_menu_items(uiLayout *layou
}
static void rna_uiLayout_template_modifier_asset_menu_items(uiLayout *layout,
bContext *C,
const char *catalog_path)
{
using namespace blender;
ed::object::ui_template_modifier_asset_menu_items(*layout, *C, StringRef(catalog_path));
ed::object::ui_template_modifier_asset_menu_items(*layout, StringRef(catalog_path));
}
static void rna_uiLayout_template_node_operator_root_items(uiLayout *layout, bContext *C)
@@ -2085,7 +2084,6 @@ void RNA_api_ui_layout(StructRNA *srna)
func = RNA_def_function(srna,
"template_modifier_asset_menu_items",
"rna_uiLayout_template_modifier_asset_menu_items");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
parm = RNA_def_string(func, "catalog_path", nullptr, 0, "", "");
func = RNA_def_function(srna,