got it!
import bge
import mathutils
import math
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
mesh = own.meshes[0]
if 'kd' not in own:
size = mesh.numPolygons
plane = own.scene.objectsInactive['Plane']
table = {}
#build array of subdivided patch on layer 2
for vertex in range(plane.meshes[0].getVertexArrayLength(0)):
vert = plane.meshes[0].getVertex(0,vertex)
value = math.ceil(vert.XYZ.x*16)/16
Y_value = math.ceil(vert.XYZ.y*16)/16
if value not in table:
table.update({ value:[ [vertex,Y_value] ] } )
else:
print('adding')
entry = table[value]
entry.append([vertex,Y_value])
table[value]=entry
for entry in table:
data = table[entry]
data = sorted(data, key=lambda x: x[1])
table[entry]=data
l = []
for entry in table:
l.append( [table[entry],entry] )
l = sorted(l, key=lambda x: x[1])
l2 = []
for entry in l:
l2.append(entry[0])
print(l2)
own['array']=l2
print('-')
#test row 1 of x
print(l[0][0][0])
print(l[0][0][1])
print(l[0][0][2])
print(l[0][0][3])
print(l[0][0][4])
print(l[0][0][5])
print(l[0][0][6])
print(l[0][0][7])
print(l[0][0][8])
print('-')
#test row 2
print(l[1][0][0])
print(l[1][0][1])
print(l[1][0][2])
print(l[1][0][3])
print(l[1][0][4])
print(l[1][0][5])
print(l[1][0][6])
print(l[1][0][7])
print(l[1][0][8])
print('-')
#build kdtree of all cubesphere faces
kd = mathutils.kdtree.KDTree(size)
FaceDict = {}
for i in range(mesh.numPolygons):
poly = mesh.getPolygon(i)
v1 = mesh.getVertex(0,poly.v1)
v2 = mesh.getVertex(0,poly.v2)
v3 = mesh.getVertex(0,poly.v3)
v4 = mesh.getVertex(0,poly.v4)
v1p = v1.XYZ
v1.color = [v1p.x,v1p.y,v1p.z,1]
v2p = v2.XYZ
v2.color = [v2p.x,v2p.y,v2p.z,1]
v3p = v3.XYZ
v3.color = [v3p.x,v3p.y,v3p.z,1]
v4p = v4.XYZ
v4.color = [v4p.x,v4p.y,v4p.z,1]
o = (v1p+v2p+v3p+v4p)/4
origin = o
FaceDict.update({ i:[origin,v1p,v2p,v3p,v4p] })
kd.insert(origin,i)
print('createdTree')
kd.balance()
own['kd']=kd
own['faceDict']=FaceDict
own['patchesFree']=[]
own['filledLocations']={}
own['P_index']=0
#own.localScale =[.95,.95,.95]
#generate patches to use to skin cubesphere
elif len(own['patchesFree'])<=256-16:
for i in range(16):
#print('creating patch '+str(own['P_index']+1))
I = own['P_index']
own['P_index']+=1
added = own.scene.addObject('Plane',own,0)
name = 'meshCopy_'+str(I)
libNew = bge.logic.LibNew(name, 'Mesh',[added.meshes[0].name] )
added.replaceMesh(libNew[0],1,1)
#added.name = name
own['patchesFree'].append(added)
#added.suspendPhysics()
added.visible = False
else:
location = own.worldOrientation.inverted()* (own.scene.objects['skin_ob'].worldPosition-own.worldPosition)
near = own['kd'].find_range(location, .25)
'''
#draw faces to be skinned using drawLine
for entry in near:
orig = own['faceDict'][entry[1]][0]
v2 = own.getVectTo(orig)
p2 = orig+(v2[1]*.25)
bge.render.drawLine(orig,p2,[0,0,1])
'''
#try and fill a face
for entry in near:
if len(own['patchesFree'])>=1 and entry[1] not in own['filledLocations']:
face = own['patchesFree'][0]
own['patchesFree'].pop(0)
face.visible = True
own['filledLocations'].update({entry[1]:face})
orig = own['faceDict'][entry[1]][0]
v1 = own['faceDict'][entry[1]][1]
#bge.render.drawLine(v1,own.worldPosition,(0,0,1))
v2 = own['faceDict'][entry[1]][2]
#bge.render.drawLine(v2,own.worldPosition,(0,0,1))
v3 = own['faceDict'][entry[1]][3]
#bge.render.drawLine(v3,own.worldPosition,(0,0,1))
v4 = own['faceDict'][entry[1]][4]
#bge.render.drawLine(v1,v2,(1,0,0))
#bge.render.drawLine(v2,v3,(0,1,0))
# bge.render.drawLine(v3,v4,(0,0,1))
# bge.render.drawLine(v4,v1,(1,0,1))
face.worldPosition = orig
for x in range(len(own['array'])):
p1 = v1.lerp(v2,x/8)
p2 = v4.lerp(v3,x/8)
#bge.render.drawLine(p1,p2,(1,1,1))
v2x = -(v2-v3).normalized()
v2x
# bge.render.drawLine(v2,p2,(1,1,1))
for y in range(len(own['array'][x])):
yWidth = ((v2-v3).magnitude) /8
p2 = p1+(v2x*(yWidth*y))
#bge.render.drawLine(own.worldPosition,p2,(0,0,0))
data = own['array'][x][y]
print("X is "+str(x)+" and y is "+str(y)+" and v_index is "+str(data[0]))
vert = face.meshes[0].getVertex(0,data[0])
vert.XYZ = face.worldOrientation.inverted()*(p2-face.worldPosition)
vect2 = own.getVectTo(p2)
vert.normal = -vect2[1]
#here is the vertex and it's position in the array
#this is where I can use the array xy to set the vert location
#[0][0] should be v1
#[0][8] should be v2
#[8][0] should be v3
#[8][8] should be v4
break
#uncomment to draw all faces origin and normal
#for entry in own['faceDict']:
# orig = own['faceDict'][entry][0]
# v2 = own.getVectTo(orig)
# p2 = orig+(-v2[1]*.125)
# bge.render.drawLine(orig,p2,[1,0,0])
main()