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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
|
### 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
import bgl
from mathutils import Vector
from .common_utilities import (
convert_distance,
get_units_info,
snap_utilities,
)
class SnapDrawn():
def __init__(self, out_color, face_color,
edge_color, vert_color, center_color,
perpendicular_color, constrain_shift_color,
axis_x_color, axis_y_color, axis_z_color):
import gpu
self.out_color = out_color
self.face_color = face_color
self.edge_color = edge_color
self.vert_color = vert_color
self.center_color = center_color
self.perpendicular_color = perpendicular_color
self.constrain_shift_color = constrain_shift_color
self.axis_x_color = axis_x_color
self.axis_y_color = axis_y_color
self.axis_z_color = axis_z_color
self._format_pos = gpu.types.GPUVertFormat()
self._format_pos.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
self._format_pos_and_color = gpu.types.GPUVertFormat()
self._format_pos_and_color.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
self._format_pos_and_color.attr_add(id="color", comp_type='F32', len=4, fetch_mode='FLOAT')
self._program_unif_col = gpu.shader.from_builtin("3D_UNIFORM_COLOR")
self._program_smooth_col = gpu.shader.from_builtin("3D_SMOOTH_COLOR")
self._batch_point = None
def batch_line_strip_create(self, coords):
from gpu.types import (
GPUVertBuf,
GPUBatch,
)
vbo = GPUVertBuf(self._format_pos, len = len(coords))
vbo.attr_fill(0, data = coords)
batch_lines = GPUBatch(type = "LINE_STRIP", buf = vbo)
return batch_lines
def batch_lines_smooth_color_create(self, coords, colors):
from gpu.types import (
GPUVertBuf,
GPUBatch,
)
vbo = GPUVertBuf(self._format_pos_and_color, len = len(coords))
vbo.attr_fill(0, data = coords)
vbo.attr_fill(1, data = colors)
batch_lines = GPUBatch(type = "LINES", buf = vbo)
return batch_lines
def batch_triangles_create(self, coords):
from gpu.types import (
GPUVertBuf,
GPUBatch,
)
vbo = GPUVertBuf(self._format_pos, len = len(coords))
vbo.attr_fill(0, data = coords)
batch_tris = GPUBatch(type = "TRIS", buf = vbo)
return batch_tris
def batch_point_get(self):
if self._batch_point is None:
from gpu.types import (
GPUVertBuf,
GPUBatch,
)
vbo = GPUVertBuf(self._format_pos, len = 1)
vbo.attr_fill(0, ((0.0, 0.0, 0.0),))
self._batch_point = GPUBatch(type = "POINTS", buf = vbo)
return self._batch_point
def draw(self, type, location, list_verts_co, vector_constrain, prevloc):
import gpu
# draw 3d point OpenGL in the 3D View
bgl.glEnable(bgl.GL_BLEND)
gpu.matrix.push()
self._program_unif_col.bind()
if list_verts_co:
# draw 3d line OpenGL in the 3D View
bgl.glDepthRange(0, 0.9999)
bgl.glLineWidth(3.0)
batch = self.batch_line_strip_create([v.to_tuple() for v in list_verts_co] + [location.to_tuple()])
self._program_unif_col.uniform_float("color", (1.0, 0.8, 0.0, 0.5))
batch.draw(self._program_unif_col)
del batch
bgl.glDisable(bgl.GL_DEPTH_TEST)
point_batch = self.batch_point_get()
if vector_constrain:
if prevloc:
bgl.glPointSize(5)
gpu.matrix.translate(prevloc)
self._program_unif_col.uniform_float("color", (1.0, 1.0, 1.0, 0.5))
point_batch.draw(self._program_unif_col)
gpu.matrix.translate(-prevloc)
if vector_constrain[2] == 'X':
Color4f = self.axis_x_color
elif vector_constrain[2] == 'Y':
Color4f = self.axis_y_color
elif vector_constrain[2] == 'Z':
Color4f = self.axis_z_color
else:
Color4f = self.constrain_shift_color
else:
if type == 'OUT':
Color4f = self.out_color
elif type == 'FACE':
Color4f = self.face_color
elif type == 'EDGE':
Color4f = self.edge_color
elif type == 'VERT':
Color4f = self.vert_color
elif type == 'CENTER':
Color4f = self.center_color
elif type == 'PERPENDICULAR':
Color4f = self.perpendicular_color
else: # type == None
Color4f = self.out_color
bgl.glPointSize(10)
gpu.matrix.translate(location)
self._program_unif_col.uniform_float("color", Color4f)
point_batch.draw(self._program_unif_col)
# restore opengl defaults
bgl.glDepthRange(0.0, 1.0)
bgl.glPointSize(1.0)
bgl.glLineWidth(1.0)
bgl.glEnable(bgl.GL_DEPTH_TEST)
bgl.glDisable(bgl.GL_BLEND)
gpu.matrix.pop()
def draw_elem(self, snap_obj, bm, elem):
#TODO: Cache coords (because antialiasing)
import gpu
from bmesh.types import(
BMVert,
BMEdge,
BMFace,
)
# draw 3d point OpenGL in the 3D View
bgl.glEnable(bgl.GL_BLEND)
bgl.glDisable(bgl.GL_DEPTH_TEST)
with gpu.matrix.push_pop():
gpu.matrix.multiply_matrix(snap_obj.mat)
if isinstance(elem, BMVert):
if elem.link_edges:
import numpy as np
color = self.vert_color
edges = np.empty((len(elem.link_edges), 2), [("pos", "f4", 3), ("color", "f4", 4)])
edges["pos"][:, 0] = elem.co
edges["pos"][:, 1] = [e.other_vert(elem).co for e in elem.link_edges]
edges["color"][:, 0] = color
edges["color"][:, 1] = (color[0], color[1], color[2], 0.0)
edges.shape = -1
self._program_smooth_col.bind()
bgl.glLineWidth(3.0)
batch = self.batch_lines_smooth_color_create(edges["pos"], edges["color"])
batch.draw(self._program_smooth_col)
bgl.glLineWidth(1.0)
else:
self._program_unif_col.bind()
if isinstance(elem, BMEdge):
self._program_unif_col.uniform_float("color", self.edge_color)
bgl.glLineWidth(3.0)
batch = self.batch_line_strip_create([v.co for v in elem.verts])
batch.draw(self._program_unif_col)
bgl.glLineWidth(1.0)
elif isinstance(elem, BMFace):
if len(snap_obj.data) == 2:
face_color = self.face_color[0], self.face_color[1], self.face_color[2], self.face_color[3] * 0.2
self._program_unif_col.uniform_float("color", face_color)
tris = snap_obj.data[1].get_loop_tri_co_by_bmface(bm, elem)
tris.shape = (-1, 3)
batch = self.batch_triangles_create(tris)
batch.draw(self._program_unif_col)
# restore opengl defaults
bgl.glEnable(bgl.GL_DEPTH_TEST)
bgl.glDisable(bgl.GL_BLEND)
class SnapNavigation():
@staticmethod
def debug_key(key):
for member in dir(key):
print(member, getattr(key, member))
@staticmethod
def convert_to_flag(shift, ctrl, alt):
return (shift << 0) | (ctrl << 1) | (alt << 2)
def __init__(self, context, use_ndof):
# TO DO:
# 'View Orbit', 'View Pan', 'NDOF Orbit View', 'NDOF Pan View'
self.use_ndof = use_ndof and context.preferences.inputs.use_ndof
self._rotate = set()
self._move = set()
self._zoom = set()
if self.use_ndof:
self._ndof_all = set()
self._ndof_orbit = set()
self._ndof_orbit_zoom = set()
self._ndof_pan = set()
for key in context.window_manager.keyconfigs.user.keymaps['3D View'].keymap_items:
if key.idname == 'view3d.rotate':
self._rotate.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type, key.value))
elif key.idname == 'view3d.move':
self._move.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type, key.value))
elif key.idname == 'view3d.zoom':
if key.type == 'WHEELINMOUSE':
self._zoom.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), 'WHEELUPMOUSE', key.value, key.properties.delta))
elif key.type == 'WHEELOUTMOUSE':
self._zoom.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), 'WHEELDOWNMOUSE', key.value, key.properties.delta))
else:
self._zoom.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type, key.value, key.properties.delta))
elif self.use_ndof:
if key.idname == 'view3d.ndof_all':
self._ndof_all.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type))
elif key.idname == 'view3d.ndof_orbit':
self._ndof_orbit.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type))
elif key.idname == 'view3d.ndof_orbit_zoom':
self._ndof_orbit_zoom.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type))
elif key.idname == 'view3d.ndof_pan':
self._ndof_pan.add((self.convert_to_flag(key.shift, key.ctrl, key.alt), key.type))
def run(self, context, event, snap_location):
evkey = (self.convert_to_flag(event.shift, event.ctrl, event.alt), event.type, event.value)
if evkey in self._rotate:
if snap_location:
bpy.ops.view3d.rotate_custom_pivot('INVOKE_DEFAULT', pivot=snap_location)
else:
bpy.ops.view3d.rotate('INVOKE_DEFAULT', use_mouse_init=True)
return True
if evkey in self._move:
#if event.shift and self.vector_constrain and \
# self.vector_constrain[2] in {'RIGHT_SHIFT', 'LEFT_SHIFT', 'shift'}:
# self.vector_constrain = None
bpy.ops.view3d.move('INVOKE_DEFAULT')
return True
for key in self._zoom:
if evkey == key[0:3]:
if snap_location and key[3]:
bpy.ops.view3d.zoom_custom_target('INVOKE_DEFAULT', delta=key[3], target=snap_location)
else:
bpy.ops.view3d.zoom('INVOKE_DEFAULT', delta=key[3])
return True
if self.use_ndof:
ndofkey = evkey[:2]
if ndofkey in self._ndof_all:
bpy.ops.view3d.ndof_all('INVOKE_DEFAULT')
return True
if ndofkey in self._ndof_orbit:
bpy.ops.view3d.ndof_orbit('INVOKE_DEFAULT')
return True
if ndofkey in self._ndof_orbit_zoom:
bpy.ops.view3d.ndof_orbit_zoom('INVOKE_DEFAULT')
return True
if ndofkey in self._ndof_pan:
bpy.ops.view3d.ndof_pan('INVOKE_DEFAULT')
return True
return False
class CharMap:
ascii = {
".", ",", "-", "+", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "0",
"c", "m", "d", "k", "h", "a",
" ", "/", "*", "'", "\""
# "="
}
type = {
'BACK_SPACE', 'DEL',
'LEFT_ARROW', 'RIGHT_ARROW'
}
def __init__(self, context):
scale = context.scene.unit_settings.scale_length
separate_units = context.scene.unit_settings.use_separate
self.unit_system = context.scene.unit_settings.system
self.uinfo = get_units_info(scale, self.unit_system, separate_units)
self.clear()
def modal_(self, context, event):
if event.value == 'PRESS':
type = event.type
ascii = event.ascii
if (type in self.type) or (ascii in self.ascii):
if ascii:
pos = self.line_pos
if ascii == ",":
ascii = "."
self.length_entered = self.length_entered[:pos] + ascii + self.length_entered[pos:]
self.line_pos += 1
if self.length_entered:
pos = self.line_pos
if type == 'BACK_SPACE':
self.length_entered = self.length_entered[:pos - 1] + self.length_entered[pos:]
self.line_pos -= 1
elif type == 'DEL':
self.length_entered = self.length_entered[:pos] + self.length_entered[pos + 1:]
elif type == 'LEFT_ARROW':
self.line_pos = (pos - 1) % (len(self.length_entered) + 1)
elif type == 'RIGHT_ARROW':
self.line_pos = (pos + 1) % (len(self.length_entered) + 1)
try:
self.length_entered_value = bpy.utils.units.to_value(
self.unit_system, 'LENGTH', self.length_entered)
except: # ValueError:
self.length_entered_value = 0.0 #invalid
#self.report({'INFO'}, "Operation not supported yet")
else:
self.length_entered_value = 0.0
return True
return False
def get_converted_length_str(self, length):
if self.length_entered:
pos = self.line_pos
ret = self.length_entered[:pos] + '|' + self.length_entered[pos:]
else:
ret = convert_distance(length, self.uinfo)
return ret
def clear(self):
self.length_entered = ''
self.length_entered_value = 0.0
self.line_pos = 0
class VIEW3D_OT_rotate_custom_pivot(bpy.types.Operator):
bl_idname = "view3d.rotate_custom_pivot"
bl_label = "Rotate the view"
bl_options = {'BLOCKING', 'GRAB_CURSOR'}
pivot: bpy.props.FloatVectorProperty("Pivot", subtype='XYZ')
g_up_axis: bpy.props.FloatVectorProperty("up_axis", default=(0.0, 0.0, 1.0), subtype='XYZ')
sensitivity: bpy.props.FloatProperty("sensitivity", default=0.007)
def modal(self, context, event):
from mathutils import Matrix
if event.value == 'PRESS' and event.type in {'MOUSEMOVE', 'INBETWEEN_MOUSEMOVE'}:
dx = self.init_coord[0] - event.mouse_region_x
dy = self.init_coord[1] - event.mouse_region_y
rot_ver = Matrix.Rotation(-dx * self.sensitivity, 3, self.g_up_axis)
rot_hor = Matrix.Rotation(dy * self.sensitivity, 3, self.view_rot[0])
rot_mat = rot_hor @ rot_ver
view_matrix = self.view_rot @ rot_mat
pos = self.pos1 @ rot_mat + self.pivot
qua = view_matrix.to_quaternion()
qua.invert()
self.rv3d.view_location = pos
self.rv3d.view_rotation = qua
context.area.tag_redraw()
return {'RUNNING_MODAL'}
return {'FINISHED'}
def invoke(self, context, event):
self.rv3d = context.region_data
self.init_coord = event.mouse_region_x, event.mouse_region_y
self.pos1 = self.rv3d.view_location - self.pivot
self.view_rot = self.rv3d.view_matrix.to_3x3()
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
class VIEW3D_OT_zoom_custom_target(bpy.types.Operator):
bl_idname = "view3d.zoom_custom_target"
bl_label = "Zoom the view"
bl_options = {'BLOCKING', 'GRAB_CURSOR'}
target: bpy.props.FloatVectorProperty("target", subtype='XYZ')
delta: bpy.props.IntProperty("delta", default=0)
step_factor = 0.333
def modal(self, context, event):
if event.value == 'PRESS' and event.type in {'MOUSEMOVE', 'INBETWEEN_MOUSEMOVE'}:
if not hasattr(self, "init_mouse_region_y"):
self.init_mouse_region_y = event.mouse_region_y
self.heigt_up = context.area.height - self.init_mouse_region_y
self.rv3d.view_location = self.target
fac = (event.mouse_region_y - self.init_mouse_region_y) / self.heigt_up
ret = 'RUNNING_MODAL'
else:
fac = self.step_factor * self.delta
ret = 'FINISHED'
self.rv3d.view_location = self.init_loc + (self.target - self.init_loc) * fac
self.rv3d.view_distance = self.init_dist - self.init_dist * fac
context.area.tag_redraw()
return {ret}
def invoke(self, context, event):
v3d = context.space_data
dist_range = (v3d.clip_start, v3d.clip_end)
self.rv3d = context.region_data
self.init_dist = self.rv3d.view_distance
if ((self.delta <= 0 and self.init_dist < dist_range[1]) or
(self.delta > 0 and self.init_dist > dist_range[0])):
self.init_loc = self.rv3d.view_location.copy()
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
return {'FINISHED'}
class SnapUtilities:
# __slots__ = (
# "sctx",
# "draw_cache",
# "outer_verts",
# "snap_face",
# "snap_to_grid",
# "unit_system",
# "rd",
# "incremental",
# )
constrain_keys = {
'X': Vector((1,0,0)),
'Y': Vector((0,1,0)),
'Z': Vector((0,0,1)),
'RIGHT_SHIFT': 'shift',
'LEFT_SHIFT': 'shift',
}
snapwidgets = []
constrain = None
@staticmethod
def set_contrain(context, key):
widget = SnapUtilities.snapwidgets[-1] if SnapUtilities.snapwidgets else None
if SnapUtilities.constrain == key:
SnapUtilities.constrain = None
return
SnapUtilities.constrain = key
def snap_context_update(self, context):
def visible_objects_and_duplis():
if self.preferences.outer_verts:
for obj in context.visible_objects:
yield (obj, obj.matrix_world)
if obj.instance_type == 'COLLECTION':
mat = obj.matrix_world.copy()
for ob in obj.instance_collection.objects:
yield (ob, mat @ ob.matrix_world)
else:
for obj in context.objects_in_mode_unique_data:
yield (obj, obj.matrix_world)
self.sctx.clear_snap_objects()
for obj, matrix in visible_objects_and_duplis():
self.sctx.add_obj(obj, matrix)
def snap_context_init(self, context, snap_edge_and_vert = True):
from .snap_context_l import global_snap_context_get
#Create Snap Context
self.sctx = global_snap_context_get(context.depsgraph, context.region, context.space_data)
self.sctx.set_pixel_dist(12)
self.sctx.use_clip_planes(True)
if SnapUtilities.snapwidgets:
widget = SnapUtilities.snapwidgets[-1]
self.snap_obj = widget.snap_obj
self.bm = widget.bm
self.geom = widget.geom
self.type = widget.type
self.location = widget.location
self.preferences = widget.preferences
self.draw_cache = widget.draw_cache
else:
#init these variables to avoid errors
self.snap_obj = None
self.bm = None
self.geom = None
self.type = 'OUT'
self.location = Vector()
preferences = context.preferences.addons[__package__].preferences
self.preferences = 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.preferences.themes[0].user_interface.axis_x) + (1.0,),
tuple(context.preferences.themes[0].user_interface.axis_y) + (1.0,),
tuple(context.preferences.themes[0].user_interface.axis_z) + (1.0,)
)
self.snap_vert = self.snap_edge = snap_edge_and_vert
shading = context.space_data.shading
self.snap_face = not (snap_edge_and_vert and
(shading.show_xray or shading.type == 'WIREFRAME'))
self.sctx.set_snap_mode(
self.snap_vert, self.snap_edge, self.snap_face)
#Configure the unit of measure
unit_system = context.scene.unit_settings.system
scale = context.scene.unit_settings.scale_length
scale /= context.space_data.overlay.grid_scale
self.rd = bpy.utils.units.to_value(unit_system, 'LENGTH', str(1 / scale))
self.incremental = bpy.utils.units.to_value(
unit_system, 'LENGTH', str(self.preferences.incremental))
def snap_to_grid(self):
if self.type == 'OUT' and self.preferences.increments_grid:
loc = self.location / self.rd
self.location = Vector((round(loc.x),
round(loc.y),
round(loc.z))) * self.rd
def snap_context_free(self):
self.sctx = None
del self.sctx
del self.bm
del self.draw_cache
del self.geom
del self.location
del self.rd
del self.snap_face
del self.snap_obj
del self.type
del self.preferences
SnapUtilities.constrain = None
#def mesh_runtime_batchcache_isdirty(me):
# import ctypes
# batch_cache = ctypes.c_void_p.from_address(me.as_pointer() + 1440)
# if batch_cache:
# return ctypes.c_bool.from_address(batch_cache.value + 549).value
# return False
class SnapWidgetCommon(SnapUtilities):
snap_to_update = False
def handler(self, scene):
cls = SnapWidgetCommon
if cls.snap_to_update is False:
last_operator = self.wm_operators[-1] if self.wm_operators else None
if (not last_operator or
last_operator.name not in {'Select', 'Loop Select', '(De)select All'}):
cls.snap_to_update = self.depsgraph.id_type_updated('MESH') or\
self.depsgraph.id_type_updated('OBJECT')
def draw_point_and_elem(self):
if self.bm:
if self.bm.is_valid and self.geom.is_valid:
self.draw_cache.draw_elem(self.snap_obj, self.bm, self.geom)
else:
self.bm = None
self.geom = None
self.sctx.update_all()
self.draw_cache.draw(self.type, self.location, None, None, None)
def init_snapwidget(self, context, snap_edge_and_vert = True):
self.snap_context_init(context, snap_edge_and_vert)
self.snap_context_update(context)
self.mode = context.mode
self.last_mval = None
self.wm_operators = context.window_manager.operators
self.depsgraph = context.depsgraph
bpy.app.handlers.depsgraph_update_post.append(self.handler)
SnapWidgetCommon.snap_to_update = False
SnapUtilities.snapwidgets.append(self)
def end_snapwidget(self):
SnapUtilities.snapwidgets.remove(self)
#from .snap_context_l import global_snap_context_get
#sctx = global_snap_context_get(None, None, None)
StructRNA = bpy.types.bpy_struct
sctx = super(StructRNA, self).__getattribute__("sctx")
if sctx and not SnapUtilities.snapwidgets:
sctx.clear_snap_objects()
handler = super(StructRNA, self).__getattribute__("handler")
bpy.app.handlers.depsgraph_update_post.remove(handler)
def update_snap(self, context, mval):
if self.last_mval == mval:
return -1
else:
self.last_mval = mval
if (SnapWidgetCommon.snap_to_update):
## Something has changed since the last time.
# Has the mesh been changed?
# In the doubt lets clear the snap context.
self.snap_context_update(context)
SnapWidgetCommon.snap_to_update = False
#print('test_select', mval)
space = context.space_data
self.sctx.update_viewport_context(context.depsgraph, context.region, space, True)
shading = space.shading
snap_face = not ((self.snap_vert or self.snap_edge) and
(shading.show_xray or shading.type == 'WIREFRAME'))
if snap_face != self.snap_face:
self.snap_face = snap_face
self.sctx.set_snap_mode(
self.snap_vert, self.snap_edge, self.snap_face)
self.snap_obj, prev_loc, self.location, self.type, self.bm, self.geom, len = snap_utilities(
self.sctx,
None,
mval,
increment=self.incremental
)
class SnapPointWidget(SnapWidgetCommon, bpy.types.Gizmo):
bl_idname = "VIEW3D_GT_snap_point"
def test_select(self, context, mval):
self.update_snap(context, mval)
self.snap_to_grid()
context.area.tag_redraw()
return -1
def draw(self, context):
self.draw_point_and_elem()
def setup(self):
self.init_snapwidget(bpy.context)
def context_mode_check(context, widget_group):
workspace = context.workspace
mode = workspace.tools_mode
for tool in workspace.tools:
if (tool.widget == widget_group) and (tool.mode == mode):
break
else:
return False
return True
class SnapWidgetGroupCommon:
bl_space_type = 'VIEW_3D'
bl_region_type = 'WINDOW'
bl_options = {'3D'}
@classmethod
def poll(cls, context):
return context_mode_check(context, cls.bl_idname)
def init_tool(self, context, gizmo_name):
self.widget = self.gizmos.new(gizmo_name)
def __del__(self):
if hasattr(self, "widget"):
super(bpy.types.bpy_struct, self.widget).__getattribute__("end_snapwidget")()
class SnapPointWidgetGroup(SnapWidgetGroupCommon, bpy.types.GizmoGroup):
bl_idname = "MESH_GGT_snap_point"
bl_label = "Draw Snap Point"
def setup(self, context):
self.init_tool(context, SnapPointWidget.bl_idname)
|