Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-08-30 18:13:46 +0300
committerJacques Lucke <jacques@blender.org>2021-08-30 18:23:05 +0300
commit1e69a25043120cc8dddc3f58622eb50e1443def1 (patch)
tree16bcb3eb98215dbbd5c54880cb3d14544e316d3f /source/blender/nodes/intern/node_socket.cc
parente8684eff301d09e25b4e7ef6221a5289661da2e3 (diff)
Nodes: add more flexible method to declare sockets of a node
Previously, built-in nodes had to implement "socket templates" (`bNodeSocketTemplate`) to tell Blender which sockets they have. It was nice that this was declarative, but this approach was way too rigid and was cumbersome to use in many cases. This commit starts to move us away from this rigid structure by letting nodes implement a function that declares the sockets the node has. Right now this is used as a direct replacement of the "socket template" approach to keep the refactor smaller. It's just a bit easier to read and write. In the future we want to support more complex features like dynamic numbers of sockets and type inferencing. Those features will be easier to build on this new approach. This new approach can live side by side with `bNodeSocketTemplate` for a while. That makes it easier to update nodes one by one. Note: In `bNodeSocketTemplate` socket identifiers were made unique automatically. In this new approach, one has to specify unique identifiers manually (unless the name is unique already). Differential Revision: https://developer.blender.org/D12335
Diffstat (limited to 'source/blender/nodes/intern/node_socket.cc')
-rw-r--r--source/blender/nodes/intern/node_socket.cc93
1 files changed, 92 insertions, 1 deletions
diff --git a/source/blender/nodes/intern/node_socket.cc b/source/blender/nodes/intern/node_socket.cc
index 528616eb23a..d386781e3ce 100644
--- a/source/blender/nodes/intern/node_socket.cc
+++ b/source/blender/nodes/intern/node_socket.cc
@@ -44,10 +44,14 @@
#include "MEM_guardedalloc.h"
+#include "NOD_node_declaration.hh"
#include "NOD_socket.h"
#include "FN_cpp_type_make.hh"
+using namespace blender;
+using blender::nodes::SocketDeclarationPtr;
+
struct bNodeSocket *node_add_socket_from_template(struct bNodeTree *ntree,
struct bNode *node,
struct bNodeSocketTemplate *stemp,
@@ -182,9 +186,96 @@ static void verify_socket_template_list(bNodeTree *ntree,
}
}
-void node_verify_socket_templates(bNodeTree *ntree, bNode *node)
+static void refresh_socket_list(bNodeTree &ntree,
+ bNode &node,
+ ListBase &sockets,
+ Span<SocketDeclarationPtr> socket_decls,
+ const eNodeSocketInOut in_out,
+ const bool do_id_user)
+{
+ Vector<bNodeSocket *> old_sockets = sockets;
+ VectorSet<bNodeSocket *> new_sockets;
+ for (const SocketDeclarationPtr &socket_decl : socket_decls) {
+ /* Try to find a socket that corresponds to the declaration. */
+ bNodeSocket *old_socket_with_same_identifier = nullptr;
+ for (const int i : old_sockets.index_range()) {
+ bNodeSocket &old_socket = *old_sockets[i];
+ if (old_socket.identifier == socket_decl->identifier()) {
+ old_sockets.remove_and_reorder(i);
+ old_socket_with_same_identifier = &old_socket;
+ break;
+ }
+ }
+ bNodeSocket *new_socket = nullptr;
+ if (old_socket_with_same_identifier == nullptr) {
+ /* Create a completely new socket. */
+ new_socket = &socket_decl->build(ntree, node, in_out);
+ }
+ else {
+ STRNCPY(old_socket_with_same_identifier->name, socket_decl->name().c_str());
+ if (socket_decl->matches(*old_socket_with_same_identifier)) {
+ /* The existing socket matches exactly, just use it. */
+ new_socket = old_socket_with_same_identifier;
+ }
+ else {
+ /* Clear out identifier to avoid name collisions when a new socket is created. */
+ old_socket_with_same_identifier->identifier[0] = '\0';
+ new_socket = &socket_decl->update_or_build(ntree, node, *old_socket_with_same_identifier);
+
+ if (new_socket == old_socket_with_same_identifier) {
+ /* The existing socket has been updated, set the correct identifier again. */
+ STRNCPY(new_socket->identifier, socket_decl->identifier().c_str());
+ }
+ else {
+ /* Move links to new socket with same identifier. */
+ LISTBASE_FOREACH (bNodeLink *, link, &ntree.links) {
+ if (link->fromsock == old_socket_with_same_identifier) {
+ link->fromsock = new_socket;
+ }
+ else if (link->tosock == old_socket_with_same_identifier) {
+ link->tosock = new_socket;
+ }
+ }
+ }
+ }
+ }
+ new_sockets.add_new(new_socket);
+ }
+ LISTBASE_FOREACH_MUTABLE (bNodeSocket *, old_socket, &sockets) {
+ if (!new_sockets.contains(old_socket)) {
+ nodeRemoveSocketEx(&ntree, &node, old_socket, do_id_user);
+ }
+ }
+ BLI_listbase_clear(&sockets);
+ for (bNodeSocket *socket : new_sockets) {
+ BLI_addtail(&sockets, socket);
+ }
+}
+
+static void refresh_node(bNodeTree &ntree,
+ bNode &node,
+ blender::nodes::NodeDeclaration &node_decl,
+ bool do_id_user)
+{
+ refresh_socket_list(ntree, node, node.inputs, node_decl.inputs(), SOCK_IN, do_id_user);
+ refresh_socket_list(ntree, node, node.outputs, node_decl.outputs(), SOCK_OUT, do_id_user);
+}
+
+void node_verify_sockets(bNodeTree *ntree, bNode *node, bool do_id_user)
{
bNodeType *ntype = node->typeinfo;
+ if (ntype == nullptr) {
+ return;
+ }
+ if (ntype->declare != nullptr) {
+ blender::nodes::NodeDeclaration node_decl;
+ blender::nodes::NodeDeclarationBuilder builder{node_decl};
+ ntype->declare(builder);
+ if (!node_decl.matches(*node)) {
+ refresh_node(*ntree, *node, node_decl, do_id_user);
+ }
+ return;
+ }
/* Don't try to match socket lists when there are no templates.
* This prevents dynamically generated sockets to be removed, like for
* group, image or render layer nodes. We have an explicit check for the