[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2248] contrib/py/scripts/addons/ mesh_vertex_slide.py: Refactory and some clean of the code.

Valter Battioli valter31 at interfree.it
Mon Aug 15 14:30:38 CEST 2011


Revision: 2248
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2248
Author:   valtervb
Date:     2011-08-15 12:30:38 +0000 (Mon, 15 Aug 2011)
Log Message:
-----------
Refactory and some clean of the code.
Add edge drawing (from chromoly vertex slide)
Add help on screen (from chromooly vertex slide)

Modified Paths:
--------------
    contrib/py/scripts/addons/mesh_vertex_slide.py

Modified: contrib/py/scripts/addons/mesh_vertex_slide.py
===================================================================
--- contrib/py/scripts/addons/mesh_vertex_slide.py	2011-08-15 11:56:36 UTC (rev 2247)
+++ contrib/py/scripts/addons/mesh_vertex_slide.py	2011-08-15 12:30:38 UTC (rev 2248)
@@ -19,11 +19,11 @@
 # <pep8 compliant>
 
 bl_info = {
-    "name": "Vertex slide",
+    "name": "Vertex slide 2",
     "author": "Valter Battioli (ValterVB) and PKHG",
-    "version": (1, 1, 1),
+    "version": (1, 1, 2),
     "blender": (2, 5, 9),
-    "api": 39263,
+    "api": 39307,
     "location": "View3D > Mesh > Vertices (CTRL V-key) or search for 'VB Vertex 2'",
     "description": "Slide a vertex along an edge",
     "warning": "",
@@ -50,341 +50,386 @@
 #ver. 1.0.9: Fix for reverse vector multiplication
 #ver. 1.1.0: Delete debug info, some cleanup and add some comments
 #ver. 1.1.1: Now UNDO work properly
+#ver. 1.1.2: Refactory and some clean of the code.
+#            Add edge drawing (from chromoly vertex slide)
+#            Add help on screen (from chromooly vertex slide)
 #***********************************************************************
 
 import bpy
 import bgl
 import blf
 from mathutils import Vector
-from bpy_extras import view3d_utils
+from bpy_extras.view3d_utils import location_3d_to_region_2d as loc3d2d
 
-###global
-direction = 1.0
-mouseVec = None
-ActiveVertex = None
 
+# Equation of the line
+# Changing t we have a new point coordinate on the line along v0 v1
+# With t from 0 to 1  I move from v0 to v1
+def NewCoordinate(v0, v1, t):
+    return v0 + t * (v1 - v0)
 
+
 #  This class store Vertex data
 class Point():
-    class Vertices():
+    def __init__(self):
+        self.original = self.Vertex()  # Original position
+        self.new = self.Vertex()  # New position
+        self.t = 0  # Used for move the vertex
+        self.x2D = 0  # Screen 2D cooord
+        self.y2D = 0  # Screen 2D cooord
+        self.selected = False
+
+    class Vertex():
         def __init__(self):
             self.co = Vector((0, 0, 0))
             self.idx = -1
 
-    def __init__(self):
-        self.original = self.Vertices()
-        self.new = self.Vertices()
-        self.t = 0
-        self.selected = False
 
+class VertexSlideOperator(bpy.types.Operator):
+    bl_idname = "vertex.slide"
+    bl_label = "VB Vertex Slide 2"  # PKHG easy to searc for ;-)
+    bl_options = {'REGISTER', 'UNDO'}
 
-# Equation of the line
-# Changing t, I have a new point coordinate on the line along v0 v1
-# With t from 0 to 1  I move from v0 to v1
-def NewCoordinate(v0, v1, t):
-    return v0 + t * (v1 - v0)
+    Vertex1 = Point()  # First selected vertex data
+    LinkedVertices1 = []  # List of index of linked vertices of Vertex1
+    Vertex2 = Point()  # Second selected vertex data
+    LinkedVertices2 = []  # List of index of linked vertices of Vertex2
+    ActiveVertex = None
+    tmpMouse_x = 0
+    tmpMouse = Vector((0, 0))
+    Direction = 1.0  # Used for direction and precision of the movement
+    FirstVertexMove = True  # If true Move the first vertex
+    VertLinkedIdx = 0  # Index of LinkedVertices1. Used only for 1 vertex select case
+    LeftAltPress = False  # Flag to know if ALT is hold on
+    LeftShiftPress = False  # Flag to know if SHIFT is hold on
 
-# Draw an asterisk near the vertex that I move
-def draw_callback_px(self, context):
-    # Get screen information
-    mid_x = context.region.width / 2.0
-    mid_y = context.region.height / 2.0
-    width = context.region.width
-    height = context.region.height
+    # Convert a 3D coord to screen 2D coord
+    def Conv3DtoScreen2D(self, context, index):
+        # Get screen information
+        mid_x = context.region.width / 2.0
+        mid_y = context.region.height / 2.0
+        width = context.region.width
+        height = context.region.height
 
-    # Get matrices
-    view_mat = context.space_data.region_3d.perspective_matrix
-    ob_mat = context.active_object.matrix_world
-    total_mat = view_mat * ob_mat
+        # Get matrices
+        view_mat = context.space_data.region_3d.perspective_matrix
+        ob_mat = context.active_object.matrix_world
+        total_mat = view_mat * ob_mat
 
-    Vertices = bpy.context.object.data.vertices
-    temp = Vertices[ActiveVertex].co
+        Vertices = bpy.context.object.data.vertices
+        loc = Vertices[index].co.to_4d()  # Where I want draw
 
-    loc = Vertices[ActiveVertex].co.to_4d()  # Where I want draw the text
+        vec = total_mat * loc
+        if vec[3] != 0:
+            vec = vec / vec[3]
+        else:
+            vec = vec
+        x = int(mid_x + vec[0] * width / 2.0)
+        y = int(mid_y + vec[1] * height / 2.0)
+        return x, y
 
-    vec = total_mat * loc
-    if vec[3] != 0:
-        vec = vec / vec[3]
-    else:
-        vec = vec
-    x = int(mid_x + vec[0] * width / 2.0)
-    y = int(mid_y + vec[1] * height / 2.0)
+    def draw_callback_px(self, context):
+        x, y = self.Conv3DtoScreen2D(context, self.ActiveVertex)
 
-    # Draw an * at the active vertex
-    blf.position(0, x, y, 0)
-    blf.size(0, 26, 72)
-    blf.draw(0, "*")
+        # Draw an * at the active vertex
+        blf.position(0, x, y, 0)
+        blf.size(0, 26, 72)
+        blf.draw(0, "*")
 
+        #Draw Help
+        blf.size(0, 11, context.user_preferences.system.dpi)
+        textlist = ['{SHIFT}:Precision',
+                    '{WHEEL}:Change the vertex/edge',
+                    '{ALT}:Continuos slide',
+                    '{NUMPAD -}:Reverse the movement',
+                    '{NUMPAD +}:Restore the movement']
+        blf.position(0, 70, 30, 0)
+        blf.draw(0, ', '.join(textlist))
 
-class VertexSlideOperator(bpy.types.Operator):
-    bl_idname = "vertex.slide"
-    bl_label = "VB Vertex Slide 2"  # PKHG easy to searc for ;-)
-    bl_options = {'REGISTER', 'UNDO'}
+        # Draw edge
+        bgl.glEnable(bgl.GL_BLEND)
+        bgl.glColor4f(1.0, 0.1, 0.8, 1.0)
+        bgl.glBegin(bgl.GL_LINES)
+        for p in self.LinkedVertices1:
+            bgl.glVertex2f(self.Vertex1.x2D, self.Vertex1.y2D)
+            bgl.glVertex2f(p.x2D, p.y2D)
+        for p in self.LinkedVertices2:
+            bgl.glVertex2f(self.Vertex2.x2D, self.Vertex2.y2D)
+            bgl.glVertex2f(p.x2D, p.y2D)
+        bgl.glEnd()
 
-    Vert1 = Point()  # Original selected vertex data
-    Vert2 = Point()  # Second selected vertex data
-    LinkedVerts = []  # List of linked vertex to Vert1
-    VertLinkedIdx = 0  # Index of the linked vertex selected
-    temp_mouse_x = 0
-    tmpMouse = Vector((0, 0))
-
-    first_vertex_move = True
-    left_alt_press = False
-    left_shift_press = False
-
-    #Compute the screendistance of two vertices PKHG
-    def denominator(self, vertex_zero_co, vertex_one_co):
-        global denom
+    # Compute the screen distance of two vertices
+    def ScreenDistance(self, vertex_zero_co, vertex_one_co):
         matw = bpy.context.active_object.matrix_world
         V0 = matw * vertex_zero_co
-        res0 = view3d_utils.location_3d_to_region_2d(bpy.context.region, bpy.context.space_data.region_3d, V0)
+        res0 = loc3d2d(bpy.context.region, bpy.context.space_data.region_3d, V0)
         V1 = matw * vertex_one_co
-        res1 = view3d_utils.location_3d_to_region_2d(bpy.context.region, bpy.context.space_data.region_3d, V1)
+        res1 = loc3d2d(bpy.context.region, bpy.context.space_data.region_3d, V1)
         result = (res0 - res1).length
-        denom = result
         return result
 
     def modal(self, context, event):
-        global ActiveVertex, mouseVec, direction
-        context.area.tag_redraw()
         if event.type == 'MOUSEMOVE':
             Vertices = bpy.context.object.data.vertices
             bpy.ops.object.mode_set(mode='OBJECT')
-            if self.Vert2.original.idx != -1:  # Starting with 2 vertex selected
-                denom = self.denominator(self.Vert1.original.co, self.Vert2.original.co)
-                tmpMouse = Vector((event.mouse_x, event.mouse_y))
-                t_diff = (tmpMouse - self.tmpMouse).length
-                self.tmpMouse = tmpMouse
-                td = t_diff * direction / denom
-                mouseDir = "right"
-                if event.mouse_x < self.temp_mouse_x:
-                    mouseDir = "left"
-                    td = -td
-                if self.first_vertex_move:
-                    self.Vert1.t = self.Vert1.t + td
-                    Vertices[self.Vert1.original.idx].co = NewCoordinate(self.Vert1.original.co, self.Vert2.original.co, self.Vert1.t)
-                    Vertices[self.Vert2.original.idx].co = NewCoordinate(self.Vert2.original.co, self.Vert1.original.co, self.Vert2.t)
+            # Calculate the temp t valuse, Stored in td
+            tmpMouse = Vector((event.mouse_x, event.mouse_y))
+            t_diff = (tmpMouse - self.tmpMouse).length
+            self.tmpMouse = tmpMouse
+            if self.Vertex2.original.idx != -1:  # 2 vertex selected
+                td = t_diff * self.Direction / self.ScreenDistance(self.Vertex1.original.co,
+                                                                   self.Vertex2.original.co)
+            else:  # 1 vertex selected
+                td = t_diff * self.Direction / self.ScreenDistance(self.Vertex1.original.co,
+                                                                   self.LinkedVertices1[self.VertLinkedIdx].original.co)
+            if event.mouse_x < self.tmpMouse_x:
+                td = -td
+
+            if self.Vertex2.original.idx != -1:  # 2 vertex selected
+                # Calculate the t valuse
+                if self.FirstVertexMove:
+                    self.Vertex1.t = self.Vertex1.t + td
                 else:
-                    self.Vert2.t = self.Vert2.t + td
-                    Vertices[self.Vert1.original.idx].co = NewCoordinate(self.Vert1.original.co, self.Vert2.original.co, self.Vert1.t)
-                    Vertices[self.Vert2.original.idx].co = NewCoordinate(self.Vert2.original.co, self.Vert1.original.co, self.Vert2.t)
-            else:  # Starting with 1 vertex selected
-                denom = self.denominator(self.Vert1.original.co, self.LinkedVerts[self.VertLinkedIdx].original.co)
-                tmpMouse = Vector((event.mouse_x, event.mouse_y))
-                t_diff = (tmpMouse - self.tmpMouse).length

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list