forked from tinyobjloader/tinyobjloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
73 lines (55 loc) · 1.94 KB
/
sample.py
File metadata and controls
73 lines (55 loc) · 1.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
import sys
import tinyobjloader
filename = "../models/cornell_box.obj";
reader = tinyobjloader.ObjReader()
# Load .obj(and .mtl) using default configuration
ret = reader.ParseFromFile(filename)
# Optionally you can set custom `config`
# config = tinyobj.ObjReaderConfig()
# config.triangulate = False
# ret = reader.ParseFromFile(filename, config)
if ret == False:
print("Failed to load : ", filename)
print("Warn:", reader.Warning())
print("Err:", reader.Error())
sys.exit(-1)
if reader.Warning():
print("Warn:", reader.Warning())
attrib = reader.GetAttrib()
print("attrib.vertices = ", len(attrib.vertices))
print("attrib.normals = ", len(attrib.normals))
print("attrib.texcoords = ", len(attrib.texcoords))
# vertex data must be `xyzxyzxyz...`
assert len(attrib.vertices) % 3 == 0
# normal data must be `xyzxyzxyz...`
assert len(attrib.normals) % 3 == 0
# texcoords data must be `uvuvuv...`
assert len(attrib.texcoords) % 2 == 0
for (i, v) in enumerate(attrib.vertices):
print("v[{}] = {}".format(i, v))
for (i, v) in enumerate(attrib.normals):
print("vn[{}] = {}".format(i, v))
for (i, v) in enumerate(attrib.texcoords):
print("vt[{}] = {}".format(i, t))
materials = reader.GetMaterials()
print("Num materials: ", len(materials))
for m in materials:
print(m.name)
print(m.diffuse)
print(m.diffuse_texname)
# Partial update(array indexing) does not work
# m.diffuse[1] = 1.0
# Update with full object assignment works
m.diffuse = [1, 2, 3]
print(m.diffuse)
#print(m.shininess)
#print(m.illum)
shapes = reader.GetShapes()
print("Num shapes: ", len(shapes))
for shape in shapes:
print(shape.name)
print("num_indices = {}".format(len(shape.mesh.indices)))
for (i, idx) in enumerate(shape.mesh.indices):
print("[{}] v_idx {}".format(i, idx.vertex_index))
print("[{}] vn_idx {}".format(i, idx.normal_index))
print("[{}] vt_idx {}".format(i, idx.texcoord_index))