'How to duplicate vertices, normals, and texture coordinates of an OBJ file and map each one to the same index in Python?
Here is the following code I wrote to split an OBJ file into different arrays containing the vertices, normals, texture coordinates, and faces:
import re
import os.path as p
import sys
from contextlib import contextmanager
import os
class MyClass:
def __init__(self):
self.v_pat = re.compile(r'^v\s[\s\S]*')
self.vn_pat = re.compile(r"^vn\s[\s\S]*")
self.vt_pat = re.compile(r'^vt\s[\s\S]*')
self.f_pat = re.compile(r"^f\s[\s\S]*")
self.vertices = ['None']
self.v_normals = ['None']
self.v_textures = ['None']
self.faces = []
def split_obj(self, file_in):
with open(file_in, 'r') as f_in:
for line in f_in:
v = self.v_pat.match(line)
vn = self.vn_pat.match(line)
vt = self.vt_pat.match(line)
f = self.f_pat.match(line)
if v:
self.vertices.append(v.group())
elif vn:
self.v_normals.append(vn.group())
elif vt:
self.v_textures.append(vt.group())
elif f:
self.faces.append(f.group())
if __name__ == '__main__':
file_in = sys.argv[1]
myClass = MyClass()
myClass.split_obj(file_in)
print(myClass.vertices)
print(myClass.v_normals)
print(myClass.v_textures)
print(myClass.faces)
However, I have an issue. I need to read the file without using any shared vertices, so:
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 15.06.2019 21:14:04
mtllib cube.mtl
#
# object Nemu_2952D29C_0_003
#
v 0.0500 -0.0500 -0.0500
v -0.0500 -0.0500 0.0500
v -0.0500 -0.0500 -0.0500
v 0.0500 -0.0500 0.0500
v 0.0500 0.0500 0.0500
v 0.0500 0.0500 -0.0500
v -0.0500 0.0500 -0.0500
v -0.0500 0.0500 0.0500
# 8 vertices
vn 0.0000 -1.0000 -0.0000
vn 1.0000 0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 1.0000 -0.0000
# 6 vertex normals
vt 1.0000 0.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
vt 1.0000 1.0000 0.0000
vt -0.0000 0.0000 0.0000
vt -0.0000 1.0000 0.0000
# 6 texture coords
g Nemu_2952D29C_0_003
usemtl M_2952D29C_c
s 1
f 1/1/1 2/2/1 3/3/1
f 2/2/1 1/1/1 4/4/1
f 1/5/2 5/4/2 4/6/2
f 5/4/2 1/5/2 6/1/2
f 1/2/3 7/1/3 6/4/3
f 7/1/3 1/2/3 3/3/3
f 3/3/4 8/4/4 7/1/4
f 8/4/4 3/3/4 2/2/4
f 2/3/5 5/4/5 8/1/5
f 5/4/5 2/3/5 4/6/5
f 5/4/6 7/3/6 8/2/6
f 7/3/6 5/4/6 6/1/6
# 0 polygons - 12 triangles
Should be read as:
v 500.0 -499.9999 -500.0001
v 500.0 -499.9999 -500.0001
v 500.0 -499.9999 -500.0001
v -500.0 -500.0001 499.9999
v -500.0 -500.0001 499.9999
v -500.0 -500.0001 499.9999
v -500.0 -499.9999 -500.0001
v -500.0 -499.9999 -500.0001
v -500.0 -499.9999 -500.0001
v 500.0 -500.0001 499.9999
v 500.0 -500.0001 499.9999
v 500.0 -500.0001 499.9999
v 500.0 499.9999 500.0001
v 500.0 499.9999 500.0001
v 500.0 499.9999 500.0001
v 500.0 500.0001 -499.9999
v 500.0 500.0001 -499.9999
v 500.0 500.0001 -499.9999
v -500.0 500.0001 -499.9999
v -500.0 500.0001 -499.9999
v -500.0 500.0001 -499.9999
v -500.0 499.9999 500.0001
v -500.0 499.9999 500.0001
v -500.0 499.9999 500.0001
vt 1.0 0.0
vt 0.0 1.0
vt 0.0 0.0
vt 0.0 1.0
vt 0.0 1.0
vt 0.0 0.0
vt 0.0 0.0
vt 0.0 0.0
vt 0.0 0.0
vt 1.0 1.0
vt 0.0 1.0
vt 0.0 1.0
vt 1.0 1.0
vt 1.0 1.0
vt 1.0 1.0
vt 1.0 1.0
vt 1.0 0.0
vt 1.0 0.0
vt 1.0 0.0
vt 1.0 0.0
vt 0.0 0.0
vt 1.0 1.0
vt 1.0 0.0
vt 0.0 1.0
f 1/1 5/5 8/8
f 5/5 5/5 8/8
f 8/8 5/5 1/1
f 5/5 10/10 1/1
f 1/1 10/10 3/3
f 10/10 15/15 3/3
f 3/3 15/15 12/12
f 15/15 15/15 12/12
f 12/12 15/15 3/3
f 15/15 18/18 3/3
f 3/3 18/18 2/2
f 18/18 20/20 2/2
f 2/2 20/20 16/16
f 20/20 20/20 16/16
f 16/16 20/20 2/2
f 20/20 9/9 2/2
f 2/2 9/9 7/7
f 9/9 22/22 7/7
f 7/7 22/22 19/19
f 22/22 22/22 19/19
f 19/19 22/22 7/7
f 22/22 4/4 7/7
f 7/7 4/4 6/6
f 4/4 13/13 6/6
f 6/6 13/13 23/23
f 13/13 13/13 23/23
f 23/23 13/13 6/6
f 13/13 11/11 6/6
f 6/6 11/11 14/14
f 11/11 21/21 14/14
f 14/14 21/21 24/24
f 21/21 21/21 24/24
f 24/24 21/21 14/14
f 21/21 17/17 14/14
What would this look like implemented into the code I wrote that reads an OBJ file? Basically, every time a vertex index doesn't match a uv or normal, it gets duplicated. Also, I should note that the desired result that was given does not include normals; however, it should. Also, the end result should have the same vertex, normal, and uv count, and vertex should map to the same index (e.g. 1/1/1, 5/5/5, 8/8/8).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
