-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexport.py
201 lines (173 loc) · 6.52 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# ##### 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 #####
# <pep8-80 compliant>
#----------------------------------------------------------
# File export.py
# Export wrappers and integration with external tools.
#----------------------------------------------------------
import os
import bpy
def image_copy_guess(filepath, objects):
# 'filepath' is the path we are writing to.
import shutil
from bpy_extras import object_utils
image = None
for obj in objects:
image = object_utils.object_image_guess(obj)
if image is not None:
break
if image is not None:
imagepath = bpy.path.abspath(image.filepath, library=image.library)
if os.path.exists(imagepath):
filepath_noext = os.path.splitext(filepath)[0]
ext = os.path.splitext(imagepath)[1]
imagepath_dst = filepath_noext + ext
print("copying texture: %r -> %r" % (imagepath, imagepath_dst))
try:
shutil.copy(imagepath, imagepath_dst)
except:
import traceback
traceback.print_exc()
def write_mesh(context, info, report_cb):
scene = context.scene
collection = context.collection
layer = context.view_layer
unit = scene.unit_settings
print_3d = scene.print_3d
obj = layer.objects.active
export_format = print_3d.export_format
global_scale = unit.scale_length if (unit.system != 'NONE' and print_3d.use_apply_scale) else 1.0
path_mode = 'COPY' if print_3d.use_export_texture else 'AUTO'
context_override = context.copy()
obj_tmp = None
# PLY can only export single mesh objects!
if export_format == 'PLY':
context_backup = context.copy()
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
from . import mesh_helpers
obj_tmp = mesh_helpers.object_merge(context, context_override["selected_objects"])
context_override["active_object"] = obj_tmp
context_override["selected_objects"] = [obj_tmp]
else:
if obj not in context_override["selected_objects"]:
context_override["selected_objects"].append(obj)
export_path = bpy.path.abspath(print_3d.export_path)
# Create name 'export_path/blendname-objname'
# add the filename component
if bpy.data.is_saved:
name = os.path.basename(bpy.data.filepath)
name = os.path.splitext(name)[0]
else:
name = "untitled"
# add object name
name += "-%s" % bpy.path.clean_name(obj.name)
# first ensure the path is created
if export_path:
# this can fail with strange errors,
# if the dir cant be made then we get an error later.
try:
os.makedirs(export_path, exist_ok=True)
except:
import traceback
traceback.print_exc()
filepath = os.path.join(export_path, name)
# ensure addon is enabled
import addon_utils
def addon_ensure(addon_id):
# Enable the addon, dont change preferences.
default_state, loaded_state = addon_utils.check(addon_id)
if not loaded_state:
addon_utils.enable(addon_id, default_set=False)
if export_format == 'STL':
addon_ensure("io_mesh_stl")
filepath = bpy.path.ensure_ext(filepath, ".stl")
ret = bpy.ops.export_mesh.stl(
context_override,
filepath=filepath,
ascii=False,
use_mesh_modifiers=True,
use_selection=True,
global_scale=global_scale,
)
elif export_format == 'PLY':
addon_ensure("io_mesh_ply")
filepath = bpy.path.ensure_ext(filepath, ".ply")
ret = bpy.ops.export_mesh.ply(
context_override,
filepath=filepath,
use_mesh_modifiers=True,
global_scale=global_scale,
)
elif export_format == 'X3D':
addon_ensure("io_scene_x3d")
filepath = bpy.path.ensure_ext(filepath, ".x3d")
ret = bpy.ops.export_scene.x3d(
context_override,
filepath=filepath,
use_mesh_modifiers=True,
use_selection=True,
path_mode=path_mode,
global_scale=global_scale,
)
elif export_format == 'WRL':
addon_ensure("io_scene_vrml2")
filepath = bpy.path.ensure_ext(filepath, ".wrl")
ret = bpy.ops.export_scene.vrml2(
context_override,
filepath=filepath,
use_mesh_modifiers=True,
use_selection=True,
path_mode=path_mode,
global_scale=global_scale,
)
elif export_format == 'OBJ':
addon_ensure("io_scene_obj")
filepath = bpy.path.ensure_ext(filepath, ".obj")
ret = bpy.ops.export_scene.obj(
context_override,
filepath=filepath,
use_mesh_modifiers=True,
use_selection=True,
path_mode=path_mode,
global_scale=global_scale,
)
else:
assert 0
# for formats that don't support images
if export_format in {'STL', 'PLY'}:
if path_mode == 'COPY':
image_copy_guess(filepath, context_override["selected_objects"])
if obj_tmp is not None:
obj = obj_tmp
mesh = obj.data
collection.objects.unlink(obj)
bpy.data.objects.remove(obj)
bpy.data.meshes.remove(mesh)
del obj_tmp, obj, mesh
# restore context
for ob in context_backup["selected_objects"]:
ob.select_set(True)
layer.objects.active = context_backup["active_object"]
if 'FINISHED' in ret:
info.append(("%r ok" % os.path.basename(filepath), None))
if report_cb is not None:
report_cb({'INFO'}, "Exported: %r" % filepath)
return True
else:
info.append(("%r fail" % os.path.basename(filepath), None))
return False