[Bf-extensions-cvs] [c5de2c52] master: [mesh_snap_utilities] use new `snap_context` module
Germano
noreply at git.blender.org
Sun Sep 10 01:24:22 CEST 2017
Commit: c5de2c524908ff9efe30bfe6ecd15f3d1f873298
Author: Germano
Date: Sat Sep 9 20:24:12 2017 -0300
Branches: master
https://developer.blender.org/rBAc5de2c524908ff9efe30bfe6ecd15f3d1f873298
[mesh_snap_utilities] use new `snap_context` module
===================================================================
M mesh_snap_utilities_line.py
===================================================================
diff --git a/mesh_snap_utilities_line.py b/mesh_snap_utilities_line.py
index d7c7bf23..c5e9a825 100644
--- a/mesh_snap_utilities_line.py
+++ b/mesh_snap_utilities_line.py
@@ -19,10 +19,11 @@
# Email: germano.costa at ig.com.br
# Twitter: wii_mano @mano_wii
+
bl_info = {
"name": "Snap Utilities Line",
"author": "Germano Cavalcante",
- "version": (5, 7, 2),
+ "version": (5, 7, 5),
"blender": (2, 75, 0),
"location": "View3D > TOOLS > Snap Utilities > snap utilities",
"description": "Extends Blender Snap controls",
@@ -51,6 +52,13 @@ from bpy.props import (
StringProperty,
)
+from snap_context import SnapContext
+
+##DEBUG = False
+##if DEBUG:
+## from .snap_framebuffer_debug import screenTexture
+## from .snap_context import mesh_drawing
+
def get_units_info(scale, unit_system, separate_units):
if unit_system == 'METRIC':
@@ -106,51 +114,6 @@ def location_3d_to_region_2d(region, rv3d, coord):
))
-def fac_nearest_to_segment_2d(co2v, v2d0, v2d1):
- u = v2d1.xy - v2d0.xy
- h = co2v.xy - v2d0.xy
- return u.dot(h) / u.length_squared
-
-
-def region_2d_to_orig_and_view_vector(region, rv3d, coord, clamp=None):
- viewinv = rv3d.view_matrix.inverted()
- persinv = rv3d.perspective_matrix.inverted()
-
- dx = (2.0 * coord[0] / region.width) - 1.0
- dy = (2.0 * coord[1] / region.height) - 1.0
-
- if rv3d.is_perspective:
- origin_start = viewinv.translation.copy()
-
- out = Vector((dx, dy, -0.5))
-
- w = out.dot(persinv[3].xyz) + persinv[3][3]
-
- view_vector = ((persinv * out) / w) - origin_start
- else:
- view_vector = -viewinv.col[2].xyz
-
- origin_start = ((persinv.col[0].xyz * dx) +
- (persinv.col[1].xyz * dy) +
- viewinv.translation)
-
- if clamp != 0.0:
- if rv3d.view_perspective != 'CAMERA':
- # this value is scaled to the far clip already
- origin_offset = persinv.col[2].xyz
- if clamp is not None:
- if clamp < 0.0:
- origin_offset.negate()
- clamp = -clamp
- if origin_offset.length > clamp:
- origin_offset.length = clamp
-
- origin_start -= origin_offset
-
- view_vector.normalize()
- return origin_start, view_vector
-
-
def out_Location(rv3d, region, orig, vector):
view_matrix = rv3d.view_matrix
v1 = Vector((int(view_matrix[0][0] * 1.5), int(view_matrix[0][1] * 1.5), int(view_matrix[0][2] * 1.5)))
@@ -166,7 +129,7 @@ def out_Location(rv3d, region, orig, vector):
return hit
-def get_closest_edge(bm, point, dist):
+def get_closest_edge(bm, point, dist_sq):
r_edge = None
for edge in bm.edges:
v1 = edge.verts[0].co
@@ -190,9 +153,9 @@ def get_closest_edge(bm, point, dist):
else:
tmp = ret[0]
- new_dist = (point - tmp).length
- if new_dist <= dist:
- dist = new_dist
+ new_dist_sq = (point - tmp).length_squared
+ if new_dist_sq <= dist_sq:
+ dist_sq = new_dist_sq
r_edge = edge
return r_edge
@@ -212,23 +175,16 @@ class SnapCache():
v2dmid = None
v2dperp = None
- bface = None
- fmid = None
- fnor = None
-
- out_obj = None
- out_obmat = None
- out_obimat = None
+ bm_geom_selected = None
def snap_utilities(
- cache, context, obj_matrix_world,
- bm, mcursor,
- outer_verts=False,
- constrain=None,
- previous_vert=None,
- ignore_obj=None,
- increment=0.0):
+ sctx, obj,
+ cache, context, obj_matrix_world,
+ bm, mcursor,
+ constrain = None,
+ previous_vert = None,
+ increment = 0.0):
rv3d = context.region_data
region = context.region
@@ -239,62 +195,89 @@ def snap_utilities(
r_len = 0.0
bm_geom = None
- if bm.select_history:
- bm.select_history[0].select = False
- bm.select_history.clear()
+ if cache.bm_geom_selected:
+ try:
+ cache.bm_geom_selected.select = False
+ except ReferenceError as e:
+ print(e)
+
+ snp_obj, loc, elem = sctx.snap_get(mcursor)
+ view_vector, orig = sctx.last_ray
+
+ if not snp_obj:
+ is_increment = True
+ r_type = 'OUT'
+ r_loc = out_Location(rv3d, region, orig, view_vector)
- bpy.ops.view3d.select(location=(int(mcursor.x), int(mcursor.y)))
+ elif snp_obj.data[0] != obj: #OUT
+ r_loc = loc
- if bm.select_history:
- bm_geom = bm.select_history[0]
+ if constrain:
+ is_increment = False
+ r_loc = intersect_point_line(r_loc, constrain[0], constrain[1])[0]
+ if not r_loc:
+ r_loc = out_Location(rv3d, region, orig, view_vector)
+ elif len(elem) == 1:
+ is_increment = False
+ r_type = 'VERT'
+ elif len(elem) == 2:
+ is_increment = True
+ r_type = 'EDGE'
+ else:
+ is_increment = True
+ r_type = 'FACE'
- if isinstance(bm_geom, bmesh.types.BMVert):
+ elif len(elem) == 1:
r_type = 'VERT'
+ bm_geom = bm.verts[elem[0]]
if cache.bvert != bm_geom:
cache.bvert = bm_geom
- cache.vco = obj_matrix_world * cache.bvert.co
- # cache.v2d = location_3d_to_region_2d(region, rv3d, cache.vco)
+ cache.vco = loc
+ #cache.v2d = location_3d_to_region_2d(region, rv3d, cache.vco)
if constrain:
location = intersect_point_line(cache.vco, constrain[0], constrain[1])
- # factor = location[1]
+ #factor = location[1]
r_loc = location[0]
else:
r_loc = cache.vco
- elif isinstance(bm_geom, bmesh.types.BMEdge):
+ elif len(elem) == 2:
+ v1 = bm.verts[elem[0]]
+ v2 = bm.verts[elem[1]]
+ bm_geom = bm.edges.get([v1, v2])
+
if cache.bedge != bm_geom:
cache.bedge = bm_geom
- cache.v0 = obj_matrix_world * bm_geom.verts[0].co
- cache.v1 = obj_matrix_world * bm_geom.verts[1].co
+ cache.v0 = obj_matrix_world * v1.co
+ cache.v1 = obj_matrix_world * v2.co
cache.vmid = 0.5 * (cache.v0 + cache.v1)
cache.v2d0 = location_3d_to_region_2d(region, rv3d, cache.v0)
cache.v2d1 = location_3d_to_region_2d(region, rv3d, cache.v1)
cache.v2dmid = location_3d_to_region_2d(region, rv3d, cache.vmid)
- if previous_vert and previous_vert not in bm_geom.verts:
+ if previous_vert and previous_vert not in {v1, v2}:
pvert_co = obj_matrix_world * previous_vert.co
perp_point = intersect_point_line(pvert_co, cache.v0, cache.v1)
cache.vperp = perp_point[0]
- # factor = point_perpendicular[1]
+ #factor = point_perpendicular[1]
cache.v2dperp = location_3d_to_region_2d(region, rv3d, perp_point[0])
- # else: cache.v2dperp = None
+ #else: cache.v2dperp = None
if constrain:
location = intersect_line_line(constrain[0], constrain[1], cache.v0, cache.v1)
- if location is None:
+ if location == None:
is_increment = True
- orig, view_vector = region_2d_to_orig_and_view_vector(region, rv3d, mcursor)
end = orig + view_vector
location = intersect_line_line(constrain[0], constrain[1], orig, end)
r_loc = location[0]
- elif cache.v2dperp and \
- abs(cache.v2dperp[0] - mcursor[0]) < 10 and abs(cache.v2dperp[1] - mcursor[1]) < 10:
- r_type = 'PERPENDICULAR'
- r_loc = cache.vperp
+ elif cache.v2dperp and\
+ abs(cache.v2dperp[0] - mcursor[0]) < 10 and abs(cache.v2dperp[1] - mcursor[1]) < 10:
+ r_type = 'PERPENDICULAR'
+ r_loc = cache.vperp
elif abs(cache.v2dmid[0] - mcursor[0]) < 10 and abs(cache.v2dmid[1] - mcursor[1]) < 10:
r_type = 'CENTER'
@@ -305,94 +288,41 @@ def snap_utilities(
is_increment = True
r_type = 'EDGE'
- orig, view_vector = region_2d_to_orig_and_view_vector(region, rv3d, mcursor)
- r_loc = intersect_line_line(cache.v0, cache.v1, orig, orig + view_vector)[0]
+ r_loc = loc
- elif isinstance(bm_geom, bmesh.types.BMFace):
+ elif len(elem) == 3:
is_increment = True
r_type = 'FACE'
+ tri = [
+ bm.verts[elem[0]],
+ bm.verts[elem[1]],
+ bm.verts[elem[2]],
+ ]
+
+ faces = set(tri[0].link_faces).intersection(tri[1].link_faces, tri[2].link_faces)
+ if len(faces) == 1:
+ bm_geom = faces.pop()
+ else:
+ i = -2
+ edge = None
+ while not edge and i != 1:
+ edge = bm.edges.get([tri[i], tri[i + 1]])
+ i += 1
+ if edge:
+ for l in edge.link_loops:
+ if l.link_loop_next.vert == tri[i] or l.link_loop_prev.vert == tri[i - 2]:
+ bm_geom = l.face
+ break
+ else: # This should never happen!!!!
+ raise
+ bm_geom = faces.pop()
- if cache.bface != bm_geom:
- cache.bface = bm_geom
- cache.fmid = obj_matrix_world * bm_geom.calc_center_median()
- cache.fnor = bm_geom.normal * obj_matrix_world
-
- orig, view_vector = region_2d_to_orig_and_view_vector(region, rv3d, mcursor)
- end = orig + view_vector
- r_loc = intersect_line_plane(orig, end, cache.fmid, cache.fnor, False)
+ r_loc = loc
if constrain:
is_increment = False
r_loc = intersect_point_line(r_loc, constrain[0], con
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list