1
$\begingroup$

I have the following situation: I have a stl-file, I write every vertices-coordinates into a csv-file. Then I kind of "arrange" all the values, do some other math-stuff and as a result I get another csv-list with (x,y,z)-co-ordinates for every vertex.

My problem is now: When I try to import and plot the vertex-co-ordinates I'll get a bunch of lines, all connected to with the origin, instead of a clean line along the, as I thought, single vertices(red line). See the picture.

enter image description here

Now, can anyone tell me if and how it is possible to create single vertices with 3D-co-ordinates? If you need some of my code feel free to ask.

Thanks to everyone.

EDIT:

Here is the script:

import numpy as np
import csv
import os
import bpy
import math



filename = 'list_of_Vertices.csv'
directory = r'full path to file'  # <-- if windows, the r is important

fullpath = os.path.join(directory, filename)

with open(fullpath, 'r', newline='') as csvfile:
    ofile = csv.reader(csvfile, delimiter=',')

    # this makes a generator of the remaining non-empty lines
    rows = (r for r in ofile if r)
    print(rows)
    # this converts the string representation of each line
    # to an x,y,z list, and stores it in the verts list.
    verts = [[float(i) for i in r] for r in rows]

if verts:
    # curve coordinates require a 4th 'W'(weight) component,
    # the +[0.0] adds that for us
    out2 = []
    [out2.extend(list(i)+[0.0]) for i in verts] 
    # has one coordinate by default, we add one fewer than we need
    num_points_to_add = len(verts) - 1
    curve = bpy.data.curves.new("path_name", type='CURVE')
    polyline = curve.splines.new(type='POLY')
    polyline.points.add(num_points_to_add)
    polyline.points.foreach_set('co', out2)

    obj = bpy.data.objects.new("obj_name", curve)
    scene = bpy.context.scene
    scene.objects.link(obj)

    # Verts in einzelne Spalten x,y, und z übergeben
    def column(matrix, i):
        return [row[i] for row in matrix]
    x=column(verts,0)
    y=column(verts,1)
    z=column(verts,2)
    #Kopie der Daten anlegen
    x2=x
    y2=y
    z2=z

    #y auf eine ganze Zahl runden
    y=np.round(y,0)

    #Maximum und Minimum herausfinden, daraus die Differenz berechnen
    Max=max(y)
    Min=min(y)
    Diff=math.floor(Max-Min+1)
    print(Min)
    print(Max)
    print(Diff)

    #Alle werte des Zahnfleisches auf Null setzen
    for i in range (1,len(y)):
        if z[i]<= -1:
            x[i]=0
            z[i]=0
    #Werte für x und z in Matrizen schreiben
    Posx=np.zeros(shape=(len(y),Diff))
    Posz=np.zeros(shape=(len(y),Diff))
    h=0
    k=0
    for i in range (0,Diff):
        h=i+Min 
        k=0
        F=np.where(y==h)
        F2=np.transpose(F)
        L=len(np.transpose(F))
        for j in range (0,len(y)):  #Diff durch ley(y) tauschen!!
            if F2[k,0] ==j:
                Posx[j][i]=x[j]
                Posz[j][i]=z[j]
                if k<L-1:
                    k=k+1        
            else:
                Posx[j][i]=0
                Posz[j][i]=0
            if Posz[j][i]<=0:
                Posx[j][i]=0
                Posz[j][i]=0
    main_path = os.getcwd()
    print("writing to: " + main_path + "\n\n")

    outputfile = 'full path to new file'
    with open(outputfile, 'w') as w_file: 
        for i in range (0,Diff):  
            coords1 = Posx[i,:]
            w_file.write(str(coords1)+" \n") 
    #Minimum und Maximum suchen
    Minimum=np.arange(Diff)
    Maximum=np.arange(Diff)
    Maxz=np.linspace(0,1,Diff)
    Minz=np.linspace(0,1,Diff)
    Posmin=np.arange(Diff)
    Posmax=np.arange(Diff)
    mini=np.min(Posx,0)
    maxi=np.max(Posx,0)
    Minimum=mini
    Maximum=maxi
    for i in range(0, Diff):
        if Minimum[i]==0 and Maximum[i] !=0:
            Minimum[i]= min(i for i in Posx[:,i] if i > 0)
        if Maximum[i]==0 and Minimum[i] !=0:
            Maximum[i]= max(i for i in Posx[:,i] if i < 0)
        for j in range (0, len(y)):
            if Posx[j][i]==Minimum[i]:
                Posmin[i]=j
                Minz[i]=Posz[j,i]

            if Posx[j][i]==Maximum[i]: 
                Posmax[i]=j
                Maxz[i]=Posz[j,i]


    yAchse=np.arange(Diff)
    n=0;
    for i in range(0, Diff):
        n=Min+i
        yAchse[i]=n
        coords = (Minimum[i], yAchse[i], Minz[i])
        curveData = bpy.data.curves.new('myCurve', type= 'CURVE')
        curveData.dimensions = '3D'
        polyline = curveData.splines.new(type='POLY')
        polyline.points.add(Diff)
        polyline.points[i].co = (Minimum[i], yAchse[i], Minz[i],1)

        curveOB = bpy.data.objects.new('myCurve', curveData)

        scn = bpy.context.scene
        scn.objects.link(curveOB)
        scn.objects.active = curveOB
        curveOB.select = True

### für die Kurve auf der anderen Seite der Zähne ###

for i in range(0, Diff):
        n=Min+i
        yAchse[i]=n
        coords = (Maximum[i], yAchse[i], Maxz[i])
        curveData = bpy.data.curves.new('myCurve2', type= 'CURVE')
        curveData.dimensions = '3D'
        polyline = curveData.splines.new(type='POLY')
        polyline.points.add(Diff)
        polyline.points[i].co = (Maximum[i], yAchse[i], Maxz[i],1)

        curveOB = bpy.data.objects.new('myCurve2', curveData)

        scn = bpy.context.scene
        scn.objects.link(curveOB)
        scn.objects.active = curveOB
        curveOB.select = True
$\endgroup$
2
  • $\begingroup$ are you looking for a way to do it through python, or just clean up and use that mesh? $\endgroup$
    – David
    Jul 28 '17 at 12:03
  • $\begingroup$ Share your script. I expect when you specify the two verts that make the edge, you are always using the first vert with the new vert. You probably want the new vert with the previous vert. $\endgroup$
    – sambler
    Jul 28 '17 at 16:36

Browse other questions tagged or ask your own question.