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