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