Open clbarnes opened 5 years ago
def write_obj(path, mesh):
vertex_fmt = "v {} {} {}\n"
face_fmt = "f {} {} {}\n"
with open(path, "w") as f:
for vertex in mesh.points:
f.write(vertex_fmt.format(*vertex))
for face in mesh.cells["triangle"]:
f.write(face_fmt.format(*(face + 1))) # 1-based indexing
def read_obj(path):
points = []
triangles = []
with open(path) as f:
for row in f:
if row.startswith("v "):
points.append([float(item) for item in row.split()[1:]])
elif row.startswith("f "):
triangles.append([int(item.split('/')[0]) for item in row.split()[1:]])
return Mesh(np.array(points), {"triangle": np.array(triangles)})