|
1 | 1 | import sys
|
2 | 2 | import tinyobjloader
|
3 | 3 |
|
| 4 | +is_numpy_available = False |
| 5 | +try: |
| 6 | + import numpy |
| 7 | + is_numpy_available = True |
| 8 | +except: |
| 9 | + print("NumPy not installed. Do not use numpy_*** API. If you encounter slow performance, see a performance tips for non-numpy API https://github.com/tinyobjloader/tinyobjloader/issues/275") |
| 10 | + |
4 | 11 | filename = "../models/cornell_box.obj"
|
5 | 12 |
|
6 | 13 |
|
|
24 | 31 | print("Warn:", reader.Warning())
|
25 | 32 |
|
26 | 33 | attrib = reader.GetAttrib()
|
27 |
| -print("attrib.vertices = ", len(attrib.vertices)) |
28 |
| -print("attrib.normals = ", len(attrib.normals)) |
29 |
| -print("attrib.texcoords = ", len(attrib.texcoords)) |
| 34 | +print("len(attrib.vertices) = ", len(attrib.vertices)) |
| 35 | +print("len(attrib.normals) = ", len(attrib.normals)) |
| 36 | +print("len(attrib.texcoords) = ", len(attrib.texcoords)) |
30 | 37 |
|
31 | 38 | # vertex data must be `xyzxyzxyz...`
|
32 | 39 | assert len(attrib.vertices) % 3 == 0
|
|
37 | 44 | # texcoords data must be `uvuvuv...`
|
38 | 45 | assert len(attrib.texcoords) % 2 == 0
|
39 | 46 |
|
| 47 | +# Performance note |
| 48 | +# (direct?) array access through member variable is quite slow. |
| 49 | +# https://github.com/tinyobjloader/tinyobjloader/issues/275#issuecomment-753465833 |
| 50 | +# |
| 51 | +# We encourage first copy(?) varible to Python world: |
| 52 | +# |
| 53 | +# vertices = attrib.vertices |
| 54 | +# |
| 55 | +# for i in range(...) |
| 56 | +# v = vertices[i] |
| 57 | +# |
| 58 | +# Or please consider using numpy_*** interface(e.g. numpy_vertices()) |
| 59 | + |
40 | 60 | for (i, v) in enumerate(attrib.vertices):
|
41 | 61 | print("v[{}] = {}".format(i, v))
|
42 | 62 |
|
|
46 | 66 | for (i, v) in enumerate(attrib.texcoords):
|
47 | 67 | print("vt[{}] = {}".format(i, t))
|
48 | 68 |
|
49 |
| -print("numpy_vertices = {}".format(attrib.numpy_vertices())) |
| 69 | +if is_numpy_available: |
| 70 | + print("numpy_v = {}".format(attrib.numpy_vertices())) |
| 71 | + print("numpy_vn = {}".format(attrib.numpy_normals())) |
| 72 | + print("numpy_vt = {}".format(attrib.numpy_texcoords())) |
50 | 73 |
|
51 | 74 | materials = reader.GetMaterials()
|
52 | 75 | print("Num materials: ", len(materials))
|
|
73 | 96 | print("[{}] v_idx {}".format(i, idx.vertex_index))
|
74 | 97 | print("[{}] vn_idx {}".format(i, idx.normal_index))
|
75 | 98 | print("[{}] vt_idx {}".format(i, idx.texcoord_index))
|
76 |
| - print("numpy_indices = {}".format(shape.mesh.numpy_indices())) |
77 |
| - print("numpy_num_face_vertices = {}".format(shape.mesh.numpy_num_face_vertices())) |
78 |
| - print("numpy_material_ids = {}".format(shape.mesh.numpy_material_ids())) |
| 99 | + print("material_ids = {}".format(shape.mesh.material_ids)) |
| 100 | + |
| 101 | + if is_numpy_available: |
| 102 | + print("numpy_indices = {}".format(shape.mesh.numpy_indices())) |
| 103 | + print("numpy_num_face_vertices = {}".format(shape.mesh.numpy_num_face_vertices())) |
| 104 | + print("numpy_material_ids = {}".format(shape.mesh.numpy_material_ids())) |
0 commit comments