50 lines
1.3 KiB
Python
50 lines
1.3 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
|
|
|
|
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) + "});"
|
|
|
|
with open("testModel.hpp", "w") as f:
|
|
f.write(out)
|
|
print(out)
|
|
# Close the file
|