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.
This commit is contained in:
Campbell Barton 2009-06-23 13:34:45 +00:00
parent bf74f105bc
commit eb22a7b210
14 changed files with 534 additions and 172 deletions

@ -98,6 +98,18 @@ public:
m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m; m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
} }
void setValue3x3(const float *m) {
m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++;
m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++;
m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
}
void setValue3x3(const double *m) {
m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++;
m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++;
m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
}
void setValue(MT_Scalar xx, MT_Scalar xy, MT_Scalar xz, void setValue(MT_Scalar xx, MT_Scalar xy, MT_Scalar xz,
MT_Scalar yx, MT_Scalar yy, MT_Scalar yz, MT_Scalar yx, MT_Scalar yy, MT_Scalar yz,
MT_Scalar zx, MT_Scalar zy, MT_Scalar zz) { MT_Scalar zx, MT_Scalar zy, MT_Scalar zz) {
@ -194,6 +206,18 @@ public:
*m++ = m_el[0][2]; *m++ = m_el[1][2]; *m++ = m_el[2][2]; *m = 0.0; *m++ = m_el[0][2]; *m++ = m_el[1][2]; *m++ = m_el[2][2]; *m = 0.0;
} }
void getValue3x3(float *m) const {
*m++ = (float) m_el[0][0]; *m++ = (float) m_el[1][0]; *m++ = (float) m_el[2][0];
*m++ = (float) m_el[0][1]; *m++ = (float) m_el[1][1]; *m++ = (float) m_el[2][1];
*m++ = (float) m_el[0][2]; *m++ = (float) m_el[1][2]; *m++ = (float) m_el[2][2];
}
void getValue3x3(double *m) const {
*m++ = m_el[0][0]; *m++ = m_el[1][0]; *m++ = m_el[2][0];
*m++ = m_el[0][1]; *m++ = m_el[1][1]; *m++ = m_el[2][1];
*m++ = m_el[0][2]; *m++ = m_el[1][2]; *m++ = m_el[2][2];
}
MT_Quaternion getRotation() const; MT_Quaternion getRotation() const;
MT_Matrix3x3& operator*=(const MT_Matrix3x3& m); MT_Matrix3x3& operator*=(const MT_Matrix3x3& m);

@ -1087,7 +1087,7 @@ static struct PyMethodDef BGL_methods[] = {
#if (PY_VERSION_HEX >= 0x03000000) #if (PY_VERSION_HEX >= 0x03000000)
static struct PyModuleDef BGL_module_def = { static struct PyModuleDef BGL_module_def = {
{}, /* m_base */ PyModuleDef_HEAD_INIT,
"BGL", /* m_name */ "BGL", /* m_name */
0, /* m_doc */ 0, /* m_doc */
0, /* m_size */ 0, /* m_size */

@ -80,7 +80,7 @@ struct PyMethodDef M_Geometry_methods[] = {
#if (PY_VERSION_HEX >= 0x03000000) #if (PY_VERSION_HEX >= 0x03000000)
static struct PyModuleDef M_Geometry_module_def = { static struct PyModuleDef M_Geometry_module_def = {
{}, /* m_base */ PyModuleDef_HEAD_INIT,
"Geometry", /* m_name */ "Geometry", /* m_name */
M_Geometry_doc, /* m_doc */ M_Geometry_doc, /* m_doc */
0, /* m_size */ 0, /* m_size */

@ -96,7 +96,7 @@ struct PyMethodDef M_Mathutils_methods[] = {
#if (PY_VERSION_HEX >= 0x03000000) #if (PY_VERSION_HEX >= 0x03000000)
static struct PyModuleDef M_Mathutils_module_def = { static struct PyModuleDef M_Mathutils_module_def = {
{}, /* m_base */ PyModuleDef_HEAD_INIT,
"Mathutils", /* m_name */ "Mathutils", /* m_name */
M_Mathutils_doc, /* m_doc */ M_Mathutils_doc, /* m_doc */
0, /* m_size */ 0, /* m_size */
@ -137,6 +137,8 @@ PyObject *Mathutils_Init(const char *from)
PyModule_AddObject( submodule, "Euler", (PyObject *)&euler_Type ); PyModule_AddObject( submodule, "Euler", (PyObject *)&euler_Type );
PyModule_AddObject( submodule, "Quaternion", (PyObject *)&quaternion_Type ); PyModule_AddObject( submodule, "Quaternion", (PyObject *)&quaternion_Type );
mathutils_matrix_vector_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_vector_cb);
return (submodule); return (submodule);
} }
@ -1164,62 +1166,76 @@ int Mathutils_RegisterCallback(Mathutils_Callback *cb)
return i; return i;
} }
int Vector_ReadCallback(VectorObject *self) { /* use macros to check for NULL */
if(self->user) { int _Vector_ReadCallback(VectorObject *self)
Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; {
if(cb->get(self->user, self->subtype, self->vec)) { Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
return 1; if(cb->get(self->cb_user, self->cb_subtype, self->vec)) {
} return 1;
else { }
PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); else {
return 0; PyErr_SetString(PyExc_SystemError, "Vector user has become invalid");
} return 0;
} }
return 1; /* no user continue silently */
} }
int Vector_WriteCallback(VectorObject *self) { int _Vector_WriteCallback(VectorObject *self)
if(self->user) { {
Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->set(self->user, self->subtype, self->vec)) { if(cb->set(self->cb_user, self->cb_subtype, self->vec)) {
return 1; return 1;
} }
else { else {
PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); PyErr_SetString(PyExc_SystemError, "Vector user has become invalid");
return 0; return 0;
}
} }
return 1; /* no user continue silently */
} }
int Vector_ReadIndexCallback(VectorObject *self, int index) { int _Vector_ReadIndexCallback(VectorObject *self, int index)
if(self->user) { {
Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->get_index(self->user, self->subtype, self->vec, index)) { if(cb->get_index(self->cb_user, self->cb_subtype, self->vec, index)) {
return 1; return 1;
} }
else { else {
PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); PyErr_SetString(PyExc_SystemError, "Vector user has become invalid");
return 0; return 0;
}
} }
return 1; /* no user continue silently */
} }
int Vector_WriteIndexCallback(VectorObject *self, int index) { int _Vector_WriteIndexCallback(VectorObject *self, int index)
if(self->user) { {
Mathutils_Callback *cb= mathutils_callbacks[self->callback_type]; Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->set_index(self->user, self->subtype, self->vec, index)) { if(cb->set_index(self->cb_user, self->cb_subtype, self->vec, index)) {
return 1; return 1;
} }
else { else {
PyErr_SetString(PyExc_SystemError, "Vector user has become invalid"); PyErr_SetString(PyExc_SystemError, "Vector user has become invalid");
return 0; 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 */
} }

@ -67,17 +67,30 @@ int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps);
typedef struct Mathutils_Callback Mathutils_Callback; typedef struct Mathutils_Callback Mathutils_Callback;
struct Mathutils_Callback { struct Mathutils_Callback {
int (*check)(PyObject *user); /* checks the user is still valid */ 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 (*get)(PyObject *user, int subtype, float *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 (*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 *vec_from, int index); /* same as above but only for an index */ 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 *vec_to, 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 Mathutils_RegisterCallback(Mathutils_Callback *cb);
int Vector_ReadCallback(VectorObject *self); int _Vector_ReadCallback(VectorObject *self);
int Vector_WriteCallback(VectorObject *self); int _Vector_WriteCallback(VectorObject *self);
int Vector_ReadIndexCallback(VectorObject *self, int index); int _Vector_ReadIndexCallback(VectorObject *self, int index);
int Vector_WriteIndexCallback(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 */ #endif /* EXPP_Mathutils_H */

@ -33,6 +33,69 @@
static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec); /* utility func */ 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 ---------------------------*/ /*-------------------------DOC STRINGS ---------------------------*/
static char Matrix_Zero_doc[] = "() - set all values in the matrix to 0"; 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"; 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); argObject = PyTuple_GET_ITEM(args, 0);
if(MatrixObject_Check(argObject)){ if(MatrixObject_Check(argObject)){
mat = (MatrixObject*)argObject; mat = (MatrixObject*)argObject;
if(!Matrix_ReadCallback(mat))
return NULL;
argSize = mat->rowSize; //rows argSize = mat->rowSize; //rows
seqSize = mat->colSize; //col seqSize = mat->colSize; //col
@ -160,6 +225,9 @@ static PyObject *Matrix_toQuat(MatrixObject * self)
{ {
float quat[4]; float quat[4];
if(!Matrix_ReadCallback(self))
return NULL;
/*must be 3-4 cols, 3-4 rows, square matrix*/ /*must be 3-4 cols, 3-4 rows, square matrix*/
if(self->colSize < 3 || self->rowSize < 3 || (self->colSize != self->rowSize)) { 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"); 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; EulerObject *eul_compat = NULL;
int x; int x;
if(!Matrix_ReadCallback(self))
return NULL;
if(!PyArg_ParseTuple(args, "|O!:toEuler", &euler_Type, &eul_compat)) if(!PyArg_ParseTuple(args, "|O!:toEuler", &euler_Type, &eul_compat))
return NULL; 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; int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index;
if(self->data.blend_data){ if(self->wrapped==Py_WRAP){
PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - only python matrices"); PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - make a copy and resize that");
return NULL; return NULL;
} }
if(self->cb_user){
self->data.py_data = PyMem_Realloc(self->data.py_data, (sizeof(float) * 16)); PyErr_SetString(PyExc_TypeError, "cannot resize owned data - make a copy and resize that");
if(self->data.py_data == NULL) { 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"); PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
return NULL; return NULL;
} }
self->contigPtr = self->data.py_data; /*force*/
self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4)); self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4));
if(self->matrix == NULL) { if(self->matrix == NULL) {
PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space"); PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
@ -268,7 +342,10 @@ PyObject *Matrix_Resize4x4(MatrixObject * self)
PyObject *Matrix_TranslationPart(MatrixObject * self) PyObject *Matrix_TranslationPart(MatrixObject * self)
{ {
float vec[4]; float vec[4];
if(!Matrix_ReadCallback(self))
return NULL;
if(self->colSize < 3 || self->rowSize < 4){ if(self->colSize < 3 || self->rowSize < 4){
PyErr_SetString(PyExc_AttributeError, "Matrix.translationPart: inappropriate matrix size"); PyErr_SetString(PyExc_AttributeError, "Matrix.translationPart: inappropriate matrix size");
return NULL; 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, 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}; 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){ if(self->colSize < 3 || self->rowSize < 3){
PyErr_SetString(PyExc_AttributeError, "Matrix.rotationPart: inappropriate matrix size\n"); PyErr_SetString(PyExc_AttributeError, "Matrix.rotationPart: inappropriate matrix size\n");
return NULL; return NULL;
@ -309,6 +389,9 @@ PyObject *Matrix_scalePart(MatrixObject * self)
float scale[3], rot[3]; float scale[3], rot[3];
float mat[3][3], imat[3][3], tmat[3][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*/ /*must be 3-4 cols, 3-4 rows, square matrix*/
if(self->colSize == 4 && self->rowSize == 4) if(self->colSize == 4 && self->rowSize == 4)
Mat3CpyMat4(mat, (float (*)[4])*self->matrix); 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, 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}; 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){ if(self->rowSize != self->colSize){
PyErr_SetString(PyExc_AttributeError, "Matrix.invert(ed): only square matrices are supported"); PyErr_SetString(PyExc_AttributeError, "Matrix.invert(ed): only square matrices are supported");
return NULL; return NULL;
@ -379,6 +465,7 @@ PyObject *Matrix_Invert(MatrixObject * self)
return NULL; return NULL;
} }
Matrix_WriteCallback(self);
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
} }
@ -389,6 +476,9 @@ PyObject *Matrix_Determinant(MatrixObject * self)
{ {
float det = 0.0f; float det = 0.0f;
if(!Matrix_ReadCallback(self))
return NULL;
if(self->rowSize != self->colSize){ if(self->rowSize != self->colSize){
PyErr_SetString(PyExc_AttributeError, "Matrix.determinant: only square matrices are supported"); PyErr_SetString(PyExc_AttributeError, "Matrix.determinant: only square matrices are supported");
return NULL; return NULL;
@ -414,6 +504,9 @@ PyObject *Matrix_Transpose(MatrixObject * self)
{ {
float t = 0.0f; float t = 0.0f;
if(!Matrix_ReadCallback(self))
return NULL;
if(self->rowSize != self->colSize){ if(self->rowSize != self->colSize){
PyErr_SetString(PyExc_AttributeError, "Matrix.transpose(d): only square matrices are supported"); PyErr_SetString(PyExc_AttributeError, "Matrix.transpose(d): only square matrices are supported");
return NULL; return NULL;
@ -429,6 +522,7 @@ PyObject *Matrix_Transpose(MatrixObject * self)
Mat4Transp((float (*)[4])*self->matrix); Mat4Transp((float (*)[4])*self->matrix);
} }
Matrix_WriteCallback(self);
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
} }
@ -438,18 +532,25 @@ PyObject *Matrix_Transpose(MatrixObject * self)
PyObject *Matrix_Zero(MatrixObject * self) PyObject *Matrix_Zero(MatrixObject * self)
{ {
int row, col; int row, col;
for(row = 0; row < self->rowSize; row++) { for(row = 0; row < self->rowSize; row++) {
for(col = 0; col < self->colSize; col++) { for(col = 0; col < self->colSize; col++) {
self->matrix[row][col] = 0.0f; self->matrix[row][col] = 0.0f;
} }
} }
if(!Matrix_WriteCallback(self))
return NULL;
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
} }
/*---------------------------Matrix.identity(() ------------------*/ /*---------------------------Matrix.identity(() ------------------*/
PyObject *Matrix_Identity(MatrixObject * self) PyObject *Matrix_Identity(MatrixObject * self)
{ {
if(!Matrix_ReadCallback(self))
return NULL;
if(self->rowSize != self->colSize){ if(self->rowSize != self->colSize){
PyErr_SetString(PyExc_AttributeError, "Matrix.identity: only square matrices are supported\n"); PyErr_SetString(PyExc_AttributeError, "Matrix.identity: only square matrices are supported\n");
return NULL; return NULL;
@ -466,6 +567,9 @@ PyObject *Matrix_Identity(MatrixObject * self)
Mat4One((float (*)[4]) *self->matrix); Mat4One((float (*)[4]) *self->matrix);
} }
if(!Matrix_WriteCallback(self))
return NULL;
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
} }
@ -473,6 +577,9 @@ PyObject *Matrix_Identity(MatrixObject * self)
/*---------------------------Matrix.inverted() ------------------*/ /*---------------------------Matrix.inverted() ------------------*/
PyObject *Matrix_copy(MatrixObject * self) PyObject *Matrix_copy(MatrixObject * self)
{ {
if(!Matrix_ReadCallback(self))
return NULL;
return (PyObject*)(MatrixObject*)newMatrixObject((float (*))*self->matrix, self->rowSize, self->colSize, Py_NEW); 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); PyMem_Free(self->matrix);
/*only free py_data*/ /*only free py_data*/
if(self->data.py_data){ if(self->wrapped==Py_WRAP)
PyMem_Free(self->data.py_data); PyMem_Free(self->contigPtr);
}
Py_XDECREF(self->cb_user);
PyObject_DEL(self); PyObject_DEL(self);
} }
@ -495,6 +603,9 @@ static PyObject *Matrix_repr(MatrixObject * self)
int x, y; int x, y;
char buffer[48], str[1024]; char buffer[48], str[1024];
if(!Matrix_ReadCallback(self))
return NULL;
BLI_strncpy(str,"",1024); BLI_strncpy(str,"",1024);
for(x = 0; x < self->rowSize; x++){ for(x = 0; x < self->rowSize; x++){
sprintf(buffer, "["); sprintf(buffer, "[");
@ -531,6 +642,9 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa
matA = (MatrixObject*)objectA; matA = (MatrixObject*)objectA;
matB = (MatrixObject*)objectB; matB = (MatrixObject*)objectB;
if(!Matrix_ReadCallback(matA) || !Matrix_ReadCallback(matB))
return NULL;
if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){ if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){
if (comparison_type == Py_NE){ if (comparison_type == Py_NE){
Py_RETURN_TRUE; Py_RETURN_TRUE;
@ -578,11 +692,14 @@ static int Matrix_len(MatrixObject * self)
the wrapped vector gives direct access to the matrix data*/ the wrapped vector gives direct access to the matrix data*/
static PyObject *Matrix_item(MatrixObject * self, int i) static PyObject *Matrix_item(MatrixObject * self, int i)
{ {
if(!Matrix_ReadCallback(self))
return NULL;
if(i < 0 || i >= self->rowSize) { if(i < 0 || i >= self->rowSize) {
PyErr_SetString(PyExc_IndexError, "matrix[attribute]: array index out of range"); PyErr_SetString(PyExc_IndexError, "matrix[attribute]: array index out of range");
return NULL; 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[]------------------------- /*----------------------------object[]-------------------------
sequence accessor (set)*/ sequence accessor (set)*/
@ -592,6 +709,9 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
float vec[4]; float vec[4];
PyObject *m, *f; PyObject *m, *f;
if(!Matrix_ReadCallback(self))
return -1;
if(i >= self->rowSize || i < 0){ if(i >= self->rowSize || i < 0){
PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad row\n"); PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad row\n");
return -1; return -1;
@ -625,6 +745,8 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
for(y = 0; y < size; y++){ for(y = 0; y < size; y++){
self->matrix[i][y] = vec[y]; self->matrix[i][y] = vec[y];
} }
Matrix_WriteCallback(self);
return 0; return 0;
}else{ }else{
PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: expects a sequence of column size\n"); 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; PyObject *list = NULL;
int count; int count;
if(!Matrix_ReadCallback(self))
return NULL;
CLAMP(begin, 0, self->rowSize); CLAMP(begin, 0, self->rowSize);
CLAMP(end, 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); list = PyList_New(end - begin);
for(count = begin; count < end; count++) { for(count = begin; count < end; count++) {
PyList_SetItem(list, count - begin, 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; return list;
@ -661,6 +787,9 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
PyObject *subseq; PyObject *subseq;
PyObject *m; PyObject *m;
if(!Matrix_ReadCallback(self))
return -1;
CLAMP(begin, 0, self->rowSize); CLAMP(begin, 0, self->rowSize);
CLAMP(end, 0, self->rowSize); CLAMP(end, 0, self->rowSize);
begin = MIN2(begin,end); 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++){ for(x = 0; x < (size * sub_size); x++){
self->matrix[begin + (int)floor(x / self->colSize)][x % self->colSize] = mat[x]; self->matrix[begin + (int)floor(x / self->colSize)][x % self->colSize] = mat[x];
} }
Matrix_WriteCallback(self);
return 0; return 0;
}else{ }else{
PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n"); 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...."); PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
return NULL; return NULL;
} }
if(!Matrix_ReadCallback(mat1) || !Matrix_ReadCallback(mat2))
return NULL;
if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){ if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation"); PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
return NULL; 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...."); PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
return NULL; return NULL;
} }
if(!Matrix_ReadCallback(mat1) || !Matrix_ReadCallback(mat2))
return NULL;
if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){ if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation"); PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
return NULL; return NULL;
@ -793,8 +932,16 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
double dot = 0.0f; double dot = 0.0f;
MatrixObject *mat1 = NULL, *mat2 = NULL; MatrixObject *mat1 = NULL, *mat2 = NULL;
if(MatrixObject_Check(m1)) mat1 = (MatrixObject*)m1; if(MatrixObject_Check(m1)) {
if(MatrixObject_Check(m2)) mat2 = (MatrixObject*)m2; 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 && mat2) { /*MATRIX * MATRIX*/
if(mat1->colSize != mat2->rowSize){ if(mat1->colSize != mat2->rowSize){
@ -853,6 +1000,9 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
} }
static PyObject* Matrix_inv(MatrixObject *self) static PyObject* Matrix_inv(MatrixObject *self)
{ {
if(!Matrix_ReadCallback(self))
return NULL;
return Matrix_Invert(self); return Matrix_Invert(self);
} }
@ -902,6 +1052,17 @@ static PyObject *Matrix_getColSize( MatrixObject * self, void *type )
return PyLong_FromLong((long) self->colSize); 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 ) static PyObject *Matrix_getWrapped( MatrixObject * self, void *type )
{ {
if (self->wrapped == Py_WRAP) if (self->wrapped == Py_WRAP)
@ -917,6 +1078,7 @@ static PyGetSetDef Matrix_getseters[] = {
{"rowSize", (getter)Matrix_getRowSize, (setter)NULL, "", NULL}, {"rowSize", (getter)Matrix_getRowSize, (setter)NULL, "", NULL},
{"colSize", (getter)Matrix_getColSize, (setter)NULL, "", NULL}, {"colSize", (getter)Matrix_getColSize, (setter)NULL, "", NULL},
{"wrapped", (getter)Matrix_getWrapped, (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 */ {NULL,NULL,NULL,NULL,NULL} /* Sentinel */
}; };
@ -986,7 +1148,7 @@ self->matrix self->contiguous_ptr (reference to data.xxx)
[4] [4]
[5] [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 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
(i.e. it was allocated elsewhere by MEM_mallocN()) (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 = PyObject_NEW(MatrixObject, &matrix_Type);
self->data.blend_data = NULL;
self->data.py_data = NULL;
self->rowSize = rowSize; self->rowSize = rowSize;
self->colSize = colSize; self->colSize = colSize;
/* init callbacks as NULL */
self->cb_user= NULL;
self->cb_type= self->cb_subtype= 0;
if(type == Py_WRAP){ if(type == Py_WRAP){
self->data.blend_data = mat; self->contigPtr = mat;
self->contigPtr = self->data.blend_data;
/*create pointer array*/ /*create pointer array*/
self->matrix = PyMem_Malloc(rowSize * sizeof(float *)); self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
if(self->matrix == NULL) { /*allocation failure*/ if(self->matrix == NULL) { /*allocation failure*/
@ -1024,16 +1187,15 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
} }
self->wrapped = Py_WRAP; self->wrapped = Py_WRAP;
}else if (type == Py_NEW){ }else if (type == Py_NEW){
self->data.py_data = PyMem_Malloc(rowSize * colSize * sizeof(float)); self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float));
if(self->data.py_data == NULL) { /*allocation failure*/ if(self->contigPtr == NULL) { /*allocation failure*/
PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n"); PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n");
return NULL; return NULL;
} }
self->contigPtr = self->data.py_data;
/*create pointer array*/ /*create pointer array*/
self->matrix = PyMem_Malloc(rowSize * sizeof(float *)); self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
if(self->matrix == NULL) { /*allocation failure*/ 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"); PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
return NULL; return NULL;
} }
@ -1059,6 +1221,18 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
return (PyObject *) self; 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 (internal)---------
//COLUMN VECTOR Multiplication (Matrix X Vector) //COLUMN VECTOR Multiplication (Matrix X Vector)
// [1][2][3] [a] // [1][2][3] [a]
@ -1071,7 +1245,7 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject*
double dot = 0.0f; double dot = 0.0f;
int x, y, z = 0; int x, y, z = 0;
if(!Vector_ReadCallback(vec)) if(!Matrix_ReadCallback(mat) || !Vector_ReadCallback(vec))
return NULL; return NULL;
if(mat->rowSize != vec->size){ if(mat->rowSize != vec->size){

@ -39,15 +39,14 @@ extern PyTypeObject matrix_Type;
typedef float **ptRow; typedef float **ptRow;
typedef struct _Matrix { typedef struct _Matrix {
PyObject_VAR_HEAD PyObject_VAR_HEAD
struct{ ptRow matrix; /*ptr to the contigPtr (accessor)*/
float *py_data; /*python managed*/ float* contigPtr; /*1D array of data (alias)*/
float *blend_data; /*blender managed*/ PyObject* cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */
}data; unsigned char rowSize;
ptRow matrix; /*ptr to the contigPtr (accessor)*/ unsigned char colSize;
float *contigPtr; /*1D array of data (alias)*/ unsigned char wrapped; /*is wrapped data?*/
int rowSize; unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */
int colSize; unsigned int cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
int wrapped; /*is wrapped data?*/
} MatrixObject; } MatrixObject;
/*struct data contains a pointer to the actual data that the /*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*/ /*prototypes*/
PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type); 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 */ #endif /* EXPP_matrix_H */

@ -75,7 +75,7 @@ static struct PyMethodDef Quaternion_methods[] = {
//----------------------------------Mathutils.Quaternion() -------------- //----------------------------------Mathutils.Quaternion() --------------
static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
PyObject *listObject = NULL, *n, *q, *f; PyObject *listObject = NULL, *n, *q;
int size, i; int size, i;
float quat[4], scalar; float quat[4], scalar;
double norm = 0.0f, angle = 0.0f; double norm = 0.0f, angle = 0.0f;
@ -159,7 +159,6 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw
} }
quat[i] = scalar; quat[i] = scalar;
Py_DECREF(f);
Py_DECREF(q); Py_DECREF(q);
} }
if(size == 3){ //calculate the quat based on axis/angle if(size == 3){ //calculate the quat based on axis/angle

@ -72,8 +72,8 @@ static struct PyMethodDef Vector_methods[] = {
{"normalize", (PyCFunction) Vector_Normalize, METH_NOARGS, Vector_Normalize_doc}, {"normalize", (PyCFunction) Vector_Normalize, METH_NOARGS, Vector_Normalize_doc},
{"negate", (PyCFunction) Vector_Negate, METH_NOARGS, Vector_Negate_doc}, {"negate", (PyCFunction) Vector_Negate, METH_NOARGS, Vector_Negate_doc},
{"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc}, {"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc},
{"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize2D_doc}, {"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize3D_doc},
{"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize2D_doc}, {"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize4D_doc},
{"toTrackQuat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc}, {"toTrackQuat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc},
{"reflect", ( PyCFunction ) Vector_Reflect, METH_O, Vector_Reflect_doc}, {"reflect", ( PyCFunction ) Vector_Reflect, METH_O, Vector_Reflect_doc},
{"cross", ( PyCFunction ) Vector_Cross, METH_O, Vector_Dot_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"); PyErr_SetString(PyExc_TypeError, "vector.resize2d(): cannot resize wrapped data - only python vectors\n");
return NULL; return NULL;
} }
if(self->user) { if(self->cb_user) {
PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner"); PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner");
return NULL; 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"); PyErr_SetString(PyExc_TypeError, "vector.resize3d(): cannot resize wrapped data - only python vectors\n");
return NULL; return NULL;
} }
if(self->user) { if(self->cb_user) {
PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner"); PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner");
return NULL; 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"); PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize wrapped data - only python vectors");
return NULL; return NULL;
} }
if(self->user) { if(self->cb_user) {
PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner"); PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize a vector that has an owner");
return NULL; return NULL;
} }
@ -478,10 +478,10 @@ static PyObject *Vector_copy(VectorObject * self)
static void Vector_dealloc(VectorObject * self) static void Vector_dealloc(VectorObject * self)
{ {
/* only free non wrapped */ /* only free non wrapped */
if(self->wrapped != Py_WRAP){ if(self->wrapped != Py_WRAP)
PyMem_Free(self->vec); PyMem_Free(self->vec);
}
Py_XDECREF(self->user); Py_XDECREF(self->cb_user);
PyObject_DEL(self); PyObject_DEL(self);
} }
@ -845,6 +845,9 @@ static PyObject *Vector_imul(PyObject * v1, PyObject * v2)
int x,y, size = vec->size; int x,y, size = vec->size;
MatrixObject *mat= (MatrixObject*)v2; MatrixObject *mat= (MatrixObject*)v2;
if(!Vector_ReadCallback(mat))
return NULL;
if(mat->colSize != size){ if(mat->colSize != size){
if(mat->rowSize == 4 && vec->size != 3){ if(mat->rowSize == 4 && vec->size != 3){
PyErr_SetString(PyExc_AttributeError, "vector * matrix: matrix column size and the vector size must be the same"); 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 ) static PyObject *Vector_getOwner( VectorObject * self, void *type )
{ {
if(self->user==NULL) { if(self->cb_user==NULL) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
else { else {
Py_INCREF(self->user); Py_INCREF(self->cb_user);
return self->user; return self->cb_user;
} }
} }
@ -1886,6 +1889,10 @@ PyObject *newVectorObject(float *vec, int size, int type)
if(size > 4 || size < 2) if(size > 4 || size < 2)
return NULL; return NULL;
self->size = size; self->size = size;
/* init callbacks as NULL */
self->cb_user= NULL;
self->cb_type= self->cb_subtype= 0;
if(type == Py_WRAP) { if(type == Py_WRAP) {
self->vec = vec; self->vec = vec;
@ -1910,15 +1917,15 @@ PyObject *newVectorObject(float *vec, int size, int type)
return (PyObject *) self; 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); VectorObject *self= newVectorObject(dummy, size, Py_NEW);
if(self) { if(self) {
Py_INCREF(user); Py_INCREF(cb_user);
self->user= user; self->cb_user= cb_user;
self->callback_type = (unsigned char)callback_type; self->cb_type= (unsigned char)cb_type;
self->subtype = (unsigned char)subtype; self->cb_subtype= (unsigned char)cb_subtype;
} }
return self; 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; return NULL;
for(x = 0; x < vec_size; x++){ for(x = 0; x < vec_size; x++){

@ -40,11 +40,11 @@ extern PyTypeObject vector_Type;
typedef struct { typedef struct {
PyObject_VAR_HEAD PyObject_VAR_HEAD
float *vec; /*1D array of data (alias), wrapped status depends on wrapped status */ 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 size; /* vec size 2,3 or 4 */
unsigned char wrapped; /* wrapped data type? */ unsigned char wrapped; /* wrapped data type? */
unsigned char callback_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ unsigned char cb_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_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
} VectorObject; } VectorObject;

@ -37,6 +37,11 @@
#include "BPY_extern.h" #include "BPY_extern.h"
#include "../generic/bpy_internal_import.h" // our own imports #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 ) 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, "types", BPY_rna_types() );
PyModule_AddObject( mod, "props", BPY_rna_props() ); PyModule_AddObject( mod, "props", BPY_rna_props() );
PyModule_AddObject( mod, "ops", BPY_operator_module() ); 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 */ /* add the module so we can import it */
PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod); PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
Py_DECREF(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) #if (PY_VERSION_HEX < 0x02050000)

@ -44,9 +44,10 @@
#ifdef USE_MATHUTILS #ifdef USE_MATHUTILS
#include "../generic/Mathutils.h" /* so we can have mathutils callbacks */ #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_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; 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_Callback mathutils_rna_vector_cb = {
mathutils_rna_vector_check, mathutils_rna_generic_check,
mathutils_rna_vector_get, mathutils_rna_vector_get,
mathutils_rna_vector_set, mathutils_rna_vector_set,
mathutils_rna_vector_get_index, mathutils_rna_vector_get_index,
mathutils_rna_vector_set_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 #endif
static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b ) 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 #ifdef USE_MATHUTILS
/* return a mathutils vector where possible */ /* return a mathutils vector where possible */
if( RNA_property_type(prop)==PROP_FLOAT && if(RNA_property_type(prop)==PROP_FLOAT) {
RNA_property_subtype(prop)==PROP_VECTOR && if(RNA_property_subtype(prop)==PROP_VECTOR) {
len>=2 && len <= 4 ) if(len>=2 && len <= 4) {
{ PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_vector_cb_index, 0);
PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_vector_cb_index, 0); Py_DECREF(ret); /* the vector owns now */
Py_DECREF(ret); /* the vector owns now */ ret= vec_cb; /* return the vector instead */
}
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 #endif
return ret; return ret;
@ -1749,6 +1792,7 @@ PyObject *BPY_rna_module( void )
#ifdef USE_MATHUTILS // register mathutils callbacks, ok to run more then once. #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_vector_cb_index= Mathutils_RegisterCallback(&mathutils_rna_vector_cb);
mathutils_rna_matrix_cb_index= Mathutils_RegisterCallback(&mathutils_rna_matrix_cb);
#endif #endif
/* This can't be set in the pytype struct because some compilers complain */ /* This can't be set in the pytype struct because some compilers complain */

@ -983,7 +983,17 @@ void KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot)
GetSGNode()->SetLocalOrientation(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) void KX_GameObject::NodeSetLocalScale(const MT_Vector3& scale)
{ {
@ -1062,7 +1072,13 @@ const MT_Matrix3x3& KX_GameObject::NodeGetWorldOrientation() const
return GetSGNode()->GetWorldOrientation(); 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 const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const
{ {
@ -1073,7 +1089,14 @@ const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const
return GetSGNode()->GetWorldScaling(); 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 const MT_Point3& KX_GameObject::NodeGetWorldPosition() const
{ {
@ -1084,6 +1107,16 @@ const MT_Point3& KX_GameObject::NodeGetWorldPosition() const
return dummy_point; 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 /* Suspend/ resume: for the dynamic behaviour, there is a simple
* method. For the residual motion, there is not. I wonder what the * method. For the residual motion, there is not. I wonder what the
* correct solution is for Sumo. Remove from the motion-update tree? * 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_SCALE_GLOBAL 4
#define MATHUTILS_VEC_CB_INERTIA_LOCAL 5 #define MATHUTILS_VEC_CB_INERTIA_LOCAL 5
static int mathutils_kxgameob_vector_cb_index= -1; /* index for our callbacks */ 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); KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v);
if(self==NULL) if(self==NULL)
@ -1184,26 +1216,21 @@ static int mathutils_kxgameob_vector_get(PyObject *self_v, int subtype, float *v
switch(subtype) { switch(subtype) {
case MATHUTILS_VEC_CB_POS_LOCAL: case MATHUTILS_VEC_CB_POS_LOCAL:
if(!self->GetSGNode()) return 0; self->NodeGetLocalPosition().getValue(vec_from);
self->GetSGNode()->GetLocalPosition().getValue(vec_from);
break; break;
case MATHUTILS_VEC_CB_POS_GLOBAL: case MATHUTILS_VEC_CB_POS_GLOBAL:
if(!self->GetSGNode()) return 0; self->NodeGetWorldPosition().getValue(vec_from);
self->GetSGNode()->GetWorldPosition().getValue(vec_from);
break; break;
case MATHUTILS_VEC_CB_SCALE_LOCAL: case MATHUTILS_VEC_CB_SCALE_LOCAL:
if(!self->GetSGNode()) return 0; self->NodeGetLocalScaling().getValue(vec_from);
self->GetSGNode()->GetLocalScale().getValue(vec_from);
break; break;
case MATHUTILS_VEC_CB_SCALE_GLOBAL: case MATHUTILS_VEC_CB_SCALE_GLOBAL:
self->NodeGetWorldScaling().getValue(vec_from); self->NodeGetWorldScaling().getValue(vec_from);
break; break;
case MATHUTILS_VEC_CB_INERTIA_LOCAL: case MATHUTILS_VEC_CB_INERTIA_LOCAL:
if(!self->GetSGNode()) return 0; if(!self->GetPhysicsController()) return 0;
self->GetPhysicsController()->GetLocalInertia().getValue(vec_from); self->GetPhysicsController()->GetLocalInertia().getValue(vec_from);
break; break;
} }
return 1; return 1;
@ -1215,11 +1242,6 @@ static int mathutils_kxgameob_vector_set(PyObject *self_v, int subtype, float *v
if(self==NULL) if(self==NULL)
return 0; return 0;
/* first */
SG_Node* sg = self->GetSGNode();
if(sg==NULL)
return 0;
switch(subtype) { switch(subtype) {
case MATHUTILS_VEC_CB_POS_LOCAL: case MATHUTILS_VEC_CB_POS_LOCAL:
self->NodeSetLocalPosition(MT_Point3(vec_to)); 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_Callback mathutils_kxgameob_vector_cb = {
mathutils_kxgameob_vector_check, mathutils_kxgameob_generic_check,
mathutils_kxgameob_vector_get, mathutils_kxgameob_vector_get,
mathutils_kxgameob_vector_set, mathutils_kxgameob_vector_set,
mathutils_kxgameob_vector_get_index, mathutils_kxgameob_vector_get_index,
mathutils_kxgameob_vector_set_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) void KX_GameObject_Mathutils_Callback_Init(void)
{ {
// register mathutils callbacks, ok to run more then once. // register mathutils callbacks, ok to run more then once.
mathutils_kxgameob_vector_cb_index= Mathutils_RegisterCallback(&mathutils_kxgameob_vector_cb); 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 #endif // USE_MATHUTILS
@ -1754,10 +1833,7 @@ PyObject* KX_GameObject::pyattr_get_localPosition(void *self_v, const KX_PYATTRI
#ifdef USE_MATHUTILS #ifdef USE_MATHUTILS
return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_POS_LOCAL); return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_POS_LOCAL);
#else #else
if (self->GetSGNode()) return PyObjectFrom(self->NodeGetLocalPosition());
return PyObjectFrom(self->GetSGNode()->GetLocalPosition());
else
return PyObjectFrom(dummy_point);
#endif #endif
} }
@ -1782,13 +1858,17 @@ PyObject* KX_GameObject::pyattr_get_localInertia(void *self_v, const KX_PYATTRIB
if (self->GetPhysicsController()) if (self->GetPhysicsController())
return PyObjectFrom(self->GetPhysicsController()->GetLocalInertia()); return PyObjectFrom(self->GetPhysicsController()->GetLocalInertia());
return Py_BuildValue("fff", 0.0f, 0.0f, 0.0f); 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) 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); KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
return PyObjectFrom(self->NodeGetWorldOrientation()); return PyObjectFrom(self->NodeGetWorldOrientation());
#endif
} }
int KX_GameObject::pyattr_set_worldOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) 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) 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); KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
if (self->GetSGNode()) return PyObjectFrom(self->NodeGetLocalOrientation());
return PyObjectFrom(self->GetSGNode()->GetLocalOrientation()); #endif
else
return PyObjectFrom(dummy_orientation);
} }
int KX_GameObject::pyattr_set_localOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) 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 #ifdef USE_MATHUTILS
return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_SCALE_LOCAL); return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_SCALE_LOCAL);
#else #else
if (self->GetSGNode()) return PyObjectFrom(self->NodeGetLocalScale());
return PyObjectFrom(self->GetSGNode()->GetLocalScale());
else
return PyObjectFrom(dummy_scaling);
#endif #endif
} }

@ -397,6 +397,7 @@ public:
void NodeSetLocalPosition(const MT_Point3& trans ); void NodeSetLocalPosition(const MT_Point3& trans );
void NodeSetLocalOrientation(const MT_Matrix3x3& rot ); void NodeSetLocalOrientation(const MT_Matrix3x3& rot );
void NodeSetGlobalOrientation(const MT_Matrix3x3& rot );
void NodeSetLocalScale( const MT_Vector3& scale ); void NodeSetLocalScale( const MT_Vector3& scale );
@ -410,21 +411,13 @@ public:
double time double time
); );
const const MT_Matrix3x3& NodeGetWorldOrientation( ) const;
MT_Matrix3x3& const MT_Vector3& NodeGetWorldScaling( ) const;
NodeGetWorldOrientation( const MT_Point3& NodeGetWorldPosition( ) const;
) 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. * @section scene graph node accessor functions.