style cleanup
[blender.git] / source / blender / editors / space_node / node_edit.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): David Millan Escriva, Juho Vepsäläinen, Nathan Letwory
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_node/node_edit.c
29  *  \ingroup spnode
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_lamp_types.h"
35 #include "DNA_material_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_world_types.h"
39
40 #include "BLI_math.h"
41 #include "BLI_blenlib.h"
42
43 #include "BKE_blender.h"
44 #include "BKE_context.h"
45 #include "BKE_depsgraph.h"
46 #include "BKE_global.h"
47 #include "BKE_image.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_material.h"
51 #include "BKE_node.h"
52 #include "BKE_paint.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_texture.h"
56
57 #include "RE_pipeline.h"
58
59
60 #include "ED_node.h"  /* own include */
61 #include "ED_screen.h"
62 #include "ED_render.h"
63
64 #include "RNA_access.h"
65 #include "RNA_define.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "UI_view2d.h"
71
72 #include "GPU_material.h"
73
74 #include "node_intern.h"  /* own include */
75
76 #define USE_ESC_COMPO
77
78 /* ***************** composite job manager ********************** */
79
80 typedef struct CompoJob {
81         Scene *scene;
82         bNodeTree *ntree;
83         bNodeTree *localtree;
84         short *stop;
85         short *do_update;
86         float *progress;
87 } CompoJob;
88
89 /* called by compo, only to check job 'stop' value */
90 static int compo_breakjob(void *cjv)
91 {
92         CompoJob *cj = cjv;
93         
94         /* without G.is_break 'ESC' wont quit - which annoys users */
95         return (*(cj->stop)
96 #ifdef USE_ESC_COMPO
97                 ||
98                 G.is_break
99 #endif
100                 );
101 }
102
103 /* called by compo, wmJob sends notifier */
104 static void compo_redrawjob(void *cjv, char *UNUSED(str))
105 {
106         CompoJob *cj = cjv;
107         
108         *(cj->do_update) = TRUE;
109 }
110
111 static void compo_freejob(void *cjv)
112 {
113         CompoJob *cj = cjv;
114
115         if (cj->localtree) {
116                 ntreeLocalMerge(cj->localtree, cj->ntree);
117         }
118         MEM_freeN(cj);
119 }
120
121 /* only now we copy the nodetree, so adding many jobs while
122  * sliding buttons doesn't frustrate */
123 static void compo_initjob(void *cjv)
124 {
125         CompoJob *cj = cjv;
126
127         cj->localtree = ntreeLocalize(cj->ntree);
128 }
129
130 /* called before redraw notifiers, it moves finished previews over */
131 static void compo_updatejob(void *cjv)
132 {
133         CompoJob *cj = cjv;
134         
135         ntreeLocalSync(cj->localtree, cj->ntree);
136 }
137
138 static void compo_progressjob(void *cjv, float progress)
139 {
140         CompoJob *cj = cjv;
141         
142         *(cj->progress) = progress;
143 }
144
145
146 /* only this runs inside thread */
147 static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
148 {
149         CompoJob *cj = cjv;
150         bNodeTree *ntree = cj->localtree;
151
152         if (cj->scene->use_nodes == FALSE)
153                 return;
154         
155         cj->stop = stop;
156         cj->do_update = do_update;
157         cj->progress = progress;
158
159         ntree->test_break = compo_breakjob;
160         ntree->tbh = cj;
161         ntree->stats_draw = compo_redrawjob;
162         ntree->sdh = cj;
163         ntree->progress = compo_progressjob;
164         ntree->prh = cj;
165         
166         // XXX BIF_store_spare();
167         
168         ntreeCompositExecTree(ntree, &cj->scene->r, 0, 1);  /* 1 is do_previews */
169
170         ntree->test_break = NULL;
171         ntree->stats_draw = NULL;
172         ntree->progress = NULL;
173
174 }
175
176 /**
177  * \param sa_owner is the owner of the job,
178  * we don't use it for anything else currently so could also be a void pointer,
179  * but for now keep it an 'Scene' for consistency.
180  *
181  * \note only call from spaces `refresh` callbacks, not direct! - use with care.
182  */
183 void ED_node_composite_job(const bContext *C, struct bNodeTree *nodetree, Scene *scene_owner)
184 {
185         wmJob *wm_job;
186         CompoJob *cj;
187
188         /* to fix bug: [#32272] */
189         if (G.is_rendering) {
190                 return;
191         }
192
193 #ifdef USE_ESC_COMPO
194         G.is_break = FALSE;
195 #endif
196
197         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene_owner, "Compositing",
198                              WM_JOB_EXCL_RENDER | WM_JOB_PROGRESS, WM_JOB_TYPE_COMPOSITE);
199         cj = MEM_callocN(sizeof(CompoJob), "compo job");
200
201         /* customdata for preview thread */
202         cj->scene = CTX_data_scene(C);
203         cj->ntree = nodetree;
204
205         /* setup job */
206         WM_jobs_customdata_set(wm_job, cj, compo_freejob);
207         WM_jobs_timer(wm_job, 0.1, NC_SCENE, NC_SCENE | ND_COMPO_RESULT);
208         WM_jobs_callbacks(wm_job, compo_startjob, compo_initjob, compo_updatejob, NULL);
209
210         WM_jobs_start(CTX_wm_manager(C), wm_job);
211 }
212
213 /* ***************************************** */
214
215 /* operator poll callback */
216 int composite_node_active(bContext *C)
217 {
218         if (ED_operator_node_active(C)) {
219                 SpaceNode *snode = CTX_wm_space_node(C);
220                 if (snode->treetype == NTREE_COMPOSIT)
221                         return 1;
222         }
223         return 0;
224 }
225
226 /* also checks for edited groups */
227 bNode *editnode_get_active(bNodeTree *ntree)
228 {
229         bNode *node;
230         
231         /* check for edited group */
232         for (node = ntree->nodes.first; node; node = node->next)
233                 if (nodeGroupEditGet(node))
234                         break;
235         if (node)
236                 return nodeGetActive((bNodeTree *)node->id);
237         else
238                 return nodeGetActive(ntree);
239 }
240
241 static int has_nodetree(bNodeTree *ntree, bNodeTree *lookup)
242 {
243         bNode *node;
244         
245         if (ntree == lookup)
246                 return 1;
247         
248         for (node = ntree->nodes.first; node; node = node->next)
249                 if (node->type == NODE_GROUP && node->id)
250                         if (has_nodetree((bNodeTree *)node->id, lookup))
251                                 return 1;
252         
253         return 0;
254 }
255
256 static void snode_dag_update_group(void *calldata, ID *owner_id, bNodeTree *ntree)
257 {
258         if (has_nodetree(ntree, calldata))
259                 DAG_id_tag_update(owner_id, 0);
260 }
261
262 void snode_dag_update(bContext *C, SpaceNode *snode)
263 {
264         Main *bmain = CTX_data_main(C);
265
266         /* for groups, update all ID's using this */
267         if (snode->edittree != snode->nodetree) {
268                 bNodeTreeType *tti = ntreeGetType(snode->edittree->type);
269                 tti->foreach_nodetree(bmain, snode->edittree, snode_dag_update_group);
270         }
271
272         DAG_id_tag_update(snode->id, 0);
273 }
274
275 void snode_notify(bContext *C, SpaceNode *snode)
276 {
277         WM_event_add_notifier(C, NC_NODE | NA_EDITED, NULL);
278
279         if (snode->treetype == NTREE_SHADER)
280                 WM_event_add_notifier(C, NC_MATERIAL | ND_NODES, snode->id);
281         else if (snode->treetype == NTREE_COMPOSIT)
282                 WM_event_add_notifier(C, NC_SCENE | ND_NODES, snode->id);
283         else if (snode->treetype == NTREE_TEXTURE)
284                 WM_event_add_notifier(C, NC_TEXTURE | ND_NODES, snode->id);
285 }
286
287 bNode *node_tree_get_editgroup(bNodeTree *nodetree)
288 {
289         bNode *gnode;
290         
291         /* get the groupnode */
292         for (gnode = nodetree->nodes.first; gnode; gnode = gnode->next)
293                 if (nodeGroupEditGet(gnode))
294                         break;
295         return gnode;
296 }
297
298 /* assumes nothing being done in ntree yet, sets the default in/out node */
299 /* called from shading buttons or header */
300 void ED_node_shader_default(Scene *scene, ID *id)
301 {
302         bNode *in, *out;
303         bNodeSocket *fromsock, *tosock, *sock;
304         bNodeTree *ntree;
305         bNodeTemplate ntemp;
306         int output_type, shader_type;
307         float color[3], strength = 1.0f;
308         
309         ntree = ntreeAddTree("Shader Nodetree", NTREE_SHADER, 0);
310
311         switch (GS(id->name)) {
312                 case ID_MA:
313                 {
314                         Material *ma = (Material *)id;
315                         ma->nodetree = ntree;
316
317                         if (BKE_scene_use_new_shading_nodes(scene)) {
318                                 output_type = SH_NODE_OUTPUT_MATERIAL;
319                                 shader_type = SH_NODE_BSDF_DIFFUSE;
320                         }
321                         else {
322                                 output_type = SH_NODE_OUTPUT;
323                                 shader_type = SH_NODE_MATERIAL;
324                         }
325
326                         copy_v3_v3(color, &ma->r);
327                         strength = 0.0f;
328                         break;
329                 }
330                 case ID_WO:
331                 {
332                         World *wo = (World *)id;
333                         wo->nodetree = ntree;
334
335                         output_type = SH_NODE_OUTPUT_WORLD;
336                         shader_type = SH_NODE_BACKGROUND;
337
338                         copy_v3_v3(color, &wo->horr);
339                         strength = 1.0f;
340                         break;
341                 }
342                 case ID_LA:
343                 {
344                         Lamp *la = (Lamp *)id;
345                         la->nodetree = ntree;
346
347                         output_type = SH_NODE_OUTPUT_LAMP;
348                         shader_type = SH_NODE_EMISSION;
349
350                         copy_v3_v3(color, &la->r);
351                         if (la->type == LA_LOCAL || la->type == LA_SPOT || la->type == LA_AREA)
352                                 strength = 100.0f;
353                         else
354                                 strength = 1.0f;
355                         break;
356                 }
357                 default:
358                         printf("ED_node_shader_default called on wrong ID type.\n");
359                         return;
360         }
361         
362         ntemp.type = output_type;
363         out = nodeAddNode(ntree, &ntemp);
364         out->locx = 300.0f; out->locy = 300.0f;
365         
366         ntemp.type = shader_type;
367         in = nodeAddNode(ntree, &ntemp);
368         in->locx = 10.0f; in->locy = 300.0f;
369         nodeSetActive(ntree, in);
370         
371         /* only a link from color to color */
372         fromsock = in->outputs.first;
373         tosock = out->inputs.first;
374         nodeAddLink(ntree, in, fromsock, out, tosock);
375
376         /* default values */
377         if (BKE_scene_use_new_shading_nodes(scene)) {
378                 sock = in->inputs.first;
379                 copy_v3_v3(((bNodeSocketValueRGBA *)sock->default_value)->value, color);
380
381                 if (strength != 0.0f) {
382                         sock = in->inputs.last;
383                         ((bNodeSocketValueFloat *)sock->default_value)->value = strength;
384                 }
385         }
386         
387         ntreeUpdateTree(ntree);
388 }
389
390 /* assumes nothing being done in ntree yet, sets the default in/out node */
391 /* called from shading buttons or header */
392 void ED_node_composit_default(Scene *sce)
393 {
394         bNode *in, *out;
395         bNodeSocket *fromsock, *tosock;
396         bNodeTemplate ntemp;
397         
398         /* but lets check it anyway */
399         if (sce->nodetree) {
400                 if (G.debug & G_DEBUG)
401                         printf("error in composite initialize\n");
402                 return;
403         }
404         
405         sce->nodetree = ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, 0);
406
407         sce->nodetree->chunksize = 256;
408         sce->nodetree->edit_quality = NTREE_QUALITY_HIGH;
409         sce->nodetree->render_quality = NTREE_QUALITY_HIGH;
410         
411         ntemp.type = CMP_NODE_COMPOSITE;
412         out = nodeAddNode(sce->nodetree, &ntemp);
413         out->locx = 300.0f; out->locy = 400.0f;
414         out->id = &sce->id;
415         id_us_plus(out->id);
416         
417         ntemp.type = CMP_NODE_R_LAYERS;
418         in = nodeAddNode(sce->nodetree, &ntemp);
419         in->locx = 10.0f; in->locy = 400.0f;
420         in->id = &sce->id;
421         id_us_plus(in->id);
422         nodeSetActive(sce->nodetree, in);
423         
424         /* links from color to color */
425         fromsock = in->outputs.first;
426         tosock = out->inputs.first;
427         nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
428         
429         ntreeUpdateTree(sce->nodetree);
430         
431         // XXX ntreeCompositForceHidden(sce->nodetree);
432 }
433
434 /* assumes nothing being done in ntree yet, sets the default in/out node */
435 /* called from shading buttons or header */
436 void ED_node_texture_default(Tex *tx)
437 {
438         bNode *in, *out;
439         bNodeSocket *fromsock, *tosock;
440         bNodeTemplate ntemp;
441         
442         /* but lets check it anyway */
443         if (tx->nodetree) {
444                 if (G.debug & G_DEBUG)
445                         printf("error in texture initialize\n");
446                 return;
447         }
448         
449         tx->nodetree = ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, 0);
450         
451         ntemp.type = TEX_NODE_OUTPUT;
452         out = nodeAddNode(tx->nodetree, &ntemp);
453         out->locx = 300.0f; out->locy = 300.0f;
454         
455         ntemp.type = TEX_NODE_CHECKER;
456         in = nodeAddNode(tx->nodetree, &ntemp);
457         in->locx = 10.0f; in->locy = 300.0f;
458         nodeSetActive(tx->nodetree, in);
459         
460         fromsock = in->outputs.first;
461         tosock = out->inputs.first;
462         nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
463         
464         ntreeUpdateTree(tx->nodetree);
465 }
466
467 /* id is supposed to contain a node tree */
468 void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
469 {
470         if (id) {
471                 bNode *node = NULL;
472                 short idtype = GS(id->name);
473         
474                 if (idtype == ID_NT) {
475                         *ntree = (bNodeTree *)id;
476                         if (treetype) *treetype = (*ntree)->type;
477                 }
478                 else if (idtype == ID_MA) {
479                         *ntree = ((Material *)id)->nodetree;
480                         if (treetype) *treetype = NTREE_SHADER;
481                 }
482                 else if (idtype == ID_LA) {
483                         *ntree = ((Lamp *)id)->nodetree;
484                         if (treetype) *treetype = NTREE_SHADER;
485                 }
486                 else if (idtype == ID_WO) {
487                         *ntree = ((World *)id)->nodetree;
488                         if (treetype) *treetype = NTREE_SHADER;
489                 }
490                 else if (idtype == ID_SCE) {
491                         *ntree = ((Scene *)id)->nodetree;
492                         if (treetype) *treetype = NTREE_COMPOSIT;
493                 }
494                 else if (idtype == ID_TE) {
495                         *ntree = ((Tex *)id)->nodetree;
496                         if (treetype) *treetype = NTREE_TEXTURE;
497                 }
498                 else {
499                         if (treetype) *treetype = 0;
500                         return;
501                 }
502         
503                 /* find editable group */
504                 if (edittree) {
505                         if (*ntree)
506                                 for (node = (*ntree)->nodes.first; node; node = node->next)
507                                         if (nodeGroupEditGet(node))
508                                                 break;
509                         
510                         if (node && node->id)
511                                 *edittree = (bNodeTree *)node->id;
512                         else
513                                 *edittree = *ntree;
514                 }
515         }
516         else {
517                 *ntree = NULL;
518                 *edittree = NULL;
519                 if (treetype) *treetype = 0;
520         }
521 }
522
523 /* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
524 void snode_set_context(SpaceNode *snode, Scene *scene)
525 {
526         Object *ob = OBACT;
527         
528         snode->id = snode->from = NULL;
529         
530         if (snode->treetype == NTREE_SHADER) {
531                 /* need active object, or we allow pinning... */
532                 if (snode->shaderfrom == SNODE_SHADER_OBJECT) {
533                         if (ob) {
534                                 if (ob->type == OB_LAMP) {
535                                         snode->from = &ob->id;
536                                         snode->id = ob->data;
537                                 }
538                                 else {
539                                         Material *ma = give_current_material(ob, ob->actcol);
540                                         if (ma) {
541                                                 snode->from = &ob->id;
542                                                 snode->id = &ma->id;
543                                         }
544                                 }
545                         }
546                 }
547                 else { /* SNODE_SHADER_WORLD */
548                         if (scene->world) {
549                                 snode->from = NULL;
550                                 snode->id = &scene->world->id;
551                         }
552                 }
553         }
554         else if (snode->treetype == NTREE_COMPOSIT) {
555                 snode->id = &scene->id;
556                 
557                 /* update output sockets based on available layers */
558                 ntreeCompositForceHidden(scene->nodetree, scene);
559         }
560         else if (snode->treetype == NTREE_TEXTURE) {
561                 Tex *tx = NULL;
562
563                 if (snode->texfrom == SNODE_TEX_OBJECT) {
564                         if (ob) {
565                                 tx = give_current_object_texture(ob);
566
567                                 if (ob->type == OB_LAMP)
568                                         snode->from = (ID *)ob->data;
569                                 else
570                                         snode->from = (ID *)give_current_material(ob, ob->actcol);
571
572                                 /* from is not set fully for material nodes, should be ID + Node then */
573                                 snode->id = &tx->id;
574                         }
575                 }
576                 else if (snode->texfrom == SNODE_TEX_WORLD) {
577                         tx = give_current_world_texture(scene->world);
578                         snode->from = (ID *)scene->world;
579                         snode->id = &tx->id;
580                 }
581                 else {
582                         struct Brush *brush = NULL;
583                         
584                         if (ob && (ob->mode & OB_MODE_SCULPT))
585                                 brush = paint_brush(&scene->toolsettings->sculpt->paint);
586                         else
587                                 brush = paint_brush(&scene->toolsettings->imapaint.paint);
588
589                         if (brush) {
590                                 snode->from = (ID *)brush;
591                                 tx = give_current_brush_texture(brush);
592                                 snode->id = &tx->id;
593                         }
594                 }
595         }
596         else {
597                 if (snode->nodetree && snode->nodetree->type == snode->treetype)
598                         snode->id = &snode->nodetree->id;
599                 else
600                         snode->id = NULL;
601         }
602
603         node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
604 }
605
606 void snode_update(SpaceNode *snode, bNode *node)
607 {
608         bNode *gnode;
609         
610         if (node)
611                 nodeUpdate(snode->edittree, node);
612         
613         /* if inside group, tag entire group */
614         gnode = node_tree_get_editgroup(snode->nodetree);
615         if (gnode)
616                 nodeUpdateID(snode->nodetree, gnode->id);
617 }
618
619 void ED_node_set_active(Main *bmain, bNodeTree *ntree, bNode *node)
620 {
621         int was_active_texture = (node->flag & NODE_ACTIVE_TEXTURE);
622
623         nodeSetActive(ntree, node);
624         
625         if (node->type != NODE_GROUP) {
626                 int was_output = (node->flag & NODE_DO_OUTPUT);
627                 
628                 /* tree specific activate calls */
629                 if (ntree->type == NTREE_SHADER) {
630                         /* when we select a material, active texture is cleared, for buttons */
631                         if (node->id && ELEM3(GS(node->id->name), ID_MA, ID_LA, ID_WO))
632                                 nodeClearActiveID(ntree, ID_TE);
633                         
634                         if (node->type == SH_NODE_OUTPUT) {
635                                 bNode *tnode;
636                                 
637                                 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
638                                         if (tnode->type == SH_NODE_OUTPUT)
639                                                 tnode->flag &= ~NODE_DO_OUTPUT;
640                                 
641                                 node->flag |= NODE_DO_OUTPUT;
642                                 if (was_output == 0)
643                                         ED_node_generic_update(bmain, ntree, node);
644                         }
645
646                         /* if active texture changed, free glsl materials */
647                         if ((node->flag & NODE_ACTIVE_TEXTURE) && !was_active_texture) {
648                                 Material *ma;
649
650                                 for (ma = bmain->mat.first; ma; ma = ma->id.next)
651                                         if (ma->nodetree && ma->use_nodes && has_nodetree(ma->nodetree, ntree))
652                                                 GPU_material_free(ma);
653
654                                 WM_main_add_notifier(NC_IMAGE, NULL);
655                         }
656
657                         WM_main_add_notifier(NC_MATERIAL | ND_NODES, node->id);
658                 }
659                 else if (ntree->type == NTREE_COMPOSIT) {
660                         /* make active viewer, currently only 1 supported... */
661                         if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
662                                 bNode *tnode;
663                                 
664
665                                 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
666                                         if (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
667                                                 tnode->flag &= ~NODE_DO_OUTPUT;
668                                 
669                                 node->flag |= NODE_DO_OUTPUT;
670                                 if (was_output == 0)
671                                         ED_node_generic_update(bmain, ntree, node);
672                                 
673                                 /* addnode() doesnt link this yet... */
674                                 node->id = (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
675                         }
676                         else if (node->type == CMP_NODE_R_LAYERS) {
677                                 Scene *scene;
678
679                                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
680                                         if (scene->nodetree && scene->use_nodes && has_nodetree(scene->nodetree, ntree)) {
681                                                 if (node->id == NULL || node->id == (ID *)scene) {
682                                                         scene->r.actlay = node->custom1;
683                                                 }
684                                         }
685                                 }
686                         }
687                         else if (node->type == CMP_NODE_COMPOSITE) {
688                                 if (was_output == 0) {
689                                         bNode *tnode;
690                                         
691                                         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
692                                                 if (tnode->type == CMP_NODE_COMPOSITE)
693                                                         tnode->flag &= ~NODE_DO_OUTPUT;
694                                         
695                                         node->flag |= NODE_DO_OUTPUT;
696                                         ED_node_generic_update(bmain, ntree, node);
697                                 }
698                         }
699                 }
700                 else if (ntree->type == NTREE_TEXTURE) {
701                         // XXX
702 #if 0
703                         if (node->id)
704                                 ;  // XXX BIF_preview_changed(-1);
705                         // allqueue(REDRAWBUTSSHADING, 1);
706                         // allqueue(REDRAWIPO, 0);
707 #endif
708                 }
709         }
710 }
711
712 void ED_node_post_apply_transform(bContext *UNUSED(C), bNodeTree *UNUSED(ntree))
713 {
714         /* XXX This does not work due to layout functions relying on node->block,
715          * which only exists during actual drawing. Can we rely on valid totr rects?
716          */
717         /* make sure nodes have correct bounding boxes after transform */
718         /* node_update_nodetree(C, ntree, 0.0f, 0.0f); */
719 }
720
721 /* ***************** generic operator functions for nodes ***************** */
722
723 #if 0 /* UNUSED */
724
725 static int edit_node_poll(bContext *C)
726 {
727         return ED_operator_node_active(C);
728 }
729
730 static void edit_node_properties(wmOperatorType *ot)
731 {
732         /* XXX could node be a context pointer? */
733         RNA_def_string(ot->srna, "node", "", MAX_NAME, "Node", "");
734         RNA_def_int(ot->srna, "socket", 0, 0, MAX_SOCKET, "Socket", "", 0, MAX_SOCKET);
735         RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Side", "");
736 }
737
738 static int edit_node_invoke_properties(bContext *C, wmOperator *op)
739 {
740         if (!RNA_struct_property_is_set(op->ptr, "node")) {
741                 bNode *node = CTX_data_pointer_get_type(C, "node", &RNA_Node).data;
742                 if (!node)
743                         return 0;
744                 else
745                         RNA_string_set(op->ptr, "node", node->name);
746         }
747         
748         if (!RNA_struct_property_is_set(op->ptr, "in_out"))
749                 RNA_enum_set(op->ptr, "in_out", SOCK_IN);
750         
751         if (!RNA_struct_property_is_set(op->ptr, "socket"))
752                 RNA_int_set(op->ptr, "socket", 0);
753         
754         return 1;
755 }
756
757 static void edit_node_properties_get(wmOperator *op, bNodeTree *ntree, bNode **rnode, bNodeSocket **rsock, int *rin_out)
758 {
759         bNode *node;
760         bNodeSocket *sock = NULL;
761         char nodename[MAX_NAME];
762         int sockindex;
763         int in_out;
764         
765         RNA_string_get(op->ptr, "node", nodename);
766         node = nodeFindNodebyName(ntree, nodename);
767         
768         in_out = RNA_enum_get(op->ptr, "in_out");
769         
770         sockindex = RNA_int_get(op->ptr, "socket");
771         switch (in_out) {
772                 case SOCK_IN:   sock = BLI_findlink(&node->inputs, sockindex);  break;
773                 case SOCK_OUT:  sock = BLI_findlink(&node->outputs, sockindex); break;
774         }
775         
776         if (rnode)
777                 *rnode = node;
778         if (rsock)
779                 *rsock = sock;
780         if (rin_out)
781                 *rin_out = in_out;
782 }
783 #endif
784
785 /* ************************** Node generic ************** */
786
787 /* is rct in visible part of node? */
788 static bNode *visible_node(SpaceNode *snode, const rctf *rct)
789 {
790         bNode *node;
791         
792         for (node = snode->edittree->nodes.last; node; node = node->prev) {
793                 if (BLI_rctf_isect(&node->totr, rct, NULL))
794                         break;
795         }
796         return node;
797 }
798
799 /* ********************** size widget operator ******************** */
800
801 typedef struct NodeSizeWidget {
802         float mxstart, mystart;
803         float oldlocx, oldlocy;
804         float oldoffsetx, oldoffsety;
805         float oldwidth, oldheight;
806         float oldminiwidth;
807         int directions;
808 } NodeSizeWidget;
809
810 static void node_resize_init(bContext *C, wmOperator *op, wmEvent *UNUSED(event), bNode *node, int dir)
811 {
812         SpaceNode *snode = CTX_wm_space_node(C);
813         
814         NodeSizeWidget *nsw = MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
815         
816         op->customdata = nsw;
817         nsw->mxstart = snode->cursor[0];
818         nsw->mystart = snode->cursor[1];
819         
820         /* store old */
821         nsw->oldlocx = node->locx;
822         nsw->oldlocy = node->locy;
823         nsw->oldoffsetx = node->offsetx;
824         nsw->oldoffsety = node->offsety;
825         nsw->oldwidth = node->width;
826         nsw->oldheight = node->height;
827         nsw->oldminiwidth = node->miniwidth;
828         nsw->directions = dir;
829         
830         WM_cursor_modal(CTX_wm_window(C), node_get_resize_cursor(dir));
831         /* add modal handler */
832         WM_event_add_modal_handler(C, op);
833 }
834
835 static void node_resize_exit(bContext *C, wmOperator *op, int UNUSED(cancel))
836 {
837         WM_cursor_restore(CTX_wm_window(C));
838         
839         MEM_freeN(op->customdata);
840         op->customdata = NULL;
841 }
842
843 static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
844 {
845         SpaceNode *snode = CTX_wm_space_node(C);
846         ARegion *ar = CTX_wm_region(C);
847         bNode *node = editnode_get_active(snode->edittree);
848         NodeSizeWidget *nsw = op->customdata;
849         float mx, my, dx, dy;
850         
851         switch (event->type) {
852                 case MOUSEMOVE:
853                         
854                         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &mx, &my);
855                         dx = mx - nsw->mxstart;
856                         dy = my - nsw->mystart;
857                         
858                         if (node) {
859                                 if (node->flag & NODE_HIDDEN) {
860                                         float widthmin = 0.0f;
861                                         float widthmax = 100.0f;
862                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
863                                                 node->miniwidth = nsw->oldminiwidth + dx;
864                                                 CLAMP(node->miniwidth, widthmin, widthmax);
865                                         }
866                                         if (nsw->directions & NODE_RESIZE_LEFT) {
867                                                 float locmax = nsw->oldlocx + nsw->oldminiwidth;
868                                                 
869                                                 node->locx = nsw->oldlocx + dx;
870                                                 CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
871                                                 node->miniwidth = locmax - node->locx;
872                                         }
873                                 }
874                                 else {
875                                         float widthmin = UI_DPI_FAC * node->typeinfo->minwidth;
876                                         float widthmax = UI_DPI_FAC * node->typeinfo->maxwidth;
877                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
878                                                 node->width = nsw->oldwidth + dx;
879                                                 CLAMP(node->width, widthmin, widthmax);
880                                         }
881                                         if (nsw->directions & NODE_RESIZE_LEFT) {
882                                                 float locmax = nsw->oldlocx + nsw->oldwidth;
883                                                 
884                                                 node->locx = nsw->oldlocx + dx;
885                                                 CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
886                                                 node->width = locmax - node->locx;
887                                         }
888                                 }
889                         
890                                 /* height works the other way round ... */
891                                 {
892                                         float heightmin = UI_DPI_FAC * node->typeinfo->minheight;
893                                         float heightmax = UI_DPI_FAC * node->typeinfo->maxheight;
894                                         if (nsw->directions & NODE_RESIZE_TOP) {
895                                                 float locmin = nsw->oldlocy - nsw->oldheight;
896                                                 
897                                                 node->locy = nsw->oldlocy + dy;
898                                                 CLAMP(node->locy, locmin + heightmin, locmin + heightmax);
899                                                 node->height = node->locy - locmin;
900                                         }
901                                         if (nsw->directions & NODE_RESIZE_BOTTOM) {
902                                                 node->height = nsw->oldheight - dy;
903                                                 CLAMP(node->height, heightmin, heightmax);
904                                         }
905                                 }
906                                 
907                                 /* XXX make callback? */
908                                 if (node->type == NODE_FRAME) {
909                                         /* keep the offset symmetric around center point */
910                                         if (nsw->directions & NODE_RESIZE_LEFT) {
911                                                 node->locx = nsw->oldlocx + 0.5f * dx;
912                                                 node->offsetx = nsw->oldoffsetx + 0.5f * dx;
913                                         }
914                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
915                                                 node->locx = nsw->oldlocx + 0.5f * dx;
916                                                 node->offsetx = nsw->oldoffsetx - 0.5f * dx;
917                                         }
918                                         if (nsw->directions & NODE_RESIZE_TOP) {
919                                                 node->locy = nsw->oldlocy + 0.5f * dy;
920                                                 node->offsety = nsw->oldoffsety + 0.5f * dy;
921                                         }
922                                         if (nsw->directions & NODE_RESIZE_BOTTOM) {
923                                                 node->locy = nsw->oldlocy + 0.5f * dy;
924                                                 node->offsety = nsw->oldoffsety - 0.5f * dy;
925                                         }
926                                 }
927                         }
928                                 
929                         ED_region_tag_redraw(ar);
930
931                         break;
932                         
933                 case LEFTMOUSE:
934                 case MIDDLEMOUSE:
935                 case RIGHTMOUSE:
936                         
937                         node_resize_exit(C, op, 0);
938                         ED_node_post_apply_transform(C, snode->edittree);
939                         
940                         return OPERATOR_FINISHED;
941         }
942         
943         return OPERATOR_RUNNING_MODAL;
944 }
945
946 static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
947 {
948         SpaceNode *snode = CTX_wm_space_node(C);
949         ARegion *ar = CTX_wm_region(C);
950         bNode *node = editnode_get_active(snode->edittree);
951         int dir;
952         
953         if (node) {
954                 /* convert mouse coordinates to v2d space */
955                 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
956                                          &snode->cursor[0], &snode->cursor[1]);
957                 dir = node->typeinfo->resize_area_func(node, snode->cursor[0], snode->cursor[1]);
958                 if (dir != 0) {
959                         node_resize_init(C, op, event, node, dir);
960                         return OPERATOR_RUNNING_MODAL;
961                 }
962         }
963         return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
964 }
965
966 static int node_resize_cancel(bContext *C, wmOperator *op)
967 {
968         node_resize_exit(C, op, 1);
969
970         return OPERATOR_CANCELLED;
971 }
972
973 void NODE_OT_resize(wmOperatorType *ot)
974 {
975         /* identifiers */
976         ot->name = "Resize Node";
977         ot->idname = "NODE_OT_resize";
978         ot->description = "Resize a node";
979         
980         /* api callbacks */
981         ot->invoke = node_resize_invoke;
982         ot->modal = node_resize_modal;
983         ot->poll = ED_operator_node_active;
984         ot->cancel = node_resize_cancel;
985         
986         /* flags */
987         ot->flag = OPTYPE_BLOCKING;
988 }
989
990
991 /* ********************** hidden sockets ******************** */
992
993 int node_has_hidden_sockets(bNode *node)
994 {
995         bNodeSocket *sock;
996         
997         for (sock = node->inputs.first; sock; sock = sock->next)
998                 if (sock->flag & SOCK_HIDDEN)
999                         return 1;
1000         for (sock = node->outputs.first; sock; sock = sock->next)
1001                 if (sock->flag & SOCK_HIDDEN)
1002                         return 1;
1003         return 0;
1004 }
1005
1006 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
1007 {
1008         bNodeSocket *sock;
1009
1010         if (set == 0) {
1011                 for (sock = node->inputs.first; sock; sock = sock->next)
1012                         sock->flag &= ~SOCK_HIDDEN;
1013                 for (sock = node->outputs.first; sock; sock = sock->next)
1014                         sock->flag &= ~SOCK_HIDDEN;
1015         }
1016         else {
1017                 /* hide unused sockets */
1018                 for (sock = node->inputs.first; sock; sock = sock->next) {
1019                         if (sock->link == NULL)
1020                                 sock->flag |= SOCK_HIDDEN;
1021                 }
1022                 for (sock = node->outputs.first; sock; sock = sock->next) {
1023                         if (nodeCountSocketLinks(snode->edittree, sock) == 0)
1024                                 sock->flag |= SOCK_HIDDEN;
1025                 }
1026         }
1027 }
1028
1029 /* return 0, nothing done */
1030 static int UNUSED_FUNCTION(node_mouse_groupheader) (SpaceNode * snode)
1031 {
1032         bNode *gnode;
1033         float mx = 0, my = 0;
1034 // XXX  int mval[2];
1035         
1036         gnode = node_tree_get_editgroup(snode->nodetree);
1037         if (gnode == NULL) return 0;
1038         
1039 // XXX  getmouseco_areawin(mval);
1040 // XXX  areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1041         
1042         /* click in header or outside? */
1043         if (BLI_rctf_isect_pt(&gnode->totr, mx, my) == 0) {
1044                 rctf rect = gnode->totr;
1045                 
1046                 rect.ymax += NODE_DY;
1047                 if (BLI_rctf_isect_pt(&rect, mx, my) == 0)
1048                         snode_make_group_editable(snode, NULL);  /* toggles, so exits editmode */
1049 //              else
1050 // XXX                  transform_nodes(snode->nodetree, 'g', "Move group");
1051                 
1052                 return 1;
1053         }
1054         return 0;
1055 }
1056
1057 /* checks snode->mouse position, and returns found node/socket */
1058 /* type is SOCK_IN and/or SOCK_OUT */
1059 int node_find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
1060 {
1061         bNode *node;
1062         bNodeSocket *sock;
1063         rctf rect;
1064         
1065         *nodep = NULL;
1066         *sockp = NULL;
1067         
1068         /* check if we click in a socket */
1069         for (node = snode->edittree->nodes.first; node; node = node->next) {
1070                 
1071                 rect.xmin = snode->cursor[0] - (NODE_SOCKSIZE + 4);
1072                 rect.ymin = snode->cursor[1] - (NODE_SOCKSIZE + 4);
1073                 rect.xmax = snode->cursor[0] + (NODE_SOCKSIZE + 4);
1074                 rect.ymax = snode->cursor[1] + (NODE_SOCKSIZE + 4);
1075                 
1076                 if (!(node->flag & NODE_HIDDEN)) {
1077                         /* extra padding inside and out - allow dragging on the text areas too */
1078                         if (in_out == SOCK_IN) {
1079                                 rect.xmax += NODE_SOCKSIZE;
1080                                 rect.xmin -= NODE_SOCKSIZE * 4;
1081                         }
1082                         else if (in_out == SOCK_OUT) {
1083                                 rect.xmax += NODE_SOCKSIZE * 4;
1084                                 rect.xmin -= NODE_SOCKSIZE;
1085                         }
1086                 }
1087                 
1088                 if (in_out & SOCK_IN) {
1089                         for (sock = node->inputs.first; sock; sock = sock->next) {
1090                                 if (!nodeSocketIsHidden(sock)) {
1091                                         if (BLI_rctf_isect_pt(&rect, sock->locx, sock->locy)) {
1092                                                 if (node == visible_node(snode, &rect)) {
1093                                                         *nodep = node;
1094                                                         *sockp = sock;
1095                                                         return 1;
1096                                                 }
1097                                         }
1098                                 }
1099                         }
1100                 }
1101                 if (in_out & SOCK_OUT) {
1102                         for (sock = node->outputs.first; sock; sock = sock->next) {
1103                                 if (!nodeSocketIsHidden(sock)) {
1104                                         if (BLI_rctf_isect_pt(&rect, sock->locx, sock->locy)) {
1105                                                 if (node == visible_node(snode, &rect)) {
1106                                                         *nodep = node;
1107                                                         *sockp = sock;
1108                                                         return 1;
1109                                                 }
1110                                         }
1111                                 }
1112                         }
1113                 }
1114         }
1115         
1116         /* check group sockets
1117          * NB: using ngroup->outputs as input sockets and vice versa here!
1118          */
1119         if (in_out & SOCK_IN) {
1120                 for (sock = snode->edittree->outputs.first; sock; sock = sock->next) {
1121                         if (!nodeSocketIsHidden(sock)) {
1122                                 if (BLI_rctf_isect_pt(&rect, sock->locx, sock->locy)) {
1123                                         *nodep = NULL;   /* NULL node pointer indicates group socket */
1124                                         *sockp = sock;
1125                                         return 1;
1126                                 }
1127                         }
1128                 }
1129         }
1130         if (in_out & SOCK_OUT) {
1131                 for (sock = snode->edittree->inputs.first; sock; sock = sock->next) {
1132                         if (!nodeSocketIsHidden(sock)) {
1133                                 if (BLI_rctf_isect_pt(&rect, sock->locx, sock->locy)) {
1134                                         *nodep = NULL;   /* NULL node pointer indicates group socket */
1135                                         *sockp = sock;
1136                                         return 1;
1137                                 }
1138                         }
1139                 }
1140         }
1141         
1142         return 0;
1143 }
1144
1145 /* ****************** Duplicate *********************** */
1146
1147 static void node_duplicate_reparent_recursive(bNode *node)
1148 {
1149         bNode *parent;
1150         
1151         node->flag |= NODE_TEST;
1152         
1153         /* find first selected parent */
1154         for (parent = node->parent; parent; parent = parent->parent) {
1155                 if (parent->flag & SELECT) {
1156                         if (!(parent->flag & NODE_TEST))
1157                                 node_duplicate_reparent_recursive(parent);
1158                         break;
1159                 }
1160         }
1161         /* reparent node copy to parent copy */
1162         if (parent) {
1163                 nodeDetachNode(node->new_node);
1164                 nodeAttachNode(node->new_node, parent->new_node);
1165         }
1166 }
1167
1168 static int node_duplicate_exec(bContext *C, wmOperator *op)
1169 {
1170         SpaceNode *snode = CTX_wm_space_node(C);
1171         bNodeTree *ntree = snode->edittree;
1172         bNode *node, *newnode, *lastnode;
1173         bNodeLink *link, *newlink, *lastlink;
1174         int keep_inputs = RNA_boolean_get(op->ptr, "keep_inputs");
1175         
1176         ED_preview_kill_jobs(C);
1177         
1178         lastnode = ntree->nodes.last;
1179         for (node = ntree->nodes.first; node; node = node->next) {
1180                 if (node->flag & SELECT) {
1181                         newnode = nodeCopyNode(ntree, node);
1182                         
1183                         if (newnode->id) {
1184                                 /* simple id user adjustment, node internal functions don't touch this
1185                                  * but operators and readfile.c do. */
1186                                 id_us_plus(newnode->id);
1187                                 /* to ensure redraws or rerenders happen */
1188                                 ED_node_changed_update(snode->id, newnode);
1189                         }
1190                 }
1191                 
1192                 /* make sure we don't copy new nodes again! */
1193                 if (node == lastnode)
1194                         break;
1195         }
1196         
1197         /* copy links between selected nodes
1198          * NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
1199          */
1200         lastlink = ntree->links.last;
1201         for (link = ntree->links.first; link; link = link->next) {
1202                 /* This creates new links between copied nodes.
1203                  * If keep_inputs is set, also copies input links from unselected (when fromnode==NULL)!
1204                  */
1205                 if (link->tonode && (link->tonode->flag & NODE_SELECT) &&
1206                     (keep_inputs || (link->fromnode && (link->fromnode->flag & NODE_SELECT))))
1207                 {
1208                         newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
1209                         newlink->flag = link->flag;
1210                         newlink->tonode = link->tonode->new_node;
1211                         newlink->tosock = link->tosock->new_sock;
1212                         if (link->fromnode && (link->fromnode->flag & NODE_SELECT)) {
1213                                 newlink->fromnode = link->fromnode->new_node;
1214                                 newlink->fromsock = link->fromsock->new_sock;
1215                         }
1216                         else {
1217                                 /* input node not copied, this keeps the original input linked */
1218                                 newlink->fromnode = link->fromnode;
1219                                 newlink->fromsock = link->fromsock;
1220                         }
1221                         
1222                         BLI_addtail(&ntree->links, newlink);
1223                 }
1224                 
1225                 /* make sure we don't copy new links again! */
1226                 if (link == lastlink)
1227                         break;
1228         }
1229         
1230         /* clear flags for recursive depth-first iteration */
1231         for (node = ntree->nodes.first; node; node = node->next)
1232                 node->flag &= ~NODE_TEST;
1233         /* reparent copied nodes */
1234         for (node = ntree->nodes.first; node; node = node->next) {
1235                 if ((node->flag & SELECT) && !(node->flag & NODE_TEST))
1236                         node_duplicate_reparent_recursive(node);
1237                 
1238                 /* only has to check old nodes */
1239                 if (node == lastnode)
1240                         break;
1241         }
1242         
1243         /* deselect old nodes, select the copies instead */
1244         for (node = ntree->nodes.first; node; node = node->next) {
1245                 if (node->flag & SELECT) {
1246                         /* has been set during copy above */
1247                         newnode = node->new_node;
1248                         
1249                         node_deselect(node);
1250                         node->flag &= ~NODE_ACTIVE;
1251                         node_select(newnode);
1252                 }
1253                 
1254                 /* make sure we don't copy new nodes again! */
1255                 if (node == lastnode)
1256                         break;
1257         }
1258         
1259         ntreeUpdateTree(snode->edittree);
1260         
1261         snode_notify(C, snode);
1262         snode_dag_update(C, snode);
1263
1264         return OPERATOR_FINISHED;
1265 }
1266
1267 void NODE_OT_duplicate(wmOperatorType *ot)
1268 {
1269         /* identifiers */
1270         ot->name = "Duplicate Nodes";
1271         ot->description = "Duplicate selected nodes";
1272         ot->idname = "NODE_OT_duplicate";
1273         
1274         /* api callbacks */
1275         ot->exec = node_duplicate_exec;
1276         ot->poll = ED_operator_node_active;
1277         
1278         /* flags */
1279         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1280         
1281         RNA_def_boolean(ot->srna, "keep_inputs", 0, "Keep Inputs", "Keep the input links to duplicated nodes");
1282 }
1283
1284 int ED_node_select_check(ListBase *lb)
1285 {
1286         bNode *node;
1287
1288         for (node = lb->first; node; node = node->next) {
1289                 if (node->flag & NODE_SELECT) {
1290                         return TRUE;
1291                 }
1292         }
1293
1294         return FALSE;
1295 }
1296
1297 /* ******************************** */
1298 // XXX some code needing updating to operators...
1299
1300
1301 /* goes over all scenes, reads render layers */
1302 static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
1303 {
1304         Main *bmain = CTX_data_main(C);
1305         SpaceNode *snode = CTX_wm_space_node(C);
1306         Scene *curscene = CTX_data_scene(C), *scene;
1307         bNode *node;
1308
1309         ED_preview_kill_jobs(C);
1310
1311         /* first tag scenes unread */
1312         for (scene = bmain->scene.first; scene; scene = scene->id.next)
1313                 scene->id.flag |= LIB_DOIT;
1314
1315         for (node = snode->edittree->nodes.first; node; node = node->next) {
1316                 if (node->type == CMP_NODE_R_LAYERS) {
1317                         ID *id = node->id;
1318                         if (id->flag & LIB_DOIT) {
1319                                 RE_ReadRenderResult(curscene, (Scene *)id);
1320                                 ntreeCompositTagRender((Scene *)id);
1321                                 id->flag &= ~LIB_DOIT;
1322                         }
1323                 }
1324         }
1325         
1326         snode_notify(C, snode);
1327         snode_dag_update(C, snode);
1328
1329         return OPERATOR_FINISHED;
1330 }
1331
1332 void NODE_OT_read_renderlayers(wmOperatorType *ot)
1333 {
1334         
1335         ot->name = "Read Render Layers";
1336         ot->idname = "NODE_OT_read_renderlayers";
1337         ot->description = "Read all render layers of all used scenes";
1338         
1339         ot->exec = node_read_renderlayers_exec;
1340         
1341         ot->poll = composite_node_active;
1342         
1343         /* flags */
1344         ot->flag = 0;
1345 }
1346
1347 static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
1348 {
1349         Main *bmain = CTX_data_main(C);
1350         SpaceNode *snode = CTX_wm_space_node(C);
1351         Scene *curscene = CTX_data_scene(C);
1352         Render *re = RE_NewRender(curscene->id.name);
1353
1354         WM_cursor_wait(1);
1355         RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
1356         WM_cursor_wait(0);
1357
1358         /* note we are careful to send the right notifier, as otherwise the
1359          * compositor would reexecute and overwrite the full sample result */
1360         WM_event_add_notifier(C, NC_SCENE | ND_COMPO_RESULT, NULL);
1361
1362         return OPERATOR_FINISHED;
1363 }
1364
1365
1366 void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
1367 {
1368         
1369         ot->name = "Read Full Sample Layers";
1370         ot->idname = "NODE_OT_read_fullsamplelayers";
1371         ot->description = "Read all render layers of current scene, in full sample";
1372         
1373         ot->exec = node_read_fullsamplelayers_exec;
1374         
1375         ot->poll = composite_node_active;
1376         
1377         /* flags */
1378         ot->flag = 0;
1379 }
1380
1381 int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
1382 {
1383         Scene *sce = CTX_data_scene(C);
1384         bNode *node;
1385         
1386         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1387                 if (node->id == (ID *)sce && node->need_exec) {
1388                         break;
1389                 }
1390         }
1391         if (node) {
1392                 SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
1393                 
1394                 if (srl) {
1395                         PointerRNA op_ptr;
1396                         
1397                         WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
1398                         RNA_string_set(&op_ptr, "layer", srl->name);
1399                         RNA_string_set(&op_ptr, "scene", sce->id.name + 2);
1400                         
1401                         /* to keep keypositions */
1402                         sce->r.scemode |= R_NO_FRAME_UPDATE;
1403                         
1404                         WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
1405
1406                         WM_operator_properties_free(&op_ptr);
1407                         
1408                         return OPERATOR_FINISHED;
1409                 }
1410                    
1411         }
1412         return OPERATOR_CANCELLED;
1413 }
1414
1415 void NODE_OT_render_changed(wmOperatorType *ot)
1416 {
1417         ot->name = "Render Changed Layer";
1418         ot->idname = "NODE_OT_render_changed";
1419         ot->description = "Render current scene, when input node's layer has been changed";
1420         
1421         ot->exec = node_render_changed_exec;
1422         
1423         ot->poll = composite_node_active;
1424         
1425         /* flags */
1426         ot->flag = 0;
1427 }
1428
1429 /* ****************** Hide operator *********************** */
1430
1431 static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
1432 {
1433         bNode *node;
1434         int tot_eq = 0, tot_neq = 0;
1435
1436         /* Toggles the flag on all selected nodes.
1437          * If the flag is set on all nodes it is unset.
1438          * If the flag is not set on all nodes, it is set.
1439          */
1440         for (node = snode->edittree->nodes.first; node; node = node->next) {
1441                 if (node->flag & SELECT) {
1442                         
1443                         if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
1444                                 continue;
1445                         if (toggle_flag == NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS) == 0)
1446                                 continue;
1447                         
1448                         if (node->flag & toggle_flag)
1449                                 tot_eq++;
1450                         else
1451                                 tot_neq++;
1452                 }
1453         }
1454         for (node = snode->edittree->nodes.first; node; node = node->next) {
1455                 if (node->flag & SELECT) {
1456                         
1457                         if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
1458                                 continue;
1459                         if (toggle_flag == NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS) == 0)
1460                                 continue;
1461                         
1462                         if ( (tot_eq && tot_neq) || tot_eq == 0)
1463                                 node->flag |= toggle_flag;
1464                         else
1465                                 node->flag &= ~toggle_flag;
1466                 }
1467         }
1468 }
1469
1470 static int node_hide_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1471 {
1472         SpaceNode *snode = CTX_wm_space_node(C);
1473         
1474         /* sanity checking (poll callback checks this already) */
1475         if ((snode == NULL) || (snode->edittree == NULL))
1476                 return OPERATOR_CANCELLED;
1477         
1478         node_flag_toggle_exec(snode, NODE_HIDDEN);
1479
1480         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1481
1482         return OPERATOR_FINISHED;
1483 }
1484
1485 void NODE_OT_hide_toggle(wmOperatorType *ot)
1486 {
1487         /* identifiers */
1488         ot->name = "Hide";
1489         ot->description = "Toggle hiding of selected nodes";
1490         ot->idname = "NODE_OT_hide_toggle";
1491         
1492         /* callbacks */
1493         ot->exec = node_hide_toggle_exec;
1494         ot->poll = ED_operator_node_active;
1495
1496         /* flags */
1497         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1498 }
1499
1500 static int node_preview_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1501 {
1502         SpaceNode *snode = CTX_wm_space_node(C);
1503
1504         /* sanity checking (poll callback checks this already) */
1505         if ((snode == NULL) || (snode->edittree == NULL))
1506                 return OPERATOR_CANCELLED;
1507
1508         ED_preview_kill_jobs(C);
1509
1510         node_flag_toggle_exec(snode, NODE_PREVIEW);
1511
1512         snode_notify(C, snode);
1513
1514         return OPERATOR_FINISHED;
1515 }
1516
1517 void NODE_OT_preview_toggle(wmOperatorType *ot)
1518 {
1519         /* identifiers */
1520         ot->name = "Toggle Node Preview";
1521         ot->description = "Toggle preview display for selected nodes";
1522         ot->idname = "NODE_OT_preview_toggle";
1523
1524         /* callbacks */
1525         ot->exec = node_preview_toggle_exec;
1526         ot->poll = ED_operator_node_active;
1527
1528         /* flags */
1529         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1530 }
1531
1532 static int node_options_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1533 {
1534         SpaceNode *snode = CTX_wm_space_node(C);
1535
1536         /* sanity checking (poll callback checks this already) */
1537         if ((snode == NULL) || (snode->edittree == NULL))
1538                 return OPERATOR_CANCELLED;
1539
1540         node_flag_toggle_exec(snode, NODE_OPTIONS);
1541
1542         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1543
1544         return OPERATOR_FINISHED;
1545 }
1546
1547 void NODE_OT_options_toggle(wmOperatorType *ot)
1548 {
1549         /* identifiers */
1550         ot->name = "Toggle Node Options";
1551         ot->description = "Toggle option buttons display for selected nodes";
1552         ot->idname = "NODE_OT_options_toggle";
1553
1554         /* callbacks */
1555         ot->exec = node_options_toggle_exec;
1556         ot->poll = ED_operator_node_active;
1557
1558         /* flags */
1559         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1560 }
1561
1562 static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1563 {
1564         SpaceNode *snode = CTX_wm_space_node(C);
1565         bNode *node;
1566         int hidden;
1567
1568         /* sanity checking (poll callback checks this already) */
1569         if ((snode == NULL) || (snode->edittree == NULL))
1570                 return OPERATOR_CANCELLED;
1571
1572         ED_preview_kill_jobs(C);
1573
1574         /* Toggle for all selected nodes */
1575         hidden = 0;
1576         for (node = snode->edittree->nodes.first; node; node = node->next) {
1577                 if (node->flag & SELECT) {
1578                         if (node_has_hidden_sockets(node)) {
1579                                 hidden = 1;
1580                                 break;
1581                         }
1582                 }
1583         }
1584         
1585         for (node = snode->edittree->nodes.first; node; node = node->next) {
1586                 if (node->flag & SELECT) {
1587                         node_set_hidden_sockets(snode, node, !hidden);
1588                 }
1589         }
1590
1591         ntreeUpdateTree(snode->edittree);
1592
1593         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1594
1595         return OPERATOR_FINISHED;
1596 }
1597
1598 void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
1599 {
1600         /* identifiers */
1601         ot->name = "Toggle Hidden Node Sockets";
1602         ot->description = "Toggle unused node socket display";
1603         ot->idname = "NODE_OT_hide_socket_toggle";
1604
1605         /* callbacks */
1606         ot->exec = node_socket_toggle_exec;
1607         ot->poll = ED_operator_node_active;
1608
1609         /* flags */
1610         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1611 }
1612
1613 /* ****************** Mute operator *********************** */
1614
1615 static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
1616 {
1617         SpaceNode *snode = CTX_wm_space_node(C);
1618         bNode *node;
1619
1620         ED_preview_kill_jobs(C);
1621
1622         for (node = snode->edittree->nodes.first; node; node = node->next) {
1623                 /* Only allow muting of nodes having a mute func! */
1624                 if ((node->flag & SELECT) && node->typeinfo->internal_connect) {
1625                         node->flag ^= NODE_MUTED;
1626                         snode_update(snode, node);
1627                 }
1628         }
1629         
1630         snode_notify(C, snode);
1631         snode_dag_update(C, snode);
1632         
1633         return OPERATOR_FINISHED;
1634 }
1635
1636 void NODE_OT_mute_toggle(wmOperatorType *ot)
1637 {
1638         /* identifiers */
1639         ot->name = "Toggle Node Mute";
1640         ot->description = "Toggle muting of the nodes";
1641         ot->idname = "NODE_OT_mute_toggle";
1642         
1643         /* callbacks */
1644         ot->exec = node_mute_exec;
1645         ot->poll = ED_operator_node_active;
1646         
1647         /* flags */
1648         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1649 }
1650
1651 /* ****************** Delete operator ******************* */
1652
1653 static int node_delete_exec(bContext *C, wmOperator *UNUSED(op))
1654 {
1655         SpaceNode *snode = CTX_wm_space_node(C);
1656         bNode *node, *next;
1657         
1658         ED_preview_kill_jobs(C);
1659
1660         for (node = snode->edittree->nodes.first; node; node = next) {
1661                 next = node->next;
1662                 if (node->flag & SELECT) {
1663                         /* check id user here, nodeFreeNode is called for free dbase too */
1664                         if (node->id)
1665                                 node->id->us--;
1666                         nodeFreeNode(snode->edittree, node);
1667                 }
1668         }
1669         
1670         ntreeUpdateTree(snode->edittree);
1671
1672         snode_notify(C, snode);
1673         snode_dag_update(C, snode);
1674         
1675         return OPERATOR_FINISHED;
1676 }
1677
1678 void NODE_OT_delete(wmOperatorType *ot)
1679 {
1680         /* identifiers */
1681         ot->name = "Delete";
1682         ot->description = "Delete selected nodes";
1683         ot->idname = "NODE_OT_delete";
1684         
1685         /* api callbacks */
1686         ot->exec = node_delete_exec;
1687         ot->poll = ED_operator_node_active;
1688         
1689         /* flags */
1690         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1691 }
1692
1693 /* ****************** Delete with reconnect ******************* */
1694 static int node_delete_reconnect_exec(bContext *C, wmOperator *UNUSED(op))
1695 {
1696         SpaceNode *snode = CTX_wm_space_node(C);
1697         bNode *node, *next;
1698
1699         ED_preview_kill_jobs(C);
1700
1701         for (node = snode->edittree->nodes.first; node; node = next) {
1702                 next = node->next;
1703                 if (node->flag & SELECT) {
1704                         nodeInternalRelink(snode->edittree, node);
1705                         
1706                         /* check id user here, nodeFreeNode is called for free dbase too */
1707                         if (node->id)
1708                                 node->id->us--;
1709                         nodeFreeNode(snode->edittree, node);
1710                 }
1711         }
1712
1713         ntreeUpdateTree(snode->edittree);
1714
1715         snode_notify(C, snode);
1716         snode_dag_update(C, snode);
1717
1718         return OPERATOR_FINISHED;
1719 }
1720
1721 void NODE_OT_delete_reconnect(wmOperatorType *ot)
1722 {
1723         /* identifiers */
1724         ot->name = "Delete with reconnect";
1725         ot->description = "Delete nodes; will reconnect nodes as if deletion was muted";
1726         ot->idname = "NODE_OT_delete_reconnect";
1727
1728         /* api callbacks */
1729         ot->exec = node_delete_reconnect_exec;
1730         ot->poll = ED_operator_node_active;
1731
1732         /* flags */
1733         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1734 }
1735
1736
1737 /* ****************** File Output Add Socket  ******************* */
1738
1739 static int node_output_file_add_socket_exec(bContext *C, wmOperator *op)
1740 {
1741         Scene *scene = CTX_data_scene(C);
1742         SpaceNode *snode = CTX_wm_space_node(C);
1743         PointerRNA ptr;
1744         bNodeTree *ntree;
1745         bNode *node;
1746         char file_path[MAX_NAME];
1747
1748         ptr = CTX_data_pointer_get(C, "node");
1749         if (!ptr.data)
1750                 return OPERATOR_CANCELLED;
1751         node = ptr.data;
1752         ntree = ptr.id.data;
1753
1754         RNA_string_get(op->ptr, "file_path", file_path);
1755         ntreeCompositOutputFileAddSocket(ntree, node, file_path, &scene->r.im_format);
1756
1757         snode_notify(C, snode);
1758
1759         return OPERATOR_FINISHED;
1760 }
1761
1762 void NODE_OT_output_file_add_socket(wmOperatorType *ot)
1763 {
1764         /* identifiers */
1765         ot->name = "Add File Node Socket";
1766         ot->description = "Add a new input to a file output node";
1767         ot->idname = "NODE_OT_output_file_add_socket";
1768
1769         /* callbacks */
1770         ot->exec = node_output_file_add_socket_exec;
1771         ot->poll = composite_node_active;
1772
1773         /* flags */
1774         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1775
1776         RNA_def_string(ot->srna, "file_path", "Image", MAX_NAME, "File Path", "Sub-path of the output file");
1777 }
1778
1779 /* ****************** Multi File Output Remove Socket  ******************* */
1780
1781 static int node_output_file_remove_active_socket_exec(bContext *C, wmOperator *UNUSED(op))
1782 {
1783         SpaceNode *snode = CTX_wm_space_node(C);
1784         PointerRNA ptr = CTX_data_pointer_get(C, "node");
1785         bNodeTree *ntree;
1786         bNode *node;
1787         
1788         if (!ptr.data)
1789                 return OPERATOR_CANCELLED;
1790         node = ptr.data;
1791         ntree = ptr.id.data;
1792         
1793         if (!ntreeCompositOutputFileRemoveActiveSocket(ntree, node))
1794                 return OPERATOR_CANCELLED;
1795         
1796         snode_notify(C, snode);
1797         
1798         return OPERATOR_FINISHED;
1799 }
1800
1801 void NODE_OT_output_file_remove_active_socket(wmOperatorType *ot)
1802 {
1803         /* identifiers */
1804         ot->name = "Remove File Node Socket";
1805         ot->description = "Remove active input from a file output node";
1806         ot->idname = "NODE_OT_output_file_remove_active_socket";
1807         
1808         /* callbacks */
1809         ot->exec = node_output_file_remove_active_socket_exec;
1810         ot->poll = composite_node_active;
1811         
1812         /* flags */
1813         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1814 }
1815
1816 /* ****************** Multi File Output Move Socket  ******************* */
1817
1818 static int node_output_file_move_active_socket_exec(bContext *C, wmOperator *op)
1819 {
1820         SpaceNode *snode = CTX_wm_space_node(C);
1821         PointerRNA ptr = CTX_data_pointer_get(C, "node");
1822         bNode *node;
1823         NodeImageMultiFile *nimf;
1824         bNodeSocket *sock;
1825         int direction;
1826         
1827         if (!ptr.data)
1828                 return OPERATOR_CANCELLED;
1829         node = ptr.data;
1830         nimf = node->storage;
1831         
1832         sock = BLI_findlink(&node->inputs, nimf->active_input);
1833         if (!sock)
1834                 return OPERATOR_CANCELLED;
1835         
1836         direction = RNA_enum_get(op->ptr, "direction");
1837         
1838         if (direction == 1) {
1839                 bNodeSocket *before = sock->prev;
1840                 if (!before)
1841                         return OPERATOR_CANCELLED;
1842                 BLI_remlink(&node->inputs, sock);
1843                 BLI_insertlinkbefore(&node->inputs, before, sock);
1844                 nimf->active_input--;
1845         }
1846         else {
1847                 bNodeSocket *after = sock->next;
1848                 if (!after)
1849                         return OPERATOR_CANCELLED;
1850                 BLI_remlink(&node->inputs, sock);
1851                 BLI_insertlinkafter(&node->inputs, after, sock);
1852                 nimf->active_input++;
1853         }
1854         
1855         snode_notify(C, snode);
1856         
1857         return OPERATOR_FINISHED;
1858 }
1859
1860 void NODE_OT_output_file_move_active_socket(wmOperatorType *ot)
1861 {
1862         static EnumPropertyItem direction_items[] = {
1863                 {1, "UP", 0, "Up", ""},
1864                 {2, "DOWN", 0, "Down", ""},
1865                 { 0, NULL, 0, NULL, NULL }
1866         };
1867         
1868         /* identifiers */
1869         ot->name = "Move File Node Socket";
1870         ot->description = "Move the active input of a file output node up or down the list";
1871         ot->idname = "NODE_OT_output_file_move_active_socket";
1872         
1873         /* callbacks */
1874         ot->exec = node_output_file_move_active_socket_exec;
1875         ot->poll = composite_node_active;
1876         
1877         /* flags */
1878         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1879         
1880         RNA_def_enum(ot->srna, "direction", direction_items, 2, "Direction", "");
1881 }
1882
1883 /* ****************** Copy Node Color ******************* */
1884
1885 static int node_copy_color_exec(bContext *C, wmOperator *UNUSED(op))
1886 {
1887         SpaceNode *snode = CTX_wm_space_node(C);
1888         bNodeTree *ntree = snode->edittree;
1889         bNode *node, *tnode;
1890         
1891         if (!ntree)
1892                 return OPERATOR_CANCELLED;
1893         node = nodeGetActive(ntree);
1894         if (!node)
1895                 return OPERATOR_CANCELLED;
1896         
1897         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
1898                 if (tnode->flag & NODE_SELECT && tnode != node) {
1899                         if (node->flag & NODE_CUSTOM_COLOR) {
1900                                 tnode->flag |= NODE_CUSTOM_COLOR;
1901                                 copy_v3_v3(tnode->color, node->color);
1902                         }
1903                         else
1904                                 tnode->flag &= ~NODE_CUSTOM_COLOR;
1905                 }
1906         }
1907
1908         ED_node_sort(ntree);
1909         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1910
1911         return OPERATOR_FINISHED;
1912 }
1913
1914 void NODE_OT_node_copy_color(wmOperatorType *ot)
1915 {
1916         /* identifiers */
1917         ot->name = "Copy Color";
1918         ot->description = "Copy color to all selected nodes";
1919         ot->idname = "NODE_OT_node_copy_color";
1920
1921         /* api callbacks */
1922         ot->exec = node_copy_color_exec;
1923         ot->poll = ED_operator_node_active;
1924
1925         /* flags */
1926         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1927 }
1928
1929 /* ****************** Copy to clipboard ******************* */
1930
1931 static int node_clipboard_copy_exec(bContext *C, wmOperator *UNUSED(op))
1932 {
1933         SpaceNode *snode = CTX_wm_space_node(C);
1934         bNodeTree *ntree = snode->edittree;
1935         bNode *gnode = node_tree_get_editgroup(snode->nodetree);
1936         float gnode_x = 0.0f, gnode_y = 0.0f;
1937         bNode *node, *new_node;
1938         bNodeLink *link, *newlink;
1939
1940         ED_preview_kill_jobs(C);
1941
1942         /* clear current clipboard */
1943         BKE_node_clipboard_clear();
1944         BKE_node_clipboard_init(ntree);
1945
1946         /* get group node offset */
1947         if (gnode)
1948                 nodeToView(gnode, 0.0f, 0.0f, &gnode_x, &gnode_y);
1949         
1950         for (node = ntree->nodes.first; node; node = node->next) {
1951                 if (node->flag & SELECT) {
1952                         new_node = nodeCopyNode(NULL, node);
1953                         BKE_node_clipboard_add_node(new_node);
1954                 }
1955         }
1956
1957         for (node = ntree->nodes.first; node; node = node->next) {
1958                 if (node->flag & SELECT) {
1959                         bNode *new_node = node->new_node;
1960                         
1961                         /* ensure valid pointers */
1962                         if (new_node->parent) {
1963                                 /* parent pointer must be redirected to new node or detached if parent is not copied */
1964                                 if (new_node->parent->flag & NODE_SELECT) {
1965                                         new_node->parent = new_node->parent->new_node;
1966                                 }
1967                                 else {
1968                                         nodeDetachNode(new_node);
1969                                 }
1970                         }
1971
1972                         /* transform to basic view space. child node location is relative to parent */
1973                         if (!new_node->parent) {        
1974                                 new_node->locx += gnode_x;
1975                                 new_node->locy += gnode_y;
1976                         }
1977                 }
1978         }
1979
1980         /* copy links between selected nodes
1981          * NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
1982          */
1983         for (link = ntree->links.first; link; link = link->next) {
1984                 /* This creates new links between copied nodes. */
1985                 if (link->tonode && (link->tonode->flag & NODE_SELECT) &&
1986                     link->fromnode && (link->fromnode->flag & NODE_SELECT))
1987                 {
1988                         newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
1989                         newlink->flag = link->flag;
1990                         newlink->tonode = link->tonode->new_node;
1991                         newlink->tosock = link->tosock->new_sock;
1992                         newlink->fromnode = link->fromnode->new_node;
1993                         newlink->fromsock = link->fromsock->new_sock;
1994
1995                         BKE_node_clipboard_add_link(newlink);
1996                 }
1997         }
1998
1999         return OPERATOR_FINISHED;
2000 }
2001
2002 void NODE_OT_clipboard_copy(wmOperatorType *ot)
2003 {
2004         /* identifiers */
2005         ot->name = "Copy to clipboard";
2006         ot->description = "Copies selected nodes to the clipboard";
2007         ot->idname = "NODE_OT_clipboard_copy";
2008
2009         /* api callbacks */
2010         ot->exec = node_clipboard_copy_exec;
2011         ot->poll = ED_operator_node_active;
2012
2013         /* flags */
2014         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2015 }
2016
2017 /* ****************** Paste from clipboard ******************* */
2018
2019 static int node_clipboard_paste_exec(bContext *C, wmOperator *op)
2020 {
2021         SpaceNode *snode = CTX_wm_space_node(C);
2022         bNodeTree *ntree = snode->edittree;
2023         bNode *gnode = node_tree_get_editgroup(snode->nodetree);
2024         float gnode_center[2];
2025         const ListBase *clipboard_nodes_lb;
2026         const ListBase *clipboard_links_lb;
2027         bNode *node;
2028         bNodeLink *link;
2029         int num_nodes;
2030         float center[2];
2031         int is_clipboard_valid;
2032
2033         /* validate pointers in the clipboard */
2034         is_clipboard_valid = BKE_node_clipboard_validate();
2035         clipboard_nodes_lb = BKE_node_clipboard_get_nodes();
2036         clipboard_links_lb = BKE_node_clipboard_get_links();
2037
2038         if (clipboard_nodes_lb->first == NULL) {
2039                 BKE_report(op->reports, RPT_ERROR, "Clipboard is empty");
2040                 return OPERATOR_CANCELLED;
2041         }
2042
2043         if (BKE_node_clipboard_get_type() != ntree->type) {
2044                 BKE_report(op->reports, RPT_ERROR, "Clipboard nodes are an incompatible type");
2045                 return OPERATOR_CANCELLED;
2046         }
2047
2048         /* only warn */
2049         if (is_clipboard_valid == FALSE) {
2050                 BKE_report(op->reports, RPT_WARNING, "Some nodes references could not be restored, will be left empty");
2051         }
2052
2053         ED_preview_kill_jobs(C);
2054
2055         /* deselect old nodes */
2056         node_deselect_all(snode);
2057
2058         /* get group node offset */
2059         if (gnode) {
2060                 nodeToView(gnode, 0.0f, 0.0f, &gnode_center[0], &gnode_center[1]);
2061         }
2062         else {
2063                 zero_v2(gnode_center);
2064         }
2065
2066         /* calculate "barycenter" for placing on mouse cursor */
2067         zero_v2(center);
2068         for (node = clipboard_nodes_lb->first, num_nodes = 0; node; node = node->next, num_nodes++) {
2069                 center[0] += BLI_RCT_CENTER_X(&node->totr);
2070                 center[1] += BLI_RCT_CENTER_Y(&node->totr);
2071         }
2072         mul_v2_fl(center, 1.0 / num_nodes);
2073
2074         /* copy nodes from clipboard */
2075         for (node = clipboard_nodes_lb->first; node; node = node->next) {
2076                 bNode *new_node = nodeCopyNode(ntree, node);
2077
2078                 /* needed since nodeCopyNode() doesn't increase ID's */
2079                 id_us_plus(node->id);
2080
2081                 /* pasted nodes are selected */
2082                 node_select(new_node);
2083         }
2084         
2085         /* reparent copied nodes */
2086         for (node = clipboard_nodes_lb->first; node; node = node->next) {
2087                 bNode *new_node = node->new_node;
2088                 if (new_node->parent)
2089                         new_node->parent = new_node->parent->new_node;
2090                 
2091                 
2092                 /* place nodes around the mouse cursor. child nodes locations are relative to parent */
2093                 if (!new_node->parent) {
2094                         new_node->locx += snode->cursor[0] - center[0] - gnode_center[0];
2095                         new_node->locy += snode->cursor[1] - center[1] - gnode_center[1];
2096                 }
2097         }
2098
2099         for (link = clipboard_links_lb->first; link; link = link->next) {
2100                 nodeAddLink(ntree, link->fromnode->new_node, link->fromsock->new_sock,
2101                             link->tonode->new_node, link->tosock->new_sock);
2102         }
2103
2104         ntreeUpdateTree(snode->edittree);
2105
2106         snode_notify(C, snode);
2107         snode_dag_update(C, snode);
2108
2109         return OPERATOR_FINISHED;
2110 }
2111
2112 static int node_clipboard_paste_invoke(bContext *C, wmOperator *op, wmEvent *event)
2113 {
2114         ARegion *ar = CTX_wm_region(C);
2115         SpaceNode *snode = CTX_wm_space_node(C);
2116
2117         /* convert mouse coordinates to v2d space */
2118         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &snode->cursor[0], &snode->cursor[1]);
2119
2120         return node_clipboard_paste_exec(C, op);
2121 }
2122
2123 void NODE_OT_clipboard_paste(wmOperatorType *ot)
2124 {
2125         /* identifiers */
2126         ot->name = "Paste from clipboard";
2127         ot->description = "Pastes nodes from the clipboard to the active node tree";
2128         ot->idname = "NODE_OT_clipboard_paste";
2129
2130         /* api callbacks */
2131         ot->exec = node_clipboard_paste_exec;
2132         ot->invoke = node_clipboard_paste_invoke;
2133         ot->poll = ED_operator_node_active;
2134
2135         /* flags */
2136         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2137 }