[Bf-blender-cvs] [e8238e1123d] master: Images: make image save operator available outside image editor

Brecht Van Lommel noreply at git.blender.org
Fri May 17 18:00:25 CEST 2019


Commit: e8238e1123d9bab7210b922b4fb2d8f395bf70ef
Author: Brecht Van Lommel
Date:   Fri May 17 15:45:15 2019 +0200
Branches: master
https://developer.blender.org/rBe8238e1123d9bab7210b922b4fb2d8f395bf70ef

Images: make image save operator available outside image editor

This includes some refactoring of image operator poll functions.

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

M	source/blender/editors/space_image/image_ops.c

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

diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c
index eec9870ae45..873eae07901 100644
--- a/source/blender/editors/space_image/image_ops.c
+++ b/source/blender/editors/space_image/image_ops.c
@@ -181,24 +181,59 @@ static void sima_zoom_set_from_bounds(SpaceImage *sima, ARegion *ar, const rctf
   sima_zoom_set(sima, ar, size, NULL);
 }
 
-static bool space_image_buffer_exists_poll(bContext *C)
+static Image *image_from_context(const bContext *C)
 {
-  SpaceImage *sima = CTX_wm_space_image(C);
-  if (sima && ED_space_image_has_buffer(sima)) {
-    return true;
+  /* Edit image is set by templates used throughout the interface, so image
+   * operations work outside the image editor. */
+  Image *ima = CTX_data_pointer_get_type(C, "edit_image", &RNA_Image).data;
+
+  if (ima) {
+    return ima;
+  }
+  else {
+    /* Image editor. */
+    SpaceImage *sima = CTX_wm_space_image(C);
+    return (sima) ? sima->image : NULL;
   }
-  return false;
 }
 
-static bool image_not_packed_poll(bContext *C)
+static ImageUser *image_user_from_context(const bContext *C)
 {
-  SpaceImage *sima = CTX_wm_space_image(C);
+  /* Edit image user is set by templates used throughout the interface, so
+   * image operations work outside the image editor. */
+  ImageUser *iuser = CTX_data_pointer_get_type(C, "edit_image_user", &RNA_ImageUser).data;
 
-  /* Do not run 'replace' on packed images, it does not give user expected results at all. */
-  if (sima && sima->image && BLI_listbase_is_empty(&sima->image->packedfiles)) {
-    return true;
+  if (iuser) {
+    return iuser;
+  }
+  else {
+    /* Image editor. */
+    SpaceImage *sima = CTX_wm_space_image(C);
+    return (sima) ? &sima->iuser : NULL;
   }
-  return false;
+}
+
+static bool image_buffer_exists_from_context(bContext *C)
+{
+  Image *ima = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
+
+  if (ima == NULL) {
+    return false;
+  }
+
+  void *lock;
+  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
+  const bool has_buffer = (ibuf && (ibuf->rect || ibuf->rect_float));
+  BKE_image_release_ibuf(ima, ibuf, lock);
+  return has_buffer;
+}
+
+static bool image_not_packed_poll(bContext *C)
+{
+  /* Do not run 'replace' on packed images, it does not give user expected results at all. */
+  Image *ima = image_from_context(C);
+  return (ima && BLI_listbase_is_empty(&ima->packedfiles));
 }
 
 static bool imbuf_format_writeable(const ImBuf *ibuf)
@@ -211,37 +246,38 @@ static bool imbuf_format_writeable(const ImBuf *ibuf)
 
 static bool space_image_file_exists_poll(bContext *C)
 {
-  if (space_image_buffer_exists_poll(C)) {
-    Main *bmain = CTX_data_main(C);
-    SpaceImage *sima = CTX_wm_space_image(C);
-    ImBuf *ibuf;
-    void *lock;
-    bool ret = false;
-    char name[FILE_MAX];
+  if (image_buffer_exists_from_context(C) == false) {
+    return false;
+  }
 
-    ibuf = ED_space_image_acquire_buffer(sima, &lock);
-    if (ibuf) {
-      BLI_strncpy(name, ibuf->name, FILE_MAX);
-      BLI_path_abs(name, BKE_main_blendfile_path(bmain));
+  Main *bmain = CTX_data_main(C);
+  Image *ima = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
+  void *lock;
+  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
+  bool ret = false;
 
-      if (BLI_exists(name) == false) {
-        CTX_wm_operator_poll_msg_set(C, "image file not found");
-      }
-      else if (!BLI_file_is_writable(name)) {
-        CTX_wm_operator_poll_msg_set(C, "image path can't be written to");
-      }
-      else if (!imbuf_format_writeable(ibuf)) {
-        CTX_wm_operator_poll_msg_set(C, "image format is read-only");
-      }
-      else {
-        ret = true;
-      }
-    }
-    ED_space_image_release_buffer(sima, ibuf, lock);
+  if (ibuf) {
+    char name[FILE_MAX];
+    BLI_strncpy(name, ibuf->name, FILE_MAX);
+    BLI_path_abs(name, BKE_main_blendfile_path(bmain));
 
-    return ret;
+    if (BLI_exists(name) == false) {
+      CTX_wm_operator_poll_msg_set(C, "image file not found");
+    }
+    else if (!BLI_file_is_writable(name)) {
+      CTX_wm_operator_poll_msg_set(C, "image path can't be written to");
+    }
+    else if (!imbuf_format_writeable(ibuf)) {
+      CTX_wm_operator_poll_msg_set(C, "image format is read-only");
+    }
+    else {
+      ret = true;
+    }
   }
-  return false;
+
+  BKE_image_release_ibuf(ima, ibuf, lock);
+  return ret;
 }
 
 bool space_image_main_region_poll(bContext *C)
@@ -272,24 +308,23 @@ static bool space_image_main_area_not_uv_brush_poll(bContext *C)
 static bool image_sample_poll(bContext *C)
 {
   SpaceImage *sima = CTX_wm_space_image(C);
-  if (sima) {
-    Object *obedit = CTX_data_edit_object(C);
-    if (obedit) {
-      /* Disable when UV editing so it doesn't swallow all click events
-       * (use for setting cursor). */
-      if (ED_space_image_show_uvedit(sima, obedit)) {
-        return false;
-      }
-    }
-    else if (sima->mode != SI_MODE_VIEW) {
+  if (sima == NULL) {
+    return false;
+  }
+
+  Object *obedit = CTX_data_edit_object(C);
+  if (obedit) {
+    /* Disable when UV editing so it doesn't swallow all click events
+     * (use for setting cursor). */
+    if (ED_space_image_show_uvedit(sima, obedit)) {
       return false;
     }
-
-    return space_image_main_region_poll(C);
   }
-  else {
+  else if (sima->mode != SI_MODE_VIEW) {
     return false;
   }
+
+  return true;
 }
 /********************** view pan operator *********************/
 
@@ -1506,21 +1541,16 @@ void IMAGE_OT_open(wmOperatorType *ot)
 static int image_match_len_exec(bContext *C, wmOperator *UNUSED(op))
 {
   Scene *scene = CTX_data_scene(C);
-  Image *ima = CTX_data_pointer_get_type(C, "edit_image", &RNA_Image).data;
-  ImageUser *iuser = CTX_data_pointer_get_type(C, "edit_image_user", &RNA_ImageUser).data;
+  Image *ima = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
 
   if (!ima || !iuser) {
     /* Try to get a Texture, or a SpaceImage from context... */
-    SpaceImage *sima = CTX_wm_space_image(C);
     Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
     if (tex && tex->type == TEX_IMAGE) {
       ima = tex->ima;
       iuser = &tex->iuser;
     }
-    else if (sima) {
-      ima = sima->image;
-      iuser = &sima->iuser;
-    }
   }
 
   if (!ima || !iuser || !BKE_image_has_anim(ima)) {
@@ -1795,14 +1825,11 @@ static void image_save_options_to_op(ImageSaveOptions *opts, wmOperator *op)
   RNA_string_set(op->ptr, "filepath", opts->filepath);
 }
 
-static bool save_image_op(const bContext *C,
-                          SpaceImage *sima,
-                          wmOperator *op,
-                          ImageSaveOptions *opts)
+static bool save_image_op(const bContext *C, wmOperator *op, ImageSaveOptions *opts)
 {
   Main *bmain = CTX_data_main(C);
-  Image *ima = ED_space_image(sima);
-  ImageUser *iuser = &sima->iuser;
+  Image *ima = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
 
   opts->relative = (RNA_struct_find_property(op->ptr, "relative_path") &&
                     RNA_boolean_get(op->ptr, "relative_path"));
@@ -1840,19 +1867,20 @@ static int image_save_as_exec(bContext *C, wmOperator *op)
 {
   Main *bmain = CTX_data_main(C);
   Scene *scene = CTX_data_scene(C);
-  SpaceImage *sima = CTX_wm_space_image(C);
+  Image *image = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
   ImageSaveOptions opts;
 
   BKE_image_save_options_init(&opts, bmain, scene);
 
   /* just in case to initialize values,
    * these should be set on invoke or by the caller. */
-  image_save_options_init(bmain, &opts, sima->image, &sima->iuser, false, false);
+  image_save_options_init(bmain, &opts, image, iuser, false, false);
 
   image_save_options_from_op(bmain, &opts, op);
   opts.do_newpath = true;
 
-  save_image_op(C, sima, op, &opts);
+  save_image_op(C, op, &opts);
 
   image_save_as_free(op);
   return OPERATOR_FINISHED;
@@ -1867,8 +1895,8 @@ static bool image_save_as_check(bContext *UNUSED(C), wmOperator *op)
 static int image_save_as_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
 {
   Main *bmain = CTX_data_main(C);
-  SpaceImage *sima = CTX_wm_space_image(C);
-  Image *ima = ED_space_image(sima);
+  Image *ima = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
   Scene *scene = CTX_data_scene(C);
   ImageSaveOptions opts;
   PropertyRNA *prop;
@@ -1881,7 +1909,7 @@ static int image_save_as_invoke(bContext *C, wmOperator *op, const wmEvent *UNUS
 
   BKE_image_save_options_init(&opts, bmain, scene);
 
-  if (image_save_options_init(bmain, &opts, ima, &sima->iuser, true, save_as_render) == 0) {
+  if (image_save_options_init(bmain, &opts, ima, iuser, true, save_as_render) == 0) {
     return OPERATOR_CANCELLED;
   }
   image_save_options_to_op(&opts, op);
@@ -1949,20 +1977,21 @@ static void image_save_as_draw(bContext *UNUSED(C), wmOperator *op)
 
 static bool image_save_as_poll(bContext *C)
 {
-  if (space_image_buffer_exists_poll(C)) {
-    if (G.is_rendering) {
-      /* no need to NULL check here */
-      SpaceImage *sima = CTX_wm_space_image(C);
-      Image *ima = ED_space_image(sima);
+  if (!image_buffer_exists_from_context(C)) {
+    return false;
+  }
 
-      if (ima->source == IMA_SRC_VIEWER) {
-        CTX_wm_operator_poll_msg_set(C, "can't save image while rendering");
-        return false;
-      }
+  if (G.is_rendering) {
+    /* no need to NULL check here */
+    Image *ima = image_from_context(C);
+
+    if (ima->source == IMA_SRC_VIEWER) {
+      CTX_wm_operator_poll_msg_set(C, "can't save image while rendering");
+      return false;
     }
-    return true;
   }
-  return false;
+
+  return true;
 }
 
 void IMAGE_OT_save_as(wmOperatorType *ot)
@@ -2009,18 +2038,19 @@ void IMAGE_OT_save_as(wmOperatorType *ot)
 static int image_save_exec(bContext *C, wmOperator *op)
 {
   Main *bmain = CTX_data_main(C);
-  SpaceImage *sima = CTX_wm_space_image(C);
+  Image *image = image_from_context(C);
+  ImageUser *iuser = image_user_from_context(C);
   Scene *scene = CTX_data_scene(C);
   ImageSaveOptions opts;
 
   

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list