1
# ##### BEGIN GPL LICENSE BLOCK #####
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License
5
# as published by the Free Software Foundation; either version 2
6
# of the License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software Foundation,
15
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
# ##### END GPL LICENSE BLOCK #####
26
# limited replacement for BPyImage.comprehensiveImageLoad
27
def load_image(imagepath,
32
convert_callback=None,
36
Return an image from the file path with options to search multiple paths
37
and return a placeholder if its not found.
39
:arg filepath: The image filename
40
If a path precedes it, this will be searched as well.
41
:type filepath: string
42
:arg dirname: is the directory where the image may be located - any file at
43
the end will be ignored.
45
:arg place_holder: if True a new place holder image will be created.
46
this is useful so later you can relink the image to its original data.
47
:type place_holder: bool
48
:arg recursive: If True, directories will be recursively searched.
49
Be careful with this if you have files in your root directory because
50
it may take a long time.
52
:arg ncase_cmp: on non windows systems, find the correct case for the file.
54
:arg convert_callback: a function that takes an existing path and returns
55
a new one. Use this when loading image formats blender may not support,
56
the CONVERT_CALLBACK can take the path for a GIF (for example),
57
convert it to a PNG and return the PNG's path.
58
For formats blender can read, simply return the path that is given.
59
:type convert_callback: function
60
:return: an image or None
61
:rtype: :class:`bpy.types.Image`
68
# -------------------------------------------------------------------------
71
def _image_load_placeholder(path):
72
name = bpy.path.basename(path)
73
if type(name) == bytes:
74
name = name.decode('utf-8', "replace")
75
image = bpy.data.images.new(name, 128, 128)
76
# allow the path to be resolved later
81
def _image_load(path):
85
path = convert_callback(path)
88
image = bpy.data.images.load(path)
94
print(" image loaded '%s'" % path)
96
print(" image load failed '%s'" % path)
98
# image path has been checked so the path could not be read for some
99
# reason, so be sure to return a placeholder
100
if place_holder and image is None:
101
image = _image_load_placeholder(path)
105
# -------------------------------------------------------------------------
108
print("load_image('%s', '%s', ...)" % (imagepath, dirname))
110
if os.path.exists(imagepath):
111
return _image_load(imagepath)
113
variants = [imagepath]
116
variants += [os.path.join(dirname, imagepath),
117
os.path.join(dirname, bpy.path.basename(imagepath)),
120
for filepath_test in variants:
122
ncase_variants = (filepath_test,
123
bpy.path.resolve_ncase(filepath_test),
126
ncase_variants = (filepath_test, )
128
for nfilepath in ncase_variants:
129
if os.path.exists(nfilepath):
130
return _image_load(nfilepath)
132
# None of the paths exist so return placeholder
134
return _image_load_placeholder(imagepath)
136
# TODO comprehensiveImageLoad also searched in bpy.config.textureDir