Created
February 6, 2019 15:52
-
-
Save fritschy/b08ab42f2639590903fd3d1532a23cd9 to your computer and use it in GitHub Desktop.
Convert OFF to OBJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
num_vert, num_face, num_edge = 0, 0, 0 | |
not_handled_lines = 0 | |
expect_header = True | |
expect_count = None | |
expect_vertices = 0 | |
expect_faces = 0 | |
for line in sys.stdin: | |
line = line.rstrip() | |
if expect_header and line.startswith('OFF'): | |
expect_count = True | |
expect_header = False | |
elif expect_count: | |
expect_count = False | |
num_vert, num_face, num_edge = tuple(map(int, line.split(' ', 2))) | |
assert(num_edge == 0) | |
expect_vertices = num_vert | |
elif expect_vertices > 0: | |
expect_vertices -= 1 | |
if expect_vertices == 0: | |
expect_faces = num_face | |
v = line.split(' ') | |
assert(len(v) == 3 or len(v) == 4) | |
print('v {}'.format(' '.join(v))) | |
elif expect_faces > 0: | |
expect_faces -= 1 | |
f = line.split(' ') | |
assert((f[0] == '3' and len(f) == 4) or (f[0] == '4' and len(f) == 5)) | |
n = f[0] | |
f = list(map(lambda x: int(x) + 1, f[1:])) # OBJ indexes from 1 | |
print('f {}'.format(' '.join(map(str, f)))) | |
else: | |
not_handled_lines += 1 | |
sys.stderr.write('Converted {} vertices and {} faces to OBJ\n'.format(num_vert, num_face)) | |
if not_handled_lines > 0: | |
sys.stderr.write('{} lines were not expected\n'.format(not_handled_lines)) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment