Skip to content

Instantly share code, notes, and snippets.

@afomi
Last active January 27, 2019 05:51
Show Gist options
  • Save afomi/87e4db214f71e756371a0e6f0f964f50 to your computer and use it in GitHub Desktop.
Save afomi/87e4db214f71e756371a0e6f0f964f50 to your computer and use it in GitHub Desktop.
jsonlines blender test
import json
import bpy
import bmesh
def createLine(name, pointList, thickness):
bpy.ops.mesh.primitive_plane_add(size=2, view_align=False, enter_editmode=False, location=(0, 0, 0))
obj = bpy.context.object
me = obj.data
bpy.ops.object.editmode_toggle()
bm = bmesh.from_edit_mesh(me)
for point in pointList:
bm.verts.new((point[0], point[1], point[2]))
vertices = []
for i in range(len(pointList)):
point = pointList[i]
print(point)
vertex = bm.verts.new((point[0], point[1], point[2]))
vertices.append(vertex)
for j in range(len(vertices)):
# Conditionally handle the (last element, first element) pair
print(j)
if j == len(vertices) - 1:
bm.edges.new((vertices[j], vertices[0]))
else:
bm.edges.new((vertices[j], vertices[j + 1]))
bm.faces.new(vertices)
bmesh.update_edit_mesh(obj.data)
bpy.ops.object.editmode_toggle()
with open("/tmp/london-office.xml.jsonl", "r") as read_file:
# plane = bpy.ops.mesh.primitive_plane_add(size=2, view_align=False, enter_editmode=False, location=(0, 0, 0))
# bpy.ops.object.editmode_toggle()
for line in read_file:
data = json.loads(line)
# process coordinates in triplets x,y,z,x2,y2,z2,etc.
coords = data["coordinates"]
# Gather vertices
vertices = []
i = 0
while i < len(coords):
x = float(coords[i])
y = float(coords[i + 1])
z = float(coords[i + 2])
vertex = [x,y,z]
vertices.append(vertex)
i = i + 3
createLine("za", vertices, 0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment