rna/python mathutils module

- return euler rotation values from rna now have correct rotation order.
- mathutils.Euler stored rotation order off by 1. (didnt work at all)
- Euler/Quat/Color sliceing working again.
This commit is contained in:
2010-04-25 23:33:09 +00:00
parent 64359c9abc
commit 4fc4fb9bfb
5 changed files with 435 additions and 258 deletions

View File

@@ -27,6 +27,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BKE_utildefines.h" #include "BKE_utildefines.h"
#define COLOR_SIZE 3
//----------------------------------mathutils.Color() ------------------- //----------------------------------mathutils.Color() -------------------
//makes a new color for you to play with //makes a new color for you to play with
static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwargs) static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
@@ -37,7 +39,7 @@ static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwar
case 0: case 0:
break; break;
case 1: case 1:
if((mathutils_array_parse(col, 3, 3, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1) if((mathutils_array_parse(col, COLOR_SIZE, COLOR_SIZE, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1)
return NULL; return NULL;
break; break;
default: default:
@@ -55,15 +57,15 @@ static PyObject *Color_ToTupleExt(ColorObject *self, int ndigits)
PyObject *ret; PyObject *ret;
int i; int i;
ret= PyTuple_New(3); ret= PyTuple_New(COLOR_SIZE);
if(ndigits >= 0) { if(ndigits >= 0) {
for(i= 0; i < 3; i++) { for(i= 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits))); PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits)));
} }
} }
else { else {
for(i= 0; i < 3; i++) { for(i= 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i])); PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
} }
} }
@@ -137,10 +139,10 @@ static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int compar
switch (comparison_type){ switch (comparison_type){
case Py_EQ: case Py_EQ:
result = EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1); result = EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1);
break; break;
case Py_NE: case Py_NE:
result = !EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1); result = !EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1);
break; break;
default: default:
printf("The result of the comparison could not be evaluated"); printf("The result of the comparison could not be evaluated");
@@ -158,15 +160,15 @@ static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int compar
//sequence length //sequence length
static int Color_len(ColorObject * self) static int Color_len(ColorObject * self)
{ {
return 3; return COLOR_SIZE;
} }
//----------------------------object[]--------------------------- //----------------------------object[]---------------------------
//sequence accessor (get) //sequence accessor (get)
static PyObject *Color_item(ColorObject * self, int i) static PyObject *Color_item(ColorObject * self, int i)
{ {
if(i<0) i= 3-i; if(i<0) i= COLOR_SIZE-i;
if(i < 0 || i >= 3) { if(i < 0 || i >= COLOR_SIZE) {
PyErr_SetString(PyExc_IndexError, "color[attribute]: array index out of range"); PyErr_SetString(PyExc_IndexError, "color[attribute]: array index out of range");
return NULL; return NULL;
} }
@@ -188,9 +190,9 @@ static int Color_ass_item(ColorObject * self, int i, PyObject * value)
return -1; return -1;
} }
if(i<0) i= 3-i; if(i<0) i= COLOR_SIZE-i;
if(i < 0 || i >= 3){ if(i < 0 || i >= COLOR_SIZE){
PyErr_SetString(PyExc_IndexError, "color[attribute] = x: array assignment index out of range\n"); PyErr_SetString(PyExc_IndexError, "color[attribute] = x: array assignment index out of range\n");
return -1; return -1;
} }
@@ -212,9 +214,9 @@ static PyObject *Color_slice(ColorObject * self, int begin, int end)
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
CLAMP(begin, 0, 3); CLAMP(begin, 0, COLOR_SIZE);
if (end<0) end= 4+end; if (end<0) end= (COLOR_SIZE + 1) + end;
CLAMP(end, 0, 3); CLAMP(end, 0, COLOR_SIZE);
begin = MIN2(begin,end); begin = MIN2(begin,end);
list = PyList_New(end - begin); list = PyList_New(end - begin);
@@ -227,61 +229,116 @@ static PyObject *Color_slice(ColorObject * self, int begin, int end)
} }
//----------------------------object[z:y]------------------------ //----------------------------object[z:y]------------------------
//sequence slice (set) //sequence slice (set)
static int Color_ass_slice(ColorObject * self, int begin, int end, static int Color_ass_slice(ColorObject * self, int begin, int end, PyObject * seq)
PyObject * seq)
{ {
int i, y, size = 0; int i, size;
float col[3]; float col[COLOR_SIZE];
PyObject *e;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return -1; return -1;
CLAMP(begin, 0, 3); CLAMP(begin, 0, COLOR_SIZE);
if (end<0) end= 4+end; if (end<0) end= (COLOR_SIZE + 1) + end;
CLAMP(end, 0, 3); CLAMP(end, 0, COLOR_SIZE);
begin = MIN2(begin,end); begin = MIN2(begin,end);
size = PySequence_Length(seq); if((size=mathutils_array_parse(col, 0, COLOR_SIZE, seq, "mathutils.Color[begin:end] = []")) == -1)
return -1;
if(size != (end - begin)){ if(size != (end - begin)){
PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: size mismatch in slice assignment"); PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: size mismatch in slice assignment");
return -1; return -1;
} }
for (i = 0; i < size; i++) { for(i= 0; i < COLOR_SIZE; i++)
e = PySequence_GetItem(seq, i); self->col[begin + i] = col[i];
if (e == NULL) { // Failed to read sequence
PyErr_SetString(PyExc_RuntimeError, "color[begin:end] = []: unable to read sequence");
return -1;
}
col[i] = (float)PyFloat_AsDouble(e);
Py_DECREF(e);
if(col[i]==-1 && PyErr_Occurred()) { // parsed item not a number
PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: sequence argument not a number");
return -1;
}
}
//parsed well - now set in vector
for(y = 0; y < 3; y++){
self->col[begin + y] = col[y];
}
BaseMath_WriteCallback(self); BaseMath_WriteCallback(self);
return 0; return 0;
} }
static PyObject *Color_subscript(ColorObject *self, PyObject *item)
{
if (PyIndex_Check(item)) {
Py_ssize_t i;
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
i += COLOR_SIZE;
return Color_item(self, i);
} else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
return NULL;
if (slicelength <= 0) {
return PyList_New(0);
}
else if (step == 1) {
return Color_slice(self, start, stop);
}
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
return NULL;
}
}
else {
PyErr_Format(PyExc_TypeError,
"euler indices must be integers, not %.200s",
item->ob_type->tp_name);
return NULL;
}
}
static int Color_ass_subscript(ColorObject *self, PyObject *item, PyObject *value)
{
if (PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return -1;
if (i < 0)
i += COLOR_SIZE;
return Color_ass_item(self, i, value);
}
else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
return -1;
if (step == 1)
return Color_ass_slice(self, start, stop, value);
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
return -1;
}
}
else {
PyErr_Format(PyExc_TypeError,
"euler indices must be integers, not %.200s",
item->ob_type->tp_name);
return -1;
}
}
//-----------------PROTCOL DECLARATIONS-------------------------- //-----------------PROTCOL DECLARATIONS--------------------------
static PySequenceMethods Color_SeqMethods = { static PySequenceMethods Color_SeqMethods = {
(lenfunc) Color_len, /* sq_length */ (lenfunc) Color_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */ (binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */ (ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Color_item, /* sq_item */ (ssizeargfunc) Color_item, /* sq_item */
(ssizessizeargfunc) Color_slice, /* sq_slice */ (ssizessizeargfunc) NULL, /* sq_slice, deprecated */
(ssizeobjargproc) Color_ass_item, /* sq_ass_item */ (ssizeobjargproc) Color_ass_item, /* sq_ass_item */
(ssizessizeobjargproc) Color_ass_slice, /* sq_ass_slice */ (ssizessizeobjargproc) NULL, /* sq_ass_slice, deprecated */
}; };
static PyMappingMethods Color_AsMapping = {
(lenfunc)Color_len,
(binaryfunc)Color_subscript,
(objobjargproc)Color_ass_subscript
};
/* color channel, vector.r/g/b */ /* color channel, vector.r/g/b */
static PyObject *Color_getChannel( ColorObject * self, void *type ) static PyObject *Color_getChannel( ColorObject * self, void *type )
@@ -414,7 +471,7 @@ PyTypeObject color_Type = {
(reprfunc) Color_repr, //tp_repr (reprfunc) Color_repr, //tp_repr
0, //tp_as_number 0, //tp_as_number
&Color_SeqMethods, //tp_as_sequence &Color_SeqMethods, //tp_as_sequence
0, //tp_as_mapping &Color_AsMapping, //tp_as_mapping
0, //tp_hash 0, //tp_hash
0, //tp_call 0, //tp_call
0, //tp_str 0, //tp_str
@@ -458,7 +515,6 @@ PyTypeObject color_Type = {
PyObject *newColorObject(float *col, int type, PyTypeObject *base_type) PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
{ {
ColorObject *self; ColorObject *self;
int x;
if(base_type) self = (ColorObject *)base_type->tp_alloc(base_type, 0); if(base_type) self = (ColorObject *)base_type->tp_alloc(base_type, 0);
else self = PyObject_NEW(ColorObject, &color_Type); else self = PyObject_NEW(ColorObject, &color_Type);
@@ -470,17 +526,17 @@ PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
if(type == Py_WRAP){ if(type == Py_WRAP){
self->col = col; self->col = col;
self->wrapped = Py_WRAP; self->wrapped = Py_WRAP;
}else if (type == Py_NEW){ }
self->col = PyMem_Malloc(3 * sizeof(float)); else if (type == Py_NEW){
if(!col) { //new empty self->col = PyMem_Malloc(COLOR_SIZE * sizeof(float));
for(x = 0; x < 3; x++) { if(col)
self->col[x] = 0.0f; copy_v3_v3(self->col, col);
} else
}else{ zero_v3(self->col);
VECCOPY(self->col, col);
}
self->wrapped = Py_NEW; self->wrapped = Py_NEW;
}else{ //bad type }
else {
return NULL; return NULL;
} }

View File

@@ -35,6 +35,8 @@
#include "BLO_sys_types.h" #include "BLO_sys_types.h"
#endif #endif
#define EULER_SIZE 3
//----------------------------------mathutils.Euler() ------------------- //----------------------------------mathutils.Euler() -------------------
//makes a new euler for you to play with //makes a new euler for you to play with
static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwargs) static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
@@ -42,8 +44,8 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar
PyObject *seq= NULL; PyObject *seq= NULL;
char *order_str= NULL; char *order_str= NULL;
float eul[3]= {0.0f, 0.0f, 0.0f}; float eul[EULER_SIZE]= {0.0f, 0.0f, 0.0f};
short order= 0; short order= EULER_ORDER_XYZ;
if(!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str)) if(!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
return NULL; return NULL;
@@ -56,7 +58,7 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar
return NULL; return NULL;
/* intentionally pass through */ /* intentionally pass through */
case 1: case 1:
if (mathutils_array_parse(eul, 3, 3, seq, "mathutils.Euler()") == -1) if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
return NULL; return NULL;
break; break;
} }
@@ -67,12 +69,12 @@ short euler_order_from_string(const char *str, const char *error_prefix)
{ {
if((str[0] && str[1] && str[2] && str[3]=='\0')) { if((str[0] && str[1] && str[2] && str[3]=='\0')) {
switch(*((int32_t *)str)) { switch(*((int32_t *)str)) {
case 'X'|'Y'<<8|'Z'<<16: return 0; case 'X'|'Y'<<8|'Z'<<16: return EULER_ORDER_XYZ;
case 'X'|'Z'<<8|'Y'<<16: return 1; case 'X'|'Z'<<8|'Y'<<16: return EULER_ORDER_XZY;
case 'Y'|'X'<<8|'Z'<<16: return 2; case 'Y'|'X'<<8|'Z'<<16: return EULER_ORDER_YXZ;
case 'Y'|'Z'<<8|'X'<<16: return 3; case 'Y'|'Z'<<8|'X'<<16: return EULER_ORDER_YZX;
case 'Z'|'X'<<8|'Y'<<16: return 4; case 'Z'|'X'<<8|'Y'<<16: return EULER_ORDER_ZXY;
case 'Z'|'Y'<<8|'X'<<16: return 5; case 'Z'|'Y'<<8|'X'<<16: return EULER_ORDER_ZYX;
} }
} }
@@ -86,15 +88,15 @@ static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
PyObject *ret; PyObject *ret;
int i; int i;
ret= PyTuple_New(3); ret= PyTuple_New(EULER_SIZE);
if(ndigits >= 0) { if(ndigits >= 0) {
for(i= 0; i < 3; i++) { for(i= 0; i < EULER_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits))); PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
} }
} }
else { else {
for(i= 0; i < 3; i++) { for(i= 0; i < EULER_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i])); PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
} }
} }
@@ -120,8 +122,8 @@ static PyObject *Euler_ToQuat(EulerObject * self)
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
if(self->order==0) eul_to_quat(quat, self->eul); if(self->order==EULER_ORDER_XYZ) eul_to_quat(quat, self->eul);
else eulO_to_quat(quat, self->eul, self->order); else eulO_to_quat(quat, self->eul, self->order);
return newQuaternionObject(quat, Py_NEW, NULL); return newQuaternionObject(quat, Py_NEW, NULL);
} }
@@ -142,8 +144,8 @@ static PyObject *Euler_ToMatrix(EulerObject * self)
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
if(self->order==0) eul_to_mat3((float (*)[3])mat, self->eul); if(self->order==EULER_ORDER_XYZ) eul_to_mat3((float (*)[3])mat, self->eul);
else eulO_to_mat3((float (*)[3])mat, self->eul, self->order); else eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
return newMatrixObject(mat, 3, 3 , Py_NEW, NULL); return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
} }
@@ -256,8 +258,8 @@ static PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
if(self->order == 0) rotate_eul(self->eul, *axis, angle); if(self->order == EULER_ORDER_XYZ) rotate_eul(self->eul, *axis, angle);
else rotate_eulO(self->eul, self->order, *axis, angle); else rotate_eulO(self->eul, self->order, *axis, angle);
BaseMath_WriteCallback(self); BaseMath_WriteCallback(self);
Py_INCREF(self); Py_INCREF(self);
@@ -367,10 +369,10 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
switch (comparison_type){ switch (comparison_type){
case Py_EQ: case Py_EQ:
result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1); result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
break; break;
case Py_NE: case Py_NE:
result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1); result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
break; break;
default: default:
printf("The result of the comparison could not be evaluated"); printf("The result of the comparison could not be evaluated");
@@ -388,15 +390,15 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
//sequence length //sequence length
static int Euler_len(EulerObject * self) static int Euler_len(EulerObject * self)
{ {
return 3; return EULER_SIZE;
} }
//----------------------------object[]--------------------------- //----------------------------object[]---------------------------
//sequence accessor (get) //sequence accessor (get)
static PyObject *Euler_item(EulerObject * self, int i) static PyObject *Euler_item(EulerObject * self, int i)
{ {
if(i<0) i= 3-i; if(i<0) i= EULER_SIZE-i;
if(i < 0 || i >= 3) { if(i < 0 || i >= EULER_SIZE) {
PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range"); PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
return NULL; return NULL;
} }
@@ -418,9 +420,9 @@ static int Euler_ass_item(EulerObject * self, int i, PyObject * value)
return -1; return -1;
} }
if(i<0) i= 3-i; if(i<0) i= EULER_SIZE-i;
if(i < 0 || i >= 3){ if(i < 0 || i >= EULER_SIZE){
PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range\n"); PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range\n");
return -1; return -1;
} }
@@ -442,9 +444,9 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
CLAMP(begin, 0, 3); CLAMP(begin, 0, EULER_SIZE);
if (end<0) end= 4+end; if (end<0) end= (EULER_SIZE + 1) + end;
CLAMP(end, 0, 3); CLAMP(end, 0, EULER_SIZE);
begin = MIN2(begin,end); begin = MIN2(begin,end);
list = PyList_New(end - begin); list = PyList_New(end - begin);
@@ -457,61 +459,117 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
} }
//----------------------------object[z:y]------------------------ //----------------------------object[z:y]------------------------
//sequence slice (set) //sequence slice (set)
static int Euler_ass_slice(EulerObject * self, int begin, int end, static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
PyObject * seq)
{ {
int i, y, size = 0; int i, size;
float eul[3]; float eul[EULER_SIZE];
PyObject *e;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return -1; return -1;
CLAMP(begin, 0, 3); CLAMP(begin, 0, EULER_SIZE);
if (end<0) end= 4+end; if (end<0) end= (EULER_SIZE + 1) + end;
CLAMP(end, 0, 3); CLAMP(end, 0, EULER_SIZE);
begin = MIN2(begin,end); begin = MIN2(begin,end);
size = PySequence_Length(seq); if((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
return -1;
if(size != (end - begin)){ if(size != (end - begin)){
PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment"); PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
return -1; return -1;
} }
for (i = 0; i < size; i++) { for(i= 0; i < EULER_SIZE; i++)
e = PySequence_GetItem(seq, i); self->eul[begin + i] = eul[i];
if (e == NULL) { // Failed to read sequence
PyErr_SetString(PyExc_RuntimeError, "euler[begin:end] = []: unable to read sequence");
return -1;
}
eul[i] = (float)PyFloat_AsDouble(e);
Py_DECREF(e);
if(eul[i]==-1 && PyErr_Occurred()) { // parsed item not a number
PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: sequence argument not a number");
return -1;
}
}
//parsed well - now set in vector
for(y = 0; y < 3; y++){
self->eul[begin + y] = eul[y];
}
BaseMath_WriteCallback(self); BaseMath_WriteCallback(self);
return 0; return 0;
} }
static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
{
if (PyIndex_Check(item)) {
Py_ssize_t i;
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
i += EULER_SIZE;
return Euler_item(self, i);
} else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
return NULL;
if (slicelength <= 0) {
return PyList_New(0);
}
else if (step == 1) {
return Euler_slice(self, start, stop);
}
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
return NULL;
}
}
else {
PyErr_Format(PyExc_TypeError,
"euler indices must be integers, not %.200s",
item->ob_type->tp_name);
return NULL;
}
}
static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
{
if (PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return -1;
if (i < 0)
i += EULER_SIZE;
return Euler_ass_item(self, i, value);
}
else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
return -1;
if (step == 1)
return Euler_ass_slice(self, start, stop, value);
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
return -1;
}
}
else {
PyErr_Format(PyExc_TypeError,
"euler indices must be integers, not %.200s",
item->ob_type->tp_name);
return -1;
}
}
//-----------------PROTCOL DECLARATIONS-------------------------- //-----------------PROTCOL DECLARATIONS--------------------------
static PySequenceMethods Euler_SeqMethods = { static PySequenceMethods Euler_SeqMethods = {
(lenfunc) Euler_len, /* sq_length */ (lenfunc) Euler_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */ (binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */ (ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Euler_item, /* sq_item */ (ssizeargfunc) Euler_item, /* sq_item */
(ssizessizeargfunc) Euler_slice, /* sq_slice */ (ssizessizeargfunc) NULL, /* sq_slice, deprecated */
(ssizeobjargproc) Euler_ass_item, /* sq_ass_item */ (ssizeobjargproc) Euler_ass_item, /* sq_ass_item */
(ssizessizeobjargproc) Euler_ass_slice, /* sq_ass_slice */ (ssizessizeobjargproc) NULL, /* sq_ass_slice, deprecated */
}; };
static PyMappingMethods Euler_AsMapping = {
(lenfunc)Euler_len,
(binaryfunc)Euler_subscript,
(objobjargproc)Euler_ass_subscript
};
/* /*
* euler axis, euler.x/y/z * euler axis, euler.x/y/z
@@ -530,7 +588,7 @@ static int Euler_setAxis( EulerObject * self, PyObject * value, void * type )
static PyObject *Euler_getOrder(EulerObject *self, void *type) static PyObject *Euler_getOrder(EulerObject *self, void *type)
{ {
static char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"}; static char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
return PyUnicode_FromString(order[self->order]); return PyUnicode_FromString(order[self->order-EULER_ORDER_XYZ]);
} }
static int Euler_setOrder( EulerObject * self, PyObject * value, void * type ) static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
@@ -538,7 +596,7 @@ static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
char *order_str= _PyUnicode_AsString(value); char *order_str= _PyUnicode_AsString(value);
short order= euler_order_from_string(order_str, "euler.order"); short order= euler_order_from_string(order_str, "euler.order");
if(order < 0) if(order == -1)
return -1; return -1;
if(self->cb_user) { if(self->cb_user) {
@@ -595,7 +653,7 @@ PyTypeObject euler_Type = {
(reprfunc) Euler_repr, //tp_repr (reprfunc) Euler_repr, //tp_repr
0, //tp_as_number 0, //tp_as_number
&Euler_SeqMethods, //tp_as_sequence &Euler_SeqMethods, //tp_as_sequence
0, //tp_as_mapping &Euler_AsMapping, //tp_as_mapping
0, //tp_hash 0, //tp_hash
0, //tp_call 0, //tp_call
0, //tp_str 0, //tp_str
@@ -639,7 +697,6 @@ PyTypeObject euler_Type = {
PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type) PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
{ {
EulerObject *self; EulerObject *self;
int x;
if(base_type) self = (EulerObject *)base_type->tp_alloc(base_type, 0); if(base_type) self = (EulerObject *)base_type->tp_alloc(base_type, 0);
else self = PyObject_NEW(EulerObject, &euler_Type); else self = PyObject_NEW(EulerObject, &euler_Type);
@@ -648,20 +705,20 @@ PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_t
self->cb_user= NULL; self->cb_user= NULL;
self->cb_type= self->cb_subtype= 0; self->cb_type= self->cb_subtype= 0;
if(type == Py_WRAP){ if(type == Py_WRAP) {
self->eul = eul; self->eul = eul;
self->wrapped = Py_WRAP; self->wrapped = Py_WRAP;
}else if (type == Py_NEW){ }
self->eul = PyMem_Malloc(3 * sizeof(float)); else if (type == Py_NEW){
if(!eul) { //new empty self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
for(x = 0; x < 3; x++) { if(eul)
self->eul[x] = 0.0f; copy_v3_v3(self->eul, eul);
} else
}else{ zero_v3(self->eul);
VECCOPY(self->eul, eul);
}
self->wrapped = Py_NEW; self->wrapped = Py_NEW;
}else{ //bad type }
else{
return NULL; return NULL;
} }

View File

@@ -245,7 +245,7 @@ static char Matrix_toEuler_doc[] =
PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args) PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
{ {
char *order_str= NULL; char *order_str= NULL;
short order= 0; short order= EULER_ORDER_XYZ;
float eul[3], eul_compatf[3]; float eul[3], eul_compatf[3];
EulerObject *eul_compat = NULL; EulerObject *eul_compat = NULL;
@@ -262,7 +262,7 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
if(!BaseMath_ReadCallback(eul_compat)) if(!BaseMath_ReadCallback(eul_compat))
return NULL; return NULL;
VECCOPY(eul_compatf, eul_compat->eul); copy_v3_v3(eul_compatf, eul_compat->eul);
} }
/*must be 3-4 cols, 3-4 rows, square matrix*/ /*must be 3-4 cols, 3-4 rows, square matrix*/
@@ -279,16 +279,16 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
if(order_str) { if(order_str) {
order= euler_order_from_string(order_str, "Matrix.to_euler()"); order= euler_order_from_string(order_str, "Matrix.to_euler()");
if(order < 0) if(order == -1)
return NULL; return NULL;
} }
if(eul_compat) { if(eul_compat) {
if(order == 0) mat3_to_compatible_eul( eul, eul_compatf, mat); if(order == 1) mat3_to_compatible_eul( eul, eul_compatf, mat);
else mat3_to_compatible_eulO(eul, eul_compatf, order, mat); else mat3_to_compatible_eulO(eul, eul_compatf, order, mat);
} }
else { else {
if(order == 0) mat3_to_eul(eul, mat); if(order == 1) mat3_to_eul(eul, mat);
else mat3_to_eulO(eul, order, mat); else mat3_to_eulO(eul, order, mat);
} }

View File

@@ -31,6 +31,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BKE_utildefines.h" #include "BKE_utildefines.h"
#define QUAT_SIZE 4
//-----------------------------METHODS------------------------------ //-----------------------------METHODS------------------------------
/* note: BaseMath_ReadCallback must be called beforehand */ /* note: BaseMath_ReadCallback must be called beforehand */
@@ -39,15 +41,15 @@ static PyObject *Quaternion_ToTupleExt(QuaternionObject *self, int ndigits)
PyObject *ret; PyObject *ret;
int i; int i;
ret= PyTuple_New(4); ret= PyTuple_New(QUAT_SIZE);
if(ndigits >= 0) { if(ndigits >= 0) {
for(i= 0; i < 4; i++) { for(i= 0; i < QUAT_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits))); PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits)));
} }
} }
else { else {
for(i= 0; i < 4; i++) { for(i= 0; i < QUAT_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i])); PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i]));
} }
} }
@@ -71,7 +73,7 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
{ {
float eul[3]; float eul[3];
char *order_str= NULL; char *order_str= NULL;
short order= 0; short order= EULER_ORDER_XYZ;
EulerObject *eul_compat = NULL; EulerObject *eul_compat = NULL;
if(!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat)) if(!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
@@ -83,7 +85,7 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
if(order_str) { if(order_str) {
order= euler_order_from_string(order_str, "Matrix.to_euler()"); order= euler_order_from_string(order_str, "Matrix.to_euler()");
if(order < 0) if(order == -1)
return NULL; return NULL;
} }
@@ -95,12 +97,12 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
quat_to_mat3(mat, self->quat); quat_to_mat3(mat, self->quat);
if(order == 0) mat3_to_compatible_eul(eul, eul_compat->eul, mat); if(order == EULER_ORDER_XYZ) mat3_to_compatible_eul(eul, eul_compat->eul, mat);
else mat3_to_compatible_eulO(eul, eul_compat->eul, order, mat); else mat3_to_compatible_eulO(eul, eul_compat->eul, order, mat);
} }
else { else {
if(order == 0) quat_to_eul(eul, self->quat); if(order == EULER_ORDER_XYZ) quat_to_eul(eul, self->quat);
else quat_to_eulO(eul, order, self->quat); else quat_to_eulO(eul, order, self->quat);
} }
return newEulerObject(eul, order, Py_NEW, NULL); return newEulerObject(eul, order, Py_NEW, NULL);
@@ -138,7 +140,7 @@ static char Quaternion_Cross_doc[] =
static PyObject *Quaternion_Cross(QuaternionObject * self, QuaternionObject * value) static PyObject *Quaternion_Cross(QuaternionObject * self, QuaternionObject * value)
{ {
float quat[4]; float quat[QUAT_SIZE];
if (!QuaternionObject_Check(value)) { if (!QuaternionObject_Check(value)) {
PyErr_SetString( PyExc_TypeError, "quat.cross(value): expected a quaternion argument" ); PyErr_SetString( PyExc_TypeError, "quat.cross(value): expected a quaternion argument" );
@@ -188,7 +190,7 @@ static char Quaternion_Difference_doc[] =
static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject * value) static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject * value)
{ {
float quat[4], tempQuat[4]; float quat[QUAT_SIZE], tempQuat[QUAT_SIZE];
double dot = 0.0f; double dot = 0.0f;
int x; int x;
@@ -200,15 +202,11 @@ static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject
if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value)) if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
return NULL; return NULL;
tempQuat[0] = self->quat[0]; copy_qt_qt(tempQuat, self->quat);
tempQuat[1] = - self->quat[1]; conjugate_qt(tempQuat);
tempQuat[2] = - self->quat[2]; dot = sqrt(dot_qtqt(tempQuat, tempQuat));
tempQuat[3] = - self->quat[3];
dot = sqrt(tempQuat[0] * tempQuat[0] + tempQuat[1] * tempQuat[1] + for(x = 0; x < QUAT_SIZE; x++) {
tempQuat[2] * tempQuat[2] + tempQuat[3] * tempQuat[3]);
for(x = 0; x < 4; x++) {
tempQuat[x] /= (float)(dot * dot); tempQuat[x] /= (float)(dot * dot);
} }
mul_qt_qtqt(quat, tempQuat, value->quat); mul_qt_qtqt(quat, tempQuat, value->quat);
@@ -230,7 +228,7 @@ static char Quaternion_Slerp_doc[] =
static PyObject *Quaternion_Slerp(QuaternionObject *self, PyObject *args) static PyObject *Quaternion_Slerp(QuaternionObject *self, PyObject *args)
{ {
QuaternionObject *value; QuaternionObject *value;
float quat[4], fac; float quat[QUAT_SIZE], fac;
if(!PyArg_ParseTuple(args, "O!f:slerp", &quaternion_Type, &value, &fac)) { if(!PyArg_ParseTuple(args, "O!f:slerp", &quaternion_Type, &value, &fac)) {
PyErr_SetString(PyExc_TypeError, "quat.slerp(): expected Quaternion types and float"); PyErr_SetString(PyExc_TypeError, "quat.slerp(): expected Quaternion types and float");
@@ -415,10 +413,10 @@ static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int c
switch (comparison_type){ switch (comparison_type){
case Py_EQ: case Py_EQ:
result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, 4, 1); result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
break; break;
case Py_NE: case Py_NE:
result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, 4, 1); result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
if (result == 0){ if (result == 0){
result = 1; result = 1;
}else{ }else{
@@ -441,15 +439,15 @@ static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int c
//sequence length //sequence length
static int Quaternion_len(QuaternionObject * self) static int Quaternion_len(QuaternionObject * self)
{ {
return 4; return QUAT_SIZE;
} }
//----------------------------object[]--------------------------- //----------------------------object[]---------------------------
//sequence accessor (get) //sequence accessor (get)
static PyObject *Quaternion_item(QuaternionObject * self, int i) static PyObject *Quaternion_item(QuaternionObject * self, int i)
{ {
if(i<0) i= 4-i; if(i<0) i= QUAT_SIZE-i;
if(i < 0 || i >= 4) { if(i < 0 || i >= QUAT_SIZE) {
PyErr_SetString(PyExc_IndexError, "quaternion[attribute]: array index out of range\n"); PyErr_SetString(PyExc_IndexError, "quaternion[attribute]: array index out of range\n");
return NULL; return NULL;
} }
@@ -470,9 +468,9 @@ static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
return -1; return -1;
} }
if(i<0) i= 4-i; if(i<0) i= QUAT_SIZE-i;
if(i < 0 || i >= 4){ if(i < 0 || i >= QUAT_SIZE){
PyErr_SetString(PyExc_IndexError, "quaternion[attribute] = x: array assignment index out of range\n"); PyErr_SetString(PyExc_IndexError, "quaternion[attribute] = x: array assignment index out of range\n");
return -1; return -1;
} }
@@ -493,9 +491,9 @@ static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
CLAMP(begin, 0, 4); CLAMP(begin, 0, QUAT_SIZE);
if (end<0) end= 5+end; if (end<0) end= (QUAT_SIZE + 1) + end;
CLAMP(end, 0, 4); CLAMP(end, 0, QUAT_SIZE);
begin = MIN2(begin,end); begin = MIN2(begin,end);
list = PyList_New(end - begin); list = PyList_New(end - begin);
@@ -510,52 +508,107 @@ static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
//sequence slice (set) //sequence slice (set)
static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end, PyObject * seq) static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end, PyObject * seq)
{ {
int i, y, size = 0; int i, size;
float quat[4]; float quat[QUAT_SIZE];
PyObject *q;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return -1; return -1;
CLAMP(begin, 0, 4); CLAMP(begin, 0, QUAT_SIZE);
if (end<0) end= 5+end; if (end<0) end= (QUAT_SIZE + 1) + end;
CLAMP(end, 0, 4); CLAMP(end, 0, QUAT_SIZE);
begin = MIN2(begin,end); begin = MIN2(begin,end);
size = PySequence_Length(seq); if((size=mathutils_array_parse(quat, 0, QUAT_SIZE, seq, "mathutils.Quaternion[begin:end] = []")) == -1)
return -1;
if(size != (end - begin)){ if(size != (end - begin)){
PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment\n"); PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment");
return -1; return -1;
} }
for (i = 0; i < size; i++) { /* parsed well - now set in vector */
q = PySequence_GetItem(seq, i); for(i= 0; i < size; i++)
if (q == NULL) { // Failed to read sequence self->quat[begin + i] = quat[i];
PyErr_SetString(PyExc_RuntimeError, "quaternion[begin:end] = []: unable to read sequence\n");
return -1;
}
quat[i]= (float)PyFloat_AsDouble(q);
Py_DECREF(q);
if(quat[i]==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: sequence argument not a number\n");
return -1;
}
}
//parsed well - now set in vector
for(y = 0; y < size; y++)
self->quat[begin + y] = quat[y];
BaseMath_WriteCallback(self); BaseMath_WriteCallback(self);
return 0; return 0;
} }
static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
{
if (PyIndex_Check(item)) {
Py_ssize_t i;
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
i += QUAT_SIZE;
return Quaternion_item(self, i);
} else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
return NULL;
if (slicelength <= 0) {
return PyList_New(0);
}
else if (step == 1) {
return Quaternion_slice(self, start, stop);
}
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternions");
return NULL;
}
}
else {
PyErr_Format(PyExc_TypeError,
"quaternion indices must be integers, not %.200s",
item->ob_type->tp_name);
return NULL;
}
}
static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyObject *value)
{
if (PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return -1;
if (i < 0)
i += QUAT_SIZE;
return Quaternion_ass_item(self, i, value);
}
else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
return -1;
if (step == 1)
return Quaternion_ass_slice(self, start, stop, value);
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternion");
return -1;
}
}
else {
PyErr_Format(PyExc_TypeError,
"quaternion indices must be integers, not %.200s",
item->ob_type->tp_name);
return -1;
}
}
//------------------------NUMERIC PROTOCOLS---------------------- //------------------------NUMERIC PROTOCOLS----------------------
//------------------------obj + obj------------------------------ //------------------------obj + obj------------------------------
//addition //addition
static PyObject *Quaternion_add(PyObject * q1, PyObject * q2) static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
{ {
float quat[4]; float quat[QUAT_SIZE];
QuaternionObject *quat1 = NULL, *quat2 = NULL; QuaternionObject *quat1 = NULL, *quat2 = NULL;
if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) { if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
@@ -576,7 +629,7 @@ static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2) static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
{ {
int x; int x;
float quat[4]; float quat[QUAT_SIZE];
QuaternionObject *quat1 = NULL, *quat2 = NULL; QuaternionObject *quat1 = NULL, *quat2 = NULL;
if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) { if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
@@ -590,7 +643,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2)) if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
return NULL; return NULL;
for(x = 0; x < 4; x++) { for(x = 0; x < QUAT_SIZE; x++) {
quat[x] = quat1->quat[x] - quat2->quat[x]; quat[x] = quat1->quat[x] - quat2->quat[x];
} }
@@ -600,7 +653,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
//mulplication //mulplication
static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2) static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
{ {
float quat[4], scalar; float quat[QUAT_SIZE], scalar;
QuaternionObject *quat1 = NULL, *quat2 = NULL; QuaternionObject *quat1 = NULL, *quat2 = NULL;
VectorObject *vec = NULL; VectorObject *vec = NULL;
@@ -658,46 +711,52 @@ static PySequenceMethods Quaternion_SeqMethods = {
(binaryfunc) 0, /* sq_concat */ (binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */ (ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Quaternion_item, /* sq_item */ (ssizeargfunc) Quaternion_item, /* sq_item */
(ssizessizeargfunc) Quaternion_slice, /* sq_slice */ (ssizessizeargfunc) NULL, /* sq_slice, deprecated */
(ssizeobjargproc) Quaternion_ass_item, /* sq_ass_item */ (ssizeobjargproc) Quaternion_ass_item, /* sq_ass_item */
(ssizessizeobjargproc) Quaternion_ass_slice, /* sq_ass_slice */ (ssizessizeobjargproc) NULL, /* sq_ass_slice, deprecated */
};
static PyMappingMethods Quaternion_AsMapping = {
(lenfunc)Quaternion_len,
(binaryfunc)Quaternion_subscript,
(objobjargproc)Quaternion_ass_subscript
}; };
static PyNumberMethods Quaternion_NumMethods = { static PyNumberMethods Quaternion_NumMethods = {
(binaryfunc) Quaternion_add, /*nb_add*/ (binaryfunc) Quaternion_add, /*nb_add*/
(binaryfunc) Quaternion_sub, /*nb_subtract*/ (binaryfunc) Quaternion_sub, /*nb_subtract*/
(binaryfunc) Quaternion_mul, /*nb_multiply*/ (binaryfunc) Quaternion_mul, /*nb_multiply*/
0, /*nb_remainder*/ 0, /*nb_remainder*/
0, /*nb_divmod*/ 0, /*nb_divmod*/
0, /*nb_power*/ 0, /*nb_power*/
(unaryfunc) 0, /*nb_negative*/ (unaryfunc) 0, /*nb_negative*/
(unaryfunc) 0, /*tp_positive*/ (unaryfunc) 0, /*tp_positive*/
(unaryfunc) 0, /*tp_absolute*/ (unaryfunc) 0, /*tp_absolute*/
(inquiry) 0, /*tp_bool*/ (inquiry) 0, /*tp_bool*/
(unaryfunc) 0, /*nb_invert*/ (unaryfunc) 0, /*nb_invert*/
0, /*nb_lshift*/ 0, /*nb_lshift*/
(binaryfunc)0, /*nb_rshift*/ (binaryfunc)0, /*nb_rshift*/
0, /*nb_and*/ 0, /*nb_and*/
0, /*nb_xor*/ 0, /*nb_xor*/
0, /*nb_or*/ 0, /*nb_or*/
0, /*nb_int*/ 0, /*nb_int*/
0, /*nb_reserved*/ 0, /*nb_reserved*/
0, /*nb_float*/ 0, /*nb_float*/
0, /* nb_inplace_add */ 0, /* nb_inplace_add */
0, /* nb_inplace_subtract */ 0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */ 0, /* nb_inplace_multiply */
0, /* nb_inplace_remainder */ 0, /* nb_inplace_remainder */
0, /* nb_inplace_power */ 0, /* nb_inplace_power */
0, /* nb_inplace_lshift */ 0, /* nb_inplace_lshift */
0, /* nb_inplace_rshift */ 0, /* nb_inplace_rshift */
0, /* nb_inplace_and */ 0, /* nb_inplace_and */
0, /* nb_inplace_xor */ 0, /* nb_inplace_xor */
0, /* nb_inplace_or */ 0, /* nb_inplace_or */
0, /* nb_floor_divide */ 0, /* nb_floor_divide */
0, /* nb_true_divide */ 0, /* nb_true_divide */
0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */ 0, /* nb_inplace_true_divide */
0, /* nb_index */ 0, /* nb_index */
}; };
static PyObject *Quaternion_getAxis( QuaternionObject * self, void *type ) static PyObject *Quaternion_getAxis( QuaternionObject * self, void *type )
@@ -722,16 +781,11 @@ static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type )
static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type ) static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type )
{ {
int i;
float vec[3]; float vec[3];
double mag = self->quat[0] * (Py_PI / 180);
mag = 2 * (saacos(mag)); normalize_v3_v3(vec, self->quat+1);
mag = sin(mag / 2);
for(i = 0; i < 3; i++) /* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations */
vec[i] = (float)(self->quat[i + 1] / mag);
normalize_v3(vec);
//If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations
if( EXPP_FloatsAreEqual(vec[0], 0.0f, 10) && if( EXPP_FloatsAreEqual(vec[0], 0.0f, 10) &&
EXPP_FloatsAreEqual(vec[1], 0.0f, 10) && EXPP_FloatsAreEqual(vec[1], 0.0f, 10) &&
EXPP_FloatsAreEqual(vec[2], 0.0f, 10) ){ EXPP_FloatsAreEqual(vec[2], 0.0f, 10) ){
@@ -745,7 +799,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw
{ {
PyObject *seq= NULL; PyObject *seq= NULL;
double angle = 0.0f; double angle = 0.0f;
float quat[4]= {0.0f, 0.0f, 0.0f, 0.0f}; float quat[QUAT_SIZE]= {0.0f, 0.0f, 0.0f, 0.0f};
if(!PyArg_ParseTuple(args, "|Of:mathutils.Quaternion", &seq, &angle)) if(!PyArg_ParseTuple(args, "|Of:mathutils.Quaternion", &seq, &angle))
return NULL; return NULL;
@@ -754,7 +808,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw
case 0: case 0:
break; break;
case 1: case 1:
if (mathutils_array_parse(quat, 4, 4, seq, "mathutils.Quaternion()") == -1) if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
return NULL; return NULL;
break; break;
case 2: case 2:
@@ -816,10 +870,10 @@ PyTypeObject quaternion_Type = {
0, //tp_getattr 0, //tp_getattr
0, //tp_setattr 0, //tp_setattr
0, //tp_compare 0, //tp_compare
(reprfunc) Quaternion_repr, //tp_repr (reprfunc) Quaternion_repr, //tp_repr
&Quaternion_NumMethods, //tp_as_number &Quaternion_NumMethods, //tp_as_number
&Quaternion_SeqMethods, //tp_as_sequence &Quaternion_SeqMethods, //tp_as_sequence
0, //tp_as_mapping &Quaternion_AsMapping, //tp_as_mapping
0, //tp_hash 0, //tp_hash
0, //tp_call 0, //tp_call
0, //tp_str 0, //tp_str
@@ -875,7 +929,7 @@ PyObject *newQuaternionObject(float *quat, int type, PyTypeObject *base_type)
self->quat = quat; self->quat = quat;
self->wrapped = Py_WRAP; self->wrapped = Py_WRAP;
}else if (type == Py_NEW){ }else if (type == Py_NEW){
self->quat = PyMem_Malloc(4 * sizeof(float)); self->quat = PyMem_Malloc(QUAT_SIZE * sizeof(float));
if(!quat) { //new empty if(!quat) { //new empty
unit_qt(self->quat); unit_qt(self->quat);
}else{ }else{

View File

@@ -241,12 +241,22 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
case PROP_EULER: case PROP_EULER:
case PROP_QUATERNION: case PROP_QUATERNION:
if(len==3) { /* euler */ if(len==3) { /* euler */
/* attempt to get order */
/* TODO, keep order in sync */
short order= ROT_MODE_XYZ;
PropertyRNA *prop_eul_order= RNA_struct_find_property(ptr, "rotation_mode");
if(prop_eul_order) {
order = RNA_property_enum_get(ptr, prop_eul_order);
if (order < ROT_MODE_XYZ || order > ROT_MODE_ZYX) /* could be quat or axisangle */
order= ROT_MODE_XYZ;
}
if(is_thick) { if(is_thick) {
ret= newEulerObject(NULL, 0, Py_NEW, NULL); // TODO, get order from RNA ret= newEulerObject(NULL, order, Py_NEW, NULL); // TODO, get order from RNA
RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul); RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
} }
else { else {
PyObject *eul_cb= newEulerObject_cb(ret, 0, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA PyObject *eul_cb= newEulerObject_cb(ret, order, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
Py_DECREF(ret); /* the euler owns now */ Py_DECREF(ret); /* the euler owns now */
ret= eul_cb; /* return the euler instead */ ret= eul_cb; /* return the euler instead */
} }