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

Skip to content

Commit 8b3d243

Browse files
committed
Implement some 2.0 API.
Write some python bindings using pybind11.
1 parent 7337053 commit 8b3d243

File tree

4 files changed

+245
-62
lines changed

4 files changed

+245
-62
lines changed

python/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
python setup.py build

python/bindings.cc

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ namespace py = pybind11;
88

99
using namespace tinyobj;
1010

11-
// Simple wrapper for LoadObj.
12-
// TODO(syoyo): Make it more pythonic.
13-
bool load_obj(attrib_t *attrib, std::vector<shape_t> *shapes,
14-
std::vector<material_t> *materials,
15-
std::string *warn, std::string *err,
16-
const std::string &filename,
17-
const std::string &mtl_basedir = "./",
18-
bool triangulate = true,
19-
bool default_vcols_fallback = true)
20-
{
21-
return LoadObj(attrib, shapes, materials, warn, err, filename.c_str(), mtl_basedir.c_str(), triangulate, default_vcols_fallback);
22-
}
23-
2411
// Simple wrapper for LoadMtl.
2512
// TODO(syoyo): Make it more pythonic.
2613
void load_mtl(std::map<std::string, int> *material_map,
@@ -36,14 +23,28 @@ PYBIND11_MODULE(tinyobjloader, tobj_module)
3623

3724
// register struct
3825
// py::init<>() for default constructor
39-
py::class_<attrib_t>(tobj_module, "Attrib")
26+
py::class_<ObjLoader>(tobj_module, "ObjLoader")
27+
.def(py::init<>())
28+
.def("Load", &ObjLoader::Load)
29+
.def("Valid", &ObjLoader::Valid)
30+
.def("GetAttrib", &ObjLoader::GetAttrib)
31+
.def("GetShapes", &ObjLoader::GetShapes)
32+
.def("GetMaterials", &ObjLoader::GetMaterials)
33+
.def("Warning", &ObjLoader::Warning)
34+
.def("Error", &ObjLoader::Error);
35+
36+
py::class_<attrib_t>(tobj_module, "attrib_t")
37+
.def(py::init<>())
38+
.def_property_readonly("vertices", &attrib_t::GetVertices);
39+
40+
py::class_<shape_t>(tobj_module, "shape_t")
4041
.def(py::init<>());
41-
py::class_<shape_t>(tobj_module, "Shape")
42+
43+
py::class_<material_t>(tobj_module, "material_t")
4244
.def(py::init<>());
43-
py::class_<material_t>(tobj_module, "Material")
45+
46+
py::class_<ObjLoaderConfig>(tobj_module, "ObjLoaderConfig")
4447
.def(py::init<>());
4548

46-
tobj_module.def("load_obj", &load_obj, "Load wavefront .obj file.");
47-
tobj_module.def("load_mtl", &load_mtl, "Load wavefront material file.");
4849
}
4950

python/sample.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
import tinyobjloader
3+
4+
filename = "../models/cornell_box.obj";
5+
6+
config = tinyobjloader.ObjLoaderConfig()
7+
8+
loader = tinyobjloader.ObjLoader()
9+
10+
ret = loader.Load(filename, config)
11+
12+
if ret == False:
13+
print("Failed to load : ", filename)
14+
sys.exit(-1)
15+
16+
attrib = loader.GetAttrib()
17+
print("attrib.vertices = ", len(attrib.vertices))
18+
for v in attrib.vertices:
19+
print(v)

0 commit comments

Comments
 (0)