diff --git a/source/blender/nodes/NOD_socket_usage_inference.hh b/source/blender/nodes/NOD_socket_usage_inference.hh index 3054e0d8bad..a1f14c0f460 100644 --- a/source/blender/nodes/NOD_socket_usage_inference.hh +++ b/source/blender/nodes/NOD_socket_usage_inference.hh @@ -89,6 +89,13 @@ class InputSocketUsageParams { */ InferenceValue get_input(StringRef identifier) const; + /** + * Returns true if any output is known to be used or false if no output is used. std::nullopt is + * returned if it's not known yet if any output is used. In this case, the caller should return + * early, it will be checked again once new information about output usages becomes available. + */ + std::optional any_output_is_used() const; + /** * Utility for the case when the socket depends on a specific menu input to have a certain value. */ diff --git a/source/blender/nodes/intern/node_declaration.cc b/source/blender/nodes/intern/node_declaration.cc index 38f91df6ec3..f8bc5a9ca19 100644 --- a/source/blender/nodes/intern/node_declaration.cc +++ b/source/blender/nodes/intern/node_declaration.cc @@ -823,6 +823,17 @@ BaseSocketDeclarationBuilder &BaseSocketDeclarationBuilder::usage_by_single_menu this->usage_inference([menu_value](const socket_usage_inference::InputSocketUsageParams ¶ms) -> std::optional { const bNodeSocket &socket = find_single_menu_input(params.node); + if (const std::optional any_output_used = params.any_output_is_used()) { + if (!*any_output_used) { + /* If no output is used, none if the inputs is used either. */ + return false; + } + } + else { + /* It's not known if any output is used yet. This function will be called again once new + * information about output usages is available. */ + return std::nullopt; + } return params.menu_input_may_be(socket.identifier, menu_value); }); return *this; diff --git a/source/blender/nodes/intern/socket_usage_inference.cc b/source/blender/nodes/intern/socket_usage_inference.cc index b2acff3dca2..3201e5bd530 100644 --- a/source/blender/nodes/intern/socket_usage_inference.cc +++ b/source/blender/nodes/intern/socket_usage_inference.cc @@ -1553,6 +1553,28 @@ InferenceValue InputSocketUsageParams::get_input(const StringRef identifier) con return inferencer_.get_socket_value(input_socket); } +std::optional InputSocketUsageParams::any_output_is_used() const +{ + const bNodeSocket *first_missing = nullptr; + for (const bNodeSocket *output_socket : this->node.output_sockets()) { + if (const std::optional is_used = inferencer_.all_socket_usages_.lookup_try( + {compute_context_, output_socket})) + { + if (*is_used) { + return true; + } + } + else { + first_missing = output_socket; + } + } + if (first_missing) { + inferencer_.push_usage_task({compute_context_, first_missing}); + return std::nullopt; + } + return false; +} + bool InputSocketUsageParams::menu_input_may_be(const StringRef identifier, const int enum_value) const {