bc9aa23ee157e10378d2dfec062f9b41f08164e7
[blender.git] / source / blender / blenkernel / intern / node.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <Python.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "DNA_ID.h"
35 #include "DNA_image_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_text_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BKE_blender.h"
42 #include "BKE_colortools.h"
43 #include "BKE_global.h"
44 #include "BKE_image.h"
45 #include "BKE_library.h"
46 #include "BKE_main.h"
47 #include "BKE_node.h"
48 #include "BKE_texture.h"
49 #include "BKE_text.h"
50 #include "BKE_utildefines.h"
51
52 #include "BLI_arithb.h"
53 #include "BLI_blenlib.h"
54 #include "BLI_rand.h"
55 #include "BLI_threads.h"
56
57 #include "PIL_time.h"
58
59 #include "MEM_guardedalloc.h"
60 #include "IMB_imbuf.h"
61
62 #include "RE_pipeline.h"
63 #include "RE_shader_ext.h"              /* <- TexResult */
64 #include "RE_render_ext.h"              /* <- ibuf_sample() */
65
66 #include "CMP_node.h"
67 #include "intern/CMP_util.h"    /* stupid include path... */
68
69 #include "SHD_node.h"
70
71 /* not very important, but the stack solver likes to know a maximum */
72 #define MAX_SOCKET      64
73
74 static ListBase empty_list = {NULL, NULL};
75 ListBase node_all_composit = {NULL, NULL};
76 ListBase node_all_shaders = {NULL, NULL};
77
78 /* ************** Type stuff **********  */
79
80 static bNodeType *node_get_type(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
81 {
82         if(type==NODE_GROUP) {
83                 if(ngroup && GS(ngroup->id.name)==ID_NT) {
84                         return ngroup->owntype;
85                 }
86                 return NULL;
87         }
88         else {
89                 bNodeType *ntype = ntree->alltypes.first;
90                 for(; ntype; ntype= ntype->next)
91                         if(ntype->type==type && id==ntype->id )
92                                 return ntype;
93                 
94                 return NULL;
95         }
96 }
97
98 void ntreeInitTypes(bNodeTree *ntree)
99 {
100         bNode *node, *next;
101         
102         if(ntree->type==NTREE_SHADER)
103                 ntree->alltypes= node_all_shaders;
104         else if(ntree->type==NTREE_COMPOSIT)
105                 ntree->alltypes= node_all_composit;
106         else {
107                 ntree->alltypes= empty_list;
108                 printf("Error: no type definitions for nodes\n");
109         }
110         
111         for(node= ntree->nodes.first; node; node= next) {
112                 next= node->next;
113                 if(node->type==NODE_DYNAMIC) {
114                         bNodeType *stype= NULL;
115                         if(node->id==NULL) { /* empty script node */
116                                 stype= node_get_type(ntree, node->type, NULL, NULL);
117                         } else { /* not an empty script node */
118                                 stype= node_get_type(ntree, node->type, NULL, node->id);
119                                 if(!stype) {
120                                         stype= node_get_type(ntree, node->type, NULL, NULL);
121                                         /* needed info if the pynode script fails now: */
122                                         if (node->id) node->storage= ntree;
123                                 } else {
124                                         node->custom1= 0;
125                                         node->custom1= BSET(node->custom1,NODE_DYNAMIC_ADDEXIST);
126                                 }
127                         }
128                         node->typeinfo= stype;
129                         node->typeinfo->initfunc(node);
130                 } else {
131                         node->typeinfo= node_get_type(ntree, node->type, (bNodeTree *)node->id, NULL);
132                 }
133
134                 if(node->typeinfo==NULL) {
135                         printf("Error: Node type %s doesn't exist anymore, removed\n", node->name);
136                         nodeFreeNode(ntree, node);
137                 }
138         }
139                         
140         ntree->init |= NTREE_TYPE_INIT;
141 }
142
143 /* updates node with (modified) bNodeType.. this should be done for all trees */
144 void ntreeUpdateType(bNodeTree *ntree, bNodeType *ntype)
145 {
146         bNode *node;
147
148         for(node= ntree->nodes.first; node; node= node->next) {
149                 if(node->typeinfo== ntype) {
150                         nodeUpdateType(ntree, node, ntype);
151                 }
152         }
153 }
154
155 /* only used internal... we depend on type definitions! */
156 static bNodeSocket *node_add_socket_type(ListBase *lb, bNodeSocketType *stype)
157 {
158         bNodeSocket *sock= MEM_callocN(sizeof(bNodeSocket), "sock");
159         
160         BLI_strncpy(sock->name, stype->name, NODE_MAXSTR);
161         if(stype->limit==0) sock->limit= 0xFFF;
162         else sock->limit= stype->limit;
163         sock->type= stype->type;
164         
165         sock->to_index= stype->own_index;
166         sock->tosock= stype->internsock;
167         
168         sock->ns.vec[0]= stype->val1;
169         sock->ns.vec[1]= stype->val2;
170         sock->ns.vec[2]= stype->val3;
171         sock->ns.vec[3]= stype->val4;
172         sock->ns.min= stype->min;
173         sock->ns.max= stype->max;
174         
175         if(lb)
176                 BLI_addtail(lb, sock);
177
178         return sock;
179 }
180
181 static void node_rem_socket(bNodeTree *ntree, ListBase *lb, bNodeSocket *sock)
182 {
183         bNodeLink *link, *next;
184         
185         for(link= ntree->links.first; link; link= next) {
186                 next= link->next;
187                 if(link->fromsock==sock || link->tosock==sock) {
188                         nodeRemLink(ntree, link);
189                 }
190         }
191         
192         BLI_remlink(lb, sock);
193         MEM_freeN(sock);
194 }
195
196 static bNodeSocket *verify_socket(ListBase *lb, bNodeSocketType *stype)
197 {
198         bNodeSocket *sock;
199         
200         for(sock= lb->first; sock; sock= sock->next) {
201                 /* both indices are zero for non-groups, otherwise it's a unique index */
202                 if(sock->to_index==stype->own_index)
203                         if(strncmp(sock->name, stype->name, NODE_MAXSTR)==0)
204                                 break;
205         }
206         if(sock) {
207                 sock->type= stype->type;                /* in future, read this from tydefs! */
208                 if(stype->limit==0) sock->limit= 0xFFF;
209                 else sock->limit= stype->limit;
210                 sock->ns.min= stype->min;
211                 sock->ns.max= stype->max;
212                 sock->tosock= stype->internsock;
213                 
214                 BLI_remlink(lb, sock);
215                 
216                 return sock;
217         }
218         else {
219                 return node_add_socket_type(NULL, stype);
220         }
221 }
222
223 static void verify_socket_list(bNodeTree *ntree, ListBase *lb, bNodeSocketType *stype_first)
224 {
225         bNodeSocketType *stype;
226         
227         /* no inputs anymore? */
228         if(stype_first==NULL) {
229                 while(lb->first)
230                         node_rem_socket(ntree, lb, lb->first);
231         }
232         else {
233                 /* step by step compare */
234                 stype= stype_first;
235                 while(stype->type != -1) {
236                         stype->sock= verify_socket(lb, stype);
237                         stype++;
238                 }
239                 /* leftovers are removed */
240                 while(lb->first)
241                         node_rem_socket(ntree, lb, lb->first);
242                 /* and we put back the verified sockets */
243                 stype= stype_first;
244                 while(stype->type != -1) {
245                         BLI_addtail(lb, stype->sock);
246                         stype++;
247                 }
248         }
249 }
250
251 void nodeVerifyType(bNodeTree *ntree, bNode *node)
252 {
253         bNodeType *ntype= node->typeinfo;
254         
255         if(ntype) {
256                 /* might add some other verify stuff here */
257                 
258                 verify_socket_list(ntree, &node->inputs, ntype->inputs);
259                 verify_socket_list(ntree, &node->outputs, ntype->outputs);
260         }
261 }
262
263 void ntreeVerifyTypes(bNodeTree *ntree)
264 {
265         bNode *node;
266         
267         /* if((ntree->init & NTREE_TYPE_INIT)==0) */
268         ntreeInitTypes(ntree);
269
270         /* check inputs and outputs, and remove or insert them */
271         for(node= ntree->nodes.first; node; node= node->next)
272                 nodeVerifyType(ntree, node);
273         
274 }
275
276 /* ************** Group stuff ********** */
277
278 bNodeType node_group_typeinfo= {
279         /* next,prev   */       NULL, NULL,
280         /* type code   */       NODE_GROUP,
281         /* name        */       "Group",
282         /* width+range */       120, 60, 200,
283         /* class+opts  */       NODE_CLASS_GROUP, NODE_OPTIONS,
284         /* input sock  */       NULL,
285         /* output sock */       NULL,
286         /* storage     */       "",
287         /* execfunc    */       NULL,
288         /* butfunc     */       NULL,
289         /* initfunc    */       NULL,
290         /* freestoragefunc    */        NULL,
291         /* copystoragefunc    */        NULL,
292         /* id          */       NULL
293 };
294
295 /* tag internal sockets */
296 static void group_tag_internal_sockets(bNodeTree *ngroup)
297 {
298         bNode *node;
299         bNodeSocket *sock;
300         bNodeLink *link;
301         
302         /* clear intern tag, but check already for hidden sockets */
303         for(node= ngroup->nodes.first; node; node= node->next) {
304                 for(sock= node->inputs.first; sock; sock= sock->next)
305                         sock->intern= sock->flag & SOCK_HIDDEN;
306                 for(sock= node->outputs.first; sock; sock= sock->next)
307                         sock->intern= sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL);
308         }
309         /* set tag */
310         for(link= ngroup->links.first; link; link= link->next) {
311                 link->fromsock->intern= 1;
312                 link->tosock->intern= 1;
313         }
314         
315         /* remove link pointer to external links (only happens on create group) */
316         for(node= ngroup->nodes.first; node; node= node->next) {
317                 for(sock= node->inputs.first; sock; sock= sock->next)
318                         if(sock->intern==0)
319                                 sock->link= NULL;
320         }
321
322         /* set all intern sockets to own_index zero, makes sure that later use won't mixup */
323         for(node= ngroup->nodes.first; node; node= node->next) {
324                 for(sock= node->inputs.first; sock; sock= sock->next)
325                         if(sock->intern)
326                                 sock->own_index= 0;
327                 for(sock= node->outputs.first; sock; sock= sock->next)
328                         if(sock->intern)
329                                 sock->own_index= 0;
330         }
331 }
332
333 /* after editing group, new sockets are zero */
334 /* this routine ensures unique identifiers for zero sockets that are exposed */
335 static void group_verify_own_indices(bNodeTree *ngroup)
336 {
337         bNode *node;
338         bNodeSocket *sock;
339         
340         for(node= ngroup->nodes.first; node; node= node->next) {
341                 for(sock= node->inputs.first; sock; sock= sock->next)
342                         if(sock->own_index==0 && sock->intern==0)
343                                 sock->own_index= ++(ngroup->cur_index);
344                 for(sock= node->outputs.first; sock; sock= sock->next)
345                         if(sock->own_index==0 && sock->intern==0)
346                                 sock->own_index= ++(ngroup->cur_index);
347         }
348         //printf("internal index %d\n", ngroup->cur_index);
349 }
350
351
352 /* nodetrees can be used as groups, so we need typeinfo structs generated */
353 void ntreeMakeOwnType(bNodeTree *ngroup)
354 {
355         bNode *node;
356         bNodeSocket *sock;
357         int totin= 0, totout=0, a;
358
359         /* tags socket when internal linked */
360         group_tag_internal_sockets(ngroup);
361         
362         /* ensure all sockets have own unique id */
363         group_verify_own_indices(ngroup);
364         
365         /* counting stats */
366         for(node= ngroup->nodes.first; node; node= node->next) {
367                 if(node->type==NODE_GROUP)
368                         break;
369                 for(sock= node->inputs.first; sock; sock= sock->next)
370                         if(sock->intern==0) 
371                                 totin++;
372                 for(sock= node->outputs.first; sock; sock= sock->next)
373                         if(sock->intern==0) 
374                                 totout++;
375         }
376         /* debug: nodetrees in nodetrees not handled yet */
377         if(node) {
378                 printf("group in group, not supported yet\n");
379                 return;
380         }
381         
382         /* free own type struct */
383         if(ngroup->owntype) {
384                 if(ngroup->owntype->inputs)
385                         MEM_freeN(ngroup->owntype->inputs);
386                 if(ngroup->owntype->outputs)
387                         MEM_freeN(ngroup->owntype->outputs);
388                 MEM_freeN(ngroup->owntype);
389         }
390         
391         /* make own type struct */
392         ngroup->owntype= MEM_callocN(sizeof(bNodeType), "group type");
393         *ngroup->owntype= node_group_typeinfo; /* copy data, for init */
394         
395         /* input type arrays */
396         if(totin) {
397                 bNodeSocketType *stype;
398                 bNodeSocketType *inputs= MEM_callocN(sizeof(bNodeSocketType)*(totin+1), "bNodeSocketType");
399                 a= 0;
400                 
401                 for(node= ngroup->nodes.first; node; node= node->next) {
402                         /* nodes are presumed fully verified, stype and socket list are in sync */
403                         stype= node->typeinfo->inputs;
404                         for(sock= node->inputs.first; sock; sock= sock->next, stype++) {
405                                 if(sock->intern==0) {
406                                         /* debug only print */
407                                         if(stype==NULL || stype->type==-1) printf("group verification error %s\n", ngroup->id.name);
408                                         
409                                         inputs[a]= *stype;
410                                         inputs[a].own_index= sock->own_index;
411                                         inputs[a].internsock= sock;     
412                                         a++;
413                                 }
414                         }
415                 }
416                 inputs[a].type= -1;     /* terminator code */
417                 ngroup->owntype->inputs= inputs;
418         }       
419         
420         /* output type arrays */
421         if(totout) {
422                 bNodeSocketType *stype;
423                 bNodeSocketType *outputs= MEM_callocN(sizeof(bNodeSocketType)*(totout+1), "bNodeSocketType");
424                 a= 0;
425                 
426                 for(node= ngroup->nodes.first; node; node= node->next) {
427                         /* nodes are presumed fully verified, stype and socket list are in sync */
428                         stype= node->typeinfo->outputs;
429                         for(sock= node->outputs.first; sock; sock= sock->next, stype++) {
430                                 if(sock->intern==0) {
431                                         /* debug only print */
432                                         if(stype==NULL || stype->type==-1) printf("group verification error %s\n", ngroup->id.name);
433                                         
434                                         outputs[a]= *stype;
435                                         outputs[a].own_index= sock->own_index;
436                                         outputs[a].internsock= sock;    
437                                         a++;
438                                 }
439                         }
440                 }
441                 outputs[a].type= -1;    /* terminator code */
442                 ngroup->owntype->outputs= outputs;
443         }
444         
445         /* voila, the nodetree has the full definition for generating group-node instances! */
446 }
447
448
449 static bNodeSocket *groupnode_find_tosock(bNode *gnode, int index)
450 {
451         bNodeSocket *sock;
452         
453         for(sock= gnode->inputs.first; sock; sock= sock->next)
454                 if(sock->to_index==index)
455                         return sock;
456         return NULL;
457 }
458
459 static bNodeSocket *groupnode_find_fromsock(bNode *gnode, int index)
460 {
461         bNodeSocket *sock;
462         
463         for(sock= gnode->outputs.first; sock; sock= sock->next)
464                 if(sock->to_index==index)
465                         return sock;
466         return NULL;
467 }
468
469 bNode *nodeMakeGroupFromSelected(bNodeTree *ntree)
470 {
471         bNodeLink *link, *linkn;
472         bNode *node, *gnode, *nextn;
473         bNodeSocket *sock;
474         bNodeTree *ngroup;
475         float min[2], max[2];
476         int totnode=0;
477         
478         INIT_MINMAX2(min, max);
479         
480         /* is there something to group? also do some clearing */
481         for(node= ntree->nodes.first; node; node= node->next) {
482                 if(node->flag & NODE_SELECT) {
483                         /* no groups in groups */
484                         if(node->type==NODE_GROUP)
485                                 return NULL;
486                         DO_MINMAX2( (&node->locx), min, max);
487                         totnode++;
488                 }
489                 node->done= 0;
490         }
491         if(totnode==0) return NULL;
492         
493         /* check if all connections are OK, no unselected node has both
494                 inputs and outputs to a selection */
495         for(link= ntree->links.first; link; link= link->next) {
496                 if(link->fromnode->flag & NODE_SELECT)
497                         link->tonode->done |= 1;
498                 if(link->tonode->flag & NODE_SELECT)
499                         link->fromnode->done |= 2;
500         }       
501         
502         for(node= ntree->nodes.first; node; node= node->next) {
503                 if((node->flag & NODE_SELECT)==0)
504                         if(node->done==3)
505                                 break;
506         }
507         if(node) 
508                 return NULL;
509         
510         /* OK! new nodetree */
511         ngroup= alloc_libblock(&G.main->nodetree, ID_NT, "NodeGroup");
512         ngroup->type= ntree->type;
513         ngroup->alltypes= ntree->alltypes;
514         
515         /* move nodes over */
516         for(node= ntree->nodes.first; node; node= nextn) {
517                 nextn= node->next;
518                 if(node->flag & NODE_SELECT) {
519                         BLI_remlink(&ntree->nodes, node);
520                         BLI_addtail(&ngroup->nodes, node);
521                         node->locx-= 0.5f*(min[0]+max[0]);
522                         node->locy-= 0.5f*(min[1]+max[1]);
523
524                         /* set selin and selout of the nodetree */
525                         for(sock= node->inputs.first; sock; sock= sock->next) {
526                                 if(sock->flag & SOCK_SEL) {
527                                         ngroup->selin= sock;
528                                         break;
529                                 }
530                         }
531                         for(sock= node->outputs.first; sock; sock= sock->next) {
532                                 if(sock->flag & SOCK_SEL) {
533                                         ngroup->selout= sock;
534                                         break;
535                                 }
536                         }
537                 }
538         }
539
540         /* move links over */
541         for(link= ntree->links.first; link; link= linkn) {
542                 linkn= link->next;
543                 if(link->fromnode->flag & link->tonode->flag & NODE_SELECT) {
544                         BLI_remlink(&ntree->links, link);
545                         BLI_addtail(&ngroup->links, link);
546                 }
547         }
548         
549         /* now we can make own group typeinfo */
550         ntreeMakeOwnType(ngroup);
551         
552         /* make group node */
553         gnode= nodeAddNodeType(ntree, NODE_GROUP, ngroup, NULL);
554         gnode->locx= 0.5f*(min[0]+max[0]);
555         gnode->locy= 0.5f*(min[1]+max[1]);
556         
557         /* relink external sockets */
558         for(link= ntree->links.first; link; link= linkn) {
559                 linkn= link->next;
560                 
561                 if(link->tonode->flag & NODE_SELECT) {
562                         link->tonode= gnode;
563                         sock= groupnode_find_tosock(gnode, link->tosock->own_index);
564                         if(sock==NULL) {
565                                 nodeRemLink(ntree, link);       
566                                 printf("Removed link, cannot mix internal and external sockets in group\n");
567                         }
568                         else link->tosock= sock;
569                 }
570                 else if(link->fromnode->flag & NODE_SELECT) {
571                         link->fromnode= gnode;
572                         sock= groupnode_find_fromsock(gnode, link->fromsock->own_index);
573                         if(sock==NULL) {
574                                 nodeRemLink(ntree, link);       
575                                 printf("Removed link, cannot mix internal and external sockets in group\n");
576                         }
577                         else link->fromsock= sock;
578                 }
579         }
580         
581         /* initialize variables of unused input sockets */
582         for(node= ngroup->nodes.first; node; node= node->next) {
583                 for(sock= node->inputs.first; sock; sock= sock->next) {
584                         if(sock->intern==0) {
585                                 bNodeSocket *nsock= groupnode_find_tosock(gnode, sock->own_index);
586                                 if(nsock) {
587                                         QUATCOPY(nsock->ns.vec, sock->ns.vec);
588                                 }
589                         }
590                 }
591         }
592         return gnode;
593 }
594
595 /* note: ungroup: group_indices zero! */
596
597 /* here's a nasty little one, need to check users... */
598 /* should become callbackable... */
599 void nodeVerifyGroup(bNodeTree *ngroup)
600 {
601         
602         /* group changed, so we rebuild the type definition */
603         ntreeMakeOwnType(ngroup);
604         
605         if(ngroup->type==NTREE_SHADER) {
606                 Material *ma;
607                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
608                         if(ma->nodetree) {
609                                 bNode *node;
610                                 
611                                 /* find if group is in tree */
612                                 for(node= ma->nodetree->nodes.first; node; node= node->next)
613                                         if(node->id == (ID *)ngroup)
614                                                 break;
615                                 
616                                 if(node) {
617                                         /* set all type pointers OK */
618                                         ntreeInitTypes(ma->nodetree);
619                                         
620                                         for(node= ma->nodetree->nodes.first; node; node= node->next)
621                                                 if(node->id == (ID *)ngroup)
622                                                         nodeVerifyType(ma->nodetree, node);
623                                 }
624                         }
625                 }
626         }
627         else if(ngroup->type==NTREE_COMPOSIT) {
628                 Scene *sce;
629                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
630                         if(sce->nodetree) {
631                                 bNode *node;
632                                 
633                                 /* find if group is in tree */
634                                 for(node= sce->nodetree->nodes.first; node; node= node->next)
635                                         if(node->id == (ID *)ngroup)
636                                                 break;
637                                 
638                                 if(node) {
639                                         /* set all type pointers OK */
640                                         ntreeInitTypes(sce->nodetree);
641                                         
642                                         for(node= sce->nodetree->nodes.first; node; node= node->next)
643                                                 if(node->id == (ID *)ngroup)
644                                                         nodeVerifyType(sce->nodetree, node);
645                                 }
646                         }
647                 }
648         }
649 }
650
651 /* also to check all users of groups. Now only used in editor for hide/unhide */
652 /* should become callbackable? */
653 void nodeGroupSocketUseFlags(bNodeTree *ngroup)
654 {
655         bNode *node;
656         bNodeSocket *sock;
657
658         /* clear flags */
659         for(node= ngroup->nodes.first; node; node= node->next) {
660                 for(sock= node->inputs.first; sock; sock= sock->next)
661                         sock->flag &= ~SOCK_IN_USE;
662                 for(sock= node->outputs.first; sock; sock= sock->next)
663                         sock->flag &= ~SOCK_IN_USE;
664         }
665         
666         /* tag all thats in use */
667         if(ngroup->type==NTREE_SHADER) {
668                 Material *ma;
669                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
670                         if(ma->nodetree) {
671                                 for(node= ma->nodetree->nodes.first; node; node= node->next) {
672                                         if(node->id==(ID *)ngroup) {
673                                                 for(sock= node->inputs.first; sock; sock= sock->next)
674                                                         if(sock->link)
675                                                                 if(sock->tosock) 
676                                                                         sock->tosock->flag |= SOCK_IN_USE;
677                                                 for(sock= node->outputs.first; sock; sock= sock->next)
678                                                         if(nodeCountSocketLinks(ma->nodetree, sock))
679                                                                 if(sock->tosock) 
680                                                                         sock->tosock->flag |= SOCK_IN_USE;
681                                         }
682                                 }
683                         }
684                 }
685         }
686         else if(ngroup->type==NTREE_COMPOSIT) {
687                 Scene *sce;
688                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
689                         if(sce->nodetree) {
690                                 for(node= sce->nodetree->nodes.first; node; node= node->next) {
691                                         if(node->id==(ID *)ngroup) {
692                                                 for(sock= node->inputs.first; sock; sock= sock->next)
693                                                         if(sock->link)
694                                                                 if(sock->tosock) 
695                                                                         sock->tosock->flag |= SOCK_IN_USE;
696                                                 for(sock= node->outputs.first; sock; sock= sock->next)
697                                                         if(nodeCountSocketLinks(sce->nodetree, sock))
698                                                                 if(sock->tosock) 
699                                                                         sock->tosock->flag |= SOCK_IN_USE;
700                                         }
701                                 }
702                         }
703                 }
704         }
705 }
706
707 /* finds a node based on given socket */
708 int nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **nodep, int *sockindex)
709 {
710         bNode *node;
711         bNodeSocket *tsock;
712         int index= 0;
713         
714         for(node= ntree->nodes.first; node; node= node->next) {
715                 for(index=0, tsock= node->inputs.first; tsock; tsock= tsock->next, index++)
716                         if(tsock==sock)
717                                 break;
718                 if(tsock)
719                         break;
720                 for(index=0, tsock= node->outputs.first; tsock; tsock= tsock->next, index++)
721                         if(tsock==sock)
722                                 break;
723                 if(tsock)
724                         break;
725         }
726
727         if(node) {
728                 *nodep= node;
729                 if(sockindex) *sockindex= index;
730                 return 1;
731         }
732         
733         *nodep= NULL;
734         return 0;
735 }
736
737 /* returns 1 if its OK */
738 int nodeGroupUnGroup(bNodeTree *ntree, bNode *gnode)
739 {
740         bNodeLink *link, *linkn;
741         bNode *node, *nextn;
742         bNodeTree *ngroup, *wgroup;
743         int index;
744         
745         ngroup= (bNodeTree *)gnode->id;
746         if(ngroup==NULL) return 0;
747         
748         /* clear new pointers, set in copytree */
749         for(node= ntree->nodes.first; node; node= node->next)
750                 node->new_node= NULL;
751
752         wgroup= ntreeCopyTree(ngroup, 0);
753         
754         /* add the nodes into the ntree */
755         for(node= wgroup->nodes.first; node; node= nextn) {
756                 nextn= node->next;
757                 BLI_remlink(&wgroup->nodes, node);
758                 BLI_addtail(&ntree->nodes, node);
759                 node->locx+= gnode->locx;
760                 node->locy+= gnode->locy;
761                 node->flag |= NODE_SELECT;
762         }
763         /* and the internal links */
764         for(link= wgroup->links.first; link; link= linkn) {
765                 linkn= link->next;
766                 BLI_remlink(&wgroup->links, link);
767                 BLI_addtail(&ntree->links, link);
768         }
769
770         /* restore links to and from the gnode */
771         for(link= ntree->links.first; link; link= link->next) {
772                 if(link->tonode==gnode) {
773                         /* link->tosock->tosock is on the node we look for */
774                         nodeFindNode(ngroup, link->tosock->tosock, &nextn, &index);
775                         if(nextn==NULL) printf("wrong stuff!\n");
776                         else if(nextn->new_node==NULL) printf("wrong stuff too!\n");
777                         else {
778                                 link->tonode= nextn->new_node;
779                                 link->tosock= BLI_findlink(&link->tonode->inputs, index);
780                         }
781                 }
782                 else if(link->fromnode==gnode) {
783                         /* link->fromsock->tosock is on the node we look for */
784                         nodeFindNode(ngroup, link->fromsock->tosock, &nextn, &index);
785                         if(nextn==NULL) printf("1 wrong stuff!\n");
786                         else if(nextn->new_node==NULL) printf("1 wrong stuff too!\n");
787                         else {
788                                 link->fromnode= nextn->new_node;
789                                 link->fromsock= BLI_findlink(&link->fromnode->outputs, index);
790                         }
791                 }
792         }
793         
794         /* remove the gnode & work tree */
795         free_libblock(&G.main->nodetree, wgroup);
796         
797         nodeFreeNode(ntree, gnode);
798         
799         /* solve order goes fine, but the level tags not... doing it twice works for now. solve this once */
800         ntreeSolveOrder(ntree);
801         ntreeSolveOrder(ntree);
802
803         return 1;
804 }
805
806 void nodeCopyGroup(bNode *gnode)
807 {
808         bNodeSocket *sock;
809
810         gnode->id->us--;
811         gnode->id= (ID *)ntreeCopyTree((bNodeTree *)gnode->id, 0);
812
813         /* new_sock was set in nodeCopyNode */
814         for(sock=gnode->inputs.first; sock; sock=sock->next)
815                 if(sock->tosock)
816                         sock->tosock= sock->tosock->new_sock;
817
818         for(sock=gnode->outputs.first; sock; sock=sock->next)
819                 if(sock->tosock)
820                         sock->tosock= sock->tosock->new_sock;
821 }
822
823 /* ************** Add stuff ********** */
824 void nodeAddSockets(bNode *node, bNodeType *ntype)
825 {
826         bNodeSocketType *stype;
827
828         if(ntype->inputs) {
829                 stype= ntype->inputs;
830                 while(stype->type != -1) {
831                         node_add_socket_type(&node->inputs, stype);
832                         stype++;
833                 }
834         }
835         if(ntype->outputs) {
836                 stype= ntype->outputs;
837                 while(stype->type != -1) {
838                         node_add_socket_type(&node->outputs, stype);
839                         stype++;
840                 }
841         }
842 }
843
844
845 bNode *nodeAddNodeType(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
846 {
847         bNode *node= NULL;
848         bNodeType *ntype= NULL;
849
850         if(type>=NODE_DYNAMIC_MENU) {
851                 int a=0, idx= type-NODE_DYNAMIC_MENU;
852                 ntype= ntree->alltypes.first;
853                 while(ntype) {
854                         if(ntype->type==NODE_DYNAMIC) {
855                                 if(a==idx)
856                                         break;
857                                 a++;
858                         }
859                         ntype= ntype->next;
860                 }
861         } else
862                 ntype= node_get_type(ntree, type, ngroup, id);
863
864         node= MEM_callocN(sizeof(bNode), "new node");
865         BLI_addtail(&ntree->nodes, node);
866         node->typeinfo= ntype;
867         if(type>=NODE_DYNAMIC_MENU)
868                 node->custom2= type; /* for node_dynamic_init */
869
870         if(ngroup)
871                 BLI_strncpy(node->name, ngroup->id.name+2, NODE_MAXSTR);
872         else if(type>NODE_DYNAMIC_MENU) {
873                 BLI_strncpy(node->name, ntype->id->name+2, NODE_MAXSTR);
874         }
875         else
876                 BLI_strncpy(node->name, ntype->name, NODE_MAXSTR);
877         node->type= ntype->type;
878         node->flag= NODE_SELECT|ntype->flag;
879         node->width= ntype->width;
880         node->miniwidth= 42.0f;         /* small value only, allows print of first chars */
881
882         if(type==NODE_GROUP)
883                 node->id= (ID *)ngroup;
884
885         /* need init handler later? */
886         /* got it-bob*/
887         if(ntype->initfunc!=NULL)
888                 ntype->initfunc(node);
889
890         nodeAddSockets(node, ntype);
891
892         return node;
893 }
894
895 void nodeMakeDynamicType(bNode *node)
896 {
897         /* find SH_DYNAMIC_NODE ntype */
898         bNodeType *ntype= node_all_shaders.first;
899         while(ntype) {
900                 if(ntype->type==NODE_DYNAMIC && ntype->id==NULL)
901                         break;
902                 ntype= ntype->next;
903         }
904
905         /* make own type struct to fill */
906         if(ntype) {
907                 /*node->typeinfo= MEM_dupallocN(ntype);*/
908                 bNodeType *newtype= MEM_callocN(sizeof(bNodeType), "dynamic bNodeType");
909                 *newtype= *ntype;
910                 newtype->name= BLI_strdup(ntype->name);
911                 node->typeinfo= newtype;
912         }
913 }
914
915 void nodeUpdateType(bNodeTree *ntree, bNode* node, bNodeType *ntype)
916 {
917         verify_socket_list(ntree, &node->inputs, ntype->inputs);
918         verify_socket_list(ntree, &node->outputs, ntype->outputs);
919 }
920
921 /* keep socket listorder identical, for copying links */
922 /* ntree is the target tree */
923 bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
924 {
925         bNode *nnode= MEM_callocN(sizeof(bNode), "dupli node");
926         bNodeSocket *sock, *oldsock;
927
928         *nnode= *node;
929         BLI_addtail(&ntree->nodes, nnode);
930         
931         duplicatelist(&nnode->inputs, &node->inputs);
932         oldsock= node->inputs.first;
933         for(sock= nnode->inputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
934                 oldsock->new_sock= sock;
935                 if(internal)
936                         sock->own_index= 0;
937         }
938         
939         duplicatelist(&nnode->outputs, &node->outputs);
940         oldsock= node->outputs.first;
941         for(sock= nnode->outputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
942                 if(internal)
943                         sock->own_index= 0;
944                 sock->stack_index= 0;
945                 sock->ns.data= NULL;
946                 oldsock->new_sock= sock;
947         }
948         
949         if(nnode->id)
950                 nnode->id->us++;
951         
952         if(node->typeinfo->copystoragefunc)
953                 node->typeinfo->copystoragefunc(node, nnode);
954         
955         node->new_node= nnode;
956         nnode->new_node= NULL;
957         nnode->preview= NULL;
958
959         return nnode;
960 }
961
962 bNodeLink *nodeAddLink(bNodeTree *ntree, bNode *fromnode, bNodeSocket *fromsock, bNode *tonode, bNodeSocket *tosock)
963 {
964         bNodeLink *link= MEM_callocN(sizeof(bNodeLink), "link");
965         
966         BLI_addtail(&ntree->links, link);
967         link->fromnode= fromnode;
968         link->fromsock= fromsock;
969         link->tonode= tonode;
970         link->tosock= tosock;
971         
972         return link;
973 }
974
975 void nodeRemLink(bNodeTree *ntree, bNodeLink *link)
976 {
977         BLI_remlink(&ntree->links, link);
978         if(link->tosock)
979                 link->tosock->link= NULL;
980         MEM_freeN(link);
981 }
982
983
984 bNodeTree *ntreeAddTree(int type)
985 {
986         bNodeTree *ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
987         ntree->type= type;
988         ntree->alltypes.first = NULL;
989         ntree->alltypes.last = NULL;
990         
991         ntreeInitTypes(ntree);
992         return ntree;
993 }
994
995 bNodeTree *ntreeCopyTree(bNodeTree *ntree, int internal_select)
996 {
997         bNodeTree *newtree;
998         bNode *node, *nnode, *last;
999         bNodeLink *link, *nlink;
1000         bNodeSocket *sock;
1001         int a;
1002         
1003         if(ntree==NULL) return NULL;
1004         
1005         if(internal_select==0) {
1006                 /* is ntree part of library? */
1007                 for(newtree=G.main->nodetree.first; newtree; newtree= newtree->id.next)
1008                         if(newtree==ntree) break;
1009                 if(newtree)
1010                         newtree= copy_libblock(ntree);
1011                 else
1012                         newtree= MEM_dupallocN(ntree);
1013                 newtree->nodes.first= newtree->nodes.last= NULL;
1014                 newtree->links.first= newtree->links.last= NULL;
1015         }
1016         else
1017                 newtree= ntree;
1018         
1019         last= ntree->nodes.last;
1020         for(node= ntree->nodes.first; node; node= node->next) {
1021                 
1022                 node->new_node= NULL;
1023                 if(internal_select==0 || (node->flag & NODE_SELECT)) {
1024                         nnode= nodeCopyNode(newtree, node, internal_select);    /* sets node->new */
1025                         if(internal_select) {
1026                                 node->flag &= ~NODE_SELECT;
1027                                 nnode->flag |= NODE_SELECT;
1028                         }
1029                         node->flag &= ~NODE_ACTIVE;
1030
1031                         /* deselect original sockets */
1032                         for(sock= node->inputs.first; sock; sock= sock->next) {
1033                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1034                         }
1035                         for(sock= node->outputs.first; sock; sock= sock->next) {
1036                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1037                         }
1038                         
1039                         /* set tree selin and selout to new sockets */
1040                         for(sock= nnode->inputs.first; sock; sock= sock->next) {
1041                                 if(sock->flag & SOCK_SEL) {
1042                                         ntree->selin= sock;
1043                                         break;
1044                                 }
1045                         }
1046                         for(sock= nnode->outputs.first; sock; sock= sock->next) {
1047                                 if(sock->flag & SOCK_SEL) {
1048                                         ntree->selout= sock;
1049                                         break;
1050                                 }
1051                         }
1052                 }
1053                 if(node==last) break;
1054         }
1055         
1056         /* check for copying links */
1057         for(link= ntree->links.first; link; link= link->next) {
1058                 if(link->fromnode->new_node && link->tonode->new_node) {
1059                         nlink= nodeAddLink(newtree, link->fromnode->new_node, NULL, link->tonode->new_node, NULL);
1060                         /* sockets were copied in order */
1061                         for(a=0, sock= link->fromnode->outputs.first; sock; sock= sock->next, a++) {
1062                                 if(sock==link->fromsock)
1063                                         break;
1064                         }
1065                         nlink->fromsock= BLI_findlink(&link->fromnode->new_node->outputs, a);
1066                         
1067                         for(a=0, sock= link->tonode->inputs.first; sock; sock= sock->next, a++) {
1068                                 if(sock==link->tosock)
1069                                         break;
1070                         }
1071                         nlink->tosock= BLI_findlink(&link->tonode->new_node->inputs, a);
1072                 }
1073         }
1074         
1075         /* own type definition for group usage */
1076         if(internal_select==0) {
1077                 if(ntree->owntype) {
1078                         newtree->owntype= MEM_dupallocN(ntree->owntype);
1079                         if(ntree->owntype->inputs)
1080                                 newtree->owntype->inputs= MEM_dupallocN(ntree->owntype->inputs);
1081                         if(ntree->owntype->outputs)
1082                                 newtree->owntype->outputs= MEM_dupallocN(ntree->owntype->outputs);
1083                 }
1084         }
1085         /* weird this is required... there seem to be link pointers wrong still? */
1086         /* anyhoo, doing this solves crashes on copying entire tree (copy scene) and delete nodes */
1087         ntreeSolveOrder(newtree);
1088
1089         return newtree;
1090 }
1091
1092 /* ************** Free stuff ********** */
1093
1094 /* goes over entire tree */
1095 void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
1096 {
1097         bNodeLink *link, *next;
1098         bNodeSocket *sock;
1099         ListBase *lb;
1100         
1101         for(link= ntree->links.first; link; link= next) {
1102                 next= link->next;
1103                 
1104                 if(link->fromnode==node) {
1105                         lb= &node->outputs;
1106                         NodeTagChanged(ntree, link->tonode);
1107                 }
1108                 else if(link->tonode==node)
1109                         lb= &node->inputs;
1110                 else
1111                         lb= NULL;
1112
1113                 if(lb) {
1114                         for(sock= lb->first; sock; sock= sock->next) {
1115                                 if(link->fromsock==sock || link->tosock==sock)
1116                                         break;
1117                         }
1118                         if(sock) {
1119                                 nodeRemLink(ntree, link);
1120                         }
1121                 }
1122         }
1123 }
1124
1125 static void composit_free_node_cache(bNode *node)
1126 {
1127         bNodeSocket *sock;
1128         
1129         for(sock= node->outputs.first; sock; sock= sock->next) {
1130                 if(sock->ns.data) {
1131                         free_compbuf(sock->ns.data);
1132                         sock->ns.data= NULL;
1133                 }
1134         }
1135 }
1136
1137 void nodeFreeNode(bNodeTree *ntree, bNode *node)
1138 {
1139         nodeUnlinkNode(ntree, node);
1140         BLI_remlink(&ntree->nodes, node);
1141
1142         /* since it is called while free database, node->id is undefined */
1143         
1144         if(ntree->type==NTREE_COMPOSIT)
1145                 composit_free_node_cache(node);
1146         BLI_freelistN(&node->inputs);
1147         BLI_freelistN(&node->outputs);
1148         
1149         if(node->preview) {
1150                 if(node->preview->rect)
1151                         MEM_freeN(node->preview->rect);
1152                 MEM_freeN(node->preview);
1153         }
1154         if(node->typeinfo && node->typeinfo->freestoragefunc) {
1155                 node->typeinfo->freestoragefunc(node);
1156         }
1157
1158         MEM_freeN(node);
1159 }
1160
1161 /* do not free ntree itself here, free_libblock calls this function too */
1162 void ntreeFreeTree(bNodeTree *ntree)
1163 {
1164         bNode *node, *next;
1165         
1166         if(ntree==NULL) return;
1167         
1168         ntreeEndExecTree(ntree);        /* checks for if it is still initialized */
1169         
1170         BLI_freelistN(&ntree->links);   /* do first, then unlink_node goes fast */
1171         
1172         for(node= ntree->nodes.first; node; node= next) {
1173                 next= node->next;
1174                 nodeFreeNode(ntree, node);
1175         }
1176         
1177         if(ntree->owntype) {
1178                 if(ntree->owntype->inputs)
1179                         MEM_freeN(ntree->owntype->inputs);
1180                 if(ntree->owntype->outputs)
1181                         MEM_freeN(ntree->owntype->outputs);
1182                 MEM_freeN(ntree->owntype);
1183         }
1184 }
1185
1186 void ntreeFreeCache(bNodeTree *ntree)
1187 {
1188         bNode *node;
1189         
1190         if(ntree==NULL) return;
1191
1192         if(ntree->type==NTREE_COMPOSIT)
1193                 for(node= ntree->nodes.first; node; node= node->next)
1194                         composit_free_node_cache(node);
1195
1196 }
1197
1198 void ntreeMakeLocal(bNodeTree *ntree)
1199 {
1200         int local=0, lib=0;
1201         
1202         /* - only lib users: do nothing
1203             * - only local users: set flag
1204             * - mixed: make copy
1205             */
1206         
1207         if(ntree->id.lib==NULL) return;
1208         if(ntree->id.us==1) {
1209                 ntree->id.lib= 0;
1210                 ntree->id.flag= LIB_LOCAL;
1211                 new_id(0, (ID *)ntree, 0);
1212                 return;
1213         }
1214         
1215         /* now check users of groups... again typedepending, callback... */
1216         if(ntree->type==NTREE_SHADER) {
1217                 Material *ma;
1218                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
1219                         if(ma->nodetree) {
1220                                 bNode *node;
1221                                 
1222                                 /* find if group is in tree */
1223                                 for(node= ma->nodetree->nodes.first; node; node= node->next) {
1224                                         if(node->id == (ID *)ntree) {
1225                                                 if(ma->id.lib) lib= 1;
1226                                                 else local= 1;
1227                                         }
1228                                 }
1229                         }
1230                 }
1231         }
1232         else if(ntree->type==NTREE_COMPOSIT) {
1233                 Scene *sce;
1234                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
1235                         if(sce->nodetree) {
1236                                 bNode *node;
1237                                 
1238                                 /* find if group is in tree */
1239                                 for(node= sce->nodetree->nodes.first; node; node= node->next) {
1240                                         if(node->id == (ID *)ntree) {
1241                                                 if(sce->id.lib) lib= 1;
1242                                                 else local= 1;
1243                                         }
1244                                 }
1245                         }
1246                 }
1247         }
1248         
1249         /* if all users are local, we simply make tree local */
1250         if(local && lib==0) {
1251                 ntree->id.lib= NULL;
1252                 ntree->id.flag= LIB_LOCAL;
1253                 new_id(0, (ID *)ntree, 0);
1254         }
1255         else if(local && lib) {
1256                 /* this is the mixed case, we copy the tree and assign it to local users */
1257                 bNodeTree *newtree= ntreeCopyTree(ntree, 0);
1258                 
1259                 newtree->id.us= 0;
1260                 
1261                 if(ntree->type==NTREE_SHADER) {
1262                         Material *ma;
1263                         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
1264                                 if(ma->nodetree) {
1265                                         bNode *node;
1266                                         
1267                                         /* find if group is in tree */
1268                                         for(node= ma->nodetree->nodes.first; node; node= node->next) {
1269                                                 if(node->id == (ID *)ntree) {
1270                                                         if(ma->id.lib==NULL) {
1271                                                                 node->id= &newtree->id;
1272                                                                 newtree->id.us++;
1273                                                                 ntree->id.us--;
1274                                                         }
1275                                                 }
1276                                         }
1277                                 }
1278                         }
1279                 }
1280                 else if(ntree->type==NTREE_COMPOSIT) {
1281                         Scene *sce;
1282                         for(sce= G.main->scene.first; sce; sce= sce->id.next) {
1283                                 if(sce->nodetree) {
1284                                         bNode *node;
1285                                         
1286                                         /* find if group is in tree */
1287                                         for(node= sce->nodetree->nodes.first; node; node= node->next) {
1288                                                 if(node->id == (ID *)ntree) {
1289                                                         if(sce->id.lib==NULL) {
1290                                                                 node->id= &newtree->id;
1291                                                                 newtree->id.us++;
1292                                                                 ntree->id.us--;
1293                                                         }
1294                                                 }
1295                                         }
1296                                 }
1297                         }
1298                 }
1299         }
1300 }
1301
1302
1303 /* ************ find stuff *************** */
1304
1305 static int ntreeHasType(bNodeTree *ntree, int type)
1306 {
1307         bNode *node;
1308         
1309         if(ntree)
1310                 for(node= ntree->nodes.first; node; node= node->next)
1311                         if(node->type == type)
1312                                 return 1;
1313         return 0;
1314 }
1315
1316 bNodeLink *nodeFindLink(bNodeTree *ntree, bNodeSocket *from, bNodeSocket *to)
1317 {
1318         bNodeLink *link;
1319         
1320         for(link= ntree->links.first; link; link= link->next) {
1321                 if(link->fromsock==from && link->tosock==to)
1322                         return link;
1323                 if(link->fromsock==to && link->tosock==from)    /* hrms? */
1324                         return link;
1325         }
1326         return NULL;
1327 }
1328
1329 int nodeCountSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1330 {
1331         bNodeLink *link;
1332         int tot= 0;
1333         
1334         for(link= ntree->links.first; link; link= link->next) {
1335                 if(link->fromsock==sock || link->tosock==sock)
1336                         tot++;
1337         }
1338         return tot;
1339 }
1340
1341 bNode *nodeGetActive(bNodeTree *ntree)
1342 {
1343         bNode *node;
1344         
1345         if(ntree==NULL) return NULL;
1346         
1347         for(node= ntree->nodes.first; node; node= node->next)
1348                 if(node->flag & NODE_ACTIVE)
1349                         break;
1350         return node;
1351 }
1352
1353 /* two active flags, ID nodes have special flag for buttons display */
1354 bNode *nodeGetActiveID(bNodeTree *ntree, short idtype)
1355 {
1356         bNode *node;
1357         
1358         if(ntree==NULL) return NULL;
1359         
1360         for(node= ntree->nodes.first; node; node= node->next)
1361                 if(node->id && GS(node->id->name)==idtype)
1362                         if(node->flag & NODE_ACTIVE_ID)
1363                                 break;
1364         return node;
1365 }
1366
1367 /* two active flags, ID nodes have special flag for buttons display */
1368 void nodeClearActiveID(bNodeTree *ntree, short idtype)
1369 {
1370         bNode *node;
1371         
1372         if(ntree==NULL) return;
1373         
1374         for(node= ntree->nodes.first; node; node= node->next)
1375                 if(node->id && GS(node->id->name)==idtype)
1376                         node->flag &= ~NODE_ACTIVE_ID;
1377 }
1378
1379 /* two active flags, ID nodes have special flag for buttons display */
1380 void nodeSetActive(bNodeTree *ntree, bNode *node)
1381 {
1382         bNode *tnode;
1383         
1384         /* make sure only one node is active, and only one per ID type */
1385         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1386                 tnode->flag &= ~NODE_ACTIVE;
1387                 
1388                 if(node->id && tnode->id) {
1389                         if(GS(node->id->name) == GS(tnode->id->name))
1390                                 tnode->flag &= ~NODE_ACTIVE_ID;
1391                 }
1392         }
1393         
1394         node->flag |= NODE_ACTIVE;
1395         if(node->id)
1396                 node->flag |= NODE_ACTIVE_ID;
1397 }
1398
1399 /* use flags are not persistant yet, groups might need different tagging, so we do it each time
1400    when we need to get this info */
1401 void ntreeSocketUseFlags(bNodeTree *ntree)
1402 {
1403         bNode *node;
1404         bNodeSocket *sock;
1405         bNodeLink *link;
1406         
1407         /* clear flags */
1408         for(node= ntree->nodes.first; node; node= node->next) {
1409                 for(sock= node->inputs.first; sock; sock= sock->next)
1410                         sock->flag &= ~SOCK_IN_USE;
1411                 for(sock= node->outputs.first; sock; sock= sock->next)
1412                         sock->flag &= ~SOCK_IN_USE;
1413         }
1414         
1415         /* tag all thats in use */
1416         for(link= ntree->links.first; link; link= link->next) {
1417                 link->fromsock->flag |= SOCK_IN_USE;
1418                 link->tosock->flag |= SOCK_IN_USE;
1419         }
1420 }
1421
1422 /* ************** dependency stuff *********** */
1423
1424 /* node is guaranteed to be not checked before */
1425 static int node_recurs_check(bNode *node, bNode ***nsort, int level)
1426 {
1427         bNode *fromnode;
1428         bNodeSocket *sock;
1429         int has_inputlinks= 0;
1430         
1431         node->done= 1;
1432         level++;
1433         
1434         for(sock= node->inputs.first; sock; sock= sock->next) {
1435                 if(sock->link) {
1436                         has_inputlinks= 1;
1437                         fromnode= sock->link->fromnode;
1438                         if(fromnode->done==0) {
1439                                 fromnode->level= node_recurs_check(fromnode, nsort, level);
1440                         }
1441                 }
1442         }
1443 //      printf("node sort %s level %d\n", node->name, level);
1444         **nsort= node;
1445         (*nsort)++;
1446         
1447         if(has_inputlinks)
1448                 return level;
1449         else 
1450                 return 0xFFF;
1451 }
1452
1453 void ntreeSolveOrder(bNodeTree *ntree)
1454 {
1455         bNode *node, **nodesort, **nsort;
1456         bNodeSocket *sock;
1457         bNodeLink *link;
1458         int a, totnode=0;
1459         
1460         /* the solve-order is called on each tree change, so we should be sure no exec can be running */
1461         ntreeEndExecTree(ntree);
1462
1463         /* set links pointers the input sockets, to find dependencies */
1464         /* first clear data */
1465         for(node= ntree->nodes.first; node; node= node->next) {
1466                 node->done= 0;
1467                 totnode++;
1468                 for(sock= node->inputs.first; sock; sock= sock->next)
1469                         sock->link= NULL;
1470         }
1471         if(totnode==0)
1472                 return;
1473         
1474         for(link= ntree->links.first; link; link= link->next) {
1475                 link->tosock->link= link;
1476         }
1477         
1478         nsort= nodesort= MEM_callocN(totnode*sizeof(void *), "sorted node array");
1479         
1480         /* recursive check */
1481         for(node= ntree->nodes.first; node; node= node->next) {
1482                 if(node->done==0) {
1483                         node->level= node_recurs_check(node, &nsort, 0);
1484                 }
1485         }
1486         
1487         /* re-insert nodes in order, first a paranoia check */
1488         for(a=0; a<totnode; a++) {
1489                 if(nodesort[a]==NULL)
1490                         break;
1491         }
1492         if(a<totnode)
1493                 printf("sort error in node tree");
1494         else {
1495                 ntree->nodes.first= ntree->nodes.last= NULL;
1496                 for(a=0; a<totnode; a++)
1497                         BLI_addtail(&ntree->nodes, nodesort[a]);
1498         }
1499         
1500         MEM_freeN(nodesort);
1501         
1502         /* find the active outputs, might become tree type dependant handler */
1503         for(node= ntree->nodes.first; node; node= node->next) {
1504                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1505                         bNode *tnode;
1506                         int output= 0;
1507                         
1508                         /* we need a check for which output node should be tagged like this, below an exception */
1509                         if(node->type==CMP_NODE_OUTPUT_FILE)
1510                            continue;
1511                            
1512                         /* there is more types having output class, each one is checked */
1513                         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1514                                 if(tnode->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1515                                         if(tnode->type==node->type) {
1516                                                 if(tnode->flag & NODE_DO_OUTPUT) {
1517                                                         output++;
1518                                                         if(output>1)
1519                                                                 tnode->flag &= ~NODE_DO_OUTPUT;
1520                                                 }
1521                                         }
1522                                 }
1523                         }
1524                         if(output==0)
1525                                 node->flag |= NODE_DO_OUTPUT;
1526                 }
1527         }
1528         
1529         /* here we could recursively set which nodes have to be done,
1530                 might be different for editor or for "real" use... */
1531 }
1532
1533 /* should be callback! */
1534 void NodeTagChanged(bNodeTree *ntree, bNode *node)
1535 {
1536         if(ntree->type==NTREE_COMPOSIT) {
1537                 bNodeSocket *sock;
1538
1539                 for(sock= node->outputs.first; sock; sock= sock->next) {
1540                         if(sock->ns.data) {
1541                                 free_compbuf(sock->ns.data);
1542                                 sock->ns.data= NULL;
1543                                 
1544                                 //if(node->preview && node->preview->rect) {
1545                                 //      MEM_freeN(node->preview->rect);
1546                                 //      node->preview->rect= NULL;
1547                                 //}
1548                                         
1549                         }
1550                 }
1551                 node->need_exec= 1;
1552         }
1553 }
1554
1555 void NodeTagIDChanged(bNodeTree *ntree, ID *id)
1556 {
1557         if(id==NULL)
1558                 return;
1559         
1560         if(ntree->type==NTREE_COMPOSIT) {
1561                 bNode *node;
1562                 
1563                 for(node= ntree->nodes.first; node; node= node->next)
1564                         if(node->id==id)
1565                                 NodeTagChanged(ntree, node);
1566         }
1567 }
1568
1569
1570 /* *************** preview *********** */
1571
1572 /* if node->preview, then we assume the rect to exist */
1573
1574 static void nodeInitPreview(bNode *node, int xsize, int ysize)
1575 {
1576         
1577         if(node->preview==NULL) {
1578                 node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
1579 //              printf("added preview %s\n", node->name);
1580         }
1581         
1582         /* node previews can get added with variable size this way */
1583         if(xsize==0 || ysize==0)
1584                 return;
1585         
1586         /* sanity checks & initialize */
1587         if(node->preview->rect) {
1588                 if(node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
1589                         MEM_freeN(node->preview->rect);
1590                         node->preview->rect= NULL;
1591                 }
1592         }
1593         
1594         if(node->preview->rect==NULL) {
1595                 node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(float)*4, "node preview rect");
1596                 node->preview->xsize= xsize;
1597                 node->preview->ysize= ysize;
1598         }
1599 }
1600
1601 void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
1602 {
1603         bNode *node;
1604         
1605         if(ntree==NULL)
1606                 return;
1607         
1608         for(node= ntree->nodes.first; node; node= node->next) {
1609                 if(node->typeinfo->flag & NODE_PREVIEW) /* hrms, check for closed nodes? */
1610                         nodeInitPreview(node, xsize, ysize);
1611                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
1612                         ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
1613         }               
1614 }
1615
1616 static void nodeClearPreview(bNode *node)
1617 {
1618         if(node->preview && node->preview->rect)
1619                 memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
1620 }
1621
1622 /* use it to enforce clear */
1623 void ntreeClearPreview(bNodeTree *ntree)
1624 {
1625         bNode *node;
1626         
1627         if(ntree==NULL)
1628                 return;
1629         
1630         for(node= ntree->nodes.first; node; node= node->next) {
1631                 if(node->typeinfo->flag & NODE_PREVIEW)
1632                         nodeClearPreview(node);
1633                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
1634                         ntreeClearPreview((bNodeTree *)node->id);
1635         }               
1636 }
1637
1638 /* hack warning! this function is only used for shader previews, and 
1639    since it gets called multiple times per pixel for Ztransp we only
1640    add the color once. Preview gets cleared before it starts render though */
1641 void nodeAddToPreview(bNode *node, float *col, int x, int y)
1642 {
1643         bNodePreview *preview= node->preview;
1644         if(preview) {
1645                 if(x>=0 && y>=0) {
1646                         if(x<preview->xsize && y<preview->ysize) {
1647                                 float *tar= preview->rect+ 4*((preview->xsize*y) + x);
1648                                 if(tar[0]==0.0f) {
1649                                         QUATCOPY(tar, col);
1650                                 }
1651                         }
1652                         //else printf("prv out bound x y %d %d\n", x, y);
1653                 }
1654                 //else printf("prv out bound x y %d %d\n", x, y);
1655         }
1656 }
1657
1658
1659
1660 /* ******************* executing ************* */
1661
1662 /* see notes at ntreeBeginExecTree */
1663 static void group_node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out, bNodeStack **gin, bNodeStack **gout)
1664 {
1665         bNodeSocket *sock;
1666         
1667         /* build pointer stack */
1668         for(sock= node->inputs.first; sock; sock= sock->next) {
1669                 if(sock->intern) {
1670                         /* yep, intern can have link or is hidden socket */
1671                         if(sock->link)
1672                                 *(in++)= stack + sock->link->fromsock->stack_index;
1673                         else
1674                                 *(in++)= &sock->ns;
1675                 }
1676                 else
1677                         *(in++)= gin[sock->stack_index_ext];
1678         }
1679         
1680         for(sock= node->outputs.first; sock; sock= sock->next) {
1681                 if(sock->intern)
1682                         *(out++)= stack + sock->stack_index;
1683                 else
1684                         *(out++)= gout[sock->stack_index_ext];
1685         }
1686 }
1687
1688 static void node_group_execute(bNodeStack *stack, void *data, bNode *gnode, bNodeStack **in, bNodeStack **out)
1689 {
1690         bNode *node;
1691         bNodeTree *ntree= (bNodeTree *)gnode->id;
1692         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
1693         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
1694         
1695         if(ntree==NULL) return;
1696         
1697         stack+= gnode->stack_index;
1698                 
1699         for(node= ntree->nodes.first; node; node= node->next) {
1700                 if(node->typeinfo->execfunc) {
1701                         group_node_get_stack(node, stack, nsin, nsout, in, out);
1702                         
1703                         /* for groups, only execute outputs for edited group */
1704                         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1705                                 if(gnode->flag & NODE_GROUP_EDIT)
1706                                         if(node->flag & NODE_DO_OUTPUT)
1707                                                 node->typeinfo->execfunc(data, node, nsin, nsout);
1708                         }
1709                         else
1710                                 node->typeinfo->execfunc(data, node, nsin, nsout);
1711                 }
1712         }
1713         
1714         /* free internal group output nodes */
1715         if(ntree->type==NTREE_COMPOSIT) {
1716                 for(node= ntree->nodes.first; node; node= node->next) {
1717                         if(node->typeinfo->execfunc) {
1718                                 bNodeSocket *sock;
1719                                 
1720                                 for(sock= node->outputs.first; sock; sock= sock->next) {
1721                                         if(sock->intern) {
1722                                                 bNodeStack *ns= stack + sock->stack_index;
1723                                                 if(ns->data) {
1724                                                         free_compbuf(ns->data);
1725                                                         ns->data= NULL;
1726                                                 }
1727                                         }
1728                                 }
1729                         }
1730                 }
1731         }
1732 }
1733
1734 /* recursively called for groups */
1735 /* we set all trees on own local indices, but put a total counter
1736    in the groups, so each instance of a group has own stack */
1737 static int ntree_begin_exec_tree(bNodeTree *ntree)
1738 {
1739         bNode *node;
1740         bNodeSocket *sock;
1741         int index= 0, index_in= 0, index_out= 0;
1742         
1743         if((ntree->init & NTREE_TYPE_INIT)==0)
1744                 ntreeInitTypes(ntree);
1745         
1746         /* create indices for stack, check preview */
1747         for(node= ntree->nodes.first; node; node= node->next) {
1748                 
1749                 for(sock= node->inputs.first; sock; sock= sock->next) {
1750                         if(sock->intern==0)
1751                                 sock->stack_index_ext= index_in++;
1752                 }
1753                 
1754                 for(sock= node->outputs.first; sock; sock= sock->next) {
1755                         sock->stack_index= index++;
1756                         if(sock->intern==0)
1757                                 sock->stack_index_ext= index_out++;
1758                 }
1759                 
1760                 if(node->type==NODE_GROUP) {
1761                         if(node->id) {
1762                                 node->stack_index= index;
1763                                 index+= ntree_begin_exec_tree((bNodeTree *)node->id);
1764                         }
1765                 }
1766         }
1767         
1768         return index;
1769 }
1770
1771 /* copy socket compbufs to stack, initialize usage of curve nodes */
1772 static void composit_begin_exec(bNodeTree *ntree, int is_group)
1773 {
1774         bNode *node;
1775         bNodeSocket *sock;
1776         
1777         for(node= ntree->nodes.first; node; node= node->next) {
1778                 
1779                 /* initialize needed for groups */
1780                 node->exec= 0;  
1781                 
1782                 if(is_group==0) {
1783                         for(sock= node->outputs.first; sock; sock= sock->next) {
1784                                 bNodeStack *ns= ntree->stack[0] + sock->stack_index;
1785                                 
1786                                 if(sock->ns.data) {
1787                                         ns->data= sock->ns.data;
1788                                         sock->ns.data= NULL;
1789                                 }
1790                         }
1791                 }               
1792                 /* cannot initialize them while using in threads */
1793                 if(ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB)) {
1794                         curvemapping_initialize(node->storage);
1795                         if(node->type==CMP_NODE_CURVE_RGB)
1796                                 curvemapping_premultiply(node->storage, 0);
1797                 }
1798                 if(node->type==NODE_GROUP)
1799                         composit_begin_exec((bNodeTree *)node->id, 1);
1800
1801         }
1802 }
1803
1804 /* copy stack compbufs to sockets */
1805 static void composit_end_exec(bNodeTree *ntree, int is_group)
1806 {
1807         extern void print_compbuf(char *str, struct CompBuf *cbuf);
1808         bNode *node;
1809         bNodeStack *ns;
1810         int a;
1811
1812         for(node= ntree->nodes.first; node; node= node->next) {
1813                 if(is_group==0) {
1814                         bNodeSocket *sock;
1815                 
1816                         for(sock= node->outputs.first; sock; sock= sock->next) {
1817                                 ns= ntree->stack[0] + sock->stack_index;
1818                                 if(ns->data) {
1819                                         sock->ns.data= ns->data;
1820                                         ns->data= NULL;
1821                                 }
1822                         }
1823                 }
1824                 if(node->type==CMP_NODE_CURVE_RGB)
1825                         curvemapping_premultiply(node->storage, 1);
1826                 
1827                 if(node->type==NODE_GROUP)
1828                         composit_end_exec((bNodeTree *)node->id, 1);
1829
1830                 node->need_exec= 0;
1831         }
1832         
1833         if(is_group==0) {
1834                 /* internally, group buffers are not stored */
1835                 for(ns= ntree->stack[0], a=0; a<ntree->stacksize; a++, ns++) {
1836                         if(ns->data) {
1837                                 printf("freed leftover buffer from stack\n");
1838                                 free_compbuf(ns->data);
1839                         }
1840                 }
1841         }
1842 }
1843
1844 static void group_tag_used_outputs(bNode *gnode, bNodeStack *stack)
1845 {
1846         bNodeTree *ntree= (bNodeTree *)gnode->id;
1847         bNode *node;
1848         
1849         stack+= gnode->stack_index;
1850         
1851         for(node= ntree->nodes.first; node; node= node->next) {
1852                 if(node->typeinfo->execfunc) {
1853                         bNodeSocket *sock;
1854                         
1855                         for(sock= node->inputs.first; sock; sock= sock->next) {
1856                                 if(sock->intern) {
1857                                         if(sock->link) {
1858                                                 bNodeStack *ns= stack + sock->link->fromsock->stack_index;
1859                                                 ns->hasoutput= 1;
1860                                                 ns->sockettype= sock->link->fromsock->type;
1861                                         }
1862                                         else
1863                                                 sock->ns.sockettype= sock->type;
1864                                 }
1865                         }
1866                 }
1867         }
1868 }
1869
1870 /* stack indices make sure all nodes only write in allocated data, for making it thread safe */
1871 /* only root tree gets the stack, to enable instances to have own stack entries */
1872 /* only two threads now! */
1873 /* per tree (and per group) unique indices are created */
1874 /* the index_ext we need to be able to map from groups to the group-node own stack */
1875
1876 void ntreeBeginExecTree(bNodeTree *ntree)
1877 {
1878         /* let's make it sure */
1879         if(ntree->init & NTREE_EXEC_INIT)
1880                 return;
1881         
1882         /* allocate the stack pointer array */
1883         ntree->stack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(void *), "stack array");
1884         
1885         /* goes recursive over all groups */
1886         ntree->stacksize= ntree_begin_exec_tree(ntree);
1887
1888         if(ntree->stacksize) {
1889                 bNode *node;
1890                 bNodeStack *ns;
1891                 int a;
1892                 
1893                 /* allocate the base stack */
1894                 ns=ntree->stack[0]= MEM_callocN(ntree->stacksize*sizeof(bNodeStack), "node stack");
1895                 
1896                 /* tag inputs, the get_stack() gives own socket stackdata if not in use */
1897                 for(a=0; a<ntree->stacksize; a++, ns++) ns->hasinput= 1;
1898                 
1899                 /* tag used outputs, so we know when we can skip operations */
1900                 for(node= ntree->nodes.first; node; node= node->next) {
1901                         bNodeSocket *sock;
1902                         for(sock= node->inputs.first; sock; sock= sock->next) {
1903                                 if(sock->link) {
1904                                         ns= ntree->stack[0] + sock->link->fromsock->stack_index;
1905                                         ns->hasoutput= 1;
1906                                         ns->sockettype= sock->link->fromsock->type;
1907                                 }
1908                                 else
1909                                         sock->ns.sockettype= sock->type;
1910                         }
1911                         if(node->type==NODE_GROUP && node->id)
1912                                 group_tag_used_outputs(node, ntree->stack[0]);
1913                 }
1914                 
1915                 /* composite does 1 node per thread, so no multiple stacks needed */
1916                 if(ntree->type==NTREE_COMPOSIT)
1917                         composit_begin_exec(ntree, 0);
1918                 else {
1919                         for(a=1; a<BLENDER_MAX_THREADS; a++)
1920                                 ntree->stack[a]= MEM_dupallocN(ntree->stack[0]);
1921                 }
1922         }
1923         
1924         ntree->init |= NTREE_EXEC_INIT;
1925 }
1926
1927 void ntreeEndExecTree(bNodeTree *ntree)
1928 {
1929         
1930         if(ntree->init & NTREE_EXEC_INIT) {
1931                 int a;
1932                 
1933                 /* another callback candidate! */
1934                 if(ntree->type==NTREE_COMPOSIT)
1935                         composit_end_exec(ntree, 0);
1936                 
1937                 if(ntree->stack) {
1938                         for(a=0; a<BLENDER_MAX_THREADS; a++)
1939                                 if(ntree->stack[a])
1940                                         MEM_freeN(ntree->stack[a]);
1941                 
1942                         MEM_freeN(ntree->stack);
1943                         ntree->stack= NULL;
1944                 }
1945
1946                 ntree->init &= ~NTREE_EXEC_INIT;
1947         }
1948 }
1949
1950 static void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
1951 {
1952         bNodeSocket *sock;
1953         
1954         /* build pointer stack */
1955         for(sock= node->inputs.first; sock; sock= sock->next) {
1956                 if(sock->link)
1957                         *(in++)= stack + sock->link->fromsock->stack_index;
1958                 else
1959                         *(in++)= &sock->ns;
1960         }
1961         
1962         for(sock= node->outputs.first; sock; sock= sock->next) {
1963                 *(out++)= stack + sock->stack_index;
1964         }
1965 }
1966
1967 /* nodes are presorted, so exec is in order of list */
1968 void ntreeExecTree(bNodeTree *ntree, void *callerdata, int thread)
1969 {
1970         bNode *node;
1971         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
1972         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
1973         bNodeStack *stack;
1974         
1975         /* only when initialized */
1976         if((ntree->init & NTREE_EXEC_INIT)==0)
1977                 ntreeBeginExecTree(ntree);
1978                 
1979         stack= ntree->stack[thread];
1980         
1981         for(node= ntree->nodes.first; node; node= node->next) {
1982                 if(node->typeinfo->execfunc) {
1983                         node_get_stack(node, stack, nsin, nsout);
1984                         node->typeinfo->execfunc(callerdata, node, nsin, nsout);
1985                 }
1986                 else if(node->type==NODE_GROUP && node->id) {
1987                         node_get_stack(node, stack, nsin, nsout);
1988                         node_group_execute(stack, callerdata, node, nsin, nsout); 
1989                 }
1990         }
1991 }
1992
1993
1994 /* ***************************** threaded version for execute composite nodes ************* */
1995 /* these are nodes without input, only giving values */
1996 /* or nodes with only value inputs */
1997 static int node_only_value(bNode *node)
1998 {
1999         bNodeSocket *sock;
2000         
2001         if(ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_VALUE, CMP_NODE_RGB))
2002                 return 1;
2003         
2004         /* doing this for all node types goes wrong. memory free errors */
2005         if(node->inputs.first && node->type==CMP_NODE_MAP_VALUE) {
2006                 int retval= 1;
2007                 for(sock= node->inputs.first; sock; sock= sock->next) {
2008                         if(sock->link)
2009                                 retval &= node_only_value(sock->link->fromnode);
2010                 }
2011                 return retval;
2012         }
2013         return 0;
2014 }
2015
2016
2017 /* not changing info, for thread callback */
2018 typedef struct ThreadData {
2019         bNodeStack *stack;
2020         RenderData *rd;
2021 } ThreadData;
2022
2023 static void *exec_composite_node(void *node_v)
2024 {
2025         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2026         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2027         bNode *node= node_v;
2028         ThreadData *thd= (ThreadData *)node->new_node; /* abuse */
2029         
2030         node_get_stack(node, thd->stack, nsin, nsout);
2031         
2032         if((node->flag & NODE_MUTED) && (!node_only_value(node))) {
2033                 /* viewers we execute, for feedback to user */
2034                 if(ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
2035                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2036                 else
2037                         node_compo_pass_on(node, nsin, nsout);
2038         }
2039         else if(node->typeinfo->execfunc) {
2040                 node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2041         }
2042         else if(node->type==NODE_GROUP && node->id) {
2043                 node_group_execute(thd->stack, thd->rd, node, nsin, nsout); 
2044         }
2045         
2046         node->exec |= NODE_READY;
2047         return 0;
2048 }
2049
2050 /* return total of executable nodes, for timecursor */
2051 /* only compositor uses it */
2052 static int setExecutableNodes(bNodeTree *ntree, ThreadData *thd)
2053 {
2054         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2055         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2056         bNode *node;
2057         bNodeSocket *sock;
2058         int totnode= 0, group_edit= 0;
2059         
2060         /* note; do not add a dependency sort here, the stack was created already */
2061         
2062         /* if we are in group edit, viewer nodes get skipped when group has viewer */
2063         for(node= ntree->nodes.first; node; node= node->next)
2064                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
2065                         if(ntreeHasType((bNodeTree *)node->id, CMP_NODE_VIEWER))
2066                                 group_edit= 1;
2067         
2068         for(node= ntree->nodes.first; node; node= node->next) {
2069                 int a;
2070                 
2071                 node_get_stack(node, thd->stack, nsin, nsout);
2072                 
2073                 /* test the outputs */
2074                 /* skip value-only nodes (should be in type!) */
2075                 if(!node_only_value(node)) {
2076                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2077                                 if(nsout[a]->data==NULL && nsout[a]->hasoutput) {
2078                                         node->need_exec= 1;
2079                                         break;
2080                                 }
2081                         }
2082                 }
2083                 
2084                 /* test the inputs */
2085                 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
2086                         /* skip viewer nodes in bg render or group edit */
2087                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) && (G.background || group_edit))
2088                                 node->need_exec= 0;
2089                         /* is sock in use? */
2090                         else if(sock->link) {
2091                                 bNodeLink *link= sock->link;
2092                                 /* this is the test for a cyclic case */
2093                                 if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2094                                         if(link->fromnode->need_exec) {
2095                                                 node->need_exec= 1;
2096                                                 break;
2097                                         }
2098                                 }
2099                                 else {
2100                                         node->need_exec= 0;
2101                                         printf("Node %s skipped, cyclic dependency\n", node->name);
2102                                 }
2103                         }
2104                 }
2105                 
2106                 if(node->need_exec) {
2107                         
2108                         /* free output buffers */
2109                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2110                                 if(nsout[a]->data) {
2111                                         free_compbuf(nsout[a]->data);
2112                                         nsout[a]->data= NULL;
2113                                 }
2114                         }
2115                         totnode++;
2116                         /* printf("node needs exec %s\n", node->name); */
2117                         
2118                         /* tag for getExecutableNode() */
2119                         node->exec= 0;
2120                 }
2121                 else {
2122                         /* tag for getExecutableNode() */
2123                         node->exec= NODE_READY|NODE_FINISHED;
2124                         
2125                 }
2126         }
2127         
2128         /* last step: set the stack values for only-value nodes */
2129         /* just does all now, compared to a full buffer exec this is nothing */
2130         if(totnode) {
2131                 for(node= ntree->nodes.first; node; node= node->next) {
2132                         if(node->need_exec==0 && node_only_value(node)) {
2133                                 if(node->typeinfo->execfunc) {
2134                                         node_get_stack(node, thd->stack, nsin, nsout);
2135                                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2136                                 }
2137                         }
2138                 }
2139         }
2140         
2141         return totnode;
2142 }
2143
2144 /* while executing tree, free buffers from nodes that are not needed anymore */
2145 static void freeExecutableNode(bNodeTree *ntree)
2146 {
2147         /* node outputs can be freed when:
2148         - not a render result or image node
2149         - when node outputs go to nodes all being set NODE_FINISHED
2150         */
2151         bNode *node;
2152         bNodeSocket *sock;
2153         
2154         /* set exec flag for finished nodes that might need freed */
2155         for(node= ntree->nodes.first; node; node= node->next) {
2156                 if(node->type!=CMP_NODE_R_LAYERS)
2157                         if(node->exec & NODE_FINISHED)
2158                                 node->exec |= NODE_FREEBUFS;
2159         }
2160         /* clear this flag for input links that are not done yet */
2161         for(node= ntree->nodes.first; node; node= node->next) {
2162                 if((node->exec & NODE_FINISHED)==0) {
2163                         for(sock= node->inputs.first; sock; sock= sock->next)
2164                                 if(sock->link)
2165                                         sock->link->fromnode->exec &= ~NODE_FREEBUFS;
2166                 }
2167         }
2168         /* now we can free buffers */
2169         for(node= ntree->nodes.first; node; node= node->next) {
2170                 if(node->exec & NODE_FREEBUFS) {
2171                         for(sock= node->outputs.first; sock; sock= sock->next) {
2172                                 bNodeStack *ns= ntree->stack[0] + sock->stack_index;
2173                                 if(ns->data) {
2174                                         free_compbuf(ns->data);
2175                                         ns->data= NULL;
2176                                         // printf("freed buf node %s \n", node->name);
2177                                 }
2178                         }
2179                 }
2180         }
2181 }
2182
2183 static bNode *getExecutableNode(bNodeTree *ntree)
2184 {
2185         bNode *node;
2186         bNodeSocket *sock;
2187         
2188         for(node= ntree->nodes.first; node; node= node->next) {
2189                 if(node->exec==0) {
2190                         
2191                         /* input sockets should be ready */
2192                         for(sock= node->inputs.first; sock; sock= sock->next) {
2193                                 if(sock->link)
2194                                         if((sock->link->fromnode->exec & NODE_READY)==0)
2195                                                 break;
2196                         }
2197                         if(sock==NULL)
2198                                 return node;
2199                 }
2200         }
2201         return NULL;
2202 }
2203
2204
2205 /* optimized tree execute test for compositing */
2206 void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
2207 {
2208         bNode *node;
2209         ListBase threads;
2210         ThreadData thdata;
2211         int totnode, rendering= 1;
2212         
2213         if(ntree==NULL) return;
2214         
2215         if(do_preview)
2216                 ntreeInitPreview(ntree, 0, 0);
2217         
2218         ntreeBeginExecTree(ntree);
2219         
2220         /* prevent unlucky accidents */
2221         if(G.background)
2222                 rd->scemode &= ~R_COMP_CROP;
2223         
2224         /* setup callerdata for thread callback */
2225         thdata.rd= rd;
2226         thdata.stack= ntree->stack[0];
2227         
2228         /* fixed seed, for example noise texture */
2229         BLI_srandom(rd->cfra);
2230
2231         /* sets need_exec tags in nodes */
2232         totnode= setExecutableNodes(ntree, &thdata);
2233         
2234         BLI_init_threads(&threads, exec_composite_node, rd->threads);
2235         
2236         while(rendering) {
2237                 
2238                 if(BLI_available_threads(&threads)) {
2239                         node= getExecutableNode(ntree);
2240                         if(node) {
2241
2242                                 if(ntree->timecursor)
2243                                         ntree->timecursor(totnode);
2244                                 if(ntree->stats_draw) {
2245                                         char str[64];
2246                                         sprintf(str, "Compositing %d %s", totnode, node->name);
2247                                         ntree->stats_draw(str);
2248                                 }
2249                                 totnode--;
2250                                 
2251                                 node->new_node = (bNode *)&thdata;
2252                                 node->exec= NODE_PROCESSING;
2253                                 BLI_insert_thread(&threads, node);
2254                         }
2255                         else
2256                                 PIL_sleep_ms(50);
2257                 }
2258                 else
2259                         PIL_sleep_ms(50);
2260                 
2261                 rendering= 0;
2262                 /* test for ESC */
2263                 if(ntree->test_break && ntree->test_break()) {
2264                         for(node= ntree->nodes.first; node; node= node->next)
2265                                 node->exec |= NODE_READY;
2266                 }
2267                 
2268                 /* check for ready ones, and if we need to continue */
2269                 for(node= ntree->nodes.first; node; node= node->next) {
2270                         if(node->exec & NODE_READY) {
2271                                 if((node->exec & NODE_FINISHED)==0) {
2272                                         BLI_remove_thread(&threads, node); /* this waits for running thread to finish btw */
2273                                         node->exec |= NODE_FINISHED;
2274                                         
2275                                         /* freeing unused buffers */
2276                                         if(rd->scemode & R_COMP_FREE)
2277                                                 freeExecutableNode(ntree);
2278                                 }
2279                         }
2280                         else rendering= 1;
2281                 }
2282         }
2283         
2284         
2285         BLI_end_threads(&threads);
2286         
2287         ntreeEndExecTree(ntree);
2288 }
2289
2290
2291 /* **************** call to switch lamploop for material node ************ */
2292
2293 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
2294
2295 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
2296 {
2297         node_shader_lamp_loop= lamp_loop_func;
2298 }
2299
2300 /* clumsy checking... should do dynamic outputs once */
2301 static void force_hidden_passes(bNode *node, int passflag)
2302 {
2303         bNodeSocket *sock;
2304         
2305         for(sock= node->outputs.first; sock; sock= sock->next)
2306                 sock->flag &= ~SOCK_UNAVAIL;
2307         
2308         sock= BLI_findlink(&node->outputs, RRES_OUT_Z);
2309         if(!(passflag & SCE_PASS_Z)) sock->flag |= SOCK_UNAVAIL;
2310         sock= BLI_findlink(&node->outputs, RRES_OUT_NORMAL);
2311         if(!(passflag & SCE_PASS_NORMAL)) sock->flag |= SOCK_UNAVAIL;
2312         sock= BLI_findlink(&node->outputs, RRES_OUT_VEC);
2313         if(!(passflag & SCE_PASS_VECTOR)) sock->flag |= SOCK_UNAVAIL;
2314         sock= BLI_findlink(&node->outputs, RRES_OUT_UV);
2315         if(!(passflag & SCE_PASS_UV)) sock->flag |= SOCK_UNAVAIL;
2316         sock= BLI_findlink(&node->outputs, RRES_OUT_RGBA);
2317         if(!(passflag & SCE_PASS_RGBA)) sock->flag |= SOCK_UNAVAIL;
2318         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF);
2319         if(!(passflag & SCE_PASS_DIFFUSE)) sock->flag |= SOCK_UNAVAIL;
2320         sock= BLI_findlink(&node->outputs, RRES_OUT_SPEC);
2321         if(!(passflag & SCE_PASS_SPEC)) sock->flag |= SOCK_UNAVAIL;
2322         sock= BLI_findlink(&node->outputs, RRES_OUT_SHADOW);
2323         if(!(passflag & SCE_PASS_SHADOW)) sock->flag |= SOCK_UNAVAIL;
2324         sock= BLI_findlink(&node->outputs, RRES_OUT_AO);
2325         if(!(passflag & SCE_PASS_AO)) sock->flag |= SOCK_UNAVAIL;
2326         sock= BLI_findlink(&node->outputs, RRES_OUT_REFLECT);
2327         if(!(passflag & SCE_PASS_REFLECT)) sock->flag |= SOCK_UNAVAIL;
2328         sock= BLI_findlink(&node->outputs, RRES_OUT_REFRACT);
2329         if(!(passflag & SCE_PASS_REFRACT)) sock->flag |= SOCK_UNAVAIL;
2330         sock= BLI_findlink(&node->outputs, RRES_OUT_RADIO);
2331         if(!(passflag & SCE_PASS_RADIO)) sock->flag |= SOCK_UNAVAIL;
2332         sock= BLI_findlink(&node->outputs, RRES_OUT_INDEXOB);
2333         if(!(passflag & SCE_PASS_INDEXOB)) sock->flag |= SOCK_UNAVAIL;
2334         sock= BLI_findlink(&node->outputs, RRES_OUT_MIST);
2335         if(!(passflag & SCE_PASS_MIST)) sock->flag |= SOCK_UNAVAIL;
2336         
2337 }
2338
2339 /* based on rules, force sockets hidden always */
2340 void ntreeCompositForceHidden(bNodeTree *ntree)
2341 {
2342         bNode *node;
2343         
2344         if(ntree==NULL) return;
2345         
2346         for(node= ntree->nodes.first; node; node= node->next) {
2347                 if( node->type==CMP_NODE_R_LAYERS) {
2348                         Scene *sce= node->id?(Scene *)node->id:G.scene; /* G.scene is WEAK! */
2349                         SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
2350                         if(srl)
2351                                 force_hidden_passes(node, srl->passflag);
2352                 }
2353                 else if( node->type==CMP_NODE_IMAGE) {
2354                         Image *ima= (Image *)node->id;
2355                         if(ima) {
2356                                 if(ima->rr) {
2357                                         ImageUser *iuser= node->storage;
2358                                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
2359                                         if(rl)
2360                                                 force_hidden_passes(node, rl->passflag);
2361                                         else
2362                                                 force_hidden_passes(node, 0);
2363                                 }
2364                                 else if(ima->type!=IMA_TYPE_MULTILAYER) {       /* if ->rr not yet read we keep inputs */
2365                                         force_hidden_passes(node, RRES_OUT_Z);
2366                                 }
2367                                 else
2368                                         force_hidden_passes(node, 0);
2369                         }
2370                         else
2371                                 force_hidden_passes(node, 0);
2372                 }
2373         }
2374
2375 }
2376
2377 /* called from render pipeline, to tag render input and output */
2378 /* need to do all scenes, to prevent errors when you re-render 1 scene */
2379 void ntreeCompositTagRender(Scene *curscene)
2380 {
2381         Scene *sce;
2382         
2383         for(sce= G.main->scene.first; sce; sce= sce->id.next) {
2384                 if(sce->nodetree) {
2385                         bNode *node;
2386                         
2387                         for(node= sce->nodetree->nodes.first; node; node= node->next) {
2388                                 if(node->id==(ID *)curscene || node->type==CMP_NODE_COMPOSITE)
2389                                         NodeTagChanged(sce->nodetree, node);
2390                         }
2391                 }
2392         }
2393 }
2394
2395 /* tags nodes that have animation capabilities */
2396 int ntreeCompositTagAnimated(bNodeTree *ntree)
2397 {
2398         bNode *node;
2399         int tagged= 0;
2400         
2401         if(ntree==NULL) return 0;
2402         
2403         for(node= ntree->nodes.first; node; node= node->next) {
2404                 if(node->type==CMP_NODE_IMAGE) {
2405                         Image *ima= (Image *)node->id;
2406                         if(ima && ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
2407                                 NodeTagChanged(ntree, node);
2408                                 tagged= 1;
2409                         }
2410                 }
2411                 else if(node->type==CMP_NODE_TIME) {
2412                         NodeTagChanged(ntree, node);
2413                         tagged= 1;
2414                 }
2415                 else if(node->type==CMP_NODE_R_LAYERS) {
2416                         NodeTagChanged(ntree, node);
2417                         tagged= 1;
2418                 }
2419                 else if(node->type==NODE_GROUP) {
2420                         if( ntreeCompositTagAnimated((bNodeTree *)node->id) ) {
2421                                 NodeTagChanged(ntree, node);
2422                         }
2423                 }
2424         }
2425         
2426         return tagged;
2427 }
2428
2429
2430 /* called from image window preview */
2431 void ntreeCompositTagGenerators(bNodeTree *ntree)
2432 {
2433         bNode *node;
2434         
2435         if(ntree==NULL) return;
2436         
2437         for(node= ntree->nodes.first; node; node= node->next) {
2438                 if( ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_IMAGE))
2439                         NodeTagChanged(ntree, node);
2440         }
2441 }
2442
2443 /* ************* node definition init ********** */
2444
2445 static bNodeType *is_nodetype_registered(ListBase *typelist, int type, ID *id) 
2446 {
2447         bNodeType *ntype= typelist->first;
2448         
2449         for(;ntype; ntype= ntype->next )
2450                 if(ntype->type==type && ntype->id==id)
2451                         return ntype;
2452         
2453         return NULL;
2454 }
2455
2456 /* type can be from a static array, we make copy for duplicate types (like group) */
2457 void nodeRegisterType(ListBase *typelist, const bNodeType *ntype) 
2458 {
2459         bNodeType *found= is_nodetype_registered(typelist, ntype->type, ntype->id);
2460         
2461         if(found==NULL) {
2462                 bNodeType *ntypen= MEM_callocN(sizeof(bNodeType), "node type");
2463                 *ntypen= *ntype;
2464                 BLI_addtail(typelist, ntypen);
2465         }
2466 }
2467
2468 static void registerCompositNodes(ListBase *ntypelist)
2469 {
2470         nodeRegisterType(ntypelist, &node_group_typeinfo);
2471         nodeRegisterType(ntypelist, &cmp_node_rlayers);
2472         nodeRegisterType(ntypelist, &cmp_node_image);
2473         nodeRegisterType(ntypelist, &cmp_node_texture);
2474         nodeRegisterType(ntypelist, &cmp_node_value);
2475         nodeRegisterType(ntypelist, &cmp_node_rgb);
2476         nodeRegisterType(ntypelist, &cmp_node_curve_time);
2477         
2478         nodeRegisterType(ntypelist, &cmp_node_composite);
2479         nodeRegisterType(ntypelist, &cmp_node_viewer);
2480         nodeRegisterType(ntypelist, &cmp_node_splitviewer);
2481         nodeRegisterType(ntypelist, &cmp_node_output_file);
2482         
2483         nodeRegisterType(ntypelist, &cmp_node_curve_rgb);
2484         nodeRegisterType(ntypelist, &cmp_node_mix_rgb);
2485         nodeRegisterType(ntypelist, &cmp_node_hue_sat);
2486         nodeRegisterType(ntypelist, &cmp_node_brightcontrast);
2487         nodeRegisterType(ntypelist, &cmp_node_gamma);
2488         nodeRegisterType(ntypelist, &cmp_node_invert);
2489         nodeRegisterType(ntypelist, &cmp_node_alphaover);
2490         nodeRegisterType(ntypelist, &cmp_node_zcombine);
2491         
2492         nodeRegisterType(ntypelist, &cmp_node_normal);
2493         nodeRegisterType(ntypelist, &cmp_node_curve_vec);
2494         nodeRegisterType(ntypelist, &cmp_node_map_value);
2495         nodeRegisterType(ntypelist, &cmp_node_normalize);
2496         
2497         nodeRegisterType(ntypelist, &cmp_node_filter);
2498         nodeRegisterType(ntypelist, &cmp_node_blur);
2499         nodeRegisterType(ntypelist, &cmp_node_dblur);
2500         nodeRegisterType(ntypelist, &cmp_node_bilateralblur);
2501         nodeRegisterType(ntypelist, &cmp_node_vecblur);
2502         nodeRegisterType(ntypelist, &cmp_node_dilateerode);
2503         nodeRegisterType(ntypelist, &cmp_node_defocus);
2504         
2505         nodeRegisterType(ntypelist, &cmp_node_valtorgb);
2506         nodeRegisterType(ntypelist, &cmp_node_rgbtobw);
2507         nodeRegisterType(ntypelist, &cmp_node_setalpha);
2508         nodeRegisterType(ntypelist, &cmp_node_idmask);
2509         nodeRegisterType(ntypelist, &cmp_node_math);
2510         nodeRegisterType(ntypelist, &cmp_node_seprgba);
2511         nodeRegisterType(ntypelist, &cmp_node_combrgba);
2512         nodeRegisterType(ntypelist, &cmp_node_sephsva);
2513         nodeRegisterType(ntypelist, &cmp_node_combhsva);
2514         nodeRegisterType(ntypelist, &cmp_node_sepyuva);
2515         nodeRegisterType(ntypelist, &cmp_node_combyuva);
2516         nodeRegisterType(ntypelist, &cmp_node_sepycca);
2517         nodeRegisterType(ntypelist, &cmp_node_combycca);
2518         nodeRegisterType(ntypelist, &cmp_node_premulkey);
2519         
2520         nodeRegisterType(ntypelist, &cmp_node_diff_matte);
2521         nodeRegisterType(ntypelist, &cmp_node_chroma);
2522         nodeRegisterType(ntypelist, &cmp_node_channel_matte);
2523         nodeRegisterType(ntypelist, &cmp_node_color_spill);
2524         nodeRegisterType(ntypelist, &cmp_node_luma_matte);
2525         
2526         nodeRegisterType(ntypelist, &cmp_node_translate);
2527         nodeRegisterType(ntypelist, &cmp_node_rotate);
2528         nodeRegisterType(ntypelist, &cmp_node_scale);
2529         nodeRegisterType(ntypelist, &cmp_node_flip);
2530         nodeRegisterType(ntypelist, &cmp_node_crop);
2531         nodeRegisterType(ntypelist, &cmp_node_displace);
2532         nodeRegisterType(ntypelist, &cmp_node_mapuv);
2533         nodeRegisterType(ntypelist, &cmp_node_glare);
2534         nodeRegisterType(ntypelist, &cmp_node_tonemap);
2535         nodeRegisterType(ntypelist, &cmp_node_lensdist);
2536 }
2537
2538 static void registerShaderNodes(ListBase *ntypelist) 
2539 {
2540         nodeRegisterType(ntypelist, &node_group_typeinfo);
2541         nodeRegisterType(ntypelist, &sh_node_output);
2542         nodeRegisterType(ntypelist, &sh_node_mix_rgb);
2543         nodeRegisterType(ntypelist, &sh_node_valtorgb);
2544         nodeRegisterType(ntypelist, &sh_node_rgbtobw);
2545         nodeRegisterType(ntypelist, &sh_node_normal);
2546         nodeRegisterType(ntypelist, &sh_node_geom);
2547         nodeRegisterType(ntypelist, &sh_node_mapping);
2548         nodeRegisterType(ntypelist, &sh_node_curve_vec);
2549         nodeRegisterType(ntypelist, &sh_node_curve_rgb);
2550         nodeRegisterType(ntypelist, &sh_node_math);
2551         nodeRegisterType(ntypelist, &sh_node_vect_math);
2552         nodeRegisterType(ntypelist, &sh_node_squeeze);
2553         nodeRegisterType(ntypelist, &sh_node_camera);
2554         nodeRegisterType(ntypelist, &sh_node_material);
2555         nodeRegisterType(ntypelist, &sh_node_material_ext);
2556         nodeRegisterType(ntypelist, &sh_node_value);
2557         nodeRegisterType(ntypelist, &sh_node_rgb);
2558         nodeRegisterType(ntypelist, &sh_node_texture);
2559         nodeRegisterType(ntypelist, &node_dynamic_typeinfo);
2560         nodeRegisterType(ntypelist, &sh_node_invert);
2561         nodeRegisterType(ntypelist, &sh_node_seprgb);
2562         nodeRegisterType(ntypelist, &sh_node_combrgb);
2563         nodeRegisterType(ntypelist, &sh_node_hue_sat);
2564 }
2565
2566 static void remove_dynamic_typeinfos(ListBase *list)
2567 {
2568         bNodeType *ntype= list->first;
2569         bNodeType *next= NULL;
2570         while(ntype) {
2571                 next= ntype->next;
2572                 if(ntype->type==NODE_DYNAMIC && ntype->id!=NULL) {
2573                         BLI_remlink(list, ntype);
2574                         if(ntype->inputs) {
2575                                 bNodeSocketType *sock= ntype->inputs;
2576                                 while(sock->type!=-1) {
2577                                         MEM_freeN(sock->name);
2578                                         sock++;
2579                                 }
2580                                 MEM_freeN(ntype->inputs);
2581                         }
2582                         if(ntype->outputs) {
2583                                 bNodeSocketType *sock= ntype->outputs;
2584                                 while(sock->type!=-1) {
2585                                         MEM_freeN(sock->name);
2586                                         sock++;
2587                                 }
2588                                 MEM_freeN(ntype->outputs);
2589                         }
2590                         if(ntype->name) {
2591                                 MEM_freeN(ntype->name);
2592                         }
2593                         MEM_freeN(ntype);
2594                 }
2595                 ntype= next;
2596         }
2597 }
2598
2599 void init_nodesystem(void) 
2600 {
2601         registerCompositNodes(&node_all_composit);
2602         registerShaderNodes(&node_all_shaders);
2603 }
2604
2605 void free_nodesystem(void) 
2606 {
2607         /*remove_dynamic_typeinfos(&node_all_composit);*/ /* unused for now */
2608         BLI_freelistN(&node_all_composit);
2609         remove_dynamic_typeinfos(&node_all_shaders);
2610         BLI_freelistN(&node_all_shaders);
2611 }