-
Notifications
You must be signed in to change notification settings - Fork 0
/
WavefrontOBJ.py
166 lines (129 loc) · 5.29 KB
/
WavefrontOBJ.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# WavefrontMTL.py - Python script for parsing wavefront mtl files
#
# Copyright (c) 2023 by FalconCoding
# Author: Stefan Johnsen
# Email: stefan.johnsen@outlook.com
#
# This software is released under the MIT License.
import os
import sys
import numpy as np
vector = np.array
class Face:
def __init__(self):
self.vertex = []
self.texture = []
self.normal = []
class Geometry:
def __init__(self):
self.material = None
self.face = []
self.point = []
self.line = []
def index(objIndex, indexToList):
i = int(objIndex)
if i > 0: return i - 1
return i + len(indexToList)
class WavefrontOBJ:
def __init__(self):
self.mtllib = None
self.vertex = []
self.texture = []
self.normal = []
self.geometry = []
def load(self, fname):
if fname is None: return
geometry = Geometry()
if not os.path.exists(fname):
print(f"obj file not found: {fname}")
return
self.mtllib = fname
with open(fname) as file_in:
for line in file_in:
line = line.strip()
if not line: continue
words = line.split()
command = words[0]
data = words[1:]
if command == 'mtllib': # Material library
path = os.path.split(fname)[0]
self.mtllib = os.path.join(path, data[0])
elif command == 'usemtl': # Use material
if geometry.material != None:
self.geometry.append(geometry)
geometry = Geometry()
geometry.material = data[0]
elif command == 'v': # Vertex
x, y, z = map(float, data[:3])
self.vertex.append(vector([x, y, z]))
elif command == 'vt': # Texture
x, y = map(float, data[:2])
self.texture.append(vector([x, y, 0]))
elif command == 'vn': # Normal
x, y, z = map(float, data[:3])
self.normal.append(vector([x, y, z]))
elif command == 'p': # Point
indices = [index(item, self.vertex) for item in data]
geometry.point.append(indices)
elif command == 'l': # Line
indices = [index(item, self.vertex) for item in data]
if len(indices) == 2:
geometry.line.append(indices)
elif command == 'f': # Face
face = Face()
for group in data:
indices = group.split('/')
if len(indices) == 0:
continue
if len(indices) > 0 and indices[0]:
face.vertex.append(index(indices[0], self.vertex))
if len(indices) > 1 and indices[1]:
face.texture.append(index(indices[1], self.texture))
if len(indices) > 2 and indices[2]:
face.normal.append(index(indices[2], self.normal))
geometry.face.append(face)
self.geometry.append(geometry)
def aabb(self): # axis-aligned bounding box
zero = np.array([0.0, 0.0, 0.0])
if not self.vertex:
return zero, zero
max = sys.float_info.max
start = np.array([max, max, max])
min_coord = +start
max_coord = -start
for geometry in self.geometry:
for face in geometry.face:
for i in face.vertex:
vertex = self.vertex[i]
min_coord = np.minimum(min_coord, vertex)
max_coord = np.maximum(max_coord, vertex)
for line in geometry.line:
for i in line:
vertex = self.vertex[i]
min_coord = np.minimum(min_coord, vertex)
max_coord = np.maximum(max_coord, vertex)
for point in geometry.point:
for i in point:
vertex = self.vertex[i]
min_coord = np.minimum(min_coord, vertex)
max_coord = np.maximum(max_coord, vertex)
if np.all(min_coord == start) or np.all(max_coord == start):
return zero, zero
# Calculate center and size
center = (max_coord + min_coord) / 2
size = max_coord - min_coord
return center, size
def translate(self, translation):
if translation is None: return
if not self.vertex: return
self.vertex += translation
def geometries(self):
geometries = {}
for geometry in self.geometry:
if geometry.material not in geometries:
geometries[geometry.material] = Geometry()
geometries[geometry.material].material = geometry.material
geometries[geometry.material].face.extend(geometry.face)
geometries[geometry.material].point.extend(geometry.point)
geometries[geometry.material].line.extend(geometry.line)
return geometries