~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/addons/io_online_sketchfab/pack_for_export.py

  • Committer: Reinhard Tartler
  • Date: 2014-05-31 01:50:05 UTC
  • mfrom: (14.2.27 sid)
  • Revision ID: siretart@tauware.de-20140531015005-ml6druahuj82nsav
mergeĀ fromĀ debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ##### BEGIN GPL LICENSE BLOCK #####
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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.
 
16
#
 
17
# ##### END GPL LICENSE BLOCK #####
 
18
 
 
19
# This script is called from the sketchfab addon directly
 
20
# to pack and save the file from a blender instance
 
21
# so that the users file is left untouched.
 
22
 
 
23
import os
 
24
import bpy
 
25
import json
 
26
import sys
 
27
 
 
28
 
 
29
SKETCHFAB_EXPORT_DATA_FILENAME = 'sketchfab-export-data.json'
 
30
 
 
31
SKETCHFAB_EXPORT_TEMP_DIR = sys.argv[-1]
 
32
SKETCHFAB_EXPORT_DATA_FILE = os.path.join(
 
33
    bpy.utils.user_resource('SCRIPTS'),
 
34
    "presets",
 
35
    SKETCHFAB_EXPORT_DATA_FILENAME,
 
36
    )
 
37
 
 
38
 
 
39
# save a copy of the current blendfile
 
40
def save_blend_copy():
 
41
    import time
 
42
 
 
43
    filepath = SKETCHFAB_EXPORT_TEMP_DIR
 
44
    filename = time.strftime("Sketchfab_%Y_%m_%d_%H_%M_%S.blend",
 
45
                             time.localtime(time.time()))
 
46
    filepath = os.path.join(filepath, filename)
 
47
    bpy.ops.wm.save_as_mainfile(filepath=filepath,
 
48
                                compress=True,
 
49
                                copy=True)
 
50
    size = os.path.getsize(filepath)
 
51
 
 
52
    return (filepath, filename, size)
 
53
 
 
54
 
 
55
# change visibility statuses and pack images
 
56
def prepare_assets(export_settings):
 
57
    hidden = set()
 
58
    images = set()
 
59
    if (export_settings['models'] == 'SELECTION' or
 
60
        export_settings['lamps'] != 'ALL'):
 
61
 
 
62
        for ob in bpy.data.objects:
 
63
            if ob.type == 'MESH':
 
64
                for mat_slot in ob.material_slots:
 
65
                    if not mat_slot.material:
 
66
                        continue
 
67
                    for tex_slot in mat_slot.material.texture_slots:
 
68
                        if not tex_slot:
 
69
                            continue
 
70
                        tex = tex_slot.texture
 
71
                        if tex.type == 'IMAGE':
 
72
                            image = tex.image
 
73
                            if image is not None:
 
74
                                images.add(image)
 
75
            if ((export_settings['models'] == 'SELECTION' and ob.type == 'MESH') or
 
76
                (export_settings['lamps'] == 'SELECTION' and ob.type == 'LAMP')):
 
77
 
 
78
                if not ob.select and not ob.hide:
 
79
                    ob.hide = True
 
80
                    hidden.add(ob)
 
81
            elif export_settings['lamps'] == 'NONE' and ob.type == 'LAMP':
 
82
                if not ob.hide:
 
83
                    ob.hide = True
 
84
                    hidden.add(ob)
 
85
 
 
86
    for img in images:
 
87
        if not img.packed_file:
 
88
            try:
 
89
                img.pack()
 
90
            except:
 
91
                # can fail in rare cases
 
92
                import traceback
 
93
                traceback.print_exc()
 
94
 
 
95
 
 
96
def prepare_file(export_settings):
 
97
    prepare_assets(export_settings)
 
98
    return save_blend_copy()
 
99
 
 
100
 
 
101
def read_settings():
 
102
    with open(SKETCHFAB_EXPORT_DATA_FILE, 'r') as s:
 
103
        return json.load(s)
 
104
 
 
105
 
 
106
def write_result(filepath, filename, size):
 
107
    with open(SKETCHFAB_EXPORT_DATA_FILE, 'w') as s:
 
108
        json.dump({
 
109
                'filepath': filepath,
 
110
                'filename': filename,
 
111
                'size': size,
 
112
                }, s)
 
113
 
 
114
 
 
115
if __name__ == "__main__":
 
116
    try:
 
117
        export_settings = read_settings()
 
118
        filepath, filename, size = prepare_file(export_settings)
 
119
        write_result(filepath, filename, size)
 
120
    except:
 
121
        import traceback
 
122
        traceback.print_exc()
 
123
 
 
124
        sys.exit(1)
 
125