Skip to content

Commit f1f0ca0

Browse files
committed
Bug: Incorrect handling of negative indices
1 parent 22dd1e8 commit f1f0ca0

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

pywavefront/obj.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ def consume_faces(self):
217217
has_vn = True
218218

219219
# Are we referencing vertex with color info?
220-
vertex = self.vertices[int(parts[0]) - 1]
220+
vindex = int(parts[0])
221+
if vindex < 0:
222+
vindex += len(self.vertices) + 1
223+
224+
vertex = self.vertices[vindex]
221225
has_colors = len(vertex) == 6
222226

223227
# Prepare vertex format string
@@ -253,13 +257,13 @@ def consume_faces(self):
253257

254258
# Resolve negative index lookups
255259
if v_index < 0:
256-
v_index += len(self.vertices) - 1
260+
v_index += len(self.vertices) + 1
257261

258262
if has_vt and t_index < 0:
259-
t_index += len(self.tex_coords) - 1
263+
t_index += len(self.tex_coords) + 1
260264

261265
if has_vn and n_index < 0:
262-
n_index += len(self.normals) - 1
266+
n_index += len(self.normals) + 1
263267

264268
pos = self.vertices[v_index][0:3] if has_colors else self.vertices[v_index]
265269
color = self.vertices[v_index][3:] if has_colors else ()

test/simple_negative_indices.obj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Comment
2+
mtllib simple.mtl
3+
o Simple
4+
v 0.01 0.02 0.03
5+
v 0.04 0.05 0.06
6+
v 0.07 0.08 0.09
7+
v 0.11 0.12 0.13
8+
vt 10 11
9+
vt 12 13
10+
vt 14 15
11+
vt 16 17
12+
vn 20 21 22
13+
usemtl Material.simple
14+
f -3/-2/-1 -4/-3/-1 -2/-4/-1
15+
o SimpleB
16+
v 1.0 0.0 1.0
17+
v -1.0 0.0 1.0
18+
v 1.0 0.0 -1.0
19+
v -1.0 0.0 -1.0
20+
vt 0.0 1.0
21+
vt 0.0 0.0
22+
vt 1.0 0.0
23+
vt 1.0 1.0
24+
vn 0.0 1.0 -0.0
25+
usemtl Material2.simple
26+
f -3/-2/-1 -4/-3/-1 -2/-4/-1

test/test_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def testObjMaterials(self):
4444
self.assertEqual(material1.name, 'Material.simple')
4545
self.assertEqual(material2.name, 'Material2.simple')
4646

47+
class NegativeIndices(TestParsers):
48+
"""Run all tests with negative indices"""
49+
def setUp(self):
50+
# Append current path to locate files
51+
meshes = pywavefront.Wavefront(prepend_dir('simple_negative_indices.obj'))
52+
self.mesh1 = meshes.mesh_list[0]
53+
self.mesh2 = meshes.mesh_list[1]
54+
4755

4856
class TestParserGz(TestParsers):
4957
"""Run all tests is TestParsers for gzip file as well"""

0 commit comments

Comments
 (0)