@@ -18,44 +18,37 @@ typedef std::vector<int> vecti;
18
18
PyObject* pyTupleFromfloat3 (float array[3 ]) {
19
19
int i;
20
20
PyObject* tuple = PyTuple_New (3 );
21
-
22
21
for (i = 0 ; i <= 2 ; i++) {
23
22
PyTuple_SetItem (tuple, i, PyFloat_FromDouble (array[i]));
24
23
}
25
-
26
24
return tuple;
27
25
}
28
26
29
27
extern " C" {
30
28
31
29
static PyObject* pyLoadObj (PyObject* self, PyObject* args) {
32
30
PyObject *rtndict, *pyshapes, *pymaterials, *pymaterial_indices, *attribobj, *current, *meshobj;
33
-
34
31
char const * current_name;
35
32
char const * filename;
36
33
vectd vect;
37
34
std::vector<tinyobj::index_t > indices;
38
35
std::vector<unsigned char > face_verts;
39
-
40
36
tinyobj::attrib_t attrib;
41
37
std::vector<tinyobj::shape_t > shapes;
42
38
std::vector<tinyobj::material_t > materials;
43
39
44
40
if (!PyArg_ParseTuple (args, " s" , &filename)) return NULL ;
45
41
46
- std::string err;
47
- tinyobj::LoadObj (&attrib, &shapes, &materials, &err, filename);
48
-
42
+ std::string err, warn;
43
+ tinyobj::LoadObj (&attrib, &shapes, &materials, &warn, &err, filename);
49
44
pyshapes = PyDict_New ();
50
45
pymaterials = PyDict_New ();
51
46
pymaterial_indices = PyList_New (0 );
52
47
rtndict = PyDict_New ();
53
-
54
48
attribobj = PyDict_New ();
55
49
56
50
for (int i = 0 ; i <= 2 ; i++) {
57
51
current = PyList_New (0 );
58
-
59
52
switch (i) {
60
53
case 0 :
61
54
current_name = " vertices" ;
@@ -72,124 +65,137 @@ static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
72
65
}
73
66
74
67
for (vectd::iterator it = vect.begin (); it != vect.end (); it++) {
75
- PyList_Insert (current, it - vect.begin (), PyFloat_FromDouble (*it));
68
+ PyObject* value = PyFloat_FromDouble (*it);
69
+ PyList_Insert (current, it - vect.begin (), value);
70
+ Py_DECREF (value);
76
71
}
77
-
78
72
PyDict_SetItemString (attribobj, current_name, current);
73
+ Py_DECREF (current);
79
74
}
80
75
81
- for (std::vector<tinyobj::shape_t >::iterator shape = shapes.begin ();
82
- shape != shapes.end (); shape++) {
76
+ for (std::vector<tinyobj::shape_t >::iterator shape = shapes.begin (); shape != shapes.end (); shape++) {
83
77
meshobj = PyDict_New ();
84
78
tinyobj::mesh_t cm = (*shape).mesh ;
85
79
86
80
{
87
81
current = PyList_New (0 );
88
-
89
82
for (size_t i = 0 ; i < cm.indices .size (); i++) {
90
83
// Flatten index array: v_idx, vn_idx, vt_idx, v_idx, vn_idx, vt_idx,
91
- // ...
92
- PyList_Insert (current, 3 * i + 0 ,
93
- PyLong_FromLong (cm.indices [i].vertex_index ));
94
- PyList_Insert (current, 3 * i + 1 ,
95
- PyLong_FromLong (cm.indices [i].normal_index ));
96
- PyList_Insert (current, 3 * i + 2 ,
97
- PyLong_FromLong (cm.indices [i].texcoord_index ));
84
+ PyObject* value = PyLong_FromLong (cm.indices [i].vertex_index );
85
+ PyList_Insert (current, 3 * i + 0 , value);
86
+ Py_DECREF (value);
87
+ value = PyLong_FromLong (cm.indices [i].normal_index );
88
+ PyList_Insert (current, 3 * i + 1 , value);
89
+ Py_DECREF (value);
90
+ value = PyLong_FromLong (cm.indices [i].texcoord_index );
91
+ PyList_Insert (current, 3 * i + 2 , value);
92
+ Py_DECREF (value);
98
93
}
99
-
100
94
PyDict_SetItemString (meshobj, " indices" , current);
95
+ Py_DECREF (current);
101
96
}
102
97
103
98
{
104
99
current = PyList_New (0 );
105
-
106
100
for (size_t i = 0 ; i < cm.num_face_vertices .size (); i++) {
107
101
// Widen data type to long.
108
- PyList_Insert (current, i, PyLong_FromLong (cm.num_face_vertices [i]));
102
+ PyObject* value = PyLong_FromLong (cm.num_face_vertices [i]);
103
+ PyList_Insert (current, i, value);
104
+ Py_DECREF (value);
109
105
}
110
-
111
106
PyDict_SetItemString (meshobj, " num_face_vertices" , current);
107
+ Py_DECREF (current);
112
108
}
113
109
114
110
{
115
111
current = PyList_New (0 );
116
-
117
112
for (size_t i = 0 ; i < cm.material_ids .size (); i++) {
118
- PyList_Insert (current, i, PyLong_FromLong (cm.material_ids [i]));
113
+ PyObject* value = PyLong_FromLong (cm.material_ids [i]);
114
+ PyList_Insert (current, i, value);
115
+ Py_DECREF (value);
119
116
}
120
-
121
117
PyDict_SetItemString (meshobj, " material_ids" , current);
118
+ Py_DECREF (current);
122
119
}
123
-
124
120
PyDict_SetItemString (pyshapes, (*shape).name .c_str (), meshobj);
121
+ Py_DECREF (meshobj);
125
122
}
126
123
127
- for (std::vector<tinyobj::material_t >::iterator mat = materials.begin ();
128
- mat != materials.end (); mat++) {
124
+ for (std::vector<tinyobj::material_t >::iterator mat = materials.begin (); mat != materials.end (); mat++) {
129
125
PyObject* matobj = PyDict_New ();
130
126
PyObject* unknown_parameter = PyDict_New ();
131
127
132
- for (std::map<std::string, std::string>::iterator p =
133
- mat->unknown_parameter .begin ();
134
- p != mat->unknown_parameter .end (); ++p) {
135
- PyDict_SetItemString (unknown_parameter, p->first .c_str (),
136
- PyUnicode_FromString (p->second .c_str ()));
128
+ for (std::map<std::string, std::string>::iterator p = mat->unknown_parameter .begin (); p != mat->unknown_parameter .end (); ++p) {
129
+ PyObject* value = PyUnicode_FromString (p->second .c_str ());
130
+ PyDict_SetItemString (unknown_parameter, p->first .c_str (), value);
131
+ Py_DECREF (value);
137
132
}
138
133
139
- PyDict_SetItemString (matobj, " shininess" ,
140
- PyFloat_FromDouble (mat->shininess ));
141
- PyDict_SetItemString (matobj, " ior" , PyFloat_FromDouble (mat->ior ));
142
- PyDict_SetItemString (matobj, " dissolve" ,
143
- PyFloat_FromDouble (mat->dissolve ));
144
- PyDict_SetItemString (matobj, " illum" , PyLong_FromLong (mat->illum ));
145
- PyDict_SetItemString (matobj, " ambient_texname" ,
146
- PyUnicode_FromString (mat->ambient_texname .c_str ()));
147
- PyDict_SetItemString (matobj, " diffuse_texname" ,
148
- PyUnicode_FromString (mat->diffuse_texname .c_str ()));
149
- PyDict_SetItemString (matobj, " specular_texname" ,
150
- PyUnicode_FromString (mat->specular_texname .c_str ()));
151
- PyDict_SetItemString (
152
- matobj, " specular_highlight_texname" ,
153
- PyUnicode_FromString (mat->specular_highlight_texname .c_str ()));
154
- PyDict_SetItemString (matobj, " bump_texname" ,
155
- PyUnicode_FromString (mat->bump_texname .c_str ()));
156
- PyDict_SetItemString (
157
- matobj, " displacement_texname" ,
158
- PyUnicode_FromString (mat->displacement_texname .c_str ()));
159
- PyDict_SetItemString (matobj, " alpha_texname" ,
160
- PyUnicode_FromString (mat->alpha_texname .c_str ()));
134
+ PyObject* value = PyFloat_FromDouble (mat->shininess );
135
+ PyDict_SetItemString (matobj, " shininess" , value);
136
+ Py_DECREF (value);
137
+ value = PyFloat_FromDouble (mat->ior );
138
+ PyDict_SetItemString (matobj, " ior" , value);
139
+ Py_DECREF (value);
140
+ value = PyFloat_FromDouble (mat->dissolve );
141
+ PyDict_SetItemString (matobj, " dissolve" , value);
142
+ Py_DECREF (value);
143
+ value = PyLong_FromLong (mat->illum );
144
+ PyDict_SetItemString (matobj, " illum" , value);
145
+ Py_DECREF (value);
146
+ value = PyUnicode_FromString (mat->ambient_texname .c_str ());
147
+ PyDict_SetItemString (matobj, " ambient_texname" , value);
148
+ Py_DECREF (value);
149
+ value = PyUnicode_FromString (mat->diffuse_texname .c_str ());
150
+ PyDict_SetItemString (matobj, " diffuse_texname" , value);
151
+ Py_DECREF (value);
152
+ value = PyUnicode_FromString (mat->specular_texname .c_str ());
153
+ PyDict_SetItemString (matobj, " specular_texname" , value);
154
+ Py_DECREF (value);
155
+ value = PyUnicode_FromString (mat->specular_highlight_texname .c_str ());
156
+ PyDict_SetItemString (matobj, " specular_highlight_texname" , value);
157
+ Py_DECREF (value);
158
+ value = PyUnicode_FromString (mat->bump_texname .c_str ());
159
+ PyDict_SetItemString (matobj, " bump_texname" , value);
160
+ Py_DECREF (value);
161
+ value = PyUnicode_FromString (mat->displacement_texname .c_str ());
162
+ PyDict_SetItemString (matobj, " displacement_texname" , value);
163
+ Py_DECREF (value);
164
+ value = PyUnicode_FromString (mat->alpha_texname .c_str ());
165
+ PyDict_SetItemString (matobj, " alpha_texname" , value);
166
+ Py_DECREF (value);
161
167
PyDict_SetItemString (matobj, " ambient" , pyTupleFromfloat3 (mat->ambient ));
162
168
PyDict_SetItemString (matobj, " diffuse" , pyTupleFromfloat3 (mat->diffuse ));
163
- PyDict_SetItemString (matobj, " specular" ,
164
- pyTupleFromfloat3 (mat->specular ));
165
- PyDict_SetItemString (matobj, " transmittance" ,
166
- pyTupleFromfloat3 (mat->transmittance ));
167
- PyDict_SetItemString (matobj, " emission" ,
168
- pyTupleFromfloat3 (mat->emission ));
169
+ PyDict_SetItemString (matobj, " specular" , pyTupleFromfloat3 (mat->specular ));
170
+ PyDict_SetItemString (matobj, " transmittance" , pyTupleFromfloat3 (mat->transmittance ));
171
+ PyDict_SetItemString (matobj, " emission" , pyTupleFromfloat3 (mat->emission ));
169
172
PyDict_SetItemString (matobj, " unknown_parameter" , unknown_parameter);
170
-
173
+ Py_DECREF (unknown_parameter);
171
174
PyDict_SetItemString (pymaterials, mat->name .c_str (), matobj);
172
- PyList_Append (pymaterial_indices, PyUnicode_FromString (mat->name .c_str ()));
175
+ Py_DECREF (matobj);
176
+ value = PyUnicode_FromString (mat->name .c_str ());
177
+ PyList_Append (pymaterial_indices, value);
178
+ Py_DECREF (value);
173
179
}
174
180
175
181
PyDict_SetItemString (rtndict, " shapes" , pyshapes);
182
+ Py_DECREF (pyshapes);
176
183
PyDict_SetItemString (rtndict, " materials" , pymaterials);
184
+ Py_DECREF (pymaterials);
177
185
PyDict_SetItemString (rtndict, " material_indices" , pymaterial_indices);
186
+ Py_DECREF (pymaterial_indices);
178
187
PyDict_SetItemString (rtndict, " attribs" , attribobj);
179
-
188
+ Py_DECREF (attribobj);
180
189
return rtndict;
181
190
}
182
191
183
192
static PyMethodDef mMethods [] = {
184
-
185
193
{" LoadObj" , pyLoadObj, METH_VARARGS}, {NULL , NULL , 0 , NULL }
186
-
187
194
};
188
195
189
196
#if PY_MAJOR_VERSION >= 3
190
197
191
- static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, " tinyobjloader" ,
192
- NULL , -1 , mMethods };
198
+ static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, " tinyobjloader" , NULL , -1 , mMethods };
193
199
194
200
PyMODINIT_FUNC PyInit_tinyobjloader (void ) {
195
201
return PyModule_Create (&moduledef);
0 commit comments