diff options
author | Hans Goudey <h.goudey@me.com> | 2022-03-31 04:54:11 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-03-31 04:54:11 +0300 |
commit | 762d3a48e860d3425fc45bb0a7d4b86fb8cc836b (patch) | |
tree | c6fdbaeae421ce7a25f8ca4dd33e31075e650f4e /source/blender/editors/space_node | |
parent | 49858bf52641831f22e1d5a85b10b8074a449a30 (diff) |
Cleanup: Avoid storing pointers for attribute search callback
It's better to use some local/stable identifiier to avoid relying on
the data not being freed in between creating the search menu and
the exec function. This is similar to c473b2ce8bdbf8fa.
Diffstat (limited to 'source/blender/editors/space_node')
4 files changed, 33 insertions, 15 deletions
diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc index c6839099827..958a9fdfc60 100644 --- a/source/blender/editors/space_node/drawnode.cc +++ b/source/blender/editors/space_node/drawnode.cc @@ -1302,8 +1302,7 @@ static void std_node_socket_draw( uiItemL(row, text, 0); if (socket_needs_attribute_search(*node, *sock)) { - const bNodeTree *node_tree = (const bNodeTree *)node_ptr->owner_id; - node_geometry_add_attribute_search_button(*C, *node_tree, *node, *ptr, *row); + node_geometry_add_attribute_search_button(*C, *node, *ptr, *row); } else { uiItemR(row, ptr, "default_value", DEFAULT_FLAGS, "", 0); diff --git a/source/blender/editors/space_node/node_geometry_attribute_search.cc b/source/blender/editors/space_node/node_geometry_attribute_search.cc index 5a6fe911a05..4a0f3b9a1f2 100644 --- a/source/blender/editors/space_node/node_geometry_attribute_search.cc +++ b/source/blender/editors/space_node/node_geometry_attribute_search.cc @@ -38,9 +38,8 @@ using geo_log::GeometryAttributeInfo; namespace blender::ed::space_node { struct AttributeSearchData { - const bNodeTree *tree; - const bNode *node; - bNodeSocket *socket; + char node_name[MAX_NAME]; + char socket_identifier[MAX_NAME]; }; /* This class must not have a destructor, since it is used by buttons and freed with #MEM_freeN. */ @@ -57,7 +56,7 @@ static void attribute_search_update_fn( SpaceNode *snode = CTX_wm_space_node(C); const geo_log::NodeLog *node_log = geo_log::ModifierLog::find_node_by_node_editor_context( - *snode, *data->node); + *snode, data->node_name); if (node_log == nullptr) { return; } @@ -71,21 +70,40 @@ static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v) if (ED_screen_animation_playing(CTX_wm_manager(C))) { return; } - if (item_v == nullptr) { + GeometryAttributeInfo *item = (GeometryAttributeInfo *)item_v; + if (item == nullptr) { + return; + } + SpaceNode *snode = CTX_wm_space_node(C); + if (!snode) { + BLI_assert_unreachable(); + return; + } + bNodeTree *node_tree = snode->edittree; + if (node_tree == nullptr) { + BLI_assert_unreachable(); return; } AttributeSearchData *data = static_cast<AttributeSearchData *>(data_v); - GeometryAttributeInfo *item = (GeometryAttributeInfo *)item_v; + bNode *node = nodeFindNodebyName(node_tree, data->node_name); + if (node == nullptr) { + BLI_assert_unreachable(); + return; + } + bNodeSocket *socket = bke::node_find_enabled_input_socket(*node, data->socket_identifier); + if (socket == nullptr) { + BLI_assert_unreachable(); + return; + } + BLI_assert(socket->type == SOCK_STRING); - bNodeSocket &socket = *data->socket; - bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket.default_value); + bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket->default_value); BLI_strncpy(value->value, item->name.c_str(), MAX_NAME); ED_undo_push(C, "Assign Attribute Name"); } void node_geometry_add_attribute_search_button(const bContext &UNUSED(C), - const bNodeTree &node_tree, const bNode &node, PointerRNA &socket_ptr, uiLayout &layout) @@ -109,8 +127,10 @@ void node_geometry_add_attribute_search_button(const bContext &UNUSED(C), 0.0f, ""); - AttributeSearchData *data = MEM_new<AttributeSearchData>( - __func__, AttributeSearchData{&node_tree, &node, (bNodeSocket *)socket_ptr.data}); + const bNodeSocket &socket = *static_cast<const bNodeSocket *>(socket_ptr.data); + AttributeSearchData *data = MEM_new<AttributeSearchData>(__func__); + BLI_strncpy(data->node_name, node.name, sizeof(data->node_name)); + BLI_strncpy(data->socket_identifier, socket.identifier, sizeof(data->socket_identifier)); UI_but_func_search_set_results_are_suggestions(but, true); UI_but_func_search_set_sep_string(but, UI_MENU_ARROW_SEP); diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh index cd40573607d..44163cd5ae3 100644 --- a/source/blender/editors/space_node/node_intern.hh +++ b/source/blender/editors/space_node/node_intern.hh @@ -353,7 +353,6 @@ void NODE_GGT_backdrop_corner_pin(wmGizmoGroupType *gzgt); /* node_geometry_attribute_search.cc */ void node_geometry_add_attribute_search_button(const bContext &C, - const bNodeTree &node_tree, const bNode &node, PointerRNA &socket_ptr, uiLayout &layout); diff --git a/source/blender/editors/space_node/node_templates.cc b/source/blender/editors/space_node/node_templates.cc index b63cb2eeee5..e92e0571157 100644 --- a/source/blender/editors/space_node/node_templates.cc +++ b/source/blender/editors/space_node/node_templates.cc @@ -884,7 +884,7 @@ static void ui_node_draw_input( if (node_tree->type == NTREE_GEOMETRY && snode != nullptr) { /* Only add the attribute search in the node editor, in other places there is not * enough context. */ - node_geometry_add_attribute_search_button(*C, *node_tree, *node, inputptr, *row); + node_geometry_add_attribute_search_button(*C, *node, inputptr, *row); } else { uiItemR(sub, &inputptr, "default_value", 0, "", ICON_NONE); |