forked from tinyobjloader/tinyobjloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_v3.py
More file actions
169 lines (138 loc) · 3.94 KB
/
Copy pathsample_v3.py
File metadata and controls
169 lines (138 loc) · 3.94 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
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
#!/usr/bin/env python3
"""
Sample script demonstrating TinyObjLoader v3 Python bindings
"""
import tinyobjloader_v3 as tobj
def test_parse_simple_obj():
"""Test parsing a simple OBJ string"""
# Simple cube OBJ data
obj_data = """
# Simple cube
v -1.0 -1.0 1.0
v 1.0 -1.0 1.0
v 1.0 1.0 1.0
v -1.0 1.0 1.0
v -1.0 -1.0 -1.0
v 1.0 -1.0 -1.0
v 1.0 1.0 -1.0
v -1.0 1.0 -1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
# Front face
f 1//1 2//1 3//1
f 1//1 3//1 4//1
# Back face
f 5//2 7//2 6//2
f 5//2 8//2 7//2
# Top face
f 4//3 3//3 7//3
f 4//3 7//3 8//3
# Bottom face
f 1//4 6//4 2//4
f 1//4 5//4 6//4
# Right face
f 2//5 6//5 7//5
f 2//5 7//5 3//5
# Left face
f 1//6 4//6 8//6
f 1//6 8//6 5//6
"""
# Create parser with default config
parser = tobj.ObjParser()
# Parse from string
result = parser.parse_from_string(obj_data)
# Check if parsing was successful
if result.success():
print("✓ Parsing successful!")
else:
print("✗ Parsing failed!")
print(f"Errors: {result.errors}")
return False
# Get statistics
stats = result.stats
print(f"\nParsing Statistics:")
print(f" Vertices parsed: {stats['vertices_parsed']}")
print(f" Normals parsed: {stats['normals_parsed']}")
print(f" Faces parsed: {stats['faces_parsed']}")
print(f" Triangles generated: {stats['triangles_generated']}")
print(f" Parse time: {stats['parse_time_seconds']:.6f} seconds")
# Get attributes
attrib = result.attributes
vertices = attrib.vertices
normals = attrib.normals
print(f"\nAttribute Data:")
print(f" Total vertices: {len(vertices) // 3}")
print(f" Total normals: {len(normals) // 3}")
# Print first few vertices
print(f"\nFirst 3 vertices:")
for i in range(min(3, len(vertices) // 3)):
x, y, z = vertices[i*3], vertices[i*3+1], vertices[i*3+2]
print(f" v{i}: ({x:.2f}, {y:.2f}, {z:.2f})")
# Get shapes
shapes = result.shapes
print(f"\nShapes: {len(shapes)}")
for i, shape in enumerate(shapes):
print(f" Shape {i}: '{shape['name']}'")
mesh = shape['mesh']
num_faces = len(mesh['num_face_vertices'])
num_indices = len(mesh['indices'])
print(f" Faces: {num_faces}")
print(f" Indices: {num_indices}")
# Print first face
if num_faces > 0:
nfv = mesh['num_face_vertices'][0]
print(f" First face ({nfv} vertices):")
for j in range(nfv):
idx = mesh['indices'][j]
print(f" v{idx[0]}/vt{idx[2]}/vn{idx[1]}")
return True
def test_with_custom_config():
"""Test parsing with custom configuration"""
obj_data = """
v 0 0 0
v 1 0 0
v 1 1 0
v 0 1 0
f 1 2 3 4
"""
# Create custom config
config = tobj.ParserConfig()
config.triangulate = True
# Create parser with config
parser = tobj.ObjParser(config)
result = parser.parse_from_string(obj_data)
if result.success():
print("\n✓ Custom config parsing successful!")
stats = result.stats
print(f" Faces parsed: {stats['faces_parsed']}")
print(f" Triangles generated: {stats['triangles_generated']}")
return True
else:
print("\n✗ Custom config parsing failed!")
return False
def main():
print("TinyObjLoader v3 Python Bindings Test")
print("=" * 50)
print(f"\nModule version: {tobj.__version__}")
# Test 1: Simple OBJ parsing
print("\n" + "=" * 50)
print("Test 1: Simple OBJ parsing")
print("=" * 50)
if not test_parse_simple_obj():
return 1
# Test 2: Custom configuration
print("\n" + "=" * 50)
print("Test 2: Custom configuration")
print("=" * 50)
if not test_with_custom_config():
return 1
print("\n" + "=" * 50)
print("All tests passed! ✓")
print("=" * 50)
return 0
if __name__ == "__main__":
exit(main())