forked from bartvdbraak/blender
Another one for drag and drop:
Allow dropping image files from outside blender, or image datablocks from inside blender to the compositing node editor, to add an image node. Also small tweak: Only set 'path' properties on drops, if the drag->path isn't empty.
This commit is contained in:
parent
d153850520
commit
7aa907c996
@ -2030,4 +2030,86 @@ void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
|
||||
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
|
||||
}
|
||||
|
||||
/* ****************** Add File Node Operator ******************* */
|
||||
|
||||
static int node_add_file_exec(bContext *C, wmOperator *op)
|
||||
{
|
||||
Scene *scene= CTX_data_scene(C);
|
||||
SpaceNode *snode= CTX_wm_space_node(C);
|
||||
bNode *node;
|
||||
Image *ima= NULL;
|
||||
int ntype=0;
|
||||
|
||||
/* check input variables */
|
||||
if (RNA_property_is_set(op->ptr, "path"))
|
||||
{
|
||||
char path[FILE_MAX];
|
||||
RNA_string_get(op->ptr, "path", path);
|
||||
ima= BKE_add_image_file(path, scene ? scene->r.cfra : 1);
|
||||
}
|
||||
else if(RNA_property_is_set(op->ptr, "name"))
|
||||
{
|
||||
char name[32];
|
||||
RNA_string_get(op->ptr, "name", name);
|
||||
ima= (Image *)find_id("IM", name);
|
||||
}
|
||||
|
||||
if(!ima) {
|
||||
BKE_report(op->reports, RPT_ERROR, "Not an Image.");
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
|
||||
node_deselectall(snode);
|
||||
|
||||
if (snode->nodetree->type==NTREE_COMPOSIT)
|
||||
ntype = CMP_NODE_IMAGE;
|
||||
|
||||
node = node_add_node(snode, scene, ntype, snode->mx, snode->my);
|
||||
|
||||
if (!node) {
|
||||
BKE_report(op->reports, RPT_ERROR, "Could not add an image node.");
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
node->id = (ID *)ima;
|
||||
|
||||
snode_notify(C, snode);
|
||||
|
||||
return OPERATOR_FINISHED;
|
||||
}
|
||||
|
||||
static int node_add_file_invoke(bContext *C, wmOperator *op, wmEvent *event)
|
||||
{
|
||||
ARegion *ar= CTX_wm_region(C);
|
||||
SpaceNode *snode= CTX_wm_space_node(C);
|
||||
|
||||
/* convert mouse coordinates to v2d space */
|
||||
UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin,
|
||||
&snode->mx, &snode->my);
|
||||
|
||||
if (RNA_property_is_set(op->ptr, "path") || RNA_property_is_set(op->ptr, "name"))
|
||||
return node_add_file_exec(C, op);
|
||||
else
|
||||
return WM_operator_filesel(C, op, event);
|
||||
}
|
||||
|
||||
void NODE_OT_add_file(wmOperatorType *ot)
|
||||
{
|
||||
/* identifiers */
|
||||
ot->name= "Add File Node";
|
||||
ot->description= "Add a file node to the current node editor";
|
||||
ot->idname= "NODE_OT_add_file";
|
||||
|
||||
/* callbacks */
|
||||
ot->exec= node_add_file_exec;
|
||||
ot->invoke= node_add_file_invoke;
|
||||
ot->poll= ED_operator_node_active;
|
||||
|
||||
/* flags */
|
||||
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
|
||||
|
||||
WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE);
|
||||
RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Datablock name to assign.");
|
||||
}
|
||||
|
||||
|
@ -108,6 +108,8 @@ void NODE_OT_read_renderlayers(struct wmOperatorType *ot);
|
||||
void NODE_OT_backimage_move(struct wmOperatorType *ot);
|
||||
void NODE_OT_backimage_zoom(struct wmOperatorType *ot);
|
||||
|
||||
void NODE_OT_add_file(struct wmOperatorType *ot);
|
||||
|
||||
// XXXXXX
|
||||
|
||||
// XXX from BSE_node.h
|
||||
|
@ -77,6 +77,8 @@ void node_operatortypes(void)
|
||||
|
||||
WM_operatortype_append(NODE_OT_backimage_move);
|
||||
WM_operatortype_append(NODE_OT_backimage_zoom);
|
||||
|
||||
WM_operatortype_append(NODE_OT_add_file);
|
||||
}
|
||||
|
||||
void ED_operatormacros_node(void)
|
||||
|
@ -53,6 +53,7 @@
|
||||
#include "WM_api.h"
|
||||
#include "WM_types.h"
|
||||
|
||||
#include "UI_resources.h"
|
||||
#include "UI_view2d.h"
|
||||
|
||||
#include "RNA_access.h"
|
||||
@ -263,6 +264,7 @@ static void node_buttons_area_draw(const bContext *C, ARegion *ar)
|
||||
static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
|
||||
{
|
||||
wmKeyMap *keymap;
|
||||
ListBase *lb;
|
||||
|
||||
UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
|
||||
|
||||
@ -272,6 +274,11 @@ static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
|
||||
|
||||
keymap= WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
|
||||
WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
|
||||
|
||||
/* add drop boxes */
|
||||
lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
|
||||
|
||||
WM_event_add_dropbox_handler(&ar->handlers, lb);
|
||||
}
|
||||
|
||||
static void node_main_area_draw(const bContext *C, ARegion *ar)
|
||||
@ -281,6 +288,47 @@ static void node_main_area_draw(const bContext *C, ARegion *ar)
|
||||
drawnodespace(C, ar, v2d);
|
||||
}
|
||||
|
||||
|
||||
/* ************* dropboxes ************* */
|
||||
|
||||
static int node_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
|
||||
{
|
||||
if(drag->type==WM_DRAG_ID) {
|
||||
ID *id= (ID *)drag->poin;
|
||||
if( GS(id->name)==ID_IM )
|
||||
return 1;
|
||||
}
|
||||
else if(drag->type==WM_DRAG_PATH){
|
||||
if(ELEM(drag->icon, 0, ICON_FILE_IMAGE)) /* rule might not work? */
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
|
||||
{
|
||||
ID *id= (ID *)drag->poin;
|
||||
|
||||
if(id) {
|
||||
RNA_string_set(drop->ptr, "name", id->name+2);
|
||||
}
|
||||
if (drag->path[0]) {
|
||||
RNA_string_set(drop->ptr, "path", drag->path);
|
||||
}
|
||||
}
|
||||
|
||||
/* this region dropbox definition */
|
||||
static void node_dropboxes(void)
|
||||
{
|
||||
ListBase *lb= WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
|
||||
|
||||
WM_dropbox_add(lb, "NODE_OT_add_file", node_drop_poll, node_id_path_drop_copy);
|
||||
|
||||
}
|
||||
|
||||
/* ************* end drop *********** */
|
||||
|
||||
|
||||
/* add handlers, stuff you only do once or on area/region changes */
|
||||
static void node_header_area_init(wmWindowManager *wm, ARegion *ar)
|
||||
{
|
||||
@ -366,6 +414,7 @@ void ED_spacetype_node(void)
|
||||
st->listener= node_area_listener;
|
||||
st->refresh= node_area_refresh;
|
||||
st->context= node_context;
|
||||
st->dropboxes = node_dropboxes;
|
||||
|
||||
/* regions: main window */
|
||||
art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
|
||||
|
@ -453,7 +453,7 @@ static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
|
||||
|
||||
if(id)
|
||||
RNA_string_set(drop->ptr, "name", id->name+2);
|
||||
if(drag->path)
|
||||
if(drag->path[0])
|
||||
RNA_string_set(drop->ptr, "path", drag->path);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user