I have 24 points which correspond to 12 different "elements" (2 points per element) saved in an .csv file with the following column order (1: Element Name, 2: x, 3: y, 4: z). I would like to import them so that there will be an edge between the two points for every element. Alternatively if this is not possible. It would be great to import all the points as separate elements. So I can know which points correspond to which element by the order of import.
Update:
This is great and I would like to extend this to the first six point: I tried to update your code with the edges I want, but it fails.
What does ensure_lookup_table() do ?
In if row[0] there is only one vert. How can I add more ? The Ideal solution would be select 1,6 or all verts and make a mesh. The order of the edges would be cool to have but if too much hustle. Just importing 1, 6 or all verts and connecting them to a mesh would good.
Here my attempt to updated your code:
import bpy
import bmesh
import csv
from mathutils import Vector
from bpy_extras.object_utils import object_data_add
csvfile = open('path2csvfile')
inFile = csv.reader(csvfile, delimiter=',', quotechar='"')
# skip header
inFile.__next__()
for row in inFile:
# column order (0: Element Name, 1: x, 2: y, 3: z)
if row[0] not in bpy.data.objects:
vert = [Vector((float(row[1]),float(row[2]),float(row[3])))]
mesh = bpy.data.meshes.new(name=row[0])
mesh.from_pydata(vert, [], [])
object_data_add(bpy.context, mesh)
else:
bpy.data.objects[row[0]].select = True
bpy.context.scene.objects.active = bpy.data.objects[row[0]]
bpy.ops.object.mode_set(mode='EDIT')
mesh = bmesh.from_edit_mesh(bpy.data.objects[row[0]].data)
mesh.verts.new((float(row[1]),float(row[2]),float(row[3])))
mesh.verts.ensure_lookup_table()
mesh.edges.new([mesh.verts[0],mesh.verts[1]])
mesh.edges.new([mesh.verts[1],mesh.verts[2]])
mesh.edges.new([mesh.verts[2],mesh.verts[3]])
mesh.edges.new([mesh.verts[1],mesh.verts[4]])
mesh.edges.new([mesh.verts[4],mesh.verts[5]])
bmesh.update_edit_mesh(bpy.data.objects[row[0]].data, True)
bpy.ops.object.mode_set(mode='OBJECT')