GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — tinyobjloader/python/main.cpp at master · basicNew/tinyobjloader Expand file tree/ Copy pathmain.cppMore file actions 212 lines (187 loc) · 7.26 KB / Copy pathmain.cppFile metadata and controls212 lines (187 loc) · 7.26 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 // python2/3 module for tinyobjloader // // usage: // import tinyobjloader as tol // model = tol.LoadObj(name) // print(model["shapes"]) // print(model["materials"] // note: // `shape.mesh.index_t` is represented as flattened array: (vertex_index, normal_index, texcoord_index) * num_faces #include <Python.h> #include <vector> #include "../tiny_obj_loader.h" typedef std::vector<double> vectd; typedef std::vector<int> vecti; 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 *rtndict, *pyshapes, *pymaterials, *pymaterial_indices, *attribobj, *current, *meshobj; char const* current_name; char const* filename; vectd vect; std::vector<tinyobj::index_t> indices; std::vector<unsigned char> face_verts; tinyobj::attrib_t attrib; std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::material_t> materials; if (!PyArg_ParseTuple(args, "s", &filename)) return NULL; std::string err, warn; tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename); pyshapes = PyDict_New(); pymaterials = PyDict_New(); pymaterial_indices = PyList_New(0); rtndict = PyDict_New(); attribobj = PyDict_New(); for (int i = 0; i <= 2; i++) { current = PyList_New(0); switch (i) { case 0: current_name = "vertices"; vect = vectd(attrib.vertices.begin(), attrib.vertices.end()); break; case 1: current_name = "normals"; vect = vectd(attrib.normals.begin(), attrib.normals.end()); break; case 2: current_name = "texcoords"; vect = vectd(attrib.texcoords.begin(), attrib.texcoords.end()); break; } for (vectd::iterator it = vect.begin(); it != vect.end(); it++) { PyObject* value = PyFloat_FromDouble(*it); PyList_Insert(current, it - vect.begin(), value); Py_DECREF(value); } PyDict_SetItemString(attribobj, current_name, current); Py_DECREF(current); } for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin(); shape != shapes.end(); shape++) { meshobj = PyDict_New(); tinyobj::mesh_t cm = (*shape).mesh; { current = PyList_New(0); for (size_t i = 0; i < cm.indices.size(); i++) { // Flatten index array: v_idx, vn_idx, vt_idx, v_idx, vn_idx, vt_idx, PyObject* value = PyLong_FromLong(cm.indices[i].vertex_index); PyList_Insert(current, 3 * i + 0, value); Py_DECREF(value); value = PyLong_FromLong(cm.indices[i].normal_index); PyList_Insert(current, 3 * i + 1, value); Py_DECREF(value); value = PyLong_FromLong(cm.indices[i].texcoord_index); PyList_Insert(current, 3 * i + 2, value); Py_DECREF(value); } PyDict_SetItemString(meshobj, "indices", current); Py_DECREF(current); } { current = PyList_New(0); for (size_t i = 0; i < cm.num_face_vertices.size(); i++) { // Widen data type to long. PyObject* value = PyLong_FromLong(cm.num_face_vertices[i]); PyList_Insert(current, i, value); Py_DECREF(value); } PyDict_SetItemString(meshobj, "num_face_vertices", current); Py_DECREF(current); } { current = PyList_New(0); for (size_t i = 0; i < cm.material_ids.size(); i++) { PyObject* value = PyLong_FromLong(cm.material_ids[i]); PyList_Insert(current, i, value); Py_DECREF(value); } PyDict_SetItemString(meshobj, "material_ids", current); Py_DECREF(current); } PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj); Py_DECREF(meshobj); } for (std::vector<tinyobj::material_t>::iterator mat = materials.begin(); mat != materials.end(); mat++) { PyObject* matobj = PyDict_New(); PyObject* unknown_parameter = PyDict_New(); for (std::map<std::string, std::string>::iterator p = mat->unknown_parameter.begin(); p != mat->unknown_parameter.end(); ++p) { PyObject* value = PyUnicode_FromString(p->second.c_str()); PyDict_SetItemString(unknown_parameter, p->first.c_str(), value); Py_DECREF(value); } PyObject* value = PyFloat_FromDouble(mat->shininess); PyDict_SetItemString(matobj, "shininess", value); Py_DECREF(value); value = PyFloat_FromDouble(mat->ior); PyDict_SetItemString(matobj, "ior", value); Py_DECREF(value); value = PyFloat_FromDouble(mat->dissolve); PyDict_SetItemString(matobj, "dissolve", value); Py_DECREF(value); value = PyLong_FromLong(mat->illum); PyDict_SetItemString(matobj, "illum", value); Py_DECREF(value); value = PyUnicode_FromString(mat->ambient_texname.c_str()); PyDict_SetItemString(matobj, "ambient_texname", value); Py_DECREF(value); value = PyUnicode_FromString(mat->diffuse_texname.c_str()); PyDict_SetItemString(matobj, "diffuse_texname", value); Py_DECREF(value); value = PyUnicode_FromString(mat->specular_texname.c_str()); PyDict_SetItemString(matobj, "specular_texname", value); Py_DECREF(value); value = PyUnicode_FromString(mat->specular_highlight_texname.c_str()); PyDict_SetItemString(matobj, "specular_highlight_texname", value); Py_DECREF(value); value = PyUnicode_FromString(mat->bump_texname.c_str()); PyDict_SetItemString(matobj, "bump_texname", value); Py_DECREF(value); value = PyUnicode_FromString(mat->displacement_texname.c_str()); PyDict_SetItemString(matobj, "displacement_texname", value); Py_DECREF(value); value = PyUnicode_FromString(mat->alpha_texname.c_str()); PyDict_SetItemString(matobj, "alpha_texname", value); Py_DECREF(value); 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(matobj, "unknown_parameter", unknown_parameter); Py_DECREF(unknown_parameter); PyDict_SetItemString(pymaterials, mat->name.c_str(), matobj); Py_DECREF(matobj); value = PyUnicode_FromString(mat->name.c_str()); PyList_Append(pymaterial_indices, value); Py_DECREF(value); } PyDict_SetItemString(rtndict, "shapes", pyshapes); Py_DECREF(pyshapes); PyDict_SetItemString(rtndict, "materials", pymaterials); Py_DECREF(pymaterials); PyDict_SetItemString(rtndict, "material_indices", pymaterial_indices); Py_DECREF(pymaterial_indices); PyDict_SetItemString(rtndict, "attribs", attribobj); Py_DECREF(attribobj); return rtndict; } static PyMethodDef mMethods[] = { {"LoadObj", pyLoadObj, METH_VARARGS}, {NULL, NULL, 0, NULL} }; #if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "tinyobjloader", NULL, -1, mMethods}; PyMODINIT_FUNC PyInit_tinyobjloader(void) { return PyModule_Create(&moduledef); } #else PyMODINIT_FUNC inittinyobjloader(void) { Py_InitModule3("tinyobjloader", mMethods, NULL); } #endif // PY_MAJOR_VERSION >= 3 } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |