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