forked from animate1978/MB-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_ops.py
409 lines (359 loc) · 13.8 KB
/
mesh_ops.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# MB-Lab
# MB-Lab fork website : https://github.com/animate1978/MB-Lab
# ##### 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 3
# 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 #####
#
# Teto, for MB-Lab
import logging
import bpy
import bmesh
from . import file_ops
logger = logging.getLogger(__name__)
class MeshHistory:
def __init__(self, name="", obj=None):
self.vertices_history = []
self.edges_history = []
self.faces_history = []
self.vertices_recover = []
self.edges_recover = []
self.faces_recover = []
self.set_name(name)
self.counter_vert = 0
self.counter_edge = 0
self.counter_face = 0
self.object = obj
def clear(self):
self.vertices_history = []
self.edges_history = []
self.faces_history = []
self.object = None
# reset recovery with actual values.
def clear_recover(self):
self.vertices_recover = self.vertices_history.copy()
self.edges_recover = self.edges_history.copy()
self.faces_recover = self.faces_history.copy()
def has_elements(self, type='VERTEX'):
if type == "EDGE":
return len(self.edges_history) > 0
elif type == "FACE":
return len(self.faces_history) > 0
return len(self.vertices_history) > 0
def get_length(self, type='VERTEX'):
if type == "EDGE":
return len(self.edges_history)
elif type == "FACE":
return len(self.faces_history)
return len(self.vertices_history)
def set_name(self, name = ""):
self.name = name
def set_object(self, obj):
if self.object == None:
self.object = obj
def add_selection(self):
bm = bmesh.from_edit_mesh(self.object.data)
element = bm.select_history
if element == None: # Could happen
return
e = element.active
if e == None: # Could happen
return
index = e.index
if isinstance(e, bmesh.types.BMVert):
self.vertices_history.append(index)
self.counter_vert = len(self.vertices_history)-1
elif isinstance(e, bmesh.types.BMEdge):
self.edges_history.append(index)
self.counter_edge = len(self.edges_history)-1
elif isinstance(e, bmesh.types.BMFace):
self.faces_history.append(index)
self.counter_face = len(self.faces_history)-1
else:
print('Not found :')
print(type(e))
def get_previous(self, type='VERTEX'):
if type == 'VERTEX':
if self.counter_vert > 0:
self.counter_vert -= 1
return get_history(type, self.counter_vert)
elif type == 'EDGE':
if self.counter_edge > 0:
self.counter_edge -= 1
return get_history(type, self.counter_edge)
elif type == 'FACE':
if self.counter_face > 0:
self.counter_face -= 1
return get_history(type, self.counter_face)
return []
def get_next(self, type='VERTEX'):
if type == 'VERTEX':
if self.counter_vert < len(self.vertices_history)-1:
self.counter_vert += 1
return get_history(type, self.counter_vert)
elif type == 'EDGE':
if self.counter_edge < len(self.edges_history)-1:
self.counter_edge += 1
return get_history(type, self.counter_edge)
elif type == 'FACE':
if self.counter_face < len(self.faces_history)-1:
self.counter_face += 1
return get_history(type, self.counter_face)
return []
def get_last(self, type='VERTEX'):
if type == 'VERTEX':
self.counter_vert = len(self.vertices_history)-1
return get_history(type, self.counter_vert)
elif type == 'EDGE':
self.counter_edge = len(self.edges_history)-1
return get_history(type, self.counter_edge)
elif type == 'FACE':
self.counter_face = len(self.faces_history)-1
return get_history(type, self.counter_face)
return []
def get_first(self, type='VERTEX'):
if type == 'VERTEX':
self.counter_vert = 0
return get_history(type, self.counter_vert)
elif type == 'EDGE':
self.counter_edge = 0
return get_history(type, self.counter_edge)
elif type == 'FACE':
self.counter_face = 0
return get_history(type, self.counter_face)
return []
def get_history(self, type='VERTEX', index=-1):
if type == 'VERTEX':
if index > -1:
return [self.vertices_history[index]]
return self.vertices_history
elif type == 'EDGE':
if index > -1:
return [self.edges_history[index]]
return self.edges_history
elif type == 'FACE':
if index > -1:
return [self.faces_history[index]]
return self.faces_history
return []
def select_previous(self, type='VERTEX'):
select_in_a_mesh(self.object, self.get_previous(type), type)
def select_next(self, type='VERTEX'):
select_in_a_mesh(self.object, self.get_next(type), type)
def select_first(self, type='VERTEX'):
select_in_a_mesh(self.object, self.get_first(type), type)
def select_last(self, type='VERTEX'):
select_in_a_mesh(self.object, self.get_last(type), type)
def select_all(self, type='VERTEX'):
select_in_a_mesh(self.object, self.get_history(type), type)
# For conveniency
def unselect_all(self):
bpy.ops.object.select_all(action='DESELECT')
# If end < 0 only start is removed.
def remove(self, type, start, end=-1):
if start < 0:
return []
if end < 0:
if type == 'EDGE':
return self.edges_history.pop(start)
elif type == 'FACE':
return self.faces_history.pop(start)
return self.vertices_history.pop(start)
if type == 'EDGE':
returned = self.edges_history[start, end]
del self.edges_history[start, end]
return returned
elif type == 'FACE':
returned = self.faces_history[start, end]
del self.faces_history[start, end]
return returned
returned = self.vertices_history[start, end]
del self.vertices_history[start, end]
return returned
# Here index = the 'name' of index.
def remove_index(self, index, type='VERTEX'):
if type == 'EDGE':
for i in range(len(self.edges_history)):
if self.edges_history[i] == index:
return self.remove(type, i)
elif type == 'FACE':
for i in range(len(self.faces_history)):
if self.faces_history[i] == index:
return self.remove(type, i)
else:
for i in range(len(self.vertices_history)):
if self.vertices_history[i] == index:
return self.remove(type, i)
def remove_selected(self):
bm = bmesh.from_edit_mesh(self.object.data)
element = bm.select_history
if element == None: # Could happen
return
e = element.active
if e == None: # Could happen
return
index = e.index
if isinstance(e, bmesh.types.BMVert):
return self.remove_index(index, 'VERTEX')
elif isinstance(e, bmesh.types.BMEdge):
return self.remove_index(index, 'EDGE')
elif isinstance(e, bmesh.types.BMFace):
return self.remove_index(index, 'FACE')
else:
print('Not found :')
print(type(e))
def remove_all(self, type='VERTEX'):
if type == 'EDGE':
self.edges_history.clear()
elif type == 'FACE':
self.faces_history.clear()
else :
self.vertices_history.clear()
def set(self, type, indices):
if type == 'EDGES':
self.edges_history = indices.copy()
self.edges_recover = indices.copy()
elif type == 'FACES':
self.faces_history = indices.copy()
self.faces_recover = indices.copy()
else:
self.vertices_history = indices.copy()
self.vertices_recover = indices.copy()
def recover(self, type):
if type == 'EDGES':
self.edges_history = self.edges_recover.copy()
elif type == 'FACES':
self.faces_history = self.faces_recover.copy()
else:
self.vertices_history = self.vertices_recover.copy()
# The active element is added,
# and first in the list is removed.
def push_selection(self, type='VERTEX'):
self.add_selection()
self.remove(type, 0)
# A dictionnary with the name as key.
# Value is a dict with keys : 'VERTEX'; 'EDGE'; 'FACE'
# Value for each key are lists with indices.
def set_standalone_form(self, hist_dict, obj = None):
if len(hist_dict) < 1:
return
self.vertices_history = []
self.edges_history = []
self.faces_history = []
if obj != None:
self.object = obj
for key, item in hist_dict.items():
self.name = key
for sub_key, sub_item in item.items():
if sub_key == 'VERTEX':
self.vertices_history = sub_item
elif sub_key == 'EDGE':
self.edges_history = sub_item
elif sub_key == 'FACE':
self.faces_history = sub_item
return # because there's just 1 key+value...
def get_standalone_form(self):
item = {
'VERTEX' : self.vertices_history,
'EDGE' : self.edges_history,
'FACE' : self.faces_history
}
return {self.name : item}
def get_measures_file_form(self):
return self.vertices_history
class MeshHandling:
def __init__(self, the_name="", obj=None):
self.history = {}
self.name = the_name # Name of the file
self.set_object(obj)
def set_object(self, obj):
if obj == None:
return
self.object = obj
if len(self.history) > 0:
for item in self.history.values():
item.set_object(obj)
def get_mesh_history(self, name):
if name in self.history:
return self.history[name]
return None
def set_mesh_history(self, hist):
if hist == None:
return
self.history[hist.name] = hist
def create_mesh_history(self, name):
hist = MeshHistory(name, self.object)
self.set_mesh_history(hist)
return self.get_mesh_history(name)
def get_for_measures_save(self):
return_dict = {}
for value in self.history.values():
key, item = value.get_measures_file_form()
return_dict[key] = item
return return_dict
def get_histories(self):
return self.history
# -----------------------------------------------------------
# General methods
# -----------------------------------------------------------
# Select vertices, lines, faces, etc in a mesh.
# Rough method, no many checks.
def select_global(mesh, int_list, unselect_all=True, vertices=False, edges=False, faces=False):
if len(int_list) < 1 or mesh == None:
return
if unselect_all:
bpy.ops.mesh.select_all(action='DESELECT')
bm = bmesh.from_edit_mesh(mesh.data)
if vertices:
for vert in bm.verts:
if vert.index in int_list:
vert.select = True
if edges:
for ed in bm.edges:
if ed.index in int_list:
ed.select = True
if faces:
for fa in bm.faces:
if fa.index in int_list:
fa.select = True
bmesh.update_edit_mesh(mesh.data, True)
def select_in_a_mesh(mesh, int_list, type, unselect_all=True):
if type == 'VERTEX':
select_global(mesh, int_list, unselect_all, vertices=True)
elif type == 'EDGE':
select_global(mesh, int_list, unselect_all, edges=True)
elif type == 'FACE':
select_global(mesh, int_list, unselect_all, faces=True)
def unselect_all():
bpy.ops.mesh.select_all(action='DESELECT')
# No security checks...
# The file must be a dict with names of historic selection as keys
# The value is itsel a dict with 'VERTEX', 'EDGE' or 'FACE' as keys
# and a list with indices in integer.
# Return a dict of MeshHistory
def load_standalone(filepath, obj=None):
file = file_ops.load_json_data(filepath, "Load history from file")
if file == None:
return
history_list = {}
for key, item in file.items():
temp = MeshHistory(key, obj)
temp.set_standalone_form(item)
history_list[temp.name] = temp
return history_list
# For conveniency
def save_in_json(filepath, history_list):
file_ops.save_json_data(filepath, history_list)