#!BPY # ######################################################################## # # AutoAlign - Aligns selected vertices to an axis. # Copyright (C) 2007 Dave Jarvis (http://www.davidjarvis.ca) # 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 2 # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # # ######################################################################## """ Name: 'AutoAlign Vertices' Blender: 243 Group: 'Mesh' Tooltip: 'Aligns selected vertices to an axis.' """ __author__ = "Dave Jarvis" __url__ = ("http://www.davidjarvis.ca/blender") __version__ = "1.0.0" __bpydoc__ = """ 1. Select a group of vertices to align. 2. Select and load this script. """ import array, Blender from Numeric import Float, zeros from Blender import NMesh, Window, Draw X_AXIS = 0 Y_AXIS = 1 Z_AXIS = 2 # Remember the state of Edit mode. # editMode = Window.EditMode() # Leave edit mode before changing an active mesh. # if editMode: Window.EditMode( 0 ) # Get a handle to the mesh of the active object. # objects = Blender.Object.GetSelected() mesh = NMesh.GetRaw( objects[0].data.name ) min = zeros( 3, Float ) max = zeros( 3, Float ) # Corral only the selected vertices. # vertices = [vertex for vertex in mesh.verts if vertex.sel] vertex = vertices[0] min[ X_AXIS ] = vertex.co[ X_AXIS ] min[ Y_AXIS ] = vertex.co[ Y_AXIS ] min[ Z_AXIS ] = vertex.co[ Z_AXIS ] max[ X_AXIS ] = vertex.co[ X_AXIS ] max[ Y_AXIS ] = vertex.co[ Y_AXIS ] max[ Z_AXIS ] = vertex.co[ Z_AXIS ] sum = zeros( 3, Float ) count = 0 # For all the selected vertices, determine the # minimum and maximum extents along each axis. # While we're at it, sum the distance between # the points (in preparation for averaging). # for vertex in vertices: count = count + 1 x = vertex.co[ X_AXIS ] y = vertex.co[ Y_AXIS ] z = vertex.co[ Z_AXIS ] if x < min[ X_AXIS ]: min[ X_AXIS ] = x if y < min[ Y_AXIS ]: min[ Y_AXIS ] = y if z < min[ Z_AXIS ]: min[ Z_AXIS ] = z if x > max[ X_AXIS ]: max[ X_AXIS ] = x if y > max[ Y_AXIS ]: max[ Y_AXIS ] = y if z > max[ Z_AXIS ]: max[ Z_AXIS ] = z sum[ X_AXIS ] += x sum[ Y_AXIS ] += y sum[ Z_AXIS ] += z # Find the differences between vertex extents. # x = max[ X_AXIS ] - min[ X_AXIS ] y = max[ Y_AXIS ] - min[ Y_AXIS ] z = max[ Z_AXIS ] - min[ Z_AXIS ] # Whichever axis has the least difference between # the minimum and maximum extents is the axis # along which the vertices will be flattened. # axis = X_AXIS if y < x: axis = Y_AXIS if (axis == X_AXIS) and (z < x): axis = Z_AXIS if (axis == Y_AXIS) and (z < y): axis = Z_AXIS # Determine the coordinate for the axis by taking # the average value of all selected vertices along # the axis of minimum extents. # coord = (sum[ axis ] / count) # Align the vertices to the determined axis at the # average location. # for vertex in mesh.verts: if vertex.sel: vertex.co[ axis ] = coord mesh.update() Draw.Redraw() # Restore edit mode to its former state. # Window.EditMode( editMode )