1 /*
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.
6 *
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.
11 *
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.
15 *
16 * The Original Code is Copyright (C) 2007 Blender Foundation.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup nodes
22 */
23
24 #include <stddef.h>
25 #include <string.h>
26
27 #include "DNA_node_types.h"
28
29 #include "BLI_listbase.h"
30 #include "BLI_string.h"
31 #include "BLI_utildefines.h"
32
33 #include "BLT_translation.h"
34
35 #include "BKE_node.h"
36
37 #include "RNA_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "NOD_common.h"
42 #include "node_common.h"
43 #include "node_util.h"
44
45 enum {
46 REFINE_FORWARD = 1 << 0,
47 REFINE_BACKWARD = 1 << 1,
48 };
49
50 /**** Group ****/
51
node_group_find_input_socket(bNode * groupnode,const char * identifier)52 bNodeSocket *node_group_find_input_socket(bNode *groupnode, const char *identifier)
53 {
54 bNodeSocket *sock;
55 for (sock = groupnode->inputs.first; sock; sock = sock->next) {
56 if (STREQ(sock->identifier, identifier)) {
57 return sock;
58 }
59 }
60 return NULL;
61 }
62
node_group_find_output_socket(bNode * groupnode,const char * identifier)63 bNodeSocket *node_group_find_output_socket(bNode *groupnode, const char *identifier)
64 {
65 bNodeSocket *sock;
66 for (sock = groupnode->outputs.first; sock; sock = sock->next) {
67 if (STREQ(sock->identifier, identifier)) {
68 return sock;
69 }
70 }
71 return NULL;
72 }
73
74 /* groups display their internal tree name as label */
node_group_label(bNodeTree * UNUSED (ntree),bNode * node,char * label,int maxlen)75 void node_group_label(bNodeTree *UNUSED(ntree), bNode *node, char *label, int maxlen)
76 {
77 BLI_strncpy(label, (node->id) ? node->id->name + 2 : IFACE_("Missing Data-Block"), maxlen);
78 }
79
node_group_poll_instance(bNode * node,bNodeTree * nodetree)80 bool node_group_poll_instance(bNode *node, bNodeTree *nodetree)
81 {
82 if (node->typeinfo->poll(node->typeinfo, nodetree)) {
83 bNodeTree *grouptree = (bNodeTree *)node->id;
84 if (grouptree) {
85 return nodeGroupPoll(nodetree, grouptree);
86 }
87
88 return true; /* without a linked node tree, group node is always ok */
89 }
90
91 return false;
92 }
93
nodeGroupPoll(bNodeTree * nodetree,bNodeTree * grouptree)94 int nodeGroupPoll(bNodeTree *nodetree, bNodeTree *grouptree)
95 {
96 bNode *node;
97 int valid = 1;
98
99 /* unspecified node group, generally allowed
100 * (if anything, should be avoided on operator level)
101 */
102 if (grouptree == NULL) {
103 return 1;
104 }
105
106 if (nodetree == grouptree) {
107 return 0;
108 }
109
110 for (node = grouptree->nodes.first; node; node = node->next) {
111 if (node->typeinfo->poll_instance && !node->typeinfo->poll_instance(node, nodetree)) {
112 valid = 0;
113 break;
114 }
115 }
116 return valid;
117 }
118
119 /* used for both group nodes and interface nodes */
group_verify_socket(bNodeTree * ntree,bNode * gnode,bNodeSocket * iosock,ListBase * verify_lb,int in_out)120 static bNodeSocket *group_verify_socket(
121 bNodeTree *ntree, bNode *gnode, bNodeSocket *iosock, ListBase *verify_lb, int in_out)
122 {
123 bNodeSocket *sock;
124
125 for (sock = verify_lb->first; sock; sock = sock->next) {
126 if (sock->typeinfo == iosock->typeinfo && STREQ(sock->identifier, iosock->identifier)) {
127 break;
128 }
129 }
130 if (sock) {
131 strcpy(sock->name, iosock->name);
132
133 const int mask = SOCK_HIDE_VALUE;
134 sock->flag = (sock->flag & ~mask) | (iosock->flag & mask);
135
136 if (iosock->typeinfo->interface_verify_socket) {
137 iosock->typeinfo->interface_verify_socket(ntree, iosock, gnode, sock, "interface");
138 }
139 }
140 else {
141 sock = nodeAddSocket(ntree, gnode, in_out, iosock->idname, iosock->identifier, iosock->name);
142
143 if (iosock->typeinfo->interface_init_socket) {
144 iosock->typeinfo->interface_init_socket(ntree, iosock, gnode, sock, "interface");
145 }
146 }
147
148 /* remove from list temporarily, to distinguish from orphaned sockets */
149 BLI_remlink(verify_lb, sock);
150
151 return sock;
152 }
153
154 /* used for both group nodes and interface nodes */
group_verify_socket_list(bNodeTree * ntree,bNode * gnode,ListBase * iosock_lb,ListBase * verify_lb,int in_out)155 static void group_verify_socket_list(
156 bNodeTree *ntree, bNode *gnode, ListBase *iosock_lb, ListBase *verify_lb, int in_out)
157 {
158 bNodeSocket *iosock, *sock, *nextsock;
159
160 /* step by step compare */
161
162 iosock = iosock_lb->first;
163 for (; iosock; iosock = iosock->next) {
164 /* abusing new_sock pointer for verification here! only used inside this function */
165 iosock->new_sock = group_verify_socket(ntree, gnode, iosock, verify_lb, in_out);
166 }
167 /* leftovers are removed */
168 for (sock = verify_lb->first; sock; sock = nextsock) {
169 nextsock = sock->next;
170 nodeRemoveSocket(ntree, gnode, sock);
171 }
172 /* and we put back the verified sockets */
173 iosock = iosock_lb->first;
174 for (; iosock; iosock = iosock->next) {
175 if (iosock->new_sock) {
176 BLI_addtail(verify_lb, iosock->new_sock);
177 iosock->new_sock = NULL;
178 }
179 }
180 }
181
182 /* make sure all group node in ntree, which use ngroup, are sync'd */
node_group_update(struct bNodeTree * ntree,struct bNode * node)183 void node_group_update(struct bNodeTree *ntree, struct bNode *node)
184 {
185 /* check inputs and outputs, and remove or insert them */
186 if (node->id == NULL) {
187 nodeRemoveAllSockets(ntree, node);
188 }
189 else {
190 bNodeTree *ngroup = (bNodeTree *)node->id;
191 group_verify_socket_list(ntree, node, &ngroup->inputs, &node->inputs, SOCK_IN);
192 group_verify_socket_list(ntree, node, &ngroup->outputs, &node->outputs, SOCK_OUT);
193 }
194 }
195
196 /**** FRAME ****/
197
node_frame_init(bNodeTree * UNUSED (ntree),bNode * node)198 static void node_frame_init(bNodeTree *UNUSED(ntree), bNode *node)
199 {
200 NodeFrame *data = (NodeFrame *)MEM_callocN(sizeof(NodeFrame), "frame node storage");
201 node->storage = data;
202
203 data->flag |= NODE_FRAME_SHRINK;
204
205 data->label_size = 20;
206 }
207
register_node_type_frame(void)208 void register_node_type_frame(void)
209 {
210 /* frame type is used for all tree types, needs dynamic allocation */
211 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "frame node type");
212 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
213
214 node_type_base(ntype, NODE_FRAME, "Frame", NODE_CLASS_LAYOUT, NODE_BACKGROUND);
215 node_type_init(ntype, node_frame_init);
216 node_type_storage(ntype, "NodeFrame", node_free_standard_storage, node_copy_standard_storage);
217 node_type_size(ntype, 150, 100, 0);
218
219 nodeRegisterType(ntype);
220 }
221
222 /* **************** REROUTE ******************** */
223
224 /* simple, only a single input and output here */
node_reroute_update_internal_links(bNodeTree * ntree,bNode * node)225 static void node_reroute_update_internal_links(bNodeTree *ntree, bNode *node)
226 {
227 bNodeLink *link;
228
229 /* Security check! */
230 if (!ntree) {
231 return;
232 }
233
234 link = MEM_callocN(sizeof(bNodeLink), "internal node link");
235 link->fromnode = node;
236 link->fromsock = node->inputs.first;
237 link->tonode = node;
238 link->tosock = node->outputs.first;
239 /* internal link is always valid */
240 link->flag |= NODE_LINK_VALID;
241 BLI_addtail(&node->internal_links, link);
242 }
243
node_reroute_init(bNodeTree * ntree,bNode * node)244 static void node_reroute_init(bNodeTree *ntree, bNode *node)
245 {
246 /* Note: Cannot use socket templates for this, since it would reset the socket type
247 * on each file read via the template verification procedure.
248 */
249 nodeAddStaticSocket(ntree, node, SOCK_IN, SOCK_RGBA, PROP_NONE, "Input", "Input");
250 nodeAddStaticSocket(ntree, node, SOCK_OUT, SOCK_RGBA, PROP_NONE, "Output", "Output");
251 }
252
register_node_type_reroute(void)253 void register_node_type_reroute(void)
254 {
255 /* frame type is used for all tree types, needs dynamic allocation */
256 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "frame node type");
257 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
258
259 node_type_base(ntype, NODE_REROUTE, "Reroute", NODE_CLASS_LAYOUT, 0);
260 node_type_init(ntype, node_reroute_init);
261 node_type_internal_links(ntype, node_reroute_update_internal_links);
262
263 nodeRegisterType(ntype);
264 }
265
node_reroute_inherit_type_recursive(bNodeTree * ntree,bNode * node,int flag)266 static void node_reroute_inherit_type_recursive(bNodeTree *ntree, bNode *node, int flag)
267 {
268 bNodeSocket *input = node->inputs.first;
269 bNodeSocket *output = node->outputs.first;
270 bNodeLink *link;
271 int type = SOCK_FLOAT;
272 const char *type_idname = nodeStaticSocketType(type, PROP_NONE);
273
274 /* XXX it would be a little bit more efficient to restrict actual updates
275 * to reroute nodes connected to an updated node, but there's no reliable flag
276 * to indicate updated nodes (node->update is not set on linking).
277 */
278
279 node->done = 1;
280
281 /* recursive update */
282 for (link = ntree->links.first; link; link = link->next) {
283 bNode *fromnode = link->fromnode;
284 bNode *tonode = link->tonode;
285 if (!tonode || !fromnode) {
286 continue;
287 }
288 if (nodeLinkIsHidden(link)) {
289 continue;
290 }
291
292 if (flag & REFINE_FORWARD) {
293 if (tonode == node && fromnode->type == NODE_REROUTE && !fromnode->done) {
294 node_reroute_inherit_type_recursive(ntree, fromnode, REFINE_FORWARD);
295 }
296 }
297 if (flag & REFINE_BACKWARD) {
298 if (fromnode == node && tonode->type == NODE_REROUTE && !tonode->done) {
299 node_reroute_inherit_type_recursive(ntree, tonode, REFINE_BACKWARD);
300 }
301 }
302 }
303
304 /* determine socket type from unambiguous input/output connection if possible */
305 if (nodeSocketLinkLimit(input) == 1 && input->link) {
306 type = input->link->fromsock->type;
307 type_idname = nodeStaticSocketType(type, PROP_NONE);
308 }
309 else if (nodeSocketLinkLimit(output) == 1 && output->link) {
310 type = output->link->tosock->type;
311 type_idname = nodeStaticSocketType(type, PROP_NONE);
312 }
313
314 if (input->type != type) {
315 bNodeSocket *ninput = nodeAddSocket(ntree, node, SOCK_IN, type_idname, "input", "Input");
316 for (link = ntree->links.first; link; link = link->next) {
317 if (link->tosock == input) {
318 link->tosock = ninput;
319 ninput->link = link;
320 }
321 }
322 nodeRemoveSocket(ntree, node, input);
323 }
324
325 if (output->type != type) {
326 bNodeSocket *noutput = nodeAddSocket(ntree, node, SOCK_OUT, type_idname, "output", "Output");
327 for (link = ntree->links.first; link; link = link->next) {
328 if (link->fromsock == output) {
329 link->fromsock = noutput;
330 }
331 }
332 nodeRemoveSocket(ntree, node, output);
333 }
334
335 nodeUpdateInternalLinks(ntree, node);
336 }
337
338 /* Global update function for Reroute node types.
339 * This depends on connected nodes, so must be done as a tree-wide update.
340 */
ntree_update_reroute_nodes(bNodeTree * ntree)341 void ntree_update_reroute_nodes(bNodeTree *ntree)
342 {
343 bNode *node;
344
345 /* clear tags */
346 for (node = ntree->nodes.first; node; node = node->next) {
347 node->done = 0;
348 }
349
350 for (node = ntree->nodes.first; node; node = node->next) {
351 if (node->type == NODE_REROUTE && !node->done) {
352 node_reroute_inherit_type_recursive(ntree, node, REFINE_FORWARD | REFINE_BACKWARD);
353 }
354 }
355 }
356
node_is_connected_to_output_recursive(bNodeTree * ntree,bNode * node)357 static bool node_is_connected_to_output_recursive(bNodeTree *ntree, bNode *node)
358 {
359 bNodeLink *link;
360
361 /* avoid redundant checks, and infinite loops in case of cyclic node links */
362 if (node->done) {
363 return false;
364 }
365 node->done = 1;
366
367 /* main test, done before child loop so it catches output nodes themselves as well */
368 if (node->typeinfo->nclass == NODE_CLASS_OUTPUT && node->flag & NODE_DO_OUTPUT) {
369 return true;
370 }
371
372 /* test all connected nodes, first positive find is sufficient to return true */
373 for (link = ntree->links.first; link; link = link->next) {
374 if (link->fromnode == node) {
375 if (node_is_connected_to_output_recursive(ntree, link->tonode)) {
376 return true;
377 }
378 }
379 }
380 return false;
381 }
382
BKE_node_is_connected_to_output(bNodeTree * ntree,bNode * node)383 bool BKE_node_is_connected_to_output(bNodeTree *ntree, bNode *node)
384 {
385 bNode *tnode;
386
387 /* clear flags */
388 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
389 tnode->done = 0;
390 }
391
392 return node_is_connected_to_output_recursive(ntree, node);
393 }
394
BKE_node_tree_unlink_id(ID * id,struct bNodeTree * ntree)395 void BKE_node_tree_unlink_id(ID *id, struct bNodeTree *ntree)
396 {
397 bNode *node;
398
399 for (node = ntree->nodes.first; node; node = node->next) {
400 if (node->id == id) {
401 node->id = NULL;
402 }
403 }
404 }
405
406 /**** GROUP_INPUT / GROUP_OUTPUT ****/
407
node_group_input_init(bNodeTree * ntree,bNode * node)408 static void node_group_input_init(bNodeTree *ntree, bNode *node)
409 {
410 node_group_input_update(ntree, node);
411 }
412
node_group_input_find_socket(bNode * node,const char * identifier)413 bNodeSocket *node_group_input_find_socket(bNode *node, const char *identifier)
414 {
415 bNodeSocket *sock;
416 for (sock = node->outputs.first; sock; sock = sock->next) {
417 if (STREQ(sock->identifier, identifier)) {
418 return sock;
419 }
420 }
421 return NULL;
422 }
423
node_group_input_update(bNodeTree * ntree,bNode * node)424 void node_group_input_update(bNodeTree *ntree, bNode *node)
425 {
426 bNodeSocket *extsock = node->outputs.last;
427 bNodeLink *link, *linknext, *exposelink;
428 /* Adding a tree socket and verifying will remove the extension socket!
429 * This list caches the existing links from the extension socket
430 * so they can be recreated after verification.
431 */
432 ListBase tmplinks;
433
434 /* find links from the extension socket and store them */
435 BLI_listbase_clear(&tmplinks);
436 for (link = ntree->links.first; link; link = linknext) {
437 linknext = link->next;
438 if (nodeLinkIsHidden(link)) {
439 continue;
440 }
441
442 if (link->fromsock == extsock) {
443 bNodeLink *tlink = MEM_callocN(sizeof(bNodeLink), "temporary link");
444 *tlink = *link;
445 BLI_addtail(&tmplinks, tlink);
446
447 nodeRemLink(ntree, link);
448 }
449 }
450
451 /* find valid link to expose */
452 exposelink = NULL;
453 for (link = tmplinks.first; link; link = link->next) {
454 /* XXX Multiple sockets can be connected to the extension socket at once,
455 * in that case the arbitrary first link determines name and type.
456 * This could be improved by choosing the "best" type among all links,
457 * whatever that means.
458 */
459 if (link->tosock->type != SOCK_CUSTOM) {
460 exposelink = link;
461 break;
462 }
463 }
464
465 if (exposelink) {
466 bNodeSocket *gsock, *newsock;
467
468 gsock = ntreeAddSocketInterfaceFromSocket(ntree, exposelink->tonode, exposelink->tosock);
469
470 node_group_input_update(ntree, node);
471 newsock = node_group_input_find_socket(node, gsock->identifier);
472
473 /* redirect links from the extension socket */
474 for (link = tmplinks.first; link; link = link->next) {
475 nodeAddLink(ntree, node, newsock, link->tonode, link->tosock);
476 }
477 }
478
479 BLI_freelistN(&tmplinks);
480
481 /* check inputs and outputs, and remove or insert them */
482 {
483 /* value_in_out inverted for interface nodes to get correct socket value_property */
484 group_verify_socket_list(ntree, node, &ntree->inputs, &node->outputs, SOCK_OUT);
485
486 /* add virtual extension socket */
487 nodeAddSocket(ntree, node, SOCK_OUT, "NodeSocketVirtual", "__extend__", "");
488 }
489 }
490
register_node_type_group_input(void)491 void register_node_type_group_input(void)
492 {
493 /* used for all tree types, needs dynamic allocation */
494 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "node type");
495 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
496
497 node_type_base(ntype, NODE_GROUP_INPUT, "Group Input", NODE_CLASS_INTERFACE, 0);
498 node_type_size(ntype, 140, 80, 400);
499 node_type_init(ntype, node_group_input_init);
500 node_type_update(ntype, node_group_input_update);
501
502 nodeRegisterType(ntype);
503 }
504
node_group_output_init(bNodeTree * ntree,bNode * node)505 static void node_group_output_init(bNodeTree *ntree, bNode *node)
506 {
507 node_group_output_update(ntree, node);
508 }
509
node_group_output_find_socket(bNode * node,const char * identifier)510 bNodeSocket *node_group_output_find_socket(bNode *node, const char *identifier)
511 {
512 bNodeSocket *sock;
513 for (sock = node->inputs.first; sock; sock = sock->next) {
514 if (STREQ(sock->identifier, identifier)) {
515 return sock;
516 }
517 }
518 return NULL;
519 }
520
node_group_output_update(bNodeTree * ntree,bNode * node)521 void node_group_output_update(bNodeTree *ntree, bNode *node)
522 {
523 bNodeSocket *extsock = node->inputs.last;
524 bNodeLink *link, *linknext, *exposelink;
525 /* Adding a tree socket and verifying will remove the extension socket!
526 * This list caches the existing links to the extension socket
527 * so they can be recreated after verification.
528 */
529 ListBase tmplinks;
530
531 /* find links to the extension socket and store them */
532 BLI_listbase_clear(&tmplinks);
533 for (link = ntree->links.first; link; link = linknext) {
534 linknext = link->next;
535 if (nodeLinkIsHidden(link)) {
536 continue;
537 }
538
539 if (link->tosock == extsock) {
540 bNodeLink *tlink = MEM_callocN(sizeof(bNodeLink), "temporary link");
541 *tlink = *link;
542 BLI_addtail(&tmplinks, tlink);
543
544 nodeRemLink(ntree, link);
545 }
546 }
547
548 /* find valid link to expose */
549 exposelink = NULL;
550 for (link = tmplinks.first; link; link = link->next) {
551 /* XXX Multiple sockets can be connected to the extension socket at once,
552 * in that case the arbitrary first link determines name and type.
553 * This could be improved by choosing the "best" type among all links,
554 * whatever that means.
555 */
556 if (link->fromsock->type != SOCK_CUSTOM) {
557 exposelink = link;
558 break;
559 }
560 }
561
562 if (exposelink) {
563 bNodeSocket *gsock, *newsock;
564
565 /* XXX what if connecting virtual to virtual socket?? */
566 gsock = ntreeAddSocketInterfaceFromSocket(ntree, exposelink->fromnode, exposelink->fromsock);
567
568 node_group_output_update(ntree, node);
569 newsock = node_group_output_find_socket(node, gsock->identifier);
570
571 /* redirect links to the extension socket */
572 for (link = tmplinks.first; link; link = link->next) {
573 nodeAddLink(ntree, link->fromnode, link->fromsock, node, newsock);
574 }
575 }
576
577 BLI_freelistN(&tmplinks);
578
579 /* check inputs and outputs, and remove or insert them */
580 {
581 /* value_in_out inverted for interface nodes to get correct socket value_property */
582 group_verify_socket_list(ntree, node, &ntree->outputs, &node->inputs, SOCK_IN);
583
584 /* add virtual extension socket */
585 nodeAddSocket(ntree, node, SOCK_IN, "NodeSocketVirtual", "__extend__", "");
586 }
587 }
588
register_node_type_group_output(void)589 void register_node_type_group_output(void)
590 {
591 /* used for all tree types, needs dynamic allocation */
592 bNodeType *ntype = MEM_callocN(sizeof(bNodeType), "node type");
593 ntype->free_self = (void (*)(bNodeType *))MEM_freeN;
594
595 node_type_base(ntype, NODE_GROUP_OUTPUT, "Group Output", NODE_CLASS_INTERFACE, 0);
596 node_type_size(ntype, 140, 80, 400);
597 node_type_init(ntype, node_group_output_init);
598 node_type_update(ntype, node_group_output_update);
599
600 nodeRegisterType(ntype);
601 }
602