diff options
author | Lukas Tönne <lukas.toenne@gmail.com> | 2021-07-06 20:36:11 +0300 |
---|---|---|
committer | Lukas Tönne <lukas.toenne@gmail.com> | 2021-07-06 20:36:11 +0300 |
commit | 586cf8b1905257bf0f7d9c20b5333271f9cab3e9 (patch) | |
tree | 5fa77626641c47f4274b9145a1f1702512fc92b6 /source/blender/blenkernel/intern | |
parent | 933eddc9a113b40849895922f2dc350305809ebd (diff) |
Nodes: Adds button to groups to change type of sockets.
The menu lists all socket types that are valid for the node tree.
Changing a socket type updates all instances of the group and keeps
existing links to the socket.
If changing the socket type leads to incorrect node connections the
links are flagged as invalid (red) and ignored but not removed. This is
so users don't lose information and can then fix resulting issues.
For example: Changing a Color socket to a Shader socket can cause an
invalid Shader-to-Color connection.
Implementation details:
The new `NODE_OT_tree_socket_change_type` operator uses the generic
`rna_node_socket_type_itemf` function to list all eligible socket types.
It uses the tree type's `valid_socket_type` callback to test for valid
types. In addition it also checks the subtype, because multiple RNA
types are registered for the same base type. The `valid_socket_type`
callback has been modified slightly to accept full socket types instead
of just the base type enum, so that custom (python) socket types can be
used by this operator.
The `nodeModifySocketType` function is now called when group nodes
encounter a socket type mismatch, instead of replacing the socket
entirely. This ensures that links are kept to/from group nodes as well
as group input/output nodes. The `nodeModifySocketType` function now
also takes a full `bNodeSocketType` instead of just the base and subtype
enum (a shortcut `nodeModifySocketTypeStatic` exists for when only
static types are used).
Differential Revision: https://developer.blender.org/D10912
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/node.cc | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index f6105bac1a8..6a6cc3ffaad 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -1425,6 +1425,12 @@ GHashIterator *nodeSocketTypeGetIterator(void) return BLI_ghashIterator_new(nodesockettypes_hash); } +const char *nodeSocketTypeLabel(const bNodeSocketType *stype) +{ + /* Use socket type name as a fallback if label is undefined. */ + return stype->label[0] != '\0' ? stype->label : RNA_struct_ui_name(stype->ext_socket.srna); +} + struct bNodeSocket *nodeFindSocket(const bNode *node, eNodeSocketInOut in_out, const char *identifier) @@ -1585,13 +1591,15 @@ static void socket_id_user_decrement(bNodeSocket *sock) } } -void nodeModifySocketType( - bNodeTree *ntree, bNode *UNUSED(node), bNodeSocket *sock, int type, int subtype) +void nodeModifySocketType(bNodeTree *ntree, + bNode *UNUSED(node), + bNodeSocket *sock, + const char *idname) { - const char *idname = nodeStaticSocketType(type, subtype); + bNodeSocketType *socktype = nodeSocketTypeFind(idname); - if (!idname) { - CLOG_ERROR(&LOG, "static node socket type %d undefined", type); + if (!socktype) { + CLOG_ERROR(&LOG, "node socket type %s undefined", idname); return; } @@ -1601,9 +1609,21 @@ void nodeModifySocketType( sock->default_value = nullptr; } - sock->type = type; BLI_strncpy(sock->idname, idname, sizeof(sock->idname)); - node_socket_set_typeinfo(ntree, sock, nodeSocketTypeFind(idname)); + node_socket_set_typeinfo(ntree, sock, socktype); +} + +void nodeModifySocketTypeStatic( + bNodeTree *ntree, bNode *node, bNodeSocket *sock, int type, int subtype) +{ + const char *idname = nodeStaticSocketType(type, subtype); + + if (!idname) { + CLOG_ERROR(&LOG, "static node socket type %d undefined", type); + return; + } + + nodeModifySocketType(ntree, node, sock, idname); } bNodeSocket *nodeAddSocket(bNodeTree *ntree, @@ -1647,6 +1667,15 @@ bNodeSocket *nodeInsertSocket(bNodeTree *ntree, return sock; } +bool nodeIsStaticSocketType(const struct bNodeSocketType *stype) +{ + /* + * Cannot rely on type==SOCK_CUSTOM here, because type is 0 by default + * and can be changed on custom sockets. + */ + return RNA_struct_is_a(stype->ext_socket.srna, &RNA_NodeSocketStandard); +} + const char *nodeStaticSocketType(int type, int subtype) { switch (type) { @@ -1801,6 +1830,39 @@ const char *nodeStaticSocketInterfaceType(int type, int subtype) return nullptr; } +const char *nodeStaticSocketLabel(int type, int UNUSED(subtype)) +{ + switch (type) { + case SOCK_FLOAT: + return "Float"; + case SOCK_INT: + return "Integer"; + case SOCK_BOOLEAN: + return "Boolean"; + case SOCK_VECTOR: + return "Vector"; + case SOCK_RGBA: + return "Color"; + case SOCK_STRING: + return "String"; + case SOCK_SHADER: + return "Shader"; + case SOCK_OBJECT: + return "Object"; + case SOCK_IMAGE: + return "Image"; + case SOCK_GEOMETRY: + return "Geometry"; + case SOCK_COLLECTION: + return "Collection"; + case SOCK_TEXTURE: + return "Texture"; + case SOCK_MATERIAL: + return "Material"; + } + return nullptr; +} + bNodeSocket *nodeAddStaticSocket(bNodeTree *ntree, bNode *node, eNodeSocketInOut in_out, |