|
| 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 | +} |
0 commit comments