Created
June 14, 2019 15:41
-
-
Save davegreenwood/ad4dfb4b5810a1928c9c0a84cbce4cd6 to your computer and use it in GitHub Desktop.
Example of how to reorder the vertex numbering in an obj file with a python script.
This file contains 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
# %% reorder the vertices | |
import numpy as np | |
# %% | |
with open("oldorder.obj") as fid: | |
lines = fid.readlines() | |
print(lines) | |
# %% | |
def split_face_line(line): | |
return [int(s.split('/')[0]) for s in line.split()[1:]] | |
def split_vert_line(line): | |
return [[float(s) for s in ss.split()] for ss in line.split()[1:]] | |
# %% | |
tri, pts = [], [] | |
for line in lines: | |
line = line.strip() | |
if '#' in line: | |
continue | |
if 'vt' in line: | |
continue | |
if 'f' in line: | |
tri.append(split_face_line(line)) | |
if 'v ' in line: | |
pts.append(split_vert_line(line)) | |
tri = np.array(tri, dtype=np.int32) - 1 | |
pts = np.array(pts, dtype=np.float32) | |
# %% | |
# the current zero indexed order... | |
reorder = np.array([ | |
55, 45, 44, 43, 42, 41, 40, 39, 0, 7, 8, 9, 10, 11, 12, 13, 23, 60, 59, | |
58, 57, 56, 25, 26, 27, 28, 29, 24, 6, 5, 4, 48, 49, 3, 17, 16, 46, 64, 65, | |
47, 67, 66, 15, 36, 35, 14, 37, 38, 52, 51, 50, 2, 18, 19, 20, 21, 22, 1, | |
54, 53, 62, 61, 30, 31, 32, 33, 34, 63]) | |
inv_reorder = np.zeros(68, dtype=np.int32) | |
for i, k in enumerate(reorder): | |
inv_reorder[k] = i | |
new_tris = inv_reorder[tri] | |
new_pts = pts[reorder] | |
#%% | |
with open("neworder.obj", 'w') as fid: | |
for p in new_pts.squeeze(): | |
fid.write(f"v {p[0]:0.3f} {p[1]:0.3f} {p[2]:0.3f}\n") | |
for t in 1 + new_tris.squeeze(): | |
fid.write(f"f {t[0]:d} {t[1]:d} {t[2]:d}\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment