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
# 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.
29
SKETCHFAB_EXPORT_DATA_FILENAME = 'sketchfab-export-data.json'
31
SKETCHFAB_EXPORT_TEMP_DIR = sys.argv[-1]
32
SKETCHFAB_EXPORT_DATA_FILE = os.path.join(
33
bpy.utils.user_resource('SCRIPTS'),
35
SKETCHFAB_EXPORT_DATA_FILENAME,
39
# save a copy of the current blendfile
40
def save_blend_copy():
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,
50
size = os.path.getsize(filepath)
52
return (filepath, filename, size)
55
# change visibility statuses and pack images
56
def prepare_assets(export_settings):
59
if (export_settings['models'] == 'SELECTION' or
60
export_settings['lamps'] != 'ALL'):
62
for ob in bpy.data.objects:
64
for mat_slot in ob.material_slots:
65
if not mat_slot.material:
67
for tex_slot in mat_slot.material.texture_slots:
70
tex = tex_slot.texture
71
if tex.type == 'IMAGE':
75
if ((export_settings['models'] == 'SELECTION' and ob.type == 'MESH') or
76
(export_settings['lamps'] == 'SELECTION' and ob.type == 'LAMP')):
78
if not ob.select and not ob.hide:
81
elif export_settings['lamps'] == 'NONE' and ob.type == 'LAMP':
87
if not img.packed_file:
91
# can fail in rare cases
96
def prepare_file(export_settings):
97
prepare_assets(export_settings)
98
return save_blend_copy()
102
with open(SKETCHFAB_EXPORT_DATA_FILE, 'r') as s:
106
def write_result(filepath, filename, size):
107
with open(SKETCHFAB_EXPORT_DATA_FILE, 'w') as s:
109
'filepath': filepath,
110
'filename': filename,
115
if __name__ == "__main__":
117
export_settings = read_settings()
118
filepath, filename, size = prepare_file(export_settings)
119
write_result(filepath, filename, size)
122
traceback.print_exc()