-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbmesh_utils.py
117 lines (105 loc) · 3.77 KB
/
bmesh_utils.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
# -*- 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 _end(bm, o):
"""
private, end bmesh editing of active object
"""
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):
bm = BmeshEdit._start(context, o)
bm.clear()
for v in verts:
bm.verts.new(v)
bm.verts.ensure_lookup_table()
for f in faces:
bm.faces.new([bm.verts[i] for i in f])
bm.faces.ensure_lookup_table()
if matids is not None:
BmeshEdit._matids(bm, matids)
if uvs is not None:
BmeshEdit._uvs(bm, uvs)
if weld:
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.001)
BmeshEdit._end(bm, o)
if clean:
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.delete_loose()
bpy.ops.object.mode_set(mode='OBJECT')
@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)