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