0
$\begingroup$

I parse 2 csvs, one with vertex coordinates

id,x,y,z,r,g,b
0,0.30778491,0.12740749,0.04903458,174.0,84.0,118.0
1,0.3315943,0.0922608,0.08094925,174.0,84.0,118.0

and one listing edges like so

source,target
214,0
214,1

After creating the vertices in a mesh, how do i add the edges between the vertex indices listed in the second csv?

Do I need a nested for loop? itertools? thank you!

import bpy
import csv, os, bmesh, math

#filepaths
filepath = bpy.data.filepath
directory = os.path.dirname(filepath)

verts = []
edges = []
faces = []

csvpoints= directory+"/csv/3d-points.csv"

csvedges = directory+"/csv/3d-edges.csv"

pointsReader = csv.reader(open(csvpoints, newline=''), delimiter=',')   

edgesReader = csv.reader(open(csvedges, newline=''), delimiter=',')

with open(csvpoints, 'rt', encoding="utf8") as csvfile:
    pointsReader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for idx, row in enumerate(pointsReader):
        if (idx > 0):
            vert = (float(row[1]), float(row[2]), float(row[3])) 
            verts.append(vert)


obj = bpy.context.object

#create mesh and object
mesh = bpy.data.meshes.new("edgewave")
object = bpy.data.objects.new("edgewave",mesh)

#create mesh from python data
mesh.from_pydata(verts,edges,[])
mesh.update(calc_edges=True)

#set mesh location
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)

with open(csvedges, 'rt', encoding="utf8") as csvfile2:
    edgesReader = csv.reader(csvfile2, delimiter=',', quotechar='|')
    for idy, row in enumerate(pointsReader):
        if (idy > 0):
            edge = mesh.verts.[edgesReader.row[0]], mesh.verts[edgesReader.row[1]])
            edges.append(edge)

bmesh.update_edit_mesh(obj.data)
$\endgroup$
1
$\begingroup$

I simplified it a bit. I later found out I had a syntax error in my loop - I was doing the right thing in principle before. The object you are adding edges to has to be selected and in edit mode when running the script.

import bpy
import csv, os, bmesh, math

#filepaths
filepath = bpy.data.filepath
directory = os.path.dirname(filepath)

verts = []
edges = []
faces = []



csvedges = directory+"/csv/3d-edges.csv"



edgesReader = csv.reader(open(csvedges, newline=''), delimiter=',')


current_obj = bpy.data.objects["wave"]  # particular object by name


obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data)


if hasattr(bm.verts, "ensure_lookup_table"): 
    bm.verts.ensure_lookup_table()

with open(csvedges, 'rt', encoding="utf8") as csvfile2:
    edgesReader = csv.reader(csvfile2, delimiter=',', quotechar='|')
    for idy, row in enumerate(edgesReader):
        if (idy > 0) :
            #print(idy)
            bm.edges.new((bm.verts[int(row[0])], bm.verts[int(row[1])]))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.