diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2019-05-16 17:01:11 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2019-05-16 17:57:38 +0300 |
commit | aac95aa1e9727f145e258561816baac80b915f20 (patch) | |
tree | ae10bc800391cf879acbcfdd8132eb9397640c36 /source/blender/editors | |
parent | c1ec6f00f278d823a7208c4cc3e44f58e20455a3 (diff) |
Images: move save modified images operator from Python to C
We will use this for saving images along with the .blend file.
Ref D4861
Diffstat (limited to 'source/blender/editors')
-rw-r--r-- | source/blender/editors/include/ED_image.h | 4 | ||||
-rw-r--r-- | source/blender/editors/space_image/image_intern.h | 1 | ||||
-rw-r--r-- | source/blender/editors/space_image/image_ops.c | 120 | ||||
-rw-r--r-- | source/blender/editors/space_image/space_image.c | 1 |
4 files changed, 125 insertions, 1 deletions
diff --git a/source/blender/editors/include/ED_image.h b/source/blender/editors/include/ED_image.h index 5bd806b3dbf..a9be9598761 100644 --- a/source/blender/editors/include/ED_image.h +++ b/source/blender/editors/include/ED_image.h @@ -28,6 +28,7 @@ struct ARegion; struct ImBuf; struct Image; struct ImageUser; +struct ReportList; struct Scene; struct SpaceImage; struct ToolSettings; @@ -110,4 +111,7 @@ void ED_image_draw_info(struct Scene *scene, bool ED_space_image_show_cache(struct SpaceImage *sima); +int ED_image_save_all_modified_count(const struct bContext *C); +bool ED_image_save_all_modified(const struct bContext *C, struct ReportList *reports); + #endif /* __ED_IMAGE_H__ */ diff --git a/source/blender/editors/space_image/image_intern.h b/source/blender/editors/space_image/image_intern.h index a851684f2f3..2c723f45e94 100644 --- a/source/blender/editors/space_image/image_intern.h +++ b/source/blender/editors/space_image/image_intern.h @@ -66,6 +66,7 @@ void IMAGE_OT_reload(struct wmOperatorType *ot); void IMAGE_OT_save(struct wmOperatorType *ot); void IMAGE_OT_save_as(struct wmOperatorType *ot); void IMAGE_OT_save_sequence(struct wmOperatorType *ot); +void IMAGE_OT_save_all_modified(struct wmOperatorType *ot); void IMAGE_OT_pack(struct wmOperatorType *ot); void IMAGE_OT_unpack(struct wmOperatorType *ot); diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c index c317cb26cb7..3dd7dd26320 100644 --- a/source/blender/editors/space_image/image_ops.c +++ b/source/blender/editors/space_image/image_ops.c @@ -34,8 +34,10 @@ #include "MEM_guardedalloc.h" -#include "BLI_math.h" #include "BLI_blenlib.h" +#include "BLI_ghash.h" +#include "BLI_math.h" +#include "BLI_string.h" #include "BLI_utildefines.h" #include "BLT_translation.h" @@ -2157,6 +2159,122 @@ void IMAGE_OT_save_sequence(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } +/********************** save all operator **********************/ + +int ED_image_save_all_modified_count(const bContext *C) +{ + Main *bmain = CTX_data_main(C); + int num_files = 0; + + for (Image *ima = bmain->images.first; ima; ima = ima->id.next) { + if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) { + continue; + } + else if (BKE_image_is_dirty(ima)) { + if (ima->source == IMA_SRC_FILE && !BKE_image_has_packedfile(ima)) { + num_files++; + } + } + } + + return num_files; +} + +bool ED_image_save_all_modified(const bContext *C, ReportList *reports) +{ + Main *bmain = CTX_data_main(C); + Scene *scene = CTX_data_scene(C); + GSet *unique_paths = BLI_gset_str_new(__func__); + bool ok = true; + + for (Image *ima = bmain->images.first; ima; ima = ima->id.next) { + if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) { + /* Don't save render results automatically. */ + } + else if (BKE_image_is_dirty(ima) && (ima->source == IMA_SRC_FILE)) { + if (BKE_image_has_packedfile(ima)) { + if (ima->id.lib == NULL) { + /* Re-pack. */ + BKE_image_memorypack(ima); + } + else { + /* Can't pack to library data. */ + BKE_reportf(reports, + RPT_ERROR, + "Packed library image: %s from library %s can't be saved", + ima->id.name, + ima->id.lib->name); + } + } + else { + /* Save to file. */ + const bool valid_path = strchr(ima->name, '\\') || strchr(ima->name, '/'); + + if (valid_path) { + ImageSaveOptions opts; + + BKE_image_save_options_init(&opts, bmain, scene); + + if (image_save_options_init(bmain, &opts, ima, NULL, false, false)) { + if (!BLI_gset_haskey(unique_paths, opts.filepath)) { + const bool save_ok = BKE_image_save(reports, bmain, ima, NULL, &opts); + + if (save_ok) { + BLI_gset_insert(unique_paths, BLI_strdup(opts.filepath)); + } + + ok = ok && save_ok; + } + else { + BKE_reportf(reports, + RPT_WARNING, + "File path used by more than one saved image: %s", + opts.filepath); + } + } + } + else { + BKE_reportf(reports, + RPT_ERROR, + "Image %s can't be saved, no valid file path: %s", + ima->id.name, + ima->name); + } + } + } + } + + BLI_gset_free(unique_paths, MEM_freeN); + + return ok; +} + +static int image_save_all_modified_exec(bContext *C, wmOperator *op) +{ + ED_image_save_all_modified(C, op->reports); + return OPERATOR_FINISHED; +} + +static bool image_save_all_modified_poll(bContext *C) +{ + return (ED_image_save_all_modified_count(C) > 0); +} + +void IMAGE_OT_save_all_modified(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Save All Modified"; + ot->idname = "IMAGE_OT_save_all_modified"; + ot->description = "Save all modified images"; + + /* api callbacks */ + ot->exec = image_save_all_modified_exec; + ot->poll = image_save_all_modified_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + /******************** reload image operator ********************/ static int image_reload_exec(bContext *C, wmOperator *UNUSED(op)) diff --git a/source/blender/editors/space_image/space_image.c b/source/blender/editors/space_image/space_image.c index cbe655fc9ae..a8be93ad213 100644 --- a/source/blender/editors/space_image/space_image.c +++ b/source/blender/editors/space_image/space_image.c @@ -224,6 +224,7 @@ static void image_operatortypes(void) WM_operatortype_append(IMAGE_OT_save); WM_operatortype_append(IMAGE_OT_save_as); WM_operatortype_append(IMAGE_OT_save_sequence); + WM_operatortype_append(IMAGE_OT_save_all_modified); WM_operatortype_append(IMAGE_OT_pack); WM_operatortype_append(IMAGE_OT_unpack); |