2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2007 Blender Foundation.
17 * All rights reserved.
27 #include "DNA_node_types.h"
29 #include "BLI_listbase.h"
30 #include "BLI_string.h"
31 #include "BLI_utildefines.h"
33 #include "BLT_translation.h"
37 #include "RNA_types.h"
39 #include "MEM_guardedalloc.h"
41 #include "NOD_common.h"
42 #include "node_common.h"
43 #include "node_util.h"
46 REFINE_FORWARD = 1 << 0,
47 REFINE_BACKWARD = 1 << 1,
50 /* -------------------------------------------------------------------- */
54 bNodeSocket *node_group_find_input_socket(bNode *groupnode, const char *identifier)
57 for (sock = groupnode->inputs.first; sock; sock = sock->next) {
58 if (STREQ(sock->identifier, identifier)) {
65 bNodeSocket *node_group_find_output_socket(bNode *groupnode, const char *identifier)
68 for (sock = groupnode->outputs.first; sock; sock = sock->next) {
69 if (STREQ(sock->identifier, identifier)) {
76 /* groups display their internal tree name as label */
77 void node_group_label(bNodeTree *UNUSED(ntree), bNode *node, char *label, int maxlen)
79 BLI_strncpy(label, (node->id) ? node->id->name + 2 : IFACE_("Missing Data-Block"), maxlen);
82 bool node_group_poll_instance(bNode *node, bNodeTree *nodetree)
84 if (node->typeinfo->poll(node->typeinfo, nodetree)) {
85 bNodeTree *grouptree = (bNodeTree *)node->id;
87 return nodeGroupPoll(nodetree, grouptree);
90 return true; /* without a linked node tree, group node is always ok */
96 int nodeGroupPoll(bNodeTree *nodetree, bNodeTree *grouptree)
101 /* unspecified node group, generally allowed
102 * (if anything, should be avoided on operator level)
104 if (grouptree == NULL) {
108 if (nodetree == grouptree) {
112 for (node = grouptree->nodes.first; node; node = node->next) {
113 if (node->typeinfo->poll_instance && !node->typeinfo->poll_instance(node, nodetree)) {
121 /* used for both group nodes and interface nodes */
122 static bNodeSocket *group_verify_socket(
123 bNodeTree *ntree, bNode *gnode, bNodeSocket *iosock, ListBase *verify_lb, int in_out)
127 for (sock = verify_lb->first; sock; sock = sock->next) {
128 if (sock->typeinfo == iosock->typeinfo && STREQ(sock->identifier, iosock->identifier)) {
133 strcpy(sock->name, iosock->name);
135 const int mask = SOCK_HIDE_VALUE;
136 sock->flag = (sock->flag & ~mask) | (iosock->flag & mask);
138 if (iosock->typeinfo->interface_verify_socket) {
139 iosock->typeinfo->interface_verify_socket(ntree, iosock, gnode, sock, "interface");
143 sock = nodeAddSocket(ntree, gnode, in_out, iosock->idname, iosock->identifier, iosock->name);
145 if (iosock->typeinfo->interface_init_socket) {
146 iosock->typeinfo->interface_init_socket(ntree, iosock, gnode, sock, "interface");
150 /* remove from list temporarily, to distinguish from orphaned sockets */
151 BLI_remlink(verify_lb, sock);
156 /* used for both group nodes and interface nodes */
157 static void group_verify_socket_list(
158 bNodeTree *ntree, bNode *gnode, ListBase *iosock_lb, ListBase *verify_lb, int in_out)
160 bNodeSocket *iosock, *sock, *nextsock;
162 /* step by step compare */
164 iosock = iosock_lb->first;
165 for (; iosock; iosock = iosock->next) {
166 /* abusing new_sock pointer for verification here! only used inside this function */
167 iosock->new_sock = group_verify_socket(ntree, gnode, iosock, verify_lb, in_out);
169 /* leftovers are removed */
170 for (sock = verify_lb->first; sock; sock = nextsock) {
171 nextsock = sock->next;
172 nodeRemoveSocket(ntree, gnode, sock);
174 /* and we put back the verified sockets */
175 iosock = iosock_lb->first;
176 for (; iosock; iosock = iosock->next) {
177 if (iosock->new_sock) {
178 BLI_addtail(verify_lb, iosock->new_sock);
179 iosock->new_sock = NULL;
184 /* make sure all group node in ntree, which use ngroup, are sync'd */
185 void node_group_update(struct bNodeTree *ntree, struct bNode *node)
187 /* check inputs and outputs, and remove or insert them */
188 if (node->id == NULL) {
189 nodeRemoveAllSockets(ntree, node);
191 else if ((ID_IS_LINKED(node->id) && (node->id->tag & LIB_TAG_MISSING))) {
192 /* Missing datablock, leave sockets unchanged so that when it comes back
193 * the links remain valid. */
196 bNodeTree *ngroup = (bNodeTree *)node->id;
197 group_verify_socket_list(ntree, node, &ngroup->inputs, &node->inputs, SOCK_IN);
198 group_verify_socket_list(ntree, node, &ngroup->outputs, &node->outputs, SOCK_OUT);
204 /* -------------------------------------------------------------------- */
208 static void node_frame_init(bNodeTree *UNUSED(ntree), bNode *node)
210 NodeFrame *data = (NodeFrame *)MEM_callocN(sizeof(NodeFrame), "frame node storage");
211 node->storage = data;
213 data->flag |= NODE_FRAME_SHRINK;
215 data->label_size = 20;
218 void register_node_type_frame(void)
220 /* frame type is used for all tree types, needs dynamic allocation */
221 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "frame node type");
222 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
224 node_type_base(ntype, NODE_FRAME, "Frame", NODE_CLASS_LAYOUT, NODE_BACKGROUND);
225 node_type_init(ntype, node_frame_init);
226 node_type_storage(ntype, "NodeFrame", node_free_standard_storage, node_copy_standard_storage);
227 node_type_size(ntype, 150, 100, 0);
229 nodeRegisterType(ntype);
234 /* -------------------------------------------------------------------- */
235 /** \name Node Re-Route
238 /* simple, only a single input and output here */
239 static void node_reroute_update_internal_links(bNodeTree *ntree, bNode *node)
243 /* Security check! */
248 link = MEM_callocN(sizeof(bNodeLink), "internal node link");
249 link->fromnode = node;
250 link->fromsock = node->inputs.first;
252 link->tosock = node->outputs.first;
253 /* internal link is always valid */
254 link->flag |= NODE_LINK_VALID;
255 BLI_addtail(&node->internal_links, link);
258 static void node_reroute_init(bNodeTree *ntree, bNode *node)
260 /* Note: Cannot use socket templates for this, since it would reset the socket type
261 * on each file read via the template verification procedure.
263 nodeAddStaticSocket(ntree, node, SOCK_IN, SOCK_RGBA, PROP_NONE, "Input", "Input");
264 nodeAddStaticSocket(ntree, node, SOCK_OUT, SOCK_RGBA, PROP_NONE, "Output", "Output");
267 void register_node_type_reroute(void)
269 /* frame type is used for all tree types, needs dynamic allocation */
270 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "frame node type");
271 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
273 node_type_base(ntype, NODE_REROUTE, "Reroute", NODE_CLASS_LAYOUT, 0);
274 node_type_init(ntype, node_reroute_init);
275 node_type_internal_links(ntype, node_reroute_update_internal_links);
277 nodeRegisterType(ntype);
280 static void node_reroute_inherit_type_recursive(bNodeTree *ntree, bNode *node, int flag)
282 bNodeSocket *input = node->inputs.first;
283 bNodeSocket *output = node->outputs.first;
285 int type = SOCK_FLOAT;
286 const char *type_idname = nodeStaticSocketType(type, PROP_NONE);
288 /* XXX it would be a little bit more efficient to restrict actual updates
289 * to reroute nodes connected to an updated node, but there's no reliable flag
290 * to indicate updated nodes (node->update is not set on linking).
295 /* recursive update */
296 for (link = ntree->links.first; link; link = link->next) {
297 bNode *fromnode = link->fromnode;
298 bNode *tonode = link->tonode;
299 if (!tonode || !fromnode) {
302 if (nodeLinkIsHidden(link)) {
306 if (flag & REFINE_FORWARD) {
307 if (tonode == node && fromnode->type == NODE_REROUTE && !fromnode->done) {
308 node_reroute_inherit_type_recursive(ntree, fromnode, REFINE_FORWARD);
311 if (flag & REFINE_BACKWARD) {
312 if (fromnode == node && tonode->type == NODE_REROUTE && !tonode->done) {
313 node_reroute_inherit_type_recursive(ntree, tonode, REFINE_BACKWARD);
318 /* determine socket type from unambiguous input/output connection if possible */
319 if (nodeSocketLinkLimit(input) == 1 && input->link) {
320 type = input->link->fromsock->type;
321 type_idname = nodeStaticSocketType(type, PROP_NONE);
323 else if (nodeSocketLinkLimit(output) == 1 && output->link) {
324 type = output->link->tosock->type;
325 type_idname = nodeStaticSocketType(type, PROP_NONE);
328 if (input->type != type) {
329 bNodeSocket *ninput = nodeAddSocket(ntree, node, SOCK_IN, type_idname, "input", "Input");
330 for (link = ntree->links.first; link; link = link->next) {
331 if (link->tosock == input) {
332 link->tosock = ninput;
336 nodeRemoveSocket(ntree, node, input);
339 if (output->type != type) {
340 bNodeSocket *noutput = nodeAddSocket(ntree, node, SOCK_OUT, type_idname, "output", "Output");
341 for (link = ntree->links.first; link; link = link->next) {
342 if (link->fromsock == output) {
343 link->fromsock = noutput;
346 nodeRemoveSocket(ntree, node, output);
349 nodeUpdateInternalLinks(ntree, node);
352 /* Global update function for Reroute node types.
353 * This depends on connected nodes, so must be done as a tree-wide update.
355 void ntree_update_reroute_nodes(bNodeTree *ntree)
360 for (node = ntree->nodes.first; node; node = node->next) {
364 for (node = ntree->nodes.first; node; node = node->next) {
365 if (node->type == NODE_REROUTE && !node->done) {
366 node_reroute_inherit_type_recursive(ntree, node, REFINE_FORWARD | REFINE_BACKWARD);
371 static bool node_is_connected_to_output_recursive(bNodeTree *ntree, bNode *node)
375 /* avoid redundant checks, and infinite loops in case of cyclic node links */
381 /* main test, done before child loop so it catches output nodes themselves as well */
382 if (node->typeinfo->nclass == NODE_CLASS_OUTPUT && node->flag & NODE_DO_OUTPUT) {
386 /* test all connected nodes, first positive find is sufficient to return true */
387 for (link = ntree->links.first; link; link = link->next) {
388 if (link->fromnode == node) {
389 if (node_is_connected_to_output_recursive(ntree, link->tonode)) {
397 bool BKE_node_is_connected_to_output(bNodeTree *ntree, bNode *node)
402 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
406 return node_is_connected_to_output_recursive(ntree, node);
409 void BKE_node_tree_unlink_id(ID *id, struct bNodeTree *ntree)
413 for (node = ntree->nodes.first; node; node = node->next) {
414 if (node->id == id) {
422 /* -------------------------------------------------------------------- */
423 /** \name Node #GROUP_INPUT / #GROUP_OUTPUT
426 static void node_group_input_init(bNodeTree *ntree, bNode *node)
428 node_group_input_update(ntree, node);
431 bNodeSocket *node_group_input_find_socket(bNode *node, const char *identifier)
434 for (sock = node->outputs.first; sock; sock = sock->next) {
435 if (STREQ(sock->identifier, identifier)) {
442 void node_group_input_update(bNodeTree *ntree, bNode *node)
444 bNodeSocket *extsock = node->outputs.last;
445 bNodeLink *link, *linknext, *exposelink;
446 /* Adding a tree socket and verifying will remove the extension socket!
447 * This list caches the existing links from the extension socket
448 * so they can be recreated after verification.
452 /* find links from the extension socket and store them */
453 BLI_listbase_clear(&tmplinks);
454 for (link = ntree->links.first; link; link = linknext) {
455 linknext = link->next;
456 if (nodeLinkIsHidden(link)) {
460 if (link->fromsock == extsock) {
461 bNodeLink *tlink = MEM_callocN(sizeof(bNodeLink), "temporary link");
463 BLI_addtail(&tmplinks, tlink);
465 nodeRemLink(ntree, link);
469 /* find valid link to expose */
471 for (link = tmplinks.first; link; link = link->next) {
472 /* XXX Multiple sockets can be connected to the extension socket at once,
473 * in that case the arbitrary first link determines name and type.
474 * This could be improved by choosing the "best" type among all links,
475 * whatever that means.
477 if (link->tosock->type != SOCK_CUSTOM) {
484 bNodeSocket *gsock, *newsock;
486 gsock = ntreeAddSocketInterfaceFromSocket(ntree, exposelink->tonode, exposelink->tosock);
488 node_group_input_update(ntree, node);
489 newsock = node_group_input_find_socket(node, gsock->identifier);
491 /* redirect links from the extension socket */
492 for (link = tmplinks.first; link; link = link->next) {
493 nodeAddLink(ntree, node, newsock, link->tonode, link->tosock);
497 BLI_freelistN(&tmplinks);
499 /* check inputs and outputs, and remove or insert them */
501 /* value_in_out inverted for interface nodes to get correct socket value_property */
502 group_verify_socket_list(ntree, node, &ntree->inputs, &node->outputs, SOCK_OUT);
504 /* add virtual extension socket */
505 nodeAddSocket(ntree, node, SOCK_OUT, "NodeSocketVirtual", "__extend__", "");
509 void register_node_type_group_input(void)
511 /* used for all tree types, needs dynamic allocation */
512 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "node type");
513 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
515 node_type_base(ntype, NODE_GROUP_INPUT, "Group Input", NODE_CLASS_INTERFACE, 0);
516 node_type_size(ntype, 140, 80, 400);
517 node_type_init(ntype, node_group_input_init);
518 node_type_update(ntype, node_group_input_update);
520 nodeRegisterType(ntype);
523 static void node_group_output_init(bNodeTree *ntree, bNode *node)
525 node_group_output_update(ntree, node);
528 bNodeSocket *node_group_output_find_socket(bNode *node, const char *identifier)
531 for (sock = node->inputs.first; sock; sock = sock->next) {
532 if (STREQ(sock->identifier, identifier)) {
539 void node_group_output_update(bNodeTree *ntree, bNode *node)
541 bNodeSocket *extsock = node->inputs.last;
542 bNodeLink *link, *linknext, *exposelink;
543 /* Adding a tree socket and verifying will remove the extension socket!
544 * This list caches the existing links to the extension socket
545 * so they can be recreated after verification.
549 /* find links to the extension socket and store them */
550 BLI_listbase_clear(&tmplinks);
551 for (link = ntree->links.first; link; link = linknext) {
552 linknext = link->next;
553 if (nodeLinkIsHidden(link)) {
557 if (link->tosock == extsock) {
558 bNodeLink *tlink = MEM_callocN(sizeof(bNodeLink), "temporary link");
560 BLI_addtail(&tmplinks, tlink);
562 nodeRemLink(ntree, link);
566 /* find valid link to expose */
568 for (link = tmplinks.first; link; link = link->next) {
569 /* XXX Multiple sockets can be connected to the extension socket at once,
570 * in that case the arbitrary first link determines name and type.
571 * This could be improved by choosing the "best" type among all links,
572 * whatever that means.
574 if (link->fromsock->type != SOCK_CUSTOM) {
581 bNodeSocket *gsock, *newsock;
583 /* XXX what if connecting virtual to virtual socket?? */
584 gsock = ntreeAddSocketInterfaceFromSocket(ntree, exposelink->fromnode, exposelink->fromsock);
586 node_group_output_update(ntree, node);
587 newsock = node_group_output_find_socket(node, gsock->identifier);
589 /* redirect links to the extension socket */
590 for (link = tmplinks.first; link; link = link->next) {
591 nodeAddLink(ntree, link->fromnode, link->fromsock, node, newsock);
595 BLI_freelistN(&tmplinks);
597 /* check inputs and outputs, and remove or insert them */
599 /* value_in_out inverted for interface nodes to get correct socket value_property */
600 group_verify_socket_list(ntree, node, &ntree->outputs, &node->inputs, SOCK_IN);
602 /* add virtual extension socket */
603 nodeAddSocket(ntree, node, SOCK_IN, "NodeSocketVirtual", "__extend__", "");
607 void register_node_type_group_output(void)
609 /* used for all tree types, needs dynamic allocation */
610 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "node type");
611 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
613 node_type_base(ntype, NODE_GROUP_OUTPUT, "Group Output", NODE_CLASS_INTERFACE, 0);
614 node_type_size(ntype, 140, 80, 400);
615 node_type_init(ntype, node_group_output_init);
616 node_type_update(ntype, node_group_output_update);
618 nodeRegisterType(ntype);