Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions mcubes/exporter.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@

import numpy as np


def export_obj(vertices: np.ndarray, triangles: np.ndarray, filename: str, flip_normals: bool = False):
"""
Export a 3D mesh to a Wavefront (.obj) file.

If `flip_normals` is True, reverses the order of the vertices in each face
to flip the normals. Default is False.
"""

with open(filename, 'w') as fh:

for v in vertices:
fh.write("v {} {} {}\n".format(*v))

# Write vertices
np.savetxt(fh, vertices, fmt='v %.6f %.6f %.6f')
# Prepare faces (OBJ is 1-based indexing)
if not flip_normals:
for f in triangles:
fh.write("f {} {} {}\n".format(*(f + 1)))
np.savetxt(fh, triangles + 1, fmt='f %d %d %d')
else:
for f in triangles:
fh.write("f {} {} {}\n".format(*(f[::-1] + 1)))

np.savetxt(fh, triangles[:, ::-1] + 1, fmt='f %d %d %d')

def export_off(vertices: np.ndarray, triangles: np.ndarray, filename: str):
"""
Expand Down