diff options
author | Jacques Lucke <jacques@blender.org> | 2021-08-30 18:13:46 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-08-30 18:23:05 +0300 |
commit | 1e69a25043120cc8dddc3f58622eb50e1443def1 (patch) | |
tree | 16bcb3eb98215dbbd5c54880cb3d14544e316d3f /source/blender/blenkernel | |
parent | e8684eff301d09e25b4e7ef6221a5289661da2e3 (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/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_node.h | 10 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/node.cc | 20 |
2 files changed, 28 insertions, 2 deletions
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index 379d4332473..5e0526ab262 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -113,6 +113,7 @@ namespace blender { namespace nodes { class NodeMultiFunctionBuilder; class GeoNodeExecParams; +class NodeDeclarationBuilder; } // namespace nodes namespace fn { class CPPType; @@ -122,6 +123,7 @@ class MFDataType; using NodeMultiFunctionBuildFunction = void (*)(blender::nodes::NodeMultiFunctionBuilder &builder); using NodeGeometryExecFunction = void (*)(blender::nodes::GeoNodeExecParams params); +using NodeDeclareFunction = void (*)(blender::nodes::NodeDeclarationBuilder &builder); using SocketGetCPPTypeFunction = const blender::fn::CPPType *(*)(); using SocketGetCPPValueFunction = void (*)(const struct bNodeSocket &socket, void *r_value); using SocketGetGeometryNodesCPPTypeFunction = const blender::fn::CPPType *(*)(); @@ -131,6 +133,7 @@ using SocketGetGeometryNodesCPPValueFunction = void (*)(const struct bNodeSocket #else typedef void *NodeMultiFunctionBuildFunction; typedef void *NodeGeometryExecFunction; +typedef void *NodeDeclareFunction; typedef void *SocketGetCPPTypeFunction; typedef void *SocketGetGeometryNodesCPPTypeFunction; typedef void *SocketGetGeometryNodesCPPValueFunction; @@ -334,6 +337,9 @@ typedef struct bNodeType { NodeGeometryExecFunction geometry_node_execute; bool geometry_node_execute_supports_laziness; + /* Declares which sockets the node has. */ + NodeDeclareFunction declare; + /* RNA integration */ ExtensionRNA rna_ext; } bNodeType; @@ -615,6 +621,10 @@ struct bNodeSocket *nodeInsertStaticSocket(struct bNodeTree *ntree, const char *identifier, const char *name); void nodeRemoveSocket(struct bNodeTree *ntree, struct bNode *node, struct bNodeSocket *sock); +void nodeRemoveSocketEx(struct bNodeTree *ntree, + struct bNode *node, + struct bNodeSocket *sock, + bool do_id_user); void nodeRemoveAllSockets(struct bNodeTree *ntree, struct bNode *node); void nodeModifySocketType(struct bNodeTree *ntree, struct bNode *node, diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index fb43d1fc87f..48d747e2bf3 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -79,6 +79,7 @@ #include "NOD_composite.h" #include "NOD_function.h" #include "NOD_geometry.h" +#include "NOD_node_declaration.hh" #include "NOD_shader.h" #include "NOD_socket.h" #include "NOD_texture.h" @@ -887,7 +888,7 @@ void ntreeBlendReadLib(struct BlendLibReader *reader, struct bNodeTree *ntree) * to match the static layout. */ if (!BLO_read_lib_is_undo(reader)) { LISTBASE_FOREACH (bNode *, node, &ntree->nodes) { - node_verify_socket_templates(ntree, node); + node_verify_sockets(ntree, node, false); } } } @@ -1011,6 +1012,13 @@ IDTypeInfo IDType_ID_NT = { static void node_add_sockets_from_type(bNodeTree *ntree, bNode *node, bNodeType *ntype) { + if (ntype->declare != nullptr) { + blender::nodes::NodeDeclaration node_decl; + blender::nodes::NodeDeclarationBuilder builder{node_decl}; + ntype->declare(builder); + node_decl.build(*ntree, *node); + return; + } bNodeSocketTemplate *sockdef; /* bNodeSocket *sock; */ /* UNUSED */ @@ -1916,6 +1924,14 @@ static void node_socket_free(bNodeTree *UNUSED(ntree), void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock) { + nodeRemoveSocketEx(ntree, node, sock, true); +} + +void nodeRemoveSocketEx(struct bNodeTree *ntree, + struct bNode *node, + struct bNodeSocket *sock, + bool do_id_user) +{ LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree->links) { if (link->fromsock == sock || link->tosock == sock) { nodeRemLink(ntree, link); @@ -1926,7 +1942,7 @@ void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock) BLI_remlink(&node->inputs, sock); BLI_remlink(&node->outputs, sock); - node_socket_free(ntree, sock, node, true); + node_socket_free(ntree, sock, node, do_id_user); MEM_freeN(sock); node->update |= NODE_UPDATE; |