Movie Clip Node: skip putting frame to cache when rendering animation
[blender.git] / source / blender / blenkernel / intern / node.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Bob Holcomb.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/node.c
29  *  \ingroup bke
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include <stdlib.h>
35 #include <stddef.h>
36 #include <string.h>
37 #include <limits.h>
38
39 #include "DNA_action_types.h"
40 #include "DNA_anim_types.h"
41 #include "DNA_node_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_scene_types.h"
44
45 #include "BLI_string.h"
46 #include "BLI_math.h"
47 #include "BLI_listbase.h"
48 #include "BLI_path_util.h"
49 #include "BLI_utildefines.h"
50
51 #include "BLF_translation.h"
52
53 #include "BKE_animsys.h"
54 #include "BKE_action.h"
55 #include "BKE_fcurve.h"
56 #include "BKE_global.h"
57 #include "BKE_image.h"
58 #include "BKE_library.h"
59 #include "BKE_main.h"
60 #include "BKE_node.h"
61 #include "BKE_utildefines.h"
62 #include "BKE_utildefines.h"
63
64 #include "RNA_access.h"
65
66 #include "NOD_socket.h"
67 #include "NOD_composite.h"
68 #include "NOD_shader.h"
69 #include "NOD_texture.h"
70
71
72 bNodeTreeType *ntreeGetType(int type)
73 {
74         static bNodeTreeType *types[NUM_NTREE_TYPES];
75         static int types_init = 1;
76         if (types_init) {
77                 types[NTREE_SHADER] = &ntreeType_Shader;
78                 types[NTREE_COMPOSIT] = &ntreeType_Composite;
79                 types[NTREE_TEXTURE] = &ntreeType_Texture;
80                 types_init = 0;
81         }
82         
83         if (type >= 0 && type < NUM_NTREE_TYPES) {
84                 return types[type];
85         }
86         else {
87                 return NULL;
88         }
89 }
90
91 static bNodeType *node_get_type(bNodeTree *ntree, int type)
92 {
93         bNodeType *ntype = ntreeGetType(ntree->type)->node_types.first;
94         for (; ntype; ntype= ntype->next)
95                 if (ntype->type==type)
96                         return ntype;
97         
98         return NULL;
99 }
100
101 bNodeType *ntreeGetNodeType(bNodeTree *ntree)
102 {
103         return node_get_type(ntree, ntree->nodetype);
104 }
105
106 bNodeSocketType *ntreeGetSocketType(int type)
107 {
108         static bNodeSocketType *types[NUM_SOCKET_TYPES]= {NULL};
109         static int types_init = 1;
110
111         if (types_init) {
112                 node_socket_type_init(types);
113                 types_init= 0;
114         }
115
116         if (type < NUM_SOCKET_TYPES) {
117                 return types[type];
118         }
119         else {
120                 return NULL;
121         }
122 }
123
124 void ntreeInitTypes(bNodeTree *ntree)
125 {
126         bNode *node, *next;
127         
128         for (node= ntree->nodes.first; node; node= next) {
129                 next= node->next;
130                 
131                 node->typeinfo = node_get_type(ntree, node->type);
132
133                 if (node->typeinfo == NULL) {
134                         printf("Error: Node type %s doesn't exist anymore, removed\n", node->name);
135                         nodeFreeNode(ntree, node);
136                 }
137         }
138                         
139         ntree->init |= NTREE_TYPE_INIT;
140 }
141
142 static bNodeSocket *make_socket(bNodeTree *UNUSED(ntree), int in_out, const char *name, int type)
143 {
144         bNodeSocket *sock;
145         
146         sock= MEM_callocN(sizeof(bNodeSocket), "sock");
147         
148         BLI_strncpy(sock->name, name, NODE_MAXSTR);
149         sock->limit = (in_out==SOCK_IN ? 1 : 0xFFF);
150         sock->type= type;
151         sock->storage = NULL;
152         
153         sock->default_value = node_socket_make_default_value(type);
154         node_socket_init_default_value(type, sock->default_value);
155         
156         return sock;
157 }
158
159 bNodeSocket *nodeAddSocket(bNodeTree *ntree, bNode *node, int in_out, const char *name, int type)
160 {
161         bNodeSocket *sock = make_socket(ntree, in_out, name, type);
162         if (in_out==SOCK_IN)
163                 BLI_addtail(&node->inputs, sock);
164         else if (in_out==SOCK_OUT)
165                 BLI_addtail(&node->outputs, sock);
166         
167         node->update |= NODE_UPDATE;
168         
169         return sock;
170 }
171
172 bNodeSocket *nodeInsertSocket(bNodeTree *ntree, bNode *node, int in_out, bNodeSocket *next_sock, const char *name, int type)
173 {
174         bNodeSocket *sock = make_socket(ntree, in_out, name, type);
175         if (in_out==SOCK_IN)
176                 BLI_insertlinkbefore(&node->inputs, next_sock, sock);
177         else if (in_out==SOCK_OUT)
178                 BLI_insertlinkbefore(&node->outputs, next_sock, sock);
179         
180         node->update |= NODE_UPDATE;
181         
182         return sock;
183 }
184
185 void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock)
186 {
187         bNodeLink *link, *next;
188         
189         for (link= ntree->links.first; link; link= next) {
190                 next= link->next;
191                 if (link->fromsock==sock || link->tosock==sock) {
192                         nodeRemLink(ntree, link);
193                 }
194         }
195         
196         /* this is fast, this way we don't need an in_out argument */
197         BLI_remlink(&node->inputs, sock);
198         BLI_remlink(&node->outputs, sock);
199         
200         node_socket_free_default_value(sock->type, sock->default_value);
201         MEM_freeN(sock);
202         
203         node->update |= NODE_UPDATE;
204 }
205
206 void nodeRemoveAllSockets(bNodeTree *ntree, bNode *node)
207 {
208         bNodeSocket *sock;
209         bNodeLink *link, *next;
210         
211         for (link= ntree->links.first; link; link= next) {
212                 next= link->next;
213                 if (link->fromnode==node || link->tonode==node) {
214                         nodeRemLink(ntree, link);
215                 }
216         }
217         
218         for (sock=node->inputs.first; sock; sock=sock->next)
219                 node_socket_free_default_value(sock->type, sock->default_value);
220         BLI_freelistN(&node->inputs);
221         for (sock=node->outputs.first; sock; sock=sock->next)
222                 node_socket_free_default_value(sock->type, sock->default_value);
223         BLI_freelistN(&node->outputs);
224         
225         node->update |= NODE_UPDATE;
226 }
227
228 /* finds a node based on its name */
229 bNode *nodeFindNodebyName(bNodeTree *ntree, const char *name)
230 {
231         return BLI_findstring(&ntree->nodes, name, offsetof(bNode, name));
232 }
233
234 /* finds a node based on given socket */
235 int nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **nodep, int *sockindex, int *in_out)
236 {
237         bNode *node;
238         bNodeSocket *tsock;
239         int index= 0;
240         
241         for (node= ntree->nodes.first; node; node= node->next) {
242                 for (index=0, tsock= node->inputs.first; tsock; tsock= tsock->next, index++) {
243                         if (tsock==sock) {
244                                 if (in_out) *in_out= SOCK_IN;
245                                 break;
246                         }
247                 }
248                 if (tsock)
249                         break;
250                 for (index=0, tsock= node->outputs.first; tsock; tsock= tsock->next, index++) {
251                         if (tsock==sock) {
252                                 if (in_out) *in_out= SOCK_OUT;
253                                 break;
254                         }
255                 }
256                 if (tsock)
257                         break;
258         }
259
260         if (node) {
261                 *nodep= node;
262                 if (sockindex) *sockindex= index;
263                 return 1;
264         }
265         
266         *nodep= NULL;
267         return 0;
268 }
269
270 /* ************** Add stuff ********** */
271 static void node_add_sockets_from_type(bNodeTree *ntree, bNode *node, bNodeType *ntype)
272 {
273         bNodeSocketTemplate *sockdef;
274         /* bNodeSocket *sock; */ /* UNUSED */
275
276         if (ntype->inputs) {
277                 sockdef= ntype->inputs;
278                 while (sockdef->type != -1) {
279                         /* sock = */ node_add_input_from_template(ntree, node, sockdef);
280                         
281                         sockdef++;
282                 }
283         }
284         if (ntype->outputs) {
285                 sockdef= ntype->outputs;
286                 while (sockdef->type != -1) {
287                         /* sock = */ node_add_output_from_template(ntree, node, sockdef);
288                         
289                         sockdef++;
290                 }
291         }
292 }
293
294 /* Find the first available, non-duplicate name for a given node */
295 void nodeUniqueName(bNodeTree *ntree, bNode *node)
296 {
297         BLI_uniquename(&ntree->nodes, node, "Node", '.', offsetof(bNode, name), sizeof(node->name));
298 }
299
300 bNode *nodeAddNode(bNodeTree *ntree, struct bNodeTemplate *ntemp)
301 {
302         bNode *node;
303         bNodeType *ntype;
304         
305         ntype= node_get_type(ntree, ntemp->type);
306         if (ntype == NULL) {
307                 printf("nodeAddNodeType() error: '%d' type invalid\n", ntemp->type);
308                 return NULL;
309         }
310         /* validity check */
311         if (!nodeValid(ntree, ntemp))
312                 return NULL;
313         
314         node= MEM_callocN(sizeof(bNode), "new node");
315         node->type= ntype->type;
316         node->typeinfo= ntype;
317         node->flag= NODE_SELECT|ntype->flag;
318         node->width= ntype->width;
319         node->miniwidth= 42.0f;
320         node->height= ntype->height;
321         node->color[0] = node->color[1] = node->color[2] = 0.608;       /* default theme color */
322         
323         node_add_sockets_from_type(ntree, node, ntype);
324
325         BLI_addtail(&ntree->nodes, node);
326         
327         if (ntype->initfunc!=NULL)
328                 ntype->initfunc(ntree, node, ntemp);
329
330         /* initialize the node name with the node label.
331          * note: do this after the initfunc so nodes get
332          * their data set which may be used in naming
333          * (node groups for example) */
334         BLI_strncpy(node->name, nodeLabel(node), NODE_MAXSTR);
335         nodeUniqueName(ntree, node);
336         
337         ntree->update |= NTREE_UPDATE_NODES;
338         
339         return node;
340 }
341
342 /* keep socket listorder identical, for copying links */
343 /* ntree is the target tree */
344 bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node)
345 {
346         bNode *nnode= MEM_callocN(sizeof(bNode), "dupli node");
347         bNodeSocket *sock, *oldsock;
348
349         *nnode= *node;
350         nodeUniqueName(ntree, nnode);
351         
352         BLI_addtail(&ntree->nodes, nnode);
353
354         BLI_duplicatelist(&nnode->inputs, &node->inputs);
355         oldsock= node->inputs.first;
356         for (sock= nnode->inputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
357                 oldsock->new_sock= sock;
358                 sock->stack_index= 0;
359                 
360                 sock->default_value = node_socket_make_default_value(oldsock->type);
361                 node_socket_copy_default_value(oldsock->type, sock->default_value, oldsock->default_value);
362                 
363                 /* XXX some compositor node (e.g. image, render layers) still store
364                  * some persistent buffer data here, need to clear this to avoid dangling pointers.
365                  */
366                 sock->cache = NULL;
367         }
368         
369         BLI_duplicatelist(&nnode->outputs, &node->outputs);
370         oldsock= node->outputs.first;
371         for (sock= nnode->outputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
372                 oldsock->new_sock= sock;
373                 sock->stack_index= 0;
374                 
375                 sock->default_value = node_socket_make_default_value(oldsock->type);
376                 node_socket_copy_default_value(oldsock->type, sock->default_value, oldsock->default_value);
377                 
378                 /* XXX some compositor node (e.g. image, render layers) still store
379                  * some persistent buffer data here, need to clear this to avoid dangling pointers.
380                  */
381                 sock->cache = NULL;
382         }
383         
384         /* don't increase node->id users, freenode doesn't decrement either */
385         
386         if (node->typeinfo->copystoragefunc)
387                 node->typeinfo->copystoragefunc(node, nnode);
388         
389         node->new_node= nnode;
390         nnode->new_node= NULL;
391         nnode->preview= NULL;
392         
393         ntree->update |= NTREE_UPDATE_NODES;
394         
395         return nnode;
396 }
397
398 /* also used via rna api, so we check for proper input output direction */
399 bNodeLink *nodeAddLink(bNodeTree *ntree, bNode *fromnode, bNodeSocket *fromsock, bNode *tonode, bNodeSocket *tosock)
400 {
401         bNodeSocket *sock;
402         bNodeLink *link= NULL; 
403         int from= 0, to= 0;
404         
405         if (fromnode) {
406                 /* test valid input */
407                 for (sock= fromnode->outputs.first; sock; sock= sock->next)
408                         if (sock==fromsock)
409                                 break;
410                 if (sock)
411                         from= 1; /* OK */
412                 else {
413                         for (sock= fromnode->inputs.first; sock; sock= sock->next)
414                                 if (sock==fromsock)
415                                         break;
416                         if (sock)
417                                 from= -1; /* OK but flip */
418                 }
419         }
420         else {
421                 /* check tree sockets */
422                 for (sock= ntree->inputs.first; sock; sock= sock->next)
423                         if (sock==fromsock)
424                                 break;
425                 if (sock)
426                         from= 1; /* OK */
427                 else {
428                         for (sock= ntree->outputs.first; sock; sock= sock->next)
429                                 if (sock==fromsock)
430                                         break;
431                         if (sock)
432                                 from= -1; /* OK but flip */
433                 }
434         }
435         if (tonode) {
436                 for (sock= tonode->inputs.first; sock; sock= sock->next)
437                         if (sock==tosock)
438                                 break;
439                 if (sock)
440                         to= 1; /* OK */
441                 else {
442                         for (sock= tonode->outputs.first; sock; sock= sock->next)
443                                 if (sock==tosock)
444                                         break;
445                         if (sock)
446                                 to= -1; /* OK but flip */
447                 }
448         }
449         else {
450                 /* check tree sockets */
451                 for (sock= ntree->outputs.first; sock; sock= sock->next)
452                         if (sock==tosock)
453                                 break;
454                 if (sock)
455                         to= 1; /* OK */
456                 else {
457                         for (sock= ntree->inputs.first; sock; sock= sock->next)
458                                 if (sock==tosock)
459                                         break;
460                         if (sock)
461                                 to= -1; /* OK but flip */
462                 }
463         }
464         
465         if (from >= 0 && to >= 0) {
466                 link= MEM_callocN(sizeof(bNodeLink), "link");
467                 BLI_addtail(&ntree->links, link);
468                 link->fromnode= fromnode;
469                 link->fromsock= fromsock;
470                 link->tonode= tonode;
471                 link->tosock= tosock;
472         }
473         else if (from <= 0 && to <= 0) {
474                 link= MEM_callocN(sizeof(bNodeLink), "link");
475                 BLI_addtail(&ntree->links, link);
476                 link->fromnode= tonode;
477                 link->fromsock= tosock;
478                 link->tonode= fromnode;
479                 link->tosock= fromsock;
480         }
481         
482         ntree->update |= NTREE_UPDATE_LINKS;
483         
484         return link;
485 }
486
487 void nodeRemLink(bNodeTree *ntree, bNodeLink *link)
488 {
489         BLI_remlink(&ntree->links, link);
490         if (link->tosock)
491                 link->tosock->link= NULL;
492         MEM_freeN(link);
493         
494         ntree->update |= NTREE_UPDATE_LINKS;
495 }
496
497 void nodeRemSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
498 {
499         bNodeLink *link, *next;
500         
501         for (link= ntree->links.first; link; link= next) {
502                 next= link->next;
503                 if (link->fromsock==sock || link->tosock==sock) {
504                         nodeRemLink(ntree, link);
505                 }
506         }
507         
508         ntree->update |= NTREE_UPDATE_LINKS;
509 }
510
511 void nodeInternalRelink(bNodeTree *ntree, bNode *node)
512 {
513         bNodeLink *link, *link_next;
514         ListBase intlinks;
515         
516         if (!node->typeinfo->internal_connect)
517                 return;
518         
519         intlinks = node->typeinfo->internal_connect(ntree, node);
520         
521         /* store link pointers in output sockets, for efficient lookup */
522         for (link=intlinks.first; link; link=link->next)
523                 link->tosock->link = link;
524         
525         /* redirect downstream links */
526         for (link=ntree->links.first; link; link=link_next) {
527                 link_next = link->next;
528                 
529                 /* do we have internal link? */
530                 if (link->fromnode==node) {
531                         if (link->fromsock->link) {
532                                 /* get the upstream input link */
533                                 bNodeLink *fromlink = link->fromsock->link->fromsock->link;
534                                 /* skip the node */
535                                 if (fromlink) {
536                                         link->fromnode = fromlink->fromnode;
537                                         link->fromsock = fromlink->fromsock;
538                                         
539                                         ntree->update |= NTREE_UPDATE_LINKS;
540                                 }
541                                 else
542                                         nodeRemLink(ntree, link);
543                         }
544                         else
545                                 nodeRemLink(ntree, link);
546                 }
547         }
548         
549         /* remove remaining upstream links */
550         for (link=ntree->links.first; link; link=link_next) {
551                 link_next = link->next;
552                 
553                 if (link->tonode==node)
554                         nodeRemLink(ntree, link);
555         }
556         
557         BLI_freelistN(&intlinks);
558 }
559
560 void nodeToView(bNode *node, float x, float y, float *rx, float *ry)
561 {
562         if (node->parent) {
563                 nodeToView(node->parent, x + node->locx, y + node->locy, rx, ry);
564         }
565         else {
566                 *rx = x + node->locx;
567                 *ry = y + node->locy;
568         }
569 }
570
571 void nodeFromView(bNode *node, float x, float y, float *rx, float *ry)
572 {
573         if (node->parent) {
574                 nodeFromView(node->parent, x, y, rx, ry);
575                 *rx -= node->locx;
576                 *ry -= node->locy;
577         }
578         else {
579                 *rx = x - node->locx;
580                 *ry = y - node->locy;
581         }
582 }
583
584 void nodeAttachNode(bNode *node, bNode *parent)
585 {
586         float locx, locy;
587         nodeToView(node, 0.0f, 0.0f, &locx, &locy);
588         
589         node->parent = parent;
590         /* transform to parent space */
591         nodeFromView(parent, locx, locy, &node->locx, &node->locy);
592 }
593
594 void nodeDetachNode(struct bNode *node)
595 {
596         float locx, locy;
597         
598         if (node->parent) {
599                 /* transform to view space */
600                 nodeToView(node, 0.0f, 0.0f, &locx, &locy);
601                 node->locx = locx;
602                 node->locy = locy;
603                 node->parent = NULL;
604         }
605 }
606
607 bNodeTree *ntreeAddTree(const char *name, int type, int nodetype)
608 {
609         bNodeTree *ntree;
610         bNodeType *ntype;
611         
612         /* trees are created as local trees if they of compositor, material or texture type,
613          * node groups and other tree types are created as library data.
614          */
615         if (ELEM3(type, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE) && nodetype==0) {
616                 ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
617                 *( (short *)ntree->id.name )= ID_NT; /* not "type", as that is ntree->type */
618                 BLI_strncpy(ntree->id.name+2, name, sizeof(ntree->id.name));
619         }
620         else
621                 ntree= BKE_libblock_alloc(&G.main->nodetree, ID_NT, name);
622         
623         ntree->type= type;
624         ntree->nodetype = nodetype;
625         
626         ntreeInitTypes(ntree);
627         
628         ntype = node_get_type(ntree, ntree->nodetype);
629         if (ntype && ntype->inittreefunc)
630                 ntype->inittreefunc(ntree);
631         
632         return ntree;
633 }
634
635 /* Warning: this function gets called during some rather unexpected times
636  *      - this gets called when executing compositing updates (for threaded previews)
637  *      - when the nodetree datablock needs to be copied (i.e. when users get copied)
638  *      - for scene duplication use ntreeSwapID() after so we don't have stale pointers.
639  *
640  * do_make_extern: keep enabled for general use, only reason _not_ to enable is when
641  * copying for internal use (threads for eg), where you wont want it to modify the
642  * scene data.
643  */
644 static bNodeTree *ntreeCopyTree_internal(bNodeTree *ntree, const short do_make_extern)
645 {
646         bNodeTree *newtree;
647         bNode *node /*, *nnode */ /* UNUSED */, *last;
648         bNodeLink *link;
649         bNodeSocket *gsock, *oldgsock;
650         
651         if (ntree==NULL) return NULL;
652         
653         /* is ntree part of library? */
654         for (newtree=G.main->nodetree.first; newtree; newtree= newtree->id.next)
655                 if (newtree==ntree) break;
656         if (newtree) {
657                 newtree= BKE_libblock_copy(&ntree->id);
658         }
659         else {
660                 newtree= MEM_dupallocN(ntree);
661                 BKE_libblock_copy_data(&newtree->id, &ntree->id, TRUE); /* copy animdata and ID props */
662         }
663
664         id_us_plus((ID *)newtree->gpd);
665
666         /* in case a running nodetree is copied */
667         newtree->execdata= NULL;
668         
669         newtree->nodes.first= newtree->nodes.last= NULL;
670         newtree->links.first= newtree->links.last= NULL;
671         
672         last = ntree->nodes.last;
673         for (node= ntree->nodes.first; node; node= node->next) {
674
675                 if (do_make_extern) {
676                         id_lib_extern(node->id);
677                 }
678
679                 node->new_node= NULL;
680                 /* nnode= */ nodeCopyNode(newtree, node);       /* sets node->new */
681                 
682                 /* make sure we don't copy new nodes again! */
683                 if (node==last)
684                         break;
685         }
686         
687         /* socket definition for group usage */
688         BLI_duplicatelist(&newtree->inputs, &ntree->inputs);
689         for (gsock= newtree->inputs.first, oldgsock= ntree->inputs.first; gsock; gsock=gsock->next, oldgsock=oldgsock->next) {
690                 oldgsock->new_sock= gsock;
691                 gsock->groupsock = (oldgsock->groupsock ? oldgsock->groupsock->new_sock : NULL);
692                 gsock->default_value = node_socket_make_default_value(oldgsock->type);
693                 node_socket_copy_default_value(oldgsock->type, gsock->default_value, oldgsock->default_value);
694         }
695         BLI_duplicatelist(&newtree->outputs, &ntree->outputs);
696         for (gsock= newtree->outputs.first, oldgsock= ntree->outputs.first; gsock; gsock=gsock->next, oldgsock=oldgsock->next) {
697                 oldgsock->new_sock= gsock;
698                 gsock->groupsock = (oldgsock->groupsock ? oldgsock->groupsock->new_sock : NULL);
699                 gsock->default_value = node_socket_make_default_value(oldgsock->type);
700                 node_socket_copy_default_value(oldgsock->type, gsock->default_value, oldgsock->default_value);
701         }
702         
703         /* copy links */
704         BLI_duplicatelist(&newtree->links, &ntree->links);
705         for (link= newtree->links.first; link; link= link->next) {
706                 link->fromnode = (link->fromnode ? link->fromnode->new_node : NULL);
707                 link->fromsock = (link->fromsock ? link->fromsock->new_sock : NULL);
708                 link->tonode = (link->tonode ? link->tonode->new_node : NULL);
709                 link->tosock = (link->tosock ? link->tosock->new_sock : NULL);
710                 /* update the link socket's pointer */
711                 if (link->tosock)
712                         link->tosock->link = link;
713         }
714         
715         /* update node->parent pointers */
716         for (node=newtree->nodes.first; node; node=node->next) {
717                 if (node->parent)
718                         node->parent = node->parent->new_node;
719         }
720         
721         return newtree;
722 }
723
724 bNodeTree *ntreeCopyTree(bNodeTree *ntree)
725 {
726         return ntreeCopyTree_internal(ntree, TRUE);
727 }
728
729 /* use when duplicating scenes */
730 void ntreeSwitchID(bNodeTree *ntree, ID *id_from, ID *id_to)
731 {
732         bNode *node;
733         /* for scene duplication only */
734         for (node= ntree->nodes.first; node; node= node->next) {
735                 if (node->id==id_from) {
736                         node->id= id_to;
737                 }
738         }
739 }
740
741 /* *************** preview *********** */
742 /* if node->preview, then we assume the rect to exist */
743
744 void nodeFreePreview(bNode *node)
745 {
746         if (node->preview) {
747                 if (node->preview->rect)
748                         MEM_freeN(node->preview->rect);
749                 MEM_freeN(node->preview);
750                 node->preview= NULL;
751         }       
752 }
753
754 static void node_init_preview(bNode *node, int xsize, int ysize)
755 {
756         
757         if (node->preview==NULL) {
758                 node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
759                 //              printf("added preview %s\n", node->name);
760         }
761         
762         /* node previews can get added with variable size this way */
763         if (xsize==0 || ysize==0)
764                 return;
765         
766         /* sanity checks & initialize */
767         if (node->preview->rect) {
768                 if (node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
769                         MEM_freeN(node->preview->rect);
770                         node->preview->rect= NULL;
771                 }
772         }
773         
774         if (node->preview->rect==NULL) {
775                 node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(char)*4, "node preview rect");
776                 node->preview->xsize= xsize;
777                 node->preview->ysize= ysize;
778         }
779         /* no clear, makes nicer previews */
780 }
781
782 void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
783 {
784         bNode *node;
785         
786         if (ntree==NULL)
787                 return;
788         
789         for (node= ntree->nodes.first; node; node= node->next) {
790                 if (node->typeinfo->flag & NODE_PREVIEW)        /* hrms, check for closed nodes? */
791                         node_init_preview(node, xsize, ysize);
792                 if (node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
793                         ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
794         }               
795 }
796
797 static void nodeClearPreview(bNode *node)
798 {
799         if (node->preview && node->preview->rect)
800                 memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
801 }
802
803 /* use it to enforce clear */
804 void ntreeClearPreview(bNodeTree *ntree)
805 {
806         bNode *node;
807         
808         if (ntree==NULL)
809                 return;
810         
811         for (node= ntree->nodes.first; node; node= node->next) {
812                 if (node->typeinfo->flag & NODE_PREVIEW)
813                         nodeClearPreview(node);
814                 if (node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
815                         ntreeClearPreview((bNodeTree *)node->id);
816         }               
817 }
818
819 /* hack warning! this function is only used for shader previews, and 
820  * since it gets called multiple times per pixel for Ztransp we only
821  * add the color once. Preview gets cleared before it starts render though */
822 void nodeAddToPreview(bNode *node, float col[4], int x, int y, int do_manage)
823 {
824         bNodePreview *preview= node->preview;
825         if (preview) {
826                 if (x>=0 && y>=0) {
827                         if (x<preview->xsize && y<preview->ysize) {
828                                 unsigned char *tar= preview->rect+ 4*((preview->xsize*y) + x);
829                                 
830                                 if (do_manage) {
831                                         linearrgb_to_srgb_uchar4(tar, col);
832                                 }
833                                 else {
834                                         rgba_float_to_uchar(tar, col);
835                                 }
836                         }
837                         //else printf("prv out bound x y %d %d\n", x, y);
838                 }
839                 //else printf("prv out bound x y %d %d\n", x, y);
840         }
841 }
842
843 /* ************** Free stuff ********** */
844
845 /* goes over entire tree */
846 void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
847 {
848         bNodeLink *link, *next;
849         bNodeSocket *sock;
850         ListBase *lb;
851         
852         for (link= ntree->links.first; link; link= next) {
853                 next= link->next;
854                 
855                 if (link->fromnode==node) {
856                         lb= &node->outputs;
857                         if (link->tonode)
858                                 link->tonode->update |= NODE_UPDATE;
859                 }
860                 else if (link->tonode==node)
861                         lb= &node->inputs;
862                 else
863                         lb= NULL;
864
865                 if (lb) {
866                         for (sock= lb->first; sock; sock= sock->next) {
867                                 if (link->fromsock==sock || link->tosock==sock)
868                                         break;
869                         }
870                         if (sock) {
871                                 nodeRemLink(ntree, link);
872                         }
873                 }
874         }
875 }
876
877 static void node_unlink_attached(bNodeTree *ntree, bNode *parent)
878 {
879         bNode *node;
880         for (node=ntree->nodes.first; node; node=node->next) {
881                 if (node->parent == parent)
882                         nodeDetachNode(node);
883         }
884 }
885
886 void nodeFreeNode(bNodeTree *ntree, bNode *node)
887 {
888         bNodeTreeType *treetype= ntreeGetType(ntree->type);
889         bNodeSocket *sock, *nextsock;
890         
891         /* remove all references to this node */
892         nodeUnlinkNode(ntree, node);
893         node_unlink_attached(ntree, node);
894         
895         BLI_remlink(&ntree->nodes, node);
896         
897         /* since it is called while free database, node->id is undefined */
898         
899         if (treetype->free_node_cache)
900                 treetype->free_node_cache(ntree, node);
901         
902         if (node->typeinfo && node->typeinfo->freestoragefunc)
903                 node->typeinfo->freestoragefunc(node);
904         
905         for (sock=node->inputs.first; sock; sock = nextsock) {
906                 nextsock = sock->next;
907                 node_socket_free_default_value(sock->type, sock->default_value);
908                 MEM_freeN(sock);
909         }
910         for (sock=node->outputs.first; sock; sock = nextsock) {
911                 nextsock = sock->next;
912                 node_socket_free_default_value(sock->type, sock->default_value);
913                 MEM_freeN(sock);
914         }
915
916         nodeFreePreview(node);
917
918         MEM_freeN(node);
919         
920         ntree->update |= NTREE_UPDATE_NODES;
921 }
922
923 /* do not free ntree itself here, BKE_libblock_free calls this function too */
924 void ntreeFreeTree(bNodeTree *ntree)
925 {
926         bNode *node, *next;
927         bNodeSocket *sock;
928         
929         if (ntree==NULL) return;
930         
931         /* XXX hack! node trees should not store execution graphs at all.
932          * This should be removed when old tree types no longer require it.
933          * Currently the execution data for texture nodes remains in the tree
934          * after execution, until the node tree is updated or freed.
935          */
936         if (ntree->execdata) {
937                 switch (ntree->type) {
938                 case NTREE_COMPOSIT:
939                         ntreeCompositEndExecTree(ntree->execdata, 1);
940                         break;
941                 case NTREE_SHADER:
942                         ntreeShaderEndExecTree(ntree->execdata, 1);
943                         break;
944                 case NTREE_TEXTURE:
945                         ntreeTexEndExecTree(ntree->execdata, 1);
946                         break;
947                 }
948         }
949         
950         BKE_free_animdata((ID *)ntree);
951         
952         id_us_min((ID *)ntree->gpd);
953
954         BLI_freelistN(&ntree->links);   /* do first, then unlink_node goes fast */
955         
956         for (node= ntree->nodes.first; node; node= next) {
957                 next= node->next;
958                 nodeFreeNode(ntree, node);
959         }
960         
961         for (sock=ntree->inputs.first; sock; sock=sock->next)
962                 node_socket_free_default_value(sock->type, sock->default_value);
963         BLI_freelistN(&ntree->inputs);
964         for (sock=ntree->outputs.first; sock; sock=sock->next)
965                 node_socket_free_default_value(sock->type, sock->default_value);
966         BLI_freelistN(&ntree->outputs);
967 }
968
969 void ntreeFreeCache(bNodeTree *ntree)
970 {
971         bNodeTreeType *treetype;
972         
973         if (ntree==NULL) return;
974         
975         treetype= ntreeGetType(ntree->type);
976         if (treetype->free_cache)
977                 treetype->free_cache(ntree);
978 }
979
980 void ntreeSetOutput(bNodeTree *ntree)
981 {
982         bNode *node;
983
984         /* find the active outputs, might become tree type dependent handler */
985         for (node= ntree->nodes.first; node; node= node->next) {
986                 if (node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
987                         bNode *tnode;
988                         int output= 0;
989                         
990                         /* we need a check for which output node should be tagged like this, below an exception */
991                         if (node->type==CMP_NODE_OUTPUT_FILE)
992                                 continue;
993
994                         /* there is more types having output class, each one is checked */
995                         for (tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
996                                 if (tnode->typeinfo->nclass==NODE_CLASS_OUTPUT) {
997                                         
998                                         if (ntree->type==NTREE_COMPOSIT) {
999                                                         
1000                                                 /* same type, exception for viewer */
1001                                                 if (tnode->type==node->type ||
1002                                                    (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) &&
1003                                                         ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))) {
1004                                                         if (tnode->flag & NODE_DO_OUTPUT) {
1005                                                                 output++;
1006                                                                 if (output>1)
1007                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
1008                                                         }
1009                                                 }
1010                                         }
1011                                         else {
1012                                                 /* same type */
1013                                                 if (tnode->type==node->type) {
1014                                                         if (tnode->flag & NODE_DO_OUTPUT) {
1015                                                                 output++;
1016                                                                 if (output>1)
1017                                                                         tnode->flag &= ~NODE_DO_OUTPUT;
1018                                                         }
1019                                                 }
1020                                         }
1021                                 }
1022                         }
1023                         if (output==0)
1024                                 node->flag |= NODE_DO_OUTPUT;
1025                 }
1026         }
1027         
1028         /* here we could recursively set which nodes have to be done,
1029          * might be different for editor or for "real" use... */
1030 }
1031
1032 typedef struct MakeLocalCallData {
1033         ID *group_id;
1034         ID *new_id;
1035         int lib, local;
1036 } MakeLocalCallData;
1037
1038 static void ntreeMakeLocal_CheckLocal(void *calldata, ID *owner_id, bNodeTree *ntree)
1039 {
1040         MakeLocalCallData *cd= (MakeLocalCallData*)calldata;
1041         bNode *node;
1042         
1043         /* find if group is in tree */
1044         for (node= ntree->nodes.first; node; node= node->next) {
1045                 if (node->id == cd->group_id) {
1046                         if (owner_id->lib) cd->lib= 1;
1047                         else cd->local= 1;
1048                 }
1049         }
1050 }
1051
1052 static void ntreeMakeLocal_LinkNew(void *calldata, ID *owner_id, bNodeTree *ntree)
1053 {
1054         MakeLocalCallData *cd= (MakeLocalCallData*)calldata;
1055         bNode *node;
1056         
1057         /* find if group is in tree */
1058         for (node= ntree->nodes.first; node; node= node->next) {
1059                 if (node->id == cd->group_id) {
1060                         if (owner_id->lib==NULL) {
1061                                 node->id= cd->new_id;
1062                                 cd->new_id->us++;
1063                                 cd->group_id->us--;
1064                         }
1065                 }
1066         }
1067 }
1068
1069 void ntreeMakeLocal(bNodeTree *ntree)
1070 {
1071         Main *bmain= G.main;
1072         bNodeTreeType *treetype= ntreeGetType(ntree->type);
1073         MakeLocalCallData cd;
1074         
1075         /* - only lib users: do nothing
1076          * - only local users: set flag
1077          * - mixed: make copy
1078          */
1079         
1080         if (ntree->id.lib==NULL) return;
1081         if (ntree->id.us==1) {
1082                 id_clear_lib_data(bmain, (ID *)ntree);
1083                 return;
1084         }
1085         
1086         /* now check users of groups... again typedepending, callback... */
1087         cd.group_id = &ntree->id;
1088         cd.new_id = NULL;
1089         cd.local = 0;
1090         cd.lib = 0;
1091         
1092         treetype->foreach_nodetree(G.main, &cd, &ntreeMakeLocal_CheckLocal);
1093         
1094         /* if all users are local, we simply make tree local */
1095         if (cd.local && cd.lib==0) {
1096                 id_clear_lib_data(bmain, (ID *)ntree);
1097         }
1098         else if (cd.local && cd.lib) {
1099                 /* this is the mixed case, we copy the tree and assign it to local users */
1100                 bNodeTree *newtree= ntreeCopyTree(ntree);
1101                 
1102                 newtree->id.us= 0;
1103                 
1104
1105                 cd.new_id = &newtree->id;
1106                 treetype->foreach_nodetree(G.main, &cd, &ntreeMakeLocal_LinkNew);
1107         }
1108 }
1109
1110 int ntreeNodeExists(bNodeTree *ntree, bNode *testnode)
1111 {
1112         bNode *node= ntree->nodes.first;
1113         for (; node; node= node->next)
1114                 if (node==testnode)
1115                         return 1;
1116         return 0;
1117 }
1118
1119 int ntreeOutputExists(bNode *node, bNodeSocket *testsock)
1120 {
1121         bNodeSocket *sock= node->outputs.first;
1122         for (; sock; sock= sock->next)
1123                 if (sock==testsock)
1124                         return 1;
1125         return 0;
1126 }
1127
1128 /* returns localized tree for execution in threads */
1129 bNodeTree *ntreeLocalize(bNodeTree *ntree)
1130 {
1131         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1132
1133         bNodeTree *ltree;
1134         bNode *node;
1135         
1136         bAction *action_backup= NULL, *tmpact_backup= NULL;
1137         
1138         /* Workaround for copying an action on each render!
1139          * set action to NULL so animdata actions don't get copied */
1140         AnimData *adt= BKE_animdata_from_id(&ntree->id);
1141
1142         if (adt) {
1143                 action_backup= adt->action;
1144                 tmpact_backup= adt->tmpact;
1145
1146                 adt->action= NULL;
1147                 adt->tmpact= NULL;
1148         }
1149
1150         /* node copy func */
1151         ltree = ntreeCopyTree_internal(ntree, FALSE);
1152
1153         if (adt) {
1154                 AnimData *ladt= BKE_animdata_from_id(&ltree->id);
1155
1156                 adt->action= ladt->action= action_backup;
1157                 adt->tmpact= ladt->tmpact= tmpact_backup;
1158
1159                 if (action_backup) action_backup->id.us++;
1160                 if (tmpact_backup) tmpact_backup->id.us++;
1161                 
1162         }
1163         /* end animdata uglyness */
1164
1165         /* ensures only a single output node is enabled */
1166         ntreeSetOutput(ntree);
1167
1168         for (node= ntree->nodes.first; node; node= node->next) {
1169                 /* store new_node pointer to original */
1170                 node->new_node->new_node= node;
1171         }
1172
1173         if (ntreetype->localize)
1174                 ntreetype->localize(ltree, ntree);
1175
1176         return ltree;
1177 }
1178
1179 /* sync local composite with real tree */
1180 /* local tree is supposed to be running, be careful moving previews! */
1181 /* is called by jobs manager, outside threads, so it doesnt happen during draw */
1182 void ntreeLocalSync(bNodeTree *localtree, bNodeTree *ntree)
1183 {
1184         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1185
1186         if (ntreetype->local_sync)
1187                 ntreetype->local_sync(localtree, ntree);
1188 }
1189
1190 /* merge local tree results back, and free local tree */
1191 /* we have to assume the editor already changed completely */
1192 void ntreeLocalMerge(bNodeTree *localtree, bNodeTree *ntree)
1193 {
1194         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1195         bNode *lnode;
1196         
1197         /* move over the compbufs and previews */
1198         for (lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
1199                 if (ntreeNodeExists(ntree, lnode->new_node)) {
1200                         if (lnode->preview && lnode->preview->rect) {
1201                                 nodeFreePreview(lnode->new_node);
1202                                 lnode->new_node->preview= lnode->preview;
1203                                 lnode->preview= NULL;
1204                         }
1205                 }
1206         }
1207
1208         if (ntreetype->local_merge)
1209                 ntreetype->local_merge(localtree, ntree);
1210
1211         ntreeFreeTree(localtree);
1212         MEM_freeN(localtree);
1213 }
1214
1215 /* ************ find stuff *************** */
1216
1217 int ntreeHasType(bNodeTree *ntree, int type)
1218 {
1219         bNode *node;
1220         
1221         if (ntree)
1222                 for (node= ntree->nodes.first; node; node= node->next)
1223                         if (node->type == type)
1224                                 return 1;
1225         return 0;
1226 }
1227
1228 bNodeLink *nodeFindLink(bNodeTree *ntree, bNodeSocket *from, bNodeSocket *to)
1229 {
1230         bNodeLink *link;
1231         
1232         for (link= ntree->links.first; link; link= link->next) {
1233                 if (link->fromsock==from && link->tosock==to)
1234                         return link;
1235                 if (link->fromsock==to && link->tosock==from)   /* hrms? */
1236                         return link;
1237         }
1238         return NULL;
1239 }
1240
1241 int nodeCountSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1242 {
1243         bNodeLink *link;
1244         int tot= 0;
1245         
1246         for (link= ntree->links.first; link; link= link->next) {
1247                 if (link->fromsock==sock || link->tosock==sock)
1248                         tot++;
1249         }
1250         return tot;
1251 }
1252
1253 bNode *nodeGetActive(bNodeTree *ntree)
1254 {
1255         bNode *node;
1256         
1257         if (ntree==NULL) return NULL;
1258         
1259         for (node= ntree->nodes.first; node; node= node->next)
1260                 if (node->flag & NODE_ACTIVE)
1261                         break;
1262         return node;
1263 }
1264
1265 /* two active flags, ID nodes have special flag for buttons display */
1266 bNode *nodeGetActiveID(bNodeTree *ntree, short idtype)
1267 {
1268         bNode *node;
1269         
1270         if (ntree==NULL) return NULL;
1271
1272         /* check for group edit */
1273         for (node= ntree->nodes.first; node; node= node->next)
1274                 if (node->flag & NODE_GROUP_EDIT)
1275                         break;
1276
1277         if (node)
1278                 ntree= (bNodeTree*)node->id;
1279         
1280         /* now find active node with this id */
1281         for (node= ntree->nodes.first; node; node= node->next)
1282                 if (node->id && GS(node->id->name)==idtype)
1283                         if (node->flag & NODE_ACTIVE_ID)
1284                                 break;
1285
1286         return node;
1287 }
1288
1289 int nodeSetActiveID(bNodeTree *ntree, short idtype, ID *id)
1290 {
1291         bNode *node;
1292         int ok= FALSE;
1293
1294         if (ntree==NULL) return ok;
1295
1296         /* check for group edit */
1297         for (node= ntree->nodes.first; node; node= node->next)
1298                 if (node->flag & NODE_GROUP_EDIT)
1299                         break;
1300
1301         if (node)
1302                 ntree= (bNodeTree*)node->id;
1303
1304         /* now find active node with this id */
1305         for (node= ntree->nodes.first; node; node= node->next) {
1306                 if (node->id && GS(node->id->name)==idtype) {
1307                         if (id && ok==FALSE && node->id==id) {
1308                                 node->flag |= NODE_ACTIVE_ID;
1309                                 ok= TRUE;
1310                         }
1311                         else {
1312                                 node->flag &= ~NODE_ACTIVE_ID;
1313                         }
1314                 }
1315         }
1316
1317         return ok;
1318 }
1319
1320
1321 /* two active flags, ID nodes have special flag for buttons display */
1322 void nodeClearActiveID(bNodeTree *ntree, short idtype)
1323 {
1324         bNode *node;
1325         
1326         if (ntree==NULL) return;
1327         
1328         for (node= ntree->nodes.first; node; node= node->next)
1329                 if (node->id && GS(node->id->name)==idtype)
1330                         node->flag &= ~NODE_ACTIVE_ID;
1331 }
1332
1333 void nodeClearActive(bNodeTree *ntree)
1334 {
1335         bNode *node;
1336
1337         if (ntree==NULL) return;
1338
1339         for (node= ntree->nodes.first; node; node= node->next)
1340                 node->flag &= ~(NODE_ACTIVE | NODE_ACTIVE_ID);
1341 }
1342
1343
1344 /* two active flags, ID nodes have special flag for buttons display */
1345 void nodeSetActive(bNodeTree *ntree, bNode *node)
1346 {
1347         bNode *tnode;
1348         
1349         /* make sure only one node is active, and only one per ID type */
1350         for (tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1351                 tnode->flag &= ~NODE_ACTIVE;
1352                 
1353                 if (node->id && tnode->id) {
1354                         if (GS(node->id->name) == GS(tnode->id->name))
1355                                 tnode->flag &= ~NODE_ACTIVE_ID;
1356                 }
1357                 if (node->typeinfo->nclass == NODE_CLASS_TEXTURE)
1358                         tnode->flag &= ~NODE_ACTIVE_TEXTURE;
1359         }
1360         
1361         node->flag |= NODE_ACTIVE;
1362         if (node->id)
1363                 node->flag |= NODE_ACTIVE_ID;
1364         if (node->typeinfo->nclass == NODE_CLASS_TEXTURE)
1365                 node->flag |= NODE_ACTIVE_TEXTURE;
1366 }
1367
1368 int nodeSocketIsHidden(bNodeSocket *sock)
1369 {
1370         return ((sock->flag & (SOCK_HIDDEN | SOCK_UNAVAIL)) != 0);
1371 }
1372
1373 void nodeSocketSetType(bNodeSocket *sock, int type)
1374 {
1375         int old_type = sock->type;
1376         void *old_default_value = sock->default_value;
1377         
1378         sock->type = type;
1379         
1380         sock->default_value = node_socket_make_default_value(sock->type);
1381         node_socket_init_default_value(type, sock->default_value);
1382         node_socket_convert_default_value(sock->type, sock->default_value, old_type, old_default_value);
1383         node_socket_free_default_value(old_type, old_default_value);
1384 }
1385
1386 /* ************** dependency stuff *********** */
1387
1388 /* node is guaranteed to be not checked before */
1389 static int node_get_deplist_recurs(bNode *node, bNode ***nsort)
1390 {
1391         bNode *fromnode;
1392         bNodeSocket *sock;
1393         int level = 0xFFF;
1394         
1395         node->done = TRUE;
1396         
1397         /* check linked nodes */
1398         for (sock= node->inputs.first; sock; sock= sock->next) {
1399                 if (sock->link) {
1400                         fromnode= sock->link->fromnode;
1401                         if (fromnode) {
1402                                 if (fromnode->done==0)
1403                                         fromnode->level= node_get_deplist_recurs(fromnode, nsort);
1404                                 if (fromnode->level <= level)
1405                                         level = fromnode->level - 1;
1406                         }
1407                 }
1408         }
1409         
1410         /* check parent node */
1411         if (node->parent) {
1412                 if (node->parent->done==0)
1413                         node->parent->level= node_get_deplist_recurs(node->parent, nsort);
1414                 if (node->parent->level <= level)
1415                         level = node->parent->level - 1;
1416         }
1417         
1418         if (nsort) {
1419                 **nsort= node;
1420                 (*nsort)++;
1421         }
1422         
1423         return level;
1424 }
1425
1426 void ntreeGetDependencyList(struct bNodeTree *ntree, struct bNode ***deplist, int *totnodes)
1427 {
1428         bNode *node, **nsort;
1429         
1430         *totnodes=0;
1431         
1432         /* first clear data */
1433         for (node= ntree->nodes.first; node; node= node->next) {
1434                 node->done = FALSE;
1435                 (*totnodes)++;
1436         }
1437         if (*totnodes==0) {
1438                 *deplist = NULL;
1439                 return;
1440         }
1441         
1442         nsort= *deplist= MEM_callocN((*totnodes)*sizeof(bNode*), "sorted node array");
1443         
1444         /* recursive check */
1445         for (node= ntree->nodes.first; node; node= node->next) {
1446                 if (node->done==0) {
1447                         node->level= node_get_deplist_recurs(node, &nsort);
1448                 }
1449         }
1450 }
1451
1452 /* only updates node->level for detecting cycles links */
1453 static void ntree_update_node_level(bNodeTree *ntree)
1454 {
1455         bNode *node;
1456         
1457         /* first clear tag */
1458         for (node= ntree->nodes.first; node; node= node->next) {
1459                 node->done = FALSE;
1460         }
1461         
1462         /* recursive check */
1463         for (node= ntree->nodes.first; node; node= node->next) {
1464                 if (node->done==0) {
1465                         node->level= node_get_deplist_recurs(node, NULL);
1466                 }
1467         }
1468 }
1469
1470 static void ntree_update_link_pointers(bNodeTree *ntree)
1471 {
1472         bNode *node;
1473         bNodeSocket *sock;
1474         bNodeLink *link;
1475         
1476         /* first clear data */
1477         for (node= ntree->nodes.first; node; node= node->next) {
1478                 for (sock= node->inputs.first; sock; sock= sock->next) {
1479                         sock->link= NULL;
1480                         sock->flag &= ~SOCK_IN_USE;
1481                 }
1482                 for (sock= node->outputs.first; sock; sock= sock->next) {
1483                         sock->flag &= ~SOCK_IN_USE;
1484                 }
1485         }
1486         for (sock= ntree->inputs.first; sock; sock= sock->next) {
1487                 sock->flag &= ~SOCK_IN_USE;
1488         }
1489         for (sock= ntree->outputs.first; sock; sock= sock->next) {
1490                 sock->link= NULL;
1491                 sock->flag &= ~SOCK_IN_USE;
1492         }
1493
1494         for (link= ntree->links.first; link; link= link->next) {
1495                 link->tosock->link= link;
1496                 
1497                 link->fromsock->flag |= SOCK_IN_USE;
1498                 link->tosock->flag |= SOCK_IN_USE;
1499         }
1500 }
1501
1502 static void ntree_validate_links(bNodeTree *ntree)
1503 {
1504         bNodeTreeType *ntreetype = ntreeGetType(ntree->type);
1505         bNodeLink *link;
1506         
1507         for (link = ntree->links.first; link; link = link->next) {
1508                 link->flag |= NODE_LINK_VALID;
1509                 if (link->fromnode && link->tonode && link->fromnode->level <= link->tonode->level)
1510                         link->flag &= ~NODE_LINK_VALID;
1511                 else if (ntreetype->validate_link) {
1512                         if (!ntreetype->validate_link(ntree, link))
1513                                 link->flag &= ~NODE_LINK_VALID;
1514                 }
1515         }
1516 }
1517
1518 static void ntree_verify_nodes_cb(void *calldata, struct ID *UNUSED(owner_id), struct bNodeTree *ntree)
1519 {
1520         ID *id= (ID*)calldata;
1521         bNode *node;
1522         
1523         for (node=ntree->nodes.first; node; node=node->next)
1524                 if (node->typeinfo->verifyfunc)
1525                         node->typeinfo->verifyfunc(ntree, node, id);
1526 }
1527
1528 void ntreeVerifyNodes(struct Main *main, struct ID *id)
1529 {
1530         bNodeTreeType *ntreetype;
1531         bNodeTree *ntree;
1532         int n;
1533         
1534         for (n=0; n < NUM_NTREE_TYPES; ++n) {
1535                 ntreetype= ntreeGetType(n);
1536                 if (ntreetype && ntreetype->foreach_nodetree)
1537                         ntreetype->foreach_nodetree(main, id, ntree_verify_nodes_cb);
1538         }
1539         for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
1540                 ntree_verify_nodes_cb(id, NULL, ntree);
1541 }
1542
1543 void ntreeUpdateTree(bNodeTree *ntree)
1544 {
1545         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1546         bNode *node;
1547         
1548         if (ntree->update & (NTREE_UPDATE_LINKS|NTREE_UPDATE_NODES)) {
1549                 /* set the bNodeSocket->link pointers */
1550                 ntree_update_link_pointers(ntree);
1551                 
1552                 /* update the node level from link dependencies */
1553                 ntree_update_node_level(ntree);
1554         }
1555         
1556         /* update individual nodes */
1557         for (node=ntree->nodes.first; node; node=node->next) {
1558                 /* node tree update tags override individual node update flags */
1559                 if ((node->update & NODE_UPDATE) || (ntree->update & NTREE_UPDATE)) {
1560                         if (ntreetype->update_node)
1561                                 ntreetype->update_node(ntree, node);
1562                         else if (node->typeinfo->updatefunc)
1563                                 node->typeinfo->updatefunc(ntree, node);
1564                 }
1565                 /* clear update flag */
1566                 node->update = 0;
1567         }
1568         
1569         /* check link validity */
1570         if (ntree->update & (NTREE_UPDATE_LINKS|NTREE_UPDATE_NODES))
1571                 ntree_validate_links(ntree);
1572         
1573         /* generic tree update callback */
1574         if (ntreetype->update)
1575                 ntreetype->update(ntree);
1576         else {
1577                 /* Trees can be associated with a specific node type (i.e. group nodes),
1578                  * in that case a tree update function may be defined by that node type.
1579                  */
1580                 bNodeType *ntype= node_get_type(ntree, ntree->nodetype);
1581                 if (ntype && ntype->updatetreefunc)
1582                         ntype->updatetreefunc(ntree);
1583         }
1584         
1585         /* XXX hack, should be done by depsgraph!! */
1586         ntreeVerifyNodes(G.main, &ntree->id);
1587         
1588         /* clear the update flag */
1589         ntree->update = 0;
1590 }
1591
1592 void nodeUpdate(bNodeTree *ntree, bNode *node)
1593 {
1594         bNodeTreeType *ntreetype= ntreeGetType(ntree->type);
1595         
1596         if (ntreetype->update_node)
1597                 ntreetype->update_node(ntree, node);
1598         else if (node->typeinfo->updatefunc)
1599                 node->typeinfo->updatefunc(ntree, node);
1600         /* clear update flag */
1601         node->update = 0;
1602 }
1603
1604 int nodeUpdateID(bNodeTree *ntree, ID *id)
1605 {
1606         bNodeTreeType *ntreetype;
1607         bNode *node;
1608         int change = FALSE;
1609         
1610         if (ELEM(NULL, id, ntree))
1611                 return change;
1612         
1613         ntreetype = ntreeGetType(ntree->type);
1614         
1615         if (ntreetype->update_node) {
1616                 for (node= ntree->nodes.first; node; node= node->next) {
1617                         if (node->id==id) {
1618                                 change = TRUE;
1619                                 node->update |= NODE_UPDATE_ID;
1620                                 ntreetype->update_node(ntree, node);
1621                                 /* clear update flag */
1622                                 node->update = 0;
1623                         }
1624                 }
1625         }
1626         else {
1627                 for (node= ntree->nodes.first; node; node= node->next) {
1628                         if (node->id==id) {
1629                                 change = TRUE;
1630                                 node->update |= NODE_UPDATE_ID;
1631                                 if (node->typeinfo->updatefunc)
1632                                         node->typeinfo->updatefunc(ntree, node);
1633                                 /* clear update flag */
1634                                 node->update = 0;
1635                         }
1636                 }
1637         }
1638         
1639         return change;
1640 }
1641
1642
1643 /* ************* node type access ********** */
1644
1645 int nodeValid(bNodeTree *ntree, bNodeTemplate *ntemp)
1646 {
1647         bNodeType *ntype= node_get_type(ntree, ntemp->type);
1648         if (ntype) {
1649                 if (ntype->validfunc)
1650                         return ntype->validfunc(ntree, ntemp);
1651                 else
1652                         return 1;
1653         }
1654         else
1655                 return 0;
1656 }
1657
1658 const char* nodeLabel(bNode *node)
1659 {
1660         if (node->label[0]!='\0')
1661                 return node->label;
1662         else if (node->typeinfo->labelfunc)
1663                 return node->typeinfo->labelfunc(node);
1664         else
1665                 return IFACE_(node->typeinfo->name);
1666 }
1667
1668 struct bNodeTree *nodeGroupEditGet(struct bNode *node)
1669 {
1670         if (node->typeinfo->group_edit_get)
1671                 return node->typeinfo->group_edit_get(node);
1672         else
1673                 return NULL;
1674 }
1675
1676 struct bNodeTree *nodeGroupEditSet(struct bNode *node, int edit)
1677 {
1678         if (node->typeinfo->group_edit_set)
1679                 return node->typeinfo->group_edit_set(node, edit);
1680         else if (node->typeinfo->group_edit_get)
1681                 return node->typeinfo->group_edit_get(node);
1682         else
1683                 return NULL;
1684 }
1685
1686 void nodeGroupEditClear(struct bNode *node)
1687 {
1688         if (node->typeinfo->group_edit_clear)
1689                 node->typeinfo->group_edit_clear(node);
1690 }
1691
1692 struct bNodeTemplate nodeMakeTemplate(struct bNode *node)
1693 {
1694         bNodeTemplate ntemp;
1695         if (node->typeinfo->templatefunc)
1696                 return node->typeinfo->templatefunc(node);
1697         else {
1698                 ntemp.type = node->type;
1699                 return ntemp;
1700         }
1701 }
1702
1703 void node_type_base(bNodeTreeType *ttype, bNodeType *ntype, int type, const char *name, short nclass, short flag)
1704 {
1705         memset(ntype, 0, sizeof(bNodeType));
1706
1707         ntype->type = type;
1708         BLI_strncpy(ntype->name, name, sizeof(ntype->name));
1709         ntype->nclass = nclass;
1710         ntype->flag = flag;
1711
1712         /* Default muting stuff. */
1713         if (ttype)
1714                 ntype->internal_connect = ttype->internal_connect;
1715
1716         /* default size values */
1717         ntype->width = 140;
1718         ntype->minwidth = 100;
1719         ntype->maxwidth = 320;
1720         ntype->height = 100;
1721         ntype->minheight = 30;
1722         ntype->maxheight = FLT_MAX;
1723 }
1724
1725 void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
1726 {
1727         ntype->inputs = inputs;
1728         ntype->outputs = outputs;
1729 }
1730
1731 void node_type_init(struct bNodeType *ntype, void (*initfunc)(struct bNodeTree *ntree, struct bNode *node, struct bNodeTemplate *ntemp))
1732 {
1733         ntype->initfunc = initfunc;
1734 }
1735
1736 void node_type_valid(struct bNodeType *ntype, int (*validfunc)(struct bNodeTree *ntree, struct bNodeTemplate *ntemp))
1737 {
1738         ntype->validfunc = validfunc;
1739 }
1740
1741 void node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth)
1742 {
1743         ntype->width = width;
1744         ntype->minwidth = minwidth;
1745         if (maxwidth <= minwidth)
1746                 ntype->maxwidth = FLT_MAX;
1747         else
1748                 ntype->maxwidth = maxwidth;
1749 }
1750
1751 void node_type_storage(bNodeType *ntype, const char *storagename, void (*freestoragefunc)(struct bNode *), void (*copystoragefunc)(struct bNode *, struct bNode *))
1752 {
1753         if (storagename)
1754                 BLI_strncpy(ntype->storagename, storagename, sizeof(ntype->storagename));
1755         else
1756                 ntype->storagename[0] = '\0';
1757         ntype->copystoragefunc = copystoragefunc;
1758         ntype->freestoragefunc = freestoragefunc;
1759 }
1760
1761 void node_type_label(struct bNodeType *ntype, const char *(*labelfunc)(struct bNode *))
1762 {
1763         ntype->labelfunc = labelfunc;
1764 }
1765
1766 void node_type_template(struct bNodeType *ntype, struct bNodeTemplate (*templatefunc)(struct bNode *))
1767 {
1768         ntype->templatefunc = templatefunc;
1769 }
1770
1771 void node_type_update(struct bNodeType *ntype,
1772                                           void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node),
1773                                           void (*verifyfunc)(struct bNodeTree *ntree, struct bNode *node, struct ID *id))
1774 {
1775         ntype->updatefunc = updatefunc;
1776         ntype->verifyfunc = verifyfunc;
1777 }
1778
1779 void node_type_tree(struct bNodeType *ntype, void (*inittreefunc)(struct bNodeTree *), void (*updatetreefunc)(struct bNodeTree *))
1780 {
1781         ntype->inittreefunc = inittreefunc;
1782         ntype->updatetreefunc = updatetreefunc;
1783 }
1784
1785 void node_type_group_edit(struct bNodeType *ntype,
1786                                                   struct bNodeTree *(*group_edit_get)(struct bNode *node),
1787                                                   struct bNodeTree *(*group_edit_set)(struct bNode *node, int edit),
1788                                                   void (*group_edit_clear)(struct bNode *node))
1789 {
1790         ntype->group_edit_get = group_edit_get;
1791         ntype->group_edit_set = group_edit_set;
1792         ntype->group_edit_clear = group_edit_clear;
1793 }
1794
1795 void node_type_exec(struct bNodeType *ntype, void (*execfunc)(void *data, struct bNode *, struct bNodeStack **, struct bNodeStack **))
1796 {
1797         ntype->execfunc = execfunc;
1798 }
1799
1800 void node_type_exec_new(struct bNodeType *ntype,
1801                                                 void *(*initexecfunc)(struct bNode *node),
1802                                                 void (*freeexecfunc)(struct bNode *node, void *nodedata),
1803                                                 void (*newexecfunc)(void *data, int thread, struct bNode *, void *nodedata, struct bNodeStack **, struct bNodeStack **))
1804 {
1805         ntype->initexecfunc = initexecfunc;
1806         ntype->freeexecfunc = freeexecfunc;
1807         ntype->newexecfunc = newexecfunc;
1808 }
1809
1810 void node_type_internal_connect(bNodeType *ntype, ListBase (*internal_connect)(bNodeTree *, bNode *))
1811 {
1812         ntype->internal_connect = internal_connect;
1813 }
1814
1815 void node_type_gpu(struct bNodeType *ntype, int (*gpufunc)(struct GPUMaterial *mat, struct bNode *node, struct GPUNodeStack *in, struct GPUNodeStack *out))
1816 {
1817         ntype->gpufunc = gpufunc;
1818 }
1819
1820 void node_type_gpu_ext(struct bNodeType *ntype, int (*gpuextfunc)(struct GPUMaterial *mat, struct bNode *node, void *nodedata, struct GPUNodeStack *in, struct GPUNodeStack *out))
1821 {
1822         ntype->gpuextfunc = gpuextfunc;
1823 }
1824
1825 void node_type_compatibility(struct bNodeType *ntype, short compatibility)
1826 {
1827         ntype->compatibility = compatibility;
1828 }
1829
1830 static bNodeType *is_nodetype_registered(ListBase *typelist, int type) 
1831 {
1832         bNodeType *ntype= typelist->first;
1833         
1834         for (;ntype; ntype= ntype->next )
1835                 if (ntype->type==type)
1836                         return ntype;
1837         
1838         return NULL;
1839 }
1840
1841 void nodeRegisterType(bNodeTreeType *ttype, bNodeType *ntype) 
1842 {
1843         ListBase *typelist = &(ttype->node_types);
1844         bNodeType *found= is_nodetype_registered(typelist, ntype->type);
1845         
1846         if (found==NULL)
1847                 BLI_addtail(typelist, ntype);
1848 }
1849
1850 static void registerCompositNodes(bNodeTreeType *ttype)
1851 {
1852         register_node_type_frame(ttype);
1853         register_node_type_reroute(ttype);
1854         
1855         register_node_type_cmp_group(ttype);
1856 //      register_node_type_cmp_forloop(ttype);
1857 //      register_node_type_cmp_whileloop(ttype);
1858         
1859         register_node_type_cmp_rlayers(ttype);
1860         register_node_type_cmp_image(ttype);
1861         register_node_type_cmp_texture(ttype);
1862         register_node_type_cmp_value(ttype);
1863         register_node_type_cmp_rgb(ttype);
1864         register_node_type_cmp_curve_time(ttype);
1865         register_node_type_cmp_movieclip(ttype);
1866         
1867         register_node_type_cmp_composite(ttype);
1868         register_node_type_cmp_viewer(ttype);
1869         register_node_type_cmp_splitviewer(ttype);
1870         register_node_type_cmp_output_file(ttype);
1871         register_node_type_cmp_view_levels(ttype);
1872         
1873         register_node_type_cmp_curve_rgb(ttype);
1874         register_node_type_cmp_mix_rgb(ttype);
1875         register_node_type_cmp_hue_sat(ttype);
1876         register_node_type_cmp_brightcontrast(ttype);
1877         register_node_type_cmp_gamma(ttype);
1878         register_node_type_cmp_invert(ttype);
1879         register_node_type_cmp_alphaover(ttype);
1880         register_node_type_cmp_zcombine(ttype);
1881         register_node_type_cmp_colorbalance(ttype);
1882         register_node_type_cmp_huecorrect(ttype);
1883         
1884         register_node_type_cmp_normal(ttype);
1885         register_node_type_cmp_curve_vec(ttype);
1886         register_node_type_cmp_map_value(ttype);
1887         register_node_type_cmp_normalize(ttype);
1888         
1889         register_node_type_cmp_filter(ttype);
1890         register_node_type_cmp_blur(ttype);
1891         register_node_type_cmp_dblur(ttype);
1892         register_node_type_cmp_bilateralblur(ttype);
1893         register_node_type_cmp_vecblur(ttype);
1894         register_node_type_cmp_dilateerode(ttype);
1895         register_node_type_cmp_defocus(ttype);
1896         
1897         register_node_type_cmp_valtorgb(ttype);
1898         register_node_type_cmp_rgbtobw(ttype);
1899         register_node_type_cmp_setalpha(ttype);
1900         register_node_type_cmp_idmask(ttype);
1901         register_node_type_cmp_math(ttype);
1902         register_node_type_cmp_seprgba(ttype);
1903         register_node_type_cmp_combrgba(ttype);
1904         register_node_type_cmp_sephsva(ttype);
1905         register_node_type_cmp_combhsva(ttype);
1906         register_node_type_cmp_sepyuva(ttype);
1907         register_node_type_cmp_combyuva(ttype);
1908         register_node_type_cmp_sepycca(ttype);
1909         register_node_type_cmp_combycca(ttype);
1910         register_node_type_cmp_premulkey(ttype);
1911         
1912         register_node_type_cmp_diff_matte(ttype);
1913         register_node_type_cmp_distance_matte(ttype);
1914         register_node_type_cmp_chroma_matte(ttype);
1915         register_node_type_cmp_color_matte(ttype);
1916         register_node_type_cmp_channel_matte(ttype);
1917         register_node_type_cmp_color_spill(ttype);
1918         register_node_type_cmp_luma_matte(ttype);
1919         register_node_type_cmp_doubleedgemask(ttype);
1920         register_node_type_cmp_keyingscreen(ttype);
1921         register_node_type_cmp_keying(ttype);
1922
1923         register_node_type_cmp_translate(ttype);
1924         register_node_type_cmp_rotate(ttype);
1925         register_node_type_cmp_scale(ttype);
1926         register_node_type_cmp_flip(ttype);
1927         register_node_type_cmp_crop(ttype);
1928         register_node_type_cmp_displace(ttype);
1929         register_node_type_cmp_mapuv(ttype);
1930         register_node_type_cmp_glare(ttype);
1931         register_node_type_cmp_tonemap(ttype);
1932         register_node_type_cmp_lensdist(ttype);
1933         register_node_type_cmp_transform(ttype);
1934         register_node_type_cmp_stabilize2d(ttype);
1935         register_node_type_cmp_moviedistortion(ttype);
1936
1937         register_node_type_cmp_colorcorrection(ttype);
1938         register_node_type_cmp_boxmask(ttype);
1939         register_node_type_cmp_ellipsemask(ttype);
1940         register_node_type_cmp_bokehimage(ttype);
1941         register_node_type_cmp_bokehblur(ttype);
1942         register_node_type_cmp_switch(ttype);
1943
1944         register_node_type_cmp_mask(ttype);
1945         register_node_type_cmp_trackpos(ttype);
1946 }
1947
1948 static void registerShaderNodes(bNodeTreeType *ttype) 
1949 {
1950         register_node_type_frame(ttype);
1951         register_node_type_reroute(ttype);
1952         
1953         register_node_type_sh_group(ttype);
1954         //register_node_type_sh_forloop(ttype);
1955         //register_node_type_sh_whileloop(ttype);
1956
1957         register_node_type_sh_output(ttype);
1958         register_node_type_sh_material(ttype);
1959         register_node_type_sh_camera(ttype);
1960         register_node_type_sh_gamma(ttype);
1961         register_node_type_sh_brightcontrast(ttype);
1962         register_node_type_sh_value(ttype);
1963         register_node_type_sh_rgb(ttype);
1964         register_node_type_sh_mix_rgb(ttype);
1965         register_node_type_sh_valtorgb(ttype);
1966         register_node_type_sh_rgbtobw(ttype);
1967         register_node_type_sh_texture(ttype);
1968         register_node_type_sh_normal(ttype);
1969         register_node_type_sh_geom(ttype);
1970         register_node_type_sh_mapping(ttype);
1971         register_node_type_sh_curve_vec(ttype);
1972         register_node_type_sh_curve_rgb(ttype);
1973         register_node_type_sh_math(ttype);
1974         register_node_type_sh_vect_math(ttype);
1975         register_node_type_sh_squeeze(ttype);
1976         register_node_type_sh_material_ext(ttype);
1977         register_node_type_sh_invert(ttype);
1978         register_node_type_sh_seprgb(ttype);
1979         register_node_type_sh_combrgb(ttype);
1980         register_node_type_sh_hue_sat(ttype);
1981
1982         register_node_type_sh_attribute(ttype);
1983         register_node_type_sh_geometry(ttype);
1984         register_node_type_sh_light_path(ttype);
1985         register_node_type_sh_light_falloff(ttype);
1986         register_node_type_sh_object_info(ttype);
1987         register_node_type_sh_fresnel(ttype);
1988         register_node_type_sh_layer_weight(ttype);
1989         register_node_type_sh_tex_coord(ttype);
1990         register_node_type_sh_particle_info(ttype);
1991
1992         register_node_type_sh_background(ttype);
1993         register_node_type_sh_bsdf_diffuse(ttype);
1994         register_node_type_sh_bsdf_glossy(ttype);
1995         register_node_type_sh_bsdf_glass(ttype);
1996         register_node_type_sh_bsdf_translucent(ttype);
1997         register_node_type_sh_bsdf_transparent(ttype);
1998         register_node_type_sh_bsdf_velvet(ttype);
1999         register_node_type_sh_emission(ttype);
2000         register_node_type_sh_holdout(ttype);
2001         //register_node_type_sh_volume_transparent(ttype);
2002         //register_node_type_sh_volume_isotropic(ttype);
2003         register_node_type_sh_mix_shader(ttype);
2004         register_node_type_sh_add_shader(ttype);
2005
2006         register_node_type_sh_output_lamp(ttype);
2007         register_node_type_sh_output_material(ttype);
2008         register_node_type_sh_output_world(ttype);
2009
2010         register_node_type_sh_tex_image(ttype);
2011         register_node_type_sh_tex_environment(ttype);
2012         register_node_type_sh_tex_sky(ttype);
2013         register_node_type_sh_tex_noise(ttype);
2014         register_node_type_sh_tex_wave(ttype);
2015         register_node_type_sh_tex_voronoi(ttype);
2016         register_node_type_sh_tex_musgrave(ttype);
2017         register_node_type_sh_tex_gradient(ttype);
2018         register_node_type_sh_tex_magic(ttype);
2019         register_node_type_sh_tex_checker(ttype);
2020 }
2021
2022 static void registerTextureNodes(bNodeTreeType *ttype)
2023 {
2024         register_node_type_frame(ttype);
2025         register_node_type_reroute(ttype);
2026         
2027         register_node_type_tex_group(ttype);
2028 //      register_node_type_tex_forloop(ttype);
2029 //      register_node_type_tex_whileloop(ttype);
2030         
2031         register_node_type_tex_math(ttype);
2032         register_node_type_tex_mix_rgb(ttype);
2033         register_node_type_tex_valtorgb(ttype);
2034         register_node_type_tex_rgbtobw(ttype);
2035         register_node_type_tex_valtonor(ttype);
2036         register_node_type_tex_curve_rgb(ttype);
2037         register_node_type_tex_curve_time(ttype);
2038         register_node_type_tex_invert(ttype);
2039         register_node_type_tex_hue_sat(ttype);
2040         register_node_type_tex_coord(ttype);
2041         register_node_type_tex_distance(ttype);
2042         register_node_type_tex_compose(ttype);
2043         register_node_type_tex_decompose(ttype);
2044         
2045         register_node_type_tex_output(ttype);
2046         register_node_type_tex_viewer(ttype);
2047         
2048         register_node_type_tex_checker(ttype);
2049         register_node_type_tex_texture(ttype);
2050         register_node_type_tex_bricks(ttype);
2051         register_node_type_tex_image(ttype);
2052         
2053         register_node_type_tex_rotate(ttype);
2054         register_node_type_tex_translate(ttype);
2055         register_node_type_tex_scale(ttype);
2056         register_node_type_tex_at(ttype);
2057         
2058         register_node_type_tex_proc_voronoi(ttype);
2059         register_node_type_tex_proc_blend(ttype);
2060         register_node_type_tex_proc_magic(ttype);
2061         register_node_type_tex_proc_marble(ttype);
2062         register_node_type_tex_proc_clouds(ttype);
2063         register_node_type_tex_proc_wood(ttype);
2064         register_node_type_tex_proc_musgrave(ttype);
2065         register_node_type_tex_proc_noise(ttype);
2066         register_node_type_tex_proc_stucci(ttype);
2067         register_node_type_tex_proc_distnoise(ttype);
2068 }
2069
2070 static void free_typeinfos(ListBase *list)
2071 {
2072         bNodeType *ntype, *next;
2073         for (ntype=list->first; ntype; ntype=next) {
2074                 next = ntype->next;
2075
2076                 if (ntype->needs_free)
2077                         MEM_freeN(ntype);
2078         }
2079 }
2080
2081 void init_nodesystem(void) 
2082 {
2083         registerCompositNodes(ntreeGetType(NTREE_COMPOSIT));
2084         registerShaderNodes(ntreeGetType(NTREE_SHADER));
2085         registerTextureNodes(ntreeGetType(NTREE_TEXTURE));
2086 }
2087
2088 void free_nodesystem(void) 
2089 {
2090         free_typeinfos(&ntreeGetType(NTREE_COMPOSIT)->node_types);
2091         free_typeinfos(&ntreeGetType(NTREE_SHADER)->node_types);
2092         free_typeinfos(&ntreeGetType(NTREE_TEXTURE)->node_types);
2093 }
2094
2095 /* called from BKE_scene_unlink, when deleting a scene goes over all scenes
2096  * other than the input, checks if they have render layer nodes referencing
2097  * the to-be-deleted scene, and resets them to NULL. */
2098
2099 /* XXX needs to get current scene then! */
2100 void clear_scene_in_nodes(Main *bmain, Scene *sce)
2101 {
2102         Scene *sce1;
2103         bNode *node;
2104
2105         for (sce1= bmain->scene.first; sce1; sce1=sce1->id.next) {
2106                 if (sce1!=sce) {
2107                         if (sce1->nodetree) {
2108                                 for (node= sce1->nodetree->nodes.first; node; node= node->next) {
2109                                         if (node->type==CMP_NODE_R_LAYERS) {
2110                                                 Scene *nodesce= (Scene *)node->id;
2111                                                 
2112                                                 if (nodesce==sce) node->id = NULL;
2113                                         }
2114                                 }
2115                         }
2116                 }
2117         }
2118 }