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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# -*- coding:utf-8 -*-
# ##### 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 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
import bpy
import bmesh
class BmeshEdit():
@staticmethod
def _start(context, o):
"""
private, start bmesh editing of active object
"""
o.select = True
context.scene.objects.active = o
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(o.data)
bm.verts.ensure_lookup_table()
bm.faces.ensure_lookup_table()
return bm
@staticmethod
def bmesh_join(context, o, list_of_bmeshes, normal_update=False):
"""
takes as input a list of bm references and outputs a single merged bmesh
allows an additional 'normal_update=True' to force _normal_ calculations.
"""
bm = BmeshEdit._start(context, o)
add_vert = bm.verts.new
add_face = bm.faces.new
add_edge = bm.edges.new
for bm_to_add in list_of_bmeshes:
offset = len(bm.verts)
for v in bm_to_add.verts:
add_vert(v.co)
bm.verts.index_update()
bm.verts.ensure_lookup_table()
if bm_to_add.faces:
layer = bm_to_add.loops.layers.uv.verify()
dest = bm.loops.layers.uv.verify()
for face in bm_to_add.faces:
f = add_face(tuple(bm.verts[i.index + offset] for i in face.verts))
f.material_index = face.material_index
for j, loop in enumerate(face.loops):
f.loops[j][dest].uv = loop[layer].uv
bm.faces.index_update()
if bm_to_add.edges:
for edge in bm_to_add.edges:
edge_seq = tuple(bm.verts[i.index + offset] for i in edge.verts)
try:
add_edge(edge_seq)
except ValueError:
# edge exists!
pass
bm.edges.index_update()
# cleanup
for old_bm in list_of_bmeshes:
old_bm.free()
if normal_update:
bm.normal_update()
BmeshEdit._end(bm, o)
@staticmethod
def _end(bm, o):
"""
private, end bmesh editing of active object
"""
bm.normal_update()
bmesh.update_edit_mesh(o.data, True)
bpy.ops.object.mode_set(mode='OBJECT')
bm.free()
@staticmethod
def _matids(bm, matids):
for i, matid in enumerate(matids):
bm.faces[i].material_index = matid
@staticmethod
def _uvs(bm, uvs):
layer = bm.loops.layers.uv.verify()
l_i = len(uvs)
for i, face in enumerate(bm.faces):
if i > l_i:
raise RuntimeError("Missing uvs for face {}".format(i))
l_j = len(uvs[i])
for j, loop in enumerate(face.loops):
if j > l_j:
raise RuntimeError("Missing uv {} for face {}".format(j, i))
loop[layer].uv = uvs[i][j]
@staticmethod
def _verts(bm, verts):
for i, v in enumerate(verts):
bm.verts[i].co = v
@staticmethod
def buildmesh(context, o, verts, faces,
matids=None, uvs=None, weld=False,
clean=False, auto_smooth=True, temporary=False):
if temporary:
bm = bmesh.new()
else:
bm = BmeshEdit._start(context, o)
bm.clear()
for v in verts:
bm.verts.new(v)
bm.verts.index_update()
bm.verts.ensure_lookup_table()
for f in faces:
bm.faces.new([bm.verts[i] for i in f])
bm.faces.index_update()
bm.faces.ensure_lookup_table()
if matids is not None:
BmeshEdit._matids(bm, matids)
if uvs is not None:
BmeshEdit._uvs(bm, uvs)
if temporary:
return bm
if weld:
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.001)
BmeshEdit._end(bm, o)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
if auto_smooth:
bpy.ops.mesh.faces_shade_smooth()
o.data.use_auto_smooth = True
else:
bpy.ops.mesh.faces_shade_flat()
if clean:
bpy.ops.mesh.delete_loose()
bpy.ops.object.mode_set(mode='OBJECT')
@staticmethod
def addmesh(context, o, verts, faces, matids=None, uvs=None, weld=False, clean=False, auto_smooth=True):
bm = BmeshEdit._start(context, o)
nv = len(bm.verts)
nf = len(bm.faces)
for v in verts:
bm.verts.new(v)
bm.verts.ensure_lookup_table()
for f in faces:
bm.faces.new([bm.verts[nv + i] for i in f])
bm.faces.ensure_lookup_table()
if matids is not None:
for i, matid in enumerate(matids):
bm.faces[nf + i].material_index = matid
if uvs is not None:
layer = bm.loops.layers.uv.verify()
for i, face in enumerate(bm.faces[nf:]):
for j, loop in enumerate(face.loops):
loop[layer].uv = uvs[i][j]
if weld:
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.001)
BmeshEdit._end(bm, o)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
if auto_smooth:
bpy.ops.mesh.faces_shade_smooth()
o.data.use_auto_smooth = True
else:
bpy.ops.mesh.faces_shade_flat()
if clean:
bpy.ops.mesh.delete_loose()
bpy.ops.object.mode_set(mode='OBJECT')
@staticmethod
def bevel(context, o,
offset,
offset_type=0,
segments=1,
profile=0.5,
vertex_only=False,
clight_overlap=True,
material=-1,
use_selection=True):
"""
/* Bevel offset_type slot values */
enum {
BEVEL_AMT_OFFSET,
BEVEL_AMT_WIDTH,
BEVEL_AMT_DEPTH,
BEVEL_AMT_PERCENT
};
"""
bm = bmesh.new()
bm.from_mesh(o.data)
bm.verts.ensure_lookup_table()
if use_selection:
geom = [v for v in bm.verts if v.select]
geom.extend([ed for ed in bm.edges if ed.select])
else:
geom = bm.verts[:]
geom.extend(bm.edges[:])
bmesh.ops.bevel(bm,
geom=geom,
offset=offset,
offset_type=offset_type,
segments=segments,
profile=profile,
vertex_only=vertex_only,
clight_overlap=clight_overlap,
material=material)
bm.to_mesh(o.data)
bm.free()
@staticmethod
def bissect(context, o,
plane_co,
plane_no,
dist=0.001,
use_snap_center=False,
clear_outer=True,
clear_inner=False
):
bm = bmesh.new()
bm.from_mesh(o.data)
bm.verts.ensure_lookup_table()
geom = bm.verts[:]
geom.extend(bm.edges[:])
geom.extend(bm.faces[:])
bmesh.ops.bisect_plane(bm,
geom=geom,
dist=dist,
plane_co=plane_co,
plane_no=plane_no,
use_snap_center=False,
clear_outer=clear_outer,
clear_inner=clear_inner
)
bm.to_mesh(o.data)
bm.free()
@staticmethod
def solidify(context, o, amt, floor_bottom=False, altitude=0):
bm = bmesh.new()
bm.from_mesh(o.data)
bm.verts.ensure_lookup_table()
geom = bm.faces[:]
bmesh.ops.solidify(bm, geom=geom, thickness=amt)
if floor_bottom:
for v in bm.verts:
if not v.select:
v.co.z = altitude
bm.to_mesh(o.data)
bm.free()
@staticmethod
def verts(context, o, verts):
"""
update vertex position of active object
"""
bm = BmeshEdit._start(context, o)
BmeshEdit._verts(bm, verts)
BmeshEdit._end(bm, o)
@staticmethod
def aspect(context, o, matids, uvs):
"""
update material id and uvmap of active object
"""
bm = BmeshEdit._start(context, o)
BmeshEdit._matids(bm, matids)
BmeshEdit._uvs(bm, uvs)
BmeshEdit._end(bm, o)
|