PyAPI: move deep-copy args check to py_capi_utils
[blender.git] / source / blender / python / mathutils / mathutils.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Gilbert, Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/mathutils/mathutils.c
24  *  \ingroup pymathutils
25  */
26
27 #include <Python.h>
28
29 #include "mathutils.h"
30
31 #include "BLI_math.h"
32 #include "BLI_utildefines.h"
33
34 #include "../generic/python_utildefines.h"
35
36 #ifndef MATH_STANDALONE
37 #  include "BLI_dynstr.h"
38 #endif
39
40 PyDoc_STRVAR(M_Mathutils_doc,
41 "This module provides access to math operations.\n"
42 "\n"
43 ".. note::\n"
44 "\n"
45 "   Classes, methods and attributes that accept vectors also accept other numeric sequences,\n"
46 "   such as tuples, lists."
47 "\n\n"
48 "Submodules:\n"
49 "\n"
50 ".. toctree::\n"
51 "   :maxdepth: 1\n"
52 "\n"
53 "   mathutils.geometry.rst\n"
54 "   mathutils.bvhtree.rst\n"
55 "   mathutils.kdtree.rst\n"
56 "   mathutils.interpolate.rst\n"
57 "   mathutils.noise.rst\n"
58 "\n"
59 "The :mod:`mathutils` module provides the following classes:\n"
60 "\n"
61 "- :class:`Color`,\n"
62 "- :class:`Euler`,\n"
63 "- :class:`Matrix`,\n"
64 "- :class:`Quaternion`,\n"
65 "- :class:`Vector`,\n"
66 );
67 static int mathutils_array_parse_fast(float *array,
68                                       int size,
69                                       PyObject *value_fast,
70                                       const char *error_prefix)
71 {
72         PyObject *item;
73         PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
74
75         int i;
76
77         i = size;
78         do {
79                 i--;
80                 if (((array[i] = PyFloat_AsDouble((item = value_fast_items[i]))) == -1.0f) &&
81                     PyErr_Occurred())
82                 {
83                         PyErr_Format(PyExc_TypeError,
84                                      "%.200s: sequence index %d expected a number, "
85                                      "found '%.200s' type, ",
86                                      error_prefix, i, Py_TYPE(item)->tp_name);
87                         size = -1;
88                         break;
89                 }
90         } while (i);
91
92         return size;
93 }
94
95 /**
96  * helper function that returns a Python ``__hash__``.
97  *
98  * \note consistent with the equivalent tuple of floats (CPython's 'tuplehash')
99  */
100 Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
101 {
102         int i;
103         Py_uhash_t x;  /* Unsigned for defined overflow behavior. */
104         Py_hash_t y;
105         Py_uhash_t mult;
106         Py_ssize_t len;
107
108         mult = _PyHASH_MULTIPLIER;
109         len = array_len;
110         x = 0x345678UL;
111         i = 0;
112         while (--len >= 0) {
113                 y = _Py_HashDouble((double)(array[i++]));
114                 if (y == -1)
115                         return -1;
116                 x = (x ^ y) * mult;
117                 /* the cast might truncate len; that doesn't change hash stability */
118                 mult += (Py_hash_t)(82520UL + len + len);
119         }
120         x += 97531UL;
121         if (x == (Py_uhash_t)-1)
122                 x = -2;
123         return x;
124 }
125
126 /* helper function returns length of the 'value', -1 on error */
127 int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
128 {
129         const unsigned int flag = array_max;
130         int size;
131
132         array_max &= ~MU_ARRAY_FLAGS;
133
134 #if 1 /* approx 6x speedup for mathutils types */
135
136         if ((size = VectorObject_Check(value)     ? ((VectorObject *)value)->size : 0) ||
137             (size = EulerObject_Check(value)      ? 3 : 0) ||
138             (size = QuaternionObject_Check(value) ? 4 : 0) ||
139             (size = ColorObject_Check(value)      ? 3 : 0))
140         {
141                 if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
142                         return -1;
143                 }
144
145                 if (flag & MU_ARRAY_SPILL) {
146                         CLAMP_MAX(size, array_max);
147                 }
148
149                 if (size > array_max || size < array_min) {
150                         if (array_max == array_min) {
151                                 PyErr_Format(PyExc_ValueError,
152                                              "%.200s: sequence size is %d, expected %d",
153                                              error_prefix, size, array_max);
154                         }
155                         else {
156                                 PyErr_Format(PyExc_ValueError,
157                                              "%.200s: sequence size is %d, expected [%d - %d]",
158                                              error_prefix, size, array_min, array_max);
159                         }
160                         return -1;
161                 }
162
163                 memcpy(array, ((BaseMathObject *)value)->data, size * sizeof(float));
164         }
165         else
166 #endif
167         {
168                 PyObject *value_fast = NULL;
169
170                 /* non list/tuple cases */
171                 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
172                         /* PySequence_Fast sets the error */
173                         return -1;
174                 }
175
176                 size = PySequence_Fast_GET_SIZE(value_fast);
177
178                 if (flag & MU_ARRAY_SPILL) {
179                         CLAMP_MAX(size, array_max);
180                 }
181
182                 if (size > array_max || size < array_min) {
183                         if (array_max == array_min) {
184                                 PyErr_Format(PyExc_ValueError,
185                                              "%.200s: sequence size is %d, expected %d",
186                                              error_prefix, size, array_max);
187                         }
188                         else {
189                                 PyErr_Format(PyExc_ValueError,
190                                              "%.200s: sequence size is %d, expected [%d - %d]",
191                                              error_prefix, size, array_min, array_max);
192                         }
193                         Py_DECREF(value_fast);
194                         return -1;
195                 }
196
197                 size = mathutils_array_parse_fast(array, size, value_fast, error_prefix);
198                 Py_DECREF(value_fast);
199         }
200
201         if (size != -1) {
202                 if (flag & MU_ARRAY_ZERO) {
203                         int size_left = array_max - size;
204                         if (size_left) {
205                                 memset(&array[size], 0, sizeof(float) * size_left);
206                         }
207                 }
208         }
209
210         return size;
211 }
212
213 /* on error, -1 is returned and no allocation is made */
214 int mathutils_array_parse_alloc(float **array, int array_min, PyObject *value, const char *error_prefix)
215 {
216         int size;
217
218 #if 1 /* approx 6x speedup for mathutils types */
219
220         if ((size = VectorObject_Check(value)     ? ((VectorObject *)value)->size : 0) ||
221             (size = EulerObject_Check(value)      ? 3 : 0) ||
222             (size = QuaternionObject_Check(value) ? 4 : 0) ||
223             (size = ColorObject_Check(value)      ? 3 : 0))
224         {
225                 if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
226                         return -1;
227                 }
228
229                 if (size < array_min) {
230                         PyErr_Format(PyExc_ValueError,
231                                      "%.200s: sequence size is %d, expected > %d",
232                                      error_prefix, size, array_min);
233                         return -1;
234                 }
235
236                 *array = PyMem_Malloc(size * sizeof(float));
237                 memcpy(*array, ((BaseMathObject *)value)->data, size * sizeof(float));
238                 return size;
239         }
240         else
241 #endif
242         {
243                 PyObject *value_fast = NULL;
244                 // *array = NULL;
245                 int ret;
246
247                 /* non list/tuple cases */
248                 if (!(value_fast = PySequence_Fast(value, error_prefix))) {
249                         /* PySequence_Fast sets the error */
250                         return -1;
251                 }
252
253                 size = PySequence_Fast_GET_SIZE(value_fast);
254
255                 if (size < array_min) {
256                         Py_DECREF(value_fast);
257                         PyErr_Format(PyExc_ValueError,
258                                      "%.200s: sequence size is %d, expected > %d",
259                                      error_prefix, size, array_min);
260                         return -1;
261                 }
262
263                 *array = PyMem_Malloc(size * sizeof(float));
264
265                 ret = mathutils_array_parse_fast(*array, size, value_fast, error_prefix);
266                 Py_DECREF(value_fast);
267
268                 if (ret == -1) {
269                         PyMem_Free(*array);
270                 }
271
272                 return ret;
273         }
274 }
275
276 /* parse an array of vectors */
277 int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
278 {
279         PyObject *value_fast;
280         const int array_dim_flag = array_dim;
281         int i, size;
282
283         /* non list/tuple cases */
284         if (!(value_fast = PySequence_Fast(value, error_prefix))) {
285                 /* PySequence_Fast sets the error */
286                 return -1;
287         }
288
289         size = PySequence_Fast_GET_SIZE(value_fast);
290
291         if (size != 0) {
292                 PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
293                 float *fp;
294
295                 array_dim &= ~MU_ARRAY_FLAGS;
296
297                 fp = *array = PyMem_Malloc(size * array_dim * sizeof(float));
298
299                 for (i = 0; i < size; i++, fp += array_dim) {
300                         PyObject *item = value_fast_items[i];
301
302                         if (mathutils_array_parse(fp, array_dim, array_dim_flag, item, error_prefix) == -1) {
303                                 PyMem_Free(*array);
304                                 *array = NULL;
305                                 size = -1;
306                                 break;
307                         }
308                 }
309         }
310
311         Py_DECREF(value_fast);
312         return size;
313 }
314
315 int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
316 {
317         if (EulerObject_Check(value)) {
318                 if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
319                         return -1;
320                 }
321                 else {
322                         eulO_to_mat3(rmat, ((EulerObject *)value)->eul, ((EulerObject *)value)->order);
323                         return 0;
324                 }
325         }
326         else if (QuaternionObject_Check(value)) {
327                 if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
328                         return -1;
329                 }
330                 else {
331                         float tquat[4];
332                         normalize_qt_qt(tquat, ((QuaternionObject *)value)->quat);
333                         quat_to_mat3(rmat, tquat);
334                         return 0;
335                 }
336         }
337         else if (MatrixObject_Check(value)) {
338                 if (BaseMath_ReadCallback((BaseMathObject *)value) == -1) {
339                         return -1;
340                 }
341                 else if (((MatrixObject *)value)->num_row < 3 || ((MatrixObject *)value)->num_col < 3) {
342                         PyErr_Format(PyExc_ValueError,
343                                      "%.200s: matrix must have minimum 3x3 dimensions",
344                                      error_prefix);
345                         return -1;
346                 }
347                 else {
348                         matrix_as_3x3(rmat, (MatrixObject *)value);
349                         normalize_m3(rmat);
350                         return 0;
351                 }
352         }
353         else {
354                 PyErr_Format(PyExc_TypeError,
355                              "%.200s: expected a Euler, Quaternion or Matrix type, "
356                              "found %.200s", error_prefix, Py_TYPE(value)->tp_name);
357                 return -1;
358         }
359 }
360
361
362 /* ----------------------------------MATRIX FUNCTIONS-------------------- */
363
364
365 /* Utility functions */
366
367 /* LomontRRDCompare4, Ever Faster Float Comparisons by Randy Dillon */
368 /* XXX We may want to use 'safer' BLI's compare_ff_relative ultimately?
369  *     LomontRRDCompare4() is an optimized version of Dawson's AlmostEqual2sComplement() (see [1] and [2]).
370  *     Dawson himself now claims this is not a 'safe' thing to do (pushing ULP method beyond its limits),
371  *     an recommends using work from [3] instead, which is done in BLI func...
372  *
373  *     [1] http://www.randydillon.org/Papers/2007/everfast.htm
374  *     [2] http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
375  *     [3] https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ instead
376  */
377 #define SIGNMASK(i) (-(int)(((unsigned int)(i)) >> 31))
378
379 int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
380 {
381         /* solid, fast routine across all platforms
382          * with constant time behavior */
383         int ai = *(int *)(&af);
384         int bi = *(int *)(&bf);
385         int test = SIGNMASK(ai ^ bi);
386         int diff, v1, v2;
387
388         assert((0 == test) || (0xFFFFFFFF == test));
389         diff = (ai ^ (test & 0x7fffffff)) - bi;
390         v1 = maxDiff + diff;
391         v2 = maxDiff - diff;
392         return (v1 | v2) >= 0;
393 }
394
395 /*---------------------- EXPP_VectorsAreEqual -------------------------
396  * Builds on EXPP_FloatsAreEqual to test vectors */
397 int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
398 {
399         int x;
400         for (x = 0; x < size; x++) {
401                 if (EXPP_FloatsAreEqual(vecA[x], vecB[x], floatSteps) == 0)
402                         return 0;
403         }
404         return 1;
405 }
406
407 #ifndef MATH_STANDALONE
408 /* dynstr as python string utility funcions, frees 'ds'! */
409 PyObject *mathutils_dynstr_to_py(struct DynStr *ds)
410 {
411         const int ds_len = BLI_dynstr_get_len(ds); /* space for \0 */
412         char *ds_buf     = PyMem_Malloc(ds_len + 1);
413         PyObject *ret;
414         BLI_dynstr_get_cstring_ex(ds, ds_buf);
415         BLI_dynstr_free(ds);
416         ret = PyUnicode_FromStringAndSize(ds_buf, ds_len);
417         PyMem_Free(ds_buf);
418         return ret;
419 }
420 #endif
421
422 /* Mathutils Callbacks */
423
424 /* for mathutils internal use only, eventually should re-alloc but to start with we only have a few users */
425 #define MATHUTILS_TOT_CB 17
426 static Mathutils_Callback *mathutils_callbacks[MATHUTILS_TOT_CB] = {NULL};
427
428 unsigned char Mathutils_RegisterCallback(Mathutils_Callback *cb)
429 {
430         unsigned char i;
431
432         /* find the first free slot */
433         for (i = 0; mathutils_callbacks[i]; i++) {
434                 if (mathutils_callbacks[i] == cb) /* already registered? */
435                         return i;
436         }
437
438         BLI_assert(i + 1 < MATHUTILS_TOT_CB);
439
440         mathutils_callbacks[i] = cb;
441         return i;
442 }
443
444 /* use macros to check for NULL */
445 int _BaseMathObject_ReadCallback(BaseMathObject *self)
446 {
447         Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
448         if (LIKELY(cb->get(self, self->cb_subtype) != -1)) {
449                 return 0;
450         }
451
452         if (!PyErr_Occurred()) {
453                 PyErr_Format(PyExc_RuntimeError,
454                              "%s read, user has become invalid",
455                              Py_TYPE(self)->tp_name);
456         }
457         return -1;
458 }
459
460 int _BaseMathObject_WriteCallback(BaseMathObject *self)
461 {
462         Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
463         if (LIKELY(cb->set(self, self->cb_subtype) != -1)) {
464                 return 0;
465         }
466
467         if (!PyErr_Occurred()) {
468                 PyErr_Format(PyExc_RuntimeError,
469                              "%s write, user has become invalid",
470                              Py_TYPE(self)->tp_name);
471         }
472         return -1;
473 }
474
475 int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
476 {
477         Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
478         if (LIKELY(cb->get_index(self, self->cb_subtype, index) != -1)) {
479                 return 0;
480         }
481
482         if (!PyErr_Occurred()) {
483                 PyErr_Format(PyExc_RuntimeError,
484                              "%s read index, user has become invalid",
485                              Py_TYPE(self)->tp_name);
486         }
487         return -1;
488 }
489
490 int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
491 {
492         Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
493         if (LIKELY(cb->set_index(self, self->cb_subtype, index) != -1)) {
494                 return 0;
495         }
496
497         if (!PyErr_Occurred()) {
498                 PyErr_Format(PyExc_RuntimeError,
499                              "%s write index, user has become invalid",
500                              Py_TYPE(self)->tp_name);
501         }
502         return -1;
503 }
504
505 void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self)
506 {
507         PyErr_Format(PyExc_TypeError,
508                      "%s is frozen (immutable)",
509                      Py_TYPE(self)->tp_name);
510 }
511
512 void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self)
513 {
514         PyErr_Format(PyExc_TypeError,
515                      "%s is not frozen (mutable), call freeze first",
516                      Py_TYPE(self)->tp_name);
517 }
518
519 /* BaseMathObject generic functions for all mathutils types */
520 char BaseMathObject_owner_doc[] = "The item this is wrapping or None  (read-only).";
521 PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure))
522 {
523         PyObject *ret = self->cb_user ? self->cb_user : Py_None;
524         return Py_INCREF_RET(ret);
525 }
526
527 char BaseMathObject_is_wrapped_doc[] = "True when this object wraps external data (read-only).\n\n:type: boolean";
528 PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *UNUSED(closure))
529 {
530         return PyBool_FromLong((self->flag & BASE_MATH_FLAG_IS_WRAP) != 0);
531 }
532
533 char BaseMathObject_is_frozen_doc[] = "True when this object has been frozen (read-only).\n\n:type: boolean";
534 PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *UNUSED(closure))
535 {
536         return PyBool_FromLong((self->flag & BASE_MATH_FLAG_IS_FROZEN) != 0);
537 }
538
539 char BaseMathObject_freeze_doc[] =
540 ".. function:: freeze()\n"
541 "\n"
542 "   Make this object immutable.\n"
543 "\n"
544 "   After this the object can be hashed, used in dictionaries & sets.\n"
545 "\n"
546 "   :return: An instance of this object.\n"
547 ;
548 PyObject *BaseMathObject_freeze(BaseMathObject *self)
549 {
550         if ((self->flag & BASE_MATH_FLAG_IS_WRAP) || (self->cb_user != NULL)) {
551                 PyErr_SetString(PyExc_TypeError, "Cannot freeze wrapped/owned data");
552                 return NULL;
553         }
554
555         self->flag |= BASE_MATH_FLAG_IS_FROZEN;
556
557         return Py_INCREF_RET((PyObject *)self);
558 }
559
560 int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
561 {
562         Py_VISIT(self->cb_user);
563         return 0;
564 }
565
566 int BaseMathObject_clear(BaseMathObject *self)
567 {
568         Py_CLEAR(self->cb_user);
569         return 0;
570 }
571
572 void BaseMathObject_dealloc(BaseMathObject *self)
573 {
574         /* only free non wrapped */
575         if ((self->flag & BASE_MATH_FLAG_IS_WRAP) == 0) {
576                 PyMem_Free(self->data);
577         }
578
579         if (self->cb_user) {
580                 PyObject_GC_UnTrack(self);
581                 BaseMathObject_clear(self);
582         }
583
584         Py_TYPE(self)->tp_free(self); // PyObject_DEL(self); // breaks subtypes
585 }
586
587 /*----------------------------MODULE INIT-------------------------*/
588 static struct PyMethodDef M_Mathutils_methods[] = {
589         {NULL, NULL, 0, NULL}
590 };
591
592 static struct PyModuleDef M_Mathutils_module_def = {
593         PyModuleDef_HEAD_INIT,
594         "mathutils",  /* m_name */
595         M_Mathutils_doc,  /* m_doc */
596         0,  /* m_size */
597         M_Mathutils_methods,  /* m_methods */
598         NULL,  /* m_reload */
599         NULL,  /* m_traverse */
600         NULL,  /* m_clear */
601         NULL,  /* m_free */
602 };
603
604
605 /* submodules only */
606 #include "mathutils_geometry.h"
607 #include "mathutils_interpolate.h"
608 #ifndef MATH_STANDALONE
609 #  include "mathutils_bvhtree.h"
610 #  include "mathutils_kdtree.h"
611 #  include "mathutils_noise.h"
612 #endif
613
614 PyMODINIT_FUNC PyInit_mathutils(void)
615 {
616         PyObject *mod;
617         PyObject *submodule;
618         PyObject *sys_modules = PyThreadState_GET()->interp->modules;
619
620         if (PyType_Ready(&vector_Type) < 0)
621                 return NULL;
622         if (PyType_Ready(&matrix_Type) < 0)
623                 return NULL;
624         if (PyType_Ready(&matrix_access_Type) < 0)
625                 return NULL;
626         if (PyType_Ready(&euler_Type) < 0)
627                 return NULL;
628         if (PyType_Ready(&quaternion_Type) < 0)
629                 return NULL;
630         if (PyType_Ready(&color_Type) < 0)
631                 return NULL;
632
633         mod = PyModule_Create(&M_Mathutils_module_def);
634
635         /* each type has its own new() function */
636         PyModule_AddObject(mod, vector_Type.tp_name,     (PyObject *)&vector_Type);
637         PyModule_AddObject(mod, matrix_Type.tp_name,     (PyObject *)&matrix_Type);
638         PyModule_AddObject(mod, euler_Type.tp_name,      (PyObject *)&euler_Type);
639         PyModule_AddObject(mod, quaternion_Type.tp_name, (PyObject *)&quaternion_Type);
640         PyModule_AddObject(mod, color_Type.tp_name,      (PyObject *)&color_Type);
641
642         /* submodule */
643         PyModule_AddObject(mod, "geometry",       (submodule = PyInit_mathutils_geometry()));
644         /* XXX, python doesnt do imports with this usefully yet
645          * 'from mathutils.geometry import PolyFill'
646          * ...fails without this. */
647         PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
648         Py_INCREF(submodule);
649
650         PyModule_AddObject(mod, "interpolate",    (submodule = PyInit_mathutils_interpolate()));
651         /* XXX, python doesnt do imports with this usefully yet
652          * 'from mathutils.geometry import PolyFill'
653          * ...fails without this. */
654         PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
655         Py_INCREF(submodule);
656
657 #ifndef MATH_STANDALONE
658         /* Noise submodule */
659         PyModule_AddObject(mod, "noise", (submodule = PyInit_mathutils_noise()));
660         PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
661         Py_INCREF(submodule);
662
663         /* BVHTree submodule */
664         PyModule_AddObject(mod, "bvhtree", (submodule = PyInit_mathutils_bvhtree()));
665         PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
666         Py_INCREF(submodule);
667
668         /* KDTree submodule */
669         PyModule_AddObject(mod, "kdtree", (submodule = PyInit_mathutils_kdtree()));
670         PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
671         Py_INCREF(submodule);
672 #endif
673
674         mathutils_matrix_row_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_row_cb);
675         mathutils_matrix_col_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_col_cb);
676         mathutils_matrix_translation_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_translation_cb);
677
678         return mod;
679 }