/
test_obj.py
179 lines (146 loc) · 6.38 KB
/
test_obj.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
167
168
169
170
171
172
173
174
175
176
177
178
179
try:
from . import generic as g
except BaseException:
import generic as g
class OBJTest(g.unittest.TestCase):
def test_rabbit(self):
# A BSD-licensed test model from pyglet
# it has mixed triangles, quads, and 16 element faces -_-
# this should test the non-vectorized load path
m = g.get_mesh('rabbit.obj')
assert len(m.faces) == 1252
def test_obj_groups(self):
# a wavefront file with groups defined
mesh = g.get_mesh('groups.obj')
# make sure some data got loaded
assert g.trimesh.util.is_shape(mesh.faces, (-1, 3))
assert g.trimesh.util.is_shape(mesh.vertices, (-1, 3))
# make sure groups are the right length
# TODO: we do not support face groups now
# assert len(mesh.metadata['face_groups']) == len(mesh.faces)
# check to make sure there is signal not just zeros
# assert mesh.metadata['face_groups'].ptp() > 0
def test_obj_quad(self):
mesh = g.get_mesh('quadknot.obj')
# make sure some data got loaded
assert g.trimesh.util.is_shape(mesh.faces, (-1, 3))
assert g.trimesh.util.is_shape(mesh.vertices, (-1, 3))
assert mesh.is_watertight
assert mesh.is_winding_consistent
def test_obj_multiobj(self):
# test a wavefront file with multiple objects in the same file
scene = g.get_mesh('two_objects.obj',
split_object=True,
group_material=False)
assert len(scene.geometry) == 2
for mesh in scene.geometry.values():
# make sure some data got loaded
assert g.trimesh.util.is_shape(mesh.faces, (-1, 3))
assert g.trimesh.util.is_shape(mesh.vertices, (-1, 3))
assert mesh.is_watertight
assert mesh.is_winding_consistent
def test_obj_split_attributes(self):
# test a wavefront file where pos/uv/norm have different indices
# and where multiple objects share vertices
# Note 'process=False' to avoid merging vertices
scene = g.get_mesh('joined_tetrahedra.obj',
process=False,
split_object=True,
group_material=False)
assert len(scene.geometry) == 2
geom = list(scene.geometry.values())
assert g.trimesh.util.is_shape(geom[0].faces, (4, 3))
assert g.trimesh.util.is_shape(geom[0].vertices, (9, 3))
assert g.trimesh.util.is_shape(geom[1].faces, (4, 3))
assert g.trimesh.util.is_shape(geom[1].vertices, (9, 3))
def test_obj_simple_order(self):
# test a simple wavefront model without split indexes
# and make sure we don't reorder vertices unnecessarily
file_name = g.os.path.join(g.dir_models,
'cube.OBJ')
# load a simple OBJ file without merging vertices
m = g.trimesh.load(file_name, process=False)
# we're going to load faces in a basic text way
# and compare the order from this method to the
# trimesh loader, to see if we get the same thing
faces = []
verts = []
with open(file_name, 'r') as f:
for line in f:
line = line.strip()
if line[0] == 'f':
faces.append(line[1:].strip().split())
if line[0] == 'v':
verts.append(line[1:].strip().split())
# get faces as basic numpy array
faces = g.np.array(faces, dtype=g.np.int64) - 1
verts = g.np.array(verts, dtype=g.np.float64)
# trimesh loader should return the same face order
assert g.np.allclose(faces, m.faces)
assert g.np.allclose(verts, m.vertices)
def test_obj_compressed(self):
mesh = g.get_mesh('cube_compressed.obj', process=False)
assert g.np.allclose(g.np.abs(mesh.vertex_normals).sum(axis=1),
1.0)
def test_vertex_color(self):
# get a box mesh
mesh = g.trimesh.creation.box()
# set each vertex to a unique random color
mesh.visual.vertex_colors = [g.trimesh.visual.random_color()
for _ in range(len(mesh.vertices))]
# export and then reload the file as OBJ
rec = g.trimesh.load(
g.trimesh.util.wrap_as_stream(
mesh.export(file_type='obj')),
file_type='obj')
# assert colors have survived the export cycle
assert (mesh.visual.vertex_colors ==
rec.visual.vertex_colors).all()
def test_single_vn(self):
"""
Make sure files with a single VN load.
"""
m = g.get_mesh('singlevn.obj')
assert len(m.vertices) > 0
assert len(m.faces) > 0
def test_polygon_faces(self):
m = g.get_mesh('polygonfaces.obj')
assert len(m.vertices) > 0
assert len(m.faces) > 0
def test_faces_not_enough_indices(self):
m = g.get_mesh('notenoughindices.obj')
assert len(m.vertices) > 0
assert len(m.faces) == 1
def test_mtl(self):
# get a mesh with texture
m = g.get_mesh('fuze.obj')
# export the mesh including data
obj, data = g.trimesh.exchange.export.export_obj(
m, include_texture=True)
with g.trimesh.util.TemporaryDirectory() as path:
# where is the OBJ file going to be saved
obj_path = g.os.path.join(path, 'test.obj')
with open(obj_path, 'w') as f:
f.write(obj)
# save the MTL and images
for k, v in data.items():
with open(g.os.path.join(path, k), 'wb') as f:
f.write(v)
# reload the mesh from the export
rec = g.trimesh.load(obj_path)
# make sure loaded image is the same size as the original
assert (rec.visual.material.image.size ==
m.visual.material.image.size)
# make sure the faces are the same size
assert rec.faces.shape == m.faces.shape
def test_scene(self):
s = g.get_mesh('cycloidal.3DXML')
e = g.trimesh.load(
g.io_wrap(s.export(file_type='obj')),
file_type='obj',
split_object=True,
group_materials=False)
assert g.np.isclose(e.area, s.area, rtol=.01)
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()