diff --git a/release/scripts/modules/bpy_extras/image_utils.py b/release/scripts/modules/bpy_extras/image_utils.py index ff6d23badb6..ad774cd1bda 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 b38f4fa67b6..dc7987c77e6 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);