diff --git a/python/cornell_box_output.json b/python/cornell_box_output.json new file mode 100644 index 0000000..ba2e7e1 --- /dev/null +++ b/python/cornell_box_output.json @@ -0,0 +1,601 @@ +{ + "shapes": { + "ceiling": { + "texcoords": [], + "positions": [ + 556.0, + 548.7999877929688, + 0.0, + 556.0, + 548.7999877929688, + 559.2000122070312, + 0.0, + 548.7999877929688, + 559.2000122070312, + 0.0, + 548.7999877929688, + 0.0 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0 + ], + "material_ids": [ + 0.0, + 0.0 + ], + "normals": [] + }, + "floor": { + "texcoords": [], + "positions": [ + 552.7999877929688, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 559.2000122070312, + 549.5999755859375, + 0.0, + 559.2000122070312, + 290.0, + 0.0, + 114.0, + 240.0, + 0.0, + 272.0, + 82.0, + 0.0, + 225.0, + 130.0, + 0.0, + 65.0, + 472.0, + 0.0, + 406.0, + 314.0, + 0.0, + 456.0, + 265.0, + 0.0, + 296.0, + 423.0, + 0.0, + 247.0 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 4.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 8.0, + 10.0, + 11.0 + ], + "material_ids": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "normals": [] + }, + "light": { + "texcoords": [], + "positions": [ + 343.0, + 548.0, + 227.0, + 343.0, + 548.0, + 332.0, + 213.0, + 548.0, + 332.0, + 213.0, + 548.0, + 227.0 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0 + ], + "material_ids": [ + 4.0, + 4.0 + ], + "normals": [] + }, + "green_wall": { + "texcoords": [], + "positions": [ + 0.0, + 0.0, + 559.2000122070312, + 0.0, + 0.0, + 0.0, + 0.0, + 548.7999877929688, + 0.0, + 0.0, + 548.7999877929688, + 559.2000122070312 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0 + ], + "material_ids": [ + 2.0, + 2.0 + ], + "normals": [] + }, + "back_wall": { + "texcoords": [], + "positions": [ + 549.5999755859375, + 0.0, + 559.2000122070312, + 0.0, + 0.0, + 559.2000122070312, + 0.0, + 548.7999877929688, + 559.2000122070312, + 556.0, + 548.7999877929688, + 559.2000122070312 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0 + ], + "material_ids": [ + 0.0, + 0.0 + ], + "normals": [] + }, + "short_block": { + "texcoords": [], + "positions": [ + 130.0, + 165.0, + 65.0, + 82.0, + 165.0, + 225.0, + 240.0, + 165.0, + 272.0, + 290.0, + 165.0, + 114.0, + 290.0, + 0.0, + 114.0, + 290.0, + 165.0, + 114.0, + 240.0, + 165.0, + 272.0, + 240.0, + 0.0, + 272.0, + 130.0, + 0.0, + 65.0, + 130.0, + 165.0, + 65.0, + 290.0, + 165.0, + 114.0, + 290.0, + 0.0, + 114.0, + 82.0, + 0.0, + 225.0, + 82.0, + 165.0, + 225.0, + 130.0, + 165.0, + 65.0, + 130.0, + 0.0, + 65.0, + 240.0, + 0.0, + 272.0, + 240.0, + 165.0, + 272.0, + 82.0, + 165.0, + 225.0, + 82.0, + 0.0, + 225.0 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 4.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 8.0, + 10.0, + 11.0, + 12.0, + 13.0, + 14.0, + 12.0, + 14.0, + 15.0, + 16.0, + 17.0, + 18.0, + 16.0, + 18.0, + 19.0 + ], + "material_ids": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "normals": [] + }, + "tall_block": { + "texcoords": [], + "positions": [ + 423.0, + 0.0, + 247.0, + 423.0, + 330.0, + 247.0, + 472.0, + 330.0, + 406.0, + 472.0, + 0.0, + 406.0, + 472.0, + 0.0, + 406.0, + 472.0, + 330.0, + 406.0, + 314.0, + 330.0, + 456.0, + 314.0, + 0.0, + 456.0, + 314.0, + 0.0, + 456.0, + 314.0, + 330.0, + 456.0, + 265.0, + 330.0, + 296.0, + 265.0, + 0.0, + 296.0, + 265.0, + 0.0, + 296.0, + 265.0, + 330.0, + 296.0, + 423.0, + 330.0, + 247.0, + 423.0, + 0.0, + 247.0 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 4.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 8.0, + 10.0, + 11.0, + 12.0, + 13.0, + 14.0, + 12.0, + 14.0, + 15.0 + ], + "material_ids": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "normals": [] + }, + "red_wall": { + "texcoords": [], + "positions": [ + 552.7999877929688, + 0.0, + 0.0, + 549.5999755859375, + 0.0, + 559.2000122070312, + 556.0, + 548.7999877929688, + 559.2000122070312, + 556.0, + 548.7999877929688, + 0.0 + ], + "indicies": [ + 0.0, + 1.0, + 2.0, + 0.0, + 2.0, + 3.0 + ], + "material_ids": [ + 1.0, + 1.0 + ], + "normals": [] + } + }, + "materials": { + "blue": { + "transmittance": [ + 0.0, + 0.0, + 0.0 + ], + "illum": 0, + "emission": [ + 0.0, + 0.0, + 0.0 + ], + "diffuse_texname": "", + "ambient_texname": "", + "normal_texname": "", + "shininess": 1.0, + "ior": 1.0, + "specular": [ + 0.0, + 0.0, + 0.0 + ], + "specular_texname": "", + "diffuse": [ + 0.0, + 0.0, + 1.0 + ], + "ambient": [ + 0.0, + 0.0, + 0.0 + ], + "dissolve": 1.0 + }, + "light": { + "transmittance": [ + 0.0, + 0.0, + 0.0 + ], + "illum": 0, + "emission": [ + 0.0, + 0.0, + 0.0 + ], + "diffuse_texname": "", + "ambient_texname": "", + "normal_texname": "", + "shininess": 1.0, + "ior": 1.0, + "specular": [ + 0.0, + 0.0, + 0.0 + ], + "specular_texname": "", + "diffuse": [ + 1.0, + 1.0, + 1.0 + ], + "ambient": [ + 20.0, + 20.0, + 20.0 + ], + "dissolve": 1.0 + }, + "white": { + "transmittance": [ + 0.0, + 0.0, + 0.0 + ], + "illum": 0, + "emission": [ + 0.0, + 0.0, + 0.0 + ], + "diffuse_texname": "", + "ambient_texname": "", + "normal_texname": "", + "shininess": 1.0, + "ior": 1.0, + "specular": [ + 0.0, + 0.0, + 0.0 + ], + "specular_texname": "", + "diffuse": [ + 1.0, + 1.0, + 1.0 + ], + "ambient": [ + 0.0, + 0.0, + 0.0 + ], + "dissolve": 1.0 + }, + "green": { + "transmittance": [ + 0.0, + 0.0, + 0.0 + ], + "illum": 0, + "emission": [ + 0.0, + 0.0, + 0.0 + ], + "diffuse_texname": "", + "ambient_texname": "", + "normal_texname": "", + "shininess": 1.0, + "ior": 1.0, + "specular": [ + 0.0, + 0.0, + 0.0 + ], + "specular_texname": "", + "diffuse": [ + 0.0, + 1.0, + 0.0 + ], + "ambient": [ + 0.0, + 0.0, + 0.0 + ], + "dissolve": 1.0 + }, + "red": { + "transmittance": [ + 0.0, + 0.0, + 0.0 + ], + "illum": 0, + "emission": [ + 0.0, + 0.0, + 0.0 + ], + "diffuse_texname": "", + "ambient_texname": "", + "normal_texname": "", + "shininess": 1.0, + "ior": 1.0, + "specular": [ + 0.0, + 0.0, + 0.0 + ], + "specular_texname": "", + "diffuse": [ + 1.0, + 0.0, + 0.0 + ], + "ambient": [ + 0.0, + 0.0, + 0.0 + ], + "dissolve": 1.0 + } + } +} diff --git a/python/howto.py b/python/howto.py index 1342dac..c526821 100644 --- a/python/howto.py +++ b/python/howto.py @@ -1,5 +1,11 @@ import tinyobjloader as tol +import json + +model = tol.LoadObj("cornell_box.obj") + +#print(model["shapes"], model["materials"]) +print( json.dumps(model, indent=4) ) + +#see cornell_box_output.json -model = tol.LoadObj("cube.obj") -print(model["shapes"], model["materials"]) diff --git a/python/main.cpp b/python/main.cpp index 636a24d..b2c6e96 100644 --- a/python/main.cpp +++ b/python/main.cpp @@ -5,6 +5,7 @@ // model = tol.LoadObj(name) // print(model["shapes"]) // print(model["materials"] + #include #include #include "../tiny_obj_loader.h" @@ -30,8 +31,13 @@ extern "C" static PyObject* pyLoadObj(PyObject* self, PyObject* args) { - PyObject *rtntpl, *pyshapes, *pymaterials; + PyObject *rtndict, *pyshapes, *pymaterials, + *current, *meshobj; + char const* filename; + char *current_name; + vectd vect; + std::vector shapes; std::vector materials; @@ -42,48 +48,46 @@ pyLoadObj(PyObject* self, PyObject* args) pyshapes = PyDict_New(); pymaterials = PyDict_New(); - rtntpl = PyDict_New(); + rtndict = 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); - + meshobj = PyDict_New(); tinyobj::mesh_t cm = (*shape).mesh; - for (int i = 0; i <= 4 ; i++ ) + + for (int i = 0; i <= 4; i++ ) { - PyObject *current; - vectd vect; + current = PyList_New(0); - 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()); + switch(i) { + + case 0: + current_name = "positions"; + vect = vectd(cm.positions.begin(), cm.positions.end()); break; + case 1: + current_name = "normals"; + vect = vectd(cm.normals.begin(), cm.normals.end()); break; + case 2: + current_name = "texcoords"; + vect = vectd(cm.texcoords.begin(), cm.texcoords.end()); break; + case 3: + current_name = "indicies"; + vect = vectd(cm.indices.begin(), cm.indices.end()); break; + case 4: + current_name = "material_ids"; + vect = vectd(cm.material_ids.begin(), cm.material_ids.end()); break; + } - - for (std::vector::iterator it = vect.begin() ; + + for (vectd::iterator it = vect.begin() ; it != vect.end(); it++) { PyList_Insert(current, it - vect.begin(), PyFloat_FromDouble(*it)); } - PyTuple_SetItem(meshobj, i, current); + PyDict_SetItemString(meshobj, current_name, current); + } PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj); @@ -111,10 +115,10 @@ pyLoadObj(PyObject* self, PyObject* args) PyDict_SetItemString(pymaterials, (*mat).name.c_str(), matobj); } - PyDict_SetItemString(rtntpl, "shapes", pyshapes); - PyDict_SetItemString(rtntpl, "materials", pymaterials); + PyDict_SetItemString(rtndict, "shapes", pyshapes); + PyDict_SetItemString(rtndict, "materials", pymaterials); - return rtntpl; + return rtndict; } diff --git a/python/setup.py b/python/setup.py new file mode 100644 index 0000000..de7b976 --- /dev/null +++ b/python/setup.py @@ -0,0 +1,13 @@ +from distutils.core import setup, Extension + + +m = Extension('tinyobjloader', + sources = ['main.cpp', '../tiny_obj_loader.cc']) + + +setup (name = 'tinyobjloader', + version = '0.1', + description = 'Python module for tinyobjloader', + ext_modules = [m]) + +