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