Files
SoftwareRenderer/parseObj.py
2025-11-29 10:07:59 +01:00

51 lines
1.4 KiB
Python

# Open the file in read mode
file = open("test.obj", "r")
# Read the entire content of the file
content = file.read().split("\n")
file.close()
startVerts = 0
verts = []
for index,line in enumerate(content):
if line[0] == 'o':
startVerts = index + 1
content = content[startVerts:]
break
for index,line in enumerate(content):
if "vn" in line:
endVerts = index
verts = content[:endVerts]
content = content[endVerts:]
break
normals = []
for index,line in enumerate(content):
if "vt" in line:
normals = content[:index]
content = content[index:]
break
startFaces = 0;
faces = []
for index,line in enumerate(content):
if line[0] == 'f':
startFaces = index
faces = content[startFaces:-1]
break
colors = ["vec3(" +",".join(vert.split(" ")[4:7]) + ")" for vert in verts]
verts = ["vec3(" +",".join(vert.split(" ")[1:4]) + ")" for vert in verts]
faces = [ ",".join(["{" + str(int((d.split("/")[0])) - 1) + "," + str(int((d.split("/")[2])) - 1) + "}" for d in face.split(" ")[1:4]]) for face in faces]
normals = ["vec3(" + ",".join(normal.split(" ")[1:4]) + ")" for normal in normals]
out = "#include \"model.hpp\" \n const model testModel({" + ",".join(verts) +"},{" + ",".join(faces) + "},{" + ",".join(normals) + "},{"+ ",".join(colors)+"});"
with open("testModel.hpp", "w") as f:
f.write(out)
print(out)
# Close the file