Skip to content

Commit 80b2430

Browse files
author
root
committed
base of python module
1 parent b214cfb commit 80b2430

3 files changed

Lines changed: 245 additions & 0 deletions

File tree

python/howto.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import tinyobjloader as tol
2+
3+
model = tol.LoadObj("cube.obj")
4+
5+
print(model["shapes"], model["materials"])

python/main.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//python3 module for tinyobjloader
2+
//
3+
//usage:
4+
// import tinyobjloader as tol
5+
// model = tol.LoadObj(name)
6+
// print(model["shapes"])
7+
// print(model["materials"]
8+
#include <Python.h>
9+
#include <vector>
10+
#include "tiny_obj_loader.h"
11+
12+
typedef std::vector<double> vectd;
13+
14+
PyObject*
15+
pyTupleFromfloat3 (float array[3])
16+
{
17+
int i;
18+
PyObject* tuple = PyTuple_New(3);
19+
20+
for(i=0; i<=2 ; i++){
21+
PyTuple_SetItem(tuple, i, PyFloat_FromDouble(array[i]));
22+
}
23+
24+
return tuple;
25+
}
26+
27+
extern "C"
28+
{
29+
30+
static PyObject*
31+
pyLoadObj(PyObject* self, PyObject* args)
32+
{
33+
PyObject *rtntpl, *pyshapes, *pymaterials;
34+
char const* filename;
35+
std::vector<tinyobj::shape_t> shapes;
36+
std::vector<tinyobj::material_t> materials;
37+
38+
if(!PyArg_ParseTuple(args, "s", &filename))
39+
return NULL;
40+
41+
tinyobj::LoadObj(shapes, materials, filename);
42+
43+
pyshapes = PyDict_New();
44+
pymaterials = PyDict_New();
45+
rtntpl = PyDict_New();
46+
47+
for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin() ;
48+
shape != shapes.end(); shape++)
49+
{
50+
PyObject *meshobj;
51+
PyObject *positions, *normals, *texcoords, *indices, *material_ids;
52+
53+
meshobj = PyTuple_New(5);
54+
positions = PyList_New(0);
55+
normals = PyList_New(0);
56+
texcoords = PyList_New(0);
57+
indices = PyList_New(0);
58+
material_ids = PyList_New(0);
59+
60+
tinyobj::mesh_t cm = (*shape).mesh;
61+
for (int i = 0; i <= 4 ; i++ )
62+
{
63+
PyObject *current;
64+
vectd vect;
65+
66+
switch (i)
67+
{
68+
case 0: current = positions;
69+
vect = vectd(cm.positions.begin(), cm.positions.end());
70+
case 1: current = normals;
71+
vect = vectd(cm.normals.begin(), cm.normals.end());
72+
case 2: current = texcoords;
73+
vect = vectd(cm.texcoords.begin(), cm.texcoords.end());
74+
case 3: current = indices;
75+
vect = vectd(cm.indices.begin(), cm.indices.end());
76+
case 4: current = material_ids;
77+
vect = vectd(cm.material_ids.begin(), cm.material_ids.end());
78+
}
79+
80+
for (std::vector<double>::iterator it = vect.begin() ;
81+
it != vect.end(); it++)
82+
{
83+
PyList_Insert(current, it - vect.begin(), PyFloat_FromDouble(*it));
84+
}
85+
86+
PyTuple_SetItem(meshobj, i, current);
87+
}
88+
89+
PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj);
90+
}
91+
92+
for (std::vector<tinyobj::material_t>::iterator mat = materials.begin() ;
93+
mat != materials.end(); mat++)
94+
{
95+
PyObject *matobj = PyDict_New();
96+
97+
PyDict_SetItemString(matobj, "shininess", PyFloat_FromDouble((*mat).shininess));
98+
PyDict_SetItemString(matobj, "ior", PyFloat_FromDouble((*mat).ior));
99+
PyDict_SetItemString(matobj, "dissolve", PyFloat_FromDouble((*mat).dissolve));
100+
PyDict_SetItemString(matobj, "illum", PyLong_FromLong((*mat).illum));
101+
PyDict_SetItemString(matobj, "ambient_texname", PyUnicode_FromString((*mat).ambient_texname.c_str()));
102+
PyDict_SetItemString(matobj, "diffuse_texname", PyUnicode_FromString((*mat).diffuse_texname.c_str()));
103+
PyDict_SetItemString(matobj, "specular_texname", PyUnicode_FromString((*mat).specular_texname.c_str()));
104+
PyDict_SetItemString(matobj, "normal_texname", PyUnicode_FromString((*mat).normal_texname.c_str()));
105+
PyDict_SetItemString(matobj, "ambient", pyTupleFromfloat3((*mat).ambient));
106+
PyDict_SetItemString(matobj, "diffuse", pyTupleFromfloat3((*mat).diffuse));
107+
PyDict_SetItemString(matobj, "specular", pyTupleFromfloat3((*mat).specular));
108+
PyDict_SetItemString(matobj, "transmittance", pyTupleFromfloat3((*mat).transmittance));
109+
PyDict_SetItemString(matobj, "emission", pyTupleFromfloat3((*mat).emission));
110+
111+
PyDict_SetItemString(pymaterials, (*mat).name.c_str(), matobj);
112+
}
113+
114+
PyDict_SetItemString(rtntpl, "shapes", pyshapes);
115+
PyDict_SetItemString(rtntpl, "materials", pymaterials);
116+
117+
return rtntpl;
118+
}
119+
120+
121+
static PyMethodDef mMethods[] = {
122+
123+
{"LoadObj", pyLoadObj, METH_VARARGS},
124+
{NULL, NULL, 0, NULL}
125+
126+
};
127+
128+
129+
static struct PyModuleDef moduledef = {
130+
PyModuleDef_HEAD_INIT,
131+
"tinyobjloader",
132+
NULL,
133+
-1,
134+
mMethods
135+
};
136+
137+
138+
PyMODINIT_FUNC
139+
PyInit_tinyobjloader(void)
140+
{
141+
return PyModule_Create(&moduledef);
142+
}
143+
144+
}

python/pyTOL.cbp.mak

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#------------------------------------------------------------------------------#
2+
# This makefile was generated by 'cbp2make' tool rev.147 #
3+
#------------------------------------------------------------------------------#
4+
#requirements : Python 3 (- dev)
5+
6+
WORKDIR = `pwd`
7+
8+
CC = gcc
9+
CXX = g++
10+
AR = ar
11+
LD = g++
12+
WINDRES = windres
13+
14+
INC =
15+
CFLAGS = -Wall -fexceptions `python3-config --cflags`
16+
RESINC =
17+
LIBDIR =
18+
LIB =
19+
LDFLAGS = `python3-config --ldflags`
20+
21+
INC_DEBUG = $(INC)
22+
CFLAGS_DEBUG = $(CFLAGS) -g
23+
RESINC_DEBUG = $(RESINC)
24+
RCFLAGS_DEBUG = $(RCFLAGS)
25+
LIBDIR_DEBUG = $(LIBDIR)
26+
LIB_DEBUG = $(LIB)
27+
LDFLAGS_DEBUG = $(LDFLAGS)
28+
OBJDIR_DEBUG = obj/Debug
29+
DEP_DEBUG =
30+
OUT_DEBUG = bin/Debug/tinyobjloader.so
31+
32+
INC_RELEASE = $(INC)
33+
CFLAGS_RELEASE = $(CFLAGS) -O2
34+
RESINC_RELEASE = $(RESINC)
35+
RCFLAGS_RELEASE = $(RCFLAGS)
36+
LIBDIR_RELEASE = $(LIBDIR)
37+
LIB_RELEASE = $(LIB)
38+
LDFLAGS_RELEASE = $(LDFLAGS) -s
39+
OBJDIR_RELEASE = obj/Release
40+
DEP_RELEASE =
41+
OUT_RELEASE = bin/Release/tinyobjloader.so
42+
43+
OBJ_DEBUG = $(OBJDIR_DEBUG)/main.o $(OBJDIR_DEBUG)/tiny_obj_loader.o
44+
45+
OBJ_RELEASE = $(OBJDIR_RELEASE)/main.o $(OBJDIR_RELEASE)/tiny_obj_loader.o
46+
47+
all: debug release
48+
49+
clean: clean_debug clean_release
50+
51+
before_debug:
52+
test -d bin/Debug || mkdir -p bin/Debug
53+
test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG)
54+
55+
after_debug:
56+
57+
debug: before_debug out_debug after_debug
58+
59+
out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG)
60+
$(LD) -shared $(LIBDIR_DEBUG) $(OBJ_DEBUG) -o $(OUT_DEBUG) $(LDFLAGS_DEBUG) $(LIB_DEBUG)
61+
62+
$(OBJDIR_DEBUG)/main.o: main.cpp
63+
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c main.cpp -o $(OBJDIR_DEBUG)/main.o
64+
65+
$(OBJDIR_DEBUG)/tiny_obj_loader.o: tiny_obj_loader.cc
66+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c tiny_obj_loader.cc -o $(OBJDIR_DEBUG)/tiny_obj_loader.o
67+
68+
clean_debug:
69+
rm -f $(OBJ_DEBUG) $(OUT_DEBUG)
70+
rm -rf bin/Debug
71+
rm -rf $(OBJDIR_DEBUG)
72+
73+
before_release:
74+
test -d bin/Release || mkdir -p bin/Release
75+
test -d $(OBJDIR_RELEASE) || mkdir -p $(OBJDIR_RELEASE)
76+
77+
after_release:
78+
79+
release: before_release out_release after_release
80+
81+
out_release: before_release $(OBJ_RELEASE) $(DEP_RELEASE)
82+
$(LD) -shared $(LIBDIR_RELEASE) $(OBJ_RELEASE) -o $(OUT_RELEASE) $(LDFLAGS_RELEASE) $(LIB_RELEASE)
83+
84+
$(OBJDIR_RELEASE)/main.o: main.cpp
85+
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c main.cpp -o $(OBJDIR_RELEASE)/main.o
86+
87+
$(OBJDIR_RELEASE)/tiny_obj_loader.o: tiny_obj_loader.cc
88+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c tiny_obj_loader.cc -o $(OBJDIR_RELEASE)/tiny_obj_loader.o
89+
90+
clean_release:
91+
rm -f $(OBJ_RELEASE) $(OUT_RELEASE)
92+
rm -rf bin/Release
93+
rm -rf $(OBJDIR_RELEASE)
94+
95+
.PHONY: before_debug after_debug clean_debug before_release after_release clean_release
96+

0 commit comments

Comments
 (0)