-
Notifications
You must be signed in to change notification settings - Fork 4
/
pack_for_export.py
144 lines (116 loc) · 4.94 KB
/
pack_for_export.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# ##### 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 #####
# This script is called from the sketchfab addon directly
# to pack and save the file from a blender instance
# so that the users file is left untouched.
import os
import bpy
import json
import sys
SKETCHFAB_EXPORT_DATA_FILENAME = 'sketchfab-export-data.json'
SKETCHFAB_EXPORT_TEMP_DIR = sys.argv[-1]
SKETCHFAB_EXPORT_DATA_FILE = os.path.join(
bpy.utils.user_resource('SCRIPTS'),
"presets",
SKETCHFAB_EXPORT_DATA_FILENAME,
)
# save a copy of the current blendfile
def save_blend_copy():
import time
filepath = SKETCHFAB_EXPORT_TEMP_DIR
filename = time.strftime("Sketchfab_%Y_%m_%d_%H_%M_%S.blend",
time.localtime(time.time()))
filepath = os.path.join(filepath, filename)
bpy.ops.wm.save_as_mainfile(filepath=filepath,
compress=True,
copy=True)
size = os.path.getsize(filepath)
return (filepath, filename, size)
# change visibility statuses and pack images
def prepare_assets(export_settings):
images = set()
for ob in bpy.data.objects:
# make sure settings are correct so that sketchfab won't reject the upload
if ob.type in ('MESH', 'LIGHT'):
if ob.hide_get() or ob.hide_viewport: # delete the ones that are hidden
bpy.data.objects.remove(ob)
continue
else: # it's something else
if ob.hide_viewport: # delete the ones that are disabled in viewport
bpy.data.objects.remove(ob)
continue
if ob.hide_render: # make sure it's turned on for all objects or sketchfab will reject the upload
ob.hide_render = False
# prepare meshes
if ob.type == 'MESH':
# the current object is what we want
if ((export_settings['models'] == 'SELECTION' and ob.select_get()) or
export_settings['models'] == 'ALL'):
# go through the materials
for mat_slot in ob.material_slots:
# CYCLES RENDER and EEVEE
if bpy.context.scene.render.engine in ('CYCLES', 'BLENDER_EEVEE'):
if not mat_slot.material:
continue
if not mat_slot.material.node_tree:
continue
imgnodes = [n for n in mat_slot.material.node_tree.nodes if n.type == 'TEX_IMAGE']
for node in imgnodes:
if node.image is not None:
images.add(node.image)
# it's not an object we want - so remove it
else:
bpy.data.objects.remove(ob)
continue
# go through the lights
elif ob.type == 'LIGHT':
# it's NOT a light that we want - remove it
if ((export_settings['lights'] == 'SELECTION' and not ob.select_get()) or
export_settings['lights'] == 'NONE'):
bpy.data.objects.remove(ob)
# prepare images
for img in images:
if not img.packed_file:
try:
img.pack()
except:
# can fail in rare cases
import traceback
traceback.print_exc()
def prepare_file(export_settings):
prepare_assets(export_settings)
return save_blend_copy()
def read_settings():
with open(SKETCHFAB_EXPORT_DATA_FILE, 'r') as s:
return json.load(s)
def write_result(filepath, filename, size):
with open(SKETCHFAB_EXPORT_DATA_FILE, 'w') as s:
json.dump({
'filepath': filepath,
'filename': filename,
'size': size,
}, s)
if __name__ == "__main__":
try:
export_settings = read_settings()
filepath, filename, size = prepare_file(export_settings)
write_result(filepath, filename, size)
except:
import traceback
traceback.print_exc()
sys.exit(1)