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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
### 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, see <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
import bpy, bmesh
from bpy.props import FloatProperty
from mathutils import Vector
from mathutils.geometry import intersect_point_line
from .common_classes import (
SnapDrawn,
CharMap,
SnapNavigation,
g_snap_widget, #TODO: remove
)
from .common_utilities import (
get_units_info,
convert_distance,
snap_utilities,
)
if not __package__:
__package__ = "mesh_snap_utilities"
def get_closest_edge(bm, point, dist):
r_edge = None
for edge in bm.edges:
v1 = edge.verts[0].co
v2 = edge.verts[1].co
# Test the BVH (AABB) first
for i in range(3):
if v1[i] <= v2[i]:
isect = v1[i] - dist <= point[i] <= v2[i] + dist
else:
isect = v2[i] - dist <= point[i] <= v1[i] + dist
if not isect:
break
else:
ret = intersect_point_line(point, v1, v2)
if ret[1] < 0.0:
tmp = v1
elif ret[1] > 1.0:
tmp = v2
else:
tmp = ret[0]
new_dist = (point - tmp).length
if new_dist <= dist:
dist = new_dist
r_edge = edge
return r_edge
def get_loose_linked_edges(bmvert):
linked = [e for e in bmvert.link_edges if not e.link_faces]
for e in linked:
linked += [le for v in e.verts if not v.link_faces for le in v.link_edges if le not in linked]
return linked
def draw_line(self, bm_geom, location):
obj = self.main_snap_obj.data[0]
bm = self.main_bm
split_faces = set()
update_edit_mesh = False
if bm_geom is None:
vert = bm.verts.new(location)
self.list_verts.append(vert)
update_edit_mesh = True
elif isinstance(bm_geom, bmesh.types.BMVert):
if (bm_geom.co - location).length_squared < .001:
if self.list_verts == [] or self.list_verts[-1] != bm_geom:
self.list_verts.append(bm_geom)
else:
vert = bm.verts.new(location)
self.list_verts.append(vert)
update_edit_mesh = True
elif isinstance(bm_geom, bmesh.types.BMEdge):
self.list_edges.append(bm_geom)
ret = intersect_point_line(location, bm_geom.verts[0].co, bm_geom.verts[1].co)
if (ret[0] - location).length_squared < .001:
if ret[1] == 0.0:
vert = bm_geom.verts[0]
elif ret[1] == 1.0:
vert = bm_geom.verts[1]
else:
edge, vert = bmesh.utils.edge_split(bm_geom, bm_geom.verts[0], ret[1])
update_edit_mesh = True
self.list_verts.append(vert)
self.geom = vert # hack to highlight in the drawing
# self.list_edges.append(edge)
else: # constrain point is near
vert = bm.verts.new(location)
self.list_verts.append(vert)
update_edit_mesh = True
elif isinstance(bm_geom, bmesh.types.BMFace):
split_faces.add(bm_geom)
vert = bm.verts.new(location)
self.list_verts.append(vert)
update_edit_mesh = True
# draw, split and create face
if len(self.list_verts) >= 2:
v1, v2 = self.list_verts[-2:]
edge = bm.edges.get([v1, v2])
if edge:
self.list_edges.append(edge)
else:
if not v2.link_edges:
edge = bm.edges.new([v1, v2])
self.list_edges.append(edge)
else: # split face
v1_link_faces = v1.link_faces
v2_link_faces = v2.link_faces
if v1_link_faces and v2_link_faces:
split_faces.update(set(v1_link_faces).intersection(v2_link_faces))
else:
if v1_link_faces:
faces = v1_link_faces
co2 = v2.co.copy()
else:
faces = v2_link_faces
co2 = v1.co.copy()
for face in faces:
if bmesh.geometry.intersect_face_point(face, co2):
co = co2 - face.calc_center_median()
if co.dot(face.normal) < 0.001:
split_faces.add(face)
if split_faces:
edge = bm.edges.new([v1, v2])
self.list_edges.append(edge)
ed_list = get_loose_linked_edges(v2)
for face in split_faces:
facesp = bmesh.utils.face_split_edgenet(face, ed_list)
del split_faces
else:
if self.intersect:
facesp = bmesh.ops.connect_vert_pair(bm, verts=[v1, v2], verts_exclude=bm.verts)
# print(facesp)
if not self.intersect or not facesp['edges']:
edge = bm.edges.new([v1, v2])
self.list_edges.append(edge)
else:
for edge in facesp['edges']:
self.list_edges.append(edge)
update_edit_mesh = True
# create face
if self.create_face:
ed_list = set(self.list_edges)
for edge in v2.link_edges:
for vert in edge.verts:
if vert != v2 and vert in self.list_verts:
ed_list.add(edge)
break
else:
continue
# Inner loop had a break, break the outer
break
ed_list.update(get_loose_linked_edges(v2))
bmesh.ops.edgenet_fill(bm, edges=list(ed_list))
update_edit_mesh = True
# print('face created')
if update_edit_mesh:
obj.data.update_gpu_tag()
obj.data.update_tag()
obj.update_from_editmode()
obj.update_tag()
bmesh.update_edit_mesh(obj.data)
self.sctx.tag_update_drawn_snap_object(self.main_snap_obj)
#bm.verts.index_update()
bpy.ops.ed.undo_push(message="Undo draw line*")
return [obj.matrix_world @ v.co for v in self.list_verts]
class SnapUtilitiesLine(bpy.types.Operator):
"""Make Lines. Connect them to split faces"""
bl_idname = "mesh.make_line"
bl_label = "Line Tool"
bl_options = {'REGISTER'}
wait_for_input: bpy.props.BoolProperty(name="Wait for Input", default=True)
constrain_keys = {
'X': Vector((1,0,0)),
'Y': Vector((0,1,0)),
'Z': Vector((0,0,1)),
'RIGHT_SHIFT': 'shift',
'LEFT_SHIFT': 'shift',
}
def _exit(self, context):
del self.main_bm #avoids unpredictable crashs
del self.bm
del self.list_edges
del self.list_verts
del self.list_verts_co
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
context.area.header_text_set(None)
if not self.snap_widget:
self.sctx.free()
del self.draw_cache
else:
self.sctx = None
self.draw_cache = None
#Restore initial state
context.tool_settings.mesh_select_mode = self.select_mode
context.space_data.overlay.show_face_center = self.show_face_center
def _init_snap_context(self, context):
if self.sctx:
self.sctx.clear_snap_objects()
else:
#Create Snap Context
from .snap_context_l import SnapContext
self.sctx = SnapContext(context.region, context.space_data)
self.sctx.set_pixel_dist(12)
self.sctx.use_clip_planes(True)
if self.outer_verts:
for base in context.visible_bases:
self.sctx.add_obj(base.object, base.object.matrix_world)
self.sctx.set_snap_mode(True, True, self.snap_face)
if self.snap_widget:
self.geom = self.snap_widget.geom
self.type = self.snap_widget.type
self.location = self.snap_widget.loc
if self.snap_widget.snap_obj:
context.view_layer.objects.active = self.snap_widget.snap_obj.data[0]
else:
#init these variables to avoid errors
self.geom = None
self.type = 'OUT'
self.location = Vector()
self.prevloc = Vector()
self.list_verts = []
self.list_edges = []
self.list_verts_co = []
self.bool_update = False
self.vector_constrain = ()
self.len = 0
self.length_entered = ""
self.line_pos = 0
active_object = context.active_object
mesh = active_object.data
self.main_snap_obj = self.snap_obj = self.sctx._get_snap_obj_by_obj(active_object)
self.main_bm = self.bm = bmesh.from_edit_mesh(mesh)
def modal(self, context, event):
if self.navigation_ops.run(context, event, self.prevloc if self.vector_constrain else self.location):
return {'RUNNING_MODAL'}
context.area.tag_redraw()
if event.ctrl and event.type == 'Z' and event.value == 'PRESS':
del self.bm
del self.main_bm
bpy.ops.ed.undo()
bpy.ops.object.mode_set(mode='EDIT') # just to be sure
bpy.ops.mesh.select_all(action='DESELECT')
context.tool_settings.mesh_select_mode = (True, False, True)
context.space_data.overlay.show_face_center = True
self._init_snap_context(context)
self.sctx.update_all()
return {'RUNNING_MODAL'}
is_making_lines = bool(self.list_verts_co)
if event.type == 'MOUSEMOVE' or self.bool_update:
if self.rv3d.view_matrix != self.rotMat:
self.rotMat = self.rv3d.view_matrix.copy()
self.bool_update = True
snap_utilities.cache.snp_obj = None # hack for snap edge elemens update
else:
self.bool_update = False
mval = Vector((event.mouse_region_x, event.mouse_region_y))
self.snap_obj, self.prevloc, self.location, self.type, self.bm, self.geom, self.len = snap_utilities(
self.sctx,
self.main_snap_obj,
mval,
constrain=self.vector_constrain,
previous_vert=(self.list_verts[-1] if self.list_verts else None),
increment=self.incremental
)
if self.snap_to_grid and self.type == 'OUT':
loc = self.location / self.rd
self.location = Vector((round(loc.x),
round(loc.y),
round(loc.z))) * self.rd
if self.keyf8 and is_making_lines:
lloc = self.list_verts_co[-1]
view_vec, orig = self.sctx.last_ray
location = intersect_point_line(lloc, orig, (orig + view_vec))
vec = (location[0] - lloc)
ax, ay, az = abs(vec.x), abs(vec.y), abs(vec.z)
vec.x = ax > ay > az or ax > az > ay
vec.y = ay > ax > az or ay > az > ax
vec.z = az > ay > ax or az > ax > ay
if vec == Vector():
self.vector_constrain = None
else:
vc = lloc + vec
try:
if vc != self.vector_constrain[1]:
type = 'X' if vec.x else 'Y' if vec.y else 'Z' if vec.z else 'shift'
self.vector_constrain = [lloc, vc, type]
except:
type = 'X' if vec.x else 'Y' if vec.y else 'Z' if vec.z else 'shift'
self.vector_constrain = [lloc, vc, type]
if event.value == 'PRESS':
if is_making_lines and (event.ascii in CharMap.ascii or event.type in CharMap.type):
CharMap.modal(self, context, event)
elif event.type in self.constrain_keys:
self.bool_update = True
self.keyf8 = False
if self.vector_constrain and self.vector_constrain[2] == event.type:
self.vector_constrain = ()
else:
if event.shift:
if isinstance(self.geom, bmesh.types.BMEdge):
if is_making_lines:
loc = self.list_verts_co[-1]
self.vector_constrain = (loc, loc + self.geom.verts[1].co -
self.geom.verts[0].co, event.type)
else:
self.vector_constrain = [self.main_snap_obj.mat @ v.co for
v in self.geom.verts] + [event.type]
else:
if is_making_lines:
loc = self.list_verts_co[-1]
else:
loc = self.location
self.vector_constrain = [loc, loc + self.constrain_keys[event.type], event.type]
elif event.type == 'LEFTMOUSE':
if not is_making_lines and self.bm:
self.main_snap_obj = self.snap_obj
self.main_bm = self.bm
mat_inv = self.main_snap_obj.mat.inverted_safe()
point = mat_inv @ self.location
# with constraint the intersection can be in a different element of the selected one
geom2 = self.geom
if geom2:
geom2.select = False
if self.vector_constrain:
geom2 = get_closest_edge(self.main_bm, point, .001)
self.vector_constrain = None
self.list_verts_co = draw_line(self, geom2, point)
elif event.type == 'F8':
self.vector_constrain = None
self.keyf8 = self.keyf8 is False
elif event.value == 'RELEASE':
if event.type in {'RET', 'NUMPAD_ENTER'}:
if self.length_entered != "" and self.list_verts_co:
try:
text_value = bpy.utils.units.to_value(self.unit_system, 'LENGTH', self.length_entered)
vector = (self.location - self.list_verts_co[-1]).normalized()
location = (self.list_verts_co[-1] + (vector * text_value))
mat_inv = self.main_snap_obj.mat.inverted_safe()
self.list_verts_co = draw_line(self, self.geom, mat_inv @ location)
self.length_entered = ""
self.vector_constrain = None
except: # ValueError:
self.report({'INFO'}, "Operation not supported yet")
if not self.wait_for_input:
self._exit(context)
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
if not self.wait_for_input or not is_making_lines or event.type == 'ESC':
self._exit(context)
return {'FINISHED'}
else:
snap_utilities.cache.snp_obj = None # hack for snap edge elemens update
self.vector_constrain = None
self.list_edges = []
self.list_verts = []
self.list_verts_co = []
a = ""
if is_making_lines:
if self.length_entered:
pos = self.line_pos
a = 'length: ' + self.length_entered[:pos] + '|' + self.length_entered[pos:]
else:
length = self.len
length = convert_distance(length, self.uinfo)
a = 'length: ' + length
context.area.header_text_set(text = "hit: %.3f %.3f %.3f %s" % (*self.location, a))
if True or is_making_lines:
return {'RUNNING_MODAL'}
return {'PASS_THROUGH'}
def draw_callback_px(self):
if self.bm:
self.draw_cache.draw_elem(self.snap_obj, self.bm, self.geom)
self.draw_cache.draw(self.type, self.location, self.list_verts_co, self.vector_constrain, self.prevloc)
def invoke(self, context, event):
if context.space_data.type == 'VIEW_3D':
# print('name', __name__, __package__)
#Store current state
self.select_mode = context.tool_settings.mesh_select_mode[:]
self.show_face_center = context.space_data.overlay.show_face_center
#Modify the current state
bpy.ops.mesh.select_all(action='DESELECT')
context.tool_settings.mesh_select_mode = (True, False, True)
context.space_data.overlay.show_face_center = True
#Store values from 3d view context
self.rv3d = context.region_data
self.rotMat = self.rv3d.view_matrix.copy()
# self.obj_glmatrix = bgl.Buffer(bgl.GL_FLOAT, [4, 4], self.obj_matrix.transposed())
#Init event variables
self.keyf8 = False
self.snap_face = True
self.snap_widget = g_snap_widget[0]
if self.snap_widget is not None:
self.draw_cache = self.snap_widget.draw_cache
self.sctx = self.snap_widget.sctx
preferences = self.snap_widget.preferences
else:
preferences = context.user_preferences.addons[__package__].preferences
#Init DrawCache
self.draw_cache = SnapDrawn(
preferences.out_color,
preferences.face_color,
preferences.edge_color,
preferences.vert_color,
preferences.center_color,
preferences.perpendicular_color,
preferences.constrain_shift_color,
tuple(context.user_preferences.themes[0].user_interface.axis_x) + (1.0,),
tuple(context.user_preferences.themes[0].user_interface.axis_y) + (1.0,),
tuple(context.user_preferences.themes[0].user_interface.axis_z) + (1.0,)
)
self.sctx = None
#Configure the unit of measure
self.unit_system = context.scene.unit_settings.system
scale = context.scene.unit_settings.scale_length
separate_units = context.scene.unit_settings.use_separate
self.uinfo = get_units_info(scale, self.unit_system, separate_units)
scale /= context.space_data.overlay.grid_scale * preferences.relative_scale
self.rd = bpy.utils.units.to_value(self.unit_system, 'LENGTH', str(1 / scale))
self.intersect = preferences.intersect
self.create_face = preferences.create_face
self.outer_verts = preferences.outer_verts
self.snap_to_grid = preferences.increments_grid
self.incremental = bpy.utils.units.to_value(self.unit_system, 'LENGTH', str(preferences.incremental))
self.navigation_ops = SnapNavigation(context, True)
self._init_snap_context(context)
#modals
context.window_manager.modal_handler_add(self)
if not self.wait_for_input:
self.modal(context, event)
self._handle = bpy.types.SpaceView3D.draw_handler_add(self.draw_callback_px, (), 'WINDOW', 'POST_VIEW')
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Active space must be a View3d")
return {'CANCELLED'}
def register():
bpy.utils.register_class(SnapUtilitiesLine)
if __name__ == "__main__":
register()
|