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

blender-thumbnailer.py « bin « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b93eebdeece7c9e673756a1f2ee900028a418d6 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python

# ##### 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>

"""
Thumbnailer runs with python 2.6 and 3.x.
To run automatically with nautilus:
   gconftool --type boolean --set /desktop/gnome/thumbnailers/application@x-blender/enable true
   gconftool --type string --set /desktop/gnome/thumbnailers/application@x-blender/command "blender-thumbnailer.py %u %o"
"""

import struct


def open_wrapper_get():
    """ wrap OS spesific read functionality here, fallback to 'open()'
    """

    def open_gio(path, mode):
        g_file = gio.File(path).read()
        g_file.orig_seek = g_file.seek

        def new_seek(offset, whence=0):
            return g_file.orig_seek(offset, [1, 0, 2][whence])

        g_file.seek = new_seek
        return g_file

    try:
        import gio
        return open_gio
    except ImportError:
        return open


def blend_extract_thumb(path):
    import os
    open_wrapper = open_wrapper_get()

    # def MAKE_ID(tag): ord(tag[0])<<24 | ord(tag[1])<<16 | ord(tag[2])<<8 | ord(tag[3])
    REND = 1145980242  # MAKE_ID(b'REND')
    TEST = 1414743380  # MAKE_ID(b'TEST')

    blendfile = open_wrapper(path, 'rb')

    head = blendfile.read(12)

    if head[0:2] == b'\x1f\x8b':  # gzip magic
        import gzip
        blendfile.close()
        blendfile = gzip.GzipFile('', 'rb', 0, open_wrapper(path, 'rb'))
        head = blendfile.read(12)

    if not head.startswith(b'BLENDER'):
        blendfile.close()
        return None, 0, 0

    is_64_bit = (head[7] == b'-'[0])

    # true for PPC, false for X86
    is_big_endian = (head[8] == b'V'[0])

    # blender pre 2.5 had no thumbs
    if head[9:11] <= b'24':
        return None, 0, 0

    sizeof_bhead = 24 if is_64_bit else 20
    int_endian_pair = '>ii' if is_big_endian else '<ii'

    while True:
        bhead = blendfile.read(sizeof_bhead)

        if len(bhead) < sizeof_bhead:
            return None, 0, 0

        code, length = struct.unpack(int_endian_pair, bhead[0:8])  # 8 == sizeof(int) * 2

        if code == REND:
            blendfile.seek(length, os.SEEK_CUR)
        else:
            break

    if code != TEST:
        return None, 0, 0

    try:
        x, y = struct.unpack(int_endian_pair, blendfile.read(8))  # 8 == sizeof(int) * 2
    except struct.error:
        return None, 0, 0

    length -= 8  # sizeof(int) * 2

    if length != x * y * 4:
        return None, 0, 0

    image_buffer = blendfile.read(length)

    if len(image_buffer) != length:
        return None, 0, 0

    return image_buffer, x, y


def write_png(buf, width, height):
    import zlib

    # reverse the vertical line order and add null bytes at the start
    width_byte_4 = width * 4
    raw_data = b"".join(b'\x00' + buf[span:span + width_byte_4] for span in range((height - 1) * width * 4, -1, - width_byte_4))

    def png_pack(png_tag, data):
        chunk_head = png_tag + data
        return struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))

    return b"".join([
        b'\x89PNG\r\n\x1a\n',
        png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)),
        png_pack(b'IDAT', zlib.compress(raw_data, 9)),
        png_pack(b'IEND', b'')])


if __name__ == '__main__':
    import sys

    if len(sys.argv) < 3:
        print("Expected 2 arguments <input.blend> <output.png>")
    else:
        file_in = sys.argv[-2]

        buf, width, height = blend_extract_thumb(file_in)

        if buf:
            file_out = sys.argv[-1]

            f = open(file_out, "wb")
            f.write(write_png(buf, width, height))
            f.close()