minor warning/fixes
[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->id);
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                                         linearrgb_to_srgb_uchar4(tar, col);
794                                 }
795                                 else {
796                                         rgba_float_to_uchar(tar, col);
797                                 }
798                         }
799                         //else printf("prv out bound x y %d %d\n", x, y);
800                 }
801                 //else printf("prv out bound x y %d %d\n", x, y);
802         }
803 }
804
805 /* ************** Free stuff ********** */
806
807 /* goes over entire tree */
808 void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
809 {
810         bNodeLink *link, *next;
811         bNodeSocket *sock;
812         ListBase *lb;
813         
814         for(link= ntree->links.first; link; link= next) {
815                 next= link->next;
816                 
817                 if(link->fromnode==node) {
818                         lb= &node->outputs;
819                         if (link->tonode)
820                                 link->tonode->update |= NODE_UPDATE;
821                 }
822                 else if(link->tonode==node)
823                         lb= &node->inputs;
824                 else
825                         lb= NULL;
826
827                 if(lb) {
828                         for(sock= lb->first; sock; sock= sock->next) {
829                                 if(link->fromsock==sock || link->tosock==sock)
830                                         break;
831                         }
832                         if(sock) {
833                                 nodeRemLink(ntree, link);
834                         }
835                 }
836         }
837 }
838
839 static void node_unlink_attached(bNodeTree *ntree, bNode *parent)
840 {
841         bNode *node;
842         for (node=ntree->nodes.first; node; node=node->next) {
843                 if (node->parent == parent)
844                         nodeDetachNode(node);
845         }
846 }
847
848 void nodeFreeNode(bNodeTree *ntree, bNode *node)
849 {
850         bNodeTreeType *treetype= ntreeGetType(ntree->type);
851         bNodeSocket *sock, *nextsock;
852         
853         /* remove all references to this node */
854         nodeUnlinkNode(ntree, node);
855         node_unlink_attached(ntree, node);
856         
857         BLI_remlink(&ntree->nodes, node);
858
859         /* since it is called while free database, node->id is undefined */
860         
861         if (treetype->free_node_cache)
862                 treetype->free_node_cache(ntree, node);
863         
864         for (sock=node->inputs.first; sock; sock = nextsock) {
865                 nextsock = sock->next;
866                 if (sock->default_value)
867                         MEM_freeN(sock->default_value);
868                 MEM_freeN(sock);
869         }
870         for (sock=node->outputs.first; sock; sock = nextsock) {
871                 nextsock = sock->next;
872                 if (sock->default_value)
873                         MEM_freeN(sock->default_value);
874                 MEM_freeN(sock);
875         }
876
877         nodeFreePreview(node);
878
879         if(node->typeinfo && node->typeinfo->freestoragefunc) {
880                 node->typeinfo->freestoragefunc(node);
881         }
882
883         MEM_freeN(node);
884         
885         ntree->update |= NTREE_UPDATE_NODES;
886 }
887
888 /* do not free ntree itself here, free_libblock calls this function too */
889 void ntreeFreeTree(bNodeTree *ntree)
890 {
891         bNode *node, *next;
892         bNodeSocket *sock;
893         
894         if(ntree==NULL) return;
895         
896         /* XXX hack! node trees should not store execution graphs at all.
897          * This should be removed when old tree types no longer require it.
898          * Currently the execution data for texture nodes remains in the tree
899          * after execution, until the node tree is updated or freed.
900          */
901         if (ntree->execdata) {
902                 switch (ntree->type) {
903                 case NTREE_COMPOSIT:
904                         ntreeCompositEndExecTree(ntree->execdata, 1);
905                         break;
906                 case NTREE_SHADER:
907                         ntreeShaderEndExecTree(ntree->execdata, 1);
908                         break;
909                 case NTREE_TEXTURE:
910                         ntreeTexEndExecTree(ntree->execdata, 1);
911                         break;
912                 }
913         }
914         
915         BKE_free_animdata((ID *)ntree);
916         
917         id_us_min((ID *)ntree->gpd);
918
919         BLI_freelistN(&ntree->links);   /* do first, then unlink_node goes fast */
920         
921         for(node= ntree->nodes.first; node; node= next) {
922                 next= node->next;
923                 nodeFreeNode(ntree, node);
924         }
925         
926         for (sock=ntree->inputs.first; sock; sock=sock->next)
927                 if (sock->default_value)
928                         MEM_freeN(sock->default_value);
929         BLI_freelistN(&ntree->inputs);
930         for (sock=ntree->outputs.first; sock; sock=sock->next)
931                 if (sock->default_value)
932                         MEM_freeN(sock->default_value);
933         BLI_freelistN(&ntree->outputs);
934 }
935
936 void ntreeFreeCache(bNodeTree *ntree)
937 {
938         bNodeTreeType *treetype;
939         
940         if(ntree==NULL) return;
941         
942         treetype= ntreeGetType(ntree->type);
943         if (treetype->free_cache)
944                 treetype->free_cache(ntree);
945 }
946
947 void ntreeSetOutput(bNodeTree *ntree)
948 {
949         bNode *node;
950
951         /* find the active outputs, might become tree type dependant handler */
952         for(node= ntree->nodes.first; node; node= node->next) {
953                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
954                         bNode *tnode;
955                         int output= 0;
956                         
957                         /* we need a check for which output node should be tagged like this, below an exception */
958                         if(node->type==CMP_NODE_OUTPUT_FILE)
959                                 continue;
960
961                         /* there is more types having output class, each one is checked */
962                         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
963                                 if(tnode->typeinfo->nclass==NODE_CLASS_OUTPUT) {
964                                         
965                                         if(ntree->type==NTREE_COMPOSIT) {
966                                                         
967                                                 /* same type, exception for viewer */
968                                                 if(tnode->type==node->type ||
969                                                    (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) &&
970                                                         ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))) {
971                                                         if(tnode->flag & NODE_DO_OUTPUT) {
972                                                                 output++;
973                                                                 if(output>1)
974                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
975                                                         }
976                                                 }
977                                         }
978                                         else {
979                                                 /* same type */
980                                                 if(tnode->type==node->type) {
981                                                         if(tnode->flag & NODE_DO_OUTPUT) {
982                                                                 output++;
983                                                                 if(output>1)
984                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
985                                                         }
986                                                 }
987                                         }
988                                 }
989                         }
990                         if(output==0)
991                                 node->flag |= NODE_DO_OUTPUT;
992                 }
993         }
994         
995         /* here we could recursively set which nodes have to be done,
996                 might be different for editor or for "real" use... */
997 }
998
999 typedef struct MakeLocalCallData {
1000         ID *group_id;
1001         ID *new_id;
1002         int lib, local;
1003 } MakeLocalCallData;
1004
1005 static void ntreeMakeLocal_CheckLocal(void *calldata, ID *owner_id, bNodeTree *ntree)
1006 {
1007         MakeLocalCallData *cd= (MakeLocalCallData*)calldata;
1008         bNode *node;
1009         
1010         /* find if group is in tree */
1011         for(node= ntree->nodes.first; node; node= node->next) {
1012                 if(node->id == cd->group_id) {
1013                         if(owner_id->lib) cd->lib= 1;
1014                         else cd->local= 1;
1015                 }
1016         }
1017 }
1018
1019 static void ntreeMakeLocal_LinkNew(void *calldata, ID *owner_id, bNodeTree *ntree)
1020 {
1021         MakeLocalCallData *cd= (MakeLocalCallData*)calldata;
1022         bNode *node;
1023         
1024         /* find if group is in tree */
1025         for(node= ntree->nodes.first; node; node= node->next) {
1026                 if(node->id == cd->group_id) {
1027                         if(owner_id->lib==NULL) {
1028                                 node->id= cd->new_id;
1029                                 cd->new_id->us++;
1030                                 cd->group_id->us--;
1031                         }
1032                 }
1033         }
1034 }
1035
1036 void ntreeMakeLocal(bNodeTree *ntree)
1037 {
1038         Main *bmain= G.main;
1039         bNodeTreeType *treetype= ntreeGetType(ntree->type);
1040         MakeLocalCallData cd;
1041         
1042         /* - only lib users: do nothing
1043                 * - only local users: set flag
1044                 * - mixed: make copy
1045                 */
1046         
1047         if(ntree->id.lib==NULL) return;
1048         if(ntree->id.us==1) {
1049                 id_clear_lib_data(bmain, (ID *)ntree);
1050                 return;
1051         }
1052         
1053         /* now check users of groups... again typedepending, callback... */
1054         cd.group_id = &ntree->id;
1055         cd.new_id = NULL;
1056         cd.local = 0;
1057         cd.lib = 0;
1058         
1059         treetype->foreach_nodetree(G.main, &cd, &ntreeMakeLocal_CheckLocal);
1060         
1061         /* if all users are local, we simply make tree local */
1062         if(cd.local && cd.lib==0) {
1063                 id_clear_lib_data(bmain, (ID *)ntree);
1064         }
1065         else if(cd.local && cd.lib) {
1066                 /* this is the mixed case, we copy the tree and assign it to local users */
1067                 bNodeTree *newtree= ntreeCopyTree(ntree);
1068                 
1069                 newtree->id.us= 0;
1070                 
1071
1072                 cd.new_id = &newtree->id;
1073                 treetype->foreach_nodetree(G.main, &cd, &ntreeMakeLocal_LinkNew);
1074         }
1075 }
1076
1077 int ntreeNodeExists(bNodeTree *ntree, bNode *testnode)
1078 {
1079         bNode *node= ntree->nodes.first;
1080         for(; node; node= node->next)
1081                 if(node==testnode)
1082                         return 1;
1083         return 0;
1084 }
1085
1086 int ntreeOutputExists(bNode *node, bNodeSocket *testsock)
1087 {
1088         bNodeSocket *sock= node->outputs.first;
1089         for(; sock; sock= sock->next)
1090                 if(sock==testsock)
1091                         return 1;
1092         return 0;
1093 }
1094
1095 /* returns localized tree for execution in threads */
1096 bNodeTree *ntreeLocalize(bNodeTree *ntree)
1097 {
1098         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1099
1100         bNodeTree *ltree;
1101         bNode *node;
1102         
1103         bAction *action_backup= NULL, *tmpact_backup= NULL;
1104         
1105         /* Workaround for copying an action on each render!
1106          * set action to NULL so animdata actions dont get copied */
1107         AnimData *adt= BKE_animdata_from_id(&ntree->id);
1108
1109         if(adt) {
1110                 action_backup= adt->action;
1111                 tmpact_backup= adt->tmpact;
1112
1113                 adt->action= NULL;
1114                 adt->tmpact= NULL;
1115         }
1116
1117         /* node copy func */
1118         ltree= ntreeCopyTree(ntree);
1119
1120         if(adt) {
1121                 AnimData *ladt= BKE_animdata_from_id(&ltree->id);
1122
1123                 adt->action= ladt->action= action_backup;
1124                 adt->tmpact= ladt->tmpact= tmpact_backup;
1125
1126                 if(action_backup) action_backup->id.us++;
1127                 if(tmpact_backup) tmpact_backup->id.us++;
1128                 
1129         }
1130         /* end animdata uglyness */
1131
1132         /* ensures only a single output node is enabled */
1133         ntreeSetOutput(ntree);
1134
1135         for(node= ntree->nodes.first; node; node= node->next) {
1136                 /* store new_node pointer to original */
1137                 node->new_node->new_node= node;
1138         }
1139
1140         if (ntreetype->localize)
1141                 ntreetype->localize(ltree, ntree);
1142
1143         return ltree;
1144 }
1145
1146 /* sync local composite with real tree */
1147 /* local tree is supposed to be running, be careful moving previews! */
1148 /* is called by jobs manager, outside threads, so it doesnt happen during draw */
1149 void ntreeLocalSync(bNodeTree *localtree, bNodeTree *ntree)
1150 {
1151         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1152
1153         if (ntreetype->local_sync)
1154                 ntreetype->local_sync(localtree, ntree);
1155 }
1156
1157 /* merge local tree results back, and free local tree */
1158 /* we have to assume the editor already changed completely */
1159 void ntreeLocalMerge(bNodeTree *localtree, bNodeTree *ntree)
1160 {
1161         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1162         bNode *lnode;
1163         
1164         /* move over the compbufs and previews */
1165         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
1166                 if(ntreeNodeExists(ntree, lnode->new_node)) {
1167                         if(lnode->preview && lnode->preview->rect) {
1168                                 nodeFreePreview(lnode->new_node);
1169                                 lnode->new_node->preview= lnode->preview;
1170                                 lnode->preview= NULL;
1171                         }
1172                 }
1173         }
1174
1175         if (ntreetype->local_merge)
1176                 ntreetype->local_merge(localtree, ntree);
1177
1178         ntreeFreeTree(localtree);
1179         MEM_freeN(localtree);
1180 }
1181
1182 /* ************ find stuff *************** */
1183
1184 int ntreeHasType(bNodeTree *ntree, int type)
1185 {
1186         bNode *node;
1187         
1188         if(ntree)
1189                 for(node= ntree->nodes.first; node; node= node->next)
1190                         if(node->type == type)
1191                                 return 1;
1192         return 0;
1193 }
1194
1195 bNodeLink *nodeFindLink(bNodeTree *ntree, bNodeSocket *from, bNodeSocket *to)
1196 {
1197         bNodeLink *link;
1198         
1199         for(link= ntree->links.first; link; link= link->next) {
1200                 if(link->fromsock==from && link->tosock==to)
1201                         return link;
1202                 if(link->fromsock==to && link->tosock==from)    /* hrms? */
1203                         return link;
1204         }
1205         return NULL;
1206 }
1207
1208 int nodeCountSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1209 {
1210         bNodeLink *link;
1211         int tot= 0;
1212         
1213         for(link= ntree->links.first; link; link= link->next) {
1214                 if(link->fromsock==sock || link->tosock==sock)
1215                         tot++;
1216         }
1217         return tot;
1218 }
1219
1220 bNode *nodeGetActive(bNodeTree *ntree)
1221 {
1222         bNode *node;
1223         
1224         if(ntree==NULL) return NULL;
1225         
1226         for(node= ntree->nodes.first; node; node= node->next)
1227                 if(node->flag & NODE_ACTIVE)
1228                         break;
1229         return node;
1230 }
1231
1232 /* two active flags, ID nodes have special flag for buttons display */
1233 bNode *nodeGetActiveID(bNodeTree *ntree, short idtype)
1234 {
1235         bNode *node;
1236         
1237         if(ntree==NULL) return NULL;
1238
1239         /* check for group edit */
1240         for(node= ntree->nodes.first; node; node= node->next)
1241                 if(node->flag & NODE_GROUP_EDIT)
1242                         break;
1243
1244         if(node)
1245                 ntree= (bNodeTree*)node->id;
1246         
1247         /* now find active node with this id */
1248         for(node= ntree->nodes.first; node; node= node->next)
1249                 if(node->id && GS(node->id->name)==idtype)
1250                         if(node->flag & NODE_ACTIVE_ID)
1251                                 break;
1252
1253         return node;
1254 }
1255
1256 int nodeSetActiveID(bNodeTree *ntree, short idtype, ID *id)
1257 {
1258         bNode *node;
1259         int ok= FALSE;
1260
1261         if(ntree==NULL) return ok;
1262
1263         /* check for group edit */
1264         for(node= ntree->nodes.first; node; node= node->next)
1265                 if(node->flag & NODE_GROUP_EDIT)
1266                         break;
1267
1268         if(node)
1269                 ntree= (bNodeTree*)node->id;
1270
1271         /* now find active node with this id */
1272         for(node= ntree->nodes.first; node; node= node->next) {
1273                 if(node->id && GS(node->id->name)==idtype) {
1274                         if(id && ok==FALSE && node->id==id) {
1275                                 node->flag |= NODE_ACTIVE_ID;
1276                                 ok= TRUE;
1277                         } else {
1278                                 node->flag &= ~NODE_ACTIVE_ID;
1279                         }
1280                 }
1281         }
1282
1283         return ok;
1284 }
1285
1286
1287 /* two active flags, ID nodes have special flag for buttons display */
1288 void nodeClearActiveID(bNodeTree *ntree, short idtype)
1289 {
1290         bNode *node;
1291         
1292         if(ntree==NULL) return;
1293         
1294         for(node= ntree->nodes.first; node; node= node->next)
1295                 if(node->id && GS(node->id->name)==idtype)
1296                         node->flag &= ~NODE_ACTIVE_ID;
1297 }
1298
1299 /* two active flags, ID nodes have special flag for buttons display */
1300 void nodeSetActive(bNodeTree *ntree, bNode *node)
1301 {
1302         bNode *tnode;
1303         
1304         /* make sure only one node is active, and only one per ID type */
1305         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1306                 tnode->flag &= ~NODE_ACTIVE;
1307                 
1308                 if(node->id && tnode->id) {
1309                         if(GS(node->id->name) == GS(tnode->id->name))
1310                                 tnode->flag &= ~NODE_ACTIVE_ID;
1311                 }
1312                 if(node->typeinfo->nclass == NODE_CLASS_TEXTURE)
1313                         tnode->flag &= ~NODE_ACTIVE_TEXTURE;
1314         }
1315         
1316         node->flag |= NODE_ACTIVE;
1317         if(node->id)
1318                 node->flag |= NODE_ACTIVE_ID;
1319         if(node->typeinfo->nclass == NODE_CLASS_TEXTURE)
1320                 node->flag |= NODE_ACTIVE_TEXTURE;
1321 }
1322
1323 int nodeSocketIsHidden(bNodeSocket *sock)
1324 {
1325         return ((sock->flag & (SOCK_HIDDEN | SOCK_AUTO_HIDDEN | SOCK_UNAVAIL)) != 0);
1326 }
1327
1328 /* ************** dependency stuff *********** */
1329
1330 /* node is guaranteed to be not checked before */
1331 static int node_get_deplist_recurs(bNode *node, bNode ***nsort)
1332 {
1333         bNode *fromnode;
1334         bNodeSocket *sock;
1335         int level = 0xFFF;
1336         
1337         node->done= 1;
1338         
1339         /* check linked nodes */
1340         for(sock= node->inputs.first; sock; sock= sock->next) {
1341                 if(sock->link) {
1342                         fromnode= sock->link->fromnode;
1343                         if(fromnode) {
1344                                 if (fromnode->done==0)
1345                                         fromnode->level= node_get_deplist_recurs(fromnode, nsort);
1346                                 if (fromnode->level <= level)
1347                                         level = fromnode->level - 1;
1348                         }
1349                 }
1350         }
1351         
1352         /* check parent node */
1353         if (node->parent) {
1354                 if (node->parent->done==0)
1355                         node->parent->level= node_get_deplist_recurs(node->parent, nsort);
1356                 if (node->parent->level <= level)
1357                         level = node->parent->level - 1;
1358         }
1359         
1360         if (nsort) {
1361                 **nsort= node;
1362                 (*nsort)++;
1363         }
1364         
1365         return level;
1366 }
1367
1368 void ntreeGetDependencyList(struct bNodeTree *ntree, struct bNode ***deplist, int *totnodes)
1369 {
1370         bNode *node, **nsort;
1371         
1372         *totnodes=0;
1373         
1374         /* first clear data */
1375         for(node= ntree->nodes.first; node; node= node->next) {
1376                 node->done= 0;
1377                 (*totnodes)++;
1378         }
1379         if(*totnodes==0) {
1380                 *deplist = NULL;
1381                 return;
1382         }
1383         
1384         nsort= *deplist= MEM_callocN((*totnodes)*sizeof(bNode*), "sorted node array");
1385         
1386         /* recursive check */
1387         for(node= ntree->nodes.first; node; node= node->next) {
1388                 if(node->done==0) {
1389                         node->level= node_get_deplist_recurs(node, &nsort);
1390                 }
1391         }
1392 }
1393
1394 static void ntree_update_link_pointers(bNodeTree *ntree)
1395 {
1396         bNode *node;
1397         bNodeSocket *sock;
1398         bNodeLink *link;
1399         
1400         /* first clear data */
1401         for(node= ntree->nodes.first; node; node= node->next) {
1402                 for(sock= node->inputs.first; sock; sock= sock->next) {
1403                         sock->link= NULL;
1404                         sock->flag &= ~SOCK_IN_USE;
1405                 }
1406                 for(sock= node->outputs.first; sock; sock= sock->next) {
1407                         sock->flag &= ~SOCK_IN_USE;
1408                 }
1409         }
1410         for(sock= ntree->inputs.first; sock; sock= sock->next) {
1411                 sock->flag &= ~SOCK_IN_USE;
1412         }
1413         for(sock= ntree->outputs.first; sock; sock= sock->next) {
1414                 sock->link= NULL;
1415                 sock->flag &= ~SOCK_IN_USE;
1416         }
1417
1418         for(link= ntree->links.first; link; link= link->next) {
1419                 link->tosock->link= link;
1420                 
1421                 link->fromsock->flag |= SOCK_IN_USE;
1422                 link->tosock->flag |= SOCK_IN_USE;
1423         }
1424 }
1425
1426 static void ntree_validate_links(bNodeTree *ntree)
1427 {
1428         bNodeTreeType *ntreetype = ntreeGetType(ntree->type);
1429         bNodeLink *link;
1430         
1431         for (link = ntree->links.first; link; link = link->next) {
1432                 link->flag |= NODE_LINK_VALID;
1433                 if (link->fromnode && link->tonode && link->fromnode->level <= link->tonode->level)
1434                         link->flag &= ~NODE_LINK_VALID;
1435                 else if (ntreetype->validate_link) {
1436                         if (!ntreetype->validate_link(ntree, link))
1437                                 link->flag &= ~NODE_LINK_VALID;
1438                 }
1439         }
1440 }
1441
1442 static void ntree_verify_nodes_cb(void *calldata, struct ID *UNUSED(owner_id), struct bNodeTree *ntree)
1443 {
1444         ID *id= (ID*)calldata;
1445         bNode *node;
1446         
1447         for (node=ntree->nodes.first; node; node=node->next)
1448                 if (node->typeinfo->verifyfunc)
1449                         node->typeinfo->verifyfunc(ntree, node, id);
1450 }
1451
1452 void ntreeVerifyNodes(struct Main *main, struct ID *id)
1453 {
1454         bNodeTreeType *ntreetype;
1455         bNodeTree *ntree;
1456         int n;
1457         
1458         for (n=0; n < NUM_NTREE_TYPES; ++n) {
1459                 ntreetype= ntreeGetType(n);
1460                 if (ntreetype && ntreetype->foreach_nodetree)
1461                         ntreetype->foreach_nodetree(main, id, ntree_verify_nodes_cb);
1462         }
1463         for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
1464                 ntree_verify_nodes_cb(id, NULL, ntree);
1465 }
1466
1467 void ntreeUpdateTree(bNodeTree *ntree)
1468 {
1469         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1470         bNode *node;
1471         bNode **deplist;
1472         int totnodes, n;
1473         
1474         ntree_update_link_pointers(ntree);
1475         
1476         /* also updates the node level! */
1477         ntreeGetDependencyList(ntree, &deplist, &totnodes);
1478         
1479         if (deplist) {
1480                 /* update individual nodes */
1481                 for (n=0; n < totnodes; ++n) {
1482                         node = deplist[n];
1483                         
1484                         /* node tree update tags override individual node update flags */
1485                         if ((node->update & NODE_UPDATE) || (ntree->update & NTREE_UPDATE)) {
1486                                 if (ntreetype->update_node)
1487                                         ntreetype->update_node(ntree, node);
1488                                 else if (node->typeinfo->updatefunc)
1489                                         node->typeinfo->updatefunc(ntree, node);
1490                         }
1491                         /* clear update flag */
1492                         node->update = 0;
1493                 }
1494                 
1495                 MEM_freeN(deplist);
1496         }
1497         
1498         /* general tree updates */
1499         if (ntree->update & (NTREE_UPDATE_LINKS|NTREE_UPDATE_NODES)) {
1500                 ntree_validate_links(ntree);
1501         }
1502         
1503         /* update tree */
1504         if (ntreetype->update)
1505                 ntreetype->update(ntree);
1506         else {
1507                 /* Trees can be associated with a specific node type (i.e. group nodes),
1508                  * in that case a tree update function may be defined by that node type.
1509                  */
1510                 bNodeType *ntype= node_get_type(ntree, ntree->nodetype);
1511                 if (ntype && ntype->updatetreefunc)
1512                         ntype->updatetreefunc(ntree);
1513         }
1514         
1515         /* XXX hack, should be done by depsgraph!! */
1516         ntreeVerifyNodes(G.main, &ntree->id);
1517         
1518         /* clear the update flag */
1519         ntree->update = 0;
1520 }
1521
1522 void nodeUpdate(bNodeTree *ntree, bNode *node)
1523 {
1524         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1525         
1526         if (ntreetype->update_node)
1527                 ntreetype->update_node(ntree, node);
1528         else if (node->typeinfo->updatefunc)
1529                 node->typeinfo->updatefunc(ntree, node);
1530         /* clear update flag */
1531         node->update = 0;
1532 }
1533
1534 int nodeUpdateID(bNodeTree *ntree, ID *id)
1535 {
1536         bNodeTreeType *ntreetype;
1537         bNode *node;
1538         int change = FALSE;
1539         
1540         if(ELEM(NULL, id, ntree))
1541                 return change;
1542         
1543         ntreetype = ntreeGetType(ntree->type);
1544         
1545         if (ntreetype->update_node) {
1546                 for(node= ntree->nodes.first; node; node= node->next) {
1547                         if(node->id==id) {
1548                                 change = TRUE;
1549                                 ntreetype->update_node(ntree, node);
1550                                 /* clear update flag */
1551                                 node->update = 0;
1552                         }
1553                 }
1554         }
1555         else {
1556                 for(node= ntree->nodes.first; node; node= node->next) {
1557                         if(node->id==id) {
1558                                 change = TRUE;
1559                                 if (node->typeinfo->updatefunc)
1560                                         node->typeinfo->updatefunc(ntree, node);
1561                                 /* clear update flag */
1562                                 node->update = 0;
1563                         }
1564                 }
1565         }
1566         
1567         return change;
1568 }
1569
1570
1571 /* ************* node type access ********** */
1572
1573 int nodeValid(bNodeTree *ntree, bNodeTemplate *ntemp)
1574 {
1575         bNodeType *ntype= node_get_type(ntree, ntemp->type);
1576         if (ntype) {
1577                 if (ntype->validfunc)
1578                         return ntype->validfunc(ntree, ntemp);
1579                 else
1580                         return 1;
1581         }
1582         else
1583                 return 0;
1584 }
1585
1586 const char* nodeLabel(bNode *node)
1587 {
1588         if (node->label[0]!='\0')
1589                 return node->label;
1590         else if (node->typeinfo->labelfunc)
1591                 return node->typeinfo->labelfunc(node);
1592         else
1593                 return node->typeinfo->name;
1594 }
1595
1596 struct bNodeTree *nodeGroupEditGet(struct bNode *node)
1597 {
1598         if (node->typeinfo->group_edit_get)
1599                 return node->typeinfo->group_edit_get(node);
1600         else
1601                 return NULL;
1602 }
1603
1604 struct bNodeTree *nodeGroupEditSet(struct bNode *node, int edit)
1605 {
1606         if (node->typeinfo->group_edit_set)
1607                 return node->typeinfo->group_edit_set(node, edit);
1608         else if (node->typeinfo->group_edit_get)
1609                 return node->typeinfo->group_edit_get(node);
1610         else
1611                 return NULL;
1612 }
1613
1614 void nodeGroupEditClear(struct bNode *node)
1615 {
1616         if (node->typeinfo->group_edit_clear)
1617                 node->typeinfo->group_edit_clear(node);
1618 }
1619
1620 struct bNodeTemplate nodeMakeTemplate(struct bNode *node)
1621 {
1622         bNodeTemplate ntemp;
1623         if (node->typeinfo->templatefunc)
1624                 return node->typeinfo->templatefunc(node);
1625         else {
1626                 ntemp.type = node->type;
1627                 return ntemp;
1628         }
1629 }
1630
1631 void node_type_base(bNodeTreeType *ttype, bNodeType *ntype, int type, const char *name, short nclass, short flag)
1632 {
1633         memset(ntype, 0, sizeof(bNodeType));
1634
1635         ntype->type = type;
1636         BLI_strncpy(ntype->name, name, sizeof(ntype->name));
1637         ntype->nclass = nclass;
1638         ntype->flag = flag;
1639
1640         /* Default muting stuff. */
1641         if(ttype) {
1642                 ntype->mutefunc      = ttype->mutefunc;
1643                 ntype->mutelinksfunc = ttype->mutelinksfunc;
1644                 ntype->gpumutefunc   = ttype->gpumutefunc;
1645         }
1646
1647         /* default size values */
1648         ntype->width = 140;
1649         ntype->minwidth = 100;
1650         ntype->maxwidth = 320;
1651         ntype->height = 100;
1652         ntype->minheight = 30;
1653         ntype->maxheight = FLT_MAX;
1654 }
1655
1656 void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
1657 {
1658         ntype->inputs = inputs;
1659         ntype->outputs = outputs;
1660 }
1661
1662 void node_type_init(struct bNodeType *ntype, void (*initfunc)(struct bNodeTree *ntree, struct bNode *node, struct bNodeTemplate *ntemp))
1663 {
1664         ntype->initfunc = initfunc;
1665 }
1666
1667 void node_type_valid(struct bNodeType *ntype, int (*validfunc)(struct bNodeTree *ntree, struct bNodeTemplate *ntemp))
1668 {
1669         ntype->validfunc = validfunc;
1670 }
1671
1672 void node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth)
1673 {
1674         ntype->width = width;
1675         ntype->minwidth = minwidth;
1676         if (maxwidth <= minwidth)
1677                 ntype->maxwidth = FLT_MAX;
1678         else
1679                 ntype->maxwidth = maxwidth;
1680 }
1681
1682 void node_type_storage(bNodeType *ntype, const char *storagename, void (*freestoragefunc)(struct bNode *), void (*copystoragefunc)(struct bNode *, struct bNode *))
1683 {
1684         if (storagename)
1685                 BLI_strncpy(ntype->storagename, storagename, sizeof(ntype->storagename));
1686         else
1687                 ntype->storagename[0] = '\0';
1688         ntype->copystoragefunc = copystoragefunc;
1689         ntype->freestoragefunc = freestoragefunc;
1690 }
1691
1692 void node_type_label(struct bNodeType *ntype, const char *(*labelfunc)(struct bNode *))
1693 {
1694         ntype->labelfunc = labelfunc;
1695 }
1696
1697 void node_type_template(struct bNodeType *ntype, struct bNodeTemplate (*templatefunc)(struct bNode *))
1698 {
1699         ntype->templatefunc = templatefunc;
1700 }
1701
1702 void node_type_update(struct bNodeType *ntype,
1703                                           void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node),
1704                                           void (*verifyfunc)(struct bNodeTree *ntree, struct bNode *node, struct ID *id))
1705 {
1706         ntype->updatefunc = updatefunc;
1707         ntype->verifyfunc = verifyfunc;
1708 }
1709
1710 void node_type_tree(struct bNodeType *ntype, void (*inittreefunc)(struct bNodeTree *), void (*updatetreefunc)(struct bNodeTree *))
1711 {
1712         ntype->inittreefunc = inittreefunc;
1713         ntype->updatetreefunc = updatetreefunc;
1714 }
1715
1716 void node_type_group_edit(struct bNodeType *ntype,
1717                                                   struct bNodeTree *(*group_edit_get)(struct bNode *node),
1718                                                   struct bNodeTree *(*group_edit_set)(struct bNode *node, int edit),
1719                                                   void (*group_edit_clear)(struct bNode *node))
1720 {
1721         ntype->group_edit_get = group_edit_get;
1722         ntype->group_edit_set = group_edit_set;
1723         ntype->group_edit_clear = group_edit_clear;
1724 }
1725
1726 void node_type_exec(struct bNodeType *ntype, void (*execfunc)(void *data, struct bNode *, struct bNodeStack **, struct bNodeStack **))
1727 {
1728         ntype->execfunc = execfunc;
1729 }
1730
1731 void node_type_exec_new(struct bNodeType *ntype,
1732                                                 void *(*initexecfunc)(struct bNode *node),
1733                                                 void (*freeexecfunc)(struct bNode *node, void *nodedata),
1734                                                 void (*newexecfunc)(void *data, int thread, struct bNode *, void *nodedata, struct bNodeStack **, struct bNodeStack **))
1735 {
1736         ntype->initexecfunc = initexecfunc;
1737         ntype->freeexecfunc = freeexecfunc;
1738         ntype->newexecfunc = newexecfunc;
1739 }
1740
1741 void node_type_mute(struct bNodeType *ntype,
1742                     void (*mutefunc)(void *data, int thread, struct bNode *, void *nodedata,
1743                                      struct bNodeStack **, struct bNodeStack **),
1744                     ListBase (*mutelinksfunc)(struct bNodeTree *, struct bNode *, struct bNodeStack **, struct bNodeStack **,
1745                                               struct GPUNodeStack *, struct GPUNodeStack *))
1746 {
1747         ntype->mutefunc = mutefunc;
1748         ntype->mutelinksfunc = mutelinksfunc;
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_gpu_mute(struct bNodeType *ntype, int (*gpumutefunc)(struct GPUMaterial *, struct bNode *, void *,
1762                                                                     struct GPUNodeStack *, struct GPUNodeStack *))
1763 {
1764         ntype->gpumutefunc = gpumutefunc;
1765 }
1766
1767 void node_type_compatibility(struct bNodeType *ntype, short compatibility)
1768 {
1769         ntype->compatibility = compatibility;
1770 }
1771
1772 static bNodeType *is_nodetype_registered(ListBase *typelist, int type) 
1773 {
1774         bNodeType *ntype= typelist->first;
1775         
1776         for(;ntype; ntype= ntype->next )
1777                 if(ntype->type==type)
1778                         return ntype;
1779         
1780         return NULL;
1781 }
1782
1783 void nodeRegisterType(bNodeTreeType *ttype, bNodeType *ntype) 
1784 {
1785         ListBase *typelist = &(ttype->node_types);
1786         bNodeType *found= is_nodetype_registered(typelist, ntype->type);
1787         
1788         if(found==NULL)
1789                 BLI_addtail(typelist, ntype);
1790 }
1791
1792 static void registerCompositNodes(bNodeTreeType *ttype)
1793 {
1794         register_node_type_frame(ttype);
1795         
1796         register_node_type_cmp_group(ttype);
1797 //      register_node_type_cmp_forloop(ttype);
1798 //      register_node_type_cmp_whileloop(ttype);
1799         
1800         register_node_type_cmp_rlayers(ttype);
1801         register_node_type_cmp_image(ttype);
1802         register_node_type_cmp_texture(ttype);
1803         register_node_type_cmp_value(ttype);
1804         register_node_type_cmp_rgb(ttype);
1805         register_node_type_cmp_curve_time(ttype);
1806         register_node_type_cmp_movieclip(ttype);
1807         
1808         register_node_type_cmp_composite(ttype);
1809         register_node_type_cmp_viewer(ttype);
1810         register_node_type_cmp_splitviewer(ttype);
1811         register_node_type_cmp_output_file(ttype);
1812         register_node_type_cmp_view_levels(ttype);
1813         
1814         register_node_type_cmp_curve_rgb(ttype);
1815         register_node_type_cmp_mix_rgb(ttype);
1816         register_node_type_cmp_hue_sat(ttype);
1817         register_node_type_cmp_brightcontrast(ttype);
1818         register_node_type_cmp_gamma(ttype);
1819         register_node_type_cmp_invert(ttype);
1820         register_node_type_cmp_alphaover(ttype);
1821         register_node_type_cmp_zcombine(ttype);
1822         register_node_type_cmp_colorbalance(ttype);
1823         register_node_type_cmp_huecorrect(ttype);
1824         
1825         register_node_type_cmp_normal(ttype);
1826         register_node_type_cmp_curve_vec(ttype);
1827         register_node_type_cmp_map_value(ttype);
1828         register_node_type_cmp_normalize(ttype);
1829         
1830         register_node_type_cmp_filter(ttype);
1831         register_node_type_cmp_blur(ttype);
1832         register_node_type_cmp_dblur(ttype);
1833         register_node_type_cmp_bilateralblur(ttype);
1834         register_node_type_cmp_vecblur(ttype);
1835         register_node_type_cmp_dilateerode(ttype);
1836         register_node_type_cmp_defocus(ttype);
1837         
1838         register_node_type_cmp_valtorgb(ttype);
1839         register_node_type_cmp_rgbtobw(ttype);
1840         register_node_type_cmp_setalpha(ttype);
1841         register_node_type_cmp_idmask(ttype);
1842         register_node_type_cmp_math(ttype);
1843         register_node_type_cmp_seprgba(ttype);
1844         register_node_type_cmp_combrgba(ttype);
1845         register_node_type_cmp_sephsva(ttype);
1846         register_node_type_cmp_combhsva(ttype);
1847         register_node_type_cmp_sepyuva(ttype);
1848         register_node_type_cmp_combyuva(ttype);
1849         register_node_type_cmp_sepycca(ttype);
1850         register_node_type_cmp_combycca(ttype);
1851         register_node_type_cmp_premulkey(ttype);
1852         
1853         register_node_type_cmp_diff_matte(ttype);
1854         register_node_type_cmp_distance_matte(ttype);
1855         register_node_type_cmp_chroma_matte(ttype);
1856         register_node_type_cmp_color_matte(ttype);
1857         register_node_type_cmp_channel_matte(ttype);
1858         register_node_type_cmp_color_spill(ttype);
1859         register_node_type_cmp_luma_matte(ttype);
1860     register_node_type_cmp_doubleedgemask(ttype);
1861
1862         register_node_type_cmp_translate(ttype);
1863         register_node_type_cmp_rotate(ttype);
1864         register_node_type_cmp_scale(ttype);
1865         register_node_type_cmp_flip(ttype);
1866         register_node_type_cmp_crop(ttype);
1867         register_node_type_cmp_displace(ttype);
1868         register_node_type_cmp_mapuv(ttype);
1869         register_node_type_cmp_glare(ttype);
1870         register_node_type_cmp_tonemap(ttype);
1871         register_node_type_cmp_lensdist(ttype);
1872         register_node_type_cmp_transform(ttype);
1873         register_node_type_cmp_stabilize2d(ttype);
1874         register_node_type_cmp_moviedistortion(ttype);
1875 }
1876
1877 static void registerShaderNodes(bNodeTreeType *ttype) 
1878 {
1879         register_node_type_frame(ttype);
1880         
1881         register_node_type_sh_group(ttype);
1882         //register_node_type_sh_forloop(ttype);
1883         //register_node_type_sh_whileloop(ttype);
1884
1885         register_node_type_sh_output(ttype);
1886         register_node_type_sh_material(ttype);
1887         register_node_type_sh_camera(ttype);
1888         register_node_type_sh_gamma(ttype);
1889         register_node_type_sh_value(ttype);
1890         register_node_type_sh_rgb(ttype);
1891         register_node_type_sh_mix_rgb(ttype);
1892         register_node_type_sh_valtorgb(ttype);
1893         register_node_type_sh_rgbtobw(ttype);
1894         register_node_type_sh_texture(ttype);
1895         register_node_type_sh_normal(ttype);
1896         register_node_type_sh_geom(ttype);
1897         register_node_type_sh_mapping(ttype);
1898         register_node_type_sh_curve_vec(ttype);
1899         register_node_type_sh_curve_rgb(ttype);
1900         register_node_type_sh_math(ttype);
1901         register_node_type_sh_vect_math(ttype);
1902         register_node_type_sh_squeeze(ttype);
1903         //register_node_type_sh_dynamic(ttype);
1904         register_node_type_sh_material_ext(ttype);
1905         register_node_type_sh_invert(ttype);
1906         register_node_type_sh_seprgb(ttype);
1907         register_node_type_sh_combrgb(ttype);
1908         register_node_type_sh_hue_sat(ttype);
1909
1910         register_node_type_sh_attribute(ttype);
1911         register_node_type_sh_geometry(ttype);
1912         register_node_type_sh_light_path(ttype);
1913         register_node_type_sh_fresnel(ttype);
1914         register_node_type_sh_layer_weight(ttype);
1915         register_node_type_sh_tex_coord(ttype);
1916
1917         register_node_type_sh_background(ttype);
1918         register_node_type_sh_bsdf_diffuse(ttype);
1919         register_node_type_sh_bsdf_glossy(ttype);
1920         register_node_type_sh_bsdf_glass(ttype);
1921         register_node_type_sh_bsdf_translucent(ttype);
1922         register_node_type_sh_bsdf_transparent(ttype);
1923         register_node_type_sh_bsdf_velvet(ttype);
1924         register_node_type_sh_emission(ttype);
1925         register_node_type_sh_holdout(ttype);
1926         //register_node_type_sh_volume_transparent(ttype);
1927         //register_node_type_sh_volume_isotropic(ttype);
1928         register_node_type_sh_mix_shader(ttype);
1929         register_node_type_sh_add_shader(ttype);
1930
1931         register_node_type_sh_output_lamp(ttype);
1932         register_node_type_sh_output_material(ttype);
1933         register_node_type_sh_output_world(ttype);
1934
1935         register_node_type_sh_tex_image(ttype);
1936         register_node_type_sh_tex_environment(ttype);
1937         register_node_type_sh_tex_sky(ttype);
1938         register_node_type_sh_tex_noise(ttype);
1939         register_node_type_sh_tex_wave(ttype);
1940         register_node_type_sh_tex_voronoi(ttype);
1941         register_node_type_sh_tex_musgrave(ttype);
1942         register_node_type_sh_tex_gradient(ttype);
1943         register_node_type_sh_tex_magic(ttype);
1944         register_node_type_sh_tex_checker(ttype);
1945 }
1946
1947 static void registerTextureNodes(bNodeTreeType *ttype)
1948 {
1949         register_node_type_frame(ttype);
1950         
1951         register_node_type_tex_group(ttype);
1952 //      register_node_type_tex_forloop(ttype);
1953 //      register_node_type_tex_whileloop(ttype);
1954         
1955         register_node_type_tex_math(ttype);
1956         register_node_type_tex_mix_rgb(ttype);
1957         register_node_type_tex_valtorgb(ttype);
1958         register_node_type_tex_rgbtobw(ttype);
1959         register_node_type_tex_valtonor(ttype);
1960         register_node_type_tex_curve_rgb(ttype);
1961         register_node_type_tex_curve_time(ttype);
1962         register_node_type_tex_invert(ttype);
1963         register_node_type_tex_hue_sat(ttype);
1964         register_node_type_tex_coord(ttype);
1965         register_node_type_tex_distance(ttype);
1966         register_node_type_tex_compose(ttype);
1967         register_node_type_tex_decompose(ttype);
1968         
1969         register_node_type_tex_output(ttype);
1970         register_node_type_tex_viewer(ttype);
1971         
1972         register_node_type_tex_checker(ttype);
1973         register_node_type_tex_texture(ttype);
1974         register_node_type_tex_bricks(ttype);
1975         register_node_type_tex_image(ttype);
1976         
1977         register_node_type_tex_rotate(ttype);
1978         register_node_type_tex_translate(ttype);
1979         register_node_type_tex_scale(ttype);
1980         register_node_type_tex_at(ttype);
1981         
1982         register_node_type_tex_proc_voronoi(ttype);
1983         register_node_type_tex_proc_blend(ttype);
1984         register_node_type_tex_proc_magic(ttype);
1985         register_node_type_tex_proc_marble(ttype);
1986         register_node_type_tex_proc_clouds(ttype);
1987         register_node_type_tex_proc_wood(ttype);
1988         register_node_type_tex_proc_musgrave(ttype);
1989         register_node_type_tex_proc_noise(ttype);
1990         register_node_type_tex_proc_stucci(ttype);
1991         register_node_type_tex_proc_distnoise(ttype);
1992 }
1993
1994 static void free_dynamic_typeinfo(bNodeType *ntype)
1995 {
1996         if(ntype->type==NODE_DYNAMIC) {
1997                 if(ntype->inputs) {
1998                         MEM_freeN(ntype->inputs);
1999                 }
2000                 if(ntype->outputs) {
2001                         MEM_freeN(ntype->outputs);
2002                 }
2003                 if(ntype->name) {
2004                         MEM_freeN((void *)ntype->name);
2005                 }
2006         }
2007 }
2008
2009 static void free_typeinfos(ListBase *list)
2010 {
2011         bNodeType *ntype, *next;
2012         for(ntype=list->first; ntype; ntype=next) {
2013                 next = ntype->next;
2014                 
2015                 if(ntype->type==NODE_DYNAMIC)
2016                         free_dynamic_typeinfo(ntype);
2017                 
2018                 if(ntype->needs_free)
2019                         MEM_freeN(ntype);
2020         }
2021 }
2022
2023 void init_nodesystem(void) 
2024 {
2025         registerCompositNodes(ntreeGetType(NTREE_COMPOSIT));
2026         registerShaderNodes(ntreeGetType(NTREE_SHADER));
2027         registerTextureNodes(ntreeGetType(NTREE_TEXTURE));
2028 }
2029
2030 void free_nodesystem(void) 
2031 {
2032         free_typeinfos(&ntreeGetType(NTREE_COMPOSIT)->node_types);
2033         free_typeinfos(&ntreeGetType(NTREE_SHADER)->node_types);
2034         free_typeinfos(&ntreeGetType(NTREE_TEXTURE)->node_types);
2035 }
2036
2037 /* called from unlink_scene, when deleting a scene goes over all scenes
2038  * other than the input, checks if they have render layer nodes referencing
2039  * the to-be-deleted scene, and resets them to NULL. */
2040
2041 /* XXX needs to get current scene then! */
2042 void clear_scene_in_nodes(Main *bmain, Scene *sce)
2043 {
2044         Scene *sce1;
2045         bNode *node;
2046
2047         for(sce1= bmain->scene.first; sce1; sce1=sce1->id.next) {
2048                 if(sce1!=sce) {
2049                         if(sce1->nodetree) {
2050                                 for(node= sce1->nodetree->nodes.first; node; node= node->next) {
2051                                         if(node->type==CMP_NODE_R_LAYERS) {
2052                                                 Scene *nodesce= (Scene *)node->id;
2053                                                 
2054                                                 if (nodesce==sce) node->id = NULL;
2055                                         }
2056                                 }
2057                         }
2058                 }
2059         }
2060 }
2061