Welcome to mirror list, hosted at ThFree Co, Russian Federation.

image_utils.py « bpy_extras « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e56c1c651c449ae80531cf75d7f0256595ad6489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# ##### 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 #####

# <pep8 compliant>

__all__ = (
    "load_image",
)


# limited replacement for BPyImage.comprehensiveImageLoad
def load_image(imagepath,
               dirname="",
               place_holder=False,
               recursive=False,
               ncase_cmp=True,
               convert_callback=None,
               verbose=False,
               ):
    """
    Return an image from the file path with options to search multiple paths and
    return a placeholder if its not found.

    :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.
       this is usefull so later you can relink the image to its original data.
    :type place_holder: bool
    :arg recursive: If True, directories will be recursivly searched.
       Be carefull with this if you have files in your root directory because
       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
    :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.
       For formats blender can read, simply return the path that is given.
    :type convert_callback: function
    :return: an image or None
    :rtype: :class:`Image`
    """
    import os
    import bpy

    # TODO: recursive

    def _image_load(path):
        import bpy

        if convert_callback:
            path = convert_callback(path)

        image = bpy.data.images.load(path)

        if verbose:
            print("    image loaded '%s'" % path)

        return image

    if verbose:
        print("load_image('%s', '%s', ...)" % (imagepath, dirname))

    if os.path.exists(imagepath):
        return _image_load(imagepath)

    variants = [imagepath]

    if dirname:
        variants += [os.path.join(dirname, imagepath),
                     os.path.join(dirname, bpy.path.basename(imagepath)),
                     ]

    for filepath_test in variants:
        if ncase_cmp:
            ncase_variants = filepath_test, bpy.path.resolve_ncase(filepath_test)
        else:
            ncase_variants = (filepath_test, )

        for nfilepath in ncase_variants:
            if os.path.exists(nfilepath):
                return _image_load(nfilepath)

    if place_holder:
        image = bpy.data.images.new(bpy.path.basename(imagepath), 128, 128)
        # allow the path to be resolved later
        image.filepath = imagepath
        return image

    # TODO comprehensiveImageLoad also searched in bpy.config.textureDir
    return None