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