Cycles: svn merge -r41225:41232 ^/trunk/blender
[blender.git] / source / blender / blenkernel / intern / node.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Bob Holcomb.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/node.c
29  *  \ingroup bke
30  */
31
32
33 #if 0 /* pynodes commented for now */
34 #  ifdef WITH_PYTHON
35 #    include <Python.h>
36 #  endif
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <limits.h>
45
46 #include "DNA_action_types.h"
47 #include "DNA_anim_types.h"
48 #include "DNA_node_types.h"
49 #include "DNA_node_types.h"
50 #include "DNA_scene_types.h"
51
52 #include "BLI_string.h"
53 #include "BLI_math.h"
54 #include "BLI_listbase.h"
55 #include "BLI_path_util.h"
56 #include "BLI_utildefines.h"
57
58 #include "BKE_animsys.h"
59 #include "BKE_action.h"
60 #include "BKE_fcurve.h"
61 #include "BKE_global.h"
62 #include "BKE_image.h"
63 #include "BKE_library.h"
64 #include "BKE_main.h"
65 #include "BKE_node.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_utildefines.h"
68
69 #include "BLI_listbase.h"
70
71 #include "RNA_access.h"
72
73 #include "NOD_socket.h"
74 #include "NOD_composite.h"
75 #include "NOD_shader.h"
76 #include "NOD_texture.h"
77
78
79 bNodeTreeType *ntreeGetType(int type)
80 {
81         static bNodeTreeType *types[NUM_NTREE_TYPES];
82         static int types_init = 1;
83         if (types_init) {
84                 types[NTREE_SHADER] = &ntreeType_Shader;
85                 types[NTREE_COMPOSIT] = &ntreeType_Composite;
86                 types[NTREE_TEXTURE] = &ntreeType_Texture;
87                 types_init = 0;
88         }
89         
90         if(type >= 0 && type < NUM_NTREE_TYPES) {
91                 return types[type];
92         }
93         else {
94                 return NULL;
95         }
96 }
97
98 static bNodeType *node_get_type(bNodeTree *ntree, int type)
99 {
100         bNodeType *ntype = ntreeGetType(ntree->type)->node_types.first;
101         for(; ntype; ntype= ntype->next)
102                 if(ntype->type==type)
103                         return ntype;
104         
105         return NULL;
106 }
107
108 bNodeType *ntreeGetNodeType(bNodeTree *ntree)
109 {
110         return node_get_type(ntree, ntree->nodetype);
111 }
112
113 bNodeSocketType *ntreeGetSocketType(int type)
114 {
115         static bNodeSocketType *types[NUM_SOCKET_TYPES]= {NULL};
116         static int types_init = 1;
117
118         if (types_init) {
119                 node_socket_type_init(types);
120                 types_init= 0;
121         }
122
123         if(type < NUM_SOCKET_TYPES) {
124                 return types[type];
125         }
126         else {
127                 return NULL;
128         }
129 }
130
131 void ntreeInitTypes(bNodeTree *ntree)
132 {
133         bNode *node, *next;
134         
135         for(node= ntree->nodes.first; node; node= next) {
136                 next= node->next;
137                 
138                 node->typeinfo= node_get_type(ntree, node->type);
139                 
140                 if(node->type==NODE_DYNAMIC) {
141                         /* needed info if the pynode script fails now: */
142                         node->storage= ntree;
143                         if(node->id!=NULL) { /* not an empty script node */
144                                 node->custom1= 0;
145                                 node->custom1= BSET(node->custom1,NODE_DYNAMIC_ADDEXIST);
146                         }
147 //                      if(node->typeinfo)
148 //                              node->typeinfo->initfunc(node);
149                 }
150
151                 if(node->typeinfo==NULL) {
152                         printf("Error: Node type %s doesn't exist anymore, removed\n", node->name);
153                         nodeFreeNode(ntree, node);
154                 }
155         }
156                         
157         ntree->init |= NTREE_TYPE_INIT;
158 }
159
160 static bNodeSocket *make_socket(bNodeTree *UNUSED(ntree), int in_out, const char *name, int type)
161 {
162         bNodeSocketType *stype= ntreeGetSocketType(type);
163         bNodeSocket *sock;
164         
165         sock= MEM_callocN(sizeof(bNodeSocket), "sock");
166         
167         BLI_strncpy(sock->name, name, NODE_MAXSTR);
168         sock->limit = (in_out==SOCK_IN ? 1 : 0xFFF);
169         sock->type= type;
170         sock->storage = NULL;
171         
172         if (stype->value_structsize > 0)
173                 sock->default_value = MEM_callocN(stype->value_structsize, "default socket value");
174         
175         return sock;
176 }
177
178 bNodeSocket *nodeAddSocket(bNodeTree *ntree, bNode *node, int in_out, const char *name, int type)
179 {
180         bNodeSocket *sock = make_socket(ntree, in_out, name, type);
181         if (in_out==SOCK_IN)
182                 BLI_addtail(&node->inputs, sock);
183         else if (in_out==SOCK_OUT)
184                 BLI_addtail(&node->outputs, sock);
185         
186         node->update |= NODE_UPDATE;
187         
188         return sock;
189 }
190
191 bNodeSocket *nodeInsertSocket(bNodeTree *ntree, bNode *node, int in_out, bNodeSocket *next_sock, const char *name, int type)
192 {
193         bNodeSocket *sock = make_socket(ntree, in_out, name, type);
194         if (in_out==SOCK_IN)
195                 BLI_insertlinkbefore(&node->inputs, next_sock, sock);
196         else if (in_out==SOCK_OUT)
197                 BLI_insertlinkbefore(&node->outputs, next_sock, sock);
198         
199         node->update |= NODE_UPDATE;
200         
201         return sock;
202 }
203
204 void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock)
205 {
206         bNodeLink *link, *next;
207         
208         for(link= ntree->links.first; link; link= next) {
209                 next= link->next;
210                 if(link->fromsock==sock || link->tosock==sock) {
211                         nodeRemLink(ntree, link);
212                 }
213         }
214         
215         /* this is fast, this way we don't need an in_out argument */
216         BLI_remlink(&node->inputs, sock);
217         BLI_remlink(&node->outputs, sock);
218         
219         if (sock->default_value)
220                 MEM_freeN(sock->default_value);
221         MEM_freeN(sock);
222         
223         node->update |= NODE_UPDATE;
224 }
225
226 void nodeRemoveAllSockets(bNodeTree *ntree, bNode *node)
227 {
228         bNodeSocket *sock;
229         bNodeLink *link, *next;
230         
231         for(link= ntree->links.first; link; link= next) {
232                 next= link->next;
233                 if(link->fromnode==node || link->tonode==node) {
234                         nodeRemLink(ntree, link);
235                 }
236         }
237         
238         for (sock=node->inputs.first; sock; sock=sock->next)
239                 if (sock->default_value)
240                         MEM_freeN(sock->default_value);
241         BLI_freelistN(&node->inputs);
242         for (sock=node->outputs.first; sock; sock=sock->next)
243                 if (sock->default_value)
244                         MEM_freeN(sock->default_value);
245         
246         BLI_freelistN(&node->outputs);
247         
248         node->update |= NODE_UPDATE;
249 }
250
251 /* finds a node based on its name */
252 bNode *nodeFindNodebyName(bNodeTree *ntree, const char *name)
253 {
254         return BLI_findstring(&ntree->nodes, name, offsetof(bNode, name));
255 }
256
257 /* finds a node based on given socket */
258 int nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **nodep, int *sockindex, int *in_out)
259 {
260         bNode *node;
261         bNodeSocket *tsock;
262         int index= 0;
263         
264         for(node= ntree->nodes.first; node; node= node->next) {
265                 for(index=0, tsock= node->inputs.first; tsock; tsock= tsock->next, index++) {
266                         if(tsock==sock) {
267                                 if (in_out) *in_out= SOCK_IN;
268                                 break;
269                         }
270                 }
271                 if(tsock)
272                         break;
273                 for(index=0, tsock= node->outputs.first; tsock; tsock= tsock->next, index++) {
274                         if(tsock==sock) {
275                                 if (in_out) *in_out= SOCK_OUT;
276                                 break;
277                         }
278                 }
279                 if(tsock)
280                         break;
281         }
282
283         if(node) {
284                 *nodep= node;
285                 if(sockindex) *sockindex= index;
286                 return 1;
287         }
288         
289         *nodep= NULL;
290         return 0;
291 }
292
293 /* ************** Add stuff ********** */
294 static void node_add_sockets_from_type(bNodeTree *ntree, bNode *node, bNodeType *ntype)
295 {
296         bNodeSocketTemplate *sockdef;
297         /* bNodeSocket *sock; */ /* UNUSED */
298
299         if(ntype->inputs) {
300                 sockdef= ntype->inputs;
301                 while(sockdef->type != -1) {
302                         /* sock = */ node_add_input_from_template(ntree, node, sockdef);
303                         
304                         sockdef++;
305                 }
306         }
307         if(ntype->outputs) {
308                 sockdef= ntype->outputs;
309                 while(sockdef->type != -1) {
310                         /* sock = */ node_add_output_from_template(ntree, node, sockdef);
311                         
312                         sockdef++;
313                 }
314         }
315 }
316
317 /* Find the first available, non-duplicate name for a given node */
318 void nodeUniqueName(bNodeTree *ntree, bNode *node)
319 {
320         BLI_uniquename(&ntree->nodes, node, "Node", '.', offsetof(bNode, name), sizeof(node->name));
321 }
322
323 bNode *nodeAddNode(bNodeTree *ntree, struct bNodeTemplate *ntemp)
324 {
325         bNode *node;
326         bNodeType *ntype;
327         
328         ntype= node_get_type(ntree, ntemp->type);
329         if(ntype == NULL) {
330                 printf("nodeAddNodeType() error: '%d' type invalid\n", ntemp->type);
331                 return NULL;
332         }
333         /* validity check */
334         if (!nodeValid(ntree, ntemp))
335                 return NULL;
336         
337         node= MEM_callocN(sizeof(bNode), "new node");
338         node->type= ntype->type;
339         node->typeinfo= ntype;
340         node->flag= NODE_SELECT|ntype->flag;
341         node->width= ntype->width;
342         node->miniwidth= 42.0f;
343         node->height= ntype->height;
344         
345         node_add_sockets_from_type(ntree, node, ntype);
346         
347         if(ntype->initfunc!=NULL)
348                 ntype->initfunc(ntree, node, ntemp);
349         
350         /* initialize the node name with the node label */
351         BLI_strncpy(node->name, nodeLabel(node), NODE_MAXSTR);
352         nodeUniqueName(ntree, node);
353         
354         BLI_addtail(&ntree->nodes, node);
355         
356         ntree->update |= NTREE_UPDATE_NODES;
357         
358         return node;
359 }
360
361 void nodeMakeDynamicType(bNode *node)
362 {
363         /* find SH_DYNAMIC_NODE ntype */
364         bNodeType *ntype= ntreeGetType(NTREE_SHADER)->node_types.first;
365         while(ntype) {
366                 if(ntype->type==NODE_DYNAMIC)
367                         break;
368                 ntype= ntype->next;
369         }
370
371         /* make own type struct to fill */
372         if(ntype) {
373                 /*node->typeinfo= MEM_dupallocN(ntype);*/
374                 bNodeType *newtype= MEM_callocN(sizeof(bNodeType), "dynamic bNodeType");
375                 *newtype= *ntype;
376                 BLI_strncpy(newtype->name, ntype->name, sizeof(newtype->name));
377                 node->typeinfo= newtype;
378         }
379 }
380
381 /* keep socket listorder identical, for copying links */
382 /* ntree is the target tree */
383 bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node)
384 {
385         bNode *nnode= MEM_callocN(sizeof(bNode), "dupli node");
386         bNodeSocket *sock, *oldsock;
387
388         *nnode= *node;
389         nodeUniqueName(ntree, nnode);
390         
391         BLI_addtail(&ntree->nodes, nnode);
392
393         BLI_duplicatelist(&nnode->inputs, &node->inputs);
394         oldsock= node->inputs.first;
395         for(sock= nnode->inputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
396                 oldsock->new_sock= sock;
397                 sock->stack_index= 0;
398                 
399                 sock->default_value = (oldsock->default_value ? MEM_dupallocN(oldsock->default_value) : NULL);
400                 
401                 /* XXX some compositor node (e.g. image, render layers) still store
402                  * some persistent buffer data here, need to clear this to avoid dangling pointers.
403                  */
404                 sock->cache = NULL;
405         }
406         
407         BLI_duplicatelist(&nnode->outputs, &node->outputs);
408         oldsock= node->outputs.first;
409         for(sock= nnode->outputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
410                 oldsock->new_sock= sock;
411                 sock->stack_index= 0;
412                 
413                 sock->default_value = (oldsock->default_value ? MEM_dupallocN(oldsock->default_value) : NULL);
414                 
415                 /* XXX some compositor node (e.g. image, render layers) still store
416                  * some persistent buffer data here, need to clear this to avoid dangling pointers.
417                  */
418                 sock->cache = NULL;
419         }
420         
421         /* don't increase node->id users, freenode doesn't decrement either */
422         
423         if(node->typeinfo->copystoragefunc)
424                 node->typeinfo->copystoragefunc(node, nnode);
425         
426         node->new_node= nnode;
427         nnode->new_node= NULL;
428         nnode->preview= NULL;
429         
430         ntree->update |= NTREE_UPDATE_NODES;
431         
432         return nnode;
433 }
434
435 /* also used via rna api, so we check for proper input output direction */
436 bNodeLink *nodeAddLink(bNodeTree *ntree, bNode *fromnode, bNodeSocket *fromsock, bNode *tonode, bNodeSocket *tosock)
437 {
438         bNodeSocket *sock;
439         bNodeLink *link= NULL; 
440         int from= 0, to= 0;
441         
442         if(fromnode) {
443                 /* test valid input */
444                 for(sock= fromnode->outputs.first; sock; sock= sock->next)
445                         if(sock==fromsock)
446                                 break;
447                 if(sock)
448                         from= 1; /* OK */
449                 else {
450                         for(sock= fromnode->inputs.first; sock; sock= sock->next)
451                                 if(sock==fromsock)
452                                         break;
453                         if(sock)
454                                 from= -1; /* OK but flip */
455                 }
456         }
457         else {
458                 /* check tree sockets */
459                 for(sock= ntree->inputs.first; sock; sock= sock->next)
460                         if(sock==fromsock)
461                                 break;
462                 if(sock)
463                         from= 1; /* OK */
464                 else {
465                         for(sock= ntree->outputs.first; sock; sock= sock->next)
466                                 if(sock==fromsock)
467                                         break;
468                         if(sock)
469                                 from= -1; /* OK but flip */
470                 }
471         }
472         if(tonode) {
473                 for(sock= tonode->inputs.first; sock; sock= sock->next)
474                         if(sock==tosock)
475                                 break;
476                 if(sock)
477                         to= 1; /* OK */
478                 else {
479                         for(sock= tonode->outputs.first; sock; sock= sock->next)
480                                 if(sock==tosock)
481                                         break;
482                         if(sock)
483                                 to= -1; /* OK but flip */
484                 }
485         }
486         else {
487                 /* check tree sockets */
488                 for(sock= ntree->outputs.first; sock; sock= sock->next)
489                         if(sock==tosock)
490                                 break;
491                 if(sock)
492                         to= 1; /* OK */
493                 else {
494                         for(sock= ntree->inputs.first; sock; sock= sock->next)
495                                 if(sock==tosock)
496                                         break;
497                         if(sock)
498                                 to= -1; /* OK but flip */
499                 }
500         }
501         
502         if(from >= 0 && to >= 0) {
503                 link= MEM_callocN(sizeof(bNodeLink), "link");
504                 BLI_addtail(&ntree->links, link);
505                 link->fromnode= fromnode;
506                 link->fromsock= fromsock;
507                 link->tonode= tonode;
508                 link->tosock= tosock;
509         }
510         else if(from <= 0 && to <= 0) {
511                 link= MEM_callocN(sizeof(bNodeLink), "link");
512                 BLI_addtail(&ntree->links, link);
513                 link->fromnode= tonode;
514                 link->fromsock= tosock;
515                 link->tonode= fromnode;
516                 link->tosock= fromsock;
517         }
518         
519         ntree->update |= NTREE_UPDATE_LINKS;
520         
521         return link;
522 }
523
524 void nodeRemLink(bNodeTree *ntree, bNodeLink *link)
525 {
526         BLI_remlink(&ntree->links, link);
527         if(link->tosock)
528                 link->tosock->link= NULL;
529         MEM_freeN(link);
530         
531         ntree->update |= NTREE_UPDATE_LINKS;
532 }
533
534 void nodeRemSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
535 {
536         bNodeLink *link, *next;
537         
538         for(link= ntree->links.first; link; link= next) {
539                 next= link->next;
540                 if(link->fromsock==sock || link->tosock==sock) {
541                         nodeRemLink(ntree, link);
542                 }
543         }
544         
545         ntree->update |= NTREE_UPDATE_LINKS;
546 }
547
548 /* transforms node location to area coords */
549 void nodeSpaceCoords(bNode *node, float *locx, float *locy)
550 {
551         if (node->parent) {
552                 nodeSpaceCoords(node->parent, locx, locy);
553                 *locx += node->locx;
554                 *locy += node->locy;
555         }
556         else {
557                 *locx = node->locx;
558                 *locy = node->locy;
559         }
560 }
561
562 void nodeAttachNode(bNode *node, bNode *parent)
563 {
564         float parentx, parenty;
565         
566         node->parent = parent;
567         /* transform to parent space */
568         nodeSpaceCoords(parent, &parentx, &parenty);
569         node->locx -= parentx;
570         node->locy -= parenty;
571 }
572
573 void nodeDetachNode(struct bNode *node)
574 {
575         float parentx, parenty;
576         
577         if (node->parent) {
578                 /* transform to "global" (area) space */
579                 nodeSpaceCoords(node->parent, &parentx, &parenty);
580                 node->locx += parentx;
581                 node->locy += parenty;
582                 node->parent = NULL;
583         }
584 }
585
586 bNodeTree *ntreeAddTree(const char *name, int type, int nodetype)
587 {
588         bNodeTree *ntree;
589         bNodeType *ntype;
590         
591         /* trees are created as local trees if they of compositor, material or texture type,
592          * node groups and other tree types are created as library data.
593          */
594         if (ELEM3(type, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE) && nodetype==0) {
595                 ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
596                 *( (short *)ntree->id.name )= ID_NT; /* not "type", as that is ntree->type */
597                 BLI_strncpy(ntree->id.name+2, name, sizeof(ntree->id.name));
598         }
599         else
600                 ntree= alloc_libblock(&G.main->nodetree, ID_NT, name);
601         
602         ntree->type= type;
603         ntree->nodetype = nodetype;
604         
605         ntreeInitTypes(ntree);
606         
607         ntype = node_get_type(ntree, ntree->nodetype);
608         if (ntype && ntype->inittreefunc)
609                 ntype->inittreefunc(ntree);
610         
611         return ntree;
612 }
613
614 /* Warning: this function gets called during some rather unexpected times
615  *      - this gets called when executing compositing updates (for threaded previews)
616  *      - when the nodetree datablock needs to be copied (i.e. when users get copied)
617  *      - for scene duplication use ntreeSwapID() after so we dont have stale pointers.
618  */
619 bNodeTree *ntreeCopyTree(bNodeTree *ntree)
620 {
621         bNodeTree *newtree;
622         bNode *node /*, *nnode */ /* UNUSED */, *last;
623         bNodeLink *link;
624         bNodeSocket *gsock, *oldgsock;
625         
626         if(ntree==NULL) return NULL;
627         
628         /* is ntree part of library? */
629         for(newtree=G.main->nodetree.first; newtree; newtree= newtree->id.next)
630                 if(newtree==ntree) break;
631         if(newtree) {
632                 newtree= copy_libblock(ntree);
633         } else {
634                 newtree= MEM_dupallocN(ntree);
635                 copy_libblock_data(&newtree->id, &ntree->id, TRUE); /* copy animdata and ID props */
636         }
637
638         id_us_plus((ID *)newtree->gpd);
639
640         /* in case a running nodetree is copied */
641         newtree->execdata= NULL;
642         
643         newtree->nodes.first= newtree->nodes.last= NULL;
644         newtree->links.first= newtree->links.last= NULL;
645         
646         last = ntree->nodes.last;
647         for(node= ntree->nodes.first; node; node= node->next) {
648                 node->new_node= NULL;
649                 /* nnode= */ nodeCopyNode(newtree, node);       /* sets node->new */
650                 
651                 /* make sure we don't copy new nodes again! */
652                 if (node==last)
653                         break;
654         }
655         
656         /* socket definition for group usage */
657         BLI_duplicatelist(&newtree->inputs, &ntree->inputs);
658         for(gsock= newtree->inputs.first, oldgsock= ntree->inputs.first; gsock; gsock=gsock->next, oldgsock=oldgsock->next) {
659                 oldgsock->new_sock= gsock;
660                 gsock->groupsock = (oldgsock->groupsock ? oldgsock->groupsock->new_sock : NULL);
661                 gsock->default_value = (oldgsock->default_value ? MEM_dupallocN(oldgsock->default_value) : NULL);
662         }
663         BLI_duplicatelist(&newtree->outputs, &ntree->outputs);
664         for(gsock= newtree->outputs.first, oldgsock= ntree->outputs.first; gsock; gsock=gsock->next, oldgsock=oldgsock->next) {
665                 oldgsock->new_sock= gsock;
666                 gsock->groupsock = (oldgsock->groupsock ? oldgsock->groupsock->new_sock : NULL);
667                 gsock->default_value = (oldgsock->default_value ? MEM_dupallocN(oldgsock->default_value) : NULL);
668         }
669         
670         /* copy links */
671         BLI_duplicatelist(&newtree->links, &ntree->links);
672         for(link= newtree->links.first; link; link= link->next) {
673                 link->fromnode = (link->fromnode ? link->fromnode->new_node : NULL);
674                 link->fromsock = (link->fromsock ? link->fromsock->new_sock : NULL);
675                 link->tonode = (link->tonode ? link->tonode->new_node : NULL);
676                 link->tosock = (link->tosock ? link->tosock->new_sock : NULL);
677                 /* update the link socket's pointer */
678                 if (link->tosock)
679                         link->tosock->link = link;
680         }
681         
682         /* update node->parent pointers */
683         for (node=newtree->nodes.first; node; node=node->next) {
684                 if (node->parent)
685                         node->parent = node->parent->new_node;
686         }
687         
688         return newtree;
689 }
690
691 /* use when duplicating scenes */
692 void ntreeSwitchID(bNodeTree *ntree, ID *id_from, ID *id_to)
693 {
694         bNode *node;
695         /* for scene duplication only */
696         for(node= ntree->nodes.first; node; node= node->next) {
697                 if(node->id==id_from) {
698                         node->id= id_to;
699                 }
700         }
701 }
702
703 /* *************** preview *********** */
704 /* if node->preview, then we assume the rect to exist */
705
706 void nodeFreePreview(bNode *node)
707 {
708         if(node->preview) {
709                 if(node->preview->rect)
710                         MEM_freeN(node->preview->rect);
711                 MEM_freeN(node->preview);
712                 node->preview= NULL;
713         }       
714 }
715
716 static void node_init_preview(bNode *node, int xsize, int ysize)
717 {
718         
719         if(node->preview==NULL) {
720                 node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
721                 //              printf("added preview %s\n", node->name);
722         }
723         
724         /* node previews can get added with variable size this way */
725         if(xsize==0 || ysize==0)
726                 return;
727         
728         /* sanity checks & initialize */
729         if(node->preview->rect) {
730                 if(node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
731                         MEM_freeN(node->preview->rect);
732                         node->preview->rect= NULL;
733                 }
734         }
735         
736         if(node->preview->rect==NULL) {
737                 node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(char)*4, "node preview rect");
738                 node->preview->xsize= xsize;
739                 node->preview->ysize= ysize;
740         }
741         /* no clear, makes nicer previews */
742 }
743
744 void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
745 {
746         bNode *node;
747         
748         if(ntree==NULL)
749                 return;
750         
751         for(node= ntree->nodes.first; node; node= node->next) {
752                 if(node->typeinfo->flag & NODE_PREVIEW) /* hrms, check for closed nodes? */
753                         node_init_preview(node, xsize, ysize);
754                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
755                         ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
756         }               
757 }
758
759 static void nodeClearPreview(bNode *node)
760 {
761         if(node->preview && node->preview->rect)
762                 memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
763 }
764
765 /* use it to enforce clear */
766 void ntreeClearPreview(bNodeTree *ntree)
767 {
768         bNode *node;
769         
770         if(ntree==NULL)
771                 return;
772         
773         for(node= ntree->nodes.first; node; node= node->next) {
774                 if(node->typeinfo->flag & NODE_PREVIEW)
775                         nodeClearPreview(node);
776                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
777                         ntreeClearPreview((bNodeTree *)node->id);
778         }               
779 }
780
781 /* hack warning! this function is only used for shader previews, and 
782 since it gets called multiple times per pixel for Ztransp we only
783 add the color once. Preview gets cleared before it starts render though */
784 void nodeAddToPreview(bNode *node, float *col, int x, int y, int do_manage)
785 {
786         bNodePreview *preview= node->preview;
787         if(preview) {
788                 if(x>=0 && y>=0) {
789                         if(x<preview->xsize && y<preview->ysize) {
790                                 unsigned char *tar= preview->rect+ 4*((preview->xsize*y) + x);
791                                 
792                                 if(do_manage) {
793                                         tar[0]= FTOCHAR(linearrgb_to_srgb(col[0]));
794                                         tar[1]= FTOCHAR(linearrgb_to_srgb(col[1]));
795                                         tar[2]= FTOCHAR(linearrgb_to_srgb(col[2]));
796                                 }
797                                 else {
798                                         tar[0]= FTOCHAR(col[0]);
799                                         tar[1]= FTOCHAR(col[1]);
800                                         tar[2]= FTOCHAR(col[2]);
801                                 }
802                                 tar[3]= FTOCHAR(col[3]);
803                         }
804                         //else printf("prv out bound x y %d %d\n", x, y);
805                 }
806                 //else printf("prv out bound x y %d %d\n", x, y);
807         }
808 }
809
810 /* ************** Free stuff ********** */
811
812 /* goes over entire tree */
813 void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
814 {
815         bNodeLink *link, *next;
816         bNodeSocket *sock;
817         ListBase *lb;
818         
819         for(link= ntree->links.first; link; link= next) {
820                 next= link->next;
821                 
822                 if(link->fromnode==node) {
823                         lb= &node->outputs;
824                         if (link->tonode)
825                                 link->tonode->update |= NODE_UPDATE;
826                 }
827                 else if(link->tonode==node)
828                         lb= &node->inputs;
829                 else
830                         lb= NULL;
831
832                 if(lb) {
833                         for(sock= lb->first; sock; sock= sock->next) {
834                                 if(link->fromsock==sock || link->tosock==sock)
835                                         break;
836                         }
837                         if(sock) {
838                                 nodeRemLink(ntree, link);
839                         }
840                 }
841         }
842 }
843
844 static void node_unlink_attached(bNodeTree *ntree, bNode *parent)
845 {
846         bNode *node;
847         for (node=ntree->nodes.first; node; node=node->next) {
848                 if (node->parent == parent)
849                         nodeDetachNode(node);
850         }
851 }
852
853 void nodeFreeNode(bNodeTree *ntree, bNode *node)
854 {
855         bNodeTreeType *treetype= ntreeGetType(ntree->type);
856         bNodeSocket *sock, *nextsock;
857         
858         /* remove all references to this node */
859         nodeUnlinkNode(ntree, node);
860         node_unlink_attached(ntree, node);
861         
862         BLI_remlink(&ntree->nodes, node);
863
864         /* since it is called while free database, node->id is undefined */
865         
866         if (treetype->free_node_cache)
867                 treetype->free_node_cache(ntree, node);
868         
869         for (sock=node->inputs.first; sock; sock = nextsock) {
870                 nextsock = sock->next;
871                 if (sock->default_value)
872                         MEM_freeN(sock->default_value);
873                 MEM_freeN(sock);
874         }
875         for (sock=node->outputs.first; sock; sock = nextsock) {
876                 nextsock = sock->next;
877                 if (sock->default_value)
878                         MEM_freeN(sock->default_value);
879                 MEM_freeN(sock);
880         }
881
882         nodeFreePreview(node);
883
884         if(node->typeinfo && node->typeinfo->freestoragefunc) {
885                 node->typeinfo->freestoragefunc(node);
886         }
887
888         MEM_freeN(node);
889         
890         ntree->update |= NTREE_UPDATE_NODES;
891 }
892
893 /* do not free ntree itself here, free_libblock calls this function too */
894 void ntreeFreeTree(bNodeTree *ntree)
895 {
896         bNode *node, *next;
897         bNodeSocket *sock;
898         
899         if(ntree==NULL) return;
900         
901         /* XXX hack! node trees should not store execution graphs at all.
902          * This should be removed when old tree types no longer require it.
903          * Currently the execution data for texture nodes remains in the tree
904          * after execution, until the node tree is updated or freed.
905          */
906         if (ntree->execdata) {
907                 switch (ntree->type) {
908                 case NTREE_COMPOSIT:
909                         ntreeCompositEndExecTree(ntree->execdata, 1);
910                         break;
911                 case NTREE_SHADER:
912                         ntreeShaderEndExecTree(ntree->execdata, 1);
913                         break;
914                 case NTREE_TEXTURE:
915                         ntreeTexEndExecTree(ntree->execdata, 1);
916                         break;
917                 }
918         }
919         
920         BKE_free_animdata((ID *)ntree);
921         
922         id_us_min((ID *)ntree->gpd);
923
924         BLI_freelistN(&ntree->links);   /* do first, then unlink_node goes fast */
925         
926         for(node= ntree->nodes.first; node; node= next) {
927                 next= node->next;
928                 nodeFreeNode(ntree, node);
929         }
930         
931         for (sock=ntree->inputs.first; sock; sock=sock->next)
932                 if (sock->default_value)
933                         MEM_freeN(sock->default_value);
934         BLI_freelistN(&ntree->inputs);
935         for (sock=ntree->outputs.first; sock; sock=sock->next)
936                 if (sock->default_value)
937                         MEM_freeN(sock->default_value);
938         BLI_freelistN(&ntree->outputs);
939 }
940
941 void ntreeFreeCache(bNodeTree *ntree)
942 {
943         bNodeTreeType *treetype;
944         
945         if(ntree==NULL) return;
946         
947         treetype= ntreeGetType(ntree->type);
948         if (treetype->free_cache)
949                 treetype->free_cache(ntree);
950 }
951
952 void ntreeSetOutput(bNodeTree *ntree)
953 {
954         bNode *node;
955
956         /* find the active outputs, might become tree type dependant handler */
957         for(node= ntree->nodes.first; node; node= node->next) {
958                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
959                         bNode *tnode;
960                         int output= 0;
961                         
962                         /* we need a check for which output node should be tagged like this, below an exception */
963                         if(node->type==CMP_NODE_OUTPUT_FILE)
964                            continue;
965                            
966                         /* there is more types having output class, each one is checked */
967                         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
968                                 if(tnode->typeinfo->nclass==NODE_CLASS_OUTPUT) {
969                                         
970                                         if(ntree->type==NTREE_COMPOSIT) {
971                                                         
972                                                 /* same type, exception for viewer */
973                                                 if(tnode->type==node->type ||
974                                                    (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) &&
975                                                         ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))) {
976                                                         if(tnode->flag & NODE_DO_OUTPUT) {
977                                                                 output++;
978                                                                 if(output>1)
979                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
980                                                         }
981                                                 }
982                                         }
983                                         else {
984                                                 /* same type */
985                                                 if(tnode->type==node->type) {
986                                                         if(tnode->flag & NODE_DO_OUTPUT) {
987                                                                 output++;
988                                                                 if(output>1)
989                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
990                                                         }
991                                                 }
992                                         }
993                                 }
994                         }
995                         if(output==0)
996                                 node->flag |= NODE_DO_OUTPUT;
997                 }
998         }
999         
1000         /* here we could recursively set which nodes have to be done,
1001                 might be different for editor or for "real" use... */
1002 }
1003
1004 typedef struct MakeLocalCallData {
1005         ID *group_id;
1006         ID *new_id;
1007         int lib, local;
1008 } MakeLocalCallData;
1009
1010 static void ntreeMakeLocal_CheckLocal(void *calldata, ID *owner_id, bNodeTree *ntree)
1011 {
1012         MakeLocalCallData *cd= (MakeLocalCallData*)calldata;
1013         bNode *node;
1014         
1015         /* find if group is in tree */
1016         for(node= ntree->nodes.first; node; node= node->next) {
1017                 if(node->id == cd->group_id) {
1018                         if(owner_id->lib) cd->lib= 1;
1019                         else cd->local= 1;
1020                 }
1021         }
1022 }
1023
1024 static void ntreeMakeLocal_LinkNew(void *calldata, ID *owner_id, bNodeTree *ntree)
1025 {
1026         MakeLocalCallData *cd= (MakeLocalCallData*)calldata;
1027         bNode *node;
1028         
1029         /* find if group is in tree */
1030         for(node= ntree->nodes.first; node; node= node->next) {
1031                 if(node->id == cd->group_id) {
1032                         if(owner_id->lib==NULL) {
1033                                 node->id= cd->new_id;
1034                                 cd->new_id->us++;
1035                                 cd->group_id->us--;
1036                         }
1037                 }
1038         }
1039 }
1040
1041 void ntreeMakeLocal(bNodeTree *ntree)
1042 {
1043         bNodeTreeType *treetype= ntreeGetType(ntree->type);
1044         MakeLocalCallData cd;
1045         
1046         /* - only lib users: do nothing
1047                 * - only local users: set flag
1048                 * - mixed: make copy
1049                 */
1050         
1051         if(ntree->id.lib==NULL) return;
1052         if(ntree->id.us==1) {
1053                 ntree->id.lib= NULL;
1054                 ntree->id.flag= LIB_LOCAL;
1055                 new_id(NULL, (ID *)ntree, NULL);
1056                 return;
1057         }
1058         
1059         /* now check users of groups... again typedepending, callback... */
1060         cd.group_id = &ntree->id;
1061         cd.new_id = NULL;
1062         cd.local = 0;
1063         cd.lib = 0;
1064         
1065         treetype->foreach_nodetree(G.main, &cd, &ntreeMakeLocal_CheckLocal);
1066         
1067         /* if all users are local, we simply make tree local */
1068         if(cd.local && cd.lib==0) {
1069                 ntree->id.lib= NULL;
1070                 ntree->id.flag= LIB_LOCAL;
1071                 new_id(NULL, (ID *)ntree, NULL);
1072         }
1073         else if(cd.local && cd.lib) {
1074                 /* this is the mixed case, we copy the tree and assign it to local users */
1075                 bNodeTree *newtree= ntreeCopyTree(ntree);
1076                 
1077                 newtree->id.us= 0;
1078                 
1079                 cd.new_id = &newtree->id;
1080                 treetype->foreach_nodetree(G.main, &cd, &ntreeMakeLocal_LinkNew);
1081         }
1082 }
1083
1084 int ntreeNodeExists(bNodeTree *ntree, bNode *testnode)
1085 {
1086         bNode *node= ntree->nodes.first;
1087         for(; node; node= node->next)
1088                 if(node==testnode)
1089                         return 1;
1090         return 0;
1091 }
1092
1093 int ntreeOutputExists(bNode *node, bNodeSocket *testsock)
1094 {
1095         bNodeSocket *sock= node->outputs.first;
1096         for(; sock; sock= sock->next)
1097                 if(sock==testsock)
1098                         return 1;
1099         return 0;
1100 }
1101
1102 /* returns localized tree for execution in threads */
1103 bNodeTree *ntreeLocalize(bNodeTree *ntree)
1104 {
1105         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1106
1107         bNodeTree *ltree;
1108         bNode *node;
1109         
1110         bAction *action_backup= NULL, *tmpact_backup= NULL;
1111         
1112         /* Workaround for copying an action on each render!
1113          * set action to NULL so animdata actions dont get copied */
1114         AnimData *adt= BKE_animdata_from_id(&ntree->id);
1115
1116         if(adt) {
1117                 action_backup= adt->action;
1118                 tmpact_backup= adt->tmpact;
1119
1120                 adt->action= NULL;
1121                 adt->tmpact= NULL;
1122         }
1123
1124         /* node copy func */
1125         ltree= ntreeCopyTree(ntree);
1126
1127         if(adt) {
1128                 AnimData *ladt= BKE_animdata_from_id(&ltree->id);
1129
1130                 adt->action= ladt->action= action_backup;
1131                 adt->tmpact= ladt->tmpact= tmpact_backup;
1132
1133                 if(action_backup) action_backup->id.us++;
1134                 if(tmpact_backup) tmpact_backup->id.us++;
1135                 
1136         }
1137         /* end animdata uglyness */
1138
1139         /* ensures only a single output node is enabled */
1140         ntreeSetOutput(ntree);
1141
1142         for(node= ntree->nodes.first; node; node= node->next) {
1143                 /* store new_node pointer to original */
1144                 node->new_node->new_node= node;
1145         }
1146
1147         if (ntreetype->localize)
1148                 ntreetype->localize(ltree, ntree);
1149
1150         return ltree;
1151 }
1152
1153 /* sync local composite with real tree */
1154 /* local tree is supposed to be running, be careful moving previews! */
1155 /* is called by jobs manager, outside threads, so it doesnt happen during draw */
1156 void ntreeLocalSync(bNodeTree *localtree, bNodeTree *ntree)
1157 {
1158         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1159
1160         if (ntreetype->local_sync)
1161                 ntreetype->local_sync(localtree, ntree);
1162 }
1163
1164 /* merge local tree results back, and free local tree */
1165 /* we have to assume the editor already changed completely */
1166 void ntreeLocalMerge(bNodeTree *localtree, bNodeTree *ntree)
1167 {
1168         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1169         bNode *lnode;
1170         
1171         /* move over the compbufs and previews */
1172         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
1173                 if(ntreeNodeExists(ntree, lnode->new_node)) {
1174                         if(lnode->preview && lnode->preview->rect) {
1175                                 nodeFreePreview(lnode->new_node);
1176                                 lnode->new_node->preview= lnode->preview;
1177                                 lnode->preview= NULL;
1178                         }
1179                 }
1180         }
1181
1182         if (ntreetype->local_merge)
1183                 ntreetype->local_merge(localtree, ntree);
1184
1185         ntreeFreeTree(localtree);
1186         MEM_freeN(localtree);
1187 }
1188
1189 /* ************ find stuff *************** */
1190
1191 int ntreeHasType(bNodeTree *ntree, int type)
1192 {
1193         bNode *node;
1194         
1195         if(ntree)
1196                 for(node= ntree->nodes.first; node; node= node->next)
1197                         if(node->type == type)
1198                                 return 1;
1199         return 0;
1200 }
1201
1202 bNodeLink *nodeFindLink(bNodeTree *ntree, bNodeSocket *from, bNodeSocket *to)
1203 {
1204         bNodeLink *link;
1205         
1206         for(link= ntree->links.first; link; link= link->next) {
1207                 if(link->fromsock==from && link->tosock==to)
1208                         return link;
1209                 if(link->fromsock==to && link->tosock==from)    /* hrms? */
1210                         return link;
1211         }
1212         return NULL;
1213 }
1214
1215 int nodeCountSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1216 {
1217         bNodeLink *link;
1218         int tot= 0;
1219         
1220         for(link= ntree->links.first; link; link= link->next) {
1221                 if(link->fromsock==sock || link->tosock==sock)
1222                         tot++;
1223         }
1224         return tot;
1225 }
1226
1227 bNode *nodeGetActive(bNodeTree *ntree)
1228 {
1229         bNode *node;
1230         
1231         if(ntree==NULL) return NULL;
1232         
1233         for(node= ntree->nodes.first; node; node= node->next)
1234                 if(node->flag & NODE_ACTIVE)
1235                         break;
1236         return node;
1237 }
1238
1239 /* two active flags, ID nodes have special flag for buttons display */
1240 bNode *nodeGetActiveID(bNodeTree *ntree, short idtype)
1241 {
1242         bNode *node;
1243         
1244         if(ntree==NULL) return NULL;
1245
1246         /* check for group edit */
1247         for(node= ntree->nodes.first; node; node= node->next)
1248                 if(node->flag & NODE_GROUP_EDIT)
1249                         break;
1250
1251         if(node)
1252                 ntree= (bNodeTree*)node->id;
1253         
1254         /* now find active node with this id */
1255         for(node= ntree->nodes.first; node; node= node->next)
1256                 if(node->id && GS(node->id->name)==idtype)
1257                         if(node->flag & NODE_ACTIVE_ID)
1258                                 break;
1259
1260         return node;
1261 }
1262
1263 int nodeSetActiveID(bNodeTree *ntree, short idtype, ID *id)
1264 {
1265         bNode *node;
1266         int ok= FALSE;
1267
1268         if(ntree==NULL) return ok;
1269
1270         /* check for group edit */
1271         for(node= ntree->nodes.first; node; node= node->next)
1272                 if(node->flag & NODE_GROUP_EDIT)
1273                         break;
1274
1275         if(node)
1276                 ntree= (bNodeTree*)node->id;
1277
1278         /* now find active node with this id */
1279         for(node= ntree->nodes.first; node; node= node->next) {
1280                 if(node->id && GS(node->id->name)==idtype) {
1281                         if(id && ok==FALSE && node->id==id) {
1282                                 node->flag |= NODE_ACTIVE_ID;
1283                                 ok= TRUE;
1284                         } else {
1285                                 node->flag &= ~NODE_ACTIVE_ID;
1286                         }
1287                 }
1288         }
1289
1290         return ok;
1291 }
1292
1293
1294 /* two active flags, ID nodes have special flag for buttons display */
1295 void nodeClearActiveID(bNodeTree *ntree, short idtype)
1296 {
1297         bNode *node;
1298         
1299         if(ntree==NULL) return;
1300         
1301         for(node= ntree->nodes.first; node; node= node->next)
1302                 if(node->id && GS(node->id->name)==idtype)
1303                         node->flag &= ~NODE_ACTIVE_ID;
1304 }
1305
1306 /* two active flags, ID nodes have special flag for buttons display */
1307 void nodeSetActive(bNodeTree *ntree, bNode *node)
1308 {
1309         bNode *tnode;
1310         
1311         /* make sure only one node is active, and only one per ID type */
1312         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1313                 tnode->flag &= ~NODE_ACTIVE;
1314                 
1315                 if(node->id && tnode->id) {
1316                         if(GS(node->id->name) == GS(tnode->id->name))
1317                                 tnode->flag &= ~NODE_ACTIVE_ID;
1318                 }
1319                 if(node->typeinfo->nclass == NODE_CLASS_TEXTURE)
1320                         tnode->flag &= ~NODE_ACTIVE_TEXTURE;
1321         }
1322         
1323         node->flag |= NODE_ACTIVE;
1324         if(node->id)
1325                 node->flag |= NODE_ACTIVE_ID;
1326         if(node->typeinfo->nclass == NODE_CLASS_TEXTURE)
1327                 node->flag |= NODE_ACTIVE_TEXTURE;
1328 }
1329
1330 /* use flags are not persistant yet, groups might need different tagging, so we do it each time
1331    when we need to get this info */
1332 void ntreeSocketUseFlags(bNodeTree *ntree)
1333 {
1334         bNode *node;
1335         bNodeSocket *sock;
1336         bNodeLink *link;
1337         
1338         /* clear flags */
1339         for(node= ntree->nodes.first; node; node= node->next) {
1340                 for(sock= node->inputs.first; sock; sock= sock->next)
1341                         sock->flag &= ~SOCK_IN_USE;
1342                 for(sock= node->outputs.first; sock; sock= sock->next)
1343                         sock->flag &= ~SOCK_IN_USE;
1344         }
1345         
1346         /* tag all thats in use */
1347         for(link= ntree->links.first; link; link= link->next) {
1348         
1349                 if(link->fromsock) // FIXME, see below
1350                         link->fromsock->flag |= SOCK_IN_USE;
1351                 if(link->tosock) // FIXME This can be NULL, when dragging a new link in the UI, should probably copy the node tree for preview render - campbell
1352                         link->tosock->flag |= SOCK_IN_USE;
1353         }
1354 }
1355
1356 /* ************** dependency stuff *********** */
1357
1358 /* node is guaranteed to be not checked before */
1359 static int node_get_deplist_recurs(bNode *node, bNode ***nsort)
1360 {
1361         bNode *fromnode;
1362         bNodeSocket *sock;
1363         int level = 0xFFF;
1364         
1365         node->done= 1;
1366         
1367         /* check linked nodes */
1368         for(sock= node->inputs.first; sock; sock= sock->next) {
1369                 if(sock->link) {
1370                         fromnode= sock->link->fromnode;
1371                         if(fromnode) {
1372                                 if (fromnode->done==0)
1373                                         fromnode->level= node_get_deplist_recurs(fromnode, nsort);
1374                                 if (fromnode->level <= level)
1375                                         level = fromnode->level - 1;
1376                         }
1377                 }
1378         }
1379         
1380         /* check parent node */
1381         if (node->parent) {
1382                 if (node->parent->done==0)
1383                         node->parent->level= node_get_deplist_recurs(node->parent, nsort);
1384                 if (node->parent->level <= level)
1385                         level = node->parent->level - 1;
1386         }
1387         
1388         if (nsort) {
1389                 **nsort= node;
1390                 (*nsort)++;
1391         }
1392         
1393         return level;
1394 }
1395
1396 void ntreeGetDependencyList(struct bNodeTree *ntree, struct bNode ***deplist, int *totnodes)
1397 {
1398         bNode *node, **nsort;
1399         
1400         *totnodes=0;
1401         
1402         /* first clear data */
1403         for(node= ntree->nodes.first; node; node= node->next) {
1404                 node->done= 0;
1405                 (*totnodes)++;
1406         }
1407         if(*totnodes==0) {
1408                 *deplist = NULL;
1409                 return;
1410         }
1411         
1412         nsort= *deplist= MEM_callocN((*totnodes)*sizeof(bNode*), "sorted node array");
1413         
1414         /* recursive check */
1415         for(node= ntree->nodes.first; node; node= node->next) {
1416                 if(node->done==0) {
1417                         node->level= node_get_deplist_recurs(node, &nsort);
1418                 }
1419         }
1420 }
1421
1422 static void ntree_update_link_pointers(bNodeTree *ntree)
1423 {
1424         bNode *node;
1425         bNodeSocket *sock;
1426         bNodeLink *link;
1427         
1428         /* first clear data */
1429         for(node= ntree->nodes.first; node; node= node->next) {
1430                 for(sock= node->inputs.first; sock; sock= sock->next)
1431                         sock->link= NULL;
1432         }
1433         /* clear socket links */
1434         for(sock= ntree->outputs.first; sock; sock= sock->next)
1435                 sock->link= NULL;
1436
1437         for(link= ntree->links.first; link; link= link->next) {
1438                 if (link->tosock)
1439                         link->tosock->link= link;
1440         }
1441 }
1442
1443 static void ntree_validate_links(bNodeTree *ntree)
1444 {
1445         bNodeTreeType *ntreetype = ntreeGetType(ntree->type);
1446         bNodeLink *link;
1447         
1448         for (link = ntree->links.first; link; link = link->next) {
1449                 link->flag |= NODE_LINK_VALID;
1450                 if (link->fromnode && link->tonode && link->fromnode->level <= link->tonode->level)
1451                         link->flag &= ~NODE_LINK_VALID;
1452                 else if (ntreetype->validate_link) {
1453                         if (!ntreetype->validate_link(ntree, link))
1454                                 link->flag &= ~NODE_LINK_VALID;
1455                 }
1456         }
1457 }
1458
1459 static void ntree_verify_nodes_cb(void *calldata, struct ID *UNUSED(owner_id), struct bNodeTree *ntree)
1460 {
1461         ID *id= (ID*)calldata;
1462         bNode *node;
1463         
1464         for (node=ntree->nodes.first; node; node=node->next)
1465                 if (node->typeinfo->verifyfunc)
1466                         node->typeinfo->verifyfunc(ntree, node, id);
1467 }
1468
1469 void ntreeVerifyNodes(struct Main *main, struct ID *id)
1470 {
1471         bNodeTreeType *ntreetype;
1472         bNodeTree *ntree;
1473         int n;
1474         
1475         for (n=0; n < NUM_NTREE_TYPES; ++n) {
1476                 ntreetype= ntreeGetType(n);
1477                 if (ntreetype && ntreetype->foreach_nodetree)
1478                         ntreetype->foreach_nodetree(main, id, ntree_verify_nodes_cb);
1479         }
1480         for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
1481                 ntree_verify_nodes_cb(id, NULL, ntree);
1482 }
1483
1484 void ntreeUpdateTree(bNodeTree *ntree)
1485 {
1486         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1487         bNode *node;
1488         bNode **deplist;
1489         int totnodes, n;
1490         
1491         ntree_update_link_pointers(ntree);
1492         
1493         /* also updates the node level! */
1494         ntreeGetDependencyList(ntree, &deplist, &totnodes);
1495         
1496         if (deplist) {
1497                 /* update individual nodes */
1498                 for (n=0; n < totnodes; ++n) {
1499                         node = deplist[n];
1500                         
1501                         /* node tree update tags override individual node update flags */
1502                         if ((node->update & NODE_UPDATE) || (ntree->update & NTREE_UPDATE)) {
1503                                 if (ntreetype->update_node)
1504                                         ntreetype->update_node(ntree, node);
1505                                 else if (node->typeinfo->updatefunc)
1506                                         node->typeinfo->updatefunc(ntree, node);
1507                         }
1508                         /* clear update flag */
1509                         node->update = 0;
1510                 }
1511                 
1512                 MEM_freeN(deplist);
1513         }
1514         
1515         /* general tree updates */
1516         if (ntree->update & (NTREE_UPDATE_LINKS|NTREE_UPDATE_NODES)) {
1517                 ntree_validate_links(ntree);
1518         }
1519         
1520         /* update tree */
1521         if (ntreetype->update)
1522                 ntreetype->update(ntree);
1523         else {
1524                 /* Trees can be associated with a specific node type (i.e. group nodes),
1525                  * in that case a tree update function may be defined by that node type.
1526                  */
1527                 bNodeType *ntype= node_get_type(ntree, ntree->nodetype);
1528                 if (ntype && ntype->updatetreefunc)
1529                         ntype->updatetreefunc(ntree);
1530         }
1531         
1532         /* XXX hack, should be done by depsgraph!! */
1533         ntreeVerifyNodes(G.main, &ntree->id);
1534         
1535         /* clear the update flag */
1536         ntree->update = 0;
1537 }
1538
1539 void nodeUpdate(bNodeTree *ntree, bNode *node)
1540 {
1541         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1542         
1543         if (ntreetype->update_node)
1544                 ntreetype->update_node(ntree, node);
1545         else if (node->typeinfo->updatefunc)
1546                 node->typeinfo->updatefunc(ntree, node);
1547         /* clear update flag */
1548         node->update = 0;
1549 }
1550
1551 int nodeUpdateID(bNodeTree *ntree, ID *id)
1552 {
1553         bNodeTreeType *ntreetype;
1554         bNode *node;
1555         int change = FALSE;
1556         
1557         if(ELEM(NULL, id, ntree))
1558                 return change;
1559         
1560         ntreetype = ntreeGetType(ntree->type);
1561         
1562         if (ntreetype->update_node) {
1563                 for(node= ntree->nodes.first; node; node= node->next) {
1564                         if(node->id==id) {
1565                                 change = TRUE;
1566                                 ntreetype->update_node(ntree, node);
1567                                 /* clear update flag */
1568                                 node->update = 0;
1569                         }
1570                 }
1571         }
1572         else {
1573                 for(node= ntree->nodes.first; node; node= node->next) {
1574                         if(node->id==id) {
1575                                 change = TRUE;
1576                                 if (node->typeinfo->updatefunc)
1577                                         node->typeinfo->updatefunc(ntree, node);
1578                                 /* clear update flag */
1579                                 node->update = 0;
1580                         }
1581                 }
1582         }
1583         
1584         return change;
1585 }
1586
1587
1588 /* ************* node type access ********** */
1589
1590 int nodeValid(bNodeTree *ntree, bNodeTemplate *ntemp)
1591 {
1592         bNodeType *ntype= node_get_type(ntree, ntemp->type);
1593         if (ntype) {
1594                 if (ntype->validfunc)
1595                         return ntype->validfunc(ntree, ntemp);
1596                 else
1597                         return 1;
1598         }
1599         else
1600                 return 0;
1601 }
1602
1603 const char* nodeLabel(bNode *node)
1604 {
1605         if (node->label[0]!='\0')
1606                 return node->label;
1607         else if (node->typeinfo->labelfunc)
1608                 return node->typeinfo->labelfunc(node);
1609         else
1610                 return node->typeinfo->name;
1611 }
1612
1613 struct bNodeTree *nodeGroupEditGet(struct bNode *node)
1614 {
1615         if (node->typeinfo->group_edit_get)
1616                 return node->typeinfo->group_edit_get(node);
1617         else
1618                 return NULL;
1619 }
1620
1621 struct bNodeTree *nodeGroupEditSet(struct bNode *node, int edit)
1622 {
1623         if (node->typeinfo->group_edit_set)
1624                 return node->typeinfo->group_edit_set(node, edit);
1625         else if (node->typeinfo->group_edit_get)
1626                 return node->typeinfo->group_edit_get(node);
1627         else
1628                 return NULL;
1629 }
1630
1631 void nodeGroupEditClear(struct bNode *node)
1632 {
1633         if (node->typeinfo->group_edit_clear)
1634                 node->typeinfo->group_edit_clear(node);
1635 }
1636
1637 struct bNodeTemplate nodeMakeTemplate(struct bNode *node)
1638 {
1639         bNodeTemplate ntemp;
1640         if (node->typeinfo->templatefunc)
1641                 return node->typeinfo->templatefunc(node);
1642         else {
1643                 ntemp.type = node->type;
1644                 return ntemp;
1645         }
1646 }
1647
1648 void node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
1649 {
1650         memset(ntype, 0, sizeof(bNodeType));
1651         
1652         ntype->type = type;
1653         BLI_strncpy(ntype->name, name, sizeof(ntype->name));
1654         ntype->nclass = nclass;
1655         ntype->flag = flag;
1656         
1657         /* default size values */
1658         ntype->width = 140;
1659         ntype->minwidth = 100;
1660         ntype->maxwidth = 320;
1661         ntype->height = 100;
1662         ntype->minheight = 30;
1663         ntype->maxheight = FLT_MAX;
1664 }
1665
1666 void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
1667 {
1668         ntype->inputs = inputs;
1669         ntype->outputs = outputs;
1670 }
1671
1672 void node_type_init(struct bNodeType *ntype, void (*initfunc)(struct bNodeTree *ntree, struct bNode *node, struct bNodeTemplate *ntemp))
1673 {
1674         ntype->initfunc = initfunc;
1675 }
1676
1677 void node_type_valid(struct bNodeType *ntype, int (*validfunc)(struct bNodeTree *ntree, struct bNodeTemplate *ntemp))
1678 {
1679         ntype->validfunc = validfunc;
1680 }
1681
1682 void node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth)
1683 {
1684         ntype->width = width;
1685         ntype->minwidth = minwidth;
1686         if (maxwidth <= minwidth)
1687                 ntype->maxwidth = FLT_MAX;
1688         else
1689                 ntype->maxwidth = maxwidth;
1690 }
1691
1692 void node_type_storage(bNodeType *ntype, const char *storagename, void (*freestoragefunc)(struct bNode *), void (*copystoragefunc)(struct bNode *, struct bNode *))
1693 {
1694         if (storagename)
1695                 BLI_strncpy(ntype->storagename, storagename, sizeof(ntype->storagename));
1696         else
1697                 ntype->storagename[0] = '\0';
1698         ntype->copystoragefunc = copystoragefunc;
1699         ntype->freestoragefunc = freestoragefunc;
1700 }
1701
1702 void node_type_label(struct bNodeType *ntype, const char *(*labelfunc)(struct bNode *))
1703 {
1704         ntype->labelfunc = labelfunc;
1705 }
1706
1707 void node_type_template(struct bNodeType *ntype, struct bNodeTemplate (*templatefunc)(struct bNode *))
1708 {
1709         ntype->templatefunc = templatefunc;
1710 }
1711
1712 void node_type_update(struct bNodeType *ntype,
1713                                           void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node),
1714                                           void (*verifyfunc)(struct bNodeTree *ntree, struct bNode *node, struct ID *id))
1715 {
1716         ntype->updatefunc = updatefunc;
1717         ntype->verifyfunc = verifyfunc;
1718 }
1719
1720 void node_type_tree(struct bNodeType *ntype, void (*inittreefunc)(struct bNodeTree *), void (*updatetreefunc)(struct bNodeTree *))
1721 {
1722         ntype->inittreefunc = inittreefunc;
1723         ntype->updatetreefunc = updatetreefunc;
1724 }
1725
1726 void node_type_group_edit(struct bNodeType *ntype,
1727                                                   struct bNodeTree *(*group_edit_get)(struct bNode *node),
1728                                                   struct bNodeTree *(*group_edit_set)(struct bNode *node, int edit),
1729                                                   void (*group_edit_clear)(struct bNode *node))
1730 {
1731         ntype->group_edit_get = group_edit_get;
1732         ntype->group_edit_set = group_edit_set;
1733         ntype->group_edit_clear = group_edit_clear;
1734 }
1735
1736 void node_type_exec(struct bNodeType *ntype, void (*execfunc)(void *data, struct bNode *, struct bNodeStack **, struct bNodeStack **))
1737 {
1738         ntype->execfunc = execfunc;
1739 }
1740
1741 void node_type_exec_new(struct bNodeType *ntype,
1742                                                 void *(*initexecfunc)(struct bNode *node),
1743                                                 void (*freeexecfunc)(struct bNode *node, void *nodedata),
1744                                                 void (*newexecfunc)(void *data, int thread, struct bNode *, void *nodedata, struct bNodeStack **, struct bNodeStack **))
1745 {
1746         ntype->initexecfunc = initexecfunc;
1747         ntype->freeexecfunc = freeexecfunc;
1748         ntype->newexecfunc = newexecfunc;
1749 }
1750
1751 void node_type_gpu(struct bNodeType *ntype, int (*gpufunc)(struct GPUMaterial *mat, struct bNode *node, struct GPUNodeStack *in, struct GPUNodeStack *out))
1752 {
1753         ntype->gpufunc = gpufunc;
1754 }
1755
1756 void node_type_gpu_ext(struct bNodeType *ntype, int (*gpuextfunc)(struct GPUMaterial *mat, struct bNode *node, void *nodedata, struct GPUNodeStack *in, struct GPUNodeStack *out))
1757 {
1758         ntype->gpuextfunc = gpuextfunc;
1759 }
1760
1761 void node_type_compatibility(struct bNodeType *ntype, short compatibility)
1762 {
1763         ntype->compatibility = compatibility;
1764 }
1765
1766 static bNodeType *is_nodetype_registered(ListBase *typelist, int type) 
1767 {
1768         bNodeType *ntype= typelist->first;
1769         
1770         for(;ntype; ntype= ntype->next )
1771                 if(ntype->type==type)
1772                         return ntype;
1773         
1774         return NULL;
1775 }
1776
1777 void nodeRegisterType(ListBase *typelist, bNodeType *ntype) 
1778 {
1779         bNodeType *found= is_nodetype_registered(typelist, ntype->type);
1780         
1781         if(found==NULL)
1782                 BLI_addtail(typelist, ntype);
1783 }
1784
1785 static void registerCompositNodes(ListBase *ntypelist)
1786 {
1787         register_node_type_frame(ntypelist);
1788         
1789         register_node_type_cmp_group(ntypelist);
1790 //      register_node_type_cmp_forloop(ntypelist);
1791 //      register_node_type_cmp_whileloop(ntypelist);
1792         
1793         register_node_type_cmp_rlayers(ntypelist);
1794         register_node_type_cmp_image(ntypelist);
1795         register_node_type_cmp_texture(ntypelist);
1796         register_node_type_cmp_value(ntypelist);
1797         register_node_type_cmp_rgb(ntypelist);
1798         register_node_type_cmp_curve_time(ntypelist);
1799         
1800         register_node_type_cmp_composite(ntypelist);
1801         register_node_type_cmp_viewer(ntypelist);
1802         register_node_type_cmp_splitviewer(ntypelist);
1803         register_node_type_cmp_output_file(ntypelist);
1804         register_node_type_cmp_view_levels(ntypelist);
1805         
1806         register_node_type_cmp_curve_rgb(ntypelist);
1807         register_node_type_cmp_mix_rgb(ntypelist);
1808         register_node_type_cmp_hue_sat(ntypelist);
1809         register_node_type_cmp_brightcontrast(ntypelist);
1810         register_node_type_cmp_gamma(ntypelist);
1811         register_node_type_cmp_invert(ntypelist);
1812         register_node_type_cmp_alphaover(ntypelist);
1813         register_node_type_cmp_zcombine(ntypelist);
1814         register_node_type_cmp_colorbalance(ntypelist);
1815         register_node_type_cmp_huecorrect(ntypelist);
1816         
1817         register_node_type_cmp_normal(ntypelist);
1818         register_node_type_cmp_curve_vec(ntypelist);
1819         register_node_type_cmp_map_value(ntypelist);
1820         register_node_type_cmp_normalize(ntypelist);
1821         
1822         register_node_type_cmp_filter(ntypelist);
1823         register_node_type_cmp_blur(ntypelist);
1824         register_node_type_cmp_dblur(ntypelist);
1825         register_node_type_cmp_bilateralblur(ntypelist);
1826         register_node_type_cmp_vecblur(ntypelist);
1827         register_node_type_cmp_dilateerode(ntypelist);
1828         register_node_type_cmp_defocus(ntypelist);
1829         
1830         register_node_type_cmp_valtorgb(ntypelist);
1831         register_node_type_cmp_rgbtobw(ntypelist);
1832         register_node_type_cmp_setalpha(ntypelist);
1833         register_node_type_cmp_idmask(ntypelist);
1834         register_node_type_cmp_math(ntypelist);
1835         register_node_type_cmp_seprgba(ntypelist);
1836         register_node_type_cmp_combrgba(ntypelist);
1837         register_node_type_cmp_sephsva(ntypelist);
1838         register_node_type_cmp_combhsva(ntypelist);
1839         register_node_type_cmp_sepyuva(ntypelist);
1840         register_node_type_cmp_combyuva(ntypelist);
1841         register_node_type_cmp_sepycca(ntypelist);
1842         register_node_type_cmp_combycca(ntypelist);
1843         register_node_type_cmp_premulkey(ntypelist);
1844         
1845         register_node_type_cmp_diff_matte(ntypelist);
1846         register_node_type_cmp_distance_matte(ntypelist);
1847         register_node_type_cmp_chroma_matte(ntypelist);
1848         register_node_type_cmp_color_matte(ntypelist);
1849         register_node_type_cmp_channel_matte(ntypelist);
1850         register_node_type_cmp_color_spill(ntypelist);
1851         register_node_type_cmp_luma_matte(ntypelist);
1852         
1853         register_node_type_cmp_translate(ntypelist);
1854         register_node_type_cmp_rotate(ntypelist);
1855         register_node_type_cmp_scale(ntypelist);
1856         register_node_type_cmp_flip(ntypelist);
1857         register_node_type_cmp_crop(ntypelist);
1858         register_node_type_cmp_displace(ntypelist);
1859         register_node_type_cmp_mapuv(ntypelist);
1860         register_node_type_cmp_glare(ntypelist);
1861         register_node_type_cmp_tonemap(ntypelist);
1862         register_node_type_cmp_lensdist(ntypelist);
1863 }
1864
1865 static void registerShaderNodes(ListBase *ntypelist) 
1866 {
1867         register_node_type_frame(ntypelist);
1868
1869         register_node_type_sh_group(ntypelist);
1870         //register_node_type_sh_forloop(ntypelist);
1871         //register_node_type_sh_whileloop(ntypelist);
1872
1873         register_node_type_sh_output(ntypelist);
1874         register_node_type_sh_material(ntypelist);
1875         register_node_type_sh_camera(ntypelist);
1876         register_node_type_sh_value(ntypelist);
1877         register_node_type_sh_rgb(ntypelist);
1878         register_node_type_sh_mix_rgb(ntypelist);
1879         register_node_type_sh_valtorgb(ntypelist);
1880         register_node_type_sh_rgbtobw(ntypelist);
1881         register_node_type_sh_texture(ntypelist);
1882         register_node_type_sh_normal(ntypelist);
1883         register_node_type_sh_geom(ntypelist);
1884         register_node_type_sh_mapping(ntypelist);
1885         register_node_type_sh_curve_vec(ntypelist);
1886         register_node_type_sh_curve_rgb(ntypelist);
1887         register_node_type_sh_math(ntypelist);
1888         register_node_type_sh_vect_math(ntypelist);
1889         register_node_type_sh_squeeze(ntypelist);
1890         //register_node_type_sh_dynamic(ntypelist);
1891         register_node_type_sh_material_ext(ntypelist);
1892         register_node_type_sh_invert(ntypelist);
1893         register_node_type_sh_seprgb(ntypelist);
1894         register_node_type_sh_combrgb(ntypelist);
1895         register_node_type_sh_hue_sat(ntypelist);
1896
1897         register_node_type_sh_attribute(ntypelist);
1898         register_node_type_sh_geometry(ntypelist);
1899         register_node_type_sh_light_path(ntypelist);
1900         register_node_type_sh_fresnel(ntypelist);
1901         register_node_type_sh_blend_weight(ntypelist);
1902         register_node_type_sh_tex_coord(ntypelist);
1903
1904         register_node_type_sh_background(ntypelist);
1905         register_node_type_sh_bsdf_diffuse(ntypelist);
1906         register_node_type_sh_bsdf_glossy(ntypelist);
1907         register_node_type_sh_bsdf_glass(ntypelist);
1908         //register_node_type_sh_bsdf_anisotropic(ntypelist);
1909         register_node_type_sh_bsdf_translucent(ntypelist);
1910         register_node_type_sh_bsdf_transparent(ntypelist);
1911         register_node_type_sh_bsdf_velvet(ntypelist);
1912         register_node_type_sh_emission(ntypelist);
1913         register_node_type_sh_holdout(ntypelist);
1914         register_node_type_sh_volume_transparent(ntypelist);
1915         register_node_type_sh_volume_isotropic(ntypelist);
1916         register_node_type_sh_mix_shader(ntypelist);
1917         register_node_type_sh_add_shader(ntypelist);
1918
1919         register_node_type_sh_output_lamp(ntypelist);
1920         register_node_type_sh_output_material(ntypelist);
1921         //register_node_type_sh_output_texture(ntypelist);
1922         register_node_type_sh_output_world(ntypelist);
1923
1924         register_node_type_sh_tex_image(ntypelist);
1925         register_node_type_sh_tex_environment(ntypelist);
1926         register_node_type_sh_tex_sky(ntypelist);
1927         register_node_type_sh_tex_voronoi(ntypelist);
1928         register_node_type_sh_tex_blend(ntypelist);
1929         register_node_type_sh_tex_magic(ntypelist);
1930         register_node_type_sh_tex_marble(ntypelist);
1931         register_node_type_sh_tex_clouds(ntypelist);
1932         register_node_type_sh_tex_wood(ntypelist);
1933         register_node_type_sh_tex_stucci(ntypelist);
1934         register_node_type_sh_tex_noise(ntypelist);
1935         register_node_type_sh_tex_distnoise(ntypelist);
1936         register_node_type_sh_tex_musgrave(ntypelist);
1937 }
1938
1939 static void registerTextureNodes(ListBase *ntypelist)
1940 {
1941         register_node_type_frame(ntypelist);
1942         
1943         register_node_type_tex_group(ntypelist);
1944 //      register_node_type_tex_forloop(ntypelist);
1945 //      register_node_type_tex_whileloop(ntypelist);
1946         
1947         register_node_type_tex_math(ntypelist);
1948         register_node_type_tex_mix_rgb(ntypelist);
1949         register_node_type_tex_valtorgb(ntypelist);
1950         register_node_type_tex_rgbtobw(ntypelist);
1951         register_node_type_tex_valtonor(ntypelist);
1952         register_node_type_tex_curve_rgb(ntypelist);
1953         register_node_type_tex_curve_time(ntypelist);
1954         register_node_type_tex_invert(ntypelist);
1955         register_node_type_tex_hue_sat(ntypelist);
1956         register_node_type_tex_coord(ntypelist);
1957         register_node_type_tex_distance(ntypelist);
1958         register_node_type_tex_compose(ntypelist);
1959         register_node_type_tex_decompose(ntypelist);
1960         
1961         register_node_type_tex_output(ntypelist);
1962         register_node_type_tex_viewer(ntypelist);
1963         
1964         register_node_type_tex_checker(ntypelist);
1965         register_node_type_tex_texture(ntypelist);
1966         register_node_type_tex_bricks(ntypelist);
1967         register_node_type_tex_image(ntypelist);
1968         
1969         register_node_type_tex_rotate(ntypelist);
1970         register_node_type_tex_translate(ntypelist);
1971         register_node_type_tex_scale(ntypelist);
1972         register_node_type_tex_at(ntypelist);
1973         
1974         register_node_type_tex_proc_voronoi(ntypelist);
1975         register_node_type_tex_proc_blend(ntypelist);
1976         register_node_type_tex_proc_magic(ntypelist);
1977         register_node_type_tex_proc_marble(ntypelist);
1978         register_node_type_tex_proc_clouds(ntypelist);
1979         register_node_type_tex_proc_wood(ntypelist);
1980         register_node_type_tex_proc_musgrave(ntypelist);
1981         register_node_type_tex_proc_noise(ntypelist);
1982         register_node_type_tex_proc_stucci(ntypelist);
1983         register_node_type_tex_proc_distnoise(ntypelist);
1984 }
1985
1986 static void free_dynamic_typeinfo(bNodeType *ntype)
1987 {
1988         if(ntype->type==NODE_DYNAMIC) {
1989                 if(ntype->inputs) {
1990                         MEM_freeN(ntype->inputs);
1991                 }
1992                 if(ntype->outputs) {
1993                         MEM_freeN(ntype->outputs);
1994                 }
1995                 if(ntype->name) {
1996                         MEM_freeN((void *)ntype->name);
1997                 }
1998         }
1999 }
2000
2001 static void free_typeinfos(ListBase *list)
2002 {
2003         bNodeType *ntype, *next;
2004         for(ntype=list->first; ntype; ntype=next) {
2005                 next = ntype->next;
2006                 
2007                 if(ntype->type==NODE_DYNAMIC)
2008                         free_dynamic_typeinfo(ntype);
2009                 
2010                 if(ntype->needs_free)
2011                         MEM_freeN(ntype);
2012         }
2013 }
2014
2015 void init_nodesystem(void) 
2016 {
2017         registerCompositNodes(&ntreeGetType(NTREE_COMPOSIT)->node_types);
2018         registerShaderNodes(&ntreeGetType(NTREE_SHADER)->node_types);
2019         registerTextureNodes(&ntreeGetType(NTREE_TEXTURE)->node_types);
2020 }
2021
2022 void free_nodesystem(void) 
2023 {
2024         free_typeinfos(&ntreeGetType(NTREE_COMPOSIT)->node_types);
2025         free_typeinfos(&ntreeGetType(NTREE_SHADER)->node_types);
2026         free_typeinfos(&ntreeGetType(NTREE_TEXTURE)->node_types);
2027 }
2028
2029 /* called from unlink_scene, when deleting a scene goes over all scenes
2030  * other than the input, checks if they have render layer nodes referencing
2031  * the to-be-deleted scene, and resets them to NULL. */
2032
2033 /* XXX needs to get current scene then! */
2034 void clear_scene_in_nodes(Main *bmain, Scene *sce)
2035 {
2036         Scene *sce1;
2037         bNode *node;
2038
2039         for(sce1= bmain->scene.first; sce1; sce1=sce1->id.next) {
2040                 if(sce1!=sce) {
2041                         if(sce1->nodetree) {
2042                                 for(node= sce1->nodetree->nodes.first; node; node= node->next) {
2043                                         if(node->type==CMP_NODE_R_LAYERS) {
2044                                                 Scene *nodesce= (Scene *)node->id;
2045                                                 
2046                                                 if (nodesce==sce) node->id = NULL;
2047                                         }
2048                                 }
2049                         }
2050                 }
2051         }
2052 }
2053