diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-06-23 17:34:45 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-06-23 17:34:45 +0400 |
commit | eb22a7b2102cceb432e3545cd342956e92873a49 (patch) | |
tree | c87ad1de81cc216403568b44f289ff9af3a2ae9e /source | |
parent | bf74f105bc5ec98980fa087347203244750fb669 (diff) |
PyRNA API support for matrix types as Mathutils matrix (with callbacks) rather then a generic rna sequence of floats.
Any 3x3 or 4x4 rna matrix will automatically be returned as a Mathutils matrix.
This makes useful stuff like multiplying a vector location by an object matrix possible.
ob = bpy.data.scenes[0].objects[0]
print (ob.data.verts[0].co * ob.matrix)
Also added mathutils matrix types to the BGE GameObject.localOrientation, worldOrientation
* MT_Matrix3x3 added getValue3x3 and setValue3x3, assumed a 4x3 float array.
* KX_GameObject.cpp convenience functions NodeSetGlobalOrientation, NodeGetLocalOrientation, NodeGetLocalScaling, NodeGetLocalPosition.
* 2.5 python api now initializes modules BGL, Mathutils and Geometry
* modules py3 PyModuleDef's use PyModuleDef_HEAD_INIT, rather then {}, was making msvc fail to build.
* added macros for Vector_ReadCallback, Vector_WriteCallback etc. to check if the callback pointer is set before calling the function.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/generic/BGL.c | 2 | ||||
-rw-r--r-- | source/blender/python/generic/Geometry.c | 2 | ||||
-rw-r--r-- | source/blender/python/generic/Mathutils.c | 114 | ||||
-rw-r--r-- | source/blender/python/generic/Mathutils.h | 29 | ||||
-rw-r--r-- | source/blender/python/generic/matrix.c | 224 | ||||
-rw-r--r-- | source/blender/python/generic/matrix.h | 21 | ||||
-rw-r--r-- | source/blender/python/generic/quat.c | 3 | ||||
-rw-r--r-- | source/blender/python/generic/vector.c | 43 | ||||
-rw-r--r-- | source/blender/python/generic/vector.h | 6 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 13 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 64 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_GameObject.cpp | 140 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_GameObject.h | 21 |
13 files changed, 510 insertions, 172 deletions
diff --git a/source/blender/python/generic/BGL.c b/source/blender/python/generic/BGL.c index 360fcbd1c6b..a90fabd3586 100644 --- a/source/blender/python/generic/BGL.c +++ b/source/blender/python/generic/BGL.c @@ -1087,7 +1087,7 @@ static struct PyMethodDef BGL_methods[] = { #if (PY_VERSION_HEX >= 0x03000000) static struct PyModuleDef BGL_module_def = { - {}, /* m_base */ + PyModuleDef_HEAD_INIT, "BGL", /* m_name */ 0, /* m_doc */ 0, /* m_size */ diff --git a/source/blender/python/generic/Geometry.c b/source/blender/python/generic/Geometry.c index f1662d7655d..ec76675f652 100644 --- a/source/blender/python/generic/Geometry.c +++ b/source/blender/python/generic/Geometry.c @@ -80,7 +80,7 @@ struct PyMethodDef M_Geometry_methods[] = { #if (PY_VERSION_HEX >= 0x03000000) static struct PyModuleDef M_Geometry_module_def = { - {}, /* m_base */ + PyModuleDef_HEAD_INIT, "Geometry", /* m_name */ M_Geometry_doc, /* m_doc */ 0, /* m_size */ diff --git a/source/blender/python/generic/Mathutils.c b/source/blender/python/generic/Mathutils.c index fa85d031afc..cd9705236d7 100644 --- a/source/blender/python/generic/Mathutils.c +++ b/source/blender/python/generic/Mathutils.c @@ -96,7 +96,7 @@ struct PyMethodDef M_Mathutils_methods[] = { #if (PY_VERSION_HEX >= 0x03000000) static struct PyModuleDef M_Mathutils_module_def = { - {}, /* m_base */ + PyModuleDef_HEAD_INIT, "Mathutils", /* m_name */ M_Mathutils_doc, /* m_doc */ 0, /* m_size */ @@ -137,6 +137,8 @@ PyObject *Mathutils_Init(const char *from) PyModule_AddObject( submodule, "Euler", (PyObject *)&euler_Type ); PyModule_AddObject( submodule, "Quaternion", (PyObject *)&quaternion_Type ); + mathutils_matrix_vector_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_vector_cb); + return (submodule); } @@ -1164,62 +1166,76 @@ int Mathutils_RegisterCallback(Mathutils_Callback *cb) return i; } -int Vector_ReadCallback(VectorObject *self) { - if(self->user) { - Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; - if(cb->get(self->user, self->subtype, self->vec)) { - return 1; - } - else { - PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); - return 0; - } +/* use macros to check for NULL */ +int _Vector_ReadCallback(VectorObject *self) +{ + Mathutils_Callback *cb= mathutils_callbacks[self->cb_type]; + if(cb->get(self->cb_user, self->cb_subtype, self->vec)) { + return 1; + } + else { + PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); + return 0; } - - return 1; /* no user continue silently */ } -int Vector_WriteCallback(VectorObject *self) { - if(self->user) { - Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; - if(cb->set(self->user, self->subtype, self->vec)) { - return 1; - } - else { - PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); - return 0; - } +int _Vector_WriteCallback(VectorObject *self) +{ + Mathutils_Callback *cb= mathutils_callbacks[self->cb_type]; + if(cb->set(self->cb_user, self->cb_subtype, self->vec)) { + return 1; + } + else { + PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); + return 0; } - - return 1; /* no user continue silently */ } -int Vector_ReadIndexCallback(VectorObject *self, int index) { - if(self->user) { - Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; - if(cb->get_index(self->user, self->subtype, self->vec, index)) { - return 1; - } - else { - PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); - return 0; - } +int _Vector_ReadIndexCallback(VectorObject *self, int index) +{ + Mathutils_Callback *cb= mathutils_callbacks[self->cb_type]; + if(cb->get_index(self->cb_user, self->cb_subtype, self->vec, index)) { + return 1; + } + else { + PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); + return 0; } - - return 1; /* no user continue silently */ } -int Vector_WriteIndexCallback(VectorObject *self, int index) { - if(self->user) { - Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; - if(cb->set_index(self->user, self->subtype, self->vec, index)) { - return 1; - } - else { - PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); - return 0; - } +int _Vector_WriteIndexCallback(VectorObject *self, int index) +{ + Mathutils_Callback *cb= mathutils_callbacks[self->cb_type]; + if(cb->set_index(self->cb_user, self->cb_subtype, self->vec, index)) { + return 1; + } + else { + PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); + return 0; + } +} + +/* matrix callbacks */ +int _Matrix_ReadCallback(MatrixObject *self) +{ + Mathutils_Callback *cb= mathutils_callbacks[self->cb_type]; + if(cb->get(self->cb_user, self->cb_subtype, self->contigPtr)) { + return 1; + } + else { + PyErr_SetString(PyExc_SystemError, "Matrix user has become invalid"); + return 0; + } +} + +int _Matrix_WriteCallback(MatrixObject *self) +{ + Mathutils_Callback *cb= mathutils_callbacks[self->cb_type]; + if(cb->set(self->cb_user, self->cb_subtype, self->contigPtr)) { + return 1; + } + else { + PyErr_SetString(PyExc_SystemError, "Matrix user has become invalid"); + return 0; } - - return 1; /* no user continue silently */ } diff --git a/source/blender/python/generic/Mathutils.h b/source/blender/python/generic/Mathutils.h index 6c769c7729b..a89b779ecbb 100644 --- a/source/blender/python/generic/Mathutils.h +++ b/source/blender/python/generic/Mathutils.h @@ -67,17 +67,30 @@ int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps); typedef struct Mathutils_Callback Mathutils_Callback; struct Mathutils_Callback { int (*check)(PyObject *user); /* checks the user is still valid */ - int (*get)(PyObject *user, int subtype, float *vec_from); /* gets the vector from the user */ - int (*set)(PyObject *user, int subtype, float *vec_to); /* sets the users vector values once the vector is modified */ - int (*get_index)(PyObject *user, int subtype, float *vec_from, int index); /* same as above but only for an index */ - int (*set_index)(PyObject *user, int subtype, float *vec_to, int index); /* same as above but only for an index */ + int (*get)(PyObject *user, int subtype, float *from); /* gets the vector from the user */ + int (*set)(PyObject *user, int subtype, float *to); /* sets the users vector values once the vector is modified */ + int (*get_index)(PyObject *user, int subtype, float *from,int index); /* same as above but only for an index */ + int (*set_index)(PyObject *user, int subtype, float *to, int index); /* same as above but only for an index */ }; int Mathutils_RegisterCallback(Mathutils_Callback *cb); -int Vector_ReadCallback(VectorObject *self); -int Vector_WriteCallback(VectorObject *self); -int Vector_ReadIndexCallback(VectorObject *self, int index); -int Vector_WriteIndexCallback(VectorObject *self, int index); +int _Vector_ReadCallback(VectorObject *self); +int _Vector_WriteCallback(VectorObject *self); +int _Vector_ReadIndexCallback(VectorObject *self, int index); +int _Vector_WriteIndexCallback(VectorObject *self, int index); + +/* since this is called so often avoid where possible */ +#define Vector_ReadCallback(_self) (((_self)->cb_user ? _Vector_ReadCallback(_self):1)) +#define Vector_WriteCallback(_self) (((_self)->cb_user ?_Vector_WriteCallback(_self):1)) +#define Vector_ReadIndexCallback(_self, _index) (((_self)->cb_user ? _Vector_ReadIndexCallback(_self, _index):1)) +#define Vector_WriteIndexCallback(_self, _index) (((_self)->cb_user ? _Vector_WriteIndexCallback(_self, _index):1)) + + +int _Matrix_ReadCallback(MatrixObject *self); +int _Matrix_WriteCallback(MatrixObject *self); + +#define Matrix_ReadCallback(_self) (((_self)->cb_user ?_Matrix_ReadCallback(_self):1)) +#define Matrix_WriteCallback(_self) (((_self)->cb_user ?_Matrix_WriteCallback(_self):1)) #endif /* EXPP_Mathutils_H */ diff --git a/source/blender/python/generic/matrix.c b/source/blender/python/generic/matrix.c index c1e36cf7c56..db5b4ab08bf 100644 --- a/source/blender/python/generic/matrix.c +++ b/source/blender/python/generic/matrix.c @@ -33,6 +33,69 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec); /* utility func */ + +/* matrix vector callbacks */ +int mathutils_matrix_vector_cb_index= -1; + +static int mathutils_matrix_vector_check(MatrixObject *self) +{ + return Matrix_ReadCallback(self); +} + +static int mathutils_matrix_vector_get(MatrixObject *self, int subtype, float *vec_from) +{ + int i; + if(!Matrix_ReadCallback(self)) + return 0; + + for(i=0; i<self->colSize; i++) + vec_from[i]= self->matrix[subtype][i]; + + return 1; +} + +static int mathutils_matrix_vector_set(MatrixObject *self, int subtype, float *vec_to) +{ + int i; + if(!Matrix_ReadCallback(self)) + return 0; + + for(i=0; i<self->colSize; i++) + self->matrix[subtype][i]= vec_to[i]; + + Matrix_WriteCallback(self); + return 1; +} + +static int mathutils_matrix_vector_get_index(MatrixObject *self, int subtype, float *vec_from, int index) +{ + if(!Matrix_ReadCallback(self)) + return 0; + + vec_from[index]= self->matrix[subtype][index]; + return 1; +} + +static int mathutils_matrix_vector_set_index(MatrixObject *self, int subtype, float *vec_to, int index) +{ + if(!Matrix_ReadCallback(self)) + return 0; + + self->matrix[subtype][index]= vec_to[index]; + + Matrix_WriteCallback(self); + return 1; +} + +Mathutils_Callback mathutils_matrix_vector_cb = { + mathutils_matrix_vector_check, + mathutils_matrix_vector_get, + mathutils_matrix_vector_set, + mathutils_matrix_vector_get_index, + mathutils_matrix_vector_set_index +}; +/* matrix vector callbacks, this is so you can do matrix[i][j] = val */ + /*-------------------------DOC STRINGS ---------------------------*/ static char Matrix_Zero_doc[] = "() - set all values in the matrix to 0"; static char Matrix_Identity_doc[] = "() - set the square matrix to it's identity matrix"; @@ -101,6 +164,8 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) argObject = PyTuple_GET_ITEM(args, 0); if(MatrixObject_Check(argObject)){ mat = (MatrixObject*)argObject; + if(!Matrix_ReadCallback(mat)) + return NULL; argSize = mat->rowSize; //rows seqSize = mat->colSize; //col @@ -160,6 +225,9 @@ static PyObject *Matrix_toQuat(MatrixObject * self) { float quat[4]; + if(!Matrix_ReadCallback(self)) + return NULL; + /*must be 3-4 cols, 3-4 rows, square matrix*/ if(self->colSize < 3 || self->rowSize < 3 || (self->colSize != self->rowSize)) { PyErr_SetString(PyExc_AttributeError, "Matrix.toQuat(): inappropriate matrix size - expects 3x3 or 4x4 matrix"); @@ -180,6 +248,9 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args) EulerObject *eul_compat = NULL; int x; + if(!Matrix_ReadCallback(self)) + return NULL; + if(!PyArg_ParseTuple(args, "|O!:toEuler", &euler_Type, &eul_compat)) return NULL; @@ -215,17 +286,20 @@ PyObject *Matrix_Resize4x4(MatrixObject * self) { int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index; - if(self->data.blend_data){ - PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - only python matrices"); + if(self->wrapped==Py_WRAP){ + PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - make a copy and resize that"); return NULL; } - - self->data.py_data = PyMem_Realloc(self->data.py_data, (sizeof(float) * 16)); - if(self->data.py_data == NULL) { + if(self->cb_user){ + PyErr_SetString(PyExc_TypeError, "cannot resize owned data - make a copy and resize that"); + return NULL; + } + + self->contigPtr = PyMem_Realloc(self->contigPtr, (sizeof(float) * 16)); + if(self->contigPtr == NULL) { PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space"); return NULL; } - self->contigPtr = self->data.py_data; /*force*/ self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4)); if(self->matrix == NULL) { PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space"); @@ -268,7 +342,10 @@ PyObject *Matrix_Resize4x4(MatrixObject * self) PyObject *Matrix_TranslationPart(MatrixObject * self) { float vec[4]; - + + if(!Matrix_ReadCallback(self)) + return NULL; + if(self->colSize < 3 || self->rowSize < 4){ PyErr_SetString(PyExc_AttributeError, "Matrix.translationPart: inappropriate matrix size"); return NULL; @@ -286,6 +363,9 @@ PyObject *Matrix_RotationPart(MatrixObject * self) float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; + if(!Matrix_ReadCallback(self)) + return NULL; + if(self->colSize < 3 || self->rowSize < 3){ PyErr_SetString(PyExc_AttributeError, "Matrix.rotationPart: inappropriate matrix size\n"); return NULL; @@ -309,6 +389,9 @@ PyObject *Matrix_scalePart(MatrixObject * self) float scale[3], rot[3]; float mat[3][3], imat[3][3], tmat[3][3]; + if(!Matrix_ReadCallback(self)) + return NULL; + /*must be 3-4 cols, 3-4 rows, square matrix*/ if(self->colSize == 4 && self->rowSize == 4) Mat3CpyMat4(mat, (float (*)[4])*self->matrix); @@ -339,6 +422,9 @@ PyObject *Matrix_Invert(MatrixObject * self) float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; + if(!Matrix_ReadCallback(self)) + return NULL; + if(self->rowSize != self->colSize){ PyErr_SetString(PyExc_AttributeError, "Matrix.invert(ed): only square matrices are supported"); return NULL; @@ -379,6 +465,7 @@ PyObject *Matrix_Invert(MatrixObject * self) return NULL; } + Matrix_WriteCallback(self); Py_INCREF(self); return (PyObject *)self; } @@ -389,6 +476,9 @@ PyObject *Matrix_Determinant(MatrixObject * self) { float det = 0.0f; + if(!Matrix_ReadCallback(self)) + return NULL; + if(self->rowSize != self->colSize){ PyErr_SetString(PyExc_AttributeError, "Matrix.determinant: only square matrices are supported"); return NULL; @@ -414,6 +504,9 @@ PyObject *Matrix_Transpose(MatrixObject * self) { float t = 0.0f; + if(!Matrix_ReadCallback(self)) + return NULL; + if(self->rowSize != self->colSize){ PyErr_SetString(PyExc_AttributeError, "Matrix.transpose(d): only square matrices are supported"); return NULL; @@ -429,6 +522,7 @@ PyObject *Matrix_Transpose(MatrixObject * self) Mat4Transp((float (*)[4])*self->matrix); } + Matrix_WriteCallback(self); Py_INCREF(self); return (PyObject *)self; } @@ -438,18 +532,25 @@ PyObject *Matrix_Transpose(MatrixObject * self) PyObject *Matrix_Zero(MatrixObject * self) { int row, col; - + for(row = 0; row < self->rowSize; row++) { for(col = 0; col < self->colSize; col++) { self->matrix[row][col] = 0.0f; } } + + if(!Matrix_WriteCallback(self)) + return NULL; + Py_INCREF(self); return (PyObject *)self; } /*---------------------------Matrix.identity(() ------------------*/ PyObject *Matrix_Identity(MatrixObject * self) { + if(!Matrix_ReadCallback(self)) + return NULL; + if(self->rowSize != self->colSize){ PyErr_SetString(PyExc_AttributeError, "Matrix.identity: only square matrices are supported\n"); return NULL; @@ -466,6 +567,9 @@ PyObject *Matrix_Identity(MatrixObject * self) Mat4One((float (*)[4]) *self->matrix); } + if(!Matrix_WriteCallback(self)) + return NULL; + Py_INCREF(self); return (PyObject *)self; } @@ -473,6 +577,9 @@ PyObject *Matrix_Identity(MatrixObject * self) /*---------------------------Matrix.inverted() ------------------*/ PyObject *Matrix_copy(MatrixObject * self) { + if(!Matrix_ReadCallback(self)) + return NULL; + return (PyObject*)(MatrixObject*)newMatrixObject((float (*))*self->matrix, self->rowSize, self->colSize, Py_NEW); } @@ -482,9 +589,10 @@ static void Matrix_dealloc(MatrixObject * self) { PyMem_Free(self->matrix); /*only free py_data*/ - if(self->data.py_data){ - PyMem_Free(self->data.py_data); - } + if(self->wrapped==Py_WRAP) + PyMem_Free(self->contigPtr); + + Py_XDECREF(self->cb_user); PyObject_DEL(self); } @@ -495,6 +603,9 @@ static PyObject *Matrix_repr(MatrixObject * self) int x, y; char buffer[48], str[1024]; + if(!Matrix_ReadCallback(self)) + return NULL; + BLI_strncpy(str,"",1024); for(x = 0; x < self->rowSize; x++){ sprintf(buffer, "["); @@ -531,6 +642,9 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa matA = (MatrixObject*)objectA; matB = (MatrixObject*)objectB; + if(!Matrix_ReadCallback(matA) || !Matrix_ReadCallback(matB)) + return NULL; + if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){ if (comparison_type == Py_NE){ Py_RETURN_TRUE; @@ -578,11 +692,14 @@ static int Matrix_len(MatrixObject * self) the wrapped vector gives direct access to the matrix data*/ static PyObject *Matrix_item(MatrixObject * self, int i) { + if(!Matrix_ReadCallback(self)) + return NULL; + if(i < 0 || i >= self->rowSize) { PyErr_SetString(PyExc_IndexError, "matrix[attribute]: array index out of range"); return NULL; } - return newVectorObject(self->matrix[i], self->colSize, Py_WRAP); + return newVectorObject_cb((PyObject *)self, self->colSize, mathutils_matrix_vector_cb_index, i); } /*----------------------------object[]------------------------- sequence accessor (set)*/ @@ -592,6 +709,9 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob) float vec[4]; PyObject *m, *f; + if(!Matrix_ReadCallback(self)) + return -1; + if(i >= self->rowSize || i < 0){ PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad row\n"); return -1; @@ -625,6 +745,8 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob) for(y = 0; y < size; y++){ self->matrix[i][y] = vec[y]; } + + Matrix_WriteCallback(self); return 0; }else{ PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: expects a sequence of column size\n"); @@ -638,6 +760,9 @@ static PyObject *Matrix_slice(MatrixObject * self, int begin, int end) PyObject *list = NULL; int count; + + if(!Matrix_ReadCallback(self)) + return NULL; CLAMP(begin, 0, self->rowSize); CLAMP(end, 0, self->rowSize); @@ -646,7 +771,8 @@ static PyObject *Matrix_slice(MatrixObject * self, int begin, int end) list = PyList_New(end - begin); for(count = begin; count < end; count++) { PyList_SetItem(list, count - begin, - newVectorObject(self->matrix[count], self->colSize, Py_WRAP)); + newVectorObject_cb((PyObject *)self, self->colSize, mathutils_matrix_vector_cb_index, count)); + } return list; @@ -661,6 +787,9 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end, PyObject *subseq; PyObject *m; + if(!Matrix_ReadCallback(self)) + return -1; + CLAMP(begin, 0, self->rowSize); CLAMP(end, 0, self->rowSize); begin = MIN2(begin,end); @@ -718,6 +847,8 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end, for(x = 0; x < (size * sub_size); x++){ self->matrix[begin + (int)floor(x / self->colSize)][x % self->colSize] = mat[x]; } + + Matrix_WriteCallback(self); return 0; }else{ PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n"); @@ -740,6 +871,10 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2) PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation...."); return NULL; } + + if(!Matrix_ReadCallback(mat1) || !Matrix_ReadCallback(mat2)) + return NULL; + if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){ PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation"); return NULL; @@ -769,6 +904,10 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2) PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation...."); return NULL; } + + if(!Matrix_ReadCallback(mat1) || !Matrix_ReadCallback(mat2)) + return NULL; + if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){ PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation"); return NULL; @@ -793,8 +932,16 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2) double dot = 0.0f; MatrixObject *mat1 = NULL, *mat2 = NULL; - if(MatrixObject_Check(m1)) mat1 = (MatrixObject*)m1; - if(MatrixObject_Check(m2)) mat2 = (MatrixObject*)m2; + if(MatrixObject_Check(m1)) { + mat1 = (MatrixObject*)m1; + if(!Matrix_ReadCallback(mat1)) + return NULL; + } + if(MatrixObject_Check(m2)) { + mat2 = (MatrixObject*)m2; + if(!Matrix_ReadCallback(mat2)) + return NULL; + } if(mat1 && mat2) { /*MATRIX * MATRIX*/ if(mat1->colSize != mat2->rowSize){ @@ -853,6 +1000,9 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2) } static PyObject* Matrix_inv(MatrixObject *self) { + if(!Matrix_ReadCallback(self)) + return NULL; + return Matrix_Invert(self); } @@ -902,6 +1052,17 @@ static PyObject *Matrix_getColSize( MatrixObject * self, void *type ) return PyLong_FromLong((long) self->colSize); } +static PyObject *Matrix_getOwner( MatrixObject * self, void *type ) +{ + if(self->cb_user==NULL) { + Py_RETURN_NONE; + } + else { + Py_INCREF(self->cb_user); + return self->cb_user; + } +} + static PyObject *Matrix_getWrapped( MatrixObject * self, void *type ) { if (self->wrapped == Py_WRAP) @@ -917,6 +1078,7 @@ static PyGetSetDef Matrix_getseters[] = { {"rowSize", (getter)Matrix_getRowSize, (setter)NULL, "", NULL}, {"colSize", (getter)Matrix_getColSize, (setter)NULL, "", NULL}, {"wrapped", (getter)Matrix_getWrapped, (setter)NULL, "", NULL}, + {"__owner__",(getter)Matrix_getOwner, (setter)NULL, "Read only owner for vectors that depend on another object", NULL}, {NULL,NULL,NULL,NULL,NULL} /* Sentinel */ }; @@ -986,7 +1148,7 @@ self->matrix self->contiguous_ptr (reference to data.xxx) [4] [5] .... -self->matrix[1][1] = self->contiguous_ptr[4] = self->data.xxx_data[4]*/ +self->matrix[1][1] = self->contigPtr[4] */ /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER (i.e. it was allocated elsewhere by MEM_mallocN()) @@ -1004,14 +1166,15 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type) } self = PyObject_NEW(MatrixObject, &matrix_Type); - self->data.blend_data = NULL; - self->data.py_data = NULL; self->rowSize = rowSize; self->colSize = colSize; + + /* init callbacks as NULL */ + self->cb_user= NULL; + self->cb_type= self->cb_subtype= 0; if(type == Py_WRAP){ - self->data.blend_data = mat; - self->contigPtr = self->data.blend_data; + self->contigPtr = mat; /*create pointer array*/ self->matrix = PyMem_Malloc(rowSize * sizeof(float *)); if(self->matrix == NULL) { /*allocation failure*/ @@ -1024,16 +1187,15 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type) } self->wrapped = Py_WRAP; }else if (type == Py_NEW){ - self->data.py_data = PyMem_Malloc(rowSize * colSize * sizeof(float)); - if(self->data.py_data == NULL) { /*allocation failure*/ + self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float)); + if(self->contigPtr == NULL) { /*allocation failure*/ PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n"); return NULL; } - self->contigPtr = self->data.py_data; /*create pointer array*/ self->matrix = PyMem_Malloc(rowSize * sizeof(float *)); if(self->matrix == NULL) { /*allocation failure*/ - PyMem_Free(self->data.py_data); + PyMem_Free(self->contigPtr); PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space"); return NULL; } @@ -1059,6 +1221,18 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type) return (PyObject *) self; } +PyObject *newMatrixObject_cb(PyObject *cb_user, int rowSize, int colSize, int cb_type, int cb_subtype) +{ + MatrixObject *self= (MatrixObject *)newMatrixObject(NULL, rowSize, colSize, Py_NEW); + if(self) { + Py_INCREF(cb_user); + self->cb_user= cb_user; + self->cb_type= (unsigned char)cb_type; + self->cb_subtype= (unsigned char)cb_subtype; + } + return (PyObject *) self; +} + //----------------column_vector_multiplication (internal)--------- //COLUMN VECTOR Multiplication (Matrix X Vector) // [1][2][3] [a] @@ -1071,7 +1245,7 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* double dot = 0.0f; int x, y, z = 0; - if(!Vector_ReadCallback(vec)) + if(!Matrix_ReadCallback(mat) || !Vector_ReadCallback(vec)) return NULL; if(mat->rowSize != vec->size){ diff --git a/source/blender/python/generic/matrix.h b/source/blender/python/generic/matrix.h index 2604d58d0d1..cc3928f1632 100644 --- a/source/blender/python/generic/matrix.h +++ b/source/blender/python/generic/matrix.h @@ -39,15 +39,14 @@ extern PyTypeObject matrix_Type; typedef float **ptRow; typedef struct _Matrix { PyObject_VAR_HEAD - struct{ - float *py_data; /*python managed*/ - float *blend_data; /*blender managed*/ - }data; - ptRow matrix; /*ptr to the contigPtr (accessor)*/ - float *contigPtr; /*1D array of data (alias)*/ - int rowSize; - int colSize; - int wrapped; /*is wrapped data?*/ + ptRow matrix; /*ptr to the contigPtr (accessor)*/ + float* contigPtr; /*1D array of data (alias)*/ + PyObject* cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ + unsigned char rowSize; + unsigned char colSize; + unsigned char wrapped; /*is wrapped data?*/ + unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ + unsigned int cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ } MatrixObject; /*struct data contains a pointer to the actual data that the @@ -57,5 +56,9 @@ blender (stored in blend_data). This is an either/or struct not both*/ /*prototypes*/ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type); +PyObject *newMatrixObject_cb(PyObject *user, int rowSize, int colSize, int cb_type, int cb_subtype); + +extern int mathutils_matrix_vector_cb_index; +extern struct Mathutils_Callback mathutils_matrix_vector_cb; #endif /* EXPP_matrix_H */ diff --git a/source/blender/python/generic/quat.c b/source/blender/python/generic/quat.c index b9ccb478ddd..8a3ded80455 100644 --- a/source/blender/python/generic/quat.c +++ b/source/blender/python/generic/quat.c @@ -75,7 +75,7 @@ static struct PyMethodDef Quaternion_methods[] = { //----------------------------------Mathutils.Quaternion() -------------- static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *listObject = NULL, *n, *q, *f; + PyObject *listObject = NULL, *n, *q; int size, i; float quat[4], scalar; double norm = 0.0f, angle = 0.0f; @@ -159,7 +159,6 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw } quat[i] = scalar; - Py_DECREF(f); Py_DECREF(q); } if(size == 3){ //calculate the quat based on axis/angle diff --git a/source/blender/python/generic/vector.c b/source/blender/python/generic/vector.c index b01a51f442a..dea5bc93898 100644 --- a/source/blender/python/generic/vector.c +++ b/source/blender/python/generic/vector.c @@ -72,8 +72,8 @@ static struct PyMethodDef Vector_methods[] = { {"normalize", (PyCFunction) Vector_Normalize, METH_NOARGS, Vector_Normalize_doc}, {"negate", (PyCFunction) Vector_Negate, METH_NOARGS, Vector_Negate_doc}, {"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc}, - {"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize2D_doc}, - {"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize2D_doc}, + {"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize3D_doc}, + {"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize4D_doc}, {"toTrackQuat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc}, {"reflect", ( PyCFunction ) Vector_Reflect, METH_O, Vector_Reflect_doc}, {"cross", ( PyCFunction ) Vector_Cross, METH_O, Vector_Dot_doc}, @@ -180,7 +180,7 @@ static PyObject *Vector_Resize2D(VectorObject * self) PyErr_SetString(PyExc_TypeError, "vector.resize2d(): cannot resize wrapped data - only python vectors\n"); return NULL; } - if(self->user) { + if(self->cb_user) { PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner"); return NULL; } @@ -203,7 +203,7 @@ static PyObject *Vector_Resize3D(VectorObject * self) PyErr_SetString(PyExc_TypeError, "vector.resize3d(): cannot resize wrapped data - only python vectors\n"); return NULL; } - if(self->user) { + if(self->cb_user) { PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner"); return NULL; } @@ -229,7 +229,7 @@ static PyObject *Vector_Resize4D(VectorObject * self) PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize wrapped data - only python vectors"); return NULL; } - if(self->user) { + if(self->cb_user) { PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner"); return NULL; } @@ -478,10 +478,10 @@ static PyObject *Vector_copy(VectorObject * self) static void Vector_dealloc(VectorObject * self) { /* only free non wrapped */ - if(self->wrapped != Py_WRAP){ + if(self->wrapped != Py_WRAP) PyMem_Free(self->vec); - } - Py_XDECREF(self->user); + + Py_XDECREF(self->cb_user); PyObject_DEL(self); } @@ -845,6 +845,9 @@ static PyObject *Vector_imul(PyObject * v1, PyObject * v2) int x,y, size = vec->size; MatrixObject *mat= (MatrixObject*)v2; + if(!Vector_ReadCallback(mat)) + return NULL; + if(mat->colSize != size){ if(mat->rowSize == 4 && vec->size != 3){ PyErr_SetString(PyExc_AttributeError, "vector * matrix: matrix column size and the vector size must be the same"); @@ -1215,12 +1218,12 @@ static PyObject *Vector_getWrapped( VectorObject * self, void *type ) static PyObject *Vector_getOwner( VectorObject * self, void *type ) { - if(self->user==NULL) { + if(self->cb_user==NULL) { Py_RETURN_NONE; } else { - Py_INCREF(self->user); - return self->user; + Py_INCREF(self->cb_user); + return self->cb_user; } } @@ -1886,6 +1889,10 @@ PyObject *newVectorObject(float *vec, int size, int type) if(size > 4 || size < 2) return NULL; self->size = size; + + /* init callbacks as NULL */ + self->cb_user= NULL; + self->cb_type= self->cb_subtype= 0; if(type == Py_WRAP) { self->vec = vec; @@ -1910,15 +1917,15 @@ PyObject *newVectorObject(float *vec, int size, int type) return (PyObject *) self; } -PyObject *newVectorObject_cb(PyObject *user, int size, int callback_type, int subtype) +PyObject *newVectorObject_cb(PyObject *cb_user, int size, int cb_type, int cb_subtype) { - float dummy[4] = {0.0, 0.0, 0.0, 0.0}; /* the same vector is used because its set each time by the user callback, saves a little ram */ + float dummy[4] = {0.0, 0.0, 0.0, 0.0}; /* dummy init vector, callbacks will be used on access */ VectorObject *self= newVectorObject(dummy, size, Py_NEW); if(self) { - Py_INCREF(user); - self->user= user; - self->callback_type = (unsigned char)callback_type; - self->subtype = (unsigned char)subtype; + Py_INCREF(cb_user); + self->cb_user= cb_user; + self->cb_type= (unsigned char)cb_type; + self->cb_subtype= (unsigned char)cb_subtype; } return self; @@ -1945,7 +1952,7 @@ static PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat } } - if(!Vector_ReadCallback(vec)) + if(!Vector_ReadCallback(vec) || !Matrix_ReadCallback(mat)) return NULL; for(x = 0; x < vec_size; x++){ diff --git a/source/blender/python/generic/vector.h b/source/blender/python/generic/vector.h index c00be8d71b7..82dbf13b9aa 100644 --- a/source/blender/python/generic/vector.h +++ b/source/blender/python/generic/vector.h @@ -40,11 +40,11 @@ extern PyTypeObject vector_Type; typedef struct { PyObject_VAR_HEAD float *vec; /*1D array of data (alias), wrapped status depends on wrapped status */ - PyObject *user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ + PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ unsigned char size; /* vec size 2,3 or 4 */ unsigned char wrapped; /* wrapped data type? */ - unsigned char callback_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ - unsigned char subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ + unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ + unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ } VectorObject; diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 559ed537757..8b5ad36f349 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -37,6 +37,11 @@ #include "BPY_extern.h" #include "../generic/bpy_internal_import.h" // our own imports +/* external util modukes */ + +#include "../generic/Mathutils.h" +#include "../generic/Geometry.h" +#include "../generic/BGL.h" void BPY_free_compiled_text( struct Text *text ) @@ -61,11 +66,17 @@ static void bpy_init_modules( void ) PyModule_AddObject( mod, "types", BPY_rna_types() ); PyModule_AddObject( mod, "props", BPY_rna_props() ); PyModule_AddObject( mod, "ops", BPY_operator_module() ); - PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experemental, consider this a test, especially PyCObject is not meant to be perminant + PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent /* add the module so we can import it */ PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod); Py_DECREF(mod); + + + /* stand alone utility modules not related to blender directly */ + Geometry_Init("Geometry"); + Mathutils_Init("Mathutils"); + BGL_Init("BGL"); } #if (PY_VERSION_HEX < 0x02050000) diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 93a8af8b177..70ccd4f58c9 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -44,9 +44,10 @@ #ifdef USE_MATHUTILS #include "../generic/Mathutils.h" /* so we can have mathutils callbacks */ +/* bpyrna vector callbacks */ static int mathutils_rna_vector_cb_index= -1; /* index for our callbacks */ -static int mathutils_rna_vector_check(BPy_PropertyRNA *self) +static int mathutils_rna_generic_check(BPy_PropertyRNA *self) { return self->prop?1:0; } @@ -88,13 +89,42 @@ static int mathutils_rna_vector_set_index(BPy_PropertyRNA *self, int subtype, fl } Mathutils_Callback mathutils_rna_vector_cb = { - mathutils_rna_vector_check, + mathutils_rna_generic_check, mathutils_rna_vector_get, mathutils_rna_vector_set, mathutils_rna_vector_get_index, mathutils_rna_vector_set_index }; +/* bpyrna matrix callbacks */ +static int mathutils_rna_matrix_cb_index= -1; /* index for our callbacks */ + +static int mathutils_rna_matrix_get(BPy_PropertyRNA *self, int subtype, float *mat_from) +{ + if(self->prop==NULL) + return 0; + + RNA_property_float_get_array(&self->ptr, self->prop, mat_from); + return 1; +} + +static int mathutils_rna_matrix_set(BPy_PropertyRNA *self, int subtype, float *mat_to) +{ + if(self->prop==NULL) + return 0; + + RNA_property_float_set_array(&self->ptr, self->prop, mat_to); + return 1; +} + +Mathutils_Callback mathutils_rna_matrix_cb = { + mathutils_rna_generic_check, + mathutils_rna_matrix_get, + mathutils_rna_matrix_set, + NULL, + NULL +}; + #endif static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b ) @@ -206,15 +236,28 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) #ifdef USE_MATHUTILS /* return a mathutils vector where possible */ - if( RNA_property_type(prop)==PROP_FLOAT && - RNA_property_subtype(prop)==PROP_VECTOR && - len>=2 && len <= 4 ) - { - PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_vector_cb_index, 0); - Py_DECREF(ret); /* the vector owns now */ - - ret= vec_cb; /* return the vector instead */ + if(RNA_property_type(prop)==PROP_FLOAT) { + if(RNA_property_subtype(prop)==PROP_VECTOR) { + if(len>=2 && len <= 4) { + PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_vector_cb_index, 0); + Py_DECREF(ret); /* the vector owns now */ + ret= vec_cb; /* return the vector instead */ + } + } + else if(RNA_property_subtype(prop)==PROP_MATRIX) { + if(len==16) { + PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_vector_cb_index, 0); + Py_DECREF(ret); /* the matrix owns now */ + ret= mat_cb; /* return the matrix instead */ + } + else if (len==9) { + PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_vector_cb_index, 0); + Py_DECREF(ret); /* the matrix owns now */ + ret= mat_cb; /* return the matrix instead */ + } + } } + #endif return ret; @@ -1749,6 +1792,7 @@ PyObject *BPY_rna_module( void ) #ifdef USE_MATHUTILS // register mathutils callbacks, ok to run more then once. mathutils_rna_vector_cb_index= Mathutils_RegisterCallback(&mathutils_rna_vector_cb); + mathutils_rna_matrix_cb_index= Mathutils_RegisterCallback(&mathutils_rna_matrix_cb); #endif /* This can't be set in the pytype struct because some compilers complain */ diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp index 081549db686..a3b2ba79e11 100644 --- a/source/gameengine/Ketsji/KX_GameObject.cpp +++ b/source/gameengine/Ketsji/KX_GameObject.cpp @@ -983,7 +983,17 @@ void KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot) GetSGNode()->SetLocalOrientation(rot); } +void KX_GameObject::NodeSetGlobalOrientation(const MT_Matrix3x3& rot) +{ + // check on valid node in case a python controller holds a reference to a deleted object + if (!GetSGNode()) + return; + if (GetSGNode()->GetSGParent()) + GetSGNode()->SetLocalOrientation(GetSGNode()->GetSGParent()->GetWorldOrientation().inverse()*rot); + else + GetSGNode()->SetLocalOrientation(rot); +} void KX_GameObject::NodeSetLocalScale(const MT_Vector3& scale) { @@ -1062,7 +1072,13 @@ const MT_Matrix3x3& KX_GameObject::NodeGetWorldOrientation() const return GetSGNode()->GetWorldOrientation(); } - +const MT_Matrix3x3& KX_GameObject::NodeGetLocalOrientation() const +{ + // check on valid node in case a python controller holds a reference to a deleted object + if (!GetSGNode()) + return dummy_orientation; + return GetSGNode()->GetLocalOrientation(); +} const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const { @@ -1073,7 +1089,14 @@ const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const return GetSGNode()->GetWorldScaling(); } +const MT_Vector3& KX_GameObject::NodeGetLocalScaling() const +{ + // check on valid node in case a python controller holds a reference to a deleted object + if (!GetSGNode()) + return dummy_scaling; + return GetSGNode()->GetLocalScale(); +} const MT_Point3& KX_GameObject::NodeGetWorldPosition() const { @@ -1084,6 +1107,16 @@ const MT_Point3& KX_GameObject::NodeGetWorldPosition() const return dummy_point; } +const MT_Point3& KX_GameObject::NodeGetLocalPosition() const +{ + // check on valid node in case a python controller holds a reference to a deleted object + if (GetSGNode()) + return GetSGNode()->GetLocalPosition(); + else + return dummy_point; +} + + /* Suspend/ resume: for the dynamic behaviour, there is a simple * method. For the residual motion, there is not. I wonder what the * correct solution is for Sumo. Remove from the motion-update tree? @@ -1164,10 +1197,9 @@ extern "C" { #define MATHUTILS_VEC_CB_SCALE_GLOBAL 4 #define MATHUTILS_VEC_CB_INERTIA_LOCAL 5 - static int mathutils_kxgameob_vector_cb_index= -1; /* index for our callbacks */ -static int mathutils_kxgameob_vector_check(PyObject *self_v) +static int mathutils_kxgameob_generic_check(PyObject *self_v) { KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v); if(self==NULL) @@ -1184,26 +1216,21 @@ static int mathutils_kxgameob_vector_get(PyObject *self_v, int subtype, float *v switch(subtype) { case MATHUTILS_VEC_CB_POS_LOCAL: - if(!self->GetSGNode()) return 0; - self->GetSGNode()->GetLocalPosition().getValue(vec_from); + self->NodeGetLocalPosition().getValue(vec_from); break; case MATHUTILS_VEC_CB_POS_GLOBAL: - if(!self->GetSGNode()) return 0; - self->GetSGNode()->GetWorldPosition().getValue(vec_from); + self->NodeGetWorldPosition().getValue(vec_from); break; case MATHUTILS_VEC_CB_SCALE_LOCAL: - if(!self->GetSGNode()) return 0; - self->GetSGNode()->GetLocalScale().getValue(vec_from); + self->NodeGetLocalScaling().getValue(vec_from); break; case MATHUTILS_VEC_CB_SCALE_GLOBAL: self->NodeGetWorldScaling().getValue(vec_from); break; case MATHUTILS_VEC_CB_INERTIA_LOCAL: - if(!self->GetSGNode()) return 0; + if(!self->GetPhysicsController()) return 0; self->GetPhysicsController()->GetLocalInertia().getValue(vec_from); break; - - } return 1; @@ -1215,11 +1242,6 @@ static int mathutils_kxgameob_vector_set(PyObject *self_v, int subtype, float *v if(self==NULL) return 0; - /* first */ - SG_Node* sg = self->GetSGNode(); - if(sg==NULL) - return 0; - switch(subtype) { case MATHUTILS_VEC_CB_POS_LOCAL: self->NodeSetLocalPosition(MT_Point3(vec_to)); @@ -1269,18 +1291,75 @@ static int mathutils_kxgameob_vector_set_index(PyObject *self_v, int subtype, fl } Mathutils_Callback mathutils_kxgameob_vector_cb = { - mathutils_kxgameob_vector_check, + mathutils_kxgameob_generic_check, mathutils_kxgameob_vector_get, mathutils_kxgameob_vector_set, mathutils_kxgameob_vector_get_index, mathutils_kxgameob_vector_set_index }; +/* Matrix */ +#define MATHUTILS_MAT_CB_ORI_LOCAL 1 +#define MATHUTILS_MAT_CB_ORI_GLOBAL 2 + +static int mathutils_kxgameob_matrix_cb_index= -1; /* index for our callbacks */ + +static int mathutils_kxgameob_matrix_get(PyObject *self_v, int subtype, float *mat_from) +{ + KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v); + if(self==NULL) + return 0; + + switch(subtype) { + case MATHUTILS_MAT_CB_ORI_LOCAL: + self->NodeGetLocalOrientation().getValue3x3(mat_from); + break; + case MATHUTILS_MAT_CB_ORI_GLOBAL: + self->NodeGetWorldOrientation().getValue3x3(mat_from); + break; + } + + return 1; +} + + +static int mathutils_kxgameob_matrix_set(PyObject *self_v, int subtype, float *mat_to) +{ + KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v); + if(self==NULL) + return 0; + + MT_Matrix3x3 mat3x3; + switch(subtype) { + case MATHUTILS_MAT_CB_ORI_LOCAL: + mat3x3.setValue3x3(mat_to); + self->NodeSetLocalOrientation(mat3x3); + self->NodeUpdateGS(0.f); + break; + case MATHUTILS_MAT_CB_ORI_GLOBAL: + mat3x3.setValue3x3(mat_to); + self->NodeSetLocalOrientation(mat3x3); + self->NodeUpdateGS(0.f); + break; + } + + return 1; +} + +Mathutils_Callback mathutils_kxgameob_matrix_cb = { + mathutils_kxgameob_generic_check, + mathutils_kxgameob_matrix_get, + mathutils_kxgameob_matrix_set, + NULL, + NULL +}; + void KX_GameObject_Mathutils_Callback_Init(void) { // register mathutils callbacks, ok to run more then once. mathutils_kxgameob_vector_cb_index= Mathutils_RegisterCallback(&mathutils_kxgameob_vector_cb); + mathutils_kxgameob_matrix_cb_index= Mathutils_RegisterCallback(&mathutils_kxgameob_matrix_cb); } #endif // USE_MATHUTILS @@ -1754,10 +1833,7 @@ PyObject* KX_GameObject::pyattr_get_localPosition(void *self_v, const KX_PYATTRI #ifdef USE_MATHUTILS return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_POS_LOCAL); #else - if (self->GetSGNode()) - return PyObjectFrom(self->GetSGNode()->GetLocalPosition()); - else - return PyObjectFrom(dummy_point); + return PyObjectFrom(self->NodeGetLocalPosition()); #endif } @@ -1782,13 +1858,17 @@ PyObject* KX_GameObject::pyattr_get_localInertia(void *self_v, const KX_PYATTRIB if (self->GetPhysicsController()) return PyObjectFrom(self->GetPhysicsController()->GetLocalInertia()); return Py_BuildValue("fff", 0.0f, 0.0f, 0.0f); - #endif +#endif } PyObject* KX_GameObject::pyattr_get_worldOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { +#ifdef USE_MATHUTILS + return newMatrixObject_cb((PyObject *)self_v, 3, 3, mathutils_kxgameob_matrix_cb_index, MATHUTILS_MAT_CB_ORI_GLOBAL); +#else KX_GameObject* self= static_cast<KX_GameObject*>(self_v); return PyObjectFrom(self->NodeGetWorldOrientation()); +#endif } int KX_GameObject::pyattr_set_worldOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) @@ -1813,11 +1893,12 @@ int KX_GameObject::pyattr_set_worldOrientation(void *self_v, const KX_PYATTRIBUT PyObject* KX_GameObject::pyattr_get_localOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { +#ifdef USE_MATHUTILS + return newMatrixObject_cb((PyObject *)self_v, 3, 3, mathutils_kxgameob_matrix_cb_index, MATHUTILS_MAT_CB_ORI_LOCAL); +#else KX_GameObject* self= static_cast<KX_GameObject*>(self_v); - if (self->GetSGNode()) - return PyObjectFrom(self->GetSGNode()->GetLocalOrientation()); - else - return PyObjectFrom(dummy_orientation); + return PyObjectFrom(self->NodeGetLocalOrientation()); +#endif } int KX_GameObject::pyattr_set_localOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) @@ -1850,10 +1931,7 @@ PyObject* KX_GameObject::pyattr_get_localScaling(void *self_v, const KX_PYATTRIB #ifdef USE_MATHUTILS return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_SCALE_LOCAL); #else - if (self->GetSGNode()) - return PyObjectFrom(self->GetSGNode()->GetLocalScale()); - else - return PyObjectFrom(dummy_scaling); + return PyObjectFrom(self->NodeGetLocalScale()); #endif } diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h index b01b0cae641..59285714950 100644 --- a/source/gameengine/Ketsji/KX_GameObject.h +++ b/source/gameengine/Ketsji/KX_GameObject.h @@ -397,6 +397,7 @@ public: void NodeSetLocalPosition(const MT_Point3& trans ); void NodeSetLocalOrientation(const MT_Matrix3x3& rot ); + void NodeSetGlobalOrientation(const MT_Matrix3x3& rot ); void NodeSetLocalScale( const MT_Vector3& scale ); @@ -410,21 +411,13 @@ public: double time ); - const - MT_Matrix3x3& - NodeGetWorldOrientation( - ) const; - - const - MT_Vector3& - NodeGetWorldScaling( - ) const; - - const - MT_Point3& - NodeGetWorldPosition( - ) const; + const MT_Matrix3x3& NodeGetWorldOrientation( ) const; + const MT_Vector3& NodeGetWorldScaling( ) const; + const MT_Point3& NodeGetWorldPosition( ) const; + const MT_Matrix3x3& NodeGetLocalOrientation( ) const; + const MT_Vector3& NodeGetLocalScaling( ) const; + const MT_Point3& NodeGetLocalPosition( ) const; /** * @section scene graph node accessor functions. |