Cleanup: remove moar ugly G.main usages...
[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_text_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_report.h"
51 #include "BKE_scene.h"
52
53 #include "RE_engine.h"
54 #include "RE_pipeline.h"
55
56
57 #include "ED_node.h"  /* own include */
58 #include "ED_screen.h"
59 #include "ED_render.h"
60
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63 #include "RNA_enum_types.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 "IMB_imbuf_types.h"
73
74 #include "node_intern.h"  /* own include */
75 #include "NOD_composite.h"
76 #include "NOD_shader.h"
77 #include "NOD_texture.h"
78
79
80 #define USE_ESC_COMPO
81
82 /* ***************** composite job manager ********************** */
83
84 enum {
85         COM_RECALC_COMPOSITE = 1,
86         COM_RECALC_VIEWER    = 2
87 };
88
89 typedef struct CompoJob {
90         Main *bmain;
91         Scene *scene;
92         bNodeTree *ntree;
93         bNodeTree *localtree;
94         const short *stop;
95         short *do_update;
96         float *progress;
97         int recalc_flags;
98 } CompoJob;
99
100 static void compo_tag_output_nodes(bNodeTree *nodetree, int recalc_flags)
101 {
102         bNode *node;
103
104         for (node = nodetree->nodes.first; node; node = node->next) {
105                 if (node->type == CMP_NODE_COMPOSITE) {
106                         if (recalc_flags & COM_RECALC_COMPOSITE)
107                                 node->flag |= NODE_DO_OUTPUT_RECALC;
108                 }
109                 else if (node->type == CMP_NODE_VIEWER || node->type == CMP_NODE_SPLITVIEWER) {
110                         if (recalc_flags & COM_RECALC_VIEWER)
111                                 node->flag |= NODE_DO_OUTPUT_RECALC;
112                 }
113                 else if (node->type == NODE_GROUP) {
114                         if (node->id)
115                                 compo_tag_output_nodes((bNodeTree *)node->id, recalc_flags);
116                 }
117         }
118 }
119
120 static int compo_get_recalc_flags(const bContext *C)
121 {
122         wmWindowManager *wm = CTX_wm_manager(C);
123         wmWindow *win;
124         int recalc_flags = 0;
125
126         for (win = wm->windows.first; win; win = win->next) {
127                 bScreen *sc = win->screen;
128                 ScrArea *sa;
129
130                 for (sa = sc->areabase.first; sa; sa = sa->next) {
131                         if (sa->spacetype == SPACE_IMAGE) {
132                                 SpaceImage *sima = sa->spacedata.first;
133                                 if (sima->image) {
134                                         if (sima->image->type == IMA_TYPE_R_RESULT)
135                                                 recalc_flags |= COM_RECALC_COMPOSITE;
136                                         else if (sima->image->type == IMA_TYPE_COMPOSITE)
137                                                 recalc_flags |= COM_RECALC_VIEWER;
138                                 }
139                         }
140                         else if (sa->spacetype == SPACE_NODE) {
141                                 SpaceNode *snode = sa->spacedata.first;
142                                 if (snode->flag & SNODE_BACKDRAW)
143                                         recalc_flags |= COM_RECALC_VIEWER;
144                         }
145                 }
146         }
147
148         return recalc_flags;
149 }
150
151 /* called by compo, only to check job 'stop' value */
152 static int compo_breakjob(void *cjv)
153 {
154         CompoJob *cj = cjv;
155
156         /* without G.is_break 'ESC' wont quit - which annoys users */
157         return (*(cj->stop)
158 #ifdef USE_ESC_COMPO
159                 ||
160                 G.is_break
161 #endif
162                 );
163 }
164
165 /* called by compo, wmJob sends notifier */
166 static void compo_statsdrawjob(void *cjv, const char *UNUSED(str))
167 {
168         CompoJob *cj = cjv;
169
170         *(cj->do_update) = true;
171 }
172
173 /* called by compo, wmJob sends notifier */
174 static void compo_redrawjob(void *cjv)
175 {
176         CompoJob *cj = cjv;
177
178         *(cj->do_update) = true;
179 }
180
181 static void compo_freejob(void *cjv)
182 {
183         CompoJob *cj = cjv;
184
185         if (cj->localtree) {
186                 ntreeLocalMerge(cj->bmain, cj->localtree, cj->ntree);
187         }
188         MEM_freeN(cj);
189 }
190
191 /* only now we copy the nodetree, so adding many jobs while
192  * sliding buttons doesn't frustrate */
193 static void compo_initjob(void *cjv)
194 {
195         CompoJob *cj = cjv;
196
197         cj->localtree = ntreeLocalize(cj->ntree);
198
199         if (cj->recalc_flags)
200                 compo_tag_output_nodes(cj->localtree, cj->recalc_flags);
201 }
202
203 /* called before redraw notifiers, it moves finished previews over */
204 static void compo_updatejob(void *UNUSED(cjv))
205 {
206         WM_main_add_notifier(NC_SCENE | ND_COMPO_RESULT, NULL);
207 }
208
209 static void compo_progressjob(void *cjv, float progress)
210 {
211         CompoJob *cj = cjv;
212
213         *(cj->progress) = progress;
214 }
215
216 /* only this runs inside thread */
217 static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
218 {
219         CompoJob *cj = cjv;
220         bNodeTree *ntree = cj->localtree;
221         Scene *scene = cj->scene;
222         SceneRenderView *srv;
223
224         if (scene->use_nodes == false)
225                 return;
226
227         cj->stop = stop;
228         cj->do_update = do_update;
229         cj->progress = progress;
230
231         ntree->test_break = compo_breakjob;
232         ntree->tbh = cj;
233         ntree->stats_draw = compo_statsdrawjob;
234         ntree->sdh = cj;
235         ntree->progress = compo_progressjob;
236         ntree->prh = cj;
237         ntree->update_draw = compo_redrawjob;
238         ntree->udh = cj;
239
240         // XXX BIF_store_spare();
241         /* 1 is do_previews */
242
243         if ((cj->scene->r.scemode & R_MULTIVIEW) == 0) {
244                 ntreeCompositExecTree(cj->scene, ntree, &cj->scene->r, false, true, &scene->view_settings, &scene->display_settings, "");
245         }
246         else {
247                 for (srv = scene->r.views.first; srv; srv = srv->next) {
248                         if (BKE_scene_multiview_is_render_view_active(&scene->r, srv) == false) continue;
249                         ntreeCompositExecTree(cj->scene, ntree, &cj->scene->r, false, true, &scene->view_settings, &scene->display_settings, srv->name);
250                 }
251         }
252
253         ntree->test_break = NULL;
254         ntree->stats_draw = NULL;
255         ntree->progress = NULL;
256
257 }
258
259 /**
260  * \param scene_owner is the owner of the job,
261  * we don't use it for anything else currently so could also be a void pointer,
262  * but for now keep it an 'Scene' for consistency.
263  *
264  * \note only call from spaces `refresh` callbacks, not direct! - use with care.
265  */
266 void ED_node_composite_job(const bContext *C, struct bNodeTree *nodetree, Scene *scene_owner)
267 {
268         wmJob *wm_job;
269         CompoJob *cj;
270         Main *bmain = CTX_data_main(C);
271         Scene *scene = CTX_data_scene(C);
272
273         /* to fix bug: [#32272] */
274         if (G.is_rendering) {
275                 return;
276         }
277
278 #ifdef USE_ESC_COMPO
279         G.is_break = false;
280 #endif
281
282         BKE_image_backup_render(scene, BKE_image_verify_viewer(bmain, IMA_TYPE_R_RESULT, "Render Result"), false);
283
284         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene_owner, "Compositing",
285                              WM_JOB_EXCL_RENDER | WM_JOB_PROGRESS, WM_JOB_TYPE_COMPOSITE);
286         cj = MEM_callocN(sizeof(CompoJob), "compo job");
287
288         /* customdata for preview thread */
289         cj->bmain = bmain;
290         cj->scene = scene;
291         cj->ntree = nodetree;
292         cj->recalc_flags = compo_get_recalc_flags(C);
293
294         /* setup job */
295         WM_jobs_customdata_set(wm_job, cj, compo_freejob);
296         WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_COMPO_RESULT, NC_SCENE | ND_COMPO_RESULT);
297         WM_jobs_callbacks(wm_job, compo_startjob, compo_initjob, compo_updatejob, NULL);
298
299         WM_jobs_start(CTX_wm_manager(C), wm_job);
300 }
301
302 /* ***************************************** */
303
304 /* operator poll callback */
305 int composite_node_active(bContext *C)
306 {
307         if (ED_operator_node_active(C)) {
308                 SpaceNode *snode = CTX_wm_space_node(C);
309                 if (ED_node_is_compositor(snode))
310                         return 1;
311         }
312         return 0;
313 }
314
315 /* operator poll callback */
316 int composite_node_editable(bContext *C)
317 {
318         if (ED_operator_node_editable(C)) {
319                 SpaceNode *snode = CTX_wm_space_node(C);
320                 if (ED_node_is_compositor(snode))
321                         return 1;
322         }
323         return 0;
324 }
325
326 void snode_dag_update(bContext *C, SpaceNode *snode)
327 {
328         Main *bmain = CTX_data_main(C);
329
330         /* for groups, update all ID's using this */
331         if (snode->edittree != snode->nodetree) {
332                 FOREACH_NODETREE(bmain, tntree, id) {
333                         if (ntreeHasTree(tntree, snode->edittree))
334                                 DAG_id_tag_update(id, 0);
335                 } FOREACH_NODETREE_END
336         }
337
338         DAG_id_tag_update(snode->id, 0);
339 }
340
341 void snode_notify(bContext *C, SpaceNode *snode)
342 {
343         ID *id = snode->id;
344
345         WM_event_add_notifier(C, NC_NODE | NA_EDITED, NULL);
346
347         if (ED_node_is_shader(snode)) {
348                 if (GS(id->name) == ID_MA)
349                         WM_main_add_notifier(NC_MATERIAL | ND_SHADING, id);
350                 else if (GS(id->name) == ID_LA)
351                         WM_main_add_notifier(NC_LAMP | ND_LIGHTING, id);
352                 else if (GS(id->name) == ID_WO)
353                         WM_main_add_notifier(NC_WORLD | ND_WORLD, id);
354         }
355         else if (ED_node_is_compositor(snode))
356                 WM_event_add_notifier(C, NC_SCENE | ND_NODES, id);
357         else if (ED_node_is_texture(snode))
358                 WM_event_add_notifier(C, NC_TEXTURE | ND_NODES, id);
359 }
360
361 void ED_node_set_tree_type(SpaceNode *snode, bNodeTreeType *typeinfo)
362 {
363         if (typeinfo)
364                 BLI_strncpy(snode->tree_idname, typeinfo->idname, sizeof(snode->tree_idname));
365         else
366                 snode->tree_idname[0] = '\0';
367 }
368
369 bool ED_node_is_compositor(struct SpaceNode *snode)
370 {
371         return STREQ(snode->tree_idname, ntreeType_Composite->idname);
372 }
373
374 bool ED_node_is_shader(struct SpaceNode *snode)
375 {
376         return STREQ(snode->tree_idname, ntreeType_Shader->idname);
377 }
378
379 bool ED_node_is_texture(struct SpaceNode *snode)
380 {
381         return STREQ(snode->tree_idname, ntreeType_Texture->idname);
382 }
383
384 /* assumes nothing being done in ntree yet, sets the default in/out node */
385 /* called from shading buttons or header */
386 void ED_node_shader_default(const bContext *C, ID *id)
387 {
388         Scene *scene = CTX_data_scene(C);
389         bNode *in, *out;
390         bNodeSocket *fromsock, *tosock, *sock;
391         bNodeTree *ntree;
392         int output_type, shader_type;
393         float color[4] = { 0.0f, 0.0f, 0.0f, 1.0f }, strength = 1.0f;
394
395         ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname);
396
397         switch (GS(id->name)) {
398                 case ID_MA:
399                 {
400                         Material *ma = (Material *)id;
401                         ma->nodetree = ntree;
402
403                         if (BKE_scene_use_new_shading_nodes(scene)) {
404                                 output_type = SH_NODE_OUTPUT_MATERIAL;
405                                 shader_type = SH_NODE_BSDF_DIFFUSE;
406                         }
407                         else {
408                                 output_type = SH_NODE_OUTPUT;
409                                 shader_type = SH_NODE_MATERIAL;
410                         }
411
412                         copy_v3_v3(color, &ma->r);
413                         strength = 0.0f;
414                         break;
415                 }
416                 case ID_WO:
417                 {
418                         World *wo = (World *)id;
419                         wo->nodetree = ntree;
420
421                         output_type = SH_NODE_OUTPUT_WORLD;
422                         shader_type = SH_NODE_BACKGROUND;
423
424                         copy_v3_v3(color, &wo->horr);
425                         strength = 1.0f;
426                         break;
427                 }
428                 case ID_LA:
429                 {
430                         Lamp *la = (Lamp *)id;
431                         la->nodetree = ntree;
432
433                         output_type = SH_NODE_OUTPUT_LAMP;
434                         shader_type = SH_NODE_EMISSION;
435
436                         copy_v3_v3(color, &la->r);
437                         if (la->type == LA_LOCAL || la->type == LA_SPOT || la->type == LA_AREA)
438                                 strength = 100.0f;
439                         else
440                                 strength = 1.0f;
441                         break;
442                 }
443                 default:
444                         printf("ED_node_shader_default called on wrong ID type.\n");
445                         return;
446         }
447
448         out = nodeAddStaticNode(C, ntree, output_type);
449         out->locx = 300.0f; out->locy = 300.0f;
450
451         in = nodeAddStaticNode(C, ntree, shader_type);
452         in->locx = 10.0f; in->locy = 300.0f;
453         nodeSetActive(ntree, in);
454
455         /* only a link from color to color */
456         fromsock = in->outputs.first;
457         tosock = out->inputs.first;
458         nodeAddLink(ntree, in, fromsock, out, tosock);
459
460         /* default values */
461         if (BKE_scene_use_new_shading_nodes(scene)) {
462                 PointerRNA sockptr;
463                 sock = in->inputs.first;
464                 RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &sockptr);
465
466                 RNA_float_set_array(&sockptr, "default_value", color);
467
468                 if (strength != 0.0f) {
469                         sock = in->inputs.last;
470                         RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &sockptr);
471                         RNA_float_set(&sockptr, "default_value", strength);
472                 }
473         }
474
475         ntreeUpdateTree(CTX_data_main(C), ntree);
476 }
477
478 /* assumes nothing being done in ntree yet, sets the default in/out node */
479 /* called from shading buttons or header */
480 void ED_node_composit_default(const bContext *C, struct Scene *sce)
481 {
482         bNode *in, *out;
483         bNodeSocket *fromsock, *tosock;
484
485         /* but lets check it anyway */
486         if (sce->nodetree) {
487                 if (G.debug & G_DEBUG)
488                         printf("error in composite initialize\n");
489                 return;
490         }
491
492         sce->nodetree = ntreeAddTree(NULL, "Compositing Nodetree", ntreeType_Composite->idname);
493
494         sce->nodetree->chunksize = 256;
495         sce->nodetree->edit_quality = NTREE_QUALITY_HIGH;
496         sce->nodetree->render_quality = NTREE_QUALITY_HIGH;
497
498         out = nodeAddStaticNode(C, sce->nodetree, CMP_NODE_COMPOSITE);
499         out->locx = 300.0f; out->locy = 400.0f;
500
501         in = nodeAddStaticNode(C, sce->nodetree, CMP_NODE_R_LAYERS);
502         in->locx = 10.0f; in->locy = 400.0f;
503         nodeSetActive(sce->nodetree, in);
504
505         /* links from color to color */
506         fromsock = in->outputs.first;
507         tosock = out->inputs.first;
508         nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
509
510         ntreeUpdateTree(CTX_data_main(C), sce->nodetree);
511 }
512
513 /* assumes nothing being done in ntree yet, sets the default in/out node */
514 /* called from shading buttons or header */
515 void ED_node_texture_default(const bContext *C, Tex *tx)
516 {
517         bNode *in, *out;
518         bNodeSocket *fromsock, *tosock;
519
520         /* but lets check it anyway */
521         if (tx->nodetree) {
522                 if (G.debug & G_DEBUG)
523                         printf("error in texture initialize\n");
524                 return;
525         }
526
527         tx->nodetree = ntreeAddTree(NULL, "Texture Nodetree", ntreeType_Texture->idname);
528
529         out = nodeAddStaticNode(C, tx->nodetree, TEX_NODE_OUTPUT);
530         out->locx = 300.0f; out->locy = 300.0f;
531
532         in = nodeAddStaticNode(C, tx->nodetree, TEX_NODE_CHECKER);
533         in->locx = 10.0f; in->locy = 300.0f;
534         nodeSetActive(tx->nodetree, in);
535
536         fromsock = in->outputs.first;
537         tosock = out->inputs.first;
538         nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
539
540         ntreeUpdateTree(CTX_data_main(C), tx->nodetree);
541 }
542
543 /* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
544 void snode_set_context(const bContext *C)
545 {
546         SpaceNode *snode = CTX_wm_space_node(C);
547         bNodeTreeType *treetype = ntreeTypeFind(snode->tree_idname);
548         bNodeTree *ntree = snode->nodetree;
549         ID *id = snode->id, *from = snode->from;
550
551         /* we use this to signal warnings, when node shaders are drawn in wrong render engine */
552         if (BKE_scene_use_new_shading_nodes(CTX_data_scene(C)))
553                 snode->flag |= SNODE_NEW_SHADERS;
554         else
555                 snode->flag &= ~SNODE_NEW_SHADERS;
556
557         /* check the tree type */
558         if (!treetype ||
559             (treetype->poll && !treetype->poll(C, treetype)))
560         {
561                 /* invalid tree type, skip
562                  * NB: not resetting the node path here, invalid bNodeTreeType
563                  * may still be registered at a later point.
564                  */
565                 return;
566         }
567
568         if (snode->nodetree && !STREQ(snode->nodetree->idname, snode->tree_idname)) {
569                 /* current tree does not match selected type, clear tree path */
570                 ntree = NULL;
571                 id = NULL;
572                 from = NULL;
573         }
574
575         if (!(snode->flag & SNODE_PIN) || ntree == NULL) {
576                 if (treetype->get_from_context) {
577                         /* reset and update from context */
578                         ntree = NULL;
579                         id = NULL;
580                         from = NULL;
581
582                         treetype->get_from_context(C, treetype, &ntree, &id, &from);
583                 }
584         }
585
586         if (snode->nodetree != ntree || snode->id != id || snode->from != from ||
587             (snode->treepath.last == NULL && ntree))
588         {
589                 ED_node_tree_start(snode, ntree, id, from);
590         }
591 }
592
593 void snode_update(SpaceNode *snode, bNode *node)
594 {
595         bNodeTreePath *path;
596
597         /* XXX this only updates nodes in the current node space tree path.
598          * The function supposedly should update any potential group node linking to changed tree,
599          * this really requires a working depsgraph ...
600          */
601
602         /* update all edited group nodes */
603         path = snode->treepath.last;
604         if (path) {
605                 bNodeTree *ngroup = path->nodetree;
606                 for (path = path->prev; path; path = path->prev) {
607                         nodeUpdateID(path->nodetree, (ID *)ngroup);
608                         ngroup = path->nodetree;
609                 }
610         }
611
612         if (node)
613                 nodeUpdate(snode->edittree, node);
614 }
615
616 void ED_node_set_active(Main *bmain, bNodeTree *ntree, bNode *node)
617 {
618         const bool was_active_texture = (node->flag & NODE_ACTIVE_TEXTURE) != 0;
619
620         nodeSetActive(ntree, node);
621
622         if (node->type != NODE_GROUP) {
623                 const bool was_output = (node->flag & NODE_DO_OUTPUT) != 0;
624                 bool do_update = false;
625
626                 /* generic node group output: set node as active output */
627                 if (node->type == NODE_GROUP_OUTPUT) {
628                         bNode *tnode;
629                         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
630                                 if (tnode->type == NODE_GROUP_OUTPUT)
631                                         tnode->flag &= ~NODE_DO_OUTPUT;
632
633                         node->flag |= NODE_DO_OUTPUT;
634                         if (!was_output)
635                                 do_update = 1;
636                 }
637
638                 /* tree specific activate calls */
639                 if (ntree->type == NTREE_SHADER) {
640                         /* when we select a material, active texture is cleared, for buttons */
641                         if (node->id && ELEM(GS(node->id->name), ID_MA, ID_LA, ID_WO))
642                                 nodeClearActiveID(ntree, ID_TE);
643
644                         if (ELEM(node->type, SH_NODE_OUTPUT, SH_NODE_OUTPUT_MATERIAL,
645                                  SH_NODE_OUTPUT_WORLD, SH_NODE_OUTPUT_LAMP, SH_NODE_OUTPUT_LINESTYLE))
646                         {
647                                 bNode *tnode;
648
649                                 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
650                                         if (tnode->type == node->type)
651                                                 tnode->flag &= ~NODE_DO_OUTPUT;
652
653                                 node->flag |= NODE_DO_OUTPUT;
654                                 if (was_output == 0)
655                                         ED_node_tag_update_nodetree(bmain, ntree, node);
656                         }
657                         else if (do_update)
658                                 ED_node_tag_update_nodetree(bmain, ntree, node);
659
660                         /* if active texture changed, free glsl materials */
661                         if ((node->flag & NODE_ACTIVE_TEXTURE) && !was_active_texture) {
662                                 Material *ma;
663                                 World *wo;
664
665                                 for (ma = bmain->mat.first; ma; ma = ma->id.next)
666                                         if (ma->nodetree && ma->use_nodes && ntreeHasTree(ma->nodetree, ntree))
667                                                 GPU_material_free(&ma->gpumaterial);
668
669                                 for (wo = bmain->world.first; wo; wo = wo->id.next)
670                                         if (wo->nodetree && wo->use_nodes && ntreeHasTree(wo->nodetree, ntree))
671                                                 GPU_material_free(&wo->gpumaterial);
672
673                                 WM_main_add_notifier(NC_IMAGE, NULL);
674                         }
675
676                         WM_main_add_notifier(NC_MATERIAL | ND_NODES, node->id);
677                 }
678                 else if (ntree->type == NTREE_COMPOSIT) {
679                         /* make active viewer, currently only 1 supported... */
680                         if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
681                                 bNode *tnode;
682
683
684                                 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
685                                         if (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
686                                                 tnode->flag &= ~NODE_DO_OUTPUT;
687
688                                 node->flag |= NODE_DO_OUTPUT;
689                                 if (was_output == 0)
690                                         ED_node_tag_update_nodetree(bmain, ntree, node);
691
692                                 /* addnode() doesnt link this yet... */
693                                 node->id = (ID *)BKE_image_verify_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
694                         }
695                         else if (node->type == CMP_NODE_R_LAYERS) {
696                                 Scene *scene;
697
698                                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
699                                         if (scene->nodetree && scene->use_nodes && ntreeHasTree(scene->nodetree, ntree)) {
700                                                 if (node->id == NULL || node->id == (ID *)scene) {
701                                                         int num_layers = BLI_listbase_count(&scene->r.layers);
702                                                         scene->r.actlay = node->custom1;
703                                                         /* Clamp the value, because it might have come from a different
704                                                          * scene which could have more render layers than new one.
705                                                          */
706                                                         scene->r.actlay = min_ff(scene->r.actlay, num_layers - 1);
707                                                 }
708                                         }
709                                 }
710                         }
711                         else if (node->type == CMP_NODE_COMPOSITE) {
712                                 if (was_output == 0) {
713                                         bNode *tnode;
714
715                                         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
716                                                 if (tnode->type == CMP_NODE_COMPOSITE)
717                                                         tnode->flag &= ~NODE_DO_OUTPUT;
718
719                                         node->flag |= NODE_DO_OUTPUT;
720                                         ED_node_tag_update_nodetree(bmain, ntree, node);
721                                 }
722                         }
723                         else if (do_update)
724                                 ED_node_tag_update_nodetree(bmain, ntree, node);
725                 }
726                 else if (ntree->type == NTREE_TEXTURE) {
727                         // XXX
728 #if 0
729                         if (node->id)
730                                 ;  // XXX BIF_preview_changed(-1);
731                         // allqueue(REDRAWBUTSSHADING, 1);
732                         // allqueue(REDRAWIPO, 0);
733 #endif
734                 }
735         }
736 }
737
738 void ED_node_post_apply_transform(bContext *UNUSED(C), bNodeTree *UNUSED(ntree))
739 {
740         /* XXX This does not work due to layout functions relying on node->block,
741          * which only exists during actual drawing. Can we rely on valid totr rects?
742          */
743         /* make sure nodes have correct bounding boxes after transform */
744         /* node_update_nodetree(C, ntree, 0.0f, 0.0f); */
745 }
746
747 /* ***************** generic operator functions for nodes ***************** */
748
749 #if 0 /* UNUSED */
750
751 static int edit_node_poll(bContext *C)
752 {
753         return ED_operator_node_active(C);
754 }
755
756 static void edit_node_properties(wmOperatorType *ot)
757 {
758         /* XXX could node be a context pointer? */
759         RNA_def_string(ot->srna, "node", NULL, MAX_NAME, "Node", "");
760         RNA_def_int(ot->srna, "socket", 0, 0, MAX_SOCKET, "Socket", "", 0, MAX_SOCKET);
761         RNA_def_enum(ot->srna, "in_out", rna_enum_node_socket_in_out_items, SOCK_IN, "Socket Side", "");
762 }
763
764 static int edit_node_invoke_properties(bContext *C, wmOperator *op)
765 {
766         if (!RNA_struct_property_is_set(op->ptr, "node")) {
767                 bNode *node = CTX_data_pointer_get_type(C, "node", &RNA_Node).data;
768                 if (!node)
769                         return 0;
770                 else
771                         RNA_string_set(op->ptr, "node", node->name);
772         }
773
774         if (!RNA_struct_property_is_set(op->ptr, "in_out"))
775                 RNA_enum_set(op->ptr, "in_out", SOCK_IN);
776
777         if (!RNA_struct_property_is_set(op->ptr, "socket"))
778                 RNA_int_set(op->ptr, "socket", 0);
779
780         return 1;
781 }
782
783 static void edit_node_properties_get(wmOperator *op, bNodeTree *ntree, bNode **rnode, bNodeSocket **rsock, int *rin_out)
784 {
785         bNode *node;
786         bNodeSocket *sock = NULL;
787         char nodename[MAX_NAME];
788         int sockindex;
789         int in_out;
790
791         RNA_string_get(op->ptr, "node", nodename);
792         node = nodeFindNodebyName(ntree, nodename);
793
794         in_out = RNA_enum_get(op->ptr, "in_out");
795
796         sockindex = RNA_int_get(op->ptr, "socket");
797         switch (in_out) {
798                 case SOCK_IN:   sock = BLI_findlink(&node->inputs, sockindex);  break;
799                 case SOCK_OUT:  sock = BLI_findlink(&node->outputs, sockindex); break;
800         }
801
802         if (rnode)
803                 *rnode = node;
804         if (rsock)
805                 *rsock = sock;
806         if (rin_out)
807                 *rin_out = in_out;
808 }
809 #endif
810
811 /* ************************** Node generic ************** */
812
813 /* is rct in visible part of node? */
814 static bNode *visible_node(SpaceNode *snode, const rctf *rct)
815 {
816         bNode *node;
817
818         for (node = snode->edittree->nodes.last; node; node = node->prev) {
819                 if (BLI_rctf_isect(&node->totr, rct, NULL))
820                         break;
821         }
822         return node;
823 }
824
825 /* ********************** size widget operator ******************** */
826
827 typedef struct NodeSizeWidget {
828         float mxstart, mystart;
829         float oldlocx, oldlocy;
830         float oldoffsetx, oldoffsety;
831         float oldwidth, oldheight;
832         float oldminiwidth;
833         int directions;
834 } NodeSizeWidget;
835
836 static void node_resize_init(bContext *C, wmOperator *op, const wmEvent *UNUSED(event), bNode *node, int dir)
837 {
838         SpaceNode *snode = CTX_wm_space_node(C);
839
840         NodeSizeWidget *nsw = MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
841
842         op->customdata = nsw;
843         nsw->mxstart = snode->cursor[0];
844         nsw->mystart = snode->cursor[1];
845
846         /* store old */
847         nsw->oldlocx = node->locx;
848         nsw->oldlocy = node->locy;
849         nsw->oldoffsetx = node->offsetx;
850         nsw->oldoffsety = node->offsety;
851         nsw->oldwidth = node->width;
852         nsw->oldheight = node->height;
853         nsw->oldminiwidth = node->miniwidth;
854         nsw->directions = dir;
855
856         WM_cursor_modal_set(CTX_wm_window(C), node_get_resize_cursor(dir));
857         /* add modal handler */
858         WM_event_add_modal_handler(C, op);
859 }
860
861 static void node_resize_exit(bContext *C, wmOperator *op, bool UNUSED(cancel))
862 {
863         WM_cursor_modal_restore(CTX_wm_window(C));
864
865         MEM_freeN(op->customdata);
866         op->customdata = NULL;
867 }
868
869 static int node_resize_modal(bContext *C, wmOperator *op, const wmEvent *event)
870 {
871         SpaceNode *snode = CTX_wm_space_node(C);
872         ARegion *ar = CTX_wm_region(C);
873         bNode *node = nodeGetActive(snode->edittree);
874         NodeSizeWidget *nsw = op->customdata;
875         float mx, my, dx, dy;
876
877         switch (event->type) {
878                 case MOUSEMOVE:
879
880                         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &mx, &my);
881                         dx = (mx - nsw->mxstart) / UI_DPI_FAC;
882                         dy = (my - nsw->mystart) / UI_DPI_FAC;
883
884                         if (node) {
885                                 /* width can use node->width or node->miniwidth (hidden nodes) */
886                                 float *pwidth;
887                                 float oldwidth, widthmin, widthmax;
888                                 /* ignore hidden flag for frame nodes */
889                                 bool use_hidden = (node->type != NODE_FRAME);
890                                 if (use_hidden && node->flag & NODE_HIDDEN) {
891                                         pwidth = &node->miniwidth;
892                                         oldwidth = nsw->oldminiwidth;
893                                         widthmin = 0.0f;
894                                 }
895                                 else {
896                                         pwidth = &node->width;
897                                         oldwidth = nsw->oldwidth;
898                                         widthmin = node->typeinfo->minwidth;
899                                 }
900                                 widthmax = node->typeinfo->maxwidth;
901
902                                 {
903                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
904                                                 *pwidth = oldwidth + dx;
905                                                 CLAMP(*pwidth, widthmin, widthmax);
906                                         }
907                                         if (nsw->directions & NODE_RESIZE_LEFT) {
908                                                 float locmax = nsw->oldlocx + oldwidth;
909
910                                                 node->locx = nsw->oldlocx + dx;
911                                                 CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
912                                                 *pwidth = locmax - node->locx;
913                                         }
914                                 }
915
916                                 /* height works the other way round ... */
917                                 {
918                                         float heightmin = UI_DPI_FAC * node->typeinfo->minheight;
919                                         float heightmax = UI_DPI_FAC * node->typeinfo->maxheight;
920                                         if (nsw->directions & NODE_RESIZE_TOP) {
921                                                 float locmin = nsw->oldlocy - nsw->oldheight;
922
923                                                 node->locy = nsw->oldlocy + dy;
924                                                 CLAMP(node->locy, locmin + heightmin, locmin + heightmax);
925                                                 node->height = node->locy - locmin;
926                                         }
927                                         if (nsw->directions & NODE_RESIZE_BOTTOM) {
928                                                 node->height = nsw->oldheight - dy;
929                                                 CLAMP(node->height, heightmin, heightmax);
930                                         }
931                                 }
932
933                                 /* XXX make callback? */
934                                 if (node->type == NODE_FRAME) {
935                                         /* keep the offset symmetric around center point */
936                                         if (nsw->directions & NODE_RESIZE_LEFT) {
937                                                 node->locx = nsw->oldlocx + 0.5f * dx;
938                                                 node->offsetx = nsw->oldoffsetx + 0.5f * dx;
939                                         }
940                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
941                                                 node->locx = nsw->oldlocx + 0.5f * dx;
942                                                 node->offsetx = nsw->oldoffsetx - 0.5f * dx;
943                                         }
944                                         if (nsw->directions & NODE_RESIZE_TOP) {
945                                                 node->locy = nsw->oldlocy + 0.5f * dy;
946                                                 node->offsety = nsw->oldoffsety + 0.5f * dy;
947                                         }
948                                         if (nsw->directions & NODE_RESIZE_BOTTOM) {
949                                                 node->locy = nsw->oldlocy + 0.5f * dy;
950                                                 node->offsety = nsw->oldoffsety - 0.5f * dy;
951                                         }
952                                 }
953                         }
954
955                         ED_region_tag_redraw(ar);
956
957                         break;
958
959                 case LEFTMOUSE:
960                 case MIDDLEMOUSE:
961                 case RIGHTMOUSE:
962
963                         node_resize_exit(C, op, false);
964                         ED_node_post_apply_transform(C, snode->edittree);
965
966                         return OPERATOR_FINISHED;
967         }
968
969         return OPERATOR_RUNNING_MODAL;
970 }
971
972 static int node_resize_invoke(bContext *C, wmOperator *op, const wmEvent *event)
973 {
974         SpaceNode *snode = CTX_wm_space_node(C);
975         ARegion *ar = CTX_wm_region(C);
976         bNode *node = nodeGetActive(snode->edittree);
977         int dir;
978
979         if (node) {
980                 /* convert mouse coordinates to v2d space */
981                 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
982                                          &snode->cursor[0], &snode->cursor[1]);
983                 dir = node->typeinfo->resize_area_func(node, snode->cursor[0], snode->cursor[1]);
984                 if (dir != 0) {
985                         node_resize_init(C, op, event, node, dir);
986                         return OPERATOR_RUNNING_MODAL;
987                 }
988         }
989         return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
990 }
991
992 static void node_resize_cancel(bContext *C, wmOperator *op)
993 {
994         node_resize_exit(C, op, true);
995 }
996
997 void NODE_OT_resize(wmOperatorType *ot)
998 {
999         /* identifiers */
1000         ot->name = "Resize Node";
1001         ot->idname = "NODE_OT_resize";
1002         ot->description = "Resize a node";
1003
1004         /* api callbacks */
1005         ot->invoke = node_resize_invoke;
1006         ot->modal = node_resize_modal;
1007         ot->poll = ED_operator_node_active;
1008         ot->cancel = node_resize_cancel;
1009
1010         /* flags */
1011         ot->flag = OPTYPE_BLOCKING;
1012 }
1013
1014
1015 /* ********************** hidden sockets ******************** */
1016
1017 int node_has_hidden_sockets(bNode *node)
1018 {
1019         bNodeSocket *sock;
1020
1021         for (sock = node->inputs.first; sock; sock = sock->next)
1022                 if (sock->flag & SOCK_HIDDEN)
1023                         return 1;
1024         for (sock = node->outputs.first; sock; sock = sock->next)
1025                 if (sock->flag & SOCK_HIDDEN)
1026                         return 1;
1027         return 0;
1028 }
1029
1030 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
1031 {
1032         bNodeSocket *sock;
1033
1034         if (set == 0) {
1035                 for (sock = node->inputs.first; sock; sock = sock->next)
1036                         sock->flag &= ~SOCK_HIDDEN;
1037                 for (sock = node->outputs.first; sock; sock = sock->next)
1038                         sock->flag &= ~SOCK_HIDDEN;
1039         }
1040         else {
1041                 /* hide unused sockets */
1042                 for (sock = node->inputs.first; sock; sock = sock->next) {
1043                         if (sock->link == NULL)
1044                                 sock->flag |= SOCK_HIDDEN;
1045                 }
1046                 for (sock = node->outputs.first; sock; sock = sock->next) {
1047                         if (nodeCountSocketLinks(snode->edittree, sock) == 0)
1048                                 sock->flag |= SOCK_HIDDEN;
1049                 }
1050         }
1051 }
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, float cursor[2], 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                 BLI_rctf_init_pt_radius(&rect, cursor, NODE_SOCKSIZE + 4);
1069
1070                 if (!(node->flag & NODE_HIDDEN)) {
1071                         /* extra padding inside and out - allow dragging on the text areas too */
1072                         if (in_out == SOCK_IN) {
1073                                 rect.xmax += NODE_SOCKSIZE;
1074                                 rect.xmin -= NODE_SOCKSIZE * 4;
1075                         }
1076                         else if (in_out == SOCK_OUT) {
1077                                 rect.xmax += NODE_SOCKSIZE * 4;
1078                                 rect.xmin -= NODE_SOCKSIZE;
1079                         }
1080                 }
1081
1082                 if (in_out & SOCK_IN) {
1083                         for (sock = node->inputs.first; sock; sock = sock->next) {
1084                                 if (!nodeSocketIsHidden(sock)) {
1085                                         if (BLI_rctf_isect_pt(&rect, sock->locx, sock->locy)) {
1086                                                 if (node == visible_node(snode, &rect)) {
1087                                                         *nodep = node;
1088                                                         *sockp = sock;
1089                                                         return 1;
1090                                                 }
1091                                         }
1092                                 }
1093                         }
1094                 }
1095                 if (in_out & SOCK_OUT) {
1096                         for (sock = node->outputs.first; sock; sock = sock->next) {
1097                                 if (!nodeSocketIsHidden(sock)) {
1098                                         if (BLI_rctf_isect_pt(&rect, sock->locx, sock->locy)) {
1099                                                 if (node == visible_node(snode, &rect)) {
1100                                                         *nodep = node;
1101                                                         *sockp = sock;
1102                                                         return 1;
1103                                                 }
1104                                         }
1105                                 }
1106                         }
1107                 }
1108         }
1109
1110         return 0;
1111 }
1112
1113 /* ****************** Duplicate *********************** */
1114
1115 static void node_duplicate_reparent_recursive(bNode *node)
1116 {
1117         bNode *parent;
1118
1119         node->flag |= NODE_TEST;
1120
1121         /* find first selected parent */
1122         for (parent = node->parent; parent; parent = parent->parent) {
1123                 if (parent->flag & SELECT) {
1124                         if (!(parent->flag & NODE_TEST))
1125                                 node_duplicate_reparent_recursive(parent);
1126                         break;
1127                 }
1128         }
1129         /* reparent node copy to parent copy */
1130         if (parent) {
1131                 nodeDetachNode(node->new_node);
1132                 nodeAttachNode(node->new_node, parent->new_node);
1133         }
1134 }
1135
1136 static int node_duplicate_exec(bContext *C, wmOperator *op)
1137 {
1138         Main *bmain = CTX_data_main(C);
1139         SpaceNode *snode = CTX_wm_space_node(C);
1140         bNodeTree *ntree = snode->edittree;
1141         bNode *node, *newnode, *lastnode;
1142         bNodeLink *link, *newlink, *lastlink;
1143         const bool keep_inputs = RNA_boolean_get(op->ptr, "keep_inputs");
1144         bool do_tag_update = false;
1145
1146         ED_preview_kill_jobs(CTX_wm_manager(C), bmain);
1147
1148         lastnode = ntree->nodes.last;
1149         for (node = ntree->nodes.first; node; node = node->next) {
1150                 if (node->flag & SELECT) {
1151                         newnode = nodeCopyNode(ntree, node);
1152
1153                         if (newnode->id) {
1154                                 /* simple id user adjustment, node internal functions don't touch this
1155                                  * but operators and readfile.c do. */
1156                                 id_us_plus(newnode->id);
1157                                 /* to ensure redraws or rerenders happen */
1158                                 ED_node_tag_update_id(snode->id);
1159                         }
1160                 }
1161
1162                 /* make sure we don't copy new nodes again! */
1163                 if (node == lastnode)
1164                         break;
1165         }
1166
1167         /* copy links between selected nodes
1168          * NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
1169          */
1170         lastlink = ntree->links.last;
1171         for (link = ntree->links.first; link; link = link->next) {
1172                 /* This creates new links between copied nodes.
1173                  * If keep_inputs is set, also copies input links from unselected (when fromnode==NULL)!
1174                  */
1175                 if (link->tonode && (link->tonode->flag & NODE_SELECT) &&
1176                     (keep_inputs || (link->fromnode && (link->fromnode->flag & NODE_SELECT))))
1177                 {
1178                         newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
1179                         newlink->flag = link->flag;
1180                         newlink->tonode = link->tonode->new_node;
1181                         newlink->tosock = link->tosock->new_sock;
1182                         if (link->fromnode && (link->fromnode->flag & NODE_SELECT)) {
1183                                 newlink->fromnode = link->fromnode->new_node;
1184                                 newlink->fromsock = link->fromsock->new_sock;
1185                         }
1186                         else {
1187                                 /* input node not copied, this keeps the original input linked */
1188                                 newlink->fromnode = link->fromnode;
1189                                 newlink->fromsock = link->fromsock;
1190                         }
1191
1192                         BLI_addtail(&ntree->links, newlink);
1193                 }
1194
1195                 /* make sure we don't copy new links again! */
1196                 if (link == lastlink)
1197                         break;
1198         }
1199
1200         /* clear flags for recursive depth-first iteration */
1201         for (node = ntree->nodes.first; node; node = node->next)
1202                 node->flag &= ~NODE_TEST;
1203         /* reparent copied nodes */
1204         for (node = ntree->nodes.first; node; node = node->next) {
1205                 if ((node->flag & SELECT) && !(node->flag & NODE_TEST))
1206                         node_duplicate_reparent_recursive(node);
1207
1208                 /* only has to check old nodes */
1209                 if (node == lastnode)
1210                         break;
1211         }
1212
1213         /* deselect old nodes, select the copies instead */
1214         for (node = ntree->nodes.first; node; node = node->next) {
1215                 if (node->flag & SELECT) {
1216                         /* has been set during copy above */
1217                         newnode = node->new_node;
1218
1219                         nodeSetSelected(node, false);
1220                         node->flag &= ~NODE_ACTIVE;
1221                         nodeSetSelected(newnode, true);
1222
1223                         do_tag_update |= (do_tag_update || node_connected_to_output(bmain, ntree, newnode));
1224                 }
1225
1226                 /* make sure we don't copy new nodes again! */
1227                 if (node == lastnode)
1228                         break;
1229         }
1230
1231         ntreeUpdateTree(CTX_data_main(C), snode->edittree);
1232
1233         snode_notify(C, snode);
1234         if (do_tag_update) {
1235                 snode_dag_update(C, snode);
1236         }
1237
1238         return OPERATOR_FINISHED;
1239 }
1240
1241 void NODE_OT_duplicate(wmOperatorType *ot)
1242 {
1243         /* identifiers */
1244         ot->name = "Duplicate Nodes";
1245         ot->description = "Duplicate selected nodes";
1246         ot->idname = "NODE_OT_duplicate";
1247
1248         /* api callbacks */
1249         ot->exec = node_duplicate_exec;
1250         ot->poll = ED_operator_node_editable;
1251
1252         /* flags */
1253         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1254
1255         RNA_def_boolean(ot->srna, "keep_inputs", 0, "Keep Inputs", "Keep the input links to duplicated nodes");
1256 }
1257
1258 bool ED_node_select_check(ListBase *lb)
1259
1260
1261 {
1262         bNode *node;
1263
1264         for (node = lb->first; node; node = node->next) {
1265                 if (node->flag & NODE_SELECT) {
1266                         return true;
1267                 }
1268         }
1269
1270         return false;
1271 }
1272
1273 /* ******************************** */
1274 // XXX some code needing updating to operators...
1275
1276
1277 /* goes over all scenes, reads render layers */
1278 static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
1279 {
1280         Main *bmain = CTX_data_main(C);
1281         SpaceNode *snode = CTX_wm_space_node(C);
1282         Scene *curscene = CTX_data_scene(C), *scene;
1283         bNode *node;
1284
1285         ED_preview_kill_jobs(CTX_wm_manager(C), bmain);
1286
1287         /* first tag scenes unread */
1288         for (scene = bmain->scene.first; scene; scene = scene->id.next)
1289                 scene->id.tag |= LIB_TAG_DOIT;
1290
1291         for (node = snode->edittree->nodes.first; node; node = node->next) {
1292                 if (node->type == CMP_NODE_R_LAYERS) {
1293                         ID *id = node->id;
1294                         if (id->tag & LIB_TAG_DOIT) {
1295                                 RE_ReadRenderResult(curscene, (Scene *)id);
1296                                 ntreeCompositTagRender((Scene *)id);
1297                                 id->tag &= ~LIB_TAG_DOIT;
1298                         }
1299                 }
1300         }
1301
1302         snode_notify(C, snode);
1303         snode_dag_update(C, snode);
1304
1305         return OPERATOR_FINISHED;
1306 }
1307
1308 void NODE_OT_read_renderlayers(wmOperatorType *ot)
1309 {
1310
1311         ot->name = "Read Render Layers";
1312         ot->idname = "NODE_OT_read_renderlayers";
1313         ot->description = "Read all render layers of all used scenes";
1314
1315         ot->exec = node_read_renderlayers_exec;
1316
1317         ot->poll = composite_node_active;
1318
1319         /* flags */
1320         ot->flag = 0;
1321 }
1322
1323 static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
1324 {
1325         Main *bmain = CTX_data_main(C);
1326         SpaceNode *snode = CTX_wm_space_node(C);
1327         Scene *curscene = CTX_data_scene(C);
1328         Render *re = RE_NewSceneRender(curscene);
1329
1330         WM_cursor_wait(1);
1331         RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
1332         WM_cursor_wait(0);
1333
1334         /* note we are careful to send the right notifier, as otherwise the
1335          * compositor would reexecute and overwrite the full sample result */
1336         WM_event_add_notifier(C, NC_SCENE | ND_COMPO_RESULT, NULL);
1337
1338         return OPERATOR_FINISHED;
1339 }
1340
1341
1342 void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
1343 {
1344
1345         ot->name = "Read Full Sample Layers";
1346         ot->idname = "NODE_OT_read_fullsamplelayers";
1347         ot->description = "Read all render layers of current scene, in full sample";
1348
1349         ot->exec = node_read_fullsamplelayers_exec;
1350
1351         ot->poll = composite_node_active;
1352
1353         /* flags */
1354         ot->flag = 0;
1355 }
1356
1357 int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
1358 {
1359         Scene *sce = CTX_data_scene(C);
1360         bNode *node;
1361
1362         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1363                 if (node->id == (ID *)sce && node->need_exec) {
1364                         break;
1365                 }
1366         }
1367         if (node) {
1368                 SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
1369
1370                 if (srl) {
1371                         PointerRNA op_ptr;
1372
1373                         WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
1374                         RNA_string_set(&op_ptr, "layer", srl->name);
1375                         RNA_string_set(&op_ptr, "scene", sce->id.name + 2);
1376
1377                         /* to keep keypositions */
1378                         sce->r.scemode |= R_NO_FRAME_UPDATE;
1379
1380                         WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
1381
1382                         WM_operator_properties_free(&op_ptr);
1383
1384                         return OPERATOR_FINISHED;
1385                 }
1386         }
1387         return OPERATOR_CANCELLED;
1388 }
1389
1390 void NODE_OT_render_changed(wmOperatorType *ot)
1391 {
1392         ot->name = "Render Changed Layer";
1393         ot->idname = "NODE_OT_render_changed";
1394         ot->description = "Render current scene, when input node's layer has been changed";
1395
1396         ot->exec = node_render_changed_exec;
1397
1398         ot->poll = composite_node_active;
1399
1400         /* flags */
1401         ot->flag = 0;
1402 }
1403
1404
1405 /* ****************** Hide operator *********************** */
1406
1407 static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
1408 {
1409         bNode *node;
1410         int tot_eq = 0, tot_neq = 0;
1411
1412         /* Toggles the flag on all selected nodes.
1413          * If the flag is set on all nodes it is unset.
1414          * If the flag is not set on all nodes, it is set.
1415          */
1416         for (node = snode->edittree->nodes.first; node; node = node->next) {
1417                 if (node->flag & SELECT) {
1418
1419                         if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
1420                                 continue;
1421                         if (toggle_flag == NODE_OPTIONS && !(node->typeinfo->draw_buttons || node->typeinfo->draw_buttons_ex))
1422                                 continue;
1423
1424                         if (node->flag & toggle_flag)
1425                                 tot_eq++;
1426                         else
1427                                 tot_neq++;
1428                 }
1429         }
1430         for (node = snode->edittree->nodes.first; node; node = node->next) {
1431                 if (node->flag & SELECT) {
1432
1433                         if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
1434                                 continue;
1435                         if (toggle_flag == NODE_OPTIONS && !(node->typeinfo->draw_buttons || node->typeinfo->draw_buttons_ex))
1436                                 continue;
1437
1438                         if ((tot_eq && tot_neq) || tot_eq == 0)
1439                                 node->flag |= toggle_flag;
1440                         else
1441                                 node->flag &= ~toggle_flag;
1442                 }
1443         }
1444 }
1445
1446 static int node_hide_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1447 {
1448         SpaceNode *snode = CTX_wm_space_node(C);
1449
1450         /* sanity checking (poll callback checks this already) */
1451         if ((snode == NULL) || (snode->edittree == NULL))
1452                 return OPERATOR_CANCELLED;
1453
1454         node_flag_toggle_exec(snode, NODE_HIDDEN);
1455
1456         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1457
1458         return OPERATOR_FINISHED;
1459 }
1460
1461 void NODE_OT_hide_toggle(wmOperatorType *ot)
1462 {
1463         /* identifiers */
1464         ot->name = "Hide";
1465         ot->description = "Toggle hiding of selected nodes";
1466         ot->idname = "NODE_OT_hide_toggle";
1467
1468         /* callbacks */
1469         ot->exec = node_hide_toggle_exec;
1470         ot->poll = ED_operator_node_active;
1471
1472         /* flags */
1473         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1474 }
1475
1476 static int node_preview_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1477 {
1478         SpaceNode *snode = CTX_wm_space_node(C);
1479
1480         /* sanity checking (poll callback checks this already) */
1481         if ((snode == NULL) || (snode->edittree == NULL))
1482                 return OPERATOR_CANCELLED;
1483
1484         ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
1485
1486         node_flag_toggle_exec(snode, NODE_PREVIEW);
1487
1488         snode_notify(C, snode);
1489
1490         return OPERATOR_FINISHED;
1491 }
1492
1493 void NODE_OT_preview_toggle(wmOperatorType *ot)
1494 {
1495         /* identifiers */
1496         ot->name = "Toggle Node Preview";
1497         ot->description = "Toggle preview display for selected nodes";
1498         ot->idname = "NODE_OT_preview_toggle";
1499
1500         /* callbacks */
1501         ot->exec = node_preview_toggle_exec;
1502         ot->poll = ED_operator_node_active;
1503
1504         /* flags */
1505         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1506 }
1507
1508 static int node_options_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1509 {
1510         SpaceNode *snode = CTX_wm_space_node(C);
1511
1512         /* sanity checking (poll callback checks this already) */
1513         if ((snode == NULL) || (snode->edittree == NULL))
1514                 return OPERATOR_CANCELLED;
1515
1516         node_flag_toggle_exec(snode, NODE_OPTIONS);
1517
1518         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1519
1520         return OPERATOR_FINISHED;
1521 }
1522
1523 void NODE_OT_options_toggle(wmOperatorType *ot)
1524 {
1525         /* identifiers */
1526         ot->name = "Toggle Node Options";
1527         ot->description = "Toggle option buttons display for selected nodes";
1528         ot->idname = "NODE_OT_options_toggle";
1529
1530         /* callbacks */
1531         ot->exec = node_options_toggle_exec;
1532         ot->poll = ED_operator_node_active;
1533
1534         /* flags */
1535         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1536 }
1537
1538 static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1539 {
1540         SpaceNode *snode = CTX_wm_space_node(C);
1541         bNode *node;
1542         int hidden;
1543
1544         /* sanity checking (poll callback checks this already) */
1545         if ((snode == NULL) || (snode->edittree == NULL))
1546                 return OPERATOR_CANCELLED;
1547
1548         ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
1549
1550         /* Toggle for all selected nodes */
1551         hidden = 0;
1552         for (node = snode->edittree->nodes.first; node; node = node->next) {
1553                 if (node->flag & SELECT) {
1554                         if (node_has_hidden_sockets(node)) {
1555                                 hidden = 1;
1556                                 break;
1557                         }
1558                 }
1559         }
1560
1561         for (node = snode->edittree->nodes.first; node; node = node->next) {
1562                 if (node->flag & SELECT) {
1563                         node_set_hidden_sockets(snode, node, !hidden);
1564                 }
1565         }
1566
1567         ntreeUpdateTree(CTX_data_main(C), snode->edittree);
1568
1569         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1570
1571         return OPERATOR_FINISHED;
1572 }
1573
1574 void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
1575 {
1576         /* identifiers */
1577         ot->name = "Toggle Hidden Node Sockets";
1578         ot->description = "Toggle unused node socket display";
1579         ot->idname = "NODE_OT_hide_socket_toggle";
1580
1581         /* callbacks */
1582         ot->exec = node_socket_toggle_exec;
1583         ot->poll = ED_operator_node_active;
1584
1585         /* flags */
1586         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1587 }
1588
1589 /* ****************** Mute operator *********************** */
1590
1591 static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
1592 {
1593         Main *bmain = CTX_data_main(C);
1594         SpaceNode *snode = CTX_wm_space_node(C);
1595         bNode *node;
1596         bool do_tag_update = false;
1597
1598         ED_preview_kill_jobs(CTX_wm_manager(C), bmain);
1599
1600         for (node = snode->edittree->nodes.first; node; node = node->next) {
1601                 /* Only allow muting of nodes having a mute func! */
1602                 if ((node->flag & SELECT) && node->typeinfo->update_internal_links) {
1603                         node->flag ^= NODE_MUTED;
1604                         snode_update(snode, node);
1605                         do_tag_update |= (do_tag_update || node_connected_to_output(bmain, snode->edittree, node));
1606                 }
1607         }
1608
1609         snode_notify(C, snode);
1610         if (do_tag_update) {
1611                 snode_dag_update(C, snode);
1612         }
1613
1614         return OPERATOR_FINISHED;
1615 }
1616
1617 void NODE_OT_mute_toggle(wmOperatorType *ot)
1618 {
1619         /* identifiers */
1620         ot->name = "Toggle Node Mute";
1621         ot->description = "Toggle muting of the nodes";
1622         ot->idname = "NODE_OT_mute_toggle";
1623
1624         /* callbacks */
1625         ot->exec = node_mute_exec;
1626         ot->poll = ED_operator_node_editable;
1627
1628         /* flags */
1629         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1630 }
1631
1632 /* ****************** Delete operator ******************* */
1633
1634 static int node_delete_exec(bContext *C, wmOperator *UNUSED(op))
1635 {
1636         Main *bmain = CTX_data_main(C);
1637         SpaceNode *snode = CTX_wm_space_node(C);
1638         bNode *node, *next;
1639         bool do_tag_update = false;
1640
1641         ED_preview_kill_jobs(CTX_wm_manager(C), bmain);
1642
1643         for (node = snode->edittree->nodes.first; node; node = next) {
1644                 next = node->next;
1645                 if (node->flag & SELECT) {
1646                         /* check id user here, nodeFreeNode is called for free dbase too */
1647                         do_tag_update |= (do_tag_update || node_connected_to_output(bmain, snode->edittree, node));
1648                         if (node->id)
1649                                 id_us_min(node->id);
1650                         nodeFreeNode(snode->edittree, node);
1651                 }
1652         }
1653
1654         ntreeUpdateTree(CTX_data_main(C), snode->edittree);
1655
1656         snode_notify(C, snode);
1657         if (do_tag_update) {
1658                 snode_dag_update(C, snode);
1659         }
1660
1661         return OPERATOR_FINISHED;
1662 }
1663
1664 void NODE_OT_delete(wmOperatorType *ot)
1665 {
1666         /* identifiers */
1667         ot->name = "Delete";
1668         ot->description = "Delete selected nodes";
1669         ot->idname = "NODE_OT_delete";
1670
1671         /* api callbacks */
1672         ot->exec = node_delete_exec;
1673         ot->poll = ED_operator_node_editable;
1674
1675         /* flags */
1676         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1677 }
1678
1679 /* ****************** Switch View ******************* */
1680
1681 static int node_switch_view_poll(bContext *C)
1682 {
1683         SpaceNode *snode = CTX_wm_space_node(C);
1684
1685         if (snode && snode->edittree)
1686                 return true;
1687
1688         return false;
1689 }
1690
1691 static int node_switch_view_exec(bContext *C, wmOperator *UNUSED(op))
1692 {
1693         SpaceNode *snode = CTX_wm_space_node(C);
1694         bNode *node, *next;
1695
1696         for (node = snode->edittree->nodes.first; node; node = next) {
1697                 next = node->next;
1698                 if (node->flag & SELECT) {
1699                         /* call the update function from the Switch View node */
1700                         node->update = NODE_UPDATE_OPERATOR;
1701                 }
1702         }
1703
1704         ntreeUpdateTree(CTX_data_main(C), snode->edittree);
1705
1706         snode_notify(C, snode);
1707         snode_dag_update(C, snode);
1708
1709         return OPERATOR_FINISHED;
1710 }
1711
1712 void NODE_OT_switch_view_update(wmOperatorType *ot)
1713 {
1714         /* identifiers */
1715         ot->name = "Update Views";
1716         ot->description = "Update views of selected node";
1717         ot->idname = "NODE_OT_switch_view_update";
1718
1719         /* api callbacks */
1720         ot->exec = node_switch_view_exec;
1721         ot->poll = node_switch_view_poll;
1722
1723         /* flags */
1724         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1725 }
1726
1727 /* ****************** Delete with reconnect ******************* */
1728 static int node_delete_reconnect_exec(bContext *C, wmOperator *UNUSED(op))
1729 {
1730         SpaceNode *snode = CTX_wm_space_node(C);
1731         bNode *node, *next;
1732
1733         ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
1734
1735         for (node = snode->edittree->nodes.first; node; node = next) {
1736                 next = node->next;
1737                 if (node->flag & SELECT) {
1738                         nodeInternalRelink(snode->edittree, node);
1739
1740                         /* check id user here, nodeFreeNode is called for free dbase too */
1741                         if (node->id)
1742                                 id_us_min(node->id);
1743                         nodeFreeNode(snode->edittree, node);
1744                 }
1745         }
1746
1747         ntreeUpdateTree(CTX_data_main(C), snode->edittree);
1748
1749         snode_notify(C, snode);
1750         snode_dag_update(C, snode);
1751
1752         return OPERATOR_FINISHED;
1753 }
1754
1755 void NODE_OT_delete_reconnect(wmOperatorType *ot)
1756 {
1757         /* identifiers */
1758         ot->name = "Delete with Reconnect";
1759         ot->description = "Delete nodes; will reconnect nodes as if deletion was muted";
1760         ot->idname = "NODE_OT_delete_reconnect";
1761
1762         /* api callbacks */
1763         ot->exec = node_delete_reconnect_exec;
1764         ot->poll = ED_operator_node_editable;
1765
1766         /* flags */
1767         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1768 }
1769
1770
1771 /* ****************** File Output Add Socket  ******************* */
1772
1773 static int node_output_file_add_socket_exec(bContext *C, wmOperator *op)
1774 {
1775         Scene *scene = CTX_data_scene(C);
1776         SpaceNode *snode = CTX_wm_space_node(C);
1777         PointerRNA ptr = CTX_data_pointer_get(C, "node");
1778         bNodeTree *ntree = NULL;
1779         bNode *node = NULL;
1780         char file_path[MAX_NAME];
1781
1782         if (ptr.data) {
1783                 node = ptr.data;
1784                 ntree = ptr.id.data;
1785         }
1786         else if (snode && snode->edittree) {
1787                 ntree = snode->edittree;
1788                 node = nodeGetActive(snode->edittree);
1789         }
1790
1791         if (!node || node->type != CMP_NODE_OUTPUT_FILE)
1792                 return OPERATOR_CANCELLED;
1793
1794         RNA_string_get(op->ptr, "file_path", file_path);
1795         ntreeCompositOutputFileAddSocket(ntree, node, file_path, &scene->r.im_format);
1796
1797         snode_notify(C, snode);
1798
1799         return OPERATOR_FINISHED;
1800 }
1801
1802 void NODE_OT_output_file_add_socket(wmOperatorType *ot)
1803 {
1804         /* identifiers */
1805         ot->name = "Add File Node Socket";
1806         ot->description = "Add a new input to a file output node";
1807         ot->idname = "NODE_OT_output_file_add_socket";
1808
1809         /* callbacks */
1810         ot->exec = node_output_file_add_socket_exec;
1811         ot->poll = composite_node_editable;
1812
1813         /* flags */
1814         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1815
1816         RNA_def_string(ot->srna, "file_path", "Image", MAX_NAME, "File Path", "Sub-path of the output file");
1817 }
1818
1819 /* ****************** Multi File Output Remove Socket  ******************* */
1820
1821 static int node_output_file_remove_active_socket_exec(bContext *C, wmOperator *UNUSED(op))
1822 {
1823         SpaceNode *snode = CTX_wm_space_node(C);
1824         PointerRNA ptr = CTX_data_pointer_get(C, "node");
1825         bNodeTree *ntree = NULL;
1826         bNode *node = NULL;
1827
1828         if (ptr.data) {
1829                 node = ptr.data;
1830                 ntree = ptr.id.data;
1831         }
1832         else if (snode && snode->edittree) {
1833                 ntree = snode->edittree;
1834                 node = nodeGetActive(snode->edittree);
1835         }
1836
1837         if (!node || node->type != CMP_NODE_OUTPUT_FILE)
1838                 return OPERATOR_CANCELLED;
1839
1840         if (!ntreeCompositOutputFileRemoveActiveSocket(ntree, node))
1841                 return OPERATOR_CANCELLED;
1842
1843         snode_notify(C, snode);
1844
1845         return OPERATOR_FINISHED;
1846 }
1847
1848 void NODE_OT_output_file_remove_active_socket(wmOperatorType *ot)
1849 {
1850         /* identifiers */
1851         ot->name = "Remove File Node Socket";
1852         ot->description = "Remove active input from a file output node";
1853         ot->idname = "NODE_OT_output_file_remove_active_socket";
1854
1855         /* callbacks */
1856         ot->exec = node_output_file_remove_active_socket_exec;
1857         ot->poll = composite_node_editable;
1858
1859         /* flags */
1860         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1861 }
1862
1863 /* ****************** Multi File Output Move Socket  ******************* */
1864
1865 static int node_output_file_move_active_socket_exec(bContext *C, wmOperator *op)
1866 {
1867         SpaceNode *snode = CTX_wm_space_node(C);
1868         PointerRNA ptr = CTX_data_pointer_get(C, "node");
1869         bNode *node = NULL;
1870         NodeImageMultiFile *nimf;
1871         bNodeSocket *sock;
1872         int direction;
1873
1874         if (ptr.data)
1875                 node = ptr.data;
1876         else if (snode && snode->edittree)
1877                 node = nodeGetActive(snode->edittree);
1878
1879         if (!node || node->type != CMP_NODE_OUTPUT_FILE)
1880                 return OPERATOR_CANCELLED;
1881
1882         nimf = node->storage;
1883
1884         sock = BLI_findlink(&node->inputs, nimf->active_input);
1885         if (!sock)
1886                 return OPERATOR_CANCELLED;
1887
1888         direction = RNA_enum_get(op->ptr, "direction");
1889
1890         if (direction == 1) {
1891                 bNodeSocket *before = sock->prev;
1892                 if (!before)
1893                         return OPERATOR_CANCELLED;
1894                 BLI_remlink(&node->inputs, sock);
1895                 BLI_insertlinkbefore(&node->inputs, before, sock);
1896                 nimf->active_input--;
1897         }
1898         else {
1899                 bNodeSocket *after = sock->next;
1900                 if (!after)
1901                         return OPERATOR_CANCELLED;
1902                 BLI_remlink(&node->inputs, sock);
1903                 BLI_insertlinkafter(&node->inputs, after, sock);
1904                 nimf->active_input++;
1905         }
1906
1907         snode_notify(C, snode);
1908
1909         return OPERATOR_FINISHED;
1910 }
1911
1912 void NODE_OT_output_file_move_active_socket(wmOperatorType *ot)
1913 {
1914         static const EnumPropertyItem direction_items[] = {
1915                 {1, "UP", 0, "Up", ""},
1916                 {2, "DOWN", 0, "Down", ""},
1917                 { 0, NULL, 0, NULL, NULL }
1918         };
1919
1920         /* identifiers */
1921         ot->name = "Move File Node Socket";
1922         ot->description = "Move the active input of a file output node up or down the list";
1923         ot->idname = "NODE_OT_output_file_move_active_socket";
1924
1925         /* callbacks */
1926         ot->exec = node_output_file_move_active_socket_exec;
1927         ot->poll = composite_node_editable;
1928
1929         /* flags */
1930         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1931
1932         RNA_def_enum(ot->srna, "direction", direction_items, 2, "Direction", "");
1933 }
1934
1935 /* ****************** Copy Node Color ******************* */
1936
1937 static int node_copy_color_exec(bContext *C, wmOperator *UNUSED(op))
1938 {
1939         SpaceNode *snode = CTX_wm_space_node(C);
1940         bNodeTree *ntree = snode->edittree;
1941         bNode *node, *tnode;
1942
1943         if (!ntree)
1944                 return OPERATOR_CANCELLED;
1945         node = nodeGetActive(ntree);
1946         if (!node)
1947                 return OPERATOR_CANCELLED;
1948
1949         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
1950                 if (tnode->flag & NODE_SELECT && tnode != node) {
1951                         if (node->flag & NODE_CUSTOM_COLOR) {
1952                                 tnode->flag |= NODE_CUSTOM_COLOR;
1953                                 copy_v3_v3(tnode->color, node->color);
1954                         }
1955                         else
1956                                 tnode->flag &= ~NODE_CUSTOM_COLOR;
1957                 }
1958         }
1959
1960         ED_node_sort(ntree);
1961         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1962
1963         return OPERATOR_FINISHED;
1964 }
1965
1966 void NODE_OT_node_copy_color(wmOperatorType *ot)
1967 {
1968         /* identifiers */
1969         ot->name = "Copy Color";
1970         ot->description = "Copy color to all selected nodes";
1971         ot->idname = "NODE_OT_node_copy_color";
1972
1973         /* api callbacks */
1974         ot->exec = node_copy_color_exec;
1975         ot->poll = ED_operator_node_editable;
1976
1977         /* flags */
1978         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1979 }
1980
1981 /* ****************** Copy to clipboard ******************* */
1982
1983 static int node_clipboard_copy_exec(bContext *C, wmOperator *UNUSED(op))
1984 {
1985         SpaceNode *snode = CTX_wm_space_node(C);
1986         bNodeTree *ntree = snode->edittree;
1987         bNode *node;
1988         bNodeLink *link, *newlink;
1989
1990         ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
1991
1992         /* clear current clipboard */
1993         BKE_node_clipboard_clear();
1994         BKE_node_clipboard_init(ntree);
1995
1996         for (node = ntree->nodes.first; node; node = node->next) {
1997                 if (node->flag & SELECT) {
1998                         bNode *new_node;
1999                         new_node = nodeCopyNode(NULL, node);
2000                         BKE_node_clipboard_add_node(new_node);
2001                 }
2002         }
2003
2004         for (node = ntree->nodes.first; node; node = node->next) {
2005                 if (node->flag & SELECT) {
2006                         bNode *new_node = node->new_node;
2007
2008                         /* ensure valid pointers */
2009                         if (new_node->parent) {
2010                                 /* parent pointer must be redirected to new node or detached if parent is not copied */
2011                                 if (new_node->parent->flag & NODE_SELECT) {
2012                                         new_node->parent = new_node->parent->new_node;
2013                                 }
2014                                 else {
2015                                         nodeDetachNode(new_node);
2016                                 }
2017                         }
2018                 }
2019         }
2020
2021         /* copy links between selected nodes
2022          * NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
2023          */
2024         for (link = ntree->links.first; link; link = link->next) {
2025                 /* This creates new links between copied nodes. */
2026                 if (link->tonode && (link->tonode->flag & NODE_SELECT) &&
2027                     link->fromnode && (link->fromnode->flag & NODE_SELECT))
2028                 {
2029                         newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
2030                         newlink->flag = link->flag;
2031                         newlink->tonode = link->tonode->new_node;
2032                         newlink->tosock = link->tosock->new_sock;
2033                         newlink->fromnode = link->fromnode->new_node;
2034                         newlink->fromsock = link->fromsock->new_sock;
2035
2036                         BKE_node_clipboard_add_link(newlink);
2037                 }
2038         }
2039
2040         return OPERATOR_FINISHED;
2041 }
2042
2043 void NODE_OT_clipboard_copy(wmOperatorType *ot)
2044 {
2045         /* identifiers */
2046         ot->name = "Copy to Clipboard";
2047         ot->description = "Copies selected nodes to the clipboard";
2048         ot->idname = "NODE_OT_clipboard_copy";
2049
2050         /* api callbacks */
2051         ot->exec = node_clipboard_copy_exec;
2052         ot->poll = ED_operator_node_active;
2053
2054         /* flags */
2055         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2056 }
2057
2058 /* ****************** Paste from clipboard ******************* */
2059
2060 static int node_clipboard_paste_exec(bContext *C, wmOperator *op)
2061 {
2062         SpaceNode *snode = CTX_wm_space_node(C);
2063         bNodeTree *ntree = snode->edittree;
2064         const ListBase *clipboard_nodes_lb;
2065         const ListBase *clipboard_links_lb;
2066         bNode *node;
2067         bNodeLink *link;
2068         int num_nodes;
2069         float center[2];
2070         bool is_clipboard_valid, all_nodes_valid;
2071
2072         /* validate pointers in the clipboard */
2073         is_clipboard_valid = BKE_node_clipboard_validate();
2074         clipboard_nodes_lb = BKE_node_clipboard_get_nodes();
2075         clipboard_links_lb = BKE_node_clipboard_get_links();
2076
2077         if (BLI_listbase_is_empty(clipboard_nodes_lb)) {
2078                 BKE_report(op->reports, RPT_ERROR, "Clipboard is empty");
2079                 return OPERATOR_CANCELLED;
2080         }
2081
2082         if (BKE_node_clipboard_get_type() != ntree->type) {
2083                 BKE_report(op->reports, RPT_ERROR, "Clipboard nodes are an incompatible type");
2084                 return OPERATOR_CANCELLED;
2085         }
2086
2087         /* only warn */
2088         if (is_clipboard_valid == false) {
2089                 BKE_report(op->reports, RPT_WARNING, "Some nodes references could not be restored, will be left empty");
2090         }
2091
2092         /* make sure all clipboard nodes would be valid in the target tree */
2093         all_nodes_valid = true;
2094         for (node = clipboard_nodes_lb->first; node; node = node->next) {
2095                 if (!node->typeinfo->poll_instance || !node->typeinfo->poll_instance(node, ntree)) {
2096                         all_nodes_valid = false;
2097                         BKE_reportf(op->reports, RPT_ERROR, "Cannot add node %s into node tree %s", node->name, ntree->id.name + 2);
2098                 }
2099         }
2100         if (!all_nodes_valid)
2101                 return OPERATOR_CANCELLED;
2102
2103         ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
2104
2105         /* deselect old nodes */
2106         node_deselect_all(snode);
2107
2108         /* calculate "barycenter" for placing on mouse cursor */
2109         zero_v2(center);
2110         for (node = clipboard_nodes_lb->first, num_nodes = 0; node; node = node->next, num_nodes++) {
2111                 center[0] += BLI_rctf_cent_x(&node->totr);
2112                 center[1] += BLI_rctf_cent_y(&node->totr);
2113         }
2114         mul_v2_fl(center, 1.0 / num_nodes);
2115
2116         /* copy nodes from clipboard */
2117         for (node = clipboard_nodes_lb->first; node; node = node->next) {
2118                 bNode *new_node = nodeCopyNode(ntree, node);
2119
2120                 /* needed since nodeCopyNode() doesn't increase ID's */
2121                 id_us_plus(node->id);
2122
2123                 /* pasted nodes are selected */
2124                 nodeSetSelected(new_node, true);
2125         }
2126
2127         /* reparent copied nodes */
2128         for (node = clipboard_nodes_lb->first; node; node = node->next) {
2129                 bNode *new_node = node->new_node;
2130                 if (new_node->parent)
2131                         new_node->parent = new_node->parent->new_node;
2132         }
2133
2134         for (link = clipboard_links_lb->first; link; link = link->next) {
2135                 nodeAddLink(ntree, link->fromnode->new_node, link->fromsock->new_sock,
2136                             link->tonode->new_node, link->tosock->new_sock);
2137         }
2138
2139         ntreeUpdateTree(CTX_data_main(C), snode->edittree);
2140
2141         snode_notify(C, snode);
2142         snode_dag_update(C, snode);
2143
2144         return OPERATOR_FINISHED;
2145 }
2146
2147 void NODE_OT_clipboard_paste(wmOperatorType *ot)
2148 {
2149         /* identifiers */
2150         ot->name = "Paste from Clipboard";
2151         ot->description = "Pastes nodes from the clipboard to the active node tree";
2152         ot->idname = "NODE_OT_clipboard_paste";
2153
2154         /* api callbacks */
2155         ot->exec = node_clipboard_paste_exec;
2156         ot->poll = ED_operator_node_editable;
2157
2158         /* flags */
2159         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2160 }
2161
2162 /********************** Add interface socket operator *********************/
2163
2164 static bNodeSocket *ntree_get_active_interface_socket(ListBase *lb)
2165 {
2166         bNodeSocket *sock;
2167         for (sock = lb->first; sock; sock = sock->next)
2168                 if (sock->flag & SELECT)
2169                         return sock;
2170         return NULL;
2171 }
2172
2173 static int ntree_socket_add_exec(bContext *C, wmOperator *op)
2174 {
2175         SpaceNode *snode = CTX_wm_space_node(C);
2176         bNodeTree *ntree = snode->edittree;
2177         int in_out = RNA_enum_get(op->ptr, "in_out");
2178         PointerRNA ntree_ptr;
2179         bNodeSocket *sock, *tsock, *active_sock;
2180         const char *default_name;
2181
2182         RNA_id_pointer_create((ID *)ntree, &ntree_ptr);
2183
2184         if (in_out == SOCK_IN) {
2185                 active_sock = ntree_get_active_interface_socket(&ntree->inputs);
2186                 default_name = "Input";
2187         }
2188         else {
2189                 active_sock = ntree_get_active_interface_socket(&ntree->outputs);
2190                 default_name = "Output";
2191         }
2192
2193         if (active_sock) {
2194                 /* insert a copy of the active socket right after it */
2195                 sock = ntreeInsertSocketInterface(ntree, in_out, active_sock->idname, active_sock->next, active_sock->name);
2196                 /* XXX this only works for actual sockets, not interface templates! */
2197                 /*nodeSocketCopyValue(sock, &ntree_ptr, active_sock, &ntree_ptr);*/
2198         }
2199         else {
2200                 /* XXX TODO define default socket type for a tree! */
2201                 sock = ntreeAddSocketInterface(ntree, in_out, "NodeSocketFloat", default_name);
2202         }
2203
2204         /* deactivate sockets (has to check both lists) */
2205         for (tsock = ntree->inputs.first; tsock; tsock = tsock->next)
2206                 tsock->flag &= ~SELECT;
2207         for (tsock = ntree->outputs.first; tsock; tsock = tsock->next)
2208                 tsock->flag &= ~SELECT;
2209         /* make the new socket active */
2210         sock->flag |= SELECT;
2211
2212         ntreeUpdateTree(CTX_data_main(C), ntree);
2213
2214         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
2215
2216         return OPERATOR_FINISHED;
2217 }
2218
2219 void NODE_OT_tree_socket_add(wmOperatorType *ot)
2220 {
2221         /* identifiers */
2222         ot->name = "Add Node Tree Interface Socket";
2223         ot->description = "Add an input or output socket to the current node tree";
2224         ot->idname = "NODE_OT_tree_socket_add";
2225
2226         /* api callbacks */
2227         ot->exec = ntree_socket_add_exec;
2228         ot->poll = ED_operator_node_editable;
2229
2230         /* flags */
2231         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2232
2233         RNA_def_enum(ot->srna, "in_out", rna_enum_node_socket_in_out_items, SOCK_IN, "Socket Type", "");
2234 }
2235
2236 /********************** Remove interface socket operator *********************/
2237
2238 static int ntree_socket_remove_exec(bContext *C, wmOperator *UNUSED(op))
2239 {
2240         SpaceNode *snode = CTX_wm_space_node(C);
2241         bNodeTree *ntree = snode->edittree;
2242         bNodeSocket *iosock, *active_sock;
2243
2244         iosock = ntree_get_active_interface_socket(&ntree->inputs);
2245         if (!iosock)
2246                 iosock = ntree_get_active_interface_socket(&ntree->outputs);
2247         if (!iosock)
2248                 return OPERATOR_CANCELLED;
2249
2250         /* preferably next socket becomes active, otherwise try previous socket */
2251         active_sock = (iosock->next ? iosock->next : iosock->prev);
2252         ntreeRemoveSocketInterface(ntree, iosock);
2253
2254         /* set active socket */
2255         if (active_sock)
2256                 active_sock->flag |= SELECT;
2257
2258         ntreeUpdateTree(CTX_data_main(C), ntree);
2259
2260         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
2261
2262         return OPERATOR_FINISHED;
2263 }
2264
2265 void NODE_OT_tree_socket_remove(wmOperatorType *ot)
2266 {
2267         /* identifiers */
2268         ot->name = "Remove Node Tree Interface Socket";
2269         ot->description = "Remove an input or output socket to the current node tree";
2270         ot->idname = "NODE_OT_tree_socket_remove";
2271
2272         /* api callbacks */
2273         ot->exec = ntree_socket_remove_exec;
2274         ot->poll = ED_operator_node_editable;
2275
2276         /* flags */
2277         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2278 }
2279
2280 /********************** Move interface socket operator *********************/
2281
2282 static const EnumPropertyItem move_direction_items[] = {
2283         { 1, "UP", 0, "Up", "" },
2284         { 2, "DOWN", 0, "Down", "" },
2285         { 0, NULL, 0, NULL, NULL },
2286 };
2287
2288 static int ntree_socket_move_exec(bContext *C, wmOperator *op)
2289 {
2290         SpaceNode *snode = CTX_wm_space_node(C);
2291         bNodeTree *ntree = snode->edittree;
2292         int direction = RNA_enum_get(op->ptr, "direction");
2293         bNodeSocket *iosock;
2294         ListBase *lb;
2295
2296         lb = &ntree->inputs;
2297         iosock = ntree_get_active_interface_socket(lb);
2298         if (!iosock) {
2299                 lb = &ntree->outputs;
2300                 iosock = ntree_get_active_interface_socket(lb);
2301         }
2302         if (!iosock)
2303                 return OPERATOR_CANCELLED;
2304
2305         switch (direction) {
2306                 case 1:
2307                 {       /* up */
2308                         bNodeSocket *before = iosock->prev;
2309                         BLI_remlink(lb, iosock);
2310                         if (before)
2311                                 BLI_insertlinkbefore(lb, before, iosock);
2312                         else
2313                                 BLI_addhead(lb, iosock);
2314                         break;
2315                 }
2316                 case 2:
2317                 {       /* down */
2318                         bNodeSocket *after = iosock->next;
2319                         BLI_remlink(lb, iosock);
2320                         if (after)
2321                                 BLI_insertlinkafter(lb, after, iosock);
2322                         else
2323                                 BLI_addtail(lb, iosock);
2324                         break;
2325                 }
2326         }
2327
2328         ntreeUpdateTree(CTX_data_main(C), ntree);
2329
2330         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
2331
2332         return OPERATOR_FINISHED;
2333 }
2334
2335 void NODE_OT_tree_socket_move(wmOperatorType *ot)
2336 {
2337         /* identifiers */
2338         ot->name = "Move Node Tree Socket";
2339         ot->description = "Move a socket up or down in the current node tree's sockets stack";
2340         ot->idname = "NODE_OT_tree_socket_move";
2341
2342         /* api callbacks */
2343         ot->exec = ntree_socket_move_exec;
2344         ot->poll = ED_operator_node_editable;
2345
2346         /* flags */
2347         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2348
2349         RNA_def_enum(ot->srna, "direction", move_direction_items, 1, "Direction", "");
2350 }
2351
2352 /* ********************** Shader Script Update ******************/
2353
2354 static int node_shader_script_update_poll(bContext *C)
2355 {
2356         Scene *scene = CTX_data_scene(C);
2357         RenderEngineType *type = RE_engines_find(scene->r.engine);
2358         SpaceNode *snode = CTX_wm_space_node(C);
2359         bNode *node;
2360         Text *text;
2361
2362         /* test if we have a render engine that supports shaders scripts */
2363         if (!(type && type->update_script_node))
2364                 return 0;
2365
2366         /* see if we have a shader script node in context */
2367         node = CTX_data_pointer_get_type(C, "node", &RNA_ShaderNodeScript).data;
2368
2369         if (!node && snode && snode->edittree)
2370                 node = nodeGetActive(snode->edittree);
2371
2372         if (node && node->type == SH_NODE_SCRIPT) {
2373                 NodeShaderScript *nss = node->storage;
2374
2375                 if (node->id || nss->filepath[0]) {
2376                         return ED_operator_node_editable(C);
2377                 }
2378         }
2379
2380         /* see if we have a text datablock in context */
2381         text = CTX_data_pointer_get_type(C, "edit_text", &RNA_Text).data;
2382         if (text)
2383                 return 1;
2384
2385         /* we don't check if text datablock is actually in use, too slow for poll */
2386
2387         return 0;
2388 }
2389
2390 /* recursively check for script nodes in groups using this text and update */
2391 static bool node_shader_script_update_text_recursive(RenderEngine *engine, RenderEngineType *type, bNodeTree *ntree, Text *text)
2392 {
2393         bool found = false;
2394         bNode *node;
2395
2396         ntree->done = true;
2397
2398         /* update each script that is using this text datablock */
2399         for (node = ntree->nodes.first; node; node = node->next) {
2400                 if (node->type == NODE_GROUP) {
2401                         bNodeTree *ngroup = (bNodeTree *)node->id;
2402                         if (ngroup && !ngroup->done)
2403                                 found |= node_shader_script_update_text_recursive(engine, type, ngroup, text);
2404                 }
2405                 else if (node->type == SH_NODE_SCRIPT && node->id == &text->id) {
2406                         type->update_script_node(engine, ntree, node);
2407                         found = true;
2408                 }
2409         }
2410
2411         return found;
2412 }
2413
2414 static int node_shader_script_update_exec(bContext *C, wmOperator *op)
2415 {
2416         Main *bmain = CTX_data_main(C);
2417         Scene *scene = CTX_data_scene(C);
2418         SpaceNode *snode = CTX_wm_space_node(C);
2419         PointerRNA nodeptr = CTX_data_pointer_get_type(C, "node", &RNA_ShaderNodeScript);
2420         bNodeTree *ntree_base = NULL;
2421         bNode *node = NULL;
2422         RenderEngine *engine;
2423         RenderEngineType *type;
2424         bool found = false;
2425
2426         /* setup render engine */
2427         type = RE_engines_find(scene->r.engine);
2428         engine = RE_engine_create(type);
2429         engine->reports = op->reports;
2430
2431         /* get node */
2432         if (nodeptr.data) {
2433                 ntree_base = nodeptr.id.data;
2434                 node = nodeptr.data;
2435         }
2436         else if (snode && snode->edittree) {
2437                 ntree_base = snode->edittree;
2438                 node = nodeGetActive(snode->edittree);
2439         }
2440
2441         if (node) {
2442                 /* update single node */
2443                 type->update_script_node(engine, ntree_base, node);
2444
2445                 found = true;
2446         }
2447         else {
2448                 /* update all nodes using text datablock */
2449                 Text *text = CTX_data_pointer_get_type(C, "edit_text", &RNA_Text).data;
2450
2451                 if (text) {
2452                         /* clear flags for recursion check */
2453                         FOREACH_NODETREE(bmain, ntree, id) {
2454                                 if (ntree->type == NTREE_SHADER)
2455                                         ntree->done = false;
2456                         } FOREACH_NODETREE_END
2457
2458                         FOREACH_NODETREE(bmain, ntree, id) {
2459                                 if (ntree->type == NTREE_SHADER) {
2460                                         if (!ntree->done)
2461                                                 found |= node_shader_script_update_text_recursive(engine, type, ntree, text);
2462                                 }
2463                         } FOREACH_NODETREE_END
2464
2465                         if (!found)
2466                                 BKE_report(op->reports, RPT_INFO, "Text not used by any node, no update done");
2467                 }
2468         }
2469
2470         RE_engine_free(engine);
2471
2472         return (found) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
2473 }
2474
2475 void NODE_OT_shader_script_update(wmOperatorType *ot)
2476 {
2477         /* identifiers */
2478         ot->name = "Script Node Update";
2479         ot->description = "Update shader script node with new sockets and options from the script";
2480         ot->idname = "NODE_OT_shader_script_update";
2481
2482         /* api callbacks */
2483         ot->exec = node_shader_script_update_exec;
2484         ot->poll = node_shader_script_update_poll;
2485
2486         /* flags */
2487         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2488 }
2489
2490 /* ********************** Viewer border ******************/
2491
2492 static void viewer_border_corner_to_backdrop(SpaceNode *snode, ARegion *ar, int x, int y,
2493                                              int backdrop_width, int backdrop_height,
2494                                              float *fx, float *fy)
2495 {
2496         float bufx, bufy;
2497
2498         bufx = backdrop_width * snode->zoom;
2499         bufy = backdrop_height * snode->zoom;
2500
2501         *fx = (bufx > 0.0f ? ((float) x - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
2502         *fy = (bufy > 0.0f ? ((float) y - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
2503 }
2504
2505 static int viewer_border_exec(bContext *C, wmOperator *op)
2506 {
2507         Main *bmain = CTX_data_main(C);
2508         Image *ima;
2509         void *lock;
2510         ImBuf *ibuf;
2511
2512         ED_preview_kill_jobs(CTX_wm_manager(C), bmain);
2513
2514         ima = BKE_image_verify_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
2515         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
2516
2517         if (ibuf) {
2518                 ARegion *ar = CTX_wm_region(C);
2519                 SpaceNode *snode = CTX_wm_space_node(C);
2520                 bNodeTree *btree = snode->nodetree;
2521                 rcti rect;
2522                 rctf rectf;
2523
2524                 /* get border from operator */
2525                 WM_operator_properties_border_to_rcti(op, &rect);
2526
2527                 /* convert border to unified space within backdrop image */
2528                 viewer_border_corner_to_backdrop(snode, ar, rect.xmin, rect.ymin, ibuf->x, ibuf->y,
2529                                                  &rectf.xmin, &rectf.ymin);
2530
2531                 viewer_border_corner_to_backdrop(snode, ar, rect.xmax, rect.ymax, ibuf->x, ibuf->y,
2532                                                  &rectf.xmax, &rectf.ymax);
2533
2534                 /* clamp coordinates */
2535                 rectf.xmin = max_ff(rectf.xmin, 0.0f);
2536                 rectf.ymin = max_ff(rectf.ymin, 0.0f);
2537                 rectf.xmax = min_ff(rectf.xmax, 1.0f);
2538                 rectf.ymax = min_ff(rectf.ymax, 1.0f);
2539
2540                 if (rectf.xmin < rectf.xmax && rectf.ymin < rectf.ymax) {
2541                         btree->viewer_border = rectf;
2542
2543                         if (rectf.xmin == 0.0f && rectf.ymin == 0.0f &&
2544                             rectf.xmax == 1.0f && rectf.ymax == 1.0f)
2545                         {
2546                                 btree->flag &= ~NTREE_VIEWER_BORDER;
2547                         }
2548                         else {
2549                                 btree->flag |= NTREE_VIEWER_BORDER;
2550                         }
2551
2552                         snode_notify(C, snode);
2553                         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
2554                 }
2555                 else {
2556                         btree->flag &= ~NTREE_VIEWER_BORDER;
2557                 }
2558         }
2559
2560         BKE_image_release_ibuf(ima, ibuf, lock);
2561
2562         return OPERATOR_FINISHED;
2563 }
2564
2565 void NODE_OT_viewer_border(wmOperatorType *ot)
2566 {
2567         /* identifiers */
2568         ot->name = "Viewer Border";
2569         ot->description = "Set the boundaries for viewer operations";
2570         ot->idname = "NODE_OT_viewer_border";
2571
2572         /* api callbacks */
2573         ot->invoke = WM_gesture_border_invoke;
2574         ot->exec = viewer_border_exec;
2575         ot->modal = WM_gesture_border_modal;
2576         ot->cancel = WM_gesture_border_cancel;
2577         ot->poll = composite_node_active;
2578
2579         /* flags */
2580         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2581
2582         /* properties */
2583         WM_operator_properties_gesture_border_select(ot);
2584 }
2585
2586 static int clear_viewer_border_exec(bContext *C, wmOperator *UNUSED(op))
2587 {
2588         SpaceNode *snode = CTX_wm_space_node(C);
2589         bNodeTree *btree = snode->nodetree;
2590
2591         btree->flag &= ~NTREE_VIEWER_BORDER;
2592         snode_notify(C, snode);
2593         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
2594
2595         return OPERATOR_FINISHED;
2596 }
2597
2598 void NODE_OT_clear_viewer_border(wmOperatorType *ot)
2599 {
2600         /* identifiers */
2601         ot->name = "Clear Viewer Border";
2602         ot->description = "Clear the boundaries for viewer operations";
2603         ot->idname = "NODE_OT_clear_viewer_border";
2604
2605         /* api callbacks */
2606         ot->exec = clear_viewer_border_exec;
2607         ot->poll = composite_node_active;
2608
2609         /* flags */
2610         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2611 }