Surface coordinate code for the retopo tool?

I have a script that finds the coords of a point on a mesh under some other point in space but currently it has to search through the verts of the mesh data to find the closest vert and then read the coords. Ouch! as you can guess it’s kinda slow!!
Sooo, can anyone give example python code of how the ‘Retopo’ or ‘Shrinkwrap’ tools get the coords of a surface behind some location?? I understand the retopo uses the view and some kind of depth buffer to find the locations of the surfaces but how its done!:eek:

Post the code - maybe we can help?

Right ! i’ll post it as soon as possible. It’s not really that complicated; it simply compares the coords of a vector to the coords of each vert as it loops through them.

Here’s the code - the indents are probably messed up but you should be able to figure out how it works!

def find_nearest_vertice(vec_loc):
counter=-1
selected=GetSelected()[0]
verts=selected.data.verts
for cords in verts:
vert=Vector(cords[0],cords[1],cords[2])
counter+=1
if counter==0:
lastval[0]=sqrt((vert.x-vec_loc.x)**2+(vert.y-vec_loc.y)**2)# on the first item the lastval is set to the distance between the vector and the vert
vertcl=vert # pointer to the last vert that this block returned true with
if counter>0:# if the item isn’t the first in the list
if sqrt((vert.x-vec_loc.x)**2+(vert.y-vec_loc.y)**2)<lastval[0]:# and the distance between the vector and vert is less than the previous ‘lastval’
lastval[0]=sqrt((vert.x-vec_loc.x)**2+(vert.y-vec_loc.y)**2)# set the smallest distance to the distance between the vert and vector
vertcl=vert# pointer to the last vert that this block return true with - the closest one
if counter==len(verts)-1:
return vertcl # when reach end of list, return the closest vert found

As you can see it’s slow because it’s forced to loop through the entire vert list every time it’s called since it’s searching for the closest vert and that vert could be at any position in the list.

Do the indents look any better?

 
def find_nearest_vertice(vec_loc):
   counter=-1
   selected=GetSelected()[0]
   verts=selected.data.verts
   for cords in verts:
    vert=Vector(cords[0],cords[1],cords[2])
    counter+=1
    if counter==0:
     lastval[0]=sqrt((vert.x-vec_loc.x)**2+(vert.y-vec_loc.y)**2)# on the first item the lastval is set to the distance between the vector and the vert
     vertcl=vert # pointer to the last vert that this block returned true with
    if counter&gt;0:# if the item isn't the first in the list
     if sqrt((vert.x-vec_loc.x)**2+(vert.y-vec_loc.y)**2)&lt;lastval[0]:# and the distance between the vector and vert is less than the previous 'lastval'
      lastval[0]=sqrt((vert.x-vec_loc.x)**2+(vert.y-vec_loc.y)**2)# set the smallest distance to the distance between the vert and vector
      vertcl=vert# pointer to the last vert that this block return true with - the closest one 
    if counter==len(verts)-1:
     return vertcl # when reach end of list, return the closest vert found
     

i suppose you can get some inspiration from these scripts:
http://wiki.blender.org/index.php/Scripts/Manual/Scriptlinks/mesh_bbrush_menu
http://wiki.blender.org/index.php/Scripts/Manual/SpaceHandlers/Espresso