From 80b243092b5cb3c3523030a188bd50aeaf8fd1e4 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 14 Nov 2014 14:32:20 +0100 Subject: [PATCH] base of python module --- python/howto.py | 5 ++ python/main.cpp | 144 +++++++++++++++++++++++++++++++++++++++++++ python/pyTOL.cbp.mak | 96 +++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 python/howto.py create mode 100644 python/main.cpp create mode 100644 python/pyTOL.cbp.mak diff --git a/python/howto.py b/python/howto.py new file mode 100644 index 0000000..1342dac --- /dev/null +++ b/python/howto.py @@ -0,0 +1,5 @@ +import tinyobjloader as tol + +model = tol.LoadObj("cube.obj") + +print(model["shapes"], model["materials"]) diff --git a/python/main.cpp b/python/main.cpp new file mode 100644 index 0000000..e41916c --- /dev/null +++ b/python/main.cpp @@ -0,0 +1,144 @@ +//python3 module for tinyobjloader +// +//usage: +// import tinyobjloader as tol +// model = tol.LoadObj(name) +// print(model["shapes"]) +// print(model["materials"] +#include +#include +#include "tiny_obj_loader.h" + +typedef std::vector vectd; + +PyObject* +pyTupleFromfloat3 (float array[3]) +{ + int i; + PyObject* tuple = PyTuple_New(3); + + for(i=0; i<=2 ; i++){ + PyTuple_SetItem(tuple, i, PyFloat_FromDouble(array[i])); + } + + return tuple; +} + +extern "C" +{ + +static PyObject* +pyLoadObj(PyObject* self, PyObject* args) +{ + PyObject *rtntpl, *pyshapes, *pymaterials; + char const* filename; + std::vector shapes; + std::vector materials; + + if(!PyArg_ParseTuple(args, "s", &filename)) + return NULL; + + tinyobj::LoadObj(shapes, materials, filename); + + pyshapes = PyDict_New(); + pymaterials = PyDict_New(); + rtntpl = PyDict_New(); + + for (std::vector::iterator shape = shapes.begin() ; + shape != shapes.end(); shape++) + { + PyObject *meshobj; + PyObject *positions, *normals, *texcoords, *indices, *material_ids; + + meshobj = PyTuple_New(5); + positions = PyList_New(0); + normals = PyList_New(0); + texcoords = PyList_New(0); + indices = PyList_New(0); + material_ids = PyList_New(0); + + tinyobj::mesh_t cm = (*shape).mesh; + for (int i = 0; i <= 4 ; i++ ) + { + PyObject *current; + vectd vect; + + switch (i) + { + case 0: current = positions; + vect = vectd(cm.positions.begin(), cm.positions.end()); + case 1: current = normals; + vect = vectd(cm.normals.begin(), cm.normals.end()); + case 2: current = texcoords; + vect = vectd(cm.texcoords.begin(), cm.texcoords.end()); + case 3: current = indices; + vect = vectd(cm.indices.begin(), cm.indices.end()); + case 4: current = material_ids; + vect = vectd(cm.material_ids.begin(), cm.material_ids.end()); + } + + for (std::vector::iterator it = vect.begin() ; + it != vect.end(); it++) + { + PyList_Insert(current, it - vect.begin(), PyFloat_FromDouble(*it)); + } + + PyTuple_SetItem(meshobj, i, current); + } + + PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj); + } + + for (std::vector::iterator mat = materials.begin() ; + mat != materials.end(); mat++) + { + PyObject *matobj = PyDict_New(); + + PyDict_SetItemString(matobj, "shininess", PyFloat_FromDouble((*mat).shininess)); + PyDict_SetItemString(matobj, "ior", PyFloat_FromDouble((*mat).ior)); + PyDict_SetItemString(matobj, "dissolve", PyFloat_FromDouble((*mat).dissolve)); + PyDict_SetItemString(matobj, "illum", PyLong_FromLong((*mat).illum)); + PyDict_SetItemString(matobj, "ambient_texname", PyUnicode_FromString((*mat).ambient_texname.c_str())); + PyDict_SetItemString(matobj, "diffuse_texname", PyUnicode_FromString((*mat).diffuse_texname.c_str())); + PyDict_SetItemString(matobj, "specular_texname", PyUnicode_FromString((*mat).specular_texname.c_str())); + PyDict_SetItemString(matobj, "normal_texname", PyUnicode_FromString((*mat).normal_texname.c_str())); + PyDict_SetItemString(matobj, "ambient", pyTupleFromfloat3((*mat).ambient)); + PyDict_SetItemString(matobj, "diffuse", pyTupleFromfloat3((*mat).diffuse)); + PyDict_SetItemString(matobj, "specular", pyTupleFromfloat3((*mat).specular)); + PyDict_SetItemString(matobj, "transmittance", pyTupleFromfloat3((*mat).transmittance)); + PyDict_SetItemString(matobj, "emission", pyTupleFromfloat3((*mat).emission)); + + PyDict_SetItemString(pymaterials, (*mat).name.c_str(), matobj); + } + + PyDict_SetItemString(rtntpl, "shapes", pyshapes); + PyDict_SetItemString(rtntpl, "materials", pymaterials); + + return rtntpl; +} + + +static PyMethodDef mMethods[] = { + +{"LoadObj", pyLoadObj, METH_VARARGS}, +{NULL, NULL, 0, NULL} + +}; + + +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "tinyobjloader", + NULL, + -1, + mMethods +}; + + +PyMODINIT_FUNC +PyInit_tinyobjloader(void) +{ + return PyModule_Create(&moduledef); +} + +} diff --git a/python/pyTOL.cbp.mak b/python/pyTOL.cbp.mak new file mode 100644 index 0000000..b14db6e --- /dev/null +++ b/python/pyTOL.cbp.mak @@ -0,0 +1,96 @@ +#------------------------------------------------------------------------------# +# This makefile was generated by 'cbp2make' tool rev.147 # +#------------------------------------------------------------------------------# +#requirements : Python 3 (- dev) + +WORKDIR = `pwd` + +CC = gcc +CXX = g++ +AR = ar +LD = g++ +WINDRES = windres + +INC = +CFLAGS = -Wall -fexceptions `python3-config --cflags` +RESINC = +LIBDIR = +LIB = +LDFLAGS = `python3-config --ldflags` + +INC_DEBUG = $(INC) +CFLAGS_DEBUG = $(CFLAGS) -g +RESINC_DEBUG = $(RESINC) +RCFLAGS_DEBUG = $(RCFLAGS) +LIBDIR_DEBUG = $(LIBDIR) +LIB_DEBUG = $(LIB) +LDFLAGS_DEBUG = $(LDFLAGS) +OBJDIR_DEBUG = obj/Debug +DEP_DEBUG = +OUT_DEBUG = bin/Debug/tinyobjloader.so + +INC_RELEASE = $(INC) +CFLAGS_RELEASE = $(CFLAGS) -O2 +RESINC_RELEASE = $(RESINC) +RCFLAGS_RELEASE = $(RCFLAGS) +LIBDIR_RELEASE = $(LIBDIR) +LIB_RELEASE = $(LIB) +LDFLAGS_RELEASE = $(LDFLAGS) -s +OBJDIR_RELEASE = obj/Release +DEP_RELEASE = +OUT_RELEASE = bin/Release/tinyobjloader.so + +OBJ_DEBUG = $(OBJDIR_DEBUG)/main.o $(OBJDIR_DEBUG)/tiny_obj_loader.o + +OBJ_RELEASE = $(OBJDIR_RELEASE)/main.o $(OBJDIR_RELEASE)/tiny_obj_loader.o + +all: debug release + +clean: clean_debug clean_release + +before_debug: + test -d bin/Debug || mkdir -p bin/Debug + test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG) + +after_debug: + +debug: before_debug out_debug after_debug + +out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG) + $(LD) -shared $(LIBDIR_DEBUG) $(OBJ_DEBUG) -o $(OUT_DEBUG) $(LDFLAGS_DEBUG) $(LIB_DEBUG) + +$(OBJDIR_DEBUG)/main.o: main.cpp + $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c main.cpp -o $(OBJDIR_DEBUG)/main.o + +$(OBJDIR_DEBUG)/tiny_obj_loader.o: tiny_obj_loader.cc + $(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c tiny_obj_loader.cc -o $(OBJDIR_DEBUG)/tiny_obj_loader.o + +clean_debug: + rm -f $(OBJ_DEBUG) $(OUT_DEBUG) + rm -rf bin/Debug + rm -rf $(OBJDIR_DEBUG) + +before_release: + test -d bin/Release || mkdir -p bin/Release + test -d $(OBJDIR_RELEASE) || mkdir -p $(OBJDIR_RELEASE) + +after_release: + +release: before_release out_release after_release + +out_release: before_release $(OBJ_RELEASE) $(DEP_RELEASE) + $(LD) -shared $(LIBDIR_RELEASE) $(OBJ_RELEASE) -o $(OUT_RELEASE) $(LDFLAGS_RELEASE) $(LIB_RELEASE) + +$(OBJDIR_RELEASE)/main.o: main.cpp + $(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c main.cpp -o $(OBJDIR_RELEASE)/main.o + +$(OBJDIR_RELEASE)/tiny_obj_loader.o: tiny_obj_loader.cc + $(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c tiny_obj_loader.cc -o $(OBJDIR_RELEASE)/tiny_obj_loader.o + +clean_release: + rm -f $(OBJ_RELEASE) $(OUT_RELEASE) + rm -rf bin/Release + rm -rf $(OBJDIR_RELEASE) + +.PHONY: before_debug after_debug clean_debug before_release after_release clean_release +