From 0dd91e4d992ede9b06e27e45347af982a5ed7d7a Mon Sep 17 00:00:00 2001 From: "Jeison Yehuda Amihud (Blender Dumbass)" Date: Mon, 7 Dec 2020 21:41:24 +0000 Subject: [PATCH] Upload files to 'UI' --- UI/UI_elements.py | 49 +++++++++- UI/blender-thumbnailer.py | 193 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 UI/blender-thumbnailer.py diff --git a/UI/UI_elements.py b/UI/UI_elements.py index 77fd93a..aa664c5 100644 --- a/UI/UI_elements.py +++ b/UI/UI_elements.py @@ -18,6 +18,7 @@ import cairo # Own modules from settings import settings from settings import talk +from settings import fileformats # UI from UI import UI_color @@ -230,8 +231,52 @@ def image(layer, win ,path, x, y, width=0, height=0, fit="crop"): except: # If it's not png it's gonna take few steps + # Let's first of all see if it's a blend file. + if path.endswith(".blend") or path.endswith(".blend1"): + IF = os.system("python3 "+os.getcwd()+"/UI/blender-thumbnailer.py "\ + +path+" /tmp/vcstudio_blender_thumbnail.png") + if not IF: + load1 = GdkPixbuf.Pixbuf.new_from_file("/tmp/vcstudio_blender_thumbnail.png") + else: + load1 = GdkPixbuf.Pixbuf.new_from_file("settings/themes/"\ + +win.settings["Theme"]+"/icons/blender.png") + else: + + # if video + video = False + for f in fileformats.videos: + if path.endswith(f): + video = True + + if video: + IF = os.system("totem-video-thumbnailer -s "+str(width)+" "+path\ + +" /tmp/vcstudio_video_thumbnail.png") + if not IF: + load1 = GdkPixbuf.Pixbuf.new_from_file("/tmp/vcstudio_video_thumbnail.png") + else: + load1 = GdkPixbuf.Pixbuf.new_from_file("settings/themes/"\ + +win.settings["Theme"]+"/icons/video.png") + else: + # if image + image = False + for f in fileformats.images: + if path.endswith(f): + image = True + + + if image: + try: + load1 = GdkPixbuf.Pixbuf.new_from_file(path) + except: + load1 = GdkPixbuf.Pixbuf.new_from_file("settings/themes/"\ + +win.settings["Theme"]+"/icons/image.png") + + else: + load1 = GdkPixbuf.Pixbuf.new_from_file("settings/themes/"\ + +win.settings["Theme"]+"/icons/file.png") + # First we need to make a pixbuf. - load1 = GdkPixbuf.Pixbuf.new_from_file(path) + Px = load1.get_width() Py = load1.get_height() @@ -514,7 +559,7 @@ def text(outlayer, win, name, x, y, width, height, set_text="", parse=False, fil # the program. # Making the layer - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height)) layer = cairo.Context(surface) layer.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) layer.set_font_size(20) diff --git a/UI/blender-thumbnailer.py b/UI/blender-thumbnailer.py new file mode 100644 index 0000000..e050a68 --- /dev/null +++ b/UI/blender-thumbnailer.py @@ -0,0 +1,193 @@ +#!/usr/bin/env python3 + +# ##### 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 ##### + +# + +""" +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; +""" + +import struct + + +def open_wrapper_get(): + """ wrap OS specific read functionality here, fallback to 'open()' + """ + + class GFileWrapper: + __slots__ = ("mode", "g_file") + + def __init__(self, url, mode='r'): + self.mode = mode # used in gzip module + self.g_file = Gio.File.parse_name(url).read(None) + + def read(self, size): + return self.g_file.read_bytes(size, None).get_data() + + 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) + + try: + from gi.repository import Gio + return GFileWrapper + except ImportError: + 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 + + +def blend_extract_thumb(path): + import os + open_wrapper = open_wrapper_get() + + REND = b'REND' + TEST = 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 = '>i' if is_big_endian else ' ") + 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() + + +if __name__ == '__main__': + main()