~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/modules/bpy_extras/image_utils.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ##### BEGIN GPL LICENSE BLOCK #####
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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.
 
16
#
 
17
# ##### END GPL LICENSE BLOCK #####
 
18
 
 
19
# <pep8-80 compliant>
 
20
 
 
21
__all__ = (
 
22
    "load_image",
 
23
    )
 
24
 
 
25
 
 
26
# limited replacement for BPyImage.comprehensiveImageLoad
 
27
def load_image(imagepath,
 
28
               dirname="",
 
29
               place_holder=False,
 
30
               recursive=False,
 
31
               ncase_cmp=True,
 
32
               convert_callback=None,
 
33
               verbose=False,
 
34
               ):
 
35
    """
 
36
    Return an image from the file path with options to search multiple paths
 
37
    and return a placeholder if its not found.
 
38
 
 
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.
 
44
    :type dirname: string
 
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.
 
51
    :type recursive: bool
 
52
    :arg ncase_cmp: on non windows systems, find the correct case for the file.
 
53
    :type ncase_cmp: bool
 
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`
 
62
    """
 
63
    import os
 
64
    import bpy
 
65
 
 
66
    # TODO: recursive
 
67
 
 
68
    # -------------------------------------------------------------------------
 
69
    # Utility Functions
 
70
 
 
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
 
77
        image.filepath = path
 
78
        image.source = 'FILE'
 
79
        return image
 
80
 
 
81
    def _image_load(path):
 
82
        import bpy
 
83
 
 
84
        if convert_callback:
 
85
            path = convert_callback(path)
 
86
 
 
87
        try:
 
88
            image = bpy.data.images.load(path)
 
89
        except RuntimeError:
 
90
            image = None
 
91
 
 
92
        if verbose:
 
93
            if image:
 
94
                print("    image loaded '%s'" % path)
 
95
            else:
 
96
                print("    image load failed '%s'" % path)
 
97
 
 
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)
 
102
 
 
103
        return image
 
104
 
 
105
    # -------------------------------------------------------------------------
 
106
 
 
107
    if verbose:
 
108
        print("load_image('%s', '%s', ...)" % (imagepath, dirname))
 
109
 
 
110
    if os.path.exists(imagepath):
 
111
        return _image_load(imagepath)
 
112
 
 
113
    variants = [imagepath]
 
114
 
 
115
    if dirname:
 
116
        variants += [os.path.join(dirname, imagepath),
 
117
                     os.path.join(dirname, bpy.path.basename(imagepath)),
 
118
                     ]
 
119
 
 
120
    for filepath_test in variants:
 
121
        if ncase_cmp:
 
122
            ncase_variants = (filepath_test,
 
123
                              bpy.path.resolve_ncase(filepath_test),
 
124
                              )
 
125
        else:
 
126
            ncase_variants = (filepath_test, )
 
127
 
 
128
        for nfilepath in ncase_variants:
 
129
            if os.path.exists(nfilepath):
 
130
                return _image_load(nfilepath)
 
131
 
 
132
    # None of the paths exist so return placeholder
 
133
    if place_holder:
 
134
        return _image_load_placeholder(imagepath)
 
135
 
 
136
    # TODO comprehensiveImageLoad also searched in bpy.config.textureDir
 
137
    return None