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