-
Notifications
You must be signed in to change notification settings - Fork 5
/
mesh_edit.py
47 lines (37 loc) · 1.37 KB
/
mesh_edit.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
import bpy, bmesh
def bmesh_join(list_of_bmeshes, list_of_matrices, normal_update=False):
# sourcery skip: use-contextlib-suppress
""" 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 = bmesh.new()
add_vert = bm.verts.new
add_face = bm.faces.new
add_edge = bm.edges.new
for bm_to_add, matrix in zip(list_of_bmeshes, list_of_matrices):
bm_to_add.transform(matrix)
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:
for face in bm_to_add.faces:
add_face(tuple(bm.verts[i.index + offset] for i in face.verts))
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()
if normal_update:
bm.normal_update()
me = bpy.data.meshes.new("joined_mesh")
bm.to_mesh(me)
return me