2013-11-18 09:38:53 +00:00
|
|
|
#!/usr/bin/env python
|
2010-07-16 01:00:56 +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-02-04 09:27:25 +00:00
|
|
|
# <pep8 compliant>
|
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
"""
|
2014-02-19 04:32:54 +00:00
|
|
|
Thumbnailer runs with python 2.7 and 3.x.
|
|
|
|
To run automatically with a file manager such as Nautilus, save this file
|
|
|
|
in a directory that is listed in PATH environment variable, and create
|
|
|
|
blender.thumbnailer file in ${HOME}/.local/share/thumbnailers/ directory
|
|
|
|
with the following contents:
|
|
|
|
|
|
|
|
[Thumbnailer Entry]
|
|
|
|
TryExec=blender-thumbnailer.py
|
|
|
|
Exec=blender-thumbnailer.py %u %o
|
|
|
|
MimeType=application/x-blender;
|
2010-07-16 01:00:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import struct
|
|
|
|
|
2011-01-20 21:02:42 +00:00
|
|
|
|
2011-02-09 02:09:30 +00:00
|
|
|
def open_wrapper_get():
|
|
|
|
""" wrap OS spesific read functionality here, fallback to 'open()'
|
|
|
|
"""
|
|
|
|
|
2014-02-19 04:32:54 +00:00
|
|
|
class GFileWrapper:
|
|
|
|
__slots__ = ("mode", "g_file")
|
2014-02-28 00:04:15 +00:00
|
|
|
|
2014-02-19 04:32:54 +00:00
|
|
|
def __init__(self, url, mode='r'):
|
2014-02-28 00:04:15 +00:00
|
|
|
self.mode = mode # used in gzip module
|
2014-02-19 04:32:54 +00:00
|
|
|
self.g_file = Gio.File.parse_name(url).read(None)
|
2011-02-09 02:09:30 +00:00
|
|
|
|
2014-02-19 04:32:54 +00:00
|
|
|
def read(self, size):
|
|
|
|
return self.g_file.read_bytes(size, None).get_data()
|
2011-02-09 02:09:30 +00:00
|
|
|
|
2014-02-19 04:32:54 +00:00
|
|
|
def seek(self, offset, whence=0):
|
|
|
|
self.g_file.seek(offset, [1, 0, 2][whence], None)
|
|
|
|
return self.g_file.tell()
|
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
return self.g_file.tell()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.g_file.close(None)
|
|
|
|
|
|
|
|
def open_local_url(url, mode='r'):
|
|
|
|
o = urlparse(url)
|
|
|
|
if o.scheme == '':
|
|
|
|
path = o.path
|
|
|
|
elif o.scheme == 'file':
|
|
|
|
path = unquote(o.path)
|
|
|
|
else:
|
|
|
|
raise(IOError('URL scheme "%s" needs gi.repository.Gio module' % o.scheme))
|
|
|
|
return open(path, mode)
|
2011-02-09 02:09:30 +00:00
|
|
|
|
|
|
|
try:
|
2014-02-19 04:32:54 +00:00
|
|
|
from gi.repository import Gio
|
|
|
|
return GFileWrapper
|
2011-02-09 02:09:30 +00:00
|
|
|
except ImportError:
|
2014-02-19 04:32:54 +00:00
|
|
|
try:
|
|
|
|
# Python 3
|
|
|
|
from urllib.parse import urlparse, unquote
|
|
|
|
except ImportError:
|
|
|
|
# Python 2
|
|
|
|
from urlparse import urlparse
|
|
|
|
from urllib import unquote
|
|
|
|
return open_local_url
|
2011-02-09 02:09:30 +00:00
|
|
|
|
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
def blend_extract_thumb(path):
|
2010-07-17 00:38:34 +00:00
|
|
|
import os
|
2011-02-09 02:09:30 +00:00
|
|
|
open_wrapper = open_wrapper_get()
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
# def MAKE_ID(tag): ord(tag[0])<<24 | ord(tag[1])<<16 | ord(tag[2])<<8 | ord(tag[3])
|
2011-01-20 21:02:42 +00:00
|
|
|
REND = 1145980242 # MAKE_ID(b'REND')
|
|
|
|
TEST = 1414743380 # MAKE_ID(b'TEST')
|
2010-07-16 01:00:56 +00:00
|
|
|
|
2011-02-09 02:09:30 +00:00
|
|
|
blendfile = open_wrapper(path, 'rb')
|
2010-07-16 01:00:56 +00:00
|
|
|
|
2010-07-17 00:38:34 +00:00
|
|
|
head = blendfile.read(12)
|
2010-07-16 01:00:56 +00:00
|
|
|
|
2011-01-20 21:02:42 +00:00
|
|
|
if head[0:2] == b'\x1f\x8b': # gzip magic
|
2010-07-16 01:00:56 +00:00
|
|
|
import gzip
|
|
|
|
blendfile.close()
|
2011-02-09 02:09:30 +00:00
|
|
|
blendfile = gzip.GzipFile('', 'rb', 0, open_wrapper(path, 'rb'))
|
2010-07-17 00:38:34 +00:00
|
|
|
head = blendfile.read(12)
|
2010-07-16 01:00:56 +00:00
|
|
|
|
2010-07-17 00:38:34 +00:00
|
|
|
if not head.startswith(b'BLENDER'):
|
2010-07-16 01:00:56 +00:00
|
|
|
blendfile.close()
|
|
|
|
return None, 0, 0
|
|
|
|
|
2011-01-20 21:02:42 +00:00
|
|
|
is_64_bit = (head[7] == b'-'[0])
|
2010-07-16 01:00:56 +00:00
|
|
|
|
|
|
|
# true for PPC, false for X86
|
2011-01-20 21:02:42 +00:00
|
|
|
is_big_endian = (head[8] == b'V'[0])
|
2010-07-16 01:00:56 +00:00
|
|
|
|
2010-07-17 00:38:34 +00:00
|
|
|
# blender pre 2.5 had no thumbs
|
|
|
|
if head[9:11] <= b'24':
|
|
|
|
return None, 0, 0
|
2010-07-16 01:00:56 +00:00
|
|
|
|
|
|
|
sizeof_bhead = 24 if is_64_bit else 20
|
|
|
|
int_endian_pair = '>ii' if is_big_endian else '<ii'
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
while True:
|
2010-07-17 00:38:34 +00:00
|
|
|
bhead = blendfile.read(sizeof_bhead)
|
|
|
|
|
|
|
|
if len(bhead) < sizeof_bhead:
|
2010-07-16 01:00:56 +00:00
|
|
|
return None, 0, 0
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2011-01-20 21:02:42 +00:00
|
|
|
code, length = struct.unpack(int_endian_pair, bhead[0:8]) # 8 == sizeof(int) * 2
|
2010-07-16 01:00:56 +00:00
|
|
|
|
|
|
|
if code == REND:
|
|
|
|
blendfile.seek(length, os.SEEK_CUR)
|
|
|
|
else:
|
|
|
|
break
|
2011-01-20 21:02:42 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
if code != TEST:
|
|
|
|
return None, 0, 0
|
|
|
|
|
|
|
|
try:
|
2011-01-20 21:02:42 +00:00
|
|
|
x, y = struct.unpack(int_endian_pair, blendfile.read(8)) # 8 == sizeof(int) * 2
|
2010-07-16 01:00:56 +00:00
|
|
|
except struct.error:
|
|
|
|
return None, 0, 0
|
|
|
|
|
2011-01-20 21:02:42 +00:00
|
|
|
length -= 8 # sizeof(int) * 2
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
if length != x * y * 4:
|
|
|
|
return None, 0, 0
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
image_buffer = blendfile.read(length)
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
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
|
2010-09-19 07:07:14 +00:00
|
|
|
raw_data = b"".join(b'\x00' + buf[span:span + width_byte_4] for span in range((height - 1) * width * 4, -1, - width_byte_4))
|
2010-07-17 00:38:34 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
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__':
|
2010-07-17 00:38:34 +00:00
|
|
|
import sys
|
|
|
|
|
2011-02-06 06:54:19 +00:00
|
|
|
if len(sys.argv) < 3:
|
2010-07-16 01:00:56 +00:00
|
|
|
print("Expected 2 arguments <input.blend> <output.png>")
|
|
|
|
else:
|
|
|
|
file_in = sys.argv[-2]
|
|
|
|
|
|
|
|
buf, width, height = blend_extract_thumb(file_in)
|
2011-01-20 21:02:42 +00:00
|
|
|
|
2010-07-16 01:00:56 +00:00
|
|
|
if buf:
|
|
|
|
file_out = sys.argv[-1]
|
|
|
|
|
|
|
|
f = open(file_out, "wb")
|
|
|
|
f.write(write_png(buf, width, height))
|
|
|
|
f.close()
|