Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf39874

Browse files
committed
Add note on developer build of python binding.
Add comment on `numpy_indices` of python binding. Fixes tinyobjloader#242
1 parent ba75951 commit bf39874

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

python/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ Please take a look at `python/sample.py` file in tinyobjloader git repo.
5252

5353
https://github.com/syoyo/tinyobjloader/blob/master/python/sample.py
5454

55+
## How to build
56+
57+
Using `cibuildwheel` is an recommended way to build a python module.
58+
See $tinyobjloader/azure-pipelines.yml for details.
59+
60+
### Developer build
61+
62+
Edit `setup.py` and uncomment `Developer option` lines
63+
64+
Assume pip is installed.
65+
66+
```
67+
$ pip install pybind11
68+
$ python setup.py build
69+
```
70+
5571
## License
5672

5773
MIT license.

python/bindings.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ PYBIND11_MODULE(tinyobjloader, tobj_module)
130130
})
131131
.def_readonly("indices", &mesh_t::indices)
132132
.def("numpy_indices", [] (mesh_t &instance) {
133+
// Flatten indexes. index_t is composed of 3 ints(vertex_index, normal_index, texcoord_index).
134+
// numpy_indices = [0, -1, -1, 1, -1, -1, ...]
135+
// C++11 or later should pack POD struct tightly and does not reorder variables,
136+
// so we can memcpy to copy data.
137+
// Still, we check the size of struct and byte offsets of each variable just for sure.
138+
static_assert(sizeof(index_t) == 12, "sizeof(index_t) must be 12");
139+
static_assert(offsetof(index_t, vertex_index) == 0, "offsetof(index_t, vertex_index) must be 0");
140+
static_assert(offsetof(index_t, normal_index) == 4, "offsetof(index_t, normal_index) must be 4");
141+
static_assert(offsetof(index_t, texcoord_index) == 8, "offsetof(index_t, texcoord_index) must be 8");
142+
133143
auto ret = py::array_t<int>(instance.indices.size() * 3);
134144
py::buffer_info buf = ret.request();
135145
memcpy(buf.ptr, instance.indices.data(), instance.indices.size() * 3 * sizeof(int));

python/sample.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
for (i, v) in enumerate(attrib.texcoords):
4747
print("vt[{}] = {}".format(i, t))
4848

49+
print("numpy_vertices = {}".format(attrib.numpy_vertices()))
50+
4951
materials = reader.GetMaterials()
5052
print("Num materials: ", len(materials))
5153
for m in materials:
@@ -66,8 +68,10 @@
6668
print("Num shapes: ", len(shapes))
6769
for shape in shapes:
6870
print(shape.name)
69-
print("num_indices = {}".format(len(shape.mesh.indices)))
71+
print("len(num_indices) = {}".format(len(shape.mesh.indices)))
7072
for (i, idx) in enumerate(shape.mesh.indices):
7173
print("[{}] v_idx {}".format(i, idx.vertex_index))
7274
print("[{}] vn_idx {}".format(i, idx.normal_index))
7375
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()))

python/setup.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import setuptools
2+
import platform
23

34
with open("README.md", "r") as fh:
45
long_description = fh.read()
@@ -44,10 +45,24 @@ def __str__(self):
4445
return interpreter_include_path
4546

4647

48+
ext_compile_args = ["-std=c++11"]
49+
ext_link_args = []
50+
51+
# Developer option
52+
#
53+
#if platform.system() == "Darwin":
54+
# # XCode10 or later does not support libstdc++, so we need to use libc++.
55+
# # macosx-version 10.6 does not support libc++, so we require min macosx version 10.9.
56+
# ext_compile_args.append("-stdlib=libc++")
57+
# ext_compile_args.append("-mmacosx-version-min=10.9")
58+
# ext_link_args.append("-stdlib=libc++")
59+
# ext_link_args.append("-mmacosx-version-min=10.9")
60+
4761
# `tiny_obj_loader.cc` contains implementation of tiny_obj_loader.
4862
m = setuptools.Extension(
4963
"tinyobjloader",
50-
extra_compile_args=["-std=c++11"],
64+
extra_compile_args=ext_compile_args,
65+
extra_link_args=ext_link_args,
5166
sources=["bindings.cc", "tiny_obj_loader.cc"],
5267
include_dirs=[
5368
# Support `build_ext` finding tinyobjloader (without first running

0 commit comments

Comments
 (0)