sorting vertex to draw a cyclic nubs curve?

Hi i’m actually stuck with a short script that pick points from a mesh and organize them into lists of same X-coordinates to draw curves from those lists. The problem is:
1- curves are not closed and I don’t know how to close them (maybe “myCurve.isCyclic== True”?)
2-curves are disorganised because points inside the lists of same X-coordinates are not sorted well, and I don’t know how to do that.

here is the script (it was specially written for metaballs converted to mesh so maybe it may causes some error for other objects):

#IMPORTING MODULES#
import Blender
from Blender import Mesh, Object, Scene, NMesh, Curve, CurNurb

ob = Blender.Object.GetSelected()[0]# Selectionne le premier objet selectionne
me = NMesh.GetRawFromObject(ob.getName())# Recupere la geometrie du mesh selectionne
# declaration des listes
VertList= []# liste de vertex du mesh
ListSameX = []# liste de vertex du mesh qui ont meme abscisse

for v in me.verts: # recupere les coordonnees des vertex du mesh et rajoute une Quatrieme valeur correspondant aux futures points de la courbe
    
    x = v.co[0]
    y = v.co[1]
    z = v.co[2]    
    w = 2.0
    
    Vco = [x,y,z,w]
    VertList.append(Vco)# la liste comprend un ensemble de listes de quatres elements chacun
    
for coorda in VertList:
    
    SameX=[]
        
    for coordb in VertList:
        
        if coordb not in SameX:
            
            if coorda[0] == coordb[0]:
                
                SameX.append(coordb)
                
    ListSameX.append(SameX)
    
for SameX in    ListSameX:
        
    cu = Curve.New()
    cu.appendNurb(SameX[0])
    cu_nurb = cu[0]
    SameX.remove(SameX[0])
    
    for coordb in SameX:
        
        cu_nurb.append(coordb)
            
    ob= Object.New('Curve')
     ob.link(cu)
    scn= Scene.GetCurrent()
    scn.link(ob)
    ob.layers= [6]
    Blender.Redraw()
    

anybody for saving me:D;)?

this dosnt address all your questions, but I wrote a script that converts mesh edges to 3d curves
mesh_edges2curves.py in blenders scripts folder.

thanks ideasman42

i’m actually testing your script mesh_edges2curve.py
i have deleted faces from my mesh and selectioned the edges (i don’t know if it it is the right way to work with the script ). But it seems to draw only one curve from all the edges.
is there any way to obtain separated curves?

I also tried to fix the problem of my script by comparing the angle between each vertex inside the list SameX (the list of vertices for one curve). so the smallest angle corespond to the nearest point from the first one(this a rule supposed to reorder vertices inside the list) and correspond to the next point of the curve. But, strangely it works like the first script.



import Blender
from Blender import Mesh, Object, Scene, NMesh, Curve, CurNurb

ob = Blender.Object.GetSelected()[0]
me = NMesh.GetRawFromObject(ob.getName())
VertList= []# liste de vertex du mesh
ListSameX = []# liste de vertex du mesh qui ont meme abscisse

for v in me.verts: 
    
    x = v.co[0]
    y = v.co[1]
    z = v.co[2]    
    w = 2.0
    
    Vco = [x,y,z,w]
    VertList.append(Vco)
    
for coorda in VertList:
    
    SameX=[]
        
    for coordb in VertList:
        
        if coordb not in SameX:
            
            if coorda[0] == coordb[0]:
                
                SameX.append(coordb)
            
    
    
    point0 = SameX[0]            
    tamponSameX = [point0]
    
    while point0:
        
        dist_point = {}
        distances = []
        
        for coordb in SameX:
            
            d = ((coordb[1]-SameX[0][1])**2+(coordb[2]-SameX[0][2])**2)**0.5    
            dist_point[d] = coordb
            distances.append(d)
            
        distances.sort()
        dmini = distances[0]
        next_point = dist_point.get(dmini)
        tamponSameX.append(next_point)
        SameX.remove(SameX[0])
        point0 = next_point
        
        if len(SameX) == 0:
            
            point0 = False
                            
    SameX = tamponSameX[:]    
    ListSameX.append(SameX)
    
for SameX in    ListSameX:
        
    cu = Curve.New()
    cu.appendNurb(SameX[0])
    cu_nurb = cu[0]
    SameX.remove(SameX[0])
    
    for coordb in SameX:
        
        cu_nurb.append(coordb)
            
    ob= Object.New('Curve')
     ob.link(cu)
    scn= Scene.GetCurrent()
    scn.link(ob)
    ob.layers= [6]
    Blender.Redraw()