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