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