With a few changes, the code can return the coordinates of the vertices of the element:
@@ -117,11 +117,11 @@ class SnapContext():
if gpu_data.draw_tris:
if index < snap_obj.data[1].num_tris:
tri_verts = gpu_data.get_tri_verts(index)
tri_co = [snap_obj.mat * Vector(v) for v in gpu_data.get_tri_co(index)]
- return intersect_ray_tri(*tri_co, *self.last_ray, False), tri_verts
+ return intersect_ray_tri(*tri_co, *self.last_ray, False), tri_verts, tri_co
index -= gpu_data.num_tris
if gpu_data.draw_edges:
if index < snap_obj.data[1].num_edges:
@@ -132,28 +132,29 @@ class SnapContext():
if (self._snap_mode) & VERT and (fac < 0.25 or fac > 0.75):
co = edge_co[0] if fac < 0.5 else edge_co[1]
proj_co = project_co_v3(self, co)
dist = self.mval - proj_co
if abs(dist.x) < self._dist_px and abs(dist.y) < self._dist_px:
- return co, (edge_verts[0] if fac < 0.5 else edge_verts[1],)
+ return co, (edge_verts[0] if fac < 0.5 else edge_verts[1],), co
if fac <= 0.0:
co = edge_co[0]
elif fac >= 1.0:
co = edge_co[1]
else:
co = edge_co[0] + fac * (edge_co[1] - edge_co[0])
- return co, edge_verts
+ return co, edge_verts, edge_co
index -= gpu_data.num_edges
if gpu_data.draw_verts:
if index < snap_obj.data[1].num_verts:
- return snap_obj.mat * Vector(gpu_data.get_loosevert_co(index)), (gpu_data.get_loosevert_index(index),)
+ co = Vector(gpu_data.get_loosevert_co(index))
+ return snap_obj.mat * co, (gpu_data.get_loosevert_index(index),), (co, )
- return None, None
+ return None, None, None
def _get_snap_obj_by_obj(self, obj):
for snap_obj in self.snap_objects:
if obj == snap_obj.data[0]:
@@ -224,11 +225,11 @@ class SnapContext():
def get_ray(self, mval):
self.last_ray = _get_ray(self.region, self.rv3d, mval)
return self.last_ray
def snap_get(self, mval):
- ret = None, None
+ ret = None, None, None
self.mval[:] = mval
snap_vert = self._snap_mode & VERT != 0
snap_edge = self._snap_mode & EDGE != 0
snap_face = self._snap_mode & FACE != 0
@@ -287,11 +288,11 @@ class SnapContext():
ret = self._get_loc(snap_obj, index)
self._offscreen.unbind()
gpu_Indices_restore_state()
- return snap_obj, ret[0], ret[1]
+ return snap_obj, ret[0], ret[1], ret[2]
def free(self):
self.__del__()
self.freed = True
With the coordinates of the edge, just get the middle, project it and test if it is within the threshold.
from snap_context.utils_projection import project_co_v3
sctx = ... (create snap context)
snp_obj, loc, elem, elem_co = sctx.snap_get(mcursor)
if len(elem_co) == 2:
middle = snp_obj.mat * (0.5 * (elem_co[0] + elem_co[1]))
proj_mid = project_co_v3(sctx, middle)
dist = mcursor - proj_mid
if abs(dist.x) < sctx._dist_px and abs(dist.y) < sctx._dist_px:
print("snap to the middle of the edge", middle)
Getting the middle of the faces is more complicated. I fear only be possible for bmesh