Extensions: change the constant for the complete status
[blender-addons-contrib.git] / netrender / thumbnail.py
blob0c72356def31d16c9fbc14ffbb0d325dab75d532
1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
19 import sys, os
20 import subprocess
22 import bpy
24 def generate(filename, external=True):
25 if external:
26 process = subprocess.Popen(
27 [bpy.app.binary_path,
28 "-b",
29 "-y",
30 "-noaudio",
31 "-P", __file__,
32 "--",
33 filename,
35 stdout=subprocess.PIPE,
36 stderr=subprocess.STDOUT,
38 while process.poll() is None:
39 process.stdout.read(1024) # empty buffer to be sure
40 process.stdout.read()
42 return _thumbname(filename)
43 else:
44 return _internal(filename)
46 def _thumbname(filename):
47 root = os.path.splitext(filename)[0]
48 return root + ".jpg"
50 def _internal(filename):
51 imagename = os.path.split(filename)[1]
52 thumbname = _thumbname(filename)
54 if os.path.exists(thumbname):
55 return thumbname
57 if bpy:
58 scene = bpy.data.scenes[0] # FIXME, this is dodgy!
59 scene.render.image_settings.file_format = "JPEG"
60 scene.render.image_settings.quality = 90
62 # remove existing image, if there's a leftover (otherwise open changes the name)
63 if imagename in bpy.data.images:
64 img = bpy.data.images[imagename]
65 bpy.data.images.remove(img)
67 bpy.ops.image.open(filepath=filename)
68 img = bpy.data.images[imagename]
70 img.save_render(thumbname, scene=scene)
72 img.user_clear()
73 bpy.data.images.remove(img)
75 try:
76 process = subprocess.Popen(["convert", thumbname, "-resize", "300x300", thumbname])
77 process.wait()
78 return thumbname
79 except Exception as exp:
80 print("Error while generating thumbnail")
81 print(exp)
83 return None
85 if __name__ == "__main__":
86 try:
87 start = sys.argv.index("--") + 1
88 except ValueError:
89 start = 0
90 for filename in sys.argv[start:]:
91 generate(filename, external=False)