-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap.py
193 lines (131 loc) · 5.62 KB
/
snap.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
import bpy
import bmesh
from . raycast import cast_scene_ray_from_mouse
class Snap:
def log(self, *args, **kwargs):
if self.debug:
print(*args, **kwargs)
debug = False
depsgraph = None
cache = None
exclude = []
exclude_wire = False
alternative = []
hit = None
hitobj = None
hitindex = None
hitlocation = None
hitnormal = None
hitmx = None
hitface = None
_edit_mesh_objs = []
_modifiers = []
def __init__(self, context, include=None, exclude=None, exclude_wire=False, alternative=None, debug=False):
self.debug = debug
self.log("\nInitialize Snapping")
self._init_edit_mode(context)
self._init_exclude(context, include, exclude, exclude_wire)
self._init_alternatives(context, alternative)
self.depsgraph = context.evaluated_depsgraph_get()
self.cache = SnapCache(debug=debug)
self.hitface = None
self.log()
def finish(self):
self.log("\nFinish Snapping")
if self._modifiers:
self._enable_modifiers()
self._remove_alternatives()
self.cache.clear()
def get_hit(self, mousepos):
self.hit, self.hitobj, self.hitindex, self.hitlocation, self.hitnormal, self.hitmx = cast_scene_ray_from_mouse(mousepos, self.depsgraph, exclude=self.exclude, exclude_wire=self.exclude_wire, unhide=self.alternative, debug=self.debug)
if self.hit:
name = self.hitobj.name
if name not in self.cache.objects:
self.cache.objects[name] = self.hitobj
mesh = bpy.data.meshes.new_from_object(self.hitobj.evaluated_get(self.depsgraph), depsgraph=self.depsgraph)
self.cache.meshes[name] = mesh
bm = bmesh.new()
bm.from_mesh(mesh)
bm.verts.ensure_lookup_table()
bm.faces.ensure_lookup_table()
self.cache.bmeshes[name] = bm
self.cache.loop_triangles[name] = bm.calc_loop_triangles()
self.cache.tri_coords[name] = {}
if not self.hitface or (self.hitface and self.hitface.index != self.hitindex):
self.log("Hitface changed to", self.hitindex)
self.hitface = self.cache.bmeshes[name].faces[self.hitindex]
if self.hitindex not in self.cache.tri_coords[name]:
self.log("Adding tri coords for face index", self.hitindex)
loop_triangles = self.cache.loop_triangles[name]
tri_coords = [self.hitmx @ l.vert.co for tri in loop_triangles if tri[0].face == self.hitface for l in tri]
self.cache.tri_coords[name][self.hitindex] = tri_coords
def _init_edit_mode(self, context):
if context.mode == 'EDIT_MESH':
self._update_meshes(context)
self._disable_modifiers()
def _init_exclude(self, context, include, exclude, exclude_wire):
if include:
self.exclude = [obj for obj in context.visible_objects if obj not in include]
elif exclude:
self.exclude = exclude
else:
self.exclude = []
view = context.space_data
if view.local_view:
hidden = [obj for obj in context.view_layer.objects if not obj.visible_get()]
self.exclude += hidden
self.exclude_wire = exclude_wire
def _init_alternatives(self, context, alternative):
self.alternative = []
if alternative:
for obj in alternative:
if obj not in self.exclude:
self.exclude.append(obj)
dup = obj.copy()
dup.data = obj.data.copy()
context.scene.collection.objects.link(dup)
dup.hide_set(True)
self.alternative.append(dup)
self.log(f" Created alternative object {dup.name} for {obj.name}")
def _remove_alternatives(self):
for obj in self.alternative:
self.log(f" Removing alternave object {obj.name}")
bpy.data.meshes.remove(obj.data, do_unlink=True)
def _update_meshes(self, context):
self._edit_mesh_objs = [obj for obj in context.visible_objects if obj.mode == 'EDIT']
for obj in self._edit_mesh_objs:
obj.update_from_editmode()
def _disable_modifiers(self):
self._modifiers = [(obj, mod) for obj in self._edit_mesh_objs for mod in obj.modifiers if mod.show_viewport]
for obj, mod in self._modifiers:
self.log(f" Disabling {obj.name}'s {mod.name}")
mod.show_viewport = False
def _enable_modifiers(self):
for obj, mod in self._modifiers:
self.log(f" Re-enabling {obj.name}'s {mod.name}")
mod.show_viewport = True
class SnapCache:
def log(self, *args, **kwargs):
if self.debug:
print(*args, **kwargs)
debug = False
objects = {}
meshes = {}
bmeshes = {}
loop_triangles = {}
tri_coords = {}
def __init__(self, debug=False):
self.debug = debug
self.log(" Initialize SnappingCache")
def clear(self):
for name, mesh in self.meshes.items():
self.log(f" Removing {name}'s temporary snapping mesh {mesh.name} with {len(mesh.polygons)} faces and {len(mesh.vertices)} verts")
bpy.data.meshes.remove(mesh, do_unlink=True)
for name, bm in self.bmeshes.items():
self.log(f" Freeing {name}'s temporary snapping bmesh")
bm.free()
self.objects.clear()
self.meshes.clear()
self.bmeshes.clear()
self.loop_triangles.clear()
self.tri_coords.clear()