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.
This commit is contained in:
Bastien Montagne 2015-10-05 18:49:20 +02:00
parent e5552f8241
commit 1cdf82d7f8
2 changed files with 20 additions and 3 deletions

@ -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

@ -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);