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