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

thumbnail.py « netrender - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ead6e82745e99f378f9d796e7226631aae1e5e6 (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
# ##### 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 #####

import sys, os
import subprocess

import bpy

def generate(filename, external=True):
    if external:
        process = subprocess.Popen([sys.argv[0], "-b", "-noaudio", "-P", __file__, "--", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        while process.poll() is None:
            process.stdout.read(1024) # empty buffer to be sure
        process.stdout.read()
        
        return _thumbname(filename)
    else:
        return _internal(filename)

def _thumbname(filename):
    root = os.path.splitext(filename)[0]
    return root + ".jpg"

def _internal(filename):
    imagename = os.path.split(filename)[1]
    thumbname = _thumbname(filename)

    if os.path.exists(thumbname):
        return thumbname

    if bpy:
        scene = bpy.data.scenes[0] # FIXME, this is dodgy!
        scene.render.file_format = "JPEG"
        scene.render.file_quality = 90
        
        # remove existing image, if there's a leftover (otherwise open changes the name)
        if imagename in bpy.data.images:
            img = bpy.data.images[imagename]
            bpy.data.images.remove(img)

        bpy.ops.image.open(filepath=filename)
        img = bpy.data.images[imagename]
            
        img.save_render(thumbname, scene=scene)
        
        img.user_clear()
        bpy.data.images.remove(img)

        try:
            process = subprocess.Popen(["convert", thumbname, "-resize", "300x300", thumbname])
            process.wait()
            return thumbname
        except Exception as exp:
            print("Error while generating thumbnail")
            print(exp)

    return None

if __name__ == "__main__":
    import bpy
    try:
        start = sys.argv.index("--") + 1
    except ValueError:
        start = 0
    for filename in sys.argv[start:]:
        generate(filename, external=False)