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