[Bf-extensions-cvs] [545288a] master: Push Snap Utilities

Germano Cavalcante mano-wii noreply at git.blender.org
Thu Sep 3 16:49:13 CEST 2015


Commit: 545288af962380b3fa1bede224a44dbffc475d86
Author: Germano Cavalcante (mano-wii)
Date:   Thu Sep 3 11:48:22 2015 -0300
Branches: master
https://developer.blender.org/rBAC545288af962380b3fa1bede224a44dbffc475d86

Push Snap Utilities

===================================================================

A	mesh_snap_utilities_line.py

===================================================================

diff --git a/mesh_snap_utilities_line.py b/mesh_snap_utilities_line.py
new file mode 100644
index 0000000..83cb743
--- /dev/null
+++ b/mesh_snap_utilities_line.py
@@ -0,0 +1,1004 @@
+### 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 #####
+
+# Contact for more information about the Addon:
+# Email:    germano.costa at ig.com.br
+# Twitter:  wii_mano @mano_wii
+
+bl_info = {
+    "name": "Snap_Utilities_Line",
+    "author": "Germano Cavalcante",
+    "version": (5, 0),
+    "blender": (2, 75, 0),
+    "location": "View3D > TOOLS > Snap Utilities > snap utilities",
+    "description": "Extends Blender Snap controls",
+    "wiki_url" : "http://blenderartists.org/forum/showthread.php?363859-Addon-CAD-Snap-Utilities",
+    "category": "Mesh"}
+    
+import bpy, bgl, bmesh, mathutils, math
+#from space_view3d_panel_measure import getUnitsInfo, convertDistance
+from mathutils import Vector#, Matrix
+from mathutils.geometry import (
+    intersect_point_line,
+    intersect_line_line,
+    intersect_line_plane,
+    intersect_ray_tri)
+
+def get_units_info(scale, unit_system, separate_units):
+    if unit_system == 'METRIC':
+            scale_steps = ((1000, 'km'), (1, 'm'), (1 / 100, 'cm'),
+                (1 / 1000, 'mm'), (1 / 1000000, '\u00b5m'))
+    elif unit_system == 'IMPERIAL':
+            scale_steps = ((5280, 'mi'), (1, '\''),
+                (1 / 12, '"'), (1 / 12000, 'thou'))
+            scale /= 0.3048  # BU to feet
+    else:
+            scale_steps = ((1, ' BU'),)
+            separate_units = False
+
+    return (scale, scale_steps, separate_units)
+
+def convert_distance(val, units_info, PRECISION = 5):
+    scale, scale_steps, separate_units = units_info
+    sval = val * scale
+    idx = 0
+    while idx < len(scale_steps) - 1:
+            if sval >= scale_steps[idx][0]:
+                    break
+            idx += 1
+    factor, suffix = scale_steps[idx]
+    sval /= factor
+    if not separate_units or idx == len(scale_steps) - 1:
+            dval = str(round(sval, PRECISION)) + suffix
+    else:
+            ival = int(sval)
+            dval = str(round(ival, PRECISION)) + suffix
+            fval = sval - ival
+            idx += 1
+            while idx < len(scale_steps):
+                    fval *= scale_steps[idx - 1][0] / scale_steps[idx][0]
+                    if fval >= 1:
+                            dval += ' ' \
+                                + ("%.1f" % fval) \
+                                + scale_steps[idx][1]
+                            break
+                    idx += 1
+    return dval
+
+def location_3d_to_region_2d(region, rv3d, coord):
+    prj = rv3d.perspective_matrix * Vector((coord[0], coord[1], coord[2], 1.0))
+    width_half = region.width / 2.0
+    height_half = region.height / 2.0
+    return Vector((width_half + width_half * (prj.x / prj.w),
+                   height_half + height_half * (prj.y / prj.w),
+                   ))
+
+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)))
+    v2 = Vector((int(view_matrix[1][0]*1.5),int(view_matrix[1][1]*1.5),int(view_matrix[1][2]*1.5)))
+
+    hit = mathutils.geometry.intersect_ray_tri(Vector((1,0,0)), Vector((0,1,0)), Vector((0,0,0)), (vector), (orig), False)
+    if hit == None:
+        hit = mathutils.geometry.intersect_ray_tri(v1, v2, Vector((0,0,0)), (vector), (orig), False)        
+    if hit == None:
+        hit = mathutils.geometry.intersect_ray_tri(v1, v2, Vector((0,0,0)), (-vector), (orig), False)
+    if hit == None:
+        hit = Vector((0,0,0))
+    return hit
+
+def snap_utilities(self,
+                   context,
+                   obj_matrix_world,
+                   bm_geom,
+                   bool_update,
+                   mcursor,
+                   outer_verts = False,
+                   constrain = None,
+                   previous_vert = None,
+                   ignore_obj = None,
+                   increment = 0.0):
+
+    rv3d = context.region_data
+    region = context.region
+    is_increment = False
+
+    if not hasattr(self, 'snap_cache'):
+        self.snap_cache = True
+        self.type = 'OUT'
+        self.bvert = None
+        self.bedge = None
+        self.bface = None
+        self.hit = False
+        self.out_obj = None
+
+    if bool_update:
+        #self.bvert = None
+        self.bedge = None
+        #self.bface = None
+
+    if isinstance(bm_geom, bmesh.types.BMVert):
+        self.type = 'VERT'
+
+        if self.bvert != bm_geom:
+            self.bvert = bm_geom
+            self.vert = obj_matrix_world * self.bvert.co
+            #self.Pvert = location_3d_to_region_2d(region, rv3d, self.vert)
+        
+        if constrain:
+            #self.location = (self.vert-self.const).project(vector_constrain) + self.const
+            location = intersect_point_line(self.vert, constrain[0], constrain[1])
+            #factor = location[1]
+            self.location = location[0]
+        else:
+            self.location = self.vert
+
+    elif isinstance(bm_geom, bmesh.types.BMEdge):
+        if self.bedge != bm_geom:
+            self.bedge = bm_geom
+            self.vert0 = obj_matrix_world*self.bedge.verts[0].co
+            self.vert1 = obj_matrix_world*self.bedge.verts[1].co
+            self.po_cent = (self.vert0+self.vert1)/2
+            self.Pcent = location_3d_to_region_2d(region, rv3d, self.po_cent)
+            self.Pvert0 = location_3d_to_region_2d(region, rv3d, self.vert0)
+            self.Pvert1 = location_3d_to_region_2d(region, rv3d, self.vert1)
+        
+            if previous_vert and previous_vert not in self.bedge.verts:
+                    pvert_co = obj_matrix_world*previous_vert.co
+                    point_perpendicular = intersect_point_line(pvert_co, self.vert0, self.vert1)
+                    self.po_perp = point_perpendicular[0]
+                    #factor = point_perpendicular[1] 
+                    self.Pperp = location_3d_to_region_2d(region, rv3d, self.po_perp)
+
+        if constrain:
+            location = intersect_line_line(constrain[0], constrain[1], self.vert0, self.vert1)
+            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)
+            self.location = location[0]
+        
+        elif hasattr(self, 'Pperp') and abs(self.Pperp[0]-mcursor[0]) < 10 and abs(self.Pperp[1]-mcursor[1]) < 10:
+            self.type = 'PERPENDICULAR'
+            self.location = self.po_perp
+
+        elif abs(self.Pcent[0]-mcursor[0]) < 10 and abs(self.Pcent[1]-mcursor[1]) < 10:
+            self.type = 'CENTER'
+            self.location = self.po_cent
+
+        else:
+            if increment and previous_vert in self.bedge.verts:
+                is_increment = True
+            self.type = 'EDGE'
+            orig, view_vector = region_2d_to_orig_and_view_vector(region, rv3d, mcursor)
+            end = orig + view_vector
+            self.location = intersect_line_line(self.vert0, self.vert1, orig, end)[0]
+
+    elif isinstance(bm_geom, bmesh.types.BMFace):
+        is_increment = True
+        self.type = 'FACE'
+
+        if self.bface != bm_geom:
+            self.bface = bm_geom
+            self.face_center = obj_matrix_world*bm_geom.calc_center_median()
+            self.face_normal = bm_geom.normal*obj_matrix_world.inverted()
+
+        orig, view_vector = region_2d_to_orig_and_view_vector(region, rv3d, mcursor)
+        end = orig + view_vector
+        location = intersect_line_plane(orig, end, self.face_center, self.face_normal, False)
+        if constrain:
+            is_increment = False
+            location = intersect_point_line(location, constrain[0], constrain[1])[0]
+
+        self.location = location
+
+    else:
+        is_increment = True
+        self.type = 'OUT'
+
+        orig, view_vector = region_2d_to_orig_and_view_vector(region, rv3d, mcursor)
+        end = orig + view_vector * 1000
+
+        if not outer_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list