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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# ##### 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>
import bpy
import bpy_extras
import bmesh
import os
from bpy_extras import object_utils
def save_bmesh(fw, bm,
use_color, color_type, material_colors,
use_uv, uv_image,
path_mode, copy_set):
base_src = os.path.dirname(bpy.data.filepath)
base_dst = os.path.dirname(fw.__self__.name)
fw('Shape {\n')
fw('\tappearance Appearance {\n')
if use_uv:
fw('\t\ttexture ImageTexture {\n')
filepath = uv_image.filepath
filepath_full = os.path.normpath(bpy.path.abspath(filepath, library=uv_image.library))
filepath_ref = bpy_extras.io_utils.path_reference(filepath_full, base_src, base_dst, path_mode, "textures", copy_set, uv_image.library)
filepath_base = os.path.basename(filepath_full)
images = [
filepath_ref,
filepath_base,
]
if path_mode != 'RELATIVE':
images.append(filepath_full)
fw('\t\t\turl [ %s ]\n' % " ".join(['"%s"' % f for f in images]) )
del images
del filepath_ref, filepath_base, filepath_full, filepath
fw('\t\t}\n') # end 'ImageTexture'
else:
fw('\t\tmaterial Material {\n')
fw('\t\t}\n') # end 'Material'
fw('\t}\n') # end 'Appearance'
fw('\tgeometry IndexedFaceSet {\n')
fw('\t\tcoord Coordinate {\n')
fw('\t\t\tpoint [ ')
v = None
for v in bm.verts:
fw("%.6f %.6f %.6f " % v.co[:])
del v
fw(']\n') # end 'point[]'
fw('\t\t}\n') # end 'Coordinate'
if use_color:
if color_type == 'MATERIAL':
fw('\t\tcolorPerVertex FALSE\n')
fw('\t\tcolor Color {\n')
fw('\t\t\tcolor [ ')
c = None
for c in material_colors:
fw(c)
del c
fw(']\n') # end 'color[]'
fw('\t\t}\n') # end 'Color'
elif color_type == 'VERTEX':
fw('\t\tcolorPerVertex TRUE\n')
fw('\t\tcolor Color {\n')
fw('\t\t\tcolor [ ')
v = None
c_none = "0.00 0.00 0.00 "
color_layer = bm.loops.layers.color.active
assert(color_layer is not None)
for v in bm.verts:
# weak, use first loops color
try:
l = v.link_loops[0]
except:
l = None
fw(c_none if l is None else ("%.2f %.2f %.2f " % l[color_layer][:]))
del v
fw(']\n') # end 'color[]'
fw('\t\t}\n') # end 'Color'
# ---
if color_type == 'MATERIAL':
fw('\t\tcolorIndex [ ')
i = None
for f in bm.faces:
i = f.material_index
if i >= len(material_colors):
i = 0
fw("%d " % i)
del i
fw(']\n') # end 'colorIndex[]'
elif color_type == 'VERTEX':
pass
if use_uv:
fw('\t\ttexCoord TextureCoordinate {\n')
fw('\t\t\tpoint [ ')
v = None
uv_layer = bm.loops.layers.uv.active
assert(uv_layer is not None)
for f in bm.faces:
for l in f.loops:
fw("%.4f %.4f " % l[uv_layer].uv[:])
del f
fw(']\n') # end 'point[]'
fw('\t\t}\n') # end 'TextureCoordinate'
# ---
fw('\t\ttexCoordIndex [ ')
i = None
for i in range(0, len(bm.faces) * 3, 3):
fw("%d %d %d -1 " % (i, i + 1, i + 2))
del i
fw(']\n') # end 'coordIndex[]'
fw('\t\tcoordIndex [ ')
f = fv = None
for f in bm.faces:
fv = f.verts[:]
fw("%d %d %d -1 " % (fv[0].index, fv[1].index, fv[2].index))
del f, fv
fw(']\n') # end 'coordIndex[]'
fw('\t}\n') # end 'IndexedFaceSet'
fw('}\n') # end 'Shape'
def save_object(fw, global_matrix,
depsgraph, scene, obj,
use_mesh_modifiers,
use_color, color_type,
use_uv,
path_mode, copy_set):
assert(obj.type == 'MESH')
if use_mesh_modifiers:
is_editmode = (obj.mode == 'EDIT')
if is_editmode:
bpy.ops.object.editmode_toggle()
me = obj.evaluated_get(depsgraph).to_mesh()
bm = bmesh.new()
bm.from_mesh(me)
if is_editmode:
bpy.ops.object.editmode_toggle()
else:
me = obj.data
if obj.mode == 'EDIT':
bm_orig = bmesh.from_edit_mesh(me)
bm = bm_orig.copy()
else:
bm = bmesh.new()
bm.from_mesh(me)
# triangulate first so tessellation matches the view-port.
bmesh.ops.triangulate(bm, faces=bm.faces)
bm.transform(global_matrix * obj.matrix_world)
# default empty
material_colors = []
uv_image = None
if use_color:
if color_type == 'VERTEX':
if bm.loops.layers.color.active is None:
# fallback to material
color_type = 'MATERIAL'
if color_type == 'MATERIAL':
if not me.materials:
use_color = False
else:
material_colors = [
"%.2f %.2f %.2f " % (m.diffuse_color[:] if m else (1.0, 1.0, 1.0))
for m in me.materials]
assert(color_type in {'VERTEX', 'MATERIAL'})
if use_uv:
if bm.loops.layers.uv.active is None:
use_uv = False
uv_image = object_utils.object_image_guess(obj, bm=bm)
if uv_image is None:
use_uv = False
save_bmesh(fw, bm,
use_color, color_type, material_colors,
use_uv, uv_image,
path_mode, copy_set)
bm.free()
def save(operator,
context,
filepath="",
global_matrix=None,
use_selection=False,
use_mesh_modifiers=True,
use_color=True,
color_type='MATERIAL',
use_uv=True,
path_mode='AUTO'):
scene = context.scene
depsgraph = context.evaluated_depsgraph_get()
# store files to copy
copy_set = set()
file = open(filepath, 'w', encoding='utf-8')
fw = file.write
fw('#VRML V2.0 utf8\n')
fw('#modeled using blender3d http://blender.org\n')
if use_selection:
objects = context.selected_objects
else:
objects = scene.objects
for obj in objects:
if obj.type == 'MESH':
fw("\n# %r\n" % obj.name)
save_object(fw, global_matrix,
depsgraph, scene, obj,
use_mesh_modifiers,
use_color, color_type,
use_uv,
path_mode, copy_set)
file.close()
# copy all collected files.
bpy_extras.io_utils.path_reference_copy(copy_set)
return {'FINISHED'}
|