Fix T92814: improve automatic linking when inserting Float Curve node

This solves the issue in a more general that can also be used to solve
similar issues for other nodes in the future. Nodes can specify their
"main" socket in their declaration so that we don't have to rely on
heuristics.

Differential Revision: https://developer.blender.org/D13108
This commit is contained in:
Jacques Lucke 2021-11-04 14:44:21 +01:00
parent ff4959eeaa
commit be4478d1f8
Notes: blender-bot 2023-02-14 08:08:56 +01:00
Referenced by issue #98171, Node muting connects mismatching sockets
Referenced by issue #92814, Fix auto-linkage in Float Curve node
3 changed files with 42 additions and 5 deletions

View File

@ -57,6 +57,7 @@
#include "BLT_translation.h"
#include "NOD_node_declaration.hh"
#include "NOD_node_tree_ref.hh"
#include "node_intern.h" /* own include */
@ -2182,9 +2183,32 @@ static int get_main_socket_priority(const bNodeSocket *socket)
return -1;
}
/** Get the "main" socket of a socket list using a heuristic based on socket types. */
static bNodeSocket *get_main_socket(ListBase *sockets)
/** Get the "main" socket based on the node declaration or an heuristic. */
static bNodeSocket *get_main_socket(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out)
{
using namespace blender;
using namespace blender::nodes;
ListBase *sockets = (in_out == SOCK_IN) ? &node.inputs : &node.outputs;
/* Try to get the main socket based on the socket declaration. */
nodeDeclarationEnsure(&ntree, &node);
const NodeDeclaration *node_decl = node.declaration;
if (node_decl != nullptr) {
Span<SocketDeclarationPtr> socket_decls = (in_out == SOCK_IN) ? node_decl->inputs() :
node_decl->outputs();
int index;
LISTBASE_FOREACH_INDEX (bNodeSocket *, socket, sockets, index) {
const SocketDeclaration &socket_decl = *socket_decls[index];
if (nodeSocketIsHidden(socket)) {
continue;
}
if (socket_decl.is_default_link_socket()) {
return socket;
}
}
}
/* find priority range */
int maxpriority = -1;
LISTBASE_FOREACH (bNodeSocket *, sock, sockets) {
@ -2561,8 +2585,8 @@ void ED_node_link_insert(Main *bmain, ScrArea *area)
}
if (link) {
bNodeSocket *best_input = get_main_socket(&select->inputs);
bNodeSocket *best_output = get_main_socket(&select->outputs);
bNodeSocket *best_input = get_main_socket(*snode->edittree, *select, SOCK_IN);
bNodeSocket *best_output = get_main_socket(*snode->edittree, *select, SOCK_OUT);
if (best_input && best_output) {
bNode *node = link->tonode;

View File

@ -89,6 +89,7 @@ class SocketDeclaration {
bool is_multi_input_ = false;
bool no_mute_links_ = false;
bool is_attribute_name_ = false;
bool is_default_link_socket_ = false;
InputSocketFieldType input_field_type_ = InputSocketFieldType::None;
OutputFieldDependency output_field_dependency_;
@ -107,6 +108,7 @@ class SocketDeclaration {
StringRefNull description() const;
StringRefNull identifier() const;
bool is_attribute_name() const;
bool is_default_link_socket() const;
InputSocketFieldType input_field_type() const;
const OutputFieldDependency &output_field_dependency() const;
@ -171,6 +173,12 @@ class SocketDeclarationBuilder : public BaseSocketDeclarationBuilder {
return *(Self *)this;
}
Self &is_default_link_socket(bool value = true)
{
decl_->is_default_link_socket_ = value;
return *(Self *)this;
}
/** The input socket allows passing in a field. */
Self &supports_field()
{
@ -363,6 +371,11 @@ inline bool SocketDeclaration::is_attribute_name() const
return is_attribute_name_;
}
inline bool SocketDeclaration::is_default_link_socket() const
{
return is_default_link_socket_;
}
inline InputSocketFieldType SocketDeclaration::input_field_type() const
{
return input_field_type_;

View File

@ -357,7 +357,7 @@ static void sh_node_curve_float_declare(NodeDeclarationBuilder &b)
.max(1.0f)
.default_value(1.0f)
.subtype(PROP_FACTOR);
b.add_input<decl::Float>(N_("Value")).default_value(1.0f);
b.add_input<decl::Float>(N_("Value")).default_value(1.0f).is_default_link_socket();
b.add_output<decl::Float>(N_("Value"));
};