fix for using freed memory with animation/group/ungroup code.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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): Bob Holcomb.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #ifdef WITH_PYTHON
31 #include <Python.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <stddef.h>
36 #include <string.h>
37
38 #include "DNA_anim_types.h"
39 #include "DNA_action_types.h"
40
41 #include "RNA_access.h"
42
43 #include "BKE_animsys.h"
44 #include "BKE_action.h"
45 #include "BKE_fcurve.h"
46 #include "BKE_utildefines.h"
47
48 #include "PIL_time.h"
49
50 #include "CMP_node.h"
51 #include "intern/CMP_util.h"    /* stupid include path... */
52
53 #include "SHD_node.h"
54 #include "TEX_node.h"
55 #include "intern/TEX_util.h"
56
57 #include "GPU_material.h"
58
59 static ListBase empty_list = {NULL, NULL};
60 ListBase node_all_composit = {NULL, NULL};
61 ListBase node_all_shaders = {NULL, NULL};
62 ListBase node_all_textures = {NULL, NULL};
63
64 /* ************** Type stuff **********  */
65
66 static bNodeType *node_get_type(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
67 {
68         if(type==NODE_GROUP) {
69                 if(ngroup && GS(ngroup->id.name)==ID_NT) {
70                         return ngroup->owntype;
71                 }
72                 return NULL;
73         }
74         else {
75                 bNodeType *ntype = ntree->alltypes.first;
76                 for(; ntype; ntype= ntype->next)
77                         if(ntype->type==type && id==ntype->id )
78                                 return ntype;
79                 
80                 return NULL;
81         }
82 }
83
84 void ntreeInitTypes(bNodeTree *ntree)
85 {
86         bNode *node, *next;
87         
88         if(ntree->type==NTREE_SHADER)
89                 ntree->alltypes= node_all_shaders;
90         else if(ntree->type==NTREE_COMPOSIT)
91                 ntree->alltypes= node_all_composit;
92         else if(ntree->type==NTREE_TEXTURE)
93                 ntree->alltypes= node_all_textures;
94         else {
95                 ntree->alltypes= empty_list;
96                 printf("Error: no type definitions for nodes\n");
97         }
98         
99         for(node= ntree->nodes.first; node; node= next) {
100                 next= node->next;
101                 if(node->type==NODE_DYNAMIC) {
102                         bNodeType *stype= NULL;
103                         if(node->id==NULL) { /* empty script node */
104                                 stype= node_get_type(ntree, node->type, NULL, NULL);
105                         } else { /* not an empty script node */
106                                 stype= node_get_type(ntree, node->type, NULL, node->id);
107                                 if(!stype) {
108                                         stype= node_get_type(ntree, node->type, NULL, NULL);
109                                         /* needed info if the pynode script fails now: */
110                                         if (node->id) node->storage= ntree;
111                                 } else {
112                                         node->custom1= 0;
113                                         node->custom1= BSET(node->custom1,NODE_DYNAMIC_ADDEXIST);
114                                 }
115                         }
116                         node->typeinfo= stype;
117                         if(node->typeinfo)
118                                 node->typeinfo->initfunc(node);
119                 } else {
120                         node->typeinfo= node_get_type(ntree, node->type, (bNodeTree *)node->id, NULL);
121                 }
122
123                 if(node->typeinfo==NULL) {
124                         printf("Error: Node type %s doesn't exist anymore, removed\n", node->name);
125                         nodeFreeNode(ntree, node);
126                 }
127         }
128                         
129         ntree->init |= NTREE_TYPE_INIT;
130 }
131
132 /* updates node with (modified) bNodeType.. this should be done for all trees */
133 void ntreeUpdateType(bNodeTree *ntree, bNodeType *ntype)
134 {
135         bNode *node;
136
137         for(node= ntree->nodes.first; node; node= node->next) {
138                 if(node->typeinfo== ntype) {
139                         nodeUpdateType(ntree, node, ntype);
140                 }
141         }
142 }
143
144 /* only used internal... we depend on type definitions! */
145 static bNodeSocket *node_add_socket_type(ListBase *lb, bNodeSocketType *stype)
146 {
147         bNodeSocket *sock= MEM_callocN(sizeof(bNodeSocket), "sock");
148         
149         BLI_strncpy(sock->name, stype->name, NODE_MAXSTR);
150         if(stype->limit==0) sock->limit= 0xFFF;
151         else sock->limit= stype->limit;
152         sock->type= stype->type;
153         
154         sock->to_index= stype->own_index;
155         sock->tosock= stype->internsock;
156         
157         sock->ns.vec[0]= stype->val1;
158         sock->ns.vec[1]= stype->val2;
159         sock->ns.vec[2]= stype->val3;
160         sock->ns.vec[3]= stype->val4;
161         sock->ns.min= stype->min;
162         sock->ns.max= stype->max;
163         
164         if(lb)
165                 BLI_addtail(lb, sock);
166
167         return sock;
168 }
169
170 static void node_rem_socket(bNodeTree *ntree, ListBase *lb, bNodeSocket *sock)
171 {
172         bNodeLink *link, *next;
173         
174         for(link= ntree->links.first; link; link= next) {
175                 next= link->next;
176                 if(link->fromsock==sock || link->tosock==sock) {
177                         nodeRemLink(ntree, link);
178                 }
179         }
180         
181         BLI_remlink(lb, sock);
182         MEM_freeN(sock);
183 }
184
185 static bNodeSocket *verify_socket(ListBase *lb, bNodeSocketType *stype)
186 {
187         bNodeSocket *sock;
188         
189         for(sock= lb->first; sock; sock= sock->next) {
190                 /* both indices are zero for non-groups, otherwise it's a unique index */
191                 if(sock->to_index==stype->own_index)
192                         if(strncmp(sock->name, stype->name, NODE_MAXSTR)==0)
193                                 break;
194         }
195         if(sock) {
196                 sock->type= stype->type;                /* in future, read this from tydefs! */
197                 if(stype->limit==0) sock->limit= 0xFFF;
198                 else sock->limit= stype->limit;
199                 sock->ns.min= stype->min;
200                 sock->ns.max= stype->max;
201                 sock->tosock= stype->internsock;
202                 
203                 BLI_remlink(lb, sock);
204                 
205                 return sock;
206         }
207         else {
208                 return node_add_socket_type(NULL, stype);
209         }
210 }
211
212 static void verify_socket_list(bNodeTree *ntree, ListBase *lb, bNodeSocketType *stype_first)
213 {
214         bNodeSocketType *stype;
215         
216         /* no inputs anymore? */
217         if(stype_first==NULL) {
218                 while(lb->first)
219                         node_rem_socket(ntree, lb, lb->first);
220         }
221         else {
222                 /* step by step compare */
223                 stype= stype_first;
224                 while(stype->type != -1) {
225                         stype->sock= verify_socket(lb, stype);
226                         stype++;
227                 }
228                 /* leftovers are removed */
229                 while(lb->first)
230                         node_rem_socket(ntree, lb, lb->first);
231                 /* and we put back the verified sockets */
232                 stype= stype_first;
233                 while(stype->type != -1) {
234                         BLI_addtail(lb, stype->sock);
235                         stype++;
236                 }
237         }
238 }
239
240 void nodeVerifyType(bNodeTree *ntree, bNode *node)
241 {
242         bNodeType *ntype= node->typeinfo;
243         
244         if(ntype) {
245                 /* might add some other verify stuff here */
246                 
247                 verify_socket_list(ntree, &node->inputs, ntype->inputs);
248                 verify_socket_list(ntree, &node->outputs, ntype->outputs);
249         }
250 }
251
252 void ntreeVerifyTypes(bNodeTree *ntree)
253 {
254         bNode *node;
255         
256         /* if((ntree->init & NTREE_TYPE_INIT)==0) */
257         ntreeInitTypes(ntree);
258
259         /* check inputs and outputs, and remove or insert them */
260         for(node= ntree->nodes.first; node; node= node->next)
261                 nodeVerifyType(ntree, node);
262         
263 }
264
265 /* ************** Group stuff ********** */
266
267 bNodeType node_group_typeinfo= {
268         /* next,prev   */       NULL, NULL,
269         /* type code   */       NODE_GROUP,
270         /* name        */       "Group",
271         /* width+range */       120, 60, 200,
272         /* class+opts  */       NODE_CLASS_GROUP, NODE_OPTIONS,
273         /* input sock  */       NULL,
274         /* output sock */       NULL,
275         /* storage     */       "",
276         /* execfunc    */       NULL,
277         /* butfunc     */       NULL,
278         /* initfunc    */       NULL,
279         /* freestoragefunc    */        NULL,
280         /* copystoragefunc    */        NULL,
281         /* id          */       NULL
282 };
283
284 /* tag internal sockets */
285 static void group_tag_internal_sockets(bNodeTree *ngroup)
286 {
287         bNode *node;
288         bNodeSocket *sock;
289         bNodeLink *link;
290         
291         /* clear intern tag, but check already for hidden sockets */
292         for(node= ngroup->nodes.first; node; node= node->next) {
293                 for(sock= node->inputs.first; sock; sock= sock->next)
294                         sock->intern= sock->flag & SOCK_HIDDEN;
295                 for(sock= node->outputs.first; sock; sock= sock->next)
296                         sock->intern= sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL);
297         }
298         /* set tag */
299         for(link= ngroup->links.first; link; link= link->next) {
300                 link->fromsock->intern= 1;
301                 link->tosock->intern= 1;
302         }
303         
304         /* remove link pointer to external links (only happens on create group) */
305         for(node= ngroup->nodes.first; node; node= node->next) {
306                 for(sock= node->inputs.first; sock; sock= sock->next)
307                         if(sock->intern==0)
308                                 sock->link= NULL;
309         }
310
311         /* set all intern sockets to own_index zero, makes sure that later use won't mixup */
312         for(node= ngroup->nodes.first; node; node= node->next) {
313                 for(sock= node->inputs.first; sock; sock= sock->next)
314                         if(sock->intern)
315                                 sock->own_index= 0;
316                 for(sock= node->outputs.first; sock; sock= sock->next)
317                         if(sock->intern)
318                                 sock->own_index= 0;
319         }
320 }
321
322 /* after editing group, new sockets are zero */
323 /* this routine ensures unique identifiers for zero sockets that are exposed */
324 static void group_verify_own_indices(bNodeTree *ngroup)
325 {
326         bNode *node;
327         bNodeSocket *sock;
328         
329         for(node= ngroup->nodes.first; node; node= node->next) {
330                 for(sock= node->inputs.first; sock; sock= sock->next)
331                         if(sock->own_index==0 && sock->intern==0)
332                                 sock->own_index= ++(ngroup->cur_index);
333                 for(sock= node->outputs.first; sock; sock= sock->next)
334                         if(sock->own_index==0 && sock->intern==0)
335                                 sock->own_index= ++(ngroup->cur_index);
336         }
337         //printf("internal index %d\n", ngroup->cur_index);
338 }
339
340
341 /* nodetrees can be used as groups, so we need typeinfo structs generated */
342 void ntreeMakeOwnType(bNodeTree *ngroup)
343 {
344         bNode *node;
345         bNodeSocket *sock;
346         int totin= 0, totout=0, a;
347
348         /* tags socket when internal linked */
349         group_tag_internal_sockets(ngroup);
350         
351         /* ensure all sockets have own unique id */
352         group_verify_own_indices(ngroup);
353         
354         /* counting stats */
355         for(node= ngroup->nodes.first; node; node= node->next) {
356                 if(node->type==NODE_GROUP)
357                         break;
358                 for(sock= node->inputs.first; sock; sock= sock->next)
359                         if(sock->intern==0) 
360                                 totin++;
361                 for(sock= node->outputs.first; sock; sock= sock->next)
362                         if(sock->intern==0) 
363                                 totout++;
364         }
365         /* debug: nodetrees in nodetrees not handled yet */
366         if(node) {
367                 printf("group in group, not supported yet\n");
368                 return;
369         }
370         
371         /* free own type struct */
372         if(ngroup->owntype) {
373                 if(ngroup->owntype->inputs)
374                         MEM_freeN(ngroup->owntype->inputs);
375                 if(ngroup->owntype->outputs)
376                         MEM_freeN(ngroup->owntype->outputs);
377                 MEM_freeN(ngroup->owntype);
378         }
379         
380         /* make own type struct */
381         ngroup->owntype= MEM_callocN(sizeof(bNodeType), "group type");
382         *ngroup->owntype= node_group_typeinfo; /* copy data, for init */
383         
384         /* input type arrays */
385         if(totin) {
386                 bNodeSocketType *stype;
387                 bNodeSocketType *inputs= MEM_callocN(sizeof(bNodeSocketType)*(totin+1), "bNodeSocketType");
388                 a= 0;
389                 
390                 for(node= ngroup->nodes.first; node; node= node->next) {
391                         /* nodes are presumed fully verified, stype and socket list are in sync */
392                         stype= node->typeinfo->inputs;
393                         for(sock= node->inputs.first; sock; sock= sock->next, stype++) {
394                                 if(sock->intern==0) {
395                                         /* debug only print */
396                                         if(stype==NULL || stype->type==-1) printf("group verification error %s\n", ngroup->id.name);
397                                         
398                                         inputs[a]= *stype;
399                                         inputs[a].own_index= sock->own_index;
400                                         inputs[a].internsock= sock;     
401                                         a++;
402                                 }
403                         }
404                 }
405                 inputs[a].type= -1;     /* terminator code */
406                 ngroup->owntype->inputs= inputs;
407         }       
408         
409         /* output type arrays */
410         if(totout) {
411                 bNodeSocketType *stype;
412                 bNodeSocketType *outputs= MEM_callocN(sizeof(bNodeSocketType)*(totout+1), "bNodeSocketType");
413                 a= 0;
414                 
415                 for(node= ngroup->nodes.first; node; node= node->next) {
416                         /* nodes are presumed fully verified, stype and socket list are in sync */
417                         stype= node->typeinfo->outputs;
418                         for(sock= node->outputs.first; sock; sock= sock->next, stype++) {
419                                 if(sock->intern==0) {
420                                         /* debug only print */
421                                         if(stype==NULL || stype->type==-1) printf("group verification error %s\n", ngroup->id.name);
422                                         
423                                         outputs[a]= *stype;
424                                         outputs[a].own_index= sock->own_index;
425                                         outputs[a].internsock= sock;    
426                                         a++;
427                                 }
428                         }
429                 }
430                 outputs[a].type= -1;    /* terminator code */
431                 ngroup->owntype->outputs= outputs;
432         }
433         
434         /* voila, the nodetree has the full definition for generating group-node instances! */
435 }
436
437
438 static bNodeSocket *groupnode_find_tosock(bNode *gnode, int index)
439 {
440         bNodeSocket *sock;
441         
442         for(sock= gnode->inputs.first; sock; sock= sock->next)
443                 if(sock->to_index==index)
444                         return sock;
445         return NULL;
446 }
447
448 static bNodeSocket *groupnode_find_fromsock(bNode *gnode, int index)
449 {
450         bNodeSocket *sock;
451         
452         for(sock= gnode->outputs.first; sock; sock= sock->next)
453                 if(sock->to_index==index)
454                         return sock;
455         return NULL;
456 }
457
458 bNode *nodeMakeGroupFromSelected(bNodeTree *ntree)
459 {
460         bNodeLink *link, *linkn;
461         bNode *node, *gnode, *nextn;
462         bNodeSocket *sock;
463         bNodeTree *ngroup;
464         ListBase anim_basepaths = {NULL, NULL};
465         float min[2], max[2];
466         int totnode=0;
467         
468         INIT_MINMAX2(min, max);
469         
470         /* is there something to group? also do some clearing */
471         for(node= ntree->nodes.first; node; node= node->next) {
472                 if(node->flag & NODE_SELECT) {
473                         /* no groups in groups */
474                         if(node->type==NODE_GROUP)
475                                 return NULL;
476                         DO_MINMAX2( (&node->locx), min, max);
477                         totnode++;
478                 }
479                 node->done= 0;
480         }
481         if(totnode==0) return NULL;
482         
483         /* check if all connections are OK, no unselected node has both
484                 inputs and outputs to a selection */
485         for(link= ntree->links.first; link; link= link->next) {
486                 if(link->fromnode->flag & NODE_SELECT)
487                         link->tonode->done |= 1;
488                 if(link->tonode->flag & NODE_SELECT)
489                         link->fromnode->done |= 2;
490         }       
491         
492         for(node= ntree->nodes.first; node; node= node->next) {
493                 if((node->flag & NODE_SELECT)==0)
494                         if(node->done==3)
495                                 break;
496         }
497         if(node) 
498                 return NULL;
499         
500         /* OK! new nodetree */
501         ngroup= ntreeAddTree("NodeGroup", ntree->type, TRUE);
502         
503         /* move nodes over */
504         for(node= ntree->nodes.first; node; node= nextn) {
505                 nextn= node->next;
506                 if(node->flag & NODE_SELECT) {
507                         /* keep track of this node's RNA "base" path (the part of the pat identifying the node) 
508                          * if the old nodetree has animation data which potentially covers this node
509                          */
510                         if (ntree->adt) {
511                                 PointerRNA ptr;
512                                 char *path;
513                                 
514                                 RNA_pointer_create(&ntree->id, &RNA_Node, node, &ptr);
515                                 path = RNA_path_from_ID_to_struct(&ptr);
516                                 
517                                 if (path)
518                                         BLI_addtail(&anim_basepaths, BLI_genericNodeN(path));
519                         }
520                         
521                         /* change node-collection membership */
522                         BLI_remlink(&ntree->nodes, node);
523                         BLI_addtail(&ngroup->nodes, node);
524                         
525                         node->locx-= 0.5f*(min[0]+max[0]);
526                         node->locy-= 0.5f*(min[1]+max[1]);
527                         
528                         /* set socket own_index to zero since it can still have a value
529                          * from being in a group before, otherwise it doesn't get a unique
530                          * index in group_verify_own_indices */
531                         for(sock= node->inputs.first; sock; sock= sock->next)
532                                 sock->own_index= 0;
533                         for(sock= node->outputs.first; sock; sock= sock->next)
534                                 sock->own_index= 0;
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         /* move animation data over */
548         if (ntree->adt) {
549                 LinkData *ld, *ldn=NULL;
550                 
551                 BKE_animdata_separate_by_basepath(&ntree->id, &ngroup->id, &anim_basepaths);
552                 
553                 /* paths + their wrappers need to be freed */
554                 for (ld = anim_basepaths.first; ld; ld = ldn) {
555                         ldn = ld->next;
556                         
557                         MEM_freeN(ld->data);
558                         BLI_freelinkN(&anim_basepaths, ld);
559                 }
560         }
561         
562         /* now we can make own group typeinfo */
563         ntreeMakeOwnType(ngroup);
564         
565         /* make group node */
566         gnode= nodeAddNodeType(ntree, NODE_GROUP, ngroup, NULL);
567         gnode->locx= 0.5f*(min[0]+max[0]);
568         gnode->locy= 0.5f*(min[1]+max[1]);
569         
570         /* relink external sockets */
571         for(link= ntree->links.first; link; link= linkn) {
572                 linkn= link->next;
573                 
574                 if(link->tonode->flag & NODE_SELECT) {
575                         link->tonode= gnode;
576                         sock= groupnode_find_tosock(gnode, link->tosock->own_index);
577                         if(sock==NULL) {
578                                 nodeRemLink(ntree, link);       
579                                 printf("Removed link, cannot mix internal and external sockets in group\n");
580                         }
581                         else link->tosock= sock;
582                 }
583                 else if(link->fromnode->flag & NODE_SELECT) {
584                         link->fromnode= gnode;
585                         sock= groupnode_find_fromsock(gnode, link->fromsock->own_index);
586                         if(sock==NULL) {
587                                 nodeRemLink(ntree, link);       
588                                 printf("Removed link, cannot mix internal and external sockets in group\n");
589                         }
590                         else link->fromsock= sock;
591                 }
592         }
593         
594         /* initialize variables of unused input sockets */
595         for(node= ngroup->nodes.first; node; node= node->next) {
596                 for(sock= node->inputs.first; sock; sock= sock->next) {
597                         if(sock->intern==0) {
598                                 bNodeSocket *nsock= groupnode_find_tosock(gnode, sock->own_index);
599                                 if(nsock) {
600                                         QUATCOPY(nsock->ns.vec, sock->ns.vec);
601                                 }
602                         }
603                 }
604         }
605
606         /* update node levels */
607         ntreeSolveOrder(ntree);
608
609         return gnode;
610 }
611
612 /* note: ungroup: group_indices zero! */
613
614 /* here's a nasty little one, need to check users... */
615 /* should become callbackable... */
616 void nodeVerifyGroup(bNodeTree *ngroup)
617 {
618         
619         /* group changed, so we rebuild the type definition */
620         ntreeMakeOwnType(ngroup);
621         
622         if(ngroup->type==NTREE_SHADER) {
623                 Material *ma;
624                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
625                         if(ma->nodetree) {
626                                 bNode *node;
627                                 
628                                 /* find if group is in tree */
629                                 for(node= ma->nodetree->nodes.first; node; node= node->next)
630                                         if(node->id == (ID *)ngroup)
631                                                 break;
632                                 
633                                 if(node) {
634                                         /* set all type pointers OK */
635                                         ntreeInitTypes(ma->nodetree);
636                                         
637                                         for(node= ma->nodetree->nodes.first; node; node= node->next)
638                                                 if(node->id == (ID *)ngroup)
639                                                         nodeVerifyType(ma->nodetree, node);
640                                 }
641                         }
642                 }
643         }
644         else if(ngroup->type==NTREE_COMPOSIT) {
645                 Scene *sce;
646                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
647                         if(sce->nodetree) {
648                                 bNode *node;
649                                 
650                                 /* find if group is in tree */
651                                 for(node= sce->nodetree->nodes.first; node; node= node->next)
652                                         if(node->id == (ID *)ngroup)
653                                                 break;
654                                 
655                                 if(node) {
656                                         /* set all type pointers OK */
657                                         ntreeInitTypes(sce->nodetree);
658                                         
659                                         for(node= sce->nodetree->nodes.first; node; node= node->next)
660                                                 if(node->id == (ID *)ngroup)
661                                                         nodeVerifyType(sce->nodetree, node);
662                                 }
663                         }
664                 }
665         }
666         else if(ngroup->type==NTREE_TEXTURE) {
667                 Tex *tx;
668                 for(tx= G.main->tex.first; tx; tx= tx->id.next) {
669                         if(tx->nodetree) {
670                                 bNode *node;
671                                 
672                                 /* find if group is in tree */
673                                 for(node= tx->nodetree->nodes.first; node; node= node->next)
674                                         if(node->id == (ID *)ngroup)
675                                                 break;
676                                 
677                                 if(node) {
678                                         /* set all type pointers OK */
679                                         ntreeInitTypes(tx->nodetree);
680                                         
681                                         for(node= tx->nodetree->nodes.first; node; node= node->next)
682                                                 if(node->id == (ID *)ngroup)
683                                                         nodeVerifyType(tx->nodetree, node);
684                                 }
685                         }
686                 }
687         }
688 }
689
690 /* also to check all users of groups. Now only used in editor for hide/unhide */
691 /* should become callbackable? */
692 void nodeGroupSocketUseFlags(bNodeTree *ngroup)
693 {
694         bNode *node;
695         bNodeSocket *sock;
696
697         /* clear flags */
698         for(node= ngroup->nodes.first; node; node= node->next) {
699                 for(sock= node->inputs.first; sock; sock= sock->next)
700                         sock->flag &= ~SOCK_IN_USE;
701                 for(sock= node->outputs.first; sock; sock= sock->next)
702                         sock->flag &= ~SOCK_IN_USE;
703         }
704         
705         /* tag all thats in use */
706         if(ngroup->type==NTREE_SHADER) {
707                 Material *ma;
708                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
709                         if(ma->nodetree) {
710                                 for(node= ma->nodetree->nodes.first; node; node= node->next) {
711                                         if(node->id==(ID *)ngroup) {
712                                                 for(sock= node->inputs.first; sock; sock= sock->next)
713                                                         if(sock->link)
714                                                                 if(sock->tosock) 
715                                                                         sock->tosock->flag |= SOCK_IN_USE;
716                                                 for(sock= node->outputs.first; sock; sock= sock->next)
717                                                         if(nodeCountSocketLinks(ma->nodetree, sock))
718                                                                 if(sock->tosock) 
719                                                                         sock->tosock->flag |= SOCK_IN_USE;
720                                         }
721                                 }
722                         }
723                 }
724         }
725         else if(ngroup->type==NTREE_COMPOSIT) {
726                 Scene *sce;
727                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
728                         if(sce->nodetree) {
729                                 for(node= sce->nodetree->nodes.first; node; node= node->next) {
730                                         if(node->id==(ID *)ngroup) {
731                                                 for(sock= node->inputs.first; sock; sock= sock->next)
732                                                         if(sock->link)
733                                                                 if(sock->tosock) 
734                                                                         sock->tosock->flag |= SOCK_IN_USE;
735                                                 for(sock= node->outputs.first; sock; sock= sock->next)
736                                                         if(nodeCountSocketLinks(sce->nodetree, sock))
737                                                                 if(sock->tosock) 
738                                                                         sock->tosock->flag |= SOCK_IN_USE;
739                                         }
740                                 }
741                         }
742                 }
743         }
744         else if(ngroup->type==NTREE_TEXTURE) {
745                 Tex *tx;
746                 for(tx= G.main->tex.first; tx; tx= tx->id.next) {
747                         if(tx->nodetree) {
748                                 for(node= tx->nodetree->nodes.first; node; node= node->next) {
749                                         if(node->id==(ID *)ngroup) {
750                                                 for(sock= node->inputs.first; sock; sock= sock->next)
751                                                         if(sock->link)
752                                                                 if(sock->tosock) 
753                                                                         sock->tosock->flag |= SOCK_IN_USE;
754                                                 for(sock= node->outputs.first; sock; sock= sock->next)
755                                                         if(nodeCountSocketLinks(tx->nodetree, sock))
756                                                                 if(sock->tosock) 
757                                                                         sock->tosock->flag |= SOCK_IN_USE;
758                                         }
759                                 }
760                         }
761                 }
762         }
763         
764 }
765 /* finds a node based on its name */
766 bNode *nodeFindNodebyName(bNodeTree *ntree, const char *name)
767 {
768         return BLI_findstring(&ntree->nodes, name, offsetof(bNode, name));
769 }
770
771 /* finds a node based on given socket */
772 int nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **nodep, int *sockindex)
773 {
774         bNode *node;
775         bNodeSocket *tsock;
776         int index= 0;
777         
778         for(node= ntree->nodes.first; node; node= node->next) {
779                 for(index=0, tsock= node->inputs.first; tsock; tsock= tsock->next, index++)
780                         if(tsock==sock)
781                                 break;
782                 if(tsock)
783                         break;
784                 for(index=0, tsock= node->outputs.first; tsock; tsock= tsock->next, index++)
785                         if(tsock==sock)
786                                 break;
787                 if(tsock)
788                         break;
789         }
790
791         if(node) {
792                 *nodep= node;
793                 if(sockindex) *sockindex= index;
794                 return 1;
795         }
796         
797         *nodep= NULL;
798         return 0;
799 }
800
801 /* returns 1 if its OK */
802 int nodeGroupUnGroup(bNodeTree *ntree, bNode *gnode)
803 {
804         bNodeLink *link, *linkn;
805         bNode *node, *nextn;
806         bNodeTree *ngroup, *wgroup;
807         ListBase anim_basepaths = {NULL, NULL};
808         int index;
809         
810         ngroup= (bNodeTree *)gnode->id;
811         if(ngroup==NULL) return 0;
812         
813         /* clear new pointers, set in copytree */
814         for(node= ntree->nodes.first; node; node= node->next)
815                 node->new_node= NULL;
816         
817         /* wgroup is a temporary copy of the NodeTree we're merging in
818          *      - all of wgroup's nodes are transferred across to their new home
819          *      - ngroup (i.e. the source NodeTree) is left unscathed
820          */
821         wgroup= ntreeCopyTree(ngroup, 0);
822         
823         /* add the nodes into the ntree */
824         for(node= wgroup->nodes.first; node; node= nextn) {
825                 nextn= node->next;
826                 
827                 /* keep track of this node's RNA "base" path (the part of the pat identifying the node) 
828                  * if the old nodetree has animation data which potentially covers this node
829                  */
830                 if (wgroup->adt) {
831                         PointerRNA ptr;
832                         char *path;
833                         
834                         RNA_pointer_create(&wgroup->id, &RNA_Node, node, &ptr);
835                         path = RNA_path_from_ID_to_struct(&ptr);
836                         
837                         if (path)
838                                 BLI_addtail(&anim_basepaths, BLI_genericNodeN(path));
839                 }
840                 
841                 /* migrate node */
842                 BLI_remlink(&wgroup->nodes, node);
843                 BLI_addtail(&ntree->nodes, node);
844                 
845                 node->locx+= gnode->locx;
846                 node->locy+= gnode->locy;
847                 
848                 node->flag |= NODE_SELECT;
849         }
850         /* and the internal links */
851         for(link= wgroup->links.first; link; link= linkn) {
852                 linkn= link->next;
853                 BLI_remlink(&wgroup->links, link);
854                 BLI_addtail(&ntree->links, link);
855         }
856         
857         /* and copy across the animation */
858         if (wgroup->adt) {
859                 LinkData *ld, *ldn=NULL;
860                 bAction *waction;
861                 
862                 /* firstly, wgroup needs to temporary dummy action that can be destroyed, as it shares copies */
863                 waction = wgroup->adt->action = copy_action(wgroup->adt->action);
864                 
865                 /* now perform the moving */
866                 BKE_animdata_separate_by_basepath(&wgroup->id, &ntree->id, &anim_basepaths);
867                 
868                 /* paths + their wrappers need to be freed */
869                 for (ld = anim_basepaths.first; ld; ld = ldn) {
870                         ldn = ld->next;
871                         
872                         MEM_freeN(ld->data);
873                         BLI_freelinkN(&anim_basepaths, ld);
874                 }
875                 
876                 /* free temp action too */
877                 free_libblock(&G.main->action, waction);
878         }
879
880         /* restore links to and from the gnode */
881         for(link= ntree->links.first; link; link= link->next) {
882                 if(link->tonode==gnode) {
883                         /* link->tosock->tosock is on the node we look for */
884                         nodeFindNode(ngroup, link->tosock->tosock, &nextn, &index);
885                         if(nextn==NULL) printf("wrong stuff!\n");
886                         else if(nextn->new_node==NULL) printf("wrong stuff too!\n");
887                         else {
888                                 link->tonode= nextn->new_node;
889                                 link->tosock= BLI_findlink(&link->tonode->inputs, index);
890                         }
891                 }
892                 else if(link->fromnode==gnode) {
893                         /* link->fromsock->tosock is on the node we look for */
894                         nodeFindNode(ngroup, link->fromsock->tosock, &nextn, &index);
895                         if(nextn==NULL) printf("1 wrong stuff!\n");
896                         else if(nextn->new_node==NULL) printf("1 wrong stuff too!\n");
897                         else {
898                                 link->fromnode= nextn->new_node;
899                                 link->fromsock= BLI_findlink(&link->fromnode->outputs, index);
900                         }
901                 }
902         }
903         
904         /* remove the gnode & work tree */
905         free_libblock(&G.main->nodetree, wgroup);
906         
907         nodeFreeNode(ntree, gnode);
908         
909         /* solve order goes fine, but the level tags not... doing it twice works for now. solve this once */
910         ntreeSolveOrder(ntree);
911         ntreeSolveOrder(ntree);
912
913         return 1;
914 }
915
916 void nodeCopyGroup(bNode *gnode)
917 {
918         bNodeSocket *sock;
919
920         gnode->id->us--;
921         gnode->id= (ID *)ntreeCopyTree((bNodeTree *)gnode->id, 0);
922
923         /* new_sock was set in nodeCopyNode */
924         for(sock=gnode->inputs.first; sock; sock=sock->next)
925                 if(sock->tosock)
926                         sock->tosock= sock->tosock->new_sock;
927
928         for(sock=gnode->outputs.first; sock; sock=sock->next)
929                 if(sock->tosock)
930                         sock->tosock= sock->tosock->new_sock;
931 }
932
933 /* ************** Add stuff ********** */
934 void nodeAddSockets(bNode *node, bNodeType *ntype)
935 {
936         bNodeSocketType *stype;
937
938         if(ntype->inputs) {
939                 stype= ntype->inputs;
940                 while(stype->type != -1) {
941                         node_add_socket_type(&node->inputs, stype);
942                         stype++;
943                 }
944         }
945         if(ntype->outputs) {
946                 stype= ntype->outputs;
947                 while(stype->type != -1) {
948                         node_add_socket_type(&node->outputs, stype);
949                         stype++;
950                 }
951         }
952 }
953 /* Find the first available, non-duplicate name for a given node */
954 void nodeUniqueName(bNodeTree *ntree, bNode *node)
955 {
956         BLI_uniquename(&ntree->nodes, node, "Node", '.', offsetof(bNode, name), sizeof(node->name));
957 }
958
959 bNode *nodeAddNodeType(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
960 {
961         bNode *node= NULL;
962         bNodeType *ntype= NULL;
963
964         if (ngroup && BLI_findindex(&G.main->nodetree, ngroup)==-1) {
965                 printf("nodeAddNodeType() error: '%s' not in main->nodetree\n", ngroup->id.name);
966                 return NULL;
967         }
968
969         if(type>=NODE_DYNAMIC_MENU) {
970                 int a=0, idx= type-NODE_DYNAMIC_MENU;
971                 ntype= ntree->alltypes.first;
972                 while(ntype) {
973                         if(ntype->type==NODE_DYNAMIC) {
974                                 if(a==idx)
975                                         break;
976                                 a++;
977                         }
978                         ntype= ntype->next;
979                 }
980         } else
981                 ntype= node_get_type(ntree, type, ngroup, id);
982
983         node= MEM_callocN(sizeof(bNode), "new node");
984         BLI_addtail(&ntree->nodes, node);
985         node->typeinfo= ntype;
986         if(type>=NODE_DYNAMIC_MENU)
987                 node->custom2= type; /* for node_dynamic_init */
988
989         if(ngroup)
990                 BLI_strncpy(node->name, ngroup->id.name+2, NODE_MAXSTR);
991         else if(type>NODE_DYNAMIC_MENU) {
992                 BLI_strncpy(node->name, ntype->id->name+2, NODE_MAXSTR);
993         }
994         else
995                 BLI_strncpy(node->name, ntype->name, NODE_MAXSTR);
996
997         nodeUniqueName(ntree, node);
998         
999         node->type= ntype->type;
1000         node->flag= NODE_SELECT|ntype->flag;
1001         node->width= ntype->width;
1002         node->miniwidth= 42.0f;         /* small value only, allows print of first chars */
1003
1004         if(type==NODE_GROUP)
1005                 node->id= (ID *)ngroup;
1006
1007         /* need init handler later? */
1008         /* got it-bob*/
1009         if(ntype->initfunc!=NULL)
1010                 ntype->initfunc(node);
1011         
1012         nodeAddSockets(node, ntype);
1013         
1014         return node;
1015 }
1016
1017 void nodeMakeDynamicType(bNode *node)
1018 {
1019         /* find SH_DYNAMIC_NODE ntype */
1020         bNodeType *ntype= node_all_shaders.first;
1021         while(ntype) {
1022                 if(ntype->type==NODE_DYNAMIC && ntype->id==NULL)
1023                         break;
1024                 ntype= ntype->next;
1025         }
1026
1027         /* make own type struct to fill */
1028         if(ntype) {
1029                 /*node->typeinfo= MEM_dupallocN(ntype);*/
1030                 bNodeType *newtype= MEM_callocN(sizeof(bNodeType), "dynamic bNodeType");
1031                 *newtype= *ntype;
1032                 newtype->name= BLI_strdup(ntype->name);
1033                 node->typeinfo= newtype;
1034         }
1035 }
1036
1037 void nodeUpdateType(bNodeTree *ntree, bNode* node, bNodeType *ntype)
1038 {
1039         verify_socket_list(ntree, &node->inputs, ntype->inputs);
1040         verify_socket_list(ntree, &node->outputs, ntype->outputs);
1041 }
1042
1043 /* keep socket listorder identical, for copying links */
1044 /* ntree is the target tree */
1045 bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
1046 {
1047         bNode *nnode= MEM_callocN(sizeof(bNode), "dupli node");
1048         bNodeSocket *sock, *oldsock;
1049
1050         *nnode= *node;
1051         nodeUniqueName(ntree, nnode);
1052         
1053         BLI_addtail(&ntree->nodes, nnode);
1054         
1055         BLI_duplicatelist(&nnode->inputs, &node->inputs);
1056         oldsock= node->inputs.first;
1057         for(sock= nnode->inputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
1058                 oldsock->new_sock= sock;
1059                 if(internal)
1060                         sock->own_index= 0;
1061         }
1062         
1063         BLI_duplicatelist(&nnode->outputs, &node->outputs);
1064         oldsock= node->outputs.first;
1065         for(sock= nnode->outputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
1066                 if(internal)
1067                         sock->own_index= 0;
1068                 sock->stack_index= 0;
1069                 sock->ns.data= NULL;
1070                 oldsock->new_sock= sock;
1071         }
1072         
1073         /* don't increase node->id users, freenode doesn't decrement either */
1074         
1075         if(node->typeinfo->copystoragefunc)
1076                 node->typeinfo->copystoragefunc(node, nnode);
1077         
1078         node->new_node= nnode;
1079         nnode->new_node= NULL;
1080         nnode->preview= NULL;
1081         
1082         return nnode;
1083 }
1084
1085 bNodeLink *nodeAddLink(bNodeTree *ntree, bNode *fromnode, bNodeSocket *fromsock, bNode *tonode, bNodeSocket *tosock)
1086 {
1087         bNodeLink *link= MEM_callocN(sizeof(bNodeLink), "link");
1088         
1089         BLI_addtail(&ntree->links, link);
1090         link->fromnode= fromnode;
1091         link->fromsock= fromsock;
1092         link->tonode= tonode;
1093         link->tosock= tosock;
1094         
1095         return link;
1096 }
1097
1098 void nodeRemLink(bNodeTree *ntree, bNodeLink *link)
1099 {
1100         BLI_remlink(&ntree->links, link);
1101         if(link->tosock)
1102                 link->tosock->link= NULL;
1103         MEM_freeN(link);
1104 }
1105
1106 void nodeRemSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1107 {
1108         bNodeLink *link, *next;
1109         
1110         for(link= ntree->links.first; link; link= next) {
1111                 next= link->next;
1112                 if(link->fromsock==sock || link->tosock==sock) {
1113                         nodeRemLink(ntree, link);
1114                 }
1115         }
1116 }
1117
1118
1119 bNodeTree *ntreeAddTree(const char *name, int type, const short is_group)
1120 {
1121         bNodeTree *ntree;
1122
1123         if (is_group)
1124                 ntree= alloc_libblock(&G.main->nodetree, ID_NT, name);
1125         else {
1126                 ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
1127                 *( (short *)ntree->id.name )= ID_NT; /* not "type", as that is ntree->type */
1128                 BLI_strncpy(ntree->id.name+2, name, sizeof(ntree->id.name));
1129         }
1130
1131         ntree->type= type;
1132         ntree->alltypes.first = NULL;
1133         ntree->alltypes.last = NULL;
1134
1135         ntreeInitTypes(ntree);
1136         return ntree;
1137 }
1138
1139 /* Warning: this function gets called during some rather unexpected times
1140  *      - internal_select is only 1 when used for duplicating selected nodes (i.e. Shift-D duplicate operator)
1141  *      - this gets called when executing compositing updates (for threaded previews)
1142  *      - when the nodetree datablock needs to be copied (i.e. when users get copied)
1143  *      - for scene duplication use ntreeSwapID() after so we dont have stale pointers.
1144  */
1145 bNodeTree *ntreeCopyTree(bNodeTree *ntree, int internal_select)
1146 {
1147         bNodeTree *newtree;
1148         bNode *node, *nnode, *last;
1149         bNodeLink *link, *nlink;
1150         bNodeSocket *sock;
1151         int a;
1152         
1153         if(ntree==NULL) return NULL;
1154         
1155         if(internal_select==0) {
1156                 /* is ntree part of library? */
1157                 for(newtree=G.main->nodetree.first; newtree; newtree= newtree->id.next)
1158                         if(newtree==ntree) break;
1159                 if(newtree) {
1160                         newtree= copy_libblock(ntree);
1161                 } else {
1162                         newtree= MEM_dupallocN(ntree);
1163                         copy_libblock_data(&newtree->id, &ntree->id, TRUE); /* copy animdata and ID props */
1164                 }
1165                 newtree->nodes.first= newtree->nodes.last= NULL;
1166                 newtree->links.first= newtree->links.last= NULL;
1167         }
1168         else
1169                 newtree= ntree;
1170         
1171         last= ntree->nodes.last;
1172         for(node= ntree->nodes.first; node; node= node->next) {
1173                 
1174                 node->new_node= NULL;
1175                 if(internal_select==0 || (node->flag & NODE_SELECT)) {
1176                         nnode= nodeCopyNode(newtree, node, internal_select);    /* sets node->new */
1177                         if(internal_select) {
1178                                 node->flag &= ~(NODE_SELECT|NODE_ACTIVE);
1179                                 nnode->flag |= NODE_SELECT;
1180                         }
1181                 }
1182                 if(node==last) break;
1183         }
1184         
1185         /* check for copying links */
1186         for(link= ntree->links.first; link; link= link->next) {
1187                 if(link->fromnode==NULL || link->tonode==NULL);
1188                 else if(link->fromnode->new_node && link->tonode->new_node) {
1189                         nlink= nodeAddLink(newtree, link->fromnode->new_node, NULL, link->tonode->new_node, NULL);
1190                         /* sockets were copied in order */
1191                         for(a=0, sock= link->fromnode->outputs.first; sock; sock= sock->next, a++) {
1192                                 if(sock==link->fromsock)
1193                                         break;
1194                         }
1195                         nlink->fromsock= BLI_findlink(&link->fromnode->new_node->outputs, a);
1196                         
1197                         for(a=0, sock= link->tonode->inputs.first; sock; sock= sock->next, a++) {
1198                                 if(sock==link->tosock)
1199                                         break;
1200                         }
1201                         nlink->tosock= BLI_findlink(&link->tonode->new_node->inputs, a);
1202                 }
1203         }
1204         
1205         /* own type definition for group usage */
1206         if(internal_select==0) {
1207                 if(ntree->owntype) {
1208                         newtree->owntype= MEM_dupallocN(ntree->owntype);
1209                         if(ntree->owntype->inputs)
1210                                 newtree->owntype->inputs= MEM_dupallocN(ntree->owntype->inputs);
1211                         if(ntree->owntype->outputs)
1212                                 newtree->owntype->outputs= MEM_dupallocN(ntree->owntype->outputs);
1213                 }
1214         }
1215         /* weird this is required... there seem to be link pointers wrong still? */
1216         /* anyhoo, doing this solves crashes on copying entire tree (copy scene) and delete nodes */
1217         ntreeSolveOrder(newtree);
1218
1219         return newtree;
1220 }
1221
1222 /* use when duplicating scenes */
1223 void ntreeSwitchID(bNodeTree *ntree, ID *id_from, ID *id_to)
1224 {
1225         bNode *node;
1226         /* for scene duplication only */
1227         for(node= ntree->nodes.first; node; node= node->next) {
1228                 if(node->id==id_from) {
1229                         node->id= id_to;
1230                 }
1231         }
1232 }
1233
1234 /* *************** preview *********** */
1235 /* if node->preview, then we assume the rect to exist */
1236
1237 static void node_free_preview(bNode *node)
1238 {
1239         if(node->preview) {
1240                 if(node->preview->rect)
1241                         MEM_freeN(node->preview->rect);
1242                 MEM_freeN(node->preview);
1243                 node->preview= NULL;
1244         }       
1245 }
1246
1247 static void node_init_preview(bNode *node, int xsize, int ysize)
1248 {
1249         
1250         if(node->preview==NULL) {
1251                 node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
1252                 //              printf("added preview %s\n", node->name);
1253         }
1254         
1255         /* node previews can get added with variable size this way */
1256         if(xsize==0 || ysize==0)
1257                 return;
1258         
1259         /* sanity checks & initialize */
1260         if(node->preview->rect) {
1261                 if(node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
1262                         MEM_freeN(node->preview->rect);
1263                         node->preview->rect= NULL;
1264                 }
1265         }
1266         
1267         if(node->preview->rect==NULL) {
1268                 node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(char)*4, "node preview rect");
1269                 node->preview->xsize= xsize;
1270                 node->preview->ysize= ysize;
1271         }
1272         /* no clear, makes nicer previews */
1273 }
1274
1275 void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
1276 {
1277         bNode *node;
1278         
1279         if(ntree==NULL)
1280                 return;
1281         
1282         for(node= ntree->nodes.first; node; node= node->next) {
1283                 if(node->typeinfo->flag & NODE_PREVIEW) /* hrms, check for closed nodes? */
1284                         node_init_preview(node, xsize, ysize);
1285                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
1286                         ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
1287         }               
1288 }
1289
1290 static void nodeClearPreview(bNode *node)
1291 {
1292         if(node->preview && node->preview->rect)
1293                 memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
1294 }
1295
1296 /* use it to enforce clear */
1297 void ntreeClearPreview(bNodeTree *ntree)
1298 {
1299         bNode *node;
1300         
1301         if(ntree==NULL)
1302                 return;
1303         
1304         for(node= ntree->nodes.first; node; node= node->next) {
1305                 if(node->typeinfo->flag & NODE_PREVIEW)
1306                         nodeClearPreview(node);
1307                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
1308                         ntreeClearPreview((bNodeTree *)node->id);
1309         }               
1310 }
1311
1312 /* hack warning! this function is only used for shader previews, and 
1313 since it gets called multiple times per pixel for Ztransp we only
1314 add the color once. Preview gets cleared before it starts render though */
1315 void nodeAddToPreview(bNode *node, float *col, int x, int y)
1316 {
1317         bNodePreview *preview= node->preview;
1318         if(preview) {
1319                 if(x>=0 && y>=0) {
1320                         if(x<preview->xsize && y<preview->ysize) {
1321                                 unsigned char *tar= preview->rect+ 4*((preview->xsize*y) + x);
1322                                 
1323                                 if(TRUE) {
1324                                         tar[0]= FTOCHAR(linearrgb_to_srgb(col[0]));
1325                                         tar[1]= FTOCHAR(linearrgb_to_srgb(col[1]));
1326                                         tar[2]= FTOCHAR(linearrgb_to_srgb(col[2]));
1327                                 }
1328                                 else {
1329                                         tar[0]= FTOCHAR(col[0]);
1330                                         tar[1]= FTOCHAR(col[1]);
1331                                         tar[2]= FTOCHAR(col[2]);
1332                                 }
1333                                 tar[3]= FTOCHAR(col[3]);
1334                         }
1335                         //else printf("prv out bound x y %d %d\n", x, y);
1336                 }
1337                 //else printf("prv out bound x y %d %d\n", x, y);
1338         }
1339 }
1340
1341
1342 /* ************** Free stuff ********** */
1343
1344 /* goes over entire tree */
1345 void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
1346 {
1347         bNodeLink *link, *next;
1348         bNodeSocket *sock;
1349         ListBase *lb;
1350         
1351         for(link= ntree->links.first; link; link= next) {
1352                 next= link->next;
1353                 
1354                 if(link->fromnode==node) {
1355                         lb= &node->outputs;
1356                         NodeTagChanged(ntree, link->tonode);
1357                 }
1358                 else if(link->tonode==node)
1359                         lb= &node->inputs;
1360                 else
1361                         lb= NULL;
1362
1363                 if(lb) {
1364                         for(sock= lb->first; sock; sock= sock->next) {
1365                                 if(link->fromsock==sock || link->tosock==sock)
1366                                         break;
1367                         }
1368                         if(sock) {
1369                                 nodeRemLink(ntree, link);
1370                         }
1371                 }
1372         }
1373 }
1374
1375 static void composit_free_node_cache(bNode *node)
1376 {
1377         bNodeSocket *sock;
1378         
1379         for(sock= node->outputs.first; sock; sock= sock->next) {
1380                 if(sock->ns.data) {
1381                         free_compbuf(sock->ns.data);
1382                         sock->ns.data= NULL;
1383                 }
1384         }
1385 }
1386
1387 void nodeFreeNode(bNodeTree *ntree, bNode *node)
1388 {
1389         nodeUnlinkNode(ntree, node);
1390         BLI_remlink(&ntree->nodes, node);
1391
1392         /* since it is called while free database, node->id is undefined */
1393         
1394         if(ntree->type==NTREE_COMPOSIT)
1395                 composit_free_node_cache(node);
1396         BLI_freelistN(&node->inputs);
1397         BLI_freelistN(&node->outputs);
1398         
1399         node_free_preview(node);
1400
1401         if(node->typeinfo && node->typeinfo->freestoragefunc) {
1402                 node->typeinfo->freestoragefunc(node);
1403         }
1404
1405         MEM_freeN(node);
1406 }
1407
1408 /* do not free ntree itself here, free_libblock calls this function too */
1409 void ntreeFreeTree(bNodeTree *ntree)
1410 {
1411         bNode *node, *next;
1412         
1413         if(ntree==NULL) return;
1414         
1415         ntreeEndExecTree(ntree);        /* checks for if it is still initialized */
1416         
1417         BKE_free_animdata((ID *)ntree);
1418
1419         BLI_freelistN(&ntree->links);   /* do first, then unlink_node goes fast */
1420         
1421         for(node= ntree->nodes.first; node; node= next) {
1422                 next= node->next;
1423                 nodeFreeNode(ntree, node);
1424         }
1425         
1426         if(ntree->owntype) {
1427                 if(ntree->owntype->inputs)
1428                         MEM_freeN(ntree->owntype->inputs);
1429                 if(ntree->owntype->outputs)
1430                         MEM_freeN(ntree->owntype->outputs);
1431                 MEM_freeN(ntree->owntype);
1432         }
1433 }
1434
1435 void ntreeFreeCache(bNodeTree *ntree)
1436 {
1437         bNode *node;
1438         
1439         if(ntree==NULL) return;
1440
1441         if(ntree->type==NTREE_COMPOSIT)
1442                 for(node= ntree->nodes.first; node; node= node->next)
1443                         composit_free_node_cache(node);
1444
1445 }
1446
1447 void ntreeMakeLocal(bNodeTree *ntree)
1448 {
1449         int local=0, lib=0;
1450         
1451         /* - only lib users: do nothing
1452                 * - only local users: set flag
1453                 * - mixed: make copy
1454                 */
1455         
1456         if(ntree->id.lib==NULL) return;
1457         if(ntree->id.us==1) {
1458                 ntree->id.lib= 0;
1459                 ntree->id.flag= LIB_LOCAL;
1460                 new_id(0, (ID *)ntree, 0);
1461                 return;
1462         }
1463         
1464         /* now check users of groups... again typedepending, callback... */
1465         if(ntree->type==NTREE_SHADER) {
1466                 Material *ma;
1467                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
1468                         if(ma->nodetree) {
1469                                 bNode *node;
1470                                 
1471                                 /* find if group is in tree */
1472                                 for(node= ma->nodetree->nodes.first; node; node= node->next) {
1473                                         if(node->id == (ID *)ntree) {
1474                                                 if(ma->id.lib) lib= 1;
1475                                                 else local= 1;
1476                                         }
1477                                 }
1478                         }
1479                 }
1480         }
1481         else if(ntree->type==NTREE_COMPOSIT) {
1482                 Scene *sce;
1483                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
1484                         if(sce->nodetree) {
1485                                 bNode *node;
1486                                 
1487                                 /* find if group is in tree */
1488                                 for(node= sce->nodetree->nodes.first; node; node= node->next) {
1489                                         if(node->id == (ID *)ntree) {
1490                                                 if(sce->id.lib) lib= 1;
1491                                                 else local= 1;
1492                                         }
1493                                 }
1494                         }
1495                 }
1496         }
1497         else if(ntree->type==NTREE_TEXTURE) {
1498                 Tex *tx;
1499                 for(tx= G.main->tex.first; tx; tx= tx->id.next) {
1500                         if(tx->nodetree) {
1501                                 bNode *node;
1502                                 
1503                                 /* find if group is in tree */
1504                                 for(node= tx->nodetree->nodes.first; node; node= node->next) {
1505                                         if(node->id == (ID *)ntree) {
1506                                                 if(tx->id.lib) lib= 1;
1507                                                 else local= 1;
1508                                         }
1509                                 }
1510                         }
1511                 }
1512         }
1513         
1514         /* if all users are local, we simply make tree local */
1515         if(local && lib==0) {
1516                 ntree->id.lib= NULL;
1517                 ntree->id.flag= LIB_LOCAL;
1518                 new_id(0, (ID *)ntree, 0);
1519         }
1520         else if(local && lib) {
1521                 /* this is the mixed case, we copy the tree and assign it to local users */
1522                 bNodeTree *newtree= ntreeCopyTree(ntree, 0);
1523                 
1524                 newtree->id.us= 0;
1525                 
1526                 if(ntree->type==NTREE_SHADER) {
1527                         Material *ma;
1528                         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
1529                                 if(ma->nodetree) {
1530                                         bNode *node;
1531                                         
1532                                         /* find if group is in tree */
1533                                         for(node= ma->nodetree->nodes.first; node; node= node->next) {
1534                                                 if(node->id == (ID *)ntree) {
1535                                                         if(ma->id.lib==NULL) {
1536                                                                 node->id= &newtree->id;
1537                                                                 newtree->id.us++;
1538                                                                 ntree->id.us--;
1539                                                         }
1540                                                 }
1541                                         }
1542                                 }
1543                         }
1544                 }
1545                 else if(ntree->type==NTREE_COMPOSIT) {
1546                         Scene *sce;
1547                         for(sce= G.main->scene.first; sce; sce= sce->id.next) {
1548                                 if(sce->nodetree) {
1549                                         bNode *node;
1550                                         
1551                                         /* find if group is in tree */
1552                                         for(node= sce->nodetree->nodes.first; node; node= node->next) {
1553                                                 if(node->id == (ID *)ntree) {
1554                                                         if(sce->id.lib==NULL) {
1555                                                                 node->id= &newtree->id;
1556                                                                 newtree->id.us++;
1557                                                                 ntree->id.us--;
1558                                                         }
1559                                                 }
1560                                         }
1561                                 }
1562                         }
1563                 }
1564                 else if(ntree->type==NTREE_TEXTURE) {
1565                         Tex *tx;
1566                         for(tx= G.main->tex.first; tx; tx= tx->id.next) {
1567                                 if(tx->nodetree) {
1568                                         bNode *node;
1569                                         
1570                                         /* find if group is in tree */
1571                                         for(node= tx->nodetree->nodes.first; node; node= node->next) {
1572                                                 if(node->id == (ID *)ntree) {
1573                                                         if(tx->id.lib==NULL) {
1574                                                                 node->id= &newtree->id;
1575                                                                 newtree->id.us++;
1576                                                                 ntree->id.us--;
1577                                                         }
1578                                                 }
1579                                         }
1580                                 }
1581                         }
1582                 }
1583         }
1584 }
1585
1586
1587 /* ************ find stuff *************** */
1588
1589 static int ntreeHasType(bNodeTree *ntree, int type)
1590 {
1591         bNode *node;
1592         
1593         if(ntree)
1594                 for(node= ntree->nodes.first; node; node= node->next)
1595                         if(node->type == type)
1596                                 return 1;
1597         return 0;
1598 }
1599
1600 bNodeLink *nodeFindLink(bNodeTree *ntree, bNodeSocket *from, bNodeSocket *to)
1601 {
1602         bNodeLink *link;
1603         
1604         for(link= ntree->links.first; link; link= link->next) {
1605                 if(link->fromsock==from && link->tosock==to)
1606                         return link;
1607                 if(link->fromsock==to && link->tosock==from)    /* hrms? */
1608                         return link;
1609         }
1610         return NULL;
1611 }
1612
1613 int nodeCountSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1614 {
1615         bNodeLink *link;
1616         int tot= 0;
1617         
1618         for(link= ntree->links.first; link; link= link->next) {
1619                 if(link->fromsock==sock || link->tosock==sock)
1620                         tot++;
1621         }
1622         return tot;
1623 }
1624
1625 bNode *nodeGetActive(bNodeTree *ntree)
1626 {
1627         bNode *node;
1628         
1629         if(ntree==NULL) return NULL;
1630         
1631         for(node= ntree->nodes.first; node; node= node->next)
1632                 if(node->flag & NODE_ACTIVE)
1633                         break;
1634         return node;
1635 }
1636
1637 /* two active flags, ID nodes have special flag for buttons display */
1638 bNode *nodeGetActiveID(bNodeTree *ntree, short idtype)
1639 {
1640         bNode *node;
1641         
1642         if(ntree==NULL) return NULL;
1643
1644         /* check for group edit */
1645         for(node= ntree->nodes.first; node; node= node->next)
1646                 if(node->flag & NODE_GROUP_EDIT)
1647                         break;
1648
1649         if(node)
1650                 ntree= (bNodeTree*)node->id;
1651         
1652         /* now find active node with this id */
1653         for(node= ntree->nodes.first; node; node= node->next)
1654                 if(node->id && GS(node->id->name)==idtype)
1655                         if(node->flag & NODE_ACTIVE_ID)
1656                                 break;
1657
1658         return node;
1659 }
1660
1661 int nodeSetActiveID(bNodeTree *ntree, short idtype, ID *id)
1662 {
1663         bNode *node;
1664         int ok= FALSE;
1665
1666         if(ntree==NULL) return ok;
1667
1668         /* check for group edit */
1669         for(node= ntree->nodes.first; node; node= node->next)
1670                 if(node->flag & NODE_GROUP_EDIT)
1671                         break;
1672
1673         if(node)
1674                 ntree= (bNodeTree*)node->id;
1675
1676         /* now find active node with this id */
1677         for(node= ntree->nodes.first; node; node= node->next) {
1678                 if(node->id && GS(node->id->name)==idtype) {
1679                         if(id && ok==FALSE && node->id==id) {
1680                                 node->flag |= NODE_ACTIVE_ID;
1681                                 ok= TRUE;
1682                         } else {
1683                                 node->flag &= ~NODE_ACTIVE_ID;
1684                         }
1685                 }
1686         }
1687
1688         return ok;
1689 }
1690
1691
1692 /* two active flags, ID nodes have special flag for buttons display */
1693 void nodeClearActiveID(bNodeTree *ntree, short idtype)
1694 {
1695         bNode *node;
1696         
1697         if(ntree==NULL) return;
1698         
1699         for(node= ntree->nodes.first; node; node= node->next)
1700                 if(node->id && GS(node->id->name)==idtype)
1701                         node->flag &= ~NODE_ACTIVE_ID;
1702 }
1703
1704 /* two active flags, ID nodes have special flag for buttons display */
1705 void nodeSetActive(bNodeTree *ntree, bNode *node)
1706 {
1707         bNode *tnode;
1708         
1709         /* make sure only one node is active, and only one per ID type */
1710         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1711                 tnode->flag &= ~NODE_ACTIVE;
1712                 
1713                 if(node->id && tnode->id) {
1714                         if(GS(node->id->name) == GS(tnode->id->name))
1715                                 tnode->flag &= ~NODE_ACTIVE_ID;
1716                 }
1717         }
1718         
1719         node->flag |= NODE_ACTIVE;
1720         if(node->id)
1721                 node->flag |= NODE_ACTIVE_ID;
1722 }
1723
1724 /* use flags are not persistant yet, groups might need different tagging, so we do it each time
1725    when we need to get this info */
1726 void ntreeSocketUseFlags(bNodeTree *ntree)
1727 {
1728         bNode *node;
1729         bNodeSocket *sock;
1730         bNodeLink *link;
1731         
1732         /* clear flags */
1733         for(node= ntree->nodes.first; node; node= node->next) {
1734                 for(sock= node->inputs.first; sock; sock= sock->next)
1735                         sock->flag &= ~SOCK_IN_USE;
1736                 for(sock= node->outputs.first; sock; sock= sock->next)
1737                         sock->flag &= ~SOCK_IN_USE;
1738         }
1739         
1740         /* tag all thats in use */
1741         for(link= ntree->links.first; link; link= link->next) {
1742         
1743                 if(link->fromsock) // FIXME, see below
1744                         link->fromsock->flag |= SOCK_IN_USE;
1745                 if(link->tosock) // FIXME This can be NULL, when dragging a new link in the UI, should probably copy the node tree for preview render - campbell
1746                         link->tosock->flag |= SOCK_IN_USE;
1747         }
1748 }
1749
1750 /* ************** dependency stuff *********** */
1751
1752 /* node is guaranteed to be not checked before */
1753 static int node_recurs_check(bNode *node, bNode ***nsort, int level)
1754 {
1755         bNode *fromnode;
1756         bNodeSocket *sock;
1757         int has_inputlinks= 0;
1758         
1759         node->done= 1;
1760         level++;
1761         
1762         for(sock= node->inputs.first; sock; sock= sock->next) {
1763                 if(sock->link) {
1764                         has_inputlinks= 1;
1765                         fromnode= sock->link->fromnode;
1766                         if(fromnode->done==0) {
1767                                 fromnode->level= node_recurs_check(fromnode, nsort, level);
1768                         }
1769                 }
1770         }
1771 //      printf("node sort %s level %d\n", node->name, level);
1772         **nsort= node;
1773         (*nsort)++;
1774         
1775         if(has_inputlinks)
1776                 return level;
1777         else 
1778                 return 0xFFF;
1779 }
1780
1781
1782 static void ntreeSetOutput(bNodeTree *ntree)
1783 {
1784         bNode *node;
1785
1786         /* find the active outputs, might become tree type dependant handler */
1787         for(node= ntree->nodes.first; node; node= node->next) {
1788                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1789                         bNode *tnode;
1790                         int output= 0;
1791                         
1792                         /* we need a check for which output node should be tagged like this, below an exception */
1793                         if(node->type==CMP_NODE_OUTPUT_FILE)
1794                            continue;
1795                            
1796                         /* there is more types having output class, each one is checked */
1797                         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1798                                 if(tnode->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1799                                         
1800                                         if(ntree->type==NTREE_COMPOSIT) {
1801                                                         
1802                                                 /* same type, exception for viewer */
1803                                                 if(tnode->type==node->type ||
1804                                                    (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) &&
1805                                                         ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))) {
1806                                                         if(tnode->flag & NODE_DO_OUTPUT) {
1807                                                                 output++;
1808                                                                 if(output>1)
1809                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
1810                                                         }
1811                                                 }
1812                                         }
1813                                         else {
1814                                                 /* same type */
1815                                                 if(tnode->type==node->type) {
1816                                                         if(tnode->flag & NODE_DO_OUTPUT) {
1817                                                                 output++;
1818                                                                 if(output>1)
1819                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
1820                                                         }
1821                                                 }
1822                                         }
1823                                 }
1824                         }
1825                         if(output==0)
1826                                 node->flag |= NODE_DO_OUTPUT;
1827                 }
1828         }
1829         
1830         /* here we could recursively set which nodes have to be done,
1831                 might be different for editor or for "real" use... */
1832 }
1833
1834 void ntreeSolveOrder(bNodeTree *ntree)
1835 {
1836         bNode *node, **nodesort, **nsort;
1837         bNodeSocket *sock;
1838         bNodeLink *link;
1839         int a, totnode=0;
1840         
1841         /* the solve-order is called on each tree change, so we should be sure no exec can be running */
1842         ntreeEndExecTree(ntree);
1843
1844         /* set links pointers the input sockets, to find dependencies */
1845         /* first clear data */
1846         for(node= ntree->nodes.first; node; node= node->next) {
1847                 node->done= 0;
1848                 totnode++;
1849                 for(sock= node->inputs.first; sock; sock= sock->next)
1850                         sock->link= NULL;
1851         }
1852         if(totnode==0)
1853                 return;
1854         
1855         for(link= ntree->links.first; link; link= link->next) {
1856                 link->tosock->link= link;
1857         }
1858         
1859         nsort= nodesort= MEM_callocN(totnode*sizeof(void *), "sorted node array");
1860         
1861         /* recursive check */
1862         for(node= ntree->nodes.first; node; node= node->next) {
1863                 if(node->done==0) {
1864                         node->level= node_recurs_check(node, &nsort, 0);
1865                 }
1866         }
1867         
1868         /* re-insert nodes in order, first a paranoia check */
1869         for(a=0; a<totnode; a++) {
1870                 if(nodesort[a]==NULL)
1871                         break;
1872         }
1873         if(a<totnode)
1874                 printf("sort error in node tree");
1875         else {
1876                 ntree->nodes.first= ntree->nodes.last= NULL;
1877                 for(a=0; a<totnode; a++)
1878                         BLI_addtail(&ntree->nodes, nodesort[a]);
1879         }
1880         
1881         MEM_freeN(nodesort);
1882
1883         ntreeSetOutput(ntree);
1884 }
1885
1886
1887 /* Should be callback! */
1888 /* Do not call execs here */
1889 void NodeTagChanged(bNodeTree *ntree, bNode *node)
1890 {
1891         if(ntree->type==NTREE_COMPOSIT) {
1892                 bNodeSocket *sock;
1893
1894                 for(sock= node->outputs.first; sock; sock= sock->next) {
1895                         if(sock->ns.data) {
1896                                 //free_compbuf(sock->ns.data);
1897                                 //sock->ns.data= NULL;
1898                         }
1899                 }
1900                 node->need_exec= 1;
1901         }
1902 }
1903
1904 int NodeTagIDChanged(bNodeTree *ntree, ID *id)
1905 {
1906         int change = FALSE;
1907
1908         if(ELEM(NULL, id, ntree))
1909                 return change;
1910         
1911         if(ntree->type==NTREE_COMPOSIT) {
1912                 bNode *node;
1913                 
1914                 for(node= ntree->nodes.first; node; node= node->next) {
1915                         if(node->id==id) {
1916                                 change= TRUE;
1917                                 NodeTagChanged(ntree, node);
1918                         }
1919                 }
1920         }
1921         
1922         return change;
1923 }
1924
1925
1926
1927 /* ******************* executing ************* */
1928
1929 /* see notes at ntreeBeginExecTree */
1930 static void group_node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out, bNodeStack **gin, bNodeStack **gout)
1931 {
1932         bNodeSocket *sock;
1933         
1934         /* build pointer stack */
1935         for(sock= node->inputs.first; sock; sock= sock->next) {
1936                 if(sock->intern) {
1937                         /* yep, intern can have link or is hidden socket */
1938                         if(sock->link)
1939                                 *(in++)= stack + sock->link->fromsock->stack_index;
1940                         else
1941                                 *(in++)= &sock->ns;
1942                 }
1943                 else
1944                         *(in++)= gin[sock->stack_index_ext];
1945         }
1946         
1947         for(sock= node->outputs.first; sock; sock= sock->next) {
1948                 if(sock->intern)
1949                         *(out++)= stack + sock->stack_index;
1950                 else
1951                         *(out++)= gout[sock->stack_index_ext];
1952         }
1953 }
1954
1955 static void node_group_execute(bNodeStack *stack, void *data, bNode *gnode, bNodeStack **in, bNodeStack **out)
1956 {
1957         bNode *node;
1958         bNodeTree *ntree= (bNodeTree *)gnode->id;
1959         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
1960         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
1961         
1962         if(ntree==NULL) return;
1963         
1964         stack+= gnode->stack_index;
1965                 
1966         for(node= ntree->nodes.first; node; node= node->next) {
1967                 if(node->typeinfo->execfunc) {
1968                         group_node_get_stack(node, stack, nsin, nsout, in, out);
1969                         
1970                         /* for groups, only execute outputs for edited group */
1971                         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1972                                 if(node->type==CMP_NODE_OUTPUT_FILE || (gnode->flag & NODE_GROUP_EDIT))
1973                                         node->typeinfo->execfunc(data, node, nsin, nsout);
1974                         }
1975                         else
1976                                 node->typeinfo->execfunc(data, node, nsin, nsout);
1977                 }
1978         }
1979         
1980         /* free internal group output nodes */
1981         if(ntree->type==NTREE_COMPOSIT) {
1982                 for(node= ntree->nodes.first; node; node= node->next) {
1983                         if(node->typeinfo->execfunc) {
1984                                 bNodeSocket *sock;
1985                                 
1986                                 for(sock= node->outputs.first; sock; sock= sock->next) {
1987                                         if(sock->intern) {
1988                                                 bNodeStack *ns= stack + sock->stack_index;
1989                                                 if(ns->data) {
1990                                                         free_compbuf(ns->data);
1991                                                         ns->data= NULL;
1992                                                 }
1993                                         }
1994                                 }
1995                         }
1996                 }
1997         }
1998 }
1999
2000 /* recursively called for groups */
2001 /* we set all trees on own local indices, but put a total counter
2002    in the groups, so each instance of a group has own stack */
2003 static int ntree_begin_exec_tree(bNodeTree *ntree)
2004 {
2005         bNode *node;
2006         bNodeSocket *sock;
2007         int index= 0, index_in= 0, index_out= 0;
2008         
2009         if((ntree->init & NTREE_TYPE_INIT)==0)
2010                 ntreeInitTypes(ntree);
2011         
2012         /* create indices for stack, check preview */
2013         for(node= ntree->nodes.first; node; node= node->next) {
2014                 
2015                 for(sock= node->inputs.first; sock; sock= sock->next) {
2016                         if(sock->intern==0)
2017                                 sock->stack_index_ext= index_in++;
2018                 }
2019                 
2020                 for(sock= node->outputs.first; sock; sock= sock->next) {
2021                         sock->stack_index= index++;
2022                         if(sock->intern==0)
2023                                 sock->stack_index_ext= index_out++;
2024                 }
2025                 
2026                 if(node->type==NODE_GROUP) {
2027                         if(node->id) {
2028                                 node->stack_index= index;
2029                                 index+= ntree_begin_exec_tree((bNodeTree *)node->id);
2030                         }
2031                 }
2032         }
2033         
2034         return index;
2035 }
2036
2037 /* copy socket compbufs to stack, initialize usage of curve nodes */
2038 static void composit_begin_exec(bNodeTree *ntree, int is_group)
2039 {
2040         bNode *node;
2041         bNodeSocket *sock;
2042         
2043         for(node= ntree->nodes.first; node; node= node->next) {
2044                 
2045                 /* initialize needed for groups */
2046                 node->exec= 0;  
2047                 
2048                 if(is_group==0) {
2049                         for(sock= node->outputs.first; sock; sock= sock->next) {
2050                                 bNodeStack *ns= ntree->stack + sock->stack_index;
2051                                 
2052                                 if(sock->ns.data) {
2053                                         ns->data= sock->ns.data;
2054                                         sock->ns.data= NULL;
2055                                 }
2056                         }
2057                 }
2058                 /* cannot initialize them while using in threads */
2059                 if(ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT)) {
2060                         curvemapping_initialize(node->storage);
2061                         if(node->type==CMP_NODE_CURVE_RGB)
2062                                 curvemapping_premultiply(node->storage, 0);
2063                 }
2064                 if(node->type==NODE_GROUP)
2065                         composit_begin_exec((bNodeTree *)node->id, 1);
2066
2067         }
2068 }
2069
2070 /* copy stack compbufs to sockets */
2071 static void composit_end_exec(bNodeTree *ntree, int is_group)
2072 {
2073         bNode *node;
2074         bNodeStack *ns;
2075         int a;
2076
2077         for(node= ntree->nodes.first; node; node= node->next) {
2078                 if(is_group==0) {
2079                         bNodeSocket *sock;
2080                 
2081                         for(sock= node->outputs.first; sock; sock= sock->next) {
2082                                 ns= ntree->stack + sock->stack_index;
2083                                 if(ns->data) {
2084                                         sock->ns.data= ns->data;
2085                                         ns->data= NULL;
2086                                 }
2087                         }
2088                 }
2089                 if(node->type==CMP_NODE_CURVE_RGB)
2090                         curvemapping_premultiply(node->storage, 1);
2091                 
2092                 if(node->type==NODE_GROUP)
2093                         composit_end_exec((bNodeTree *)node->id, 1);
2094
2095                 node->need_exec= 0;
2096         }
2097         
2098         if(is_group==0) {
2099                 /* internally, group buffers are not stored */
2100                 for(ns= ntree->stack, a=0; a<ntree->stacksize; a++, ns++) {
2101                         if(ns->data) {
2102                                 printf("freed leftover buffer from stack\n");
2103                                 free_compbuf(ns->data);
2104                                 ns->data= NULL;
2105                         }
2106                 }
2107         }
2108 }
2109
2110 static void group_tag_used_outputs(bNode *gnode, bNodeStack *stack)
2111 {
2112         bNodeTree *ntree= (bNodeTree *)gnode->id;
2113         bNode *node;
2114         
2115         stack+= gnode->stack_index;
2116         
2117         for(node= ntree->nodes.first; node; node= node->next) {
2118                 if(node->typeinfo->execfunc) {
2119                         bNodeSocket *sock;
2120                         
2121                         for(sock= node->inputs.first; sock; sock= sock->next) {
2122                                 if(sock->intern) {
2123                                         if(sock->link) {
2124                                                 bNodeStack *ns= stack + sock->link->fromsock->stack_index;
2125                                                 ns->hasoutput= 1;
2126                                                 ns->sockettype= sock->link->fromsock->type;
2127                                         }
2128                                         else
2129                                                 sock->ns.sockettype= sock->type;
2130                                 }
2131                         }
2132                 }
2133         }
2134 }
2135
2136 /* notes below are ancient! (ton) */
2137 /* stack indices make sure all nodes only write in allocated data, for making it thread safe */
2138 /* only root tree gets the stack, to enable instances to have own stack entries */
2139 /* per tree (and per group) unique indices are created */
2140 /* the index_ext we need to be able to map from groups to the group-node own stack */
2141
2142 typedef struct bNodeThreadStack {
2143         struct bNodeThreadStack *next, *prev;
2144         bNodeStack *stack;
2145         int used;
2146 } bNodeThreadStack;
2147
2148 static bNodeThreadStack *ntreeGetThreadStack(bNodeTree *ntree, int thread)
2149 {
2150         ListBase *lb= &ntree->threadstack[thread];
2151         bNodeThreadStack *nts;
2152         
2153         for(nts=lb->first; nts; nts=nts->next) {
2154                 if(!nts->used) {
2155                         nts->used= 1;
2156                         return nts;
2157                 }
2158         }
2159         nts= MEM_callocN(sizeof(bNodeThreadStack), "bNodeThreadStack");
2160         nts->stack= MEM_dupallocN(ntree->stack);
2161         nts->used= 1;
2162         BLI_addtail(lb, nts);
2163
2164         return nts;
2165 }
2166
2167 static void ntreeReleaseThreadStack(bNodeThreadStack *nts)
2168 {
2169         nts->used= 0;
2170 }
2171
2172 /* free texture delegates */
2173 static void tex_end_exec(bNodeTree *ntree)
2174 {
2175         bNodeThreadStack *nts;
2176         bNodeStack *ns;
2177         int th, a;
2178         
2179         if(ntree->threadstack)
2180                 for(th=0; th<BLENDER_MAX_THREADS; th++)
2181                         for(nts=ntree->threadstack[th].first; nts; nts=nts->next)
2182                                 for(ns= nts->stack, a=0; a<ntree->stacksize; a++, ns++)
2183                                         if(ns->data)
2184                                                 MEM_freeN(ns->data);
2185                                                 
2186 }
2187
2188 void ntreeBeginExecTree(bNodeTree *ntree)
2189 {
2190         /* let's make it sure */
2191         if(ntree->init & NTREE_EXEC_INIT)
2192                 return;
2193         
2194         /* allocate the thread stack listbase array */
2195         if(ntree->type!=NTREE_COMPOSIT)
2196                 ntree->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
2197
2198         /* goes recursive over all groups */
2199         ntree->stacksize= ntree_begin_exec_tree(ntree);
2200
2201         if(ntree->stacksize) {
2202                 bNode *node;
2203                 bNodeStack *ns;
2204                 int a;
2205                 
2206                 /* allocate the base stack */
2207                 ns=ntree->stack= MEM_callocN(ntree->stacksize*sizeof(bNodeStack), "node stack");
2208                 
2209                 /* tag inputs, the get_stack() gives own socket stackdata if not in use */
2210                 for(a=0; a<ntree->stacksize; a++, ns++) ns->hasinput= 1;
2211                 
2212                 /* tag used outputs, so we know when we can skip operations */
2213                 for(node= ntree->nodes.first; node; node= node->next) {
2214                         bNodeSocket *sock;
2215                         
2216                         /* composite has own need_exec tag handling */
2217                         if(ntree->type!=NTREE_COMPOSIT)
2218                                 node->need_exec= 1;
2219
2220                         for(sock= node->inputs.first; sock; sock= sock->next) {
2221                                 if(sock->link) {
2222                                         ns= ntree->stack + sock->link->fromsock->stack_index;
2223                                         ns->hasoutput= 1;
2224                                         ns->sockettype= sock->link->fromsock->type;
2225                                 }
2226                                 else
2227                                         sock->ns.sockettype= sock->type;
2228                                 
2229                                 if(sock->link) {
2230                                         bNodeLink *link= sock->link;
2231                                         /* this is the test for a cyclic case */
2232                                         if(link->fromnode && link->tonode) {
2233                                                 if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF);
2234                                                 else {
2235                                                         node->need_exec= 0;
2236                                                 }
2237                                         }
2238                                 }
2239                         }
2240                         
2241                         if(node->type==NODE_GROUP && node->id)
2242                                 group_tag_used_outputs(node, ntree->stack);
2243                         
2244                 }
2245                 
2246                 if(ntree->type==NTREE_COMPOSIT)
2247                         composit_begin_exec(ntree, 0);
2248         }
2249         
2250         ntree->init |= NTREE_EXEC_INIT;
2251 }
2252
2253 void ntreeEndExecTree(bNodeTree *ntree)
2254 {
2255         
2256         if(ntree->init & NTREE_EXEC_INIT) {
2257                 bNodeThreadStack *nts;
2258                 int a;
2259                 
2260                 /* another callback candidate! */
2261                 if(ntree->type==NTREE_COMPOSIT)
2262                         composit_end_exec(ntree, 0);
2263                 else if(ntree->type==NTREE_TEXTURE)
2264                         tex_end_exec(ntree);
2265                 
2266                 if(ntree->stack) {
2267                         MEM_freeN(ntree->stack);
2268                         ntree->stack= NULL;
2269                 }
2270
2271                 if(ntree->threadstack) {
2272                         for(a=0; a<BLENDER_MAX_THREADS; a++) {
2273                                 for(nts=ntree->threadstack[a].first; nts; nts=nts->next)
2274                                         if (nts->stack) MEM_freeN(nts->stack);
2275                                 BLI_freelistN(&ntree->threadstack[a]);
2276                         }
2277
2278                         MEM_freeN(ntree->threadstack);
2279                         ntree->threadstack= NULL;
2280                 }
2281
2282                 ntree->init &= ~NTREE_EXEC_INIT;
2283         }
2284 }
2285
2286 static void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
2287 {
2288         bNodeSocket *sock;
2289         
2290         /* build pointer stack */
2291         for(sock= node->inputs.first; sock; sock= sock->next) {
2292                 if(sock->link)
2293                         *(in++)= stack + sock->link->fromsock->stack_index;
2294                 else
2295                         *(in++)= &sock->ns;
2296         }
2297         
2298         for(sock= node->outputs.first; sock; sock= sock->next) {
2299                 *(out++)= stack + sock->stack_index;
2300         }
2301 }
2302
2303 /* nodes are presorted, so exec is in order of list */
2304 void ntreeExecTree(bNodeTree *ntree, void *callerdata, int thread)
2305 {
2306         bNode *node;
2307         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2308         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2309         bNodeStack *stack;
2310         bNodeThreadStack *nts = NULL;
2311         
2312         /* only when initialized */
2313         if((ntree->init & NTREE_EXEC_INIT)==0)
2314                 ntreeBeginExecTree(ntree);
2315                 
2316         /* composite does 1 node per thread, so no multiple stacks needed */
2317         if(ntree->type==NTREE_COMPOSIT) {
2318                 stack= ntree->stack;
2319         }
2320         else {
2321                 nts= ntreeGetThreadStack(ntree, thread);
2322                 stack= nts->stack;
2323         }
2324         
2325         for(node= ntree->nodes.first; node; node= node->next) {
2326                 if(node->need_exec) {
2327                         if(node->typeinfo->execfunc) {
2328                                 node_get_stack(node, stack, nsin, nsout);
2329                                 node->typeinfo->execfunc(callerdata, node, nsin, nsout);
2330                         }
2331                         else if(node->type==NODE_GROUP && node->id) {
2332                                 node_get_stack(node, stack, nsin, nsout);
2333                                 node_group_execute(stack, callerdata, node, nsin, nsout); 
2334                         }
2335                 }
2336         }
2337
2338         if(nts)
2339                 ntreeReleaseThreadStack(nts);
2340 }
2341
2342
2343 /* ***************************** threaded version for execute composite nodes ************* */
2344 /* these are nodes without input, only giving values */
2345 /* or nodes with only value inputs */
2346 static int node_only_value(bNode *node)
2347 {
2348         bNodeSocket *sock;
2349         
2350         if(ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_VALUE, CMP_NODE_RGB))
2351                 return 1;
2352         
2353         /* doing this for all node types goes wrong. memory free errors */
2354         if(node->inputs.first && node->type==CMP_NODE_MAP_VALUE) {
2355                 int retval= 1;
2356                 for(sock= node->inputs.first; sock; sock= sock->next) {
2357                         if(sock->link)
2358                                 retval &= node_only_value(sock->link->fromnode);
2359                 }
2360                 return retval;
2361         }
2362         return 0;
2363 }
2364
2365
2366 /* not changing info, for thread callback */
2367 typedef struct ThreadData {
2368         bNodeStack *stack;
2369         RenderData *rd;
2370 } ThreadData;
2371
2372 static void *exec_composite_node(void *node_v)
2373 {
2374         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2375         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2376         bNode *node= node_v;
2377         ThreadData *thd= (ThreadData *)node->threaddata;
2378         
2379         node_get_stack(node, thd->stack, nsin, nsout);
2380         
2381         if((node->flag & NODE_MUTED) && (!node_only_value(node))) {
2382                 /* viewers we execute, for feedback to user */
2383                 if(ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
2384                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2385                 else
2386                         node_compo_pass_on(node, nsin, nsout);
2387         }
2388         else if(node->typeinfo->execfunc) {
2389                 node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2390         }
2391         else if(node->type==NODE_GROUP && node->id) {
2392                 node_group_execute(thd->stack, thd->rd, node, nsin, nsout); 
2393         }
2394         
2395         node->exec |= NODE_READY;
2396         return 0;
2397 }
2398
2399 /* return total of executable nodes, for timecursor */
2400 /* only compositor uses it */
2401 static int setExecutableNodes(bNodeTree *ntree, ThreadData *thd)
2402 {
2403         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2404         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2405         bNode *node;
2406         bNodeSocket *sock;
2407         int totnode= 0, group_edit= 0;
2408         
2409         /* note; do not add a dependency sort here, the stack was created already */
2410         
2411         /* if we are in group edit, viewer nodes get skipped when group has viewer */
2412         for(node= ntree->nodes.first; node; node= node->next)
2413                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
2414                         if(ntreeHasType((bNodeTree *)node->id, CMP_NODE_VIEWER))
2415                                 group_edit= 1;
2416         
2417         for(node= ntree->nodes.first; node; node= node->next) {
2418                 int a;
2419                 
2420                 node_get_stack(node, thd->stack, nsin, nsout);
2421                 
2422                 /* test the outputs */
2423                 /* skip value-only nodes (should be in type!) */
2424                 if(!node_only_value(node)) {
2425                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2426                                 if(nsout[a]->data==NULL && nsout[a]->hasoutput) {
2427                                         node->need_exec= 1;
2428                                         break;
2429                                 }
2430                         }
2431                 }
2432                 
2433                 /* test the inputs */
2434                 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
2435                         /* skip viewer nodes in bg render or group edit */
2436                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) && (G.background || group_edit))
2437                                 node->need_exec= 0;
2438                         /* is sock in use? */
2439                         else if(sock->link) {
2440                                 bNodeLink *link= sock->link;
2441                                 
2442                                 /* this is the test for a cyclic case */
2443                                 if(link->fromnode==NULL || link->tonode==NULL);
2444                                 else if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2445                                         if(link->fromnode->need_exec) {
2446                                                 node->need_exec= 1;
2447                                                 break;
2448                                         }
2449                                 }
2450                                 else {
2451                                         node->need_exec= 0;
2452                                         printf("Node %s skipped, cyclic dependency\n", node->name);
2453                                 }
2454                         }
2455                 }
2456                 
2457                 if(node->need_exec) {
2458                         
2459                         /* free output buffers */
2460                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2461                                 if(nsout[a]->data) {
2462                                         free_compbuf(nsout[a]->data);
2463                                         nsout[a]->data= NULL;
2464                                 }
2465                         }
2466                         totnode++;
2467                         /* printf("node needs exec %s\n", node->name); */
2468                         
2469                         /* tag for getExecutableNode() */
2470                         node->exec= 0;
2471                 }
2472                 else {
2473                         /* tag for getExecutableNode() */
2474                         node->exec= NODE_READY|NODE_FINISHED|NODE_SKIPPED;
2475                         
2476                 }
2477         }
2478         
2479         /* last step: set the stack values for only-value nodes */
2480         /* just does all now, compared to a full buffer exec this is nothing */
2481         if(totnode) {
2482                 for(node= ntree->nodes.first; node; node= node->next) {
2483                         if(node->need_exec==0 && node_only_value(node)) {
2484                                 if(node->typeinfo->execfunc) {
2485                                         node_get_stack(node, thd->stack, nsin, nsout);
2486                                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2487                                 }
2488                         }
2489                 }
2490         }
2491         
2492         return totnode;
2493 }
2494
2495 /* while executing tree, free buffers from nodes that are not needed anymore */
2496 static void freeExecutableNode(bNodeTree *ntree)
2497 {
2498         /* node outputs can be freed when:
2499         - not a render result or image node
2500         - when node outputs go to nodes all being set NODE_FINISHED
2501         */
2502         bNode *node;
2503         bNodeSocket *sock;
2504         
2505         /* set exec flag for finished nodes that might need freed */
2506         for(node= ntree->nodes.first; node; node= node->next) {
2507                 if(node->type!=CMP_NODE_R_LAYERS)
2508                         if(node->exec & NODE_FINISHED)
2509                                 node->exec |= NODE_FREEBUFS;
2510         }
2511         /* clear this flag for input links that are not done yet */
2512         for(node= ntree->nodes.first; node; node= node->next) {
2513                 if((node->exec & NODE_FINISHED)==0) {
2514                         for(sock= node->inputs.first; sock; sock= sock->next)
2515                                 if(sock->link)
2516                                         sock->link->fromnode->exec &= ~NODE_FREEBUFS;
2517                 }
2518         }
2519         /* now we can free buffers */
2520         for(node= ntree->nodes.first; node; node= node->next) {
2521                 if(node->exec & NODE_FREEBUFS) {
2522                         for(sock= node->outputs.first; sock; sock= sock->next) {
2523                                 bNodeStack *ns= ntree->stack + sock->stack_index;
2524                                 if(ns->data) {
2525                                         free_compbuf(ns->data);
2526                                         ns->data= NULL;
2527                                         // printf("freed buf node %s \n", node->name);
2528                                 }
2529                         }
2530                 }
2531         }
2532 }
2533
2534 static bNode *getExecutableNode(bNodeTree *ntree)
2535 {
2536         bNode *node;
2537         bNodeSocket *sock;
2538         
2539         for(node= ntree->nodes.first; node; node= node->next) {
2540                 if(node->exec==0) {
2541                         
2542                         /* input sockets should be ready */
2543                         for(sock= node->inputs.first; sock; sock= sock->next) {
2544                                 if(sock->link)
2545                                         if((sock->link->fromnode->exec & NODE_READY)==0)
2546                                                 break;
2547                         }
2548                         if(sock==NULL)
2549                                 return node;
2550                 }
2551         }
2552         return NULL;
2553 }
2554
2555 /* check if texture nodes need exec or end */
2556 static  void ntree_composite_texnode(bNodeTree *ntree, int init)
2557 {
2558         bNode *node;
2559         
2560         for(node= ntree->nodes.first; node; node= node->next) {
2561                 if(node->type==CMP_NODE_TEXTURE && node->id) {
2562                         Tex *tex= (Tex *)node->id;
2563                         if(tex->nodetree && tex->use_nodes) {
2564                                 /* has internal flag to detect it only does it once */
2565                                 if(init)
2566                                         ntreeBeginExecTree(tex->nodetree); 
2567                                 else
2568                                         ntreeEndExecTree(tex->nodetree);
2569                         }
2570                 }
2571         }
2572
2573 }
2574
2575 /* optimized tree execute test for compositing */
2576 void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
2577 {
2578         bNode *node;
2579         ListBase threads;
2580         ThreadData thdata;
2581         int totnode, curnode, rendering= 1;
2582         
2583         if(ntree==NULL) return;
2584         
2585         if(do_preview)
2586                 ntreeInitPreview(ntree, 0, 0);
2587         
2588         ntreeBeginExecTree(ntree);
2589         ntree_composite_texnode(ntree, 1);
2590         
2591         /* prevent unlucky accidents */
2592         if(G.background)
2593                 rd->scemode &= ~R_COMP_CROP;
2594         
2595         /* setup callerdata for thread callback */
2596         thdata.rd= rd;
2597         thdata.stack= ntree->stack;
2598         
2599         /* fixed seed, for example noise texture */
2600         BLI_srandom(rd->cfra);
2601
2602         /* ensures only a single output node is enabled */
2603         ntreeSetOutput(ntree);
2604
2605         /* sets need_exec tags in nodes */
2606         curnode = totnode= setExecutableNodes(ntree, &thdata);
2607
2608         BLI_init_threads(&threads, exec_composite_node, rd->threads);
2609         
2610         while(rendering) {
2611                 
2612                 if(BLI_available_threads(&threads)) {
2613                         node= getExecutableNode(ntree);
2614                         if(node) {
2615                                 
2616                                 if(ntree->progress && totnode)
2617                                         ntree->progress(ntree->prh, (1.0 - curnode/(float)totnode));
2618                                 if(ntree->stats_draw) {
2619                                         char str[64];
2620                                         sprintf(str, "Compositing %d %s", curnode, node->name);
2621                                         ntree->stats_draw(ntree->sdh, str);
2622                                 }
2623                                 curnode--;
2624                                 
2625                                 node->threaddata = &thdata;
2626                                 node->exec= NODE_PROCESSING;
2627                                 BLI_insert_thread(&threads, node);
2628                         }
2629                         else
2630                                 PIL_sleep_ms(50);
2631                 }
2632                 else
2633                         PIL_sleep_ms(50);
2634                 
2635                 rendering= 0;
2636                 /* test for ESC */
2637                 if(ntree->test_break && ntree->test_break(ntree->tbh)) {
2638                         for(node= ntree->nodes.first; node; node= node->next)
2639                                 node->exec |= NODE_READY;
2640                 }
2641                 
2642                 /* check for ready ones, and if we need to continue */
2643                 for(node= ntree->nodes.first; node; node= node->next) {
2644                         if(node->exec & NODE_READY) {
2645                                 if((node->exec & NODE_FINISHED)==0) {
2646                                         BLI_remove_thread(&threads, node); /* this waits for running thread to finish btw */
2647                                         node->exec |= NODE_FINISHED;
2648                                         
2649                                         /* freeing unused buffers */
2650                                         if(rd->scemode & R_COMP_FREE)
2651                                                 freeExecutableNode(ntree);
2652                                 }
2653                         }
2654                         else rendering= 1;
2655                 }
2656         }
2657         
2658         BLI_end_threads(&threads);
2659         
2660         ntreeEndExecTree(ntree);
2661 }
2662
2663
2664 /* ********** copy composite tree entirely, to allow threaded exec ******************* */
2665 /* ***************** do NOT execute this in a thread!               ****************** */
2666
2667 /* returns localized composite tree for execution in threads */
2668 /* local tree then owns all compbufs */
2669 bNodeTree *ntreeLocalize(bNodeTree *ntree)
2670 {
2671         bNodeTree *ltree;
2672         bNode *node;
2673         bNodeSocket *sock;
2674         
2675         bAction *action_backup= NULL, *tmpact_backup= NULL;
2676         
2677         /* Workaround for copying an action on each render!
2678          * set action to NULL so animdata actions dont get copied */
2679         AnimData *adt= BKE_animdata_from_id(&ntree->id);
2680
2681         if(adt) {
2682                 action_backup= adt->action;
2683                 tmpact_backup= adt->tmpact;
2684
2685                 adt->action= NULL;
2686                 adt->tmpact= NULL;
2687         }
2688
2689         /* node copy func */
2690         ltree= ntreeCopyTree(ntree, 0);
2691
2692         if(adt) {
2693                 AnimData *ladt= BKE_animdata_from_id(&ltree->id);
2694
2695                 adt->action= ladt->action= action_backup;
2696                 adt->tmpact= ladt->tmpact= tmpact_backup;
2697
2698                 if(action_backup) action_backup->id.us++;
2699                 if(tmpact_backup) tmpact_backup->id.us++;
2700                 
2701         }
2702         /* end animdata uglyness */
2703
2704         /* ensures only a single output node is enabled */
2705         ntreeSetOutput(ntree);
2706
2707         for(node= ntree->nodes.first; node; node= node->next) {
2708                 
2709                 /* store new_node pointer to original */
2710                 node->new_node->new_node= node;
2711                 /* ensure new user input gets handled ok */
2712                 node->need_exec= 0;
2713                 
2714                 if(ntree->type==NTREE_COMPOSIT) {
2715                         /* move over the compbufs */
2716                         /* right after ntreeCopyTree() oldsock pointers are valid */
2717                         
2718                         if(ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
2719                                 if(node->id) {
2720                                         if(node->flag & NODE_DO_OUTPUT)
2721                                                 node->new_node->id= (ID *)copy_image((Image *)node->id);
2722                                         else
2723                                                 node->new_node->id= NULL;
2724                                 }
2725                         }
2726                         
2727                         for(sock= node->outputs.first; sock; sock= sock->next) {
2728                                 
2729                                 sock->new_sock->ns.data= sock->ns.data;
2730                                 compbuf_set_node(sock->new_sock->ns.data, node->new_node);
2731                                 
2732                                 sock->ns.data= NULL;
2733                                 sock->new_sock->new_sock= sock;
2734                         }
2735                 }
2736         }
2737         
2738         return ltree;
2739 }
2740
2741 static int node_exists(bNodeTree *ntree, bNode *testnode)
2742 {
2743         bNode *node= ntree->nodes.first;
2744         for(; node; node= node->next)
2745                 if(node==testnode)
2746                         return 1;
2747         return 0;
2748 }
2749
2750 static int outsocket_exists(bNode *node, bNodeSocket *testsock)
2751 {
2752         bNodeSocket *sock= node->outputs.first;
2753         for(; sock; sock= sock->next)
2754                 if(sock==testsock)
2755                         return 1;
2756         return 0;
2757 }
2758
2759
2760 /* sync local composite with real tree */
2761 /* local composite is supposed to be running, be careful moving previews! */
2762 /* is called by jobs manager, outside threads, so it doesnt happen during draw */
2763 void ntreeLocalSync(bNodeTree *localtree, bNodeTree *ntree)
2764 {
2765         bNode *lnode;
2766         
2767         if(ntree->type==NTREE_COMPOSIT) {
2768                 /* move over the compbufs and previews */
2769                 for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
2770                         if( (lnode->exec & NODE_READY) && !(lnode->exec & NODE_SKIPPED) ) {
2771                                 if(node_exists(ntree, lnode->new_node)) {
2772                                         
2773                                         if(lnode->preview && lnode->preview->rect) {
2774                                                 node_free_preview(lnode->new_node);
2775                                                 lnode->new_node->preview= lnode->preview;
2776                                                 lnode->preview= NULL;
2777                                         }
2778                                 }
2779                         }
2780                 }
2781         }
2782         else if(ntree->type==NTREE_SHADER) {
2783                 /* copy over contents of previews */
2784                 for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
2785                         if(node_exists(ntree, lnode->new_node)) {
2786                                 bNode *node= lnode->new_node;
2787                                 
2788                                 if(node->preview && node->preview->rect) {
2789                                         if(lnode->preview && lnode->preview->rect) {
2790                                                 int xsize= node->preview->xsize;
2791                                                 int ysize= node->preview->ysize;
2792                                                 memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
2793                                         }
2794                                 }
2795                         }
2796                 }
2797         }
2798 }
2799
2800 /* merge local tree results back, and free local tree */
2801 /* we have to assume the editor already changed completely */
2802 void ntreeLocalMerge(bNodeTree *localtree, bNodeTree *ntree)
2803 {
2804         bNode *lnode;
2805         bNodeSocket *lsock;
2806         
2807         /* move over the compbufs and previews */
2808         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
2809                 if(node_exists(ntree, lnode->new_node)) {
2810                         
2811                         if(lnode->preview && lnode->preview->rect) {
2812                                 node_free_preview(lnode->new_node);
2813                                 lnode->new_node->preview= lnode->preview;
2814                                 lnode->preview= NULL;
2815                         }
2816                         
2817                         if(ELEM(lnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
2818                                 if(lnode->id && (lnode->flag & NODE_DO_OUTPUT)) {
2819                                         /* image_merge does sanity check for pointers */
2820                                         BKE_image_merge((Image *)lnode->new_node->id, (Image *)lnode->id);
2821                                 }
2822                         }
2823                         
2824                         for(lsock= lnode->outputs.first; lsock; lsock= lsock->next) {
2825                                 if(outsocket_exists(lnode->new_node, lsock->new_sock)) {
2826                                         lsock->new_sock->ns.data= lsock->ns.data;
2827                                         compbuf_set_node(lsock->new_sock->ns.data, lnode->new_node);
2828                                         lsock->ns.data= NULL;
2829                                         lsock->new_sock= NULL;
2830                                 }
2831                         }
2832                 }
2833         }
2834         ntreeFreeTree(localtree);
2835         MEM_freeN(localtree);
2836 }
2837
2838 /* *********************************************** */
2839
2840 /* GPU material from shader nodes */
2841
2842 static void gpu_from_node_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
2843 {
2844         bNodeSocket *sock;
2845         int i;
2846
2847         for (sock=sockets->first, i=0; sock; sock=sock->next, i++) {
2848                 memset(&gs[i], 0, sizeof(gs[i]));
2849
2850                 QUATCOPY(gs[i].vec, ns[i]->vec);
2851                 gs[i].link= ns[i]->data;
2852
2853                 if (sock->type == SOCK_VALUE)
2854                         gs[i].type= GPU_FLOAT;
2855                 else if (sock->type == SOCK_VECTOR)
2856                         gs[i].type= GPU_VEC3;
2857                 else if (sock->type == SOCK_RGBA)
2858                         gs[i].type= GPU_VEC4;
2859                 else
2860                         gs[i].type= GPU_NONE;
2861
2862                 gs[i].name = "";
2863                 gs[i].hasinput= ns[i]->hasinput && ns[i]->data;
2864                 gs[i].hasoutput= ns[i]->hasoutput && ns[i]->data;
2865                 gs[i].sockettype= ns[i]->sockettype;
2866         }
2867
2868         gs[i].type= GPU_NONE;
2869 }
2870
2871 static void data_from_gpu_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
2872 {
2873         bNodeSocket *sock;
2874         int i;
2875
2876         for (sock=sockets->first, i=0; sock; sock=sock->next, i++) {
2877                 ns[i]->data= gs[i].link;
2878                 ns[i]->sockettype= gs[i].sockettype;
2879         }
2880 }
2881
2882 static void gpu_node_group_execute(bNodeStack *stack, GPUMaterial *mat, bNode *gnode, bNodeStack **in, bNodeStack **out)
2883 {
2884         bNode *node;
2885         bNodeTree *ntree= (bNodeTree *)gnode->id;
2886         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2887         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2888         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
2889         int doit = 0;
2890         
2891         if(ntree==NULL) return;
2892         
2893         stack+= gnode->stack_index;
2894                 
2895         for(node= ntree->nodes.first; node; node= node->next) {
2896                 if(node->typeinfo->gpufunc) {
2897                         group_node_get_stack(node, stack, nsin, nsout, in, out);
2898
2899                         doit = 0;
2900                         
2901                         /* for groups, only execute outputs for edited group */
2902                         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
2903                                 if(gnode->flag & NODE_GROUP_EDIT)
2904                                         if(node->flag & NODE_DO_OUTPUT)
2905                                                 doit = 1;
2906                         }
2907                         else
2908                                 doit = 1;
2909
2910                         if(doit)  {
2911                                 gpu_from_node_stack(&node->inputs, nsin, gpuin);
2912                                 gpu_from_node_stack(&node->outputs, nsout, gpuout);
2913                                 if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
2914                                         data_from_gpu_stack(&node->outputs, nsout, gpuout);
2915                         }
2916                 }
2917         }
2918 }
2919
2920 void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
2921 {
2922         bNode *node;
2923         bNodeStack *stack;
2924         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2925         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2926         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
2927
2928         if((ntree->init & NTREE_EXEC_INIT)==0)
2929                 ntreeBeginExecTree(ntree);
2930
2931         stack= ntree->stack;
2932
2933         for(node= ntree->nodes.first; node; node= node->next) {
2934                 if(node->typeinfo->gpufunc) {
2935                         node_get_stack(node, stack, nsin, nsout);
2936                         gpu_from_node_stack(&node->inputs, nsin, gpuin);
2937                         gpu_from_node_stack(&node->outputs, nsout, gpuout);
2938                         if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
2939                                 data_from_gpu_stack(&node->outputs, nsout, gpuout);
2940                 }
2941                 else if(node->type==NODE_GROUP && node->id) {
2942                         node_get_stack(node, stack, nsin, nsout);
2943                         gpu_node_group_execute(stack, mat, node, nsin, nsout);
2944                 }
2945         }
2946
2947         ntreeEndExecTree(ntree);
2948 }
2949
2950 /* **************** call to switch lamploop for material node ************ */
2951
2952 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
2953
2954 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
2955 {
2956         node_shader_lamp_loop= lamp_loop_func;
2957 }
2958
2959 /* clumsy checking... should do dynamic outputs once */
2960 static void force_hidden_passes(bNode *node, int passflag)
2961 {
2962         bNodeSocket *sock;
2963         
2964         for(sock= node->outputs.first; sock; sock= sock->next)
2965                 sock->flag &= ~SOCK_UNAVAIL;
2966         
2967         sock= BLI_findlink(&node->outputs, RRES_OUT_Z);
2968         if(!(passflag & SCE_PASS_Z)) sock->flag |= SOCK_UNAVAIL;
2969         sock= BLI_findlink(&node->outputs, RRES_OUT_NORMAL);
2970         if(!(passflag & SCE_PASS_NORMAL)) sock->flag |= SOCK_UNAVAIL;
2971         sock= BLI_findlink(&node->outputs, RRES_OUT_VEC);
2972         if(!(passflag & SCE_PASS_VECTOR)) sock->flag |= SOCK_UNAVAIL;
2973         sock= BLI_findlink(&node->outputs, RRES_OUT_UV);
2974         if(!(passflag & SCE_PASS_UV)) sock->flag |= SOCK_UNAVAIL;
2975         sock= BLI_findlink(&node->outputs, RRES_OUT_RGBA);
2976         if(!(passflag & SCE_PASS_RGBA)) sock->flag |= SOCK_UNAVAIL;
2977         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF);
2978         if(!(passflag & SCE_PASS_DIFFUSE)) sock->flag |= SOCK_UNAVAIL;
2979         sock= BLI_findlink(&node->outputs, RRES_OUT_SPEC);
2980         if(!(passflag & SCE_PASS_SPEC)) sock->flag |= SOCK_UNAVAIL;
2981         sock= BLI_findlink(&node->outputs, RRES_OUT_SHADOW);
2982         if(!(passflag & SCE_PASS_SHADOW)) sock->flag |= SOCK_UNAVAIL;
2983         sock= BLI_findlink(&node->outputs, RRES_OUT_AO);
2984         if(!(passflag & SCE_PASS_AO)) sock->flag |= SOCK_UNAVAIL;
2985         sock= BLI_findlink(&node->outputs, RRES_OUT_REFLECT);
2986         if(!(passflag & SCE_PASS_REFLECT)) sock->flag |= SOCK_UNAVAIL;
2987         sock= BLI_findlink(&node->outputs, RRES_OUT_REFRACT);
2988         if(!(passflag & SCE_PASS_REFRACT)) sock->flag |= SOCK_UNAVAIL;
2989         sock= BLI_findlink(&node->outputs, RRES_OUT_INDIRECT);
2990         if(!(passflag & SCE_PASS_INDIRECT)) sock->flag |= SOCK_UNAVAIL;
2991         sock= BLI_findlink(&node->outputs, RRES_OUT_INDEXOB);
2992         if(!(passflag & SCE_PASS_INDEXOB)) sock->flag |= SOCK_UNAVAIL;
2993         sock= BLI_findlink(&node->outputs, RRES_OUT_MIST);
2994         if(!(passflag & SCE_PASS_MIST)) sock->flag |= SOCK_UNAVAIL;
2995         sock= BLI_findlink(&node->outputs, RRES_OUT_EMIT);
2996         if(!(passflag & SCE_PASS_EMIT)) sock->flag |= SOCK_UNAVAIL;
2997         sock= BLI_findlink(&node->outputs, RRES_OUT_ENV);
2998         if(!(passflag & SCE_PASS_ENVIRONMENT)) sock->flag |= SOCK_UNAVAIL;
2999         
3000 }
3001
3002 /* based on rules, force sockets hidden always */
3003 void ntreeCompositForceHidden(bNodeTree *ntree, Scene *curscene)
3004 {
3005         bNode *node;
3006         
3007         if(ntree==NULL) return;
3008         
3009         for(node= ntree->nodes.first; node; node= node->next) {
3010                 if( node->type==CMP_NODE_R_LAYERS) {
3011                         Scene *sce= node->id?(Scene *)node->id:curscene;
3012                         SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
3013                         if(srl)
3014                                 force_hidden_passes(node, srl->passflag);
3015                 }
3016                 else if( node->type==CMP_NODE_IMAGE) {
3017                         Image *ima= (Image *)node->id;
3018                         if(ima) {
3019                                 if(ima->rr) {
3020                                         ImageUser *iuser= node->storage;
3021                                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
3022                                         if(rl)
3023                                                 force_hidden_passes(node, rl->passflag);
3024                                         else
3025                                                 force_hidden_passes(node, 0);
3026                                 }
3027                                 else if(ima->type!=IMA_TYPE_MULTILAYER) {       /* if ->rr not yet read we keep inputs */
3028                                         force_hidden_passes(node, RRES_OUT_Z);
3029                                 }
3030                                 else
3031                                         force_hidden_passes(node, 0);
3032                         }
3033                         else
3034                                 force_hidden_passes(node, 0);
3035                 }
3036         }
3037
3038 }
3039
3040 /* called from render pipeline, to tag render input and output */
3041 /* need to do all scenes, to prevent errors when you re-render 1 scene */
3042 void ntreeCompositTagRender(Scene *curscene)
3043 {
3044         Scene *sce;
3045         
3046         for(sce= G.main->scene.first; sce; sce= sce->id.next) {
3047                 if(sce->nodetree) {
3048                         bNode *node;
3049                         
3050                         for(node= sce->nodetree->nodes.first; node; node= node->next) {
3051                                 if(node->id==(ID *)curscene || node->type==CMP_NODE_COMPOSITE)
3052                                         NodeTagChanged(sce->nodetree, node);
3053                                 else if(node->type==CMP_NODE_TEXTURE) /* uses scene sizex/sizey */
3054                                         NodeTagChanged(sce->nodetree, node);
3055                         }
3056                 }
3057         }
3058 }
3059
3060 static int node_animation_properties(bNodeTree *ntree, bNode *node)
3061 {
3062         bNodeSocket *sock;
3063         const ListBase *lb;