From f00c1ab6587586e06ef4ade5b2c23e22f3bb3860 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 20 Sep 2023 17:53:07 +0200 Subject: [PATCH] Nodes: deduplicate code for pairing zone nodes --- .../blender/makesrna/intern/rna_nodetree.cc | 80 +++++++------------ source/blender/nodes/NOD_geometry.hh | 14 ---- .../geometry/nodes/node_geo_repeat_input.cc | 26 ------ .../nodes/node_geo_simulation_input.cc | 35 -------- 4 files changed, 30 insertions(+), 125 deletions(-) diff --git a/source/blender/makesrna/intern/rna_nodetree.cc b/source/blender/makesrna/intern/rna_nodetree.cc index fb2ddf363f4..0959ab47c2b 100644 --- a/source/blender/makesrna/intern/rna_nodetree.cc +++ b/source/blender/makesrna/intern/rna_nodetree.cc @@ -3228,64 +3228,48 @@ static void rna_RepeatItem_color_get(PointerRNA *ptr, float *values) ED_node_type_draw_color(socket_type_idname, values); } -static PointerRNA rna_NodeGeometrySimulationInput_paired_output_get(PointerRNA *ptr) +static PointerRNA rna_Node_paired_output_get(PointerRNA *ptr) { bNodeTree *ntree = reinterpret_cast(ptr->owner_id); bNode *node = static_cast(ptr->data); - bNode *output_node = NOD_geometry_simulation_input_get_paired_output(ntree, node); + const blender::bke::bNodeZoneType &zone_type = *blender::bke::zone_type_by_node_type(node->type); + bNode *output_node = zone_type.get_corresponding_output(*ntree, *node); PointerRNA r_ptr = RNA_pointer_create(&ntree->id, &RNA_Node, output_node); return r_ptr; } -static PointerRNA rna_NodeGeometryRepeatInput_paired_output_get(PointerRNA *ptr) -{ - bNodeTree *ntree = reinterpret_cast(ptr->owner_id); - bNode *node = static_cast(ptr->data); - NodeGeometryRepeatInput *storage = static_cast(node->storage); - bNode *output_node = ntree->node_by_id(storage->output_node_id); - PointerRNA r_ptr = RNA_pointer_create(&ntree->id, &RNA_Node, output_node); - return r_ptr; -} - -static bool rna_GeometryNodeSimulationInput_pair_with_output( +static bool rna_Node_pair_with_output( ID *id, bNode *node, bContext *C, ReportList *reports, bNode *output_node) { bNodeTree *ntree = reinterpret_cast(id); - - if (!NOD_geometry_simulation_input_pair_with_output(ntree, node, output_node)) { - BKE_reportf(reports, - RPT_ERROR, - "Failed to pair simulation input node %s with output node %s", - node->name, - output_node->name); + const blender::bke::bNodeZoneType &zone_type = *blender::bke::zone_type_by_node_type(node->type); + if (output_node->type != zone_type.output_type) { + BKE_reportf( + reports, + RPT_ERROR, + "Can't pair zone input node %s with %s because it does not have the same zone type", + node->name, + output_node->name); return false; } + for (const bNode *other_input_node : ntree->nodes_by_type(zone_type.input_idname)) { + if (other_input_node != node) { + if (zone_type.get_corresponding_output(*ntree, *other_input_node) == output_node) { + BKE_reportf(reports, + RPT_ERROR, + "The output node %s is already paired with %s", + output_node->name, + other_input_node->name); + return false; + } + } + } + int &output_node_id = zone_type.get_corresponding_output_id(*node); + output_node_id = output_node->identifier; BKE_ntree_update_tag_node_property(ntree, node); ED_node_tree_propagate_change(C, CTX_data_main(C), ntree); WM_main_add_notifier(NC_NODE | NA_EDITED, ntree); - - return true; -} - -static bool rna_GeometryNodeRepeatInput_pair_with_output( - ID *id, bNode *node, bContext *C, ReportList *reports, bNode *output_node) -{ - bNodeTree *ntree = (bNodeTree *)id; - - if (!NOD_geometry_repeat_input_pair_with_output(ntree, node, output_node)) { - BKE_reportf(reports, - RPT_ERROR, - "Failed to pair repeat input node %s with output node %s", - node->name, - output_node->name); - return false; - } - - BKE_ntree_update_tag_node_property(ntree, node); - ED_node_tree_propagate_change(C, CTX_data_main(C), ntree); - WM_main_add_notifier(NC_NODE | NA_EDITED, ntree); - return true; } @@ -8858,13 +8842,11 @@ static void def_geo_simulation_input(StructRNA *srna) prop = RNA_def_property(srna, "paired_output", PROP_POINTER, PROP_NONE); RNA_def_property_struct_type(prop, "Node"); RNA_def_property_clear_flag(prop, PROP_EDITABLE); - RNA_def_property_pointer_funcs( - prop, "rna_NodeGeometrySimulationInput_paired_output_get", nullptr, nullptr, nullptr); + RNA_def_property_pointer_funcs(prop, "rna_Node_paired_output_get", nullptr, nullptr, nullptr); RNA_def_property_ui_text( prop, "Paired Output", "Simulation output node that this input node is paired with"); - func = RNA_def_function( - srna, "pair_with_output", "rna_GeometryNodeSimulationInput_pair_with_output"); + func = RNA_def_function(srna, "pair_with_output", "rna_Node_pair_with_output"); RNA_def_function_ui_description(func, "Pair a simulation input node with an output node."); RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_REPORTS | FUNC_USE_CONTEXT); parm = RNA_def_pointer( @@ -8887,13 +8869,11 @@ static void def_geo_repeat_input(StructRNA *srna) prop = RNA_def_property(srna, "paired_output", PROP_POINTER, PROP_NONE); RNA_def_property_struct_type(prop, "Node"); RNA_def_property_clear_flag(prop, PROP_EDITABLE); - RNA_def_property_pointer_funcs( - prop, "rna_NodeGeometryRepeatInput_paired_output_get", nullptr, nullptr, nullptr); + RNA_def_property_pointer_funcs(prop, "rna_Node_paired_output_get", nullptr, nullptr, nullptr); RNA_def_property_ui_text( prop, "Paired Output", "Repeat output node that this input node is paired with"); - func = RNA_def_function( - srna, "pair_with_output", "rna_GeometryNodeRepeatInput_pair_with_output"); + func = RNA_def_function(srna, "pair_with_output", "rna_Node_pair_with_output"); RNA_def_function_ui_description(func, "Pair a repeat input node with an output node."); RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_REPORTS | FUNC_USE_CONTEXT); parm = RNA_def_pointer( diff --git a/source/blender/nodes/NOD_geometry.hh b/source/blender/nodes/NOD_geometry.hh index 4541d8c7f3a..8a87de500a1 100644 --- a/source/blender/nodes/NOD_geometry.hh +++ b/source/blender/nodes/NOD_geometry.hh @@ -15,20 +15,6 @@ void register_node_type_geo_custom_group(bNodeType *ntype); /** \name Simulation Input Node * \{ */ -bNode *NOD_geometry_simulation_input_get_paired_output(bNodeTree *node_tree, - const bNode *simulation_input_node); - -/** - * Pair a simulation input node with an output node. - * \return True if pairing the node was successful. - */ -bool NOD_geometry_simulation_input_pair_with_output(const bNodeTree *node_tree, - bNode *simulation_input_node, - const bNode *simulation_output_node); -bool NOD_geometry_repeat_input_pair_with_output(const bNodeTree *node_tree, - bNode *repeat_input_node, - const bNode *repeat_output_node); - /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/nodes/geometry/nodes/node_geo_repeat_input.cc b/source/blender/nodes/geometry/nodes/node_geo_repeat_input.cc index 3c6e21577c9..b927a269545 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_repeat_input.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_repeat_input.cc @@ -96,29 +96,3 @@ static void node_register() NOD_REGISTER_NODE(node_register) } // namespace blender::nodes::node_geo_repeat_input_cc - -bool NOD_geometry_repeat_input_pair_with_output(const bNodeTree *node_tree, - bNode *repeat_input_node, - const bNode *repeat_output_node) -{ - namespace file_ns = blender::nodes::node_geo_repeat_input_cc; - - BLI_assert(repeat_input_node->type == GEO_NODE_REPEAT_INPUT); - if (repeat_output_node->type != GEO_NODE_REPEAT_OUTPUT) { - return false; - } - - /* Allow only one input paired to an output. */ - for (const bNode *other_input_node : node_tree->nodes_by_type("GeometryNodeRepeatInput")) { - if (other_input_node != repeat_input_node) { - const NodeGeometryRepeatInput &other_storage = file_ns::node_storage(*other_input_node); - if (other_storage.output_node_id == repeat_output_node->identifier) { - return false; - } - } - } - - NodeGeometryRepeatInput &storage = file_ns::node_storage(*repeat_input_node); - storage.output_node_id = repeat_output_node->identifier; - return true; -} diff --git a/source/blender/nodes/geometry/nodes/node_geo_simulation_input.cc b/source/blender/nodes/geometry/nodes/node_geo_simulation_input.cc index 41a355ef915..8f7d3fd0b5f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_simulation_input.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_simulation_input.cc @@ -268,38 +268,3 @@ static void node_register() NOD_REGISTER_NODE(node_register) } // namespace blender::nodes::node_geo_simulation_input_cc - -bNode *NOD_geometry_simulation_input_get_paired_output(bNodeTree *node_tree, - const bNode *simulation_input_node) -{ - namespace file_ns = blender::nodes::node_geo_simulation_input_cc; - - const NodeGeometrySimulationInput &data = file_ns::node_storage(*simulation_input_node); - return node_tree->node_by_id(data.output_node_id); -} - -bool NOD_geometry_simulation_input_pair_with_output(const bNodeTree *node_tree, - bNode *sim_input_node, - const bNode *sim_output_node) -{ - namespace file_ns = blender::nodes::node_geo_simulation_input_cc; - - BLI_assert(sim_input_node->type == GEO_NODE_SIMULATION_INPUT); - if (sim_output_node->type != GEO_NODE_SIMULATION_OUTPUT) { - return false; - } - - /* Allow only one input paired to an output. */ - for (const bNode *other_input_node : node_tree->nodes_by_type("GeometryNodeSimulationInput")) { - if (other_input_node != sim_input_node) { - const NodeGeometrySimulationInput &other_storage = file_ns::node_storage(*other_input_node); - if (other_storage.output_node_id == sim_output_node->identifier) { - return false; - } - } - } - - NodeGeometrySimulationInput &storage = file_ns::node_storage(*sim_input_node); - storage.output_node_id = sim_output_node->identifier; - return true; -}