2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2005 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Nathan Letwory
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/space_node/node_edit.c
39 #include "MEM_guardedalloc.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_node_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_particle_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_world_types.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
54 #include "BKE_context.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_global.h"
57 #include "BKE_image.h"
58 #include "BKE_library.h"
61 #include "BKE_material.h"
62 #include "BKE_modifier.h"
63 #include "BKE_paint.h"
64 #include "BKE_scene.h"
65 #include "BKE_screen.h"
66 #include "BKE_texture.h"
67 #include "BKE_report.h"
69 #include "RE_pipeline.h"
71 #include "IMB_imbuf_types.h"
75 #include "ED_screen.h"
76 #include "ED_space_api.h"
77 #include "ED_render.h"
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81 #include "RNA_enum_types.h"
86 #include "UI_interface.h"
87 #include "UI_resources.h"
88 #include "UI_view2d.h"
90 #include "IMB_imbuf.h"
92 #include "RNA_enum_types.h"
94 #include "GPU_material.h"
96 #include "node_intern.h"
98 static EnumPropertyItem socket_in_out_items[] = {
99 { SOCK_IN, "SOCK_IN", 0, "Input", "" },
100 { SOCK_OUT, "SOCK_OUT", 0, "Output", "" },
101 { 0, NULL, 0, NULL, NULL },
104 /* ***************** composite job manager ********************** */
106 typedef struct CompoJob {
109 bNodeTree *localtree;
115 /* called by compo, only to check job 'stop' value */
116 static int compo_breakjob(void *cjv)
123 /* called by compo, wmJob sends notifier */
124 static void compo_redrawjob(void *cjv, char *UNUSED(str))
131 static void compo_freejob(void *cjv)
136 ntreeLocalMerge(cj->localtree, cj->ntree);
141 /* only now we copy the nodetree, so adding many jobs while
142 sliding buttons doesn't frustrate */
143 static void compo_initjob(void *cjv)
147 cj->localtree= ntreeLocalize(cj->ntree);
150 /* called before redraw notifiers, it moves finished previews over */
151 static void compo_updatejob(void *cjv)
155 ntreeLocalSync(cj->localtree, cj->ntree);
158 static void compo_progressjob(void *cjv, float progress)
162 *(cj->progress) = progress;
166 /* only this runs inside thread */
167 static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
170 bNodeTree *ntree= cj->localtree;
172 if(cj->scene->use_nodes==0)
176 cj->do_update= do_update;
177 cj->progress= progress;
179 ntree->test_break= compo_breakjob;
181 ntree->stats_draw= compo_redrawjob;
183 ntree->progress= compo_progressjob;
186 // XXX BIF_store_spare();
188 ntreeCompositExecTree(ntree, &cj->scene->r, 1); /* 1 is do_previews */
190 ntree->test_break= NULL;
191 ntree->stats_draw= NULL;
192 ntree->progress= NULL;
196 void snode_composite_job(const bContext *C, ScrArea *sa)
198 SpaceNode *snode= sa->spacedata.first;
202 steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Compositing", WM_JOB_EXCL_RENDER|WM_JOB_PROGRESS);
203 cj= MEM_callocN(sizeof(CompoJob), "compo job");
205 /* customdata for preview thread */
206 cj->scene= CTX_data_scene(C);
207 cj->ntree= snode->nodetree;
210 WM_jobs_customdata(steve, cj, compo_freejob);
211 WM_jobs_timer(steve, 0.1, NC_SCENE, NC_SCENE|ND_COMPO_RESULT);
212 WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob, NULL);
214 WM_jobs_start(CTX_wm_manager(C), steve);
218 /* ***************************************** */
220 /* operator poll callback */
221 static int composite_node_active(bContext *C)
223 if( ED_operator_node_active(C)) {
224 SpaceNode *snode= CTX_wm_space_node(C);
225 if(snode->treetype==NTREE_COMPOSIT)
231 /* also checks for edited groups */
232 static bNode *editnode_get_active(bNodeTree *ntree)
236 /* check for edited group */
237 for(node= ntree->nodes.first; node; node= node->next)
238 if(nodeGroupEditGet(node))
241 return nodeGetActive((bNodeTree *)node->id);
243 return nodeGetActive(ntree);
246 void snode_dag_update(bContext *UNUSED(C), SpaceNode *snode)
248 DAG_id_tag_update(snode->id, 0);
251 void snode_notify(bContext *C, SpaceNode *snode)
253 WM_event_add_notifier(C, NC_NODE|NA_EDITED, NULL);
255 if(snode->treetype==NTREE_SHADER)
256 WM_event_add_notifier(C, NC_MATERIAL|ND_NODES, snode->id);
257 else if(snode->treetype==NTREE_COMPOSIT)
258 WM_event_add_notifier(C, NC_SCENE|ND_NODES, snode->id);
259 else if(snode->treetype==NTREE_TEXTURE)
260 WM_event_add_notifier(C, NC_TEXTURE|ND_NODES, snode->id);
263 bNode *node_tree_get_editgroup(bNodeTree *nodetree)
267 /* get the groupnode */
268 for(gnode= nodetree->nodes.first; gnode; gnode= gnode->next)
269 if(nodeGroupEditGet(gnode))
274 /* assumes nothing being done in ntree yet, sets the default in/out node */
275 /* called from shading buttons or header */
276 void ED_node_shader_default(Scene *scene, ID *id)
279 bNodeSocket *fromsock, *tosock, *sock;
282 int output_type, shader_type;
283 float color[3], strength = 1.0f;
285 ntree= ntreeAddTree("Shader Nodetree", NTREE_SHADER, 0);
287 switch(GS(id->name)) {
289 Material *ma= (Material*)id;
290 ma->nodetree = ntree;
292 if(scene_use_new_shading_nodes(scene)) {
293 output_type = SH_NODE_OUTPUT_MATERIAL;
294 shader_type = SH_NODE_BSDF_DIFFUSE;
297 output_type = SH_NODE_OUTPUT;
298 shader_type = SH_NODE_MATERIAL;
301 copy_v3_v3(color, &ma->r);
306 World *wo= (World*)id;
307 wo->nodetree = ntree;
309 output_type = SH_NODE_OUTPUT_WORLD;
310 shader_type = SH_NODE_BACKGROUND;
312 copy_v3_v3(color, &wo->horr);
318 la->nodetree = ntree;
320 output_type = SH_NODE_OUTPUT_LAMP;
321 shader_type = SH_NODE_EMISSION;
323 copy_v3_v3(color, &la->r);
324 if(la->type == LA_LOCAL || la->type == LA_SPOT || la->type == LA_AREA)
331 printf("ED_node_shader_default called on wrong ID type.\n");
335 ntemp.type = output_type;
336 out= nodeAddNode(ntree, &ntemp);
337 out->locx= 300.0f; out->locy= 300.0f;
339 ntemp.type = shader_type;
340 in= nodeAddNode(ntree, &ntemp);
341 in->locx= 10.0f; in->locy= 300.0f;
342 nodeSetActive(ntree, in);
344 /* only a link from color to color */
345 fromsock= in->outputs.first;
346 tosock= out->inputs.first;
347 nodeAddLink(ntree, in, fromsock, out, tosock);
350 if(scene_use_new_shading_nodes(scene)) {
351 sock= in->inputs.first;
352 copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, color);
354 if(strength != 0.0f) {
355 sock= in->inputs.last;
356 ((bNodeSocketValueFloat*)sock->default_value)->value= strength;
360 ntreeUpdateTree(ntree);
363 /* assumes nothing being done in ntree yet, sets the default in/out node */
364 /* called from shading buttons or header */
365 void ED_node_composit_default(Scene *sce)
368 bNodeSocket *fromsock, *tosock;
371 /* but lets check it anyway */
374 printf("error in composite initialize\n");
378 sce->nodetree= ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, 0);
380 ntemp.type = CMP_NODE_COMPOSITE;
381 out= nodeAddNode(sce->nodetree, &ntemp);
382 out->locx= 300.0f; out->locy= 400.0f;
386 ntemp.type = CMP_NODE_R_LAYERS;
387 in= nodeAddNode(sce->nodetree, &ntemp);
388 in->locx= 10.0f; in->locy= 400.0f;
391 nodeSetActive(sce->nodetree, in);
393 /* links from color to color */
394 fromsock= in->outputs.first;
395 tosock= out->inputs.first;
396 nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
398 ntreeUpdateTree(sce->nodetree);
400 // XXX ntreeCompositForceHidden(sce->nodetree);
403 /* assumes nothing being done in ntree yet, sets the default in/out node */
404 /* called from shading buttons or header */
405 void ED_node_texture_default(Tex *tx)
408 bNodeSocket *fromsock, *tosock;
411 /* but lets check it anyway */
414 printf("error in texture initialize\n");
418 tx->nodetree= ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, 0);
420 ntemp.type = TEX_NODE_OUTPUT;
421 out= nodeAddNode(tx->nodetree, &ntemp);
422 out->locx= 300.0f; out->locy= 300.0f;
424 ntemp.type = TEX_NODE_CHECKER;
425 in= nodeAddNode(tx->nodetree, &ntemp);
426 in->locx= 10.0f; in->locy= 300.0f;
427 nodeSetActive(tx->nodetree, in);
429 fromsock= in->outputs.first;
430 tosock= out->inputs.first;
431 nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
433 ntreeUpdateTree(tx->nodetree);
436 /* id is supposed to contain a node tree */
437 void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
441 short idtype= GS(id->name);
443 if(idtype == ID_NT) {
444 *ntree= (bNodeTree*)id;
445 if(treetype) *treetype= (*ntree)->type;
447 else if(idtype == ID_MA) {
448 *ntree= ((Material*)id)->nodetree;
449 if(treetype) *treetype= NTREE_SHADER;
451 else if(idtype == ID_LA) {
452 *ntree= ((Lamp*)id)->nodetree;
453 if(treetype) *treetype= NTREE_SHADER;
455 else if(idtype == ID_WO) {
456 *ntree= ((World*)id)->nodetree;
457 if(treetype) *treetype= NTREE_SHADER;
459 else if(idtype == ID_SCE) {
460 *ntree= ((Scene*)id)->nodetree;
461 if(treetype) *treetype= NTREE_COMPOSIT;
463 else if(idtype == ID_TE) {
464 *ntree= ((Tex*)id)->nodetree;
465 if(treetype) *treetype= NTREE_TEXTURE;
468 if(treetype) *treetype= 0;
472 /* find editable group */
475 for(node= (*ntree)->nodes.first; node; node= node->next)
476 if(nodeGroupEditGet(node))
480 *edittree= (bNodeTree *)node->id;
488 if(treetype) *treetype= 0;
492 /* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
493 void snode_set_context(SpaceNode *snode, Scene *scene)
497 snode->id= snode->from= NULL;
499 if(snode->treetype==NTREE_SHADER) {
500 /* need active object, or we allow pinning... */
501 if(snode->shaderfrom == SNODE_SHADER_OBJECT) {
503 if(ob->type == OB_LAMP) {
504 snode->from= &ob->id;
508 Material *ma= give_current_material(ob, ob->actcol);
510 snode->from= &ob->id;
516 else { /* SNODE_SHADER_WORLD */
519 snode->id= &scene->world->id;
523 else if(snode->treetype==NTREE_COMPOSIT) {
524 snode->id= &scene->id;
526 /* update output sockets based on available layers */
527 ntreeCompositForceHidden(scene->nodetree, scene);
529 else if(snode->treetype==NTREE_TEXTURE) {
532 if(snode->texfrom==SNODE_TEX_OBJECT) {
534 tx= give_current_object_texture(ob);
536 if(ob->type == OB_LAMP)
537 snode->from= (ID*)ob->data;
539 snode->from= (ID*)give_current_material(ob, ob->actcol);
541 /* from is not set fully for material nodes, should be ID + Node then */
545 else if(snode->texfrom==SNODE_TEX_WORLD) {
546 tx= give_current_world_texture(scene->world);
547 snode->from= (ID *)scene->world;
551 struct Brush *brush= NULL;
553 if(ob && (ob->mode & OB_MODE_SCULPT))
554 brush= paint_brush(&scene->toolsettings->sculpt->paint);
556 brush= paint_brush(&scene->toolsettings->imapaint.paint);
559 snode->from= (ID *)brush;
560 tx= give_current_brush_texture(brush);
566 if (snode->nodetree && snode->nodetree->type == snode->treetype)
567 snode->id = &snode->nodetree->id;
572 node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
575 static void snode_update(SpaceNode *snode, bNode *node)
580 nodeUpdate(snode->edittree, node);
582 /* if inside group, tag entire group */
583 gnode= node_tree_get_editgroup(snode->nodetree);
585 nodeUpdateID(snode->nodetree, gnode->id);
588 static int has_nodetree(bNodeTree *ntree, bNodeTree *lookup)
595 for(node=ntree->nodes.first; node; node=node->next)
596 if(node->type == NODE_GROUP && node->id)
597 if(has_nodetree((bNodeTree*)node->id, lookup))
603 void ED_node_set_active(Main *bmain, bNodeTree *ntree, bNode *node)
605 int was_active_texture = (node->flag & NODE_ACTIVE_TEXTURE);
607 nodeSetActive(ntree, node);
609 if(node->type!=NODE_GROUP) {
610 int was_output= (node->flag & NODE_DO_OUTPUT);
612 /* tree specific activate calls */
613 if(ntree->type==NTREE_SHADER) {
614 /* when we select a material, active texture is cleared, for buttons */
615 if(node->id && ELEM3(GS(node->id->name), ID_MA, ID_LA, ID_WO))
616 nodeClearActiveID(ntree, ID_TE);
618 if(node->type==SH_NODE_OUTPUT) {
621 for(tnode= ntree->nodes.first; tnode; tnode= tnode->next)
622 if( tnode->type==SH_NODE_OUTPUT)
623 tnode->flag &= ~NODE_DO_OUTPUT;
625 node->flag |= NODE_DO_OUTPUT;
627 ED_node_generic_update(bmain, ntree, node);
630 /* if active texture changed, free glsl materials */
631 if((node->flag & NODE_ACTIVE_TEXTURE) && !was_active_texture) {
634 for(ma=bmain->mat.first; ma; ma=ma->id.next)
635 if(ma->nodetree && ma->use_nodes && has_nodetree(ma->nodetree, ntree))
636 GPU_material_free(ma);
639 WM_main_add_notifier(NC_MATERIAL|ND_NODES, node->id);
641 else if(ntree->type==NTREE_COMPOSIT) {
642 /* make active viewer, currently only 1 supported... */
643 if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
647 for(tnode= ntree->nodes.first; tnode; tnode= tnode->next)
648 if( ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
649 tnode->flag &= ~NODE_DO_OUTPUT;
651 node->flag |= NODE_DO_OUTPUT;
653 ED_node_generic_update(bmain, ntree, node);
655 /* addnode() doesnt link this yet... */
656 node->id= (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
658 else if(node->type==CMP_NODE_R_LAYERS) {
661 for(scene=bmain->scene.first; scene; scene=scene->id.next) {
662 if(scene->nodetree && scene->use_nodes && has_nodetree(scene->nodetree, ntree)) {
663 if(node->id==NULL || node->id==(ID *)scene) {
664 scene->r.actlay= node->custom1;
669 else if(node->type==CMP_NODE_COMPOSITE) {
673 for(tnode= ntree->nodes.first; tnode; tnode= tnode->next)
674 if( tnode->type==CMP_NODE_COMPOSITE)
675 tnode->flag &= ~NODE_DO_OUTPUT;
677 node->flag |= NODE_DO_OUTPUT;
678 ED_node_generic_update(bmain, ntree, node);
682 else if(ntree->type==NTREE_TEXTURE) {
686 ; // XXX BIF_preview_changed(-1);
687 // allqueue(REDRAWBUTSSHADING, 1);
688 // allqueue(REDRAWIPO, 0);
694 static int inside_rctf(rctf *bounds, rctf *rect)
696 return (bounds->xmin <= rect->xmin && bounds->xmax >= rect->xmax
697 && bounds->ymin <= rect->ymin && bounds->ymax >= rect->ymax);
700 static void node_frame_attach_nodes(bNodeTree *UNUSED(ntree), bNode *frame)
704 /* only check nodes on top of the frame for attaching */
705 for (node=frame->next; node; node=node->next) {
706 if (node->parent==frame) {
707 /* detach nodes that went outside the frame */
708 if (!inside_rctf(&frame->totr, &node->totr))
709 nodeDetachNode(node);
711 else if (node->flag & NODE_SELECT && node->parent==NULL) {
712 /* attach selected, still unparented nodes */
713 if (inside_rctf(&frame->totr, &node->totr))
714 nodeAttachNode(node, frame);
719 void ED_node_update_hierarchy(bContext *UNUSED(C), bNodeTree *ntree)
723 /* XXX This does not work due to layout functions relying on node->block,
724 * which only exists during actual drawing. Can we rely on valid totr rects?
726 /* make sure nodes have correct bounding boxes after transform */
727 // node_update_nodetree(C, ntree, 0.0f, 0.0f);
729 /* all selected nodes are re-parented */
730 for (node=ntree->nodes.last; node; node=node->prev) {
731 if (node->flag & NODE_SELECT && node->parent)
732 nodeDetachNode(node);
735 /* update higher Z-level nodes first */
736 for (node=ntree->nodes.last; node; node=node->prev) {
738 if (node->type==NODE_FRAME)
739 node_frame_attach_nodes(ntree, node);
743 /* ***************** generic operator functions for nodes ***************** */
747 static int edit_node_poll(bContext *C)
749 return ED_operator_node_active(C);
752 static void edit_node_properties(wmOperatorType *ot)
754 /* XXX could node be a context pointer? */
755 RNA_def_string(ot->srna, "node", "", MAX_NAME, "Node", "");
756 RNA_def_int(ot->srna, "socket", 0, 0, MAX_SOCKET, "Socket", "", 0, MAX_SOCKET);
757 RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Side", "");
760 static int edit_node_invoke_properties(bContext *C, wmOperator *op)
762 if (!RNA_struct_property_is_set(op->ptr, "node")) {
763 bNode *node= CTX_data_pointer_get_type(C, "node", &RNA_Node).data;
767 RNA_string_set(op->ptr, "node", node->name);
770 if (!RNA_struct_property_is_set(op->ptr, "in_out"))
771 RNA_enum_set(op->ptr, "in_out", SOCK_IN);
773 if (!RNA_struct_property_is_set(op->ptr, "socket"))
774 RNA_int_set(op->ptr, "socket", 0);
779 static void edit_node_properties_get(wmOperator *op, bNodeTree *ntree, bNode **rnode, bNodeSocket **rsock, int *rin_out)
782 bNodeSocket *sock=NULL;
783 char nodename[MAX_NAME];
787 RNA_string_get(op->ptr, "node", nodename);
788 node = nodeFindNodebyName(ntree, nodename);
790 in_out = RNA_enum_get(op->ptr, "in_out");
792 sockindex = RNA_int_get(op->ptr, "socket");
794 case SOCK_IN: sock = BLI_findlink(&node->inputs, sockindex); break;
795 case SOCK_OUT: sock = BLI_findlink(&node->outputs, sockindex); break;
807 /* ***************** Edit Group operator ************* */
809 void snode_make_group_editable(SpaceNode *snode, bNode *gnode)
813 /* make sure nothing has group editing on */
814 for(node=snode->nodetree->nodes.first; node; node=node->next)
815 nodeGroupEditClear(node);
818 /* with NULL argument we do a toggle */
819 if(snode->edittree==snode->nodetree)
820 gnode= nodeGetActive(snode->nodetree);
824 snode->edittree = nodeGroupEditSet(gnode, 1);
826 /* deselect all other nodes, so we can also do grabbing of entire subtree */
827 for(node= snode->nodetree->nodes.first; node; node= node->next)
828 node->flag &= ~SELECT;
829 gnode->flag |= SELECT;
832 snode->edittree= snode->nodetree;
835 static int node_group_edit_exec(bContext *C, wmOperator *UNUSED(op))
837 SpaceNode *snode = CTX_wm_space_node(C);
839 ED_preview_kill_jobs(C);
841 if (snode->nodetree==snode->edittree) {
842 bNode *gnode = nodeGetActive(snode->edittree);
843 snode_make_group_editable(snode, gnode);
846 snode_make_group_editable(snode, NULL);
848 WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
850 return OPERATOR_FINISHED;
853 static int node_group_edit_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
855 SpaceNode *snode = CTX_wm_space_node(C);
859 if (snode->nodetree==snode->edittree) {
860 gnode = nodeGetActive(snode->edittree);
861 if(gnode && gnode->id && GS(gnode->id->name)==ID_NT && gnode->id->lib) {
862 uiPupMenuOkee(C, op->type->idname, "Make group local?");
863 return OPERATOR_CANCELLED;
867 return node_group_edit_exec(C, op);
870 void NODE_OT_group_edit(wmOperatorType *ot)
873 ot->name = "Edit Group";
874 ot->description = "Edit node group";
875 ot->idname = "NODE_OT_group_edit";
878 ot->invoke = node_group_edit_invoke;
879 ot->exec = node_group_edit_exec;
880 ot->poll = ED_operator_node_active;
883 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
886 /* ***************** Add Group Socket operator ************* */
888 static int node_group_socket_add_exec(bContext *C, wmOperator *op)
890 SpaceNode *snode = CTX_wm_space_node(C);
892 char name[MAX_NAME]= "";
893 int type= SOCK_FLOAT;
894 bNodeTree *ngroup= snode->edittree;
895 /* bNodeSocket *sock; */ /* UNUSED */
897 ED_preview_kill_jobs(C);
899 if (RNA_struct_property_is_set(op->ptr, "name"))
900 RNA_string_get(op->ptr, "name", name);
902 if (RNA_struct_property_is_set(op->ptr, "type"))
903 type = RNA_enum_get(op->ptr, "type");
905 if (RNA_struct_property_is_set(op->ptr, "in_out"))
906 in_out = RNA_enum_get(op->ptr, "in_out");
908 return OPERATOR_CANCELLED;
910 /* using placeholder subtype first */
911 /* sock = */ /* UNUSED */ node_group_add_socket(ngroup, name, type, in_out);
913 ntreeUpdateTree(ngroup);
915 snode_notify(C, snode);
917 return OPERATOR_FINISHED;
920 void NODE_OT_group_socket_add(wmOperatorType *ot)
923 ot->name = "Add Group Socket";
924 ot->description = "Add node group socket";
925 ot->idname = "NODE_OT_group_socket_add";
928 ot->exec = node_group_socket_add_exec;
929 ot->poll = ED_operator_node_active;
932 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
934 RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
935 RNA_def_string(ot->srna, "name", "", MAX_NAME, "Name", "Group socket name");
936 RNA_def_enum(ot->srna, "type", node_socket_type_items, SOCK_FLOAT, "Type", "Type of the group socket");
939 /* ***************** Remove Group Socket operator ************* */
941 static int node_group_socket_remove_exec(bContext *C, wmOperator *op)
943 SpaceNode *snode = CTX_wm_space_node(C);
946 bNodeTree *ngroup= snode->edittree;
949 ED_preview_kill_jobs(C);
951 if (RNA_struct_property_is_set(op->ptr, "index"))
952 index = RNA_int_get(op->ptr, "index");
954 return OPERATOR_CANCELLED;
956 if (RNA_struct_property_is_set(op->ptr, "in_out"))
957 in_out = RNA_enum_get(op->ptr, "in_out");
959 return OPERATOR_CANCELLED;
961 sock = (bNodeSocket*)BLI_findlink(in_out==SOCK_IN ? &ngroup->inputs : &ngroup->outputs, index);
963 node_group_remove_socket(ngroup, sock, in_out);
964 ntreeUpdateTree(ngroup);
966 snode_notify(C, snode);
969 return OPERATOR_FINISHED;
972 void NODE_OT_group_socket_remove(wmOperatorType *ot)
975 ot->name = "Remove Group Socket";
976 ot->description = "Remove a node group socket";
977 ot->idname = "NODE_OT_group_socket_remove";
980 ot->exec = node_group_socket_remove_exec;
981 ot->poll = ED_operator_node_active;
984 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
986 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
987 RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
990 /* ***************** Move Group Socket Up operator ************* */
992 static int node_group_socket_move_up_exec(bContext *C, wmOperator *op)
994 SpaceNode *snode = CTX_wm_space_node(C);
997 bNodeTree *ngroup= snode->edittree;
998 bNodeSocket *sock, *prev;
1000 ED_preview_kill_jobs(C);
1002 if (RNA_struct_property_is_set(op->ptr, "index"))
1003 index = RNA_int_get(op->ptr, "index");
1005 return OPERATOR_CANCELLED;
1007 if (RNA_struct_property_is_set(op->ptr, "in_out"))
1008 in_out = RNA_enum_get(op->ptr, "in_out");
1010 return OPERATOR_CANCELLED;
1013 if (in_out==SOCK_IN) {
1014 sock = (bNodeSocket*)BLI_findlink(&ngroup->inputs, index);
1016 /* can't move up the first socket */
1018 return OPERATOR_CANCELLED;
1019 BLI_remlink(&ngroup->inputs, sock);
1020 BLI_insertlinkbefore(&ngroup->inputs, prev, sock);
1022 ngroup->update |= NTREE_UPDATE_GROUP_IN;
1024 else if (in_out==SOCK_OUT) {
1025 sock = (bNodeSocket*)BLI_findlink(&ngroup->outputs, index);
1027 /* can't move up the first socket */
1029 return OPERATOR_CANCELLED;
1030 BLI_remlink(&ngroup->outputs, sock);
1031 BLI_insertlinkbefore(&ngroup->outputs, prev, sock);
1033 ngroup->update |= NTREE_UPDATE_GROUP_OUT;
1035 ntreeUpdateTree(ngroup);
1037 snode_notify(C, snode);
1039 return OPERATOR_FINISHED;
1042 void NODE_OT_group_socket_move_up(wmOperatorType *ot)
1045 ot->name = "Move Group Socket Up";
1046 ot->description = "Move up node group socket";
1047 ot->idname = "NODE_OT_group_socket_move_up";
1050 ot->exec = node_group_socket_move_up_exec;
1051 ot->poll = ED_operator_node_active;
1054 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1056 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
1057 RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
1060 /* ***************** Move Group Socket Up operator ************* */
1062 static int node_group_socket_move_down_exec(bContext *C, wmOperator *op)
1064 SpaceNode *snode = CTX_wm_space_node(C);
1067 bNodeTree *ngroup= snode->edittree;
1068 bNodeSocket *sock, *next;
1070 ED_preview_kill_jobs(C);
1072 if (RNA_struct_property_is_set(op->ptr, "index"))
1073 index = RNA_int_get(op->ptr, "index");
1075 return OPERATOR_CANCELLED;
1077 if (RNA_struct_property_is_set(op->ptr, "in_out"))
1078 in_out = RNA_enum_get(op->ptr, "in_out");
1080 return OPERATOR_CANCELLED;
1083 if (in_out==SOCK_IN) {
1084 sock = (bNodeSocket*)BLI_findlink(&ngroup->inputs, index);
1086 /* can't move down the last socket */
1088 return OPERATOR_CANCELLED;
1089 BLI_remlink(&ngroup->inputs, sock);
1090 BLI_insertlinkafter(&ngroup->inputs, next, sock);
1092 ngroup->update |= NTREE_UPDATE_GROUP_IN;
1094 else if (in_out==SOCK_OUT) {
1095 sock = (bNodeSocket*)BLI_findlink(&ngroup->outputs, index);
1097 /* can't move down the last socket */
1099 return OPERATOR_CANCELLED;
1100 BLI_remlink(&ngroup->outputs, sock);
1101 BLI_insertlinkafter(&ngroup->outputs, next, sock);
1103 ngroup->update |= NTREE_UPDATE_GROUP_OUT;
1105 ntreeUpdateTree(ngroup);
1107 snode_notify(C, snode);
1109 return OPERATOR_FINISHED;
1112 void NODE_OT_group_socket_move_down(wmOperatorType *ot)
1115 ot->name = "Move Group Socket Down";
1116 ot->description = "Move down node group socket";
1117 ot->idname = "NODE_OT_group_socket_move_down";
1120 ot->exec = node_group_socket_move_down_exec;
1121 ot->poll = ED_operator_node_active;
1124 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1126 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
1127 RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
1130 /* ******************** Ungroup operator ********************** */
1132 static int node_group_ungroup_exec(bContext *C, wmOperator *op)
1134 SpaceNode *snode = CTX_wm_space_node(C);
1137 ED_preview_kill_jobs(C);
1139 /* are we inside of a group? */
1140 gnode= node_tree_get_editgroup(snode->nodetree);
1142 snode_make_group_editable(snode, NULL);
1144 gnode= nodeGetActive(snode->edittree);
1146 return OPERATOR_CANCELLED;
1148 if(gnode->type!=NODE_GROUP) {
1149 BKE_report(op->reports, RPT_WARNING, "Not a group");
1150 return OPERATOR_CANCELLED;
1152 else if(!node_group_ungroup(snode->edittree, gnode)) {
1153 BKE_report(op->reports, RPT_WARNING, "Can't ungroup");
1154 return OPERATOR_CANCELLED;
1157 snode_notify(C, snode);
1158 snode_dag_update(C, snode);
1160 return OPERATOR_FINISHED;
1163 void NODE_OT_group_ungroup(wmOperatorType *ot)
1166 ot->name = "Ungroup";
1167 ot->description = "Ungroup selected nodes";
1168 ot->idname = "NODE_OT_group_ungroup";
1171 ot->exec = node_group_ungroup_exec;
1172 ot->poll = ED_operator_node_active;
1175 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1178 /* ************************** Node generic ************** */
1180 /* is rct in visible part of node? */
1181 static bNode *visible_node(SpaceNode *snode, rctf *rct)
1185 for(node=snode->edittree->nodes.last; node; node=node->prev) {
1186 if(BLI_isect_rctf(&node->totr, rct, NULL))
1192 /* **************************** */
1194 typedef struct NodeViewMove {
1196 int xmin, ymin, xmax, ymax;
1199 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1201 SpaceNode *snode= CTX_wm_space_node(C);
1202 ARegion *ar= CTX_wm_region(C);
1203 NodeViewMove *nvm= op->customdata;
1205 switch (event->type) {
1208 snode->xof -= (nvm->mvalo[0]-event->mval[0]);
1209 snode->yof -= (nvm->mvalo[1]-event->mval[1]);
1210 nvm->mvalo[0]= event->mval[0];
1211 nvm->mvalo[1]= event->mval[1];
1213 /* prevent dragging image outside of the window and losing it! */
1214 CLAMP(snode->xof, nvm->xmin, nvm->xmax);
1215 CLAMP(snode->yof, nvm->ymin, nvm->ymax);
1217 ED_region_tag_redraw(ar);
1226 op->customdata= NULL;
1228 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_NODE, NULL);
1230 return OPERATOR_FINISHED;
1233 return OPERATOR_RUNNING_MODAL;
1236 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1238 ARegion *ar= CTX_wm_region(C);
1245 ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
1246 ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
1249 BKE_image_release_ibuf(ima, lock);
1250 return OPERATOR_CANCELLED;
1253 nvm= MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
1254 op->customdata= nvm;
1255 nvm->mvalo[0]= event->mval[0];
1256 nvm->mvalo[1]= event->mval[1];
1258 nvm->xmin = -(ar->winx/2) - ibuf->x/2 + pad;
1259 nvm->xmax = ar->winx/2 + ibuf->x/2 - pad;
1260 nvm->ymin = -(ar->winy/2) - ibuf->y/2 + pad;
1261 nvm->ymax = ar->winy/2 + ibuf->y/2 - pad;
1263 BKE_image_release_ibuf(ima, lock);
1265 /* add modal handler */
1266 WM_event_add_modal_handler(C, op);
1268 return OPERATOR_RUNNING_MODAL;
1271 static int snode_bg_viewmove_cancel(bContext *UNUSED(C), wmOperator *op)
1273 MEM_freeN(op->customdata);
1274 op->customdata= NULL;
1276 return OPERATOR_CANCELLED;
1279 void NODE_OT_backimage_move(wmOperatorType *ot)
1282 ot->name= "Background Image Move";
1283 ot->description = "Move Node backdrop";
1284 ot->idname= "NODE_OT_backimage_move";
1287 ot->invoke= snode_bg_viewmove_invoke;
1288 ot->modal= snode_bg_viewmove_modal;
1289 ot->poll= composite_node_active;
1290 ot->cancel= snode_bg_viewmove_cancel;
1293 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1296 static int backimage_zoom(bContext *C, wmOperator *op)
1298 SpaceNode *snode= CTX_wm_space_node(C);
1299 ARegion *ar= CTX_wm_region(C);
1300 float fac= RNA_float_get(op->ptr, "factor");
1303 ED_region_tag_redraw(ar);
1305 return OPERATOR_FINISHED;
1309 void NODE_OT_backimage_zoom(wmOperatorType *ot)
1313 ot->name= "Background Image Zoom";
1314 ot->idname= "NODE_OT_backimage_zoom";
1317 ot->exec= backimage_zoom;
1318 ot->poll= composite_node_active;
1321 ot->flag= OPTYPE_BLOCKING;
1324 RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
1327 /******************** sample backdrop operator ********************/
1329 typedef struct ImageSampleInfo {
1336 unsigned char col[4];
1342 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
1344 Scene *scene = CTX_data_scene(C);
1345 ImageSampleInfo *info= arg_info;
1347 ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
1348 info->x, info->y, info->col, info->colf,
1349 NULL, NULL /* zbuf - unused for nodes */
1353 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1355 SpaceNode *snode= CTX_wm_space_node(C);
1356 ARegion *ar= CTX_wm_region(C);
1357 ImageSampleInfo *info= op->customdata;
1361 float fx, fy, bufx, bufy;
1363 ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
1364 ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
1369 if(info->color_manage)
1370 ibuf->profile = IB_PROFILE_LINEAR_RGB;
1372 ibuf->profile = IB_PROFILE_NONE;
1373 IMB_rect_from_float(ibuf);
1376 /* map the mouse coords to the backdrop image space */
1377 bufx = ibuf->x * snode->zoom;
1378 bufy = ibuf->y * snode->zoom;
1379 fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f*ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
1380 fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f*ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
1382 if(fx>=0.0f && fy>=0.0f && fx<1.0f && fy<1.0f) {
1385 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1387 CLAMP(x, 0, ibuf->x-1);
1388 CLAMP(y, 0, ibuf->y-1);
1393 info->channels= ibuf->channels;
1396 cp= (char *)(ibuf->rect + y*ibuf->x + x);
1398 info->col[0]= cp[0];
1399 info->col[1]= cp[1];
1400 info->col[2]= cp[2];
1401 info->col[3]= cp[3];
1403 info->colf[0]= (float)cp[0]/255.0f;
1404 info->colf[1]= (float)cp[1]/255.0f;
1405 info->colf[2]= (float)cp[2]/255.0f;
1406 info->colf[3]= (float)cp[3]/255.0f;
1408 if(ibuf->rect_float) {
1409 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1411 info->colf[0]= fp[0];
1412 info->colf[1]= fp[1];
1413 info->colf[2]= fp[2];
1414 info->colf[3]= fp[3];
1420 BKE_image_release_ibuf(ima, lock);
1422 ED_area_tag_redraw(CTX_wm_area(C));
1425 static void sample_exit(bContext *C, wmOperator *op)
1427 ImageSampleInfo *info= op->customdata;
1429 ED_region_draw_cb_exit(info->art, info->draw_handle);
1430 ED_area_tag_redraw(CTX_wm_area(C));
1434 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1436 SpaceNode *snode= CTX_wm_space_node(C);
1437 ARegion *ar= CTX_wm_region(C);
1438 ImageSampleInfo *info;
1440 if(snode->treetype!=NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
1441 return OPERATOR_CANCELLED;
1443 info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1444 info->art= ar->type;
1445 info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1446 op->customdata= info;
1448 sample_apply(C, op, event);
1450 WM_event_add_modal_handler(C, op);
1452 return OPERATOR_RUNNING_MODAL;
1455 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1457 switch(event->type) {
1459 case RIGHTMOUSE: // XXX hardcoded
1461 return OPERATOR_CANCELLED;
1463 sample_apply(C, op, event);
1467 return OPERATOR_RUNNING_MODAL;
1470 static int sample_cancel(bContext *C, wmOperator *op)
1473 return OPERATOR_CANCELLED;
1476 void NODE_OT_backimage_sample(wmOperatorType *ot)
1479 ot->name= "Backimage Sample";
1480 ot->idname= "NODE_OT_backimage_sample";
1483 ot->invoke= sample_invoke;
1484 ot->modal= sample_modal;
1485 ot->cancel= sample_cancel;
1486 ot->poll= ED_operator_node_active;
1489 ot->flag= OPTYPE_BLOCKING;
1492 /* ********************** size widget operator ******************** */
1494 typedef struct NodeSizeWidget {
1495 float mxstart, mystart;
1496 float oldwidth, oldheight;
1500 static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
1502 SpaceNode *snode= CTX_wm_space_node(C);
1503 ARegion *ar= CTX_wm_region(C);
1504 bNode *node= editnode_get_active(snode->edittree);
1505 NodeSizeWidget *nsw= op->customdata;
1508 switch (event->type) {
1511 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
1515 if(node->flag & NODE_HIDDEN) {
1516 node->miniwidth= nsw->oldminiwidth + mx - nsw->mxstart;
1517 CLAMP(node->miniwidth, 0.0f, 100.0f);
1520 node->width= nsw->oldwidth + mx - nsw->mxstart;
1521 CLAMP(node->width, UI_DPI_FAC*node->typeinfo->minwidth, UI_DPI_FAC*node->typeinfo->maxwidth);
1523 /* height works the other way round ... */
1524 node->height= nsw->oldheight - my + nsw->mystart;
1525 CLAMP(node->height, node->typeinfo->minheight, node->typeinfo->maxheight);
1528 ED_region_tag_redraw(ar);
1537 op->customdata= NULL;
1539 ED_node_update_hierarchy(C, snode->edittree);
1541 return OPERATOR_FINISHED;
1544 return OPERATOR_RUNNING_MODAL;
1547 static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
1549 SpaceNode *snode= CTX_wm_space_node(C);
1550 ARegion *ar= CTX_wm_region(C);
1551 bNode *node= editnode_get_active(snode->edittree);
1554 /* convert mouse coordinates to v2d space */
1555 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
1556 &snode->mx, &snode->my);
1558 if(node->typeinfo->resize_area_func(node, snode->mx, snode->my)) {
1559 NodeSizeWidget *nsw= MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
1561 op->customdata= nsw;
1562 nsw->mxstart= snode->mx;
1563 nsw->mystart= snode->my;
1566 nsw->oldwidth= node->width;
1567 nsw->oldheight= node->height;
1568 nsw->oldminiwidth= node->miniwidth;
1570 /* add modal handler */
1571 WM_event_add_modal_handler(C, op);
1573 return OPERATOR_RUNNING_MODAL;
1576 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1579 static int node_resize_cancel(bContext *UNUSED(C), wmOperator *op)
1581 MEM_freeN(op->customdata);
1582 op->customdata= NULL;
1584 return OPERATOR_CANCELLED;
1587 void NODE_OT_resize(wmOperatorType *ot)
1590 ot->name= "Resize Node";
1591 ot->idname= "NODE_OT_resize";
1594 ot->invoke= node_resize_invoke;
1595 ot->modal= node_resize_modal;
1596 ot->poll= ED_operator_node_active;
1597 ot->cancel= node_resize_cancel;
1600 ot->flag= OPTYPE_BLOCKING;
1604 /* ********************** hidden sockets ******************** */
1606 int node_has_hidden_sockets(bNode *node, short flag)
1610 for(sock= node->inputs.first; sock; sock= sock->next)
1611 if(sock->flag & flag)
1613 for(sock= node->outputs.first; sock; sock= sock->next)
1614 if(sock->flag & flag)
1619 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, short flag, int set)
1624 for(sock= node->inputs.first; sock; sock= sock->next)
1625 sock->flag &= ~flag;
1626 for(sock= node->outputs.first; sock; sock= sock->next)
1627 sock->flag &= ~flag;
1630 /* hide unused sockets */
1631 for(sock= node->inputs.first; sock; sock= sock->next) {
1632 if(sock->link==NULL)
1635 for(sock= node->outputs.first; sock; sock= sock->next) {
1636 if(nodeCountSocketLinks(snode->edittree, sock)==0)
1642 static void node_link_viewer(SpaceNode *snode, bNode *tonode)
1647 if(tonode==NULL || tonode->outputs.first==NULL)
1649 if( ELEM(tonode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1653 for(node= snode->edittree->nodes.first; node; node= node->next)
1654 if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1655 if(node->flag & NODE_DO_OUTPUT)
1657 /* no viewer, we make one active */
1659 for(node= snode->edittree->nodes.first; node; node= node->next) {
1660 if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
1661 node->flag |= NODE_DO_OUTPUT;
1669 bNodeSocket *sock= NULL;
1671 /* try to find an already connected socket to cycle to the next */
1672 for(link= snode->edittree->links.first; link; link= link->next)
1673 if(link->tonode==node && link->fromnode==tonode)
1674 if(link->tosock==node->inputs.first)
1678 /* unlink existing connection */
1679 sock= link->fromsock;
1680 nodeRemLink(snode->edittree, link);
1682 /* find a socket after the previously connected socket */
1683 for(sock=sock->next; sock; sock= sock->next)
1684 if(!nodeSocketIsHidden(sock))
1688 /* find a socket starting from the first socket */
1690 for(sock= tonode->outputs.first; sock; sock= sock->next)
1691 if(!nodeSocketIsHidden(sock))
1696 /* get link to viewer */
1697 for(link= snode->edittree->links.first; link; link= link->next)
1698 if(link->tonode==node && link->tosock==node->inputs.first)
1702 nodeAddLink(snode->edittree, tonode, sock, node, node->inputs.first);
1705 link->fromnode= tonode;
1706 link->fromsock= sock;
1708 ntreeUpdateTree(snode->edittree);
1709 snode_update(snode, node);
1715 static int node_active_link_viewer(bContext *C, wmOperator *UNUSED(op))
1717 SpaceNode *snode= CTX_wm_space_node(C);
1720 node= editnode_get_active(snode->edittree);
1723 return OPERATOR_CANCELLED;
1725 ED_preview_kill_jobs(C);
1727 node_link_viewer(snode, node);
1728 snode_notify(C, snode);
1730 return OPERATOR_FINISHED;
1735 void NODE_OT_link_viewer(wmOperatorType *ot)
1738 ot->name= "Link to Viewer Node";
1739 ot->description = "Link to viewer node";
1740 ot->idname= "NODE_OT_link_viewer";
1743 ot->exec= node_active_link_viewer;
1744 ot->poll= ED_operator_node_active;
1747 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1752 /* return 0, nothing done */
1753 static int UNUSED_FUNCTION(node_mouse_groupheader)(SpaceNode *snode)
1759 gnode= node_tree_get_editgroup(snode->nodetree);
1760 if(gnode==NULL) return 0;
1762 // XXX getmouseco_areawin(mval);
1763 // XXX areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1765 /* click in header or outside? */
1766 if(BLI_in_rctf(&gnode->totr, mx, my)==0) {
1767 rctf rect= gnode->totr;
1769 rect.ymax += NODE_DY;
1770 if(BLI_in_rctf(&rect, mx, my)==0)
1771 snode_make_group_editable(snode, NULL); /* toggles, so exits editmode */
1773 // XXX transform_nodes(snode->nodetree, 'g', "Move group");
1780 /* checks snode->mouse position, and returns found node/socket */
1781 /* type is SOCK_IN and/or SOCK_OUT */
1782 static int find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
1788 /* check if we click in a socket */
1789 for(node= snode->edittree->nodes.first; node; node= node->next) {
1791 rect.xmin = snode->mx - (NODE_SOCKSIZE+4);
1792 rect.ymin = snode->my - (NODE_SOCKSIZE+4);
1793 rect.xmax = snode->mx + (NODE_SOCKSIZE+4);
1794 rect.ymax = snode->my + (NODE_SOCKSIZE+4);
1796 if (!(node->flag & NODE_HIDDEN)) {
1797 /* extra padding inside and out - allow dragging on the text areas too */
1798 if (in_out == SOCK_IN) {
1799 rect.xmax += NODE_SOCKSIZE;
1800 rect.xmin -= NODE_SOCKSIZE*4;
1801 } else if (in_out == SOCK_OUT) {
1802 rect.xmax += NODE_SOCKSIZE*4;
1803 rect.xmin -= NODE_SOCKSIZE;
1807 if(in_out & SOCK_IN) {
1808 for(sock= node->inputs.first; sock; sock= sock->next) {
1809 if(!nodeSocketIsHidden(sock)) {
1810 if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1811 if(node == visible_node(snode, &rect)) {
1820 if(in_out & SOCK_OUT) {
1821 for(sock= node->outputs.first; sock; sock= sock->next) {
1822 if(!nodeSocketIsHidden(sock)) {
1823 if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1824 if(node == visible_node(snode, &rect)) {
1835 /* check group sockets
1836 * NB: using ngroup->outputs as input sockets and vice versa here!
1838 if(in_out & SOCK_IN) {
1839 for(sock= snode->edittree->outputs.first; sock; sock= sock->next) {
1840 if(!nodeSocketIsHidden(sock)) {
1841 if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1842 *nodep= NULL; /* NULL node pointer indicates group socket */
1849 if(in_out & SOCK_OUT) {
1850 for(sock= snode->edittree->inputs.first; sock; sock= sock->next) {
1851 if(!nodeSocketIsHidden(sock)) {
1852 if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1853 *nodep= NULL; /* NULL node pointer indicates group socket */
1864 static int node_socket_hilights(SpaceNode *snode, int in_out)
1867 bNodeSocket *sock, *tsock, *socksel= NULL;
1870 if(snode->edittree==NULL) return 0;
1872 /* deselect sockets */
1873 for(node= snode->edittree->nodes.first; node; node= node->next) {
1874 for(sock= node->inputs.first; sock; sock= sock->next) {
1875 if(sock->flag & SELECT) {
1876 sock->flag &= ~SELECT;
1881 for(sock= node->outputs.first; sock; sock= sock->next) {
1882 if(sock->flag & SELECT) {
1883 sock->flag &= ~SELECT;
1890 // XXX mousepos should be set here!
1892 if(find_indicated_socket(snode, &node, &tsock, in_out)) {
1893 tsock->flag |= SELECT;
1894 if(redraw==1 && tsock==socksel) redraw= 0;
1901 static int outside_group_rect(SpaceNode *snode)
1903 bNode *gnode= node_tree_get_editgroup(snode->nodetree);
1905 return (snode->mx < gnode->totr.xmin || snode->mx >= gnode->totr.xmax
1906 || snode->my < gnode->totr.ymin || snode->my >= gnode->totr.ymax);
1911 /* ****************** Add *********************** */
1914 typedef struct bNodeListItem {
1915 struct bNodeListItem *next, *prev;
1919 static int sort_nodes_locx(void *a, void *b)
1921 bNodeListItem *nli1 = (bNodeListItem *)a;
1922 bNodeListItem *nli2 = (bNodeListItem *)b;
1923 bNode *node1 = nli1->node;
1924 bNode *node2 = nli2->node;
1926 if (node1->locx > node2->locx)
1932 static int socket_is_available(bNodeTree *UNUSED(ntree), bNodeSocket *sock, int allow_used)
1934 if (nodeSocketIsHidden(sock))
1937 if (!allow_used && (sock->flag & SOCK_IN_USE))
1943 static bNodeSocket *best_socket_output(bNodeTree *ntree, bNode *node, bNodeSocket *sock_target, int allow_multiple)
1947 /* first try to find a socket with a matching name */
1948 for (sock=node->outputs.first; sock; sock=sock->next) {
1950 if (!socket_is_available(ntree, sock, allow_multiple))
1953 /* check for same types */
1954 if (sock->type == sock_target->type) {
1955 if (strcmp(sock->name, sock_target->name)==0)
1960 /* otherwise settle for the first available socket of the right type */
1961 for (sock=node->outputs.first; sock; sock=sock->next) {
1963 if (!socket_is_available(ntree, sock, allow_multiple))
1966 /* check for same types */
1967 if (sock->type == sock_target->type) {
1975 /* this is a bit complicated, but designed to prioritise finding
1976 * sockets of higher types, such as image, first */
1977 static bNodeSocket *best_socket_input(bNodeTree *ntree, bNode *node, int num, int replace)
1980 int socktype, maxtype=0;
1983 for (sock=node->inputs.first; sock; sock=sock->next) {
1984 maxtype = MAX2(sock->type, maxtype);
1987 /* find sockets of higher 'types' first (i.e. image) */
1988 for (socktype=maxtype; socktype >= 0; socktype--) {
1989 for (sock=node->inputs.first; sock; sock=sock->next) {
1991 if (!socket_is_available(ntree, sock, replace)) {
1996 if (sock->type == socktype) {
1997 /* increment to make sure we don't keep finding
1998 * the same socket on every attempt running this function */
2009 void snode_autoconnect(SpaceNode *snode, int allow_multiple, int replace)
2011 ListBase *nodelist = MEM_callocN(sizeof(ListBase), "items_list");
2017 for(node= snode->edittree->nodes.first; node; node= node->next) {
2018 if(node->flag & NODE_SELECT) {
2019 nli = MEM_mallocN(sizeof(bNodeListItem), "temporary node list item");
2021 BLI_addtail(nodelist, nli);
2025 /* sort nodes left to right */
2026 BLI_sortlist(nodelist, sort_nodes_locx);
2028 for (nli=nodelist->first; nli; nli=nli->next) {
2029 bNode *node_fr, *node_to;
2030 bNodeSocket *sock_fr, *sock_to;
2032 if (nli->next == NULL) break;
2034 node_fr = nli->node;
2035 node_to = nli->next->node;
2037 /* check over input sockets first */
2038 for (i=0; i<BLI_countlist(&node_to->inputs); i++) {
2040 /* find the best guess input socket */
2041 sock_to = best_socket_input(snode->edittree, node_to, i, replace);
2042 if (!sock_to) continue;
2044 /* check for an appropriate output socket to connect from */
2045 sock_fr = best_socket_output(snode->edittree, node_fr, sock_to, allow_multiple);
2046 if (!sock_fr) continue;
2048 /* then we can connect */
2050 nodeRemSocketLinks(snode->edittree, sock_to);
2052 link = nodeAddLink(snode->edittree, node_fr, sock_fr, node_to, sock_to);
2053 /* validate the new link */
2054 ntreeUpdateTree(snode->edittree);
2055 if (!(link->flag & NODE_LINK_VALID)) {
2056 nodeRemLink(snode->edittree, link);
2060 snode_update(snode, node_to);
2067 ntreeUpdateTree(snode->edittree);
2070 BLI_freelistN(nodelist);
2071 MEM_freeN(nodelist);
2074 /* can be called from menus too, but they should do own undopush and redraws */
2075 bNode *node_add_node(SpaceNode *snode, Main *bmain, Scene *scene, bNodeTemplate *ntemp, float locx, float locy)
2077 bNode *node= NULL, *gnode;
2079 node_deselect_all(snode);
2081 node = nodeAddNode(snode->edittree, ntemp);
2086 node->locy= locy + 60.0f; // arbitrary.. so its visible, (0,0) is top of node
2087 node->flag |= SELECT;
2089 gnode= node_tree_get_editgroup(snode->nodetree);
2091 node->locx -= gnode->locx;
2092 node->locy -= gnode->locy;
2095 ntreeUpdateTree(snode->edittree);
2096 ED_node_set_active(bmain, snode->edittree, node);
2098 if(snode->nodetree->type==NTREE_COMPOSIT) {
2099 if(ELEM4(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS, CMP_NODE_OUTPUT_FILE)) {
2100 node->id = &scene->id;
2102 else if(ELEM3(node->type, CMP_NODE_MOVIECLIP, CMP_NODE_MOVIEDISTORTION, CMP_NODE_STABILIZE2D)) {
2103 node->id = (ID *)scene->clip;
2106 ntreeCompositForceHidden(snode->edittree, scene);
2110 id_us_plus(node->id);
2112 snode_update(snode, node);
2115 if(snode->nodetree->type==NTREE_TEXTURE) {
2116 ntreeTexCheckCyclics(snode->edittree);
2122 /* ****************** Duplicate *********************** */
2124 static int node_duplicate_exec(bContext *C, wmOperator *op)
2126 SpaceNode *snode= CTX_wm_space_node(C);
2127 bNodeTree *ntree= snode->edittree;
2128 bNode *node, *newnode, *lastnode;
2129 bNodeLink *link, *newlink, *lastlink;
2130 int keep_inputs = RNA_boolean_get(op->ptr, "keep_inputs");
2132 ED_preview_kill_jobs(C);
2134 lastnode = ntree->nodes.last;
2135 for(node= ntree->nodes.first; node; node= node->next) {
2136 if(node->flag & SELECT) {
2137 newnode = nodeCopyNode(ntree, node);
2140 /* simple id user adjustment, node internal functions dont touch this
2141 * but operators and readfile.c do. */
2142 id_us_plus(newnode->id);
2143 /* to ensure redraws or rerenders happen */
2144 ED_node_changed_update(snode->id, newnode);
2148 /* make sure we don't copy new nodes again! */
2153 /* copy links between selected nodes
2154 * NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
2156 lastlink = ntree->links.last;
2157 for (link=ntree->links.first; link; link=link->next) {
2158 /* This creates new links between copied nodes.
2159 * If keep_inputs is set, also copies input links from unselected (when fromnode==NULL)!
2161 if (link->tonode && (link->tonode->flag & NODE_SELECT)
2162 && (keep_inputs || (link->fromnode && (link->fromnode->flag & NODE_SELECT)))) {
2163 newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
2164 newlink->flag = link->flag;
2165 newlink->tonode = link->tonode->new_node;
2166 newlink->tosock = link->tosock->new_sock;
2167 if (link->fromnode && (link->fromnode->flag & NODE_SELECT)) {
2168 newlink->fromnode = link->fromnode->new_node;
2169 newlink->fromsock = link->fromsock->new_sock;
2172 /* input node not copied, this keeps the original input linked */
2173 newlink->fromnode = link->fromnode;
2174 newlink->fromsock = link->fromsock;
2177 BLI_addtail(&ntree->links, newlink);
2180 /* make sure we don't copy new links again! */
2185 /* deselect old nodes, select the copies instead */
2186 for(node= ntree->nodes.first; node; node= node->next) {
2187 if(node->flag & SELECT) {
2188 /* has been set during copy above */
2189 newnode = node->new_node;
2191 node->flag &= ~(NODE_SELECT|NODE_ACTIVE);
2192 newnode->flag |= NODE_SELECT;
2195 /* make sure we don't copy new nodes again! */
2200 ntreeUpdateTree(snode->edittree);
2202 snode_notify(C, snode);
2203 snode_dag_update(C, snode);
2205 return OPERATOR_FINISHED;
2208 void NODE_OT_duplicate(wmOperatorType *ot)
2211 ot->name= "Duplicate Nodes";
2212 ot->description = "Duplicate selected nodes";
2213 ot->idname= "NODE_OT_duplicate";
2216 ot->exec= node_duplicate_exec;
2217 ot->poll= ED_operator_node_active;
2220 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2222 RNA_def_boolean(ot->srna, "keep_inputs", 0, "Keep Inputs", "Keep the input links to duplicated nodes");
2225 /* *************************** add link op ******************** */
2227 static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeLink *link)
2232 if(tsock && nodeCountSocketLinks(snode->edittree, link->tosock) > tsock->limit) {
2234 for(tlink= snode->edittree->links.first; tlink; tlink= tlink->next) {
2235 if(link!=tlink && tlink->tosock==link->tosock)
2239 /* try to move the existing link to the next available socket */
2240 if (tlink->tonode) {
2241 /* is there a free input socket with the target type? */
2242 for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
2243 if(sock->type==tlink->tosock->type)
2244 if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
2248 tlink->tosock= sock;
2249 sock->flag &= ~(SOCK_HIDDEN|SOCK_AUTO_HIDDEN);
2252 nodeRemLink(snode->edittree, tlink);
2256 nodeRemLink(snode->edittree, tlink);
2261 /* loop that adds a nodelink, called by function below */
2262 /* in_out = starting socket */
2263 static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
2265 SpaceNode *snode= CTX_wm_space_node(C);
2266 ARegion *ar= CTX_wm_region(C);
2267 bNodeLinkDrag *nldrag= op->customdata;
2268 bNode *tnode, *node;
2269 bNodeSocket *tsock= NULL, *sock;
2273 in_out= nldrag->in_out;
2278 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
2279 &snode->mx, &snode->my);
2281 switch (event->type) {
2284 if(in_out==SOCK_OUT) {
2285 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_IN)) {
2286 if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
2287 if( link->tosock!= tsock && (!tnode || (tnode!=node && link->tonode!=tnode)) ) {
2288 link->tonode= tnode;
2289 link->tosock= tsock;
2290 if (link->prev==NULL && link->next==NULL) {
2291 BLI_addtail(&snode->edittree->links, link);
2294 snode->edittree->update |= NTREE_UPDATE_LINKS;
2295 ntreeUpdateTree(snode->edittree);
2300 if (link->tonode || link->tosock) {
2301 BLI_remlink(&snode->edittree->links, link);
2302 link->prev = link->next = NULL;
2306 snode->edittree->update |= NTREE_UPDATE_LINKS;
2307 ntreeUpdateTree(snode->edittree);
2312 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_OUT)) {
2313 if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
2314 if(nodeCountSocketLinks(snode->edittree, tsock) < tsock->limit) {
2315 if( link->fromsock!= tsock && (!tnode || (tnode!=node && link->fromnode!=tnode)) ) {
2316 link->fromnode= tnode;
2317 link->fromsock= tsock;
2318 if (link->prev==NULL && link->next==NULL) {
2319 BLI_addtail(&snode->edittree->links, link);
2322 snode->edittree->update |= NTREE_UPDATE_LINKS;
2323 ntreeUpdateTree(snode->edittree);
2329 if (link->tonode || link->tosock) {
2330 BLI_remlink(&snode->edittree->links, link);
2331 link->prev = link->next = NULL;
2332 link->fromnode= NULL;
2333 link->fromsock= NULL;
2334 snode->edittree->update |= NTREE_UPDATE_LINKS;
2335 ntreeUpdateTree(snode->edittree);
2339 /* hilight target sockets only */
2340 node_socket_hilights(snode, in_out==SOCK_OUT?SOCK_IN:SOCK_OUT);
2341 ED_region_tag_redraw(ar);
2347 if(link->tosock && link->fromsock) {
2348 /* send changed events for original tonode and new */
2349 snode_update(snode, link->tonode);
2351 /* we might need to remove a link */
2352 if(in_out==SOCK_OUT)
2353 node_remove_extra_links(snode, link->tosock, link);
2355 /* when linking to group outputs, update the socket type */
2356 /* XXX this should all be part of a generic update system */
2357 if (!link->tonode) {
2358 link->tosock->type = link->fromsock->type;
2361 else if (outside_group_rect(snode) && (link->tonode || link->fromnode)) {
2362 /* automatically add new group socket */
2363 if (link->tonode && link->tosock) {
2364 link->fromsock = node_group_expose_socket(snode->edittree, link->tosock, SOCK_IN);
2365 link->fromnode = NULL;
2366 if (link->prev==NULL && link->next==NULL) {
2367 BLI_addtail(&snode->edittree->links, link);
2369 snode->edittree->update |= NTREE_UPDATE_GROUP_IN | NTREE_UPDATE_LINKS;
2371 else if (link->fromnode && link->fromsock) {
2372 link->tosock = node_group_expose_socket(snode->edittree, link->fromsock, SOCK_OUT);
2373 link->tonode = NULL;
2374 if (link->prev==NULL && link->next==NULL) {
2375 BLI_addtail(&snode->edittree->links, link);
2377 snode->edittree->update |= NTREE_UPDATE_GROUP_OUT | NTREE_UPDATE_LINKS;
2381 nodeRemLink(snode->edittree, link);
2383 ntreeUpdateTree(snode->edittree);
2384 snode_notify(C, snode);
2385 snode_dag_update(C, snode);
2387 BLI_remlink(&snode->linkdrag, nldrag);
2390 return OPERATOR_FINISHED;
2393 return OPERATOR_RUNNING_MODAL;
2396 /* return 1 when socket clicked */
2397 static int node_link_init(SpaceNode *snode, bNodeLinkDrag *nldrag)
2401 /* output indicated? */
2402 if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_OUT)) {
2403 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
2406 /* find if we break a link */
2407 for(link= snode->edittree->links.first; link; link= link->next) {
2408 if(link->fromsock==nldrag->sock)
2412 nldrag->node= link->tonode;
2413 nldrag->sock= link->tosock;
2414 nodeRemLink(snode->edittree, link);
2420 else if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_IN)) {
2421 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
2424 /* find if we break a link */
2425 for(link= snode->edittree->links.first; link; link= link->next) {
2426 if(link->tosock==nldrag->sock)
2430 /* send changed event to original tonode */
2432 snode_update(snode, link->tonode);
2434 nldrag->node= link->fromnode;
2435 nldrag->sock= link->fromsock;
2436 nodeRemLink(snode->edittree, link);
2445 static int node_link_invoke(bContext *C, wmOperator *op, wmEvent *event)
2447 SpaceNode *snode= CTX_wm_space_node(C);
2448 ARegion *ar= CTX_wm_region(C);
2449 bNodeLinkDrag *nldrag= MEM_callocN(sizeof(bNodeLinkDrag), "drag link op customdata");
2452 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
2453 &snode->mx, &snode->my);
2455 ED_preview_kill_jobs(C);
2457 nldrag->in_out= node_link_init(snode, nldrag);
2459 if(nldrag->in_out) {
2460 op->customdata= nldrag;
2462 /* we make a temporal link */
2463 if(nldrag->in_out==SOCK_OUT) {
2464 nldrag->link= MEM_callocN(sizeof(bNodeLink), "link");
2465 nldrag->link->fromnode= nldrag->node;
2466 nldrag->link->fromsock= nldrag->sock;
2467 nldrag->link->tonode= NULL;
2468 nldrag->link->tosock= NULL;
2471 nldrag->link= MEM_callocN(sizeof(bNodeLink), "link");
2472 nldrag->link->fromnode= NULL;
2473 nldrag->link->fromsock= NULL;
2474 nldrag->link->tonode= nldrag->node;
2475 nldrag->link->tosock= nldrag->sock;
2477 BLI_addtail(&snode->linkdrag, nldrag);
2479 /* add modal handler */
2480 WM_event_add_modal_handler(C, op);
2482 return OPERATOR_RUNNING_MODAL;
2486 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2490 static int node_link_cancel(bContext *C, wmOperator *op)
2492 SpaceNode *snode= CTX_wm_space_node(C);
2493 bNodeLinkDrag *nldrag= op->customdata;
2495 nodeRemLink(snode->edittree, nldrag->link);
2496 BLI_remlink(&snode->linkdrag, nldrag);
2499 return OPERATOR_CANCELLED;
2502 void NODE_OT_link(wmOperatorType *ot)
2505 ot->name= "Link Nodes";
2506 ot->idname= "NODE_OT_link";
2509 ot->invoke= node_link_invoke;
2510 ot->modal= node_link_modal;
2511 // ot->exec= node_link_exec;
2512 ot->poll= ED_operator_node_active;
2513 ot->cancel= node_link_cancel;
2516 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2519 /* ********************** Make Link operator ***************** */
2521 /* makes a link between selected output and input sockets */
2522 static int node_make_link_exec(bContext *C, wmOperator *op)
2524 SpaceNode *snode= CTX_wm_space_node(C);
2525 int replace = RNA_boolean_get(op->ptr, "replace");
2527 ED_preview_kill_jobs(C);
2529 snode_autoconnect(snode, 1, replace);
2531 ntreeUpdateTree(snode->edittree);
2532 snode_notify(C, snode);
2533 snode_dag_update(C, snode);
2535 return OPERATOR_FINISHED;
2538 void NODE_OT_link_make(wmOperatorType *ot)
2541 ot->name= "Make Links";
2542 ot->description= "Makes a link between selected output in input sockets";
2543 ot->idname= "NODE_OT_link_make";
2546 ot->exec= node_make_link_exec;
2547 ot->poll= ED_operator_node_active; // XXX we need a special poll which checks that there are selected input/output sockets
2550 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2552 RNA_def_boolean(ot->srna, "replace", 0, "Replace", "Replace socket connections with the new links");
2555 /* ********************** Cut Link operator ***************** */
2557 #define LINK_RESOL 12
2558 static int cut_links_intersect(bNodeLink *link, float mcoords[][2], int tot)
2560 float coord_array[LINK_RESOL+1][2];
2563 if(node_link_bezier_points(NULL, NULL, link, coord_array, LINK_RESOL)) {
2565 for(i=0; i<tot-1; i++)
2566 for(b=0; b<LINK_RESOL; b++)
2567 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
2573 static int cut_links_exec(bContext *C, wmOperator *op)
2575 SpaceNode *snode= CTX_wm_space_node(C);
2576 ARegion *ar= CTX_wm_region(C);
2577 float mcoords[256][2];
2580 RNA_BEGIN(op->ptr, itemptr, "path") {
2583 RNA_float_get_array(&itemptr, "loc", loc);
2584 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1],
2585 &mcoords[i][0], &mcoords[i][1]);
2592 bNodeLink *link, *next;
2594 ED_preview_kill_jobs(C);
2596 for(link= snode->edittree->links.first; link; link= next) {
2599 if(cut_links_intersect(link, mcoords, i)) {
2600 snode_update(snode, link->tonode);
2601 nodeRemLink(snode->edittree, link);
2605 ntreeUpdateTree(snode->edittree);
2606 snode_notify(C, snode);
2607 snode_dag_update(C, snode);
2609 return OPERATOR_FINISHED;
2612 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2615 void NODE_OT_links_cut(wmOperatorType *ot)
2619 ot->name= "Cut links";
2620 ot->idname= "NODE_OT_links_cut";
2622 ot->invoke= WM_gesture_lines_invoke;
2623 ot->modal= WM_gesture_lines_modal;
2624 ot->exec= cut_links_exec;
2625 ot->cancel= WM_gesture_lines_cancel;
2627 ot->poll= ED_operator_node_active;
2630 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2632 prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2633 RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2635 RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
2638 /* ********************** Detach links operator ***************** */
2640 static int detach_links_exec(bContext *C, wmOperator *op)
2642 SpaceNode *snode= CTX_wm_space_node(C);
2643 bNodeTree *ntree= snode->edittree;
2646 ED_preview_kill_jobs(C);
2648 for(node= ntree->nodes.first; node; node= node->next) {
2649 if(node->flag & SELECT) {
2650 nodeInternalRelink(ntree, node);
2654 ntreeUpdateTree(ntree);
2656 snode_notify(C, snode);
2657 snode_dag_update(C, snode);
2659 return OPERATOR_FINISHED;
2662 void NODE_OT_links_detach(wmOperatorType *ot)
2664 ot->name= "Detach Links";
2665 ot->idname= "NODE_OT_links_detach";
2667 ot->exec= detach_links_exec;
2668 ot->poll= ED_operator_node_active;
2671 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2674 /* ********************* automatic node insert on dragging ******************* */
2676 /* assumes sockets in list */
2677 static bNodeSocket *socket_best_match(ListBase *sockets, int type)
2681 /* first, match type */
2682 for(sock= sockets->first; sock; sock= sock->next)
2683 if(!nodeSocketIsHidden(sock))
2684 if(type == sock->type)
2687 /* then just use first unhidden socket */
2688 for(sock= sockets->first; sock; sock= sock->next)
2689 if(!nodeSocketIsHidden(sock))
2692 /* OK, let's unhide proper one */
2693 for(sock= sockets->first; sock; sock= sock->next) {
2694 if(type == sock->type) {
2695 sock->flag &= ~(SOCK_HIDDEN|SOCK_AUTO_HIDDEN);
2700 /* just the first */
2701 sock= sockets->first;
2702 sock->flag &= ~(SOCK_HIDDEN|SOCK_AUTO_HIDDEN);
2704 return sockets->first;
2707 /* prevent duplicate testing code below */
2708 static SpaceNode *ed_node_link_conditions(ScrArea *sa, bNode **select)
2710 SpaceNode *snode= sa?sa->spacedata.first:NULL;
2714 /* no unlucky accidents */
2715 if(sa==NULL || sa->spacetype!=SPACE_NODE) return NULL;
2719 for(node= snode->edittree->nodes.first; node; node= node->next) {
2720 if(node->flag & SELECT) {
2727 /* only one selected */
2728 if(node || *select==NULL) return NULL;
2731 if((*select)->inputs.first==NULL || (*select)->outputs.first==NULL) return NULL;
2733 /* test node for links */
2734 for(link= snode->edittree->links.first; link; link=link->next) {
2735 if(link->tonode == *select || link->fromnode == *select)
2742 /* assumes link with NODE_LINKFLAG_HILITE set */
2743 void ED_node_link_insert(ScrArea *sa)
2745 bNode *node, *select;
2746 SpaceNode *snode= ed_node_link_conditions(sa, &select);
2748 bNodeSocket *sockto;
2750 if(snode==NULL) return;
2753 for(link= snode->edittree->links.first; link; link=link->next)
2754 if(link->flag & NODE_LINKFLAG_HILITE)
2759 sockto= link->tosock;
2761 link->tonode= select;
2762 link->tosock= socket_best_match(&select->inputs, link->fromsock->type);
2763 link->flag &= ~NODE_LINKFLAG_HILITE;
2765 nodeAddLink(snode->edittree, select, socket_best_match(&select->outputs, sockto->type), node, sockto);
2766 ntreeUpdateTree(snode->edittree); /* needed for pointers */
2767 snode_update(snode, select);
2768 ED_node_changed_update(snode->id, select);
2773 /* test == 0, clear all intersect flags */
2774 void ED_node_link_intersect_test(ScrArea *sa, int test)
2777 SpaceNode *snode= ed_node_link_conditions(sa, &select);
2778 bNodeLink *link, *selink=NULL;
2779 float mcoords[6][2];
2781 if(snode==NULL) return;
2784 for(link= snode->edittree->links.first; link; link=link->next)
2785 link->flag &= ~NODE_LINKFLAG_HILITE;
2789 /* okay, there's 1 node, without links, now intersect */
2790 mcoords[0][0]= select->totr.xmin;
2791 mcoords[0][1]= select->totr.ymin;
2792 mcoords[1][0]= select->totr.xmax;
2793 mcoords[1][1]= select->totr.ymin;
2794 mcoords[2][0]= select->totr.xmax;
2795 mcoords[2][1]= select->totr.ymax;
2796 mcoords[3][0]= select->totr.xmin;
2797 mcoords[3][1]= select->totr.ymax;
2798 mcoords[4][0]= select->totr.xmin;
2799 mcoords[4][1]= select->totr.ymin;
2800 mcoords[5][0]= select->totr.xmax;
2801 mcoords[5][1]= select->totr.ymax;
2803 /* we only tag a single link for intersect now */
2804 /* idea; use header dist when more? */
2805 for(link= snode->edittree->links.first; link; link=link->next) {
2807 if(cut_links_intersect(link, mcoords, 5)) { /* intersect code wants edges */
2814 if(link==NULL && selink)
2815 selink->flag |= NODE_LINKFLAG_HILITE;
2819 /* ******************************** */
2820 // XXX some code needing updating to operators...
2823 /* goes over all scenes, reads render layers */
2824 static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
2826 Main *bmain= CTX_data_main(C);
2827 SpaceNode *snode= CTX_wm_space_node(C);
2828 Scene *curscene= CTX_data_scene(C), *scene;
2831 ED_preview_kill_jobs(C);
2833 /* first tag scenes unread */
2834 for(scene= bmain->scene.first; scene; scene= scene->id.next)
2835 scene->id.flag |= LIB_DOIT;
2837 for(node= snode->edittree->nodes.first; node; node= node->next) {
2838 if(node->type==CMP_NODE_R_LAYERS) {
2840 if(id->flag & LIB_DOIT) {
2841 RE_ReadRenderResult(curscene, (Scene *)id);
2842 ntreeCompositTagRender((Scene *)id);
2843 id->flag &= ~LIB_DOIT;
2848 snode_notify(C, snode);
2849 snode_dag_update(C, snode);
2851 return OPERATOR_FINISHED;
2854 void NODE_OT_read_renderlayers(wmOperatorType *ot)
2857 ot->name= "Read Render Layers";
2858 ot->idname= "NODE_OT_read_renderlayers";
2860 ot->exec= node_read_renderlayers_exec;
2862 ot->poll= composite_node_active;
2868 static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
2870 Main *bmain= CTX_data_main(C);
2871 SpaceNode *snode= CTX_wm_space_node(C);
2872 Scene *curscene= CTX_data_scene(C);
2873 Render *re= RE_NewRender(curscene->id.name);
2876 RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
2879 /* note we are careful to send the right notifier, as otherwise the
2880 compositor would reexecute and overwrite the full sample result */
2881 WM_event_add_notifier(C, NC_SCENE|ND_COMPO_RESULT, NULL);
2883 return OPERATOR_FINISHED;
2887 void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
2890 ot->name= "Read Full Sample Layers";
2891 ot->idname= "NODE_OT_read_fullsamplelayers";
2893 ot->exec= node_read_fullsamplelayers_exec;
2895 ot->poll= composite_node_active;
2901 int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
2903 Scene *sce= CTX_data_scene(C);
2906 for(node= sce->nodetree->nodes.first; node; node= node->next) {
2907 if(node->id==(ID *)sce && node->need_exec) {
2912 SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
2917 WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
2918 RNA_string_set(&op_ptr, "layer", srl->name);
2919 RNA_string_set(&op_ptr, "scene", sce->id.name+2);
2921 /* to keep keypositions */
2922 sce->r.scemode |= R_NO_FRAME_UPDATE;
2924 WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
2926 WM_operator_properties_free(&op_ptr);
2928 return OPERATOR_FINISHED;
2932 return OPERATOR_CANCELLED;
2935 void NODE_OT_render_changed(wmOperatorType *ot)
2938 ot->name= "Render Changed Layer";
2939 ot->idname= "NODE_OT_render_changed";
2941 ot->exec= node_render_changed_exec;
2943 ot->poll= composite_node_active;
2950 /* ****************** Make Group operator ******************* */
2952 static int node_group_make_exec(bContext *C, wmOperator *op)
2954 SpaceNode *snode = CTX_wm_space_node(C);
2957 if(snode->edittree!=snode->nodetree) {
2958 BKE_report(op->reports, RPT_WARNING, "Can not add a new Group in a Group");
2959 return OPERATOR_CANCELLED;
2962 /* for time being... is too complex to handle */
2963 if(snode->treetype==NTREE_COMPOSIT) {
2964 for(gnode=snode->nodetree->nodes.first; gnode; gnode= gnode->next) {
2965 if(gnode->flag & SELECT)
2966 if(gnode->type==CMP_NODE_R_LAYERS)
2971 BKE_report(op->reports, RPT_WARNING, "Can not add RenderLayer in a Group");
2972 return OPERATOR_CANCELLED;
2976 ED_preview_kill_jobs(C);
2978 gnode= node_group_make_from_selected(snode->nodetree);
2980 BKE_report(op->reports, RPT_WARNING, "Can not make Group");
2981 return OPERATOR_CANCELLED;
2984 nodeSetActive(snode->nodetree, gnode);
2985 ntreeUpdateTree(snode->nodetree);
2988 snode_notify(C, snode);
2989 snode_dag_update(C, snode);
2991 return OPERATOR_FINISHED;
2994 void NODE_OT_group_make(wmOperatorType *ot)
2998 ot->description = "Make group from selected nodes";
2999 ot->idname = "NODE_OT_group_make";
3002 ot->exec = node_group_make_exec;
3003 ot->poll = ED_operator_node_active;
3006 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
3009 /* ****************** Hide operator *********************** */
3011 static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
3014 int tot_eq= 0, tot_neq= 0;
3016 /* Toggles the flag on all selected nodes.
3017 * If the flag is set on all nodes it is unset.
3018 * If the flag is not set on all nodes, it is set.
3020 for(node= snode->edittree->nodes.first; node; node= node->next) {
3021 if(node->flag & SELECT) {
3023 if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
3025 if(toggle_flag== NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS)==0)
3028 if(node->flag & toggle_flag)
3034 for(node= snode->edittree->nodes.first; node; node= node->next) {
3035 if(node->flag & SELECT) {
3037 if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
3039 if(toggle_flag== NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS)==0)
3042 if( (tot_eq && tot_neq) || tot_eq==0) {
3043 node->flag |= toggle_flag;
3045 /* hide/unhide node also toggles unlinked socket display */
3046 if (toggle_flag== NODE_HIDDEN)
3047 node_set_hidden_sockets(snode, node, SOCK_AUTO_HIDDEN, 1);
3050 node->flag &= ~toggle_flag;
3052 /* hide/unhide node also toggles unlinked socket display */
3053 if (toggle_flag== NODE_HIDDEN)
3054 node_set_hidden_sockets(snode, node, SOCK_AUTO_HIDDEN, 0);
3060 static int node_hide_toggle_exec(bContext *C, wmOperator *UNUSED(op))
3062 SpaceNode *snode= CTX_wm_space_node(C);
3064 /* sanity checking (poll callback checks this already) */
3065 if((snode == NULL) || (snode->edittree == NULL))
3066 return OPERATOR_CANCELLED;
3068 node_flag_toggle_exec(snode, NODE_HIDDEN);
3070 snode_notify(C, snode);
3072 return OPERATOR_FINISHED;
3075 void NODE_OT_hide_toggle(wmOperatorType *ot)
3079 ot->description= "Toggle hiding of selected nodes";
3080 ot->idname= "NODE_OT_hide_toggle";
3083 ot->exec= node_hide_toggle_exec;
3084 ot->poll= ED_operator_node_active;
3087 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
3090 static int node_preview_toggle_exec(bContext *C, wmOperator *UNUSED(op))
3092 SpaceNode *snode= CTX_wm_space_node(C);
3094 /* sanity checking (poll callback checks this already) */
3095 if((snode == NULL) || (snode->edittree == NULL))
3096 return OPERATOR_CANCELLED;
3098 ED_preview_kill_jobs(C);