Create edges from points csv

Hi guys,

I found a lot of information in here, but still can’t find a way to connect all my points with edges and fill the boundary with polygon.
I have an csv file and I managed to create a mesh with it. Now I need to connect points. Assume points are in correct order. I have attached an csv as well…test.csv (18 Bytes)

import bpy
import csv, os, bmesh, math

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

#read file change file path
csvPoints = "/Desktop/test.csv"
pointsReader = csv.reader(open(csvPoints, newline=''), delimiter=',')   

#add xyz to verts
for row in pointsReader:
    vert = (float(row[0]), float(row[1]), 0) 
    verts.append(vert)
      
#create mesh and object
mesh = bpy.data.meshes.new("Boundary")
object = bpy.data.objects.new("Boundary",mesh)

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

#set mesh location
bpy.context.collection.objects.link(object)

#add edges
???
    

Your csv file does not include face data which would allow the calculation of edges. You either need to have edge data included in your file (how verts are connected) [(v1, v2) ,(v1, v4), (v2, v3), (v3, v4), …] or face data [(v1, v2, v3, v4), (v1, v2, v5, v6), …] to calculate the other.

Don’t think I need a second file or include a face data. I was thinking of using the same file to loop thought points already in a mesh and create edges between points. Cant find which command will construct edge between two points. Then I can just loop through points in csv

So basically I have added all points into list form csv file. They are in correct order.

for row in pointsReader:
    vert = (float(row[0]), float(row[1]), 0) 
    verts.append(vert)

Now I need to add these points in pairs to edges list. Something like this but with the loop to iterate through all points:

#add edges
edges.append((mesh.verts[2]), (mesh.verts[3]))

And last step will be create mesh using two lists verts and edges:

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

#set mesh location
bpy.context.collection.objects.link(object)

So I am not sure how to do the edges part.New to Blender Python API.

I found this example. But I don’t want to create a new list of edges using points list.

What is the right syntax for this?

edges.append((int(a[1:]), int(b[1:])))

I think I solved it. Just one question. Why I am getting error while creating edges?

TypeError: an integer is required (got type tuple)

import bpy
import csv, os, bmesh, math

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

#read file change file path
csvPoints = "/Desktop/test.csv"
pointsReader = csv.reader(open(csvPoints, newline=''), delimiter=',')   

#add xyz to verts
for row in pointsReader:
    vert = (float(row[0]), float(row[1]), 0) 
    verts.append(vert)
    
#create mesh and object
mesh = bpy.data.meshes.new("Boundary")
object = bpy.data.objects.new("Boundary",mesh)

#Add edges. Using verts list to get start and end points for edges
for idx, vert in enumerate(verts):
    startPoint = verts[idx]
    if idx < len(verts)-1:
        endPoint = verts[idx+1]
    else:
        endPoint = verts[0]
    edge = startPoint, endPoint
    edges.append(edge)
    
#create mesh from python data
mesh.from_pydata(verts,edges,[])
mesh.update(calc_edges=True)

#set mesh location
bpy.context.collection.objects.link(object)

I thought I have to provide a set of two values to construct edges?

#Add edges. Using verts list to get start and end points for edges
for idx, vert in enumerate(verts):
    startPoint = verts[idx]
    if idx < len(verts)-1:
        endPoint = verts[idx+1]
    else:
        endPoint = verts[0]
    edge = startPoint, endPoint
    edges.append(edge)

Anyone?

Edges should be pairs of vertex indices, not pairs of vertices.

Thank you @Stan_Pancakes so should I access points of the mesh after I have made the mesh with only points?
Is the order of created mesh points will be the same as in my csv file?

Initially, yes, but the order may change if you alter the topology (and there are sorting operators in edit mode). Anyway, after you create the Mesh it has its own data structures for accessing elements.

1 Like

Thanks. Let me try if I can make it to work…

Hi @Stan_Pancakes
So I now can create and view my mesh points on the screen. I have also took pairs of points indexes from verbs list. (The list I used to create mesh).

Screenshot 2021-03-21 at 13.43.52

import bpy
import csv, os, bmesh, math

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

#read file change file path
csvPoints = "/Users/Desktop/test.csv"
pointsReader = csv.reader(open(csvPoints, newline=''), delimiter=',')   

#add xyz to verts
for row in pointsReader:
    vert = (float(row[0]), float(row[1]), 0) 
    verts.append(vert)

#create mesh and object
mesh = bpy.data.meshes.new("Boundary")
object = bpy.data.objects.new("Boundary",mesh)
    
#create mesh from python data
mesh.from_pydata(verts,edges,[])
mesh.update(calc_edges=True)

#set mesh location
bpy.context.collection.objects.link(object)

#Add edges. Using verts list to get start and end points for edges
for idx, vert in enumerate(verts):
    startPoint = idx
    if idx < len(verts)-1:
        endPoint = idx+1
    else:
        endPoint = 0
    edge = startPoint, endPoint
    edges.append(edge)

print(edges)

# Updated mesh

How now to draw edges and update mesh? Sorry just getting used to Blender API

Ok so you don’t have to update mesh. You can actual create all the data first.
Working script if anyone needs it.

import bpy
import csv

#data blocks for the mesh
verts = []
edges = []
faces = []

#read file change file path
csvPoints = "/Users/Desktop/test.csv"
pointsReader = csv.reader(open(csvPoints, newline=''), delimiter=',')   

#add points to verts
for row in pointsReader:
    vert = (float(row[0]), float(row[1]), 0) 
    verts.append(vert)
    
#Add points indexes to edges
for idx, vert in enumerate(verts):
    startPoint = idx
    if idx < len(verts)-1:
        endPoint = idx+1
    else:
        endPoint = 0
    edge = startPoint, endPoint
    edges.append(edge)

#create mesh 
newMesh = bpy.data.meshes.new("Boundary")
    
#cAdd python data blocks to mesh
newMesh.from_pydata(verts,edges,faces)
newMesh.update(calc_edges=True)

#create an object
newObject = bpy.data.objects.new("Boundary",newMesh)

#set mesh location
bpy.context.collection.objects.link(newObject)