-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractModelData.py
More file actions
105 lines (90 loc) · 3.87 KB
/
extractModelData.py
File metadata and controls
105 lines (90 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os.path
from OpenGL.GL import *
from OpenGL.GLU import *
def extractModelData(filePath):
basePath = filePath.removesuffix('.obj')
#modelVerticesPath = basePath + "-vertices.txt"
#modelTexturesPath = basePath + "-textures.txt"
#modelNormalsPath = basePath + "-normals.txt"
verticesPath = basePath + "-vertices.txt"
vtPath = basePath + "-vt.txt"
vn = basePath + "-vn.txt"
vertexIndicesPath = basePath + "-vertexIndices.txt"
uvIndicesPath = basePath + "-uvIndices.txt"
normalIndicesPath = basePath + "-normalIndices.txt"
vertices = []
vt = []
vn = []
vertexIndices = []
uvIndices = []
normalIndices = []
# Check if model info files exist
if not os.path.isfile(verticesPath) or not os.path.isfile(vertexIndicesPath):
# Create new model info files
with open(filePath) as file:
# Read through file
for line in file:
line = line.rstrip("\r\n")
line = list(line.split(' '))
type = line[0]
# Add values to associated list
if type == 'v':
values = [float(value) for value in line[1:]]
vertices.append(values)
elif type == 'vn':
values = [float(value) for value in line[1:]]
vn.append(values)
elif type == 'vt':
values = [float(value) for value in line[1:]]
vt.append(values)
elif type == 'f':
indexGroups = line[1:]
faceVertexIndices = []
faceUvIndices = []
faceNormalIndices = []
for i in range(len(indexGroups)):
indices = [int(value) for value in list(indexGroups[i].split('/'))]
faceVertexIndices.append(indices[0])
faceUvIndices.append(indices[1])
faceNormalIndices.append(indices[2])
vertexIndices.append(faceVertexIndices)
uvIndices.append(faceUvIndices)
normalIndices.append(faceNormalIndices)
# Index faces
#modelVertices = indexVertices(vertices, vertexIndices)
#modelTextures = indexVertices(vt, uvIndices)
#modelNormals = indexVertices(vn, normalIndices)
# Write vertex and face file
#writeListToFile(modelVerticesPath, modelVertices)
#writeListToFile(modelTexturesPath, modelTextures)
#writeListToFile(modelNormalsPath, modelNormals)
writeListToFile(verticesPath, vertices)
writeListToFile(vertexIndicesPath, vertexIndices)
else:
# Read existing model info files
#modelVertices = readFileToList(modelVerticesPath)
#modelTextures = readFileToList(modelTexturesPath)
#modelNormals = readFileToList(modelNormalsPath)
vertices = readFileToList(verticesPath)
vertexIndices = readFileToList(vertexIndicesPath, True)
return vertices, vertexIndices
def indexVertices(vertices, indices):
indexed = []
for index in indices:
indexed.append(vertices[index - 1]) # index starts at 1
return indexed
def writeListToFile(filePath, list):
with open(filePath, 'w+') as file:
for line in list:
file.write(f'{line}\n')
def readFileToList(filePath, index=False):
outputList = []
with open(filePath, 'r') as file:
for line in file:
values = list(line.strip('][\n').split(', '))
if index:
converted = [int(value) for value in values]
else:
converted = [float(value) for value in values]
outputList.append(converted)
return outputList