[Bf-blender-cvs] [1cdf82d] master: Image Py API: Expose 'load_exists' to RNA image load(), and extend load_image() helper.

Bastien Montagne noreply at git.blender.org
Mon Oct 5 18:59:01 CEST 2015


Commit: 1cdf82d7f8dd117441405b99f744e2f7095e5c7c
Author: Bastien Montagne
Date:   Mon Oct 5 18:49:20 2015 +0200
Branches: master
https://developer.blender.org/rB1cdf82d7f8dd117441405b99f744e2f7095e5c7c

Image Py API: Expose 'load_exists' to RNA image load(), and extend load_image() helper.

Expose our `BKE_image_load_exists` feature through an optional parameter to `Image.load()`.

Extend `image_utils.load_image()` with two optional parameters, to return existing image datablock
if possible, and in that case, to force reloading said image.

Needed by incomming 'import images as planes' addon enhancement.

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

M	release/scripts/modules/bpy_extras/image_utils.py
M	source/blender/makesrna/intern/rna_main_api.c

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

diff --git a/release/scripts/modules/bpy_extras/image_utils.py b/release/scripts/modules/bpy_extras/image_utils.py
index ff6d23b..ad774cd 100644
--- a/release/scripts/modules/bpy_extras/image_utils.py
+++ b/release/scripts/modules/bpy_extras/image_utils.py
@@ -32,6 +32,8 @@ def load_image(imagepath,
                convert_callback=None,
                verbose=False,
                relpath=None,
+               check_existing=False,
+               force_reload=False,
                ):
     """
     Return an image from the file path with options to search multiple paths
@@ -60,6 +62,12 @@ def load_image(imagepath,
     :type convert_callback: function
     :arg relpath: If not None, make the file relative to this path.
     :type relpath: None or string
+    :arg check_existing: If true, returns already loaded image datablock if possible
+       (based on file path).
+    :type check_existing: bool
+    :arg force_reload: If true, force reloading of image (only useful when `check_existing`
+        is also enabled).
+    :type force_reload: bool
     :return: an image or None
     :rtype: :class:`bpy.types.Image`
     """
@@ -86,7 +94,7 @@ def load_image(imagepath,
             path = convert_callback(path)
 
         try:
-            image = bpy.data.images.load(path)
+            image = bpy.data.images.load(path, check_existing)
         except RuntimeError:
             image = None
 
@@ -102,6 +110,8 @@ def load_image(imagepath,
             image = _image_load_placeholder(path)
 
         if image:
+            if force_reload:
+                image.reload()
             if relpath is not None:
                 # make relative
                 from bpy.path import relpath as relpath_fn
diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c
index b38f4fa..dc7987c 100644
--- a/source/blender/makesrna/intern/rna_main_api.c
+++ b/source/blender/makesrna/intern/rna_main_api.c
@@ -352,12 +352,17 @@ static Image *rna_Main_images_new(Main *bmain, const char *name, int width, int
 	id_us_min(&image->id);
 	return image;
 }
-static Image *rna_Main_images_load(Main *bmain, ReportList *reports, const char *filepath)
+static Image *rna_Main_images_load(Main *bmain, ReportList *reports, const char *filepath, int check_existing)
 {
 	Image *ima;
 
 	errno = 0;
-	ima = BKE_image_load(bmain, filepath);
+	if (check_existing) {
+		ima = BKE_image_load_exists(filepath);
+	}
+	else {
+		ima = BKE_image_load(bmain, filepath);
+	}
 
 	if (!ima) {
 		BKE_reportf(reports, RPT_ERROR, "Cannot read '%s': %s", filepath,
@@ -1207,6 +1212,8 @@ void RNA_def_main_images(BlenderRNA *brna, PropertyRNA *cprop)
 	RNA_def_function_ui_description(func, "Load a new image into the main database");
 	parm = RNA_def_string_file_path(func, "filepath", "File Path", 0, "", "path of the file to load");
 	RNA_def_property_flag(parm, PROP_REQUIRED);
+	RNA_def_boolean(func, "check_existing", false, "",
+	                "Check whether this image filepath is already used, and return existing datablock in this case");
 	/* return type */
 	parm = RNA_def_pointer(func, "image", "Image", "", "New image datablock");
 	RNA_def_function_return(func, parm);




More information about the Bf-blender-cvs mailing list