Cleanup: de-duplicate image drag & drop logic
also don't make library paths relative on image load.
This commit is contained in:
parent
888ab78edf
commit
929dbc6644
Notes:
blender-bot
2023-02-14 09:47:10 +01:00
Referenced by issue #42723, Regression: cannot add background image in current master
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
||||
ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
|
||||
if (!ima) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue