Commit: 929dbc66445ba2004be52bd29f5bd06bcd1f8a26
Author: Campbell Barton
Date:   Sun Nov 23 22:48:48 2014 +0100
Branches: master
https://developer.blender.org/rB929dbc66445ba2004be52bd29f5bd06bcd1f8a26

Cleanup: de-duplicate image drag & drop logic

also don't make library paths relative on image load.

===================================================================

M       source/blender/editors/mesh/mesh_data.c
M       source/blender/editors/object/object_add.c
M       source/blender/editors/space_image/image_ops.c
M       source/blender/editors/space_node/node_add.c
M       source/blender/editors/space_view3d/view3d_edit.c
M       source/blender/windowmanager/WM_api.h
M       source/blender/windowmanager/intern/wm_operators.c

===================================================================

diff --git a/source/blender/editors/mesh/mesh_data.c 
b/source/blender/editors/mesh/mesh_data.c
index e2ce97a..4bffb49 100644
--- a/source/blender/editors/mesh/mesh_data.c
+++ b/source/blender/editors/mesh/mesh_data.c
@@ -571,24 +571,13 @@ static int drop_named_image_invoke(bContext *C, 
wmOperator *op, const wmEvent *e
                return OPERATOR_CANCELLED;
        }
        
-       /* check input variables */
-       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
-               char path[FILE_MAX];
-               
-               RNA_string_get(op->ptr, "filepath", path);
-               ima = BKE_image_load_exists(path);
-       }
-       else {
-               char name[MAX_ID_NAME - 2];
-               RNA_string_get(op->ptr, "name", name);
-               ima = (Image *)BKE_libblock_find_name(ID_IM, name);
-       }
-       
+       ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
        if (!ima) {
-               BKE_report(op->reports, RPT_ERROR, "Not an image");
                return OPERATOR_CANCELLED;
        }
-       
+       /* handled below */
+       id_us_min((ID *)ima);
+
        /* put mesh in editmode */
 
        obedit = base->object;
diff --git a/source/blender/editors/object/object_add.c 
b/source/blender/editors/object/object_add.c
index 24dcf85..8cea1ce 100644
--- a/source/blender/editors/object/object_add.c
+++ b/source/blender/editors/object/object_add.c
@@ -833,24 +833,12 @@ static int empty_drop_named_image_invoke(bContext *C, 
wmOperator *op, const wmEv
        Image *ima = NULL;
        Object *ob = NULL;
 
-       /* check image input variables */
-       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
-               char path[FILE_MAX];
-
-               RNA_string_get(op->ptr, "filepath", path);
-               ima = BKE_image_load_exists(path);
-       }
-       else if (RNA_struct_property_is_set(op->ptr, "name")) {
-               char name[MAX_ID_NAME - 2];
-
-               RNA_string_get(op->ptr, "name", name);
-               ima = (Image *)BKE_libblock_find_name(ID_IM, name);
-       }
-
-       if (ima == NULL) {
-               BKE_report(op->reports, RPT_ERROR, "Not an image");
+       ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
+       if (!ima) {
                return OPERATOR_CANCELLED;
        }
+       /* handled below */
+       id_us_min((ID *)ima);
 
        base = ED_view3d_give_base_under_cursor(C, event->mval);
 
diff --git a/source/blender/editors/space_image/image_ops.c 
b/source/blender/editors/space_image/image_ops.c
index 2a03dc6..50aee93 100644
--- a/source/blender/editors/space_image/image_ops.c
+++ b/source/blender/editors/space_image/image_ops.c
@@ -1052,6 +1052,7 @@ static int image_open_exec(bContext *C, wmOperator *op)
        char path[FILE_MAX];
        int frame_seq_len = 0;
        int frame_ofs = 1;
+       bool exists = false;
 
        const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
 
@@ -1070,7 +1071,7 @@ static int image_open_exec(bContext *C, wmOperator *op)
 
        errno = 0;
 
-       ima = BKE_image_load_exists(path);
+       ima = BKE_image_load_exists_ex(path, &exists);
 
        if (!ima) {
                if (op->customdata) MEM_freeN(op->customdata);
@@ -1084,8 +1085,9 @@ static int image_open_exec(bContext *C, wmOperator *op)
 
        /* only image path after save, never ibuf */
        if (is_relative_path) {
-               const char *relbase = ID_BLEND_PATH(bmain, &ima->id);
-               BLI_path_rel(ima->name, relbase);
+               if (!exists) {
+                       BLI_path_rel(ima->name, bmain->name);
+               }
        }
 
        /* hook into UI */
diff --git a/source/blender/editors/space_node/node_add.c 
b/source/blender/editors/space_node/node_add.c
index a22ccac..1e7fffa 100644
--- a/source/blender/editors/space_node/node_add.c
+++ b/source/blender/editors/space_node/node_add.c
@@ -29,8 +29,6 @@
  *  \ingroup spnode
  */
 
-#include <errno.h>
-
 #include "MEM_guardedalloc.h"
 
 #include "DNA_node_types.h"
@@ -305,43 +303,12 @@ static int node_add_file_exec(bContext *C, wmOperator *op)
 {
        SpaceNode *snode = CTX_wm_space_node(C);
        bNode *node;
-       Image *ima = NULL;
+       Image *ima;
        int type = 0;
-       bool exists = false;
-
-       const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
-
-       /* check input variables */
-       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
-               char path[FILE_MAX];
-               RNA_string_get(op->ptr, "filepath", path);
-
-               errno = 0;
-
-               ima = BKE_image_load_exists_ex(path, &exists);
-
-               if (!ima) {
-                       BKE_reportf(op->reports, RPT_ERROR, "Cannot read image 
'%s': %s",
-                                   path, errno ? strerror(errno) : 
TIP_("unsupported format"));
-                       return OPERATOR_CANCELLED;
-               }
-
-               if (is_relative_path) {
-                       if (exists == false) {
-                               Main *bmain = CTX_data_main(C);
-                               BLI_path_rel(ima->name, bmain->name);
-                       }
-               }
-       }
-       else if (RNA_struct_property_is_set(op->ptr, "name")) {
-               char name[MAX_ID_NAME - 2];
-               RNA_string_get(op->ptr, "name", name);
-               ima = (Image *)BKE_libblock_find_name(ID_IM, name);
 
-               if (!ima) {
-                       BKE_reportf(op->reports, RPT_ERROR, "Image '%s' not 
found", name);
-                       return OPERATOR_CANCELLED;
-               }
+       ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
+       if (!ima) {
+               return OPERATOR_CANCELLED;
        }
 
        switch (snode->nodetree->type) {
diff --git a/source/blender/editors/space_view3d/view3d_edit.c 
b/source/blender/editors/space_view3d/view3d_edit.c
index 4e997d0..aa0e6e5 100644
--- a/source/blender/editors/space_view3d/view3d_edit.c
+++ b/source/blender/editors/space_view3d/view3d_edit.c
@@ -4282,41 +4282,16 @@ static int background_image_add_invoke(bContext *C, 
wmOperator *op, const wmEven
        View3D *v3d = CTX_wm_view3d(C);
        Image *ima = NULL;
        BGpic *bgpic;
-       char name[MAX_ID_NAME - 2];
-       bool exists = false;
-
-       const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
        
-       /* check input variables */
-       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
-               char path[FILE_MAX];
-               
-               RNA_string_get(op->ptr, "filepath", path);
-               ima = BKE_image_load_exists_ex(path, &exists);
-       }
-       else if (RNA_struct_property_is_set(op->ptr, "name")) {
-               RNA_string_get(op->ptr, "name", name);
-               ima = (Image *)BKE_libblock_find_name(ID_IM, name);
-               exists = true;
+       ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
+       if (!ima) {
+               return OPERATOR_CANCELLED;
        }
-       
+
        bgpic = background_image_add(C);
-       
-       if (ima) {
-               if (is_relative_path) {
-                       if (exists == false) {
-                               Main *bmain = CTX_data_main(C);
-                               BLI_path_rel(ima->name, bmain->name);
-                       }
-               }
+       bgpic->ima = ima;
 
-               bgpic->ima = ima;
-               
-               id_us_plus(&ima->id);
-               
-               if (!(v3d->flag & V3D_DISPBGPICS))
-                       v3d->flag |= V3D_DISPBGPICS;
-       }
+       v3d->flag |= V3D_DISPBGPICS;
        
        WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
        
diff --git a/source/blender/windowmanager/WM_api.h 
b/source/blender/windowmanager/WM_api.h
index a580260..d626c91 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -268,6 +268,7 @@ void                
WM_operator_properties_select_action_simple(struct wmOperatorType *ot, int
 
 bool        WM_operator_check_ui_enabled(const struct bContext *C, const char 
*idname);
 wmOperator *WM_operator_last_redo(const struct bContext *C);
+ID         *WM_operator_drop_load_path(struct bContext *C, struct wmOperator 
*op, const short idcode);
 
 bool        WM_operator_last_properties_init(struct wmOperator *op);
 bool        WM_operator_last_properties_store(struct wmOperator *op);
diff --git a/source/blender/windowmanager/intern/wm_operators.c 
b/source/blender/windowmanager/intern/wm_operators.c
index 598175a..7961619 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -38,6 +38,7 @@
 #include <stdio.h>
 #include <stddef.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "GHOST_C-api.h"
 
@@ -1400,6 +1401,64 @@ wmOperator *WM_operator_last_redo(const bContext *C)
        return op;
 }
 
+/**
+ * Use for drag & drop a path or name with opeators invoke() function.
+ */
+ID *WM_operator_drop_load_path(struct bContext *C, wmOperator *op, const short 
idcode)
+{
+       ID *id = NULL;
+       /* check input variables */
+       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
+               const bool is_relative_path = RNA_boolean_get(op->ptr, 
"relative_path");
+               char path[FILE_MAX];
+               bool exists = false;
+
+               RNA_string_get(op->ptr, "filepath", path);
+
+               errno = 0;
+
+               if (idcode == ID_IM) {
+                       id = (ID *)BKE_image_load_exists_ex(path, &exists);
+               }
+               else {
+                       BLI_assert(0);
+               }
+
+               if (!id) {
+                       BKE_reportf(op->reports, RPT_ERROR, "Cannot read %s 
'%s': %s",
+                                   BKE_idcode_to_name(idcode), path,
+                                   errno ? strerror(errno) : TIP_("unsupported 
format"));
+                       return NULL;
+               }
+
+               if (is_relative_path ) {
+                       if (exists == false) {
+                               Main *bmain = CTX_data_main(C);
+
+                               if (idcode == ID_IM) {
+                                       BLI_path_rel(((Image *)id)->name, 
bmain->name);
+                               }
+                               else {
+                                       BLI_assert(0);
+                               }
+                       }
+               }
+       }
+       else if (RNA_struct_property_is_set(op->ptr, "name")) {
+               char name[MAX_ID_NAME - 2];
+               RNA_string_get(op->ptr, "name", name);
+               id = BKE_libblock_find_name(idcode, name);
+               if (!id) {
+                       BKE_reportf(op->reports, RPT_ERROR, "%s '%s' not found",
+                                   BKE_idcode_to_name(idcode), name);
+                       return NULL;
+               }
+               id_us_plus(id);
+       }
+
+       return id;
+}
+
 static void wm_block_redo_cb(bContext *C, void *arg_op, int UNUSED(arg_event))
 {
        wmOperator *op = arg_op;

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to