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