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
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)
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.
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).
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