[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3032] contrib/py/scripts/addons/ mesh_vertex_slide.py: Version 2.0

Valter Battioli valtervb at live.com
Mon Feb 27 23:14:57 CET 2012


Revision: 3032
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3032
Author:   valtervb
Date:     2012-02-27 22:14:52 +0000 (Mon, 27 Feb 2012)
Log Message:
-----------
Version 2.0
Update to Bmesh API. Now work only with 2.62 or higher
Remove all mode switching, stay always in edit mode so now work quickly also with big mesh.

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	2012-02-27 18:15:21 UTC (rev 3031)
+++ contrib/py/scripts/addons/mesh_vertex_slide.py	2012-02-27 22:14:52 UTC (rev 3032)
@@ -19,13 +19,13 @@
 # <pep8 compliant>
 
 bl_info = {
-    "name": "Vertex slide",
+    "name": "Vertex slide for Bmesh",
     "author": "Valter Battioli (ValterVB) and PKHG",
-    "version": (1, 1, 6),
-    "blender": (2, 6, 0),
+    "version": (2, 0, 0),
+    "blender": (2, 6, 2),
     "location": "View3D > Mesh > Vertices (CTRL V-key)",
     "description": "Slide a vertex along an edge or a line",
-    "warning": "Broken, under developement",
+    "warning": "Work only with Blender 2.62 or higher",
     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
         "Scripts/Modeling/Vertex_Slide2",
     "tracker_url": "http://projects.blender.org/tracker/index.php?"\
@@ -60,22 +60,22 @@
 #ver. 1.1.5: Changed Lay out of the Help and the Key for reverse the 
 #            movement. Left/Right arrow rather than Pus/Minus numpad
 #ver. 1.1.6: Now the vertex movement is always coherent with Mouse movement
+#ver. 2.0.0: Update to Bmesh and remove all mode switching
 #***********************************************************************
 
 import bpy
+import bmesh
 import bgl
 import blf
 from mathutils import Vector
 from bpy_extras.view3d_utils import location_3d_to_region_2d as loc3d2d
 
-
 # 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():
     def __init__(self):
@@ -90,9 +90,8 @@
         def __init__(self):
             self.co = None
             self.idx = None
-
-
-class VertexSlideOperator(bpy.types.Operator):
+            
+class BVertexSlideOperator(bpy.types.Operator):
     bl_idname = "vertex.slide"
     bl_label = "Vertex Slide"
     bl_options = {'REGISTER', 'UNDO', 'GRAB_POINTER', 'BLOCKING', 'INTERNAL'}
@@ -109,9 +108,13 @@
     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
-
+    obj = None  #Object
+    mesh = None  #Mesh
+    bm = None  #BMesh
+    
     # Convert a 3D coord to screen 2D coord
-    def Conv3DtoScreen2D(self, context, index):  # ^^^ could this function be replaced by bpy_extras.view3d_utils.location_3d_to_region_2d ?
+    #To be replaced with bpy_extras.view3d_utils.location_3d_to_region_2d
+    def Conv3DtoScreen2D(self, context, index):
         # Get screen information
         mid_x = context.region.width / 2.0
         mid_y = context.region.height / 2.0
@@ -123,7 +126,7 @@
         ob_mat = context.active_object.matrix_world
         total_mat = view_mat * ob_mat
 
-        Vertices = bpy.context.object.data.vertices
+        Vertices = self.bm.verts
         loc = Vertices[index].co.to_4d()  # Where I want draw
 
         vec = total_mat * loc
@@ -135,6 +138,7 @@
         y = int(mid_y + vec[1] * height / 2.0)
         return x, y
 
+    # OpenGL Function to draw on the screen help and pointer *
     def draw_callback_px(self, context):
         x, y = self.Conv3DtoScreen2D(context, self.ActiveVertex)
 
@@ -178,7 +182,6 @@
             bgl.glVertex2f(p.x2D, p.y2D)
         bgl.glEnd()
 
-
     # Compute the screen distance of two vertices
     def ScreenDistance(self, vertex_zero_co, vertex_one_co):
         matw = bpy.context.active_object.matrix_world
@@ -191,8 +194,7 @@
 
     def modal(self, context, event):
         if event.type == 'MOUSEMOVE':
-            bpy.ops.object.mode_set(mode='OBJECT')
-            Vertices = bpy.context.object.data.vertices
+            Vertices = self.bm.verts
             # Calculate the temp t valuse. Stored in td
             tmpMouse = Vector((event.mouse_x, event.mouse_y))
             t_diff = (tmpMouse - self.tmpMouse).length
@@ -236,22 +238,45 @@
                                                                            self.LinkedVertices1[self.VertLinkedIdx].t)
                 self.ActiveVertex = self.Vertex1.original.idx
             self.tmpMouse_x = event.mouse_x
-            bpy.ops.object.mode_set(mode='EDIT')
+            context.area.tag_redraw()
 
+        elif event.type == 'LEFT_SHIFT':  # Hold left SHIFT for precision
+            self.LeftShiftPress = not self.LeftShiftPress
+            if self.LeftShiftPress:
+                self.Direction *= 0.1
+            else:
+                if self.Direction < 0:
+                    self.Direction = -1
+                else:
+                    self.Direction = 1
+
+        elif event.type == 'LEFT_ALT':  # Hold ALT to use continuous slide
+            self.LeftAltPress = not self.LeftAltPress
+            if self.LeftAltPress and self.Vertex2.original.idx is None:
+                vert = bpy.context.object.data.vertices[self.Vertex1.original.idx]
+                self.Vertex2.original.co = Vector((vert.co.x, vert.co.y, vert.co.z))
+                self.Vertex2.t = 0
+
+        elif event.type == 'LEFT_ARROW':  # Reverse direction
+            if self.Direction < 0.0:
+                self.Direction = - self.Direction
+
+        elif event.type == 'RIGHT_ARROW':  # Restore direction
+            if self.Direction > 0.0:
+                self.Direction = - self.Direction
+
         elif event.type == 'WHEELDOWNMOUSE':  # Change the vertex to be moved
             if self.Vertex2.original.idx is None:
                 if self.LeftAltPress:
-                    vert = bpy.context.object.data.vertices[self.Vertex1.original.idx]
+                    vert=self.bm.verts[self.Vertex1.original.idx]
                     self.Vertex2.original.co = Vector((vert.co.x, vert.co.y, vert.co.z))
                     self.Vertex2.t = 0
                 self.VertLinkedIdx = self.VertLinkedIdx + 1
                 if self.VertLinkedIdx > len(self.LinkedVertices1) - 1:
                     self.VertLinkedIdx = 0
                 bpy.ops.mesh.select_all(action='DESELECT')
-                bpy.ops.object.mode_set(mode='OBJECT')
-                bpy.context.object.data.vertices[self.Vertex1.original.idx].select = True
-                bpy.context.object.data.vertices[self.LinkedVertices1[self.VertLinkedIdx].original.idx].select = True
-                bpy.ops.object.mode_set(mode='EDIT')
+                self.bm.verts[self.Vertex1.original.idx].select = True
+                self.bm.verts[self.LinkedVertices1[self.VertLinkedIdx].original.idx].select = True
             else:
                 self.FirstVertexMove = not self.FirstVertexMove
                 if self.LeftAltPress == False:
@@ -283,17 +308,15 @@
         elif event.type == 'WHEELUPMOUSE':  # Change the vertex to be moved
             if self.Vertex2.original.idx is None:
                 if self.LeftAltPress:
-                    vert = bpy.context.object.data.vertices[self.Vertex1.original.idx]
+                    vert = self.bm.verts[self.Vertex1.original.idx]
                     self.Vertex2.original.co = Vector((vert.co.x, vert.co.y, vert.co.z))
                     self.Vertex2.t = 0
                 self.VertLinkedIdx = self.VertLinkedIdx - 1
                 if self.VertLinkedIdx < 0:
                     self.VertLinkedIdx = len(self.LinkedVertices1) - 1
                 bpy.ops.mesh.select_all(action='DESELECT')
-                bpy.ops.object.mode_set(mode='OBJECT')
-                bpy.context.object.data.vertices[self.Vertex1.original.idx].select = True
-                bpy.context.object.data.vertices[self.LinkedVertices1[self.VertLinkedIdx].original.idx].select = True
-                bpy.ops.object.mode_set(mode='EDIT')
+                self.bm.verts[self.Vertex1.original.idx].select = True
+                self.bm.verts[self.LinkedVertices1[self.VertLinkedIdx].original.idx].select = True
             else:
                 self.FirstVertexMove = not self.FirstVertexMove
                 if self.LeftAltPress == False:
@@ -322,52 +345,24 @@
                     self.Direction = -self.Direction
             context.area.tag_redraw()
 
-        elif event.type == 'LEFT_SHIFT':  # Hold left SHIFT for precision
-            self.LeftShiftPress = not self.LeftShiftPress
-            if self.LeftShiftPress:
-                self.Direction *= 0.1
-            else:
-                if self.Direction < 0:
-                    self.Direction = -1
-                else:
-                    self.Direction = 1
-
-        elif event.type == 'LEFT_ALT':  # Hold ALT to use continuous slide
-            self.LeftAltPress = not self.LeftAltPress
-            if self.LeftAltPress and self.Vertex2.original.idx is None:
-                vert = bpy.context.object.data.vertices[self.Vertex1.original.idx]
-                self.Vertex2.original.co = Vector((vert.co.x, vert.co.y, vert.co.z))
-                self.Vertex2.t = 0
-
-        elif event.type == 'LEFT_ARROW':  # Reverse direction
-            if self.Direction < 0.0:
-                self.Direction = - self.Direction
-
-        elif event.type == 'RIGHT_ARROW':  # Restore direction
-            if self.Direction > 0.0:
-                self.Direction = - self.Direction
-
         elif event.type == 'LEFTMOUSE':  # Confirm and exit
-            Vertices = bpy.context.object.data.vertices
+            Vertices = self.bm.verts
             bpy.ops.mesh.select_all(action='DESELECT')
-            bpy.ops.object.mode_set(mode='OBJECT')
             Vertices[self.Vertex1.original.idx].select = True
             if self.Vertex2.original.idx is not None:
                 Vertices[self.Vertex2.original.idx].select = True
-            bpy.ops.object.mode_set(mode='EDIT')
             context.region.callback_remove(self._handle)
+            context.area.tag_redraw()
             return {'FINISHED'}
 
         elif event.type in {'RIGHTMOUSE', 'ESC'}:  # Restore and exit
-            Vertices = bpy.context.object.data.vertices
+            Vertices = self.bm.verts
             bpy.ops.mesh.select_all(action='DESELECT')
-            bpy.ops.object.mode_set(mode='OBJECT')
             Vertices[self.Vertex1.original.idx].co = self.Vertex1.original.co

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list