While doing typography design in blender i was faced repeatedly with some very basic but tedious and time consuming operations. Instead of waiting for the pre 2.5 CAD tools to be updated to 2.5x I started coding some of the prerequisites.
here is what i put into blender console to get the selected edges of the current
object. ( tho, after selecting the edges you must enter object mode with the object still selected for this script to be able to get the information )
import sys
obj = bpy.context.active_object
edges = []
mesh = obj.data
verts = mesh.vertices
# this for loop is from the 3dview_spacebar_menu
for e in mesh.edges:
if e.select:
edges.append(e)
meshMatrix = []
edgenum = 0
for edge_to_test in edges:
p1 = verts[edge_to_test.vertices[0]].co
p2 = verts[edge_to_test.vertices[1]].co
meshMatrix.append([(p1.x,p1.y,p1.z),(p2.x,p2.y,p2.z)])
edgenum += 1
for edge_ in meshMatrix:
edge_
this spits out vertex data like :
[(0.0, 1.0, 1.0), (5.211963127749186e-08, 0.0, 1.0)]
[(5.211963127749186e-08, 0.0, 1.0), (5.211963127749186e-08, 0.0, 2.0)]
[(0.6621248126029968, 0.0, 2.0), (0.6621248126029968, 0.0, 1.0)]
[(0.6621248126029968, 0.0, 1.0), (0.6621248126029968, 1.637786626815796, 1.0)]
[(0.6621248126029968, 1.637786626815796, -0.04176926612854004), (0.6621248126029968, 1.637786626815796, 1.0)]
[(2.0378873348236084, 1.637786626815796, 1.0), (2.0378873348236084, 1.637786626815796, -0.04176926612854004)]
[(2.0378873348236084, 1.637786626815796, 1.0), (2.0378873348236084, 0.0, 1.0)]
the previous code snippet needs commas to seperate the list. This is purposely coded verbosely with many print statements while i'm bug testing my logic :) optimizing will happen when i'm confident this catches undesirable anomalies.
# test
import sys
import math
preStantonList = [
[(0.0, 1.0, 1.0), (5.211963127749186e-08, 0.0, 1.0)],
[(5.211963127749186e-08, 0.0, 1.0), (5.211963127749186e-08, 0.0, 2.0)],
[(0.6621248126029968, 0.0, 2.0), (0.6621248126029968, 0.0, 1.0)],
[(0.6621248126029968, 0.0, 1.0), (0.6621248126029968, 1.637786626815796, 1.0)],
[(0.6621248126029968, 1.637786626815796, -0.04176926612854004), (0.6621248126029968, 1.637786626815796, 1.0)],
[(2.0378873348236084, 1.637786626815796, 1.0), (2.0378873348236084, 1.637786626815796, -0.04176926612854004)],
[(2.0378873348236084, 1.637786626815796, 1.0), (2.0378873348236084, 0.0, 1.0)]
]
def calcDifference(val1, val2):
if val1 > val2: return val1-val2
if val2 > val1: return val2-val1
if val1 == val2: return 0.0
def isListHomogenous(vertList):
'''
function returns True if no anomalies are found, False if one vert exceeds epsilon
'''
def average(vertList):
sum = 0.0
for i in vertList:
sum += i
avsum = sum / len(vertList)
return avsum
def isEpsilon(val1, val2):
min = 0.0000001
if calcDifference(val1, val2) == 0.0: return True
if calcDifference(val1, val2) < min: return True
return False
def checkList(vertList):
print average(vertList)
for i in vertList:
if not isEpsilon(i, average(vertList)): return False
# reaches this stage, means the variation is probably minimal
return True
#my Boolean
GOODTOGO = False
GOODTOGO = checkList(vertList)
return GOODTOGO
def printEdgeList(eList):
(p1x, p1y, p1z) = (0.0, 0.0, 0.0)
(p2x, p2y, p2z) = (0.0, 0.0, 0.0)
planarX = 0.0
planarY = 0.0
planarZ = 0.0
edgenum = 0
vertXList = []
vertYList = []
vertZList = []
xPlaneVerts = []
yPlaneVerts = []
zPlaneVerts = []
def sumList(coordinate_list):
summing = 0.0
for i in coordinate_list:
summing += i
return summing
def VertsDeltaInPlane(axis, value1, value2):
min = 0.0000001
difference = calcDifference(value1, value2)
# gets dirty for larger numbers, but we aren't doing intergalatic travel.
if difference < min: difference = 0.0
print "difference in axis " + axis + " component of this edge is " + str(difference)
return difference
def printCoordinate(val_x,val_y,val_z):
print "| x = " + str(val_x),
print "| y = " + str(val_y),
print "| z = " + str(val_z)
for item in eList:
print "=========="
print "edge [" + str(edgenum) + "] .....(" + str(edgenum+1) + ")"
print "vertex 0",
p1x = item[0][0]
p1y = item[0][1]
p1z = item[0][2]
printCoordinate(p1x,p1y,p1z)
print "vertex 1",
p2x = item[1][0]
p2y = item[1][1]
p2z = item[1][2]
printCoordinate(p2x,p2y,p2z)
planarX = VertsDeltaInPlane("x", p1x, p2x)
planarY = VertsDeltaInPlane("y", p1y, p2y)
planarZ = VertsDeltaInPlane("z", p1z, p2z)
# collects all vertices
xComp = [p1x,p2x]
yComp = [p1y,p2y]
zComp = [p1z,p2z]
xPlaneVerts.extend(xComp)
yPlaneVerts.extend(yComp)
zPlaneVerts.extend(zComp)
vertXList.append(planarX)
vertYList.append(planarY)
vertZList.append(planarZ)
edgenum += 1
def myRound(value):
return ".7f" % value
print "-------conclusion---------"
# this statement checks if the the verts on each edge or on the same plane
# check here if the selection is usable or not.
if sumList(vertXList) == 0.0:
print "x", xPlaneVerts
if isListHomogenous(xPlaneVerts):
print "GOOD TO GO!",
print "the plane is X at " + str(round(p1x,6))
else:
print "same plane but not same elevation"
elif sumList(vertYList) == 0.0:
print "y", yPlaneVerts
if isListHomogenous(yPlaneVerts):
print "GOOD TO GO!",
print "the plane is Y at " + str(round(p1y,6))
else:
print "same plane but not same elevation"
elif sumList(vertZList) == 0.0:
print "z", zPlaneVerts
if isListHomogenous(zPlaneVerts):
print "GOOD TO GO!",
print "the plane is Z at " + str(round(p1z,6))
else:
print "same plane but not same elevation"
else:
print "not on the same plane, the script will not proceed"
printEdgeList(preStantonList)