2011-01-11 02:49:01 +00:00
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
# <pep8-80 compliant>
|
2011-01-11 02:49:01 +00:00
|
|
|
|
2011-05-28 07:47:58 +00:00
|
|
|
__all__ = (
|
2011-05-28 09:34:45 +00:00
|
|
|
"load_image",
|
2011-07-31 03:15:37 +00:00
|
|
|
)
|
2011-05-28 07:47:58 +00:00
|
|
|
|
2011-06-21 17:17:51 +00:00
|
|
|
|
2011-05-28 09:34:45 +00:00
|
|
|
# limited replacement for BPyImage.comprehensiveImageLoad
|
|
|
|
def load_image(imagepath,
|
|
|
|
dirname="",
|
|
|
|
place_holder=False,
|
|
|
|
recursive=False,
|
|
|
|
ncase_cmp=True,
|
|
|
|
convert_callback=None,
|
|
|
|
verbose=False,
|
2013-04-05 00:30:32 +00:00
|
|
|
relpath=None,
|
2011-05-28 09:34:45 +00:00
|
|
|
):
|
|
|
|
"""
|
2011-07-31 03:15:37 +00:00
|
|
|
Return an image from the file path with options to search multiple paths
|
|
|
|
and return a placeholder if its not found.
|
2011-01-13 23:00:51 +00:00
|
|
|
|
2011-05-28 09:34:45 +00:00
|
|
|
:arg filepath: The image filename
|
|
|
|
If a path precedes it, this will be searched as well.
|
|
|
|
:type filepath: string
|
|
|
|
:arg dirname: is the directory where the image may be located - any file at
|
|
|
|
the end will be ignored.
|
|
|
|
:type dirname: string
|
|
|
|
:arg place_holder: if True a new place holder image will be created.
|
2011-10-17 06:58:07 +00:00
|
|
|
this is useful so later you can relink the image to its original data.
|
2011-05-28 09:34:45 +00:00
|
|
|
:type place_holder: bool
|
2011-10-17 06:58:07 +00:00
|
|
|
:arg recursive: If True, directories will be recursively searched.
|
|
|
|
Be careful with this if you have files in your root directory because
|
2011-05-28 09:34:45 +00:00
|
|
|
it may take a long time.
|
|
|
|
:type recursive: bool
|
|
|
|
:arg ncase_cmp: on non windows systems, find the correct case for the file.
|
|
|
|
:type ncase_cmp: bool
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg convert_callback: a function that takes an existing path and returns
|
|
|
|
a new one. Use this when loading image formats blender may not support,
|
|
|
|
the CONVERT_CALLBACK can take the path for a GIF (for example),
|
|
|
|
convert it to a PNG and return the PNG's path.
|
2011-05-28 09:34:45 +00:00
|
|
|
For formats blender can read, simply return the path that is given.
|
|
|
|
:type convert_callback: function
|
2013-04-05 00:30:32 +00:00
|
|
|
:arg relpath: If not None, make the file relative to this path.
|
|
|
|
:type relpath: None or string
|
2011-05-28 09:34:45 +00:00
|
|
|
:return: an image or None
|
2011-08-26 18:48:48 +00:00
|
|
|
:rtype: :class:`bpy.types.Image`
|
2011-05-28 09:34:45 +00:00
|
|
|
"""
|
2011-05-28 07:47:58 +00:00
|
|
|
import os
|
2011-05-30 12:19:30 +00:00
|
|
|
import bpy
|
2011-05-28 07:47:58 +00:00
|
|
|
|
2011-12-10 01:01:22 +00:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Utility Functions
|
|
|
|
|
|
|
|
def _image_load_placeholder(path):
|
|
|
|
name = bpy.path.basename(path)
|
|
|
|
if type(name) == bytes:
|
2012-06-19 22:17:19 +00:00
|
|
|
name = name.decode("utf-8", "replace")
|
2011-12-10 01:01:22 +00:00
|
|
|
image = bpy.data.images.new(name, 128, 128)
|
|
|
|
# allow the path to be resolved later
|
|
|
|
image.filepath = path
|
|
|
|
image.source = 'FILE'
|
|
|
|
return image
|
|
|
|
|
2011-05-28 09:34:45 +00:00
|
|
|
def _image_load(path):
|
|
|
|
import bpy
|
|
|
|
|
|
|
|
if convert_callback:
|
|
|
|
path = convert_callback(path)
|
|
|
|
|
2011-12-10 01:01:22 +00:00
|
|
|
try:
|
|
|
|
image = bpy.data.images.load(path)
|
|
|
|
except RuntimeError:
|
|
|
|
image = None
|
2011-05-28 09:34:45 +00:00
|
|
|
|
|
|
|
if verbose:
|
2011-12-10 01:01:22 +00:00
|
|
|
if image:
|
|
|
|
print(" image loaded '%s'" % path)
|
|
|
|
else:
|
|
|
|
print(" image load failed '%s'" % path)
|
|
|
|
|
|
|
|
# image path has been checked so the path could not be read for some
|
|
|
|
# reason, so be sure to return a placeholder
|
2012-02-16 04:08:52 +00:00
|
|
|
if place_holder and image is None:
|
2011-12-10 01:01:22 +00:00
|
|
|
image = _image_load_placeholder(path)
|
2011-05-28 09:34:45 +00:00
|
|
|
|
2013-04-05 00:30:32 +00:00
|
|
|
if image:
|
|
|
|
if relpath is not None:
|
|
|
|
# make relative
|
|
|
|
from bpy.path import relpath as relpath_fn
|
2013-08-15 00:32:12 +00:00
|
|
|
# can't always find the relative path
|
|
|
|
# (between drive letters on windows)
|
|
|
|
try:
|
|
|
|
filepath_rel = relpath_fn(path, start=relpath)
|
|
|
|
except ValueError:
|
|
|
|
filepath_rel = None
|
|
|
|
|
|
|
|
if filepath_rel is not None:
|
|
|
|
image.filepath_raw = filepath_rel
|
2013-04-05 00:30:32 +00:00
|
|
|
|
2011-05-28 09:34:45 +00:00
|
|
|
return image
|
|
|
|
|
2013-08-12 07:48:31 +00:00
|
|
|
def _recursive_search(paths, filename_check):
|
|
|
|
for path in paths:
|
|
|
|
for dirpath, dirnames, filenames in os.walk(path):
|
|
|
|
|
|
|
|
# skip '.svn'
|
|
|
|
if dirpath[0] in {".", b'.'}:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
if filename_check(filename):
|
|
|
|
yield os.path.join(dirpath, filename)
|
|
|
|
|
2011-12-10 01:01:22 +00:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2011-05-28 09:34:45 +00:00
|
|
|
if verbose:
|
|
|
|
print("load_image('%s', '%s', ...)" % (imagepath, dirname))
|
|
|
|
|
|
|
|
if os.path.exists(imagepath):
|
|
|
|
return _image_load(imagepath)
|
|
|
|
|
|
|
|
variants = [imagepath]
|
|
|
|
|
|
|
|
if dirname:
|
2011-07-20 08:10:01 +00:00
|
|
|
variants += [os.path.join(dirname, imagepath),
|
|
|
|
os.path.join(dirname, bpy.path.basename(imagepath)),
|
|
|
|
]
|
2011-05-28 09:34:45 +00:00
|
|
|
|
|
|
|
for filepath_test in variants:
|
|
|
|
if ncase_cmp:
|
2011-07-31 03:15:37 +00:00
|
|
|
ncase_variants = (filepath_test,
|
|
|
|
bpy.path.resolve_ncase(filepath_test),
|
|
|
|
)
|
2011-05-28 09:34:45 +00:00
|
|
|
else:
|
|
|
|
ncase_variants = (filepath_test, )
|
|
|
|
|
|
|
|
for nfilepath in ncase_variants:
|
|
|
|
if os.path.exists(nfilepath):
|
|
|
|
return _image_load(nfilepath)
|
|
|
|
|
2013-08-12 07:48:31 +00:00
|
|
|
if recursive:
|
|
|
|
search_paths = []
|
|
|
|
|
|
|
|
for dirpath_test in (os.path.dirname(imagepath), dirname):
|
|
|
|
if os.path.exists(dirpath_test):
|
|
|
|
search_paths.append(dirpath_test)
|
|
|
|
search_paths[:] = bpy.path.reduce_dirs(search_paths)
|
|
|
|
|
|
|
|
imagepath_base = bpy.path.basename(imagepath)
|
|
|
|
if ncase_cmp:
|
|
|
|
imagepath_base = imagepath_base.lower()
|
|
|
|
|
|
|
|
def image_filter(fn):
|
|
|
|
return (imagepath_base == fn.lower())
|
|
|
|
else:
|
|
|
|
def image_filter(fn):
|
|
|
|
return (imagepath_base == fn)
|
|
|
|
|
|
|
|
nfilepath = next(_recursive_search(search_paths, image_filter), None)
|
|
|
|
if nfilepath is not None:
|
|
|
|
return _image_load(nfilepath)
|
|
|
|
|
2011-12-10 01:01:22 +00:00
|
|
|
# None of the paths exist so return placeholder
|
2011-05-28 09:34:45 +00:00
|
|
|
if place_holder:
|
2011-12-10 01:01:22 +00:00
|
|
|
return _image_load_placeholder(imagepath)
|
2011-05-28 09:34:45 +00:00
|
|
|
|
|
|
|
# TODO comprehensiveImageLoad also searched in bpy.config.textureDir
|
|
|
|
return None
|