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