3
# ##### BEGIN GPL LICENSE BLOCK #####
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software Foundation,
17
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
# ##### END GPL LICENSE BLOCK #####
24
Thumbnailer runs with python 2.6 and 3.x.
25
To run automatically with nautilus:
26
gconftool --type boolean --set /desktop/gnome/thumbnailers/application@x-blender/enable true
27
gconftool --type string --set /desktop/gnome/thumbnailers/application@x-blender/command "/usr/share/blender/scripts/blender-thumbnailer.py %i %o"
33
def open_wrapper_get():
34
""" wrap OS spesific read functionality here, fallback to 'open()'
37
def open_gio(path, mode):
38
g_file = gio.File(path).read()
39
g_file.orig_seek = g_file.seek
41
def new_seek(offset, whence=0):
42
return g_file.orig_seek(offset, [1, 0, 2][whence])
44
g_file.seek = new_seek
54
def blend_extract_thumb(path):
56
open_wrapper = open_wrapper_get()
58
# def MAKE_ID(tag): ord(tag[0])<<24 | ord(tag[1])<<16 | ord(tag[2])<<8 | ord(tag[3])
59
REND = 1145980242 # MAKE_ID(b'REND')
60
TEST = 1414743380 # MAKE_ID(b'TEST')
62
blendfile = open_wrapper(path, 'rb')
64
head = blendfile.read(12)
66
if head[0:2] == b'\x1f\x8b': # gzip magic
69
blendfile = gzip.GzipFile('', 'rb', 0, open_wrapper(path, 'rb'))
70
head = blendfile.read(12)
72
if not head.startswith(b'BLENDER'):
76
is_64_bit = (head[7] == b'-'[0])
78
# true for PPC, false for X86
79
is_big_endian = (head[8] == b'V'[0])
81
# blender pre 2.5 had no thumbs
82
if head[9:11] <= b'24':
85
sizeof_bhead = 24 if is_64_bit else 20
86
int_endian_pair = '>ii' if is_big_endian else '<ii'
89
bhead = blendfile.read(sizeof_bhead)
91
if len(bhead) < sizeof_bhead:
94
code, length = struct.unpack(int_endian_pair, bhead[0:8]) # 8 == sizeof(int) * 2
97
blendfile.seek(length, os.SEEK_CUR)
105
x, y = struct.unpack(int_endian_pair, blendfile.read(8)) # 8 == sizeof(int) * 2
109
length -= 8 # sizeof(int) * 2
111
if length != x * y * 4:
114
image_buffer = blendfile.read(length)
116
if len(image_buffer) != length:
119
return image_buffer, x, y
122
def write_png(buf, width, height):
125
# reverse the vertical line order and add null bytes at the start
126
width_byte_4 = width * 4
127
raw_data = b"".join(b'\x00' + buf[span:span + width_byte_4] for span in range((height - 1) * width * 4, -1, - width_byte_4))
129
def png_pack(png_tag, data):
130
chunk_head = png_tag + data
131
return struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))
134
b'\x89PNG\r\n\x1a\n',
135
png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)),
136
png_pack(b'IDAT', zlib.compress(raw_data, 9)),
137
png_pack(b'IEND', b'')])
140
if __name__ == '__main__':
143
if len(sys.argv) < 3:
144
print("Expected 2 arguments <input.blend> <output.png>")
146
file_in = sys.argv[-2]
148
buf, width, height = blend_extract_thumb(file_in)
151
file_out = sys.argv[-1]
153
f = open(file_out, "wb")
154
f.write(write_png(buf, width, height))