Merged changes in the trunk up to revision 28536.

This commit is contained in:
2010-05-02 23:10:22 +00:00
623 changed files with 22402 additions and 21879 deletions

View File

@@ -36,10 +36,6 @@
#ifndef EXPP_BGL_H
#define EXPP_BGL_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <Python.h>
PyObject *BGL_Init(void);

View File

@@ -28,25 +28,26 @@
#include "../../blenfont/BLF_api.h"
static char py_blf_position_doc[] =
".. function:: position(x, y, z)\n"
".. function:: position(fontid, x, y, z)\n"
"\n"
" Set the position for drawing text.\n";
static PyObject *py_blf_position(PyObject *self, PyObject *args)
{
int fontid;
float x, y, z;
if (!PyArg_ParseTuple(args, "fff:BLF.position", &x, &y, &z))
if (!PyArg_ParseTuple(args, "ifff:BLF.position", &fontid, &x, &y, &z))
return NULL;
BLF_position(x, y, z);
BLF_position(fontid, x, y, z);
Py_RETURN_NONE;
}
static char py_blf_size_doc[] =
".. function:: size(size, dpi)\n"
".. function:: size(fontid, size, dpi)\n"
"\n"
" Set the size and dpi for drawing text.\n"
"\n"
@@ -57,19 +58,19 @@ static char py_blf_size_doc[] =
static PyObject *py_blf_size(PyObject *self, PyObject *args)
{
int size, dpi;
int fontid, size, dpi;
if (!PyArg_ParseTuple(args, "ii:BLF.size", &size, &dpi))
if (!PyArg_ParseTuple(args, "iii:BLF.size", &fontid, &size, &dpi))
return NULL;
BLF_size(size, dpi);
BLF_size(fontid, size, dpi);
Py_RETURN_NONE;
}
static char py_blf_aspect_doc[] =
".. function:: aspect(aspect)\n"
".. function:: aspect(fontid, aspect)\n"
"\n"
" Set the aspect for drawing text.\n"
"\n"
@@ -79,18 +80,19 @@ static char py_blf_aspect_doc[] =
static PyObject *py_blf_aspect(PyObject *self, PyObject *args)
{
float aspect;
int fontid;
if (!PyArg_ParseTuple(args, "f:BLF.aspect", &aspect))
if (!PyArg_ParseTuple(args, "if:BLF.aspect", &fontid, &aspect))
return NULL;
BLF_aspect(aspect);
BLF_aspect(fontid, aspect);
Py_RETURN_NONE;
}
static char py_blf_blur_doc[] =
".. function:: blur(radius)\n"
".. function:: blur(fontid, radius)\n"
"\n"
" Set the blur radius for drawing text.\n"
"\n"
@@ -99,19 +101,19 @@ static char py_blf_blur_doc[] =
static PyObject *py_blf_blur(PyObject *self, PyObject *args)
{
int blur;
int blur, fontid;
if (!PyArg_ParseTuple(args, "i:BLF.blur", &blur))
if (!PyArg_ParseTuple(args, "ii:BLF.blur", &fontid, &blur))
return NULL;
BLF_blur(blur);
BLF_blur(fontid, blur);
Py_RETURN_NONE;
}
static char py_blf_draw_doc[] =
".. function:: draw(text)\n"
".. function:: draw(fontid, text)\n"
"\n"
" Draw text in the current context.\n"
"\n"
@@ -121,17 +123,18 @@ static char py_blf_draw_doc[] =
static PyObject *py_blf_draw(PyObject *self, PyObject *args)
{
char *text;
int fontid;
if (!PyArg_ParseTuple(args, "s:BLF.draw", &text))
if (!PyArg_ParseTuple(args, "is:BLF.draw", &fontid, &text))
return NULL;
BLF_draw(text);
BLF_draw(fontid, text);
Py_RETURN_NONE;
}
static char py_blf_dimensions_doc[] =
".. function:: dimensions(text)\n"
".. function:: dimensions(fontid, text)\n"
"\n"
" Return the width and hight of the text.\n"
"\n"
@@ -145,11 +148,12 @@ static PyObject *py_blf_dimensions(PyObject *self, PyObject *args)
char *text;
float r_width, r_height;
PyObject *ret;
int fontid;
if (!PyArg_ParseTuple(args, "s:BLF.dimensions", &text))
if (!PyArg_ParseTuple(args, "is:BLF.dimensions", &fontid, &text))
return NULL;
BLF_width_and_height(text, &r_width, &r_height);
BLF_width_and_height(fontid, text, &r_width, &r_height);
ret= PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(r_width));
@@ -158,24 +162,25 @@ static PyObject *py_blf_dimensions(PyObject *self, PyObject *args)
}
static char py_blf_clipping_doc[] =
".. function:: clipping(xmin, ymin, xmax, ymax)\n"
".. function:: clipping(fontid, xmin, ymin, xmax, ymax)\n"
"\n"
" Set the clipping, enable/disable using CLIPPING.\n";
static PyObject *py_blf_clipping(PyObject *self, PyObject *args)
{
float xmin, ymin, xmax, ymax;
int fontid;
if (!PyArg_ParseTuple(args, "ffff:BLF.clipping", &xmin, &ymin, &xmax, &ymax))
if (!PyArg_ParseTuple(args, "iffff:BLF.clipping", &fontid, &xmin, &ymin, &xmax, &ymax))
return NULL;
BLF_clipping(xmin, ymin, xmax, ymax);
BLF_clipping(fontid, xmin, ymin, xmax, ymax);
Py_RETURN_NONE;
}
static char py_blf_disable_doc[] =
".. function:: disable(option)\n"
".. function:: disable(fontid, option)\n"
"\n"
" Disable option.\n"
"\n"
@@ -184,18 +189,18 @@ static char py_blf_disable_doc[] =
static PyObject *py_blf_disable(PyObject *self, PyObject *args)
{
int option;
int option, fontid;
if (!PyArg_ParseTuple(args, "i:BLF.disable", &option))
if (!PyArg_ParseTuple(args, "ii:BLF.disable", &fontid, &option))
return NULL;
BLF_disable(option);
BLF_disable(fontid, option);
Py_RETURN_NONE;
}
static char py_blf_enable_doc[] =
".. function:: enable(option)\n"
".. function:: enable(fontid, option)\n"
"\n"
" Enable option.\n"
"\n"
@@ -204,18 +209,18 @@ static char py_blf_enable_doc[] =
static PyObject *py_blf_enable(PyObject *self, PyObject *args)
{
int option;
int option, fontid;
if (!PyArg_ParseTuple(args, "i:BLF.enable", &option))
if (!PyArg_ParseTuple(args, "ii:BLF.enable", &fontid, &option))
return NULL;
BLF_enable(option);
BLF_enable(fontid, option);
Py_RETURN_NONE;
}
static char py_blf_rotation_doc[] =
".. function:: rotation(angle)\n"
".. function:: rotation(fontid, angle)\n"
"\n"
" Set the text rotation angle, enable/disable using ROTATION.\n"
"\n"
@@ -225,17 +230,18 @@ static char py_blf_rotation_doc[] =
static PyObject *py_blf_rotation(PyObject *self, PyObject *args)
{
float angle;
int fontid;
if (!PyArg_ParseTuple(args, "f:BLF.rotation", &angle))
if (!PyArg_ParseTuple(args, "if:BLF.rotation", &fontid, &angle))
return NULL;
BLF_rotation(angle);
BLF_rotation(fontid, angle);
Py_RETURN_NONE;
}
static char py_blf_shadow_doc[] =
".. function:: shadow(level, r, g, b, a)\n"
".. function:: shadow(fontid, level, r, g, b, a)\n"
"\n"
" Shadow options, enable/disable using SHADOW .\n"
"\n"
@@ -244,10 +250,10 @@ static char py_blf_shadow_doc[] =
static PyObject *py_blf_shadow(PyObject *self, PyObject *args)
{
int level;
int level, fontid;
float r, g, b, a;
if (!PyArg_ParseTuple(args, "iffff:BLF.shadow", &level, &r, &g, &b, &a))
if (!PyArg_ParseTuple(args, "iiffff:BLF.shadow", &fontid, &level, &r, &g, &b, &a))
return NULL;
if (level != 0 && level != 3 && level != 5) {
@@ -255,24 +261,24 @@ static PyObject *py_blf_shadow(PyObject *self, PyObject *args)
return NULL;
}
BLF_shadow(level, r, g, b, a);
BLF_shadow(fontid, level, r, g, b, a);
Py_RETURN_NONE;
}
static char py_blf_shadow_offset_doc[] =
".. function:: shadow_offset(x, y)\n"
".. function:: shadow_offset(fontid, x, y)\n"
"\n"
" Set the offset for shadow text.\n";
static PyObject *py_blf_shadow_offset(PyObject *self, PyObject *args)
{
int x, y;
int x, y, fontid;
if (!PyArg_ParseTuple(args, "ii:BLF.shadow_offset", &x, &y))
if (!PyArg_ParseTuple(args, "iii:BLF.shadow_offset", &fontid, &x, &y))
return NULL;
BLF_shadow_offset(x, y);
BLF_shadow_offset(fontid, x, y);
Py_RETURN_NONE;
}

View File

@@ -29,6 +29,7 @@
/* Note: Changes to Mathutils since 2.4x
* use radians rather then degrees
* - Mathutils.Vector/Euler/Quaternion(), now only take single sequence arguments.
* - Mathutils.MidpointVecs --> vector.lerp(other, fac)
* - Mathutils.AngleBetweenVecs --> vector.angle(other)
* - Mathutils.ProjectVecs --> vector.project(other)
@@ -55,6 +56,42 @@
static char M_Mathutils_doc[] =
"This module provides access to matrices, eulers, quaternions and vectors.";
/* helper functionm returns length of the 'value', -1 on error */
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
{
PyObject *value_fast= NULL;
int i, size;
/* non list/tuple cases */
if(!(value_fast=PySequence_Fast(value, error_prefix))) {
/* PySequence_Fast sets the error */
return -1;
}
size= PySequence_Fast_GET_SIZE(value_fast);
if(size > array_max || size < array_min) {
if (array_max == array_min) PyErr_Format(PyExc_ValueError, "%.200s: sequence size is %d, expected %d", error_prefix, size, array_max);
else PyErr_Format(PyExc_ValueError, "%.200s: sequence size is %d, expected [%d - %d]", error_prefix, size, array_min, array_max);
Py_DECREF(value_fast);
return -1;
}
i= size;
do {
i--;
if(((array[i]= PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value_fast, i))) == -1.0) && PyErr_Occurred()) {
PyErr_Format(PyExc_ValueError, "%.200s: sequence index %d is not a float", error_prefix, i);
Py_DECREF(value_fast);
return -1;
}
} while(i);
Py_XDECREF(value_fast);
return size;
}
//-----------------------------METHODS----------------------------
//-----------------quat_rotation (internal)-----------
//This function multiplies a vector/point * quat or vice versa
@@ -609,7 +646,7 @@ int Mathutils_RegisterCallback(Mathutils_Callback *cb)
int _BaseMathObject_ReadCallback(BaseMathObject *self)
{
Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->get(self->cb_user, self->cb_subtype, self->data))
if(cb->get(self, self->cb_subtype))
return 1;
PyErr_Format(PyExc_SystemError, "%s user has become invalid", Py_TYPE(self)->tp_name);
@@ -619,7 +656,7 @@ int _BaseMathObject_ReadCallback(BaseMathObject *self)
int _BaseMathObject_WriteCallback(BaseMathObject *self)
{
Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->set(self->cb_user, self->cb_subtype, self->data))
if(cb->set(self, self->cb_subtype))
return 1;
PyErr_Format(PyExc_SystemError, "%s user has become invalid", Py_TYPE(self)->tp_name);
@@ -629,7 +666,7 @@ int _BaseMathObject_WriteCallback(BaseMathObject *self)
int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
{
Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->get_index(self->cb_user, self->cb_subtype, self->data, index))
if(cb->get_index(self, self->cb_subtype, index))
return 1;
PyErr_Format(PyExc_SystemError, "%s user has become invalid", Py_TYPE(self)->tp_name);
@@ -639,7 +676,7 @@ int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
{
Mathutils_Callback *cb= mathutils_callbacks[self->cb_type];
if(cb->set_index(self->cb_user, self->cb_subtype, self->data, index))
if(cb->set_index(self, self->cb_subtype, index))
return 1;
PyErr_Format(PyExc_SystemError, "%s user has become invalid", Py_TYPE(self)->tp_name);

View File

@@ -88,18 +88,18 @@ int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps);
typedef struct Mathutils_Callback Mathutils_Callback;
typedef int (*BaseMathCheckFunc)(PyObject *);
typedef int (*BaseMathGetFunc)(PyObject *, int, float *);
typedef int (*BaseMathSetFunc)(PyObject *, int, float *);
typedef int (*BaseMathGetIndexFunc)(PyObject *, int, float *, int);
typedef int (*BaseMathSetIndexFunc)(PyObject *, int, float *, int);
typedef int (*BaseMathCheckFunc)(BaseMathObject *); /* checks the user is still valid */
typedef int (*BaseMathGetFunc)(BaseMathObject *, int); /* gets the vector from the user */
typedef int (*BaseMathSetFunc)(BaseMathObject *, int); /* sets the users vector values once the vector is modified */
typedef int (*BaseMathGetIndexFunc)(BaseMathObject *, int, int); /* same as above but only for an index */
typedef int (*BaseMathSetIndexFunc)(BaseMathObject *, int, int); /* same as above but only for an index */
struct Mathutils_Callback {
int (*check)(PyObject *user); /* checks the user is still valid */
int (*get)(PyObject *user, int subtype, float *from); /* gets the vector from the user */
int (*set)(PyObject *user, int subtype, float *to); /* sets the users vector values once the vector is modified */
int (*get_index)(PyObject *user, int subtype, float *from,int index); /* same as above but only for an index */
int (*set_index)(PyObject *user, int subtype, float *to, int index); /* same as above but only for an index */
BaseMathCheckFunc check;
BaseMathGetFunc get;
BaseMathSetFunc set;
BaseMathGetIndexFunc get_index;
BaseMathSetIndexFunc set_index;
};
int Mathutils_RegisterCallback(Mathutils_Callback *cb);
@@ -115,4 +115,7 @@ int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index);
#define BaseMath_ReadIndexCallback(_self, _index) (((_self)->cb_user ? _BaseMathObject_ReadIndexCallback((BaseMathObject *)_self, _index):1))
#define BaseMath_WriteIndexCallback(_self, _index) (((_self)->cb_user ? _BaseMathObject_WriteIndexCallback((BaseMathObject *)_self, _index):1))
/* utility func */
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix);
#endif /* EXPP_Mathutils_H */

View File

@@ -27,60 +27,51 @@
#include "BLI_math.h"
#include "BKE_utildefines.h"
#define COLOR_SIZE 3
//----------------------------------mathutils.Color() -------------------
//makes a new color for you to play with
static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
{
PyObject *listObject = NULL;
int size, i;
float col[3];
PyObject *e;
float col[3]= {0.0f, 0.0f, 0.0f};
size = PyTuple_GET_SIZE(args);
if (size == 1) {
listObject = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(listObject)) {
size = PySequence_Length(listObject);
} else { // Single argument was not a sequence
PyErr_SetString(PyExc_TypeError, "mathutils.Color(): 3d numeric sequence expected\n");
switch(PyTuple_GET_SIZE(args)) {
case 0:
break;
case 1:
if((mathutils_array_parse(col, COLOR_SIZE, COLOR_SIZE, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1)
return NULL;
}
} else if (size == 0) {
//returns a new empty 3d color
return newColorObject(NULL, Py_NEW, NULL);
} else {
listObject = args;
}
if (size != 3) { // Invalid color size
PyErr_SetString(PyExc_AttributeError, "mathutils.Color(): 3d numeric sequence expected\n");
break;
default:
PyErr_SetString(PyExc_TypeError, "mathutils.Color(): more then a single arg given");
return NULL;
}
for (i=0; i<size; i++) {
e = PySequence_GetItem(listObject, i);
if (e == NULL) { // Failed to read sequence
Py_DECREF(listObject);
PyErr_SetString(PyExc_RuntimeError, "mathutils.Color(): 3d numeric sequence expected\n");
return NULL;
}
col[i]= (float)PyFloat_AsDouble(e);
Py_DECREF(e);
if(col[i]==-1 && PyErr_Occurred()) { // parsed item is not a number
PyErr_SetString(PyExc_TypeError, "mathutils.Color(): 3d numeric sequence expected\n");
return NULL;
}
}
return newColorObject(col, Py_NEW, NULL);
return newColorObject(col, Py_NEW, type);
}
//-----------------------------METHODS----------------------------
//----------------------------Color.rotate()-----------------------
// return a copy of the color
/* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Color_ToTupleExt(ColorObject *self, int ndigits)
{
PyObject *ret;
int i;
ret= PyTuple_New(COLOR_SIZE);
if(ndigits >= 0) {
for(i= 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits)));
}
}
else {
for(i= 0; i < COLOR_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
}
}
return ret;
}
static char Color_copy_doc[] =
".. function:: copy()\n"
@@ -102,16 +93,22 @@ static PyObject *Color_copy(ColorObject * self, PyObject *args)
//----------------------------print object (internal)--------------
//print the object to screen
static PyObject *Color_repr(ColorObject * self)
{
char str[64];
PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self))
return NULL;
sprintf(str, "[%.6f, %.6f, %.6f](color)", self->col[0], self->col[1], self->col[2]);
return PyUnicode_FromString(str);
tuple= Color_ToTupleExt(self, -1);
ret= PyUnicode_FromFormat("Color(%R)", tuple);
Py_DECREF(tuple);
return ret;
}
//------------------------tp_richcmpr
//returns -1 execption, 0 false, 1 true
static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
@@ -142,10 +139,10 @@ static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int compar
switch (comparison_type){
case Py_EQ:
result = EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1);
result = EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1);
break;
case Py_NE:
result = !EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1);
result = !EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1);
break;
default:
printf("The result of the comparison could not be evaluated");
@@ -163,15 +160,15 @@ static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int compar
//sequence length
static int Color_len(ColorObject * self)
{
return 3;
return COLOR_SIZE;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
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");
return NULL;
}
@@ -193,9 +190,9 @@ static int Color_ass_item(ColorObject * self, int i, PyObject * value)
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");
return -1;
}
@@ -217,9 +214,9 @@ static PyObject *Color_slice(ColorObject * self, int begin, int end)
if(!BaseMath_ReadCallback(self))
return NULL;
CLAMP(begin, 0, 3);
if (end<0) end= 4+end;
CLAMP(end, 0, 3);
CLAMP(begin, 0, COLOR_SIZE);
if (end<0) end= (COLOR_SIZE + 1) + end;
CLAMP(end, 0, COLOR_SIZE);
begin = MIN2(begin,end);
list = PyList_New(end - begin);
@@ -232,61 +229,116 @@ static PyObject *Color_slice(ColorObject * self, int begin, int end)
}
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Color_ass_slice(ColorObject * self, int begin, int end,
PyObject * seq)
static int Color_ass_slice(ColorObject * self, int begin, int end, PyObject * seq)
{
int i, y, size = 0;
float col[3];
PyObject *e;
int i, size;
float col[COLOR_SIZE];
if(!BaseMath_ReadCallback(self))
return -1;
CLAMP(begin, 0, 3);
if (end<0) end= 4+end;
CLAMP(end, 0, 3);
CLAMP(begin, 0, COLOR_SIZE);
if (end<0) end= (COLOR_SIZE + 1) + end;
CLAMP(end, 0, COLOR_SIZE);
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)){
PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: size mismatch in slice assignment");
return -1;
}
for (i = 0; i < size; i++) {
e = PySequence_GetItem(seq, 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];
}
for(i= 0; i < COLOR_SIZE; i++)
self->col[begin + i] = col[i];
BaseMath_WriteCallback(self);
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 color");
return NULL;
}
}
else {
PyErr_Format(PyExc_TypeError,
"color 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 color");
return -1;
}
}
else {
PyErr_Format(PyExc_TypeError,
"color indices must be integers, not %.200s",
item->ob_type->tp_name);
return -1;
}
}
//-----------------PROTCOL DECLARATIONS--------------------------
static PySequenceMethods Color_SeqMethods = {
(lenfunc) Color_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Color_item, /* sq_item */
(ssizessizeargfunc) Color_slice, /* sq_slice */
(ssizeobjargproc) Color_ass_item, /* sq_ass_item */
(ssizessizeobjargproc) Color_ass_slice, /* sq_ass_slice */
(lenfunc) Color_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Color_item, /* sq_item */
(ssizessizeargfunc) NULL, /* sq_slice, deprecated */
(ssizeobjargproc) Color_ass_item, /* sq_ass_item */
(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 */
static PyObject *Color_getChannel( ColorObject * self, void *type )
@@ -338,6 +390,43 @@ static int Color_setChannelHSV(ColorObject * self, PyObject * value, void * type
return 0;
}
/* color channel (HSV), color.h/s/v */
static PyObject *Color_getHSV(ColorObject * self, void *type)
{
float hsv[3];
PyObject *ret;
if(!BaseMath_ReadCallback(self))
return NULL;
rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2]));
ret= PyTuple_New(3);
PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(hsv[0]));
PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(hsv[1]));
PyTuple_SET_ITEM(ret, 2, PyFloat_FromDouble(hsv[2]));
return ret;
}
static int Color_setHSV(ColorObject * self, PyObject * value, void * type)
{
float hsv[3];
if(mathutils_array_parse(hsv, 3, 3, value, "mathutils.Color.hsv = value") == -1)
return -1;
CLAMP(hsv[0], 0.0f, 1.0f);
CLAMP(hsv[1], 0.0f, 1.0f);
CLAMP(hsv[2], 0.0f, 1.0f);
hsv_to_rgb(hsv[0], hsv[1], hsv[2], &(self->col[0]), &(self->col[1]), &(self->col[2]));
if(!BaseMath_WriteCallback(self))
return -1;
return 0;
}
/*****************************************************************************/
/* Python attributes get/set structure: */
/*****************************************************************************/
@@ -350,6 +439,8 @@ static PyGetSetDef Color_getseters[] = {
{"s", (getter)Color_getChannelHSV, (setter)Color_setChannelHSV, "HSV Saturation component in [0, 1]. **type** float", (void *)1},
{"v", (getter)Color_getChannelHSV, (setter)Color_setChannelHSV, "HSV Value component in [0, 1]. **type** float", (void *)2},
{"hsv", (getter)Color_getHSV, (setter)Color_setHSV, "HSV Values in [0, 1]. **type** float triplet", (void *)0},
{"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, BaseMathObject_Wrapped_doc, NULL},
{"_owner", (getter)BaseMathObject_getOwner, (setter)NULL, BaseMathObject_Owner_doc, NULL},
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
@@ -380,7 +471,7 @@ PyTypeObject color_Type = {
(reprfunc) Color_repr, //tp_repr
0, //tp_as_number
&Color_SeqMethods, //tp_as_sequence
0, //tp_as_mapping
&Color_AsMapping, //tp_as_mapping
0, //tp_hash
0, //tp_call
0, //tp_str
@@ -424,7 +515,6 @@ PyTypeObject color_Type = {
PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
{
ColorObject *self;
int x;
if(base_type) self = (ColorObject *)base_type->tp_alloc(base_type, 0);
else self = PyObject_NEW(ColorObject, &color_Type);
@@ -436,17 +526,17 @@ PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
if(type == Py_WRAP){
self->col = col;
self->wrapped = Py_WRAP;
}else if (type == Py_NEW){
self->col = PyMem_Malloc(3 * sizeof(float));
if(!col) { //new empty
for(x = 0; x < 3; x++) {
self->col[x] = 0.0f;
}
}else{
VECCOPY(self->col, col);
}
}
else if (type == Py_NEW){
self->col = PyMem_Malloc(COLOR_SIZE * sizeof(float));
if(col)
copy_v3_v3(self->col, col);
else
zero_v3(self->col);
self->wrapped = Py_NEW;
}else{ //bad type
}
else {
return NULL;
}

View File

@@ -35,52 +35,32 @@
#include "BLO_sys_types.h"
#endif
#define EULER_SIZE 3
//----------------------------------mathutils.Euler() -------------------
//makes a new euler for you to play with
static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
{
PyObject *listObject = NULL;
int size, i;
float eul[3];
PyObject *e;
short order= 0; // TODO, add order option
PyObject *seq= NULL;
char *order_str= NULL;
size = PyTuple_GET_SIZE(args);
if (size == 1) {
listObject = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(listObject)) {
size = PySequence_Length(listObject);
} else { // Single argument was not a sequence
PyErr_SetString(PyExc_TypeError, "mathutils.Euler(): 3d numeric sequence expected\n");
return NULL;
}
} else if (size == 0) {
//returns a new empty 3d euler
return newEulerObject(NULL, order, Py_NEW, NULL);
} else {
listObject = args;
}
float eul[EULER_SIZE]= {0.0f, 0.0f, 0.0f};
short order= EULER_ORDER_XYZ;
if (size != 3) { // Invalid euler size
PyErr_SetString(PyExc_AttributeError, "mathutils.Euler(): 3d numeric sequence expected\n");
if(!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
return NULL;
}
for (i=0; i<size; i++) {
e = PySequence_GetItem(listObject, i);
if (e == NULL) { // Failed to read sequence
Py_DECREF(listObject);
PyErr_SetString(PyExc_RuntimeError, "mathutils.Euler(): 3d numeric sequence expected\n");
switch(PyTuple_GET_SIZE(args)) {
case 0:
break;
case 2:
if((order=euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
return NULL;
}
eul[i]= (float)PyFloat_AsDouble(e);
Py_DECREF(e);
if(eul[i]==-1 && PyErr_Occurred()) { // parsed item is not a number
PyErr_SetString(PyExc_TypeError, "mathutils.Euler(): 3d numeric sequence expected\n");
/* intentionally pass through */
case 1:
if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
return NULL;
}
break;
}
return newEulerObject(eul, order, Py_NEW, NULL);
}
@@ -89,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')) {
switch(*((int32_t *)str)) {
case 'X'|'Y'<<8|'Z'<<16: return 0;
case 'X'|'Z'<<8|'Y'<<16: return 1;
case 'Y'|'X'<<8|'Z'<<16: return 2;
case 'Y'|'Z'<<8|'X'<<16: return 3;
case 'Z'|'X'<<8|'Y'<<16: return 4;
case 'Z'|'Y'<<8|'X'<<16: return 5;
case 'X'|'Y'<<8|'Z'<<16: return EULER_ORDER_XYZ;
case 'X'|'Z'<<8|'Y'<<16: return EULER_ORDER_XZY;
case 'Y'|'X'<<8|'Z'<<16: return EULER_ORDER_YXZ;
case 'Y'|'Z'<<8|'X'<<16: return EULER_ORDER_YZX;
case 'Z'|'X'<<8|'Y'<<16: return EULER_ORDER_ZXY;
case 'Z'|'Y'<<8|'X'<<16: return EULER_ORDER_ZYX;
}
}
@@ -102,8 +82,29 @@ short euler_order_from_string(const char *str, const char *error_prefix)
return -1;
}
/* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
{
PyObject *ret;
int i;
ret= PyTuple_New(EULER_SIZE);
if(ndigits >= 0) {
for(i= 0; i < EULER_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
}
}
else {
for(i= 0; i < EULER_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
}
}
return ret;
}
//-----------------------------METHODS----------------------------
//----------------------------Euler.toQuat()----------------------
//return a quaternion representation of the euler
static char Euler_ToQuat_doc[] =
@@ -121,12 +122,12 @@ static PyObject *Euler_ToQuat(EulerObject * self)
if(!BaseMath_ReadCallback(self))
return NULL;
if(self->order==0) eul_to_quat(quat, self->eul);
else eulO_to_quat(quat, self->eul, self->order);
if(self->order==EULER_ORDER_XYZ) eul_to_quat(quat, self->eul);
else eulO_to_quat(quat, self->eul, self->order);
return newQuaternionObject(quat, Py_NEW, NULL);
}
//----------------------------Euler.toMatrix()---------------------
//return a matrix representation of the euler
static char Euler_ToMatrix_doc[] =
".. method:: to_matrix()\n"
@@ -143,12 +144,12 @@ static PyObject *Euler_ToMatrix(EulerObject * self)
if(!BaseMath_ReadCallback(self))
return NULL;
if(self->order==0) eul_to_mat3((float (*)[3])mat, self->eul);
else eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
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);
return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
}
//----------------------------Euler.unique()-----------------------
//sets the x,y,z values to a unique euler rotation
// TODO, check if this works with rotation order!!!
static char Euler_Unique_doc[] =
@@ -207,7 +208,7 @@ static PyObject *Euler_Unique(EulerObject * self)
Py_INCREF(self);
return (PyObject *)self;
}
//----------------------------Euler.zero()-------------------------
//sets the euler to 0,0,0
static char Euler_Zero_doc[] =
".. method:: zero()\n"
@@ -227,28 +228,38 @@ static PyObject *Euler_Zero(EulerObject * self)
Py_INCREF(self);
return (PyObject *)self;
}
//----------------------------Euler.rotate()-----------------------
//rotates a euler a certain amount and returns the result
//should return a unique euler rotation (i.e. no 720 degree pitches :)
static char Euler_Rotate_doc[] =
".. method:: rotate(angle, axis)\n"
"\n"
" Rotates the euler a certain amount and returning a unique euler rotation (no 720 degree pitches).\n"
"\n"
" :arg angle: angle in radians.\n"
" :type angle: float\n"
" :arg axis: single character in ['X, 'Y', 'Z'].\n"
" :type axis: string\n"
" :return: an instance of itself\n"
" :rtype: :class:`Euler`";
static PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
{
float angle = 0.0f;
char *axis;
if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){
PyErr_SetString(PyExc_TypeError, "euler.rotate():expected angle (float) and axis (x,y,z)");
if(!PyArg_ParseTuple(args, "fs:rotate", &angle, &axis)){
PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected angle (float) and axis (x,y,z)");
return NULL;
}
if(ELEM3(*axis, 'x', 'y', 'z') && axis[1]=='\0'){
PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'x', 'y' or 'z'");
if(ELEM3(*axis, 'X', 'Y', 'Z') && axis[1]=='\0'){
PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'X', 'Y' or 'Z'");
return NULL;
}
if(!BaseMath_ReadCallback(self))
return NULL;
if(self->order == 0) rotate_eul(self->eul, *axis, angle);
else rotate_eulO(self->eul, self->order, *axis, angle);
if(self->order == EULER_ORDER_XYZ) rotate_eul(self->eul, *axis, angle);
else rotate_eulO(self->eul, self->order, *axis, angle);
BaseMath_WriteCallback(self);
Py_INCREF(self);
@@ -312,16 +323,22 @@ static PyObject *Euler_copy(EulerObject * self, PyObject *args)
//----------------------------print object (internal)--------------
//print the object to screen
static PyObject *Euler_repr(EulerObject * self)
{
char str[64];
PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self))
return NULL;
sprintf(str, "[%.6f, %.6f, %.6f](euler)", self->eul[0], self->eul[1], self->eul[2]);
return PyUnicode_FromString(str);
tuple= Euler_ToTupleExt(self, -1);
ret= PyUnicode_FromFormat("Euler(%R)", tuple);
Py_DECREF(tuple);
return ret;
}
//------------------------tp_richcmpr
//returns -1 execption, 0 false, 1 true
static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
@@ -352,10 +369,10 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
switch (comparison_type){
case Py_EQ:
result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
break;
case Py_NE:
result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
break;
default:
printf("The result of the comparison could not be evaluated");
@@ -373,15 +390,15 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
//sequence length
static int Euler_len(EulerObject * self)
{
return 3;
return EULER_SIZE;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
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");
return NULL;
}
@@ -403,9 +420,9 @@ static int Euler_ass_item(EulerObject * self, int i, PyObject * value)
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");
return -1;
}
@@ -427,9 +444,9 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
if(!BaseMath_ReadCallback(self))
return NULL;
CLAMP(begin, 0, 3);
if (end<0) end= 4+end;
CLAMP(end, 0, 3);
CLAMP(begin, 0, EULER_SIZE);
if (end<0) end= (EULER_SIZE + 1) + end;
CLAMP(end, 0, EULER_SIZE);
begin = MIN2(begin,end);
list = PyList_New(end - begin);
@@ -442,61 +459,117 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
}
//----------------------------object[z:y]------------------------
//sequence slice (set)
static int Euler_ass_slice(EulerObject * self, int begin, int end,
PyObject * seq)
static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
{
int i, y, size = 0;
float eul[3];
PyObject *e;
int i, size;
float eul[EULER_SIZE];
if(!BaseMath_ReadCallback(self))
return -1;
CLAMP(begin, 0, 3);
if (end<0) end= 4+end;
CLAMP(end, 0, 3);
CLAMP(begin, 0, EULER_SIZE);
if (end<0) end= (EULER_SIZE + 1) + end;
CLAMP(end, 0, EULER_SIZE);
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)){
PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
return -1;
}
for (i = 0; i < size; i++) {
e = PySequence_GetItem(seq, 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];
}
for(i= 0; i < EULER_SIZE; i++)
self->eul[begin + i] = eul[i];
BaseMath_WriteCallback(self);
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--------------------------
static PySequenceMethods Euler_SeqMethods = {
(lenfunc) Euler_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Euler_item, /* sq_item */
(ssizessizeargfunc) Euler_slice, /* sq_slice */
(ssizeobjargproc) Euler_ass_item, /* sq_ass_item */
(ssizessizeobjargproc) Euler_ass_slice, /* sq_ass_slice */
(lenfunc) Euler_len, /* sq_length */
(binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Euler_item, /* sq_item */
(ssizessizeargfunc) NULL, /* sq_slice, deprecated */
(ssizeobjargproc) Euler_ass_item, /* sq_ass_item */
(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
@@ -514,8 +587,12 @@ static int Euler_setAxis( EulerObject * self, PyObject * value, void * type )
/* rotation order */
static PyObject *Euler_getOrder(EulerObject *self, void *type)
{
static char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
return PyUnicode_FromString(order[self->order]);
const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
if(!BaseMath_ReadCallback(self)) /* can read order too */
return NULL;
return PyUnicode_FromString(order[self->order-EULER_ORDER_XYZ]);
}
static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
@@ -523,15 +600,11 @@ static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
char *order_str= _PyUnicode_AsString(value);
short order= euler_order_from_string(order_str, "euler.order");
if(order < 0)
if(order == -1)
return -1;
if(self->cb_user) {
PyErr_SetString(PyExc_TypeError, "euler.order: assignment is not allowed on eulers with an owner");
return -1;
}
self->order= order;
BaseMath_WriteCallback(self); /* order can be written back */
return 0;
}
@@ -556,7 +629,7 @@ static struct PyMethodDef Euler_methods[] = {
{"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc},
{"to_matrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc},
{"to_quat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc},
{"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, NULL},
{"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, Euler_Rotate_doc},
{"make_compatible", (PyCFunction) Euler_MakeCompatible, METH_O, Euler_MakeCompatible_doc},
{"__copy__", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
{"copy", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
@@ -580,7 +653,7 @@ PyTypeObject euler_Type = {
(reprfunc) Euler_repr, //tp_repr
0, //tp_as_number
&Euler_SeqMethods, //tp_as_sequence
0, //tp_as_mapping
&Euler_AsMapping, //tp_as_mapping
0, //tp_hash
0, //tp_call
0, //tp_str
@@ -624,7 +697,6 @@ PyTypeObject euler_Type = {
PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
{
EulerObject *self;
int x;
if(base_type) self = (EulerObject *)base_type->tp_alloc(base_type, 0);
else self = PyObject_NEW(EulerObject, &euler_Type);
@@ -633,20 +705,20 @@ PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_t
self->cb_user= NULL;
self->cb_type= self->cb_subtype= 0;
if(type == Py_WRAP){
if(type == Py_WRAP) {
self->eul = eul;
self->wrapped = Py_WRAP;
}else if (type == Py_NEW){
self->eul = PyMem_Malloc(3 * sizeof(float));
if(!eul) { //new empty
for(x = 0; x < 3; x++) {
self->eul[x] = 0.0f;
}
}else{
VECCOPY(self->eul, eul);
}
}
else if (type == Py_NEW){
self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
if(eul)
copy_v3_v3(self->eul, eul);
else
zero_v3(self->eul);
self->wrapped = Py_NEW;
}else{ //bad type
}
else{
return NULL;
}

View File

@@ -37,60 +37,60 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject*
/* matrix vector callbacks */
int mathutils_matrix_vector_cb_index= -1;
static int mathutils_matrix_vector_check(PyObject *self_p)
static int mathutils_matrix_vector_check(BaseMathObject *bmo)
{
MatrixObject *self= (MatrixObject*)self_p;
MatrixObject *self= (MatrixObject *)bmo->cb_user;
return BaseMath_ReadCallback(self);
}
static int mathutils_matrix_vector_get(PyObject *self_p, int subtype, float *vec_from)
static int mathutils_matrix_vector_get(BaseMathObject *bmo, int subtype)
{
MatrixObject *self= (MatrixObject*)self_p;
MatrixObject *self= (MatrixObject *)bmo->cb_user;
int i;
if(!BaseMath_ReadCallback(self))
return 0;
for(i=0; i<self->colSize; i++)
vec_from[i]= self->matrix[subtype][i];
for(i=0; i < self->colSize; i++)
bmo->data[i]= self->matrix[subtype][i];
return 1;
}
static int mathutils_matrix_vector_set(PyObject *self_p, int subtype, float *vec_to)
static int mathutils_matrix_vector_set(BaseMathObject *bmo, int subtype)
{
MatrixObject *self= (MatrixObject*)self_p;
MatrixObject *self= (MatrixObject *)bmo->cb_user;
int i;
if(!BaseMath_ReadCallback(self))
return 0;
for(i=0; i<self->colSize; i++)
self->matrix[subtype][i]= vec_to[i];
for(i=0; i < self->colSize; i++)
self->matrix[subtype][i]= bmo->data[i];
BaseMath_WriteCallback(self);
return 1;
}
static int mathutils_matrix_vector_get_index(PyObject *self_p, int subtype, float *vec_from, int index)
static int mathutils_matrix_vector_get_index(BaseMathObject *bmo, int subtype, int index)
{
MatrixObject *self= (MatrixObject*)self_p;
MatrixObject *self= (MatrixObject *)bmo->cb_user;
if(!BaseMath_ReadCallback(self))
return 0;
vec_from[index]= self->matrix[subtype][index];
bmo->data[index]= self->matrix[subtype][index];
return 1;
}
static int mathutils_matrix_vector_set_index(PyObject *self_p, int subtype, float *vec_to, int index)
static int mathutils_matrix_vector_set_index(BaseMathObject *bmo, int subtype, int index)
{
MatrixObject *self= (MatrixObject*)self_p;
MatrixObject *self= (MatrixObject *)bmo->cb_user;
if(!BaseMath_ReadCallback(self))
return 0;
self->matrix[subtype][index]= vec_to[index];
self->matrix[subtype][index]= bmo->data[index];
BaseMath_WriteCallback(self);
return 1;
@@ -245,7 +245,7 @@ static char Matrix_toEuler_doc[] =
PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
{
char *order_str= NULL;
short order= 0;
short order= EULER_ORDER_XYZ;
float eul[3], eul_compatf[3];
EulerObject *eul_compat = NULL;
@@ -262,7 +262,7 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
if(!BaseMath_ReadCallback(eul_compat))
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*/
@@ -279,16 +279,16 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
if(order_str) {
order= euler_order_from_string(order_str, "Matrix.to_euler()");
if(order < 0)
if(order == -1)
return NULL;
}
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 {
if(order == 0) mat3_to_eul(eul, mat);
if(order == 1) mat3_to_eul(eul, mat);
else mat3_to_eulO(eul, order, mat);
}
@@ -723,27 +723,25 @@ PyObject *Matrix_copy(MatrixObject * self)
static PyObject *Matrix_repr(MatrixObject * self)
{
int x, y;
char buffer[48], str[1024];
char str[1024]="Matrix((", *str_p;
if(!BaseMath_ReadCallback(self))
return NULL;
BLI_strncpy(str,"",1024);
str_p= &str[8];
for(x = 0; x < self->colSize; x++){
sprintf(buffer, "[");
strcat(str,buffer);
for(y = 0; y < (self->rowSize - 1); y++) {
sprintf(buffer, "%.6f, ", self->matrix[y][x]);
strcat(str,buffer);
str_p += sprintf(str_p, "%f, ", self->matrix[y][x]);
}
if(x < (self->colSize-1)){
sprintf(buffer, "%.6f](matrix [row %d])\n", self->matrix[y][x], x);
strcat(str,buffer);
}else{
sprintf(buffer, "%.6f](matrix [row %d])", self->matrix[y][x], x);
strcat(str,buffer);
str_p += sprintf(str_p, "%f), (", self->matrix[y][x]);
}
else{
str_p += sprintf(str_p, "%f)", self->matrix[y][x]);
}
}
strcat(str_p, ")");
return PyUnicode_FromString(str);
}

View File

@@ -31,7 +31,32 @@
#include "BLI_math.h"
#include "BKE_utildefines.h"
#define QUAT_SIZE 4
//-----------------------------METHODS------------------------------
/* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Quaternion_ToTupleExt(QuaternionObject *self, int ndigits)
{
PyObject *ret;
int i;
ret= PyTuple_New(QUAT_SIZE);
if(ndigits >= 0) {
for(i= 0; i < QUAT_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits)));
}
}
else {
for(i= 0; i < QUAT_SIZE; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i]));
}
}
return ret;
}
static char Quaternion_ToEuler_doc[] =
".. method:: to_euler(order, euler_compat)\n"
"\n"
@@ -48,7 +73,7 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
{
float eul[3];
char *order_str= NULL;
short order= 0;
short order= EULER_ORDER_XYZ;
EulerObject *eul_compat = NULL;
if(!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
@@ -60,7 +85,7 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
if(order_str) {
order= euler_order_from_string(order_str, "Matrix.to_euler()");
if(order < 0)
if(order == -1)
return NULL;
}
@@ -72,12 +97,12 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
quat_to_mat3(mat, self->quat);
if(order == 0) mat3_to_compatible_eul(eul, eul_compat->eul, mat);
else mat3_to_compatible_eulO(eul, eul_compat->eul, order, 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 {
if(order == 0) quat_to_eul(eul, self->quat);
else quat_to_eulO(eul, order, self->quat);
if(order == EULER_ORDER_XYZ) quat_to_eul(eul, self->quat);
else quat_to_eulO(eul, order, self->quat);
}
return newEulerObject(eul, order, Py_NEW, NULL);
@@ -115,7 +140,7 @@ static char Quaternion_Cross_doc[] =
static PyObject *Quaternion_Cross(QuaternionObject * self, QuaternionObject * value)
{
float quat[4];
float quat[QUAT_SIZE];
if (!QuaternionObject_Check(value)) {
PyErr_SetString( PyExc_TypeError, "quat.cross(value): expected a quaternion argument" );
@@ -165,7 +190,7 @@ static char Quaternion_Difference_doc[] =
static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject * value)
{
float quat[4], tempQuat[4];
float quat[QUAT_SIZE], tempQuat[QUAT_SIZE];
double dot = 0.0f;
int x;
@@ -177,15 +202,11 @@ static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject
if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
return NULL;
tempQuat[0] = self->quat[0];
tempQuat[1] = - self->quat[1];
tempQuat[2] = - self->quat[2];
tempQuat[3] = - self->quat[3];
copy_qt_qt(tempQuat, self->quat);
conjugate_qt(tempQuat);
dot = sqrt(dot_qtqt(tempQuat, tempQuat));
dot = sqrt(tempQuat[0] * tempQuat[0] + tempQuat[1] * tempQuat[1] +
tempQuat[2] * tempQuat[2] + tempQuat[3] * tempQuat[3]);
for(x = 0; x < 4; x++) {
for(x = 0; x < QUAT_SIZE; x++) {
tempQuat[x] /= (float)(dot * dot);
}
mul_qt_qtqt(quat, tempQuat, value->quat);
@@ -207,7 +228,7 @@ static char Quaternion_Slerp_doc[] =
static PyObject *Quaternion_Slerp(QuaternionObject *self, PyObject *args)
{
QuaternionObject *value;
float quat[4], fac;
float quat[QUAT_SIZE], fac;
if(!PyArg_ParseTuple(args, "O!f:slerp", &quaternion_Type, &value, &fac)) {
PyErr_SetString(PyExc_TypeError, "quat.slerp(): expected Quaternion types and float");
@@ -351,14 +372,19 @@ static PyObject *Quaternion_copy(QuaternionObject * self)
//print the object to screen
static PyObject *Quaternion_repr(QuaternionObject * self)
{
char str[64];
PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self))
return NULL;
sprintf(str, "[%.6f, %.6f, %.6f, %.6f](quaternion)", self->quat[0], self->quat[1], self->quat[2], self->quat[3]);
return PyUnicode_FromString(str);
tuple= Quaternion_ToTupleExt(self, -1);
ret= PyUnicode_FromFormat("Quaternion(%R)", tuple);
Py_DECREF(tuple);
return ret;
}
//------------------------tp_richcmpr
//returns -1 execption, 0 false, 1 true
static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
@@ -387,10 +413,10 @@ static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int c
switch (comparison_type){
case Py_EQ:
result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, 4, 1);
result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
break;
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){
result = 1;
}else{
@@ -413,15 +439,15 @@ static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int c
//sequence length
static int Quaternion_len(QuaternionObject * self)
{
return 4;
return QUAT_SIZE;
}
//----------------------------object[]---------------------------
//sequence accessor (get)
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");
return NULL;
}
@@ -442,9 +468,9 @@ static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
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");
return -1;
}
@@ -465,9 +491,9 @@ static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
if(!BaseMath_ReadCallback(self))
return NULL;
CLAMP(begin, 0, 4);
if (end<0) end= 5+end;
CLAMP(end, 0, 4);
CLAMP(begin, 0, QUAT_SIZE);
if (end<0) end= (QUAT_SIZE + 1) + end;
CLAMP(end, 0, QUAT_SIZE);
begin = MIN2(begin,end);
list = PyList_New(end - begin);
@@ -482,52 +508,107 @@ static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
//sequence slice (set)
static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end, PyObject * seq)
{
int i, y, size = 0;
float quat[4];
PyObject *q;
int i, size;
float quat[QUAT_SIZE];
if(!BaseMath_ReadCallback(self))
return -1;
CLAMP(begin, 0, 4);
if (end<0) end= 5+end;
CLAMP(end, 0, 4);
CLAMP(begin, 0, QUAT_SIZE);
if (end<0) end= (QUAT_SIZE + 1) + end;
CLAMP(end, 0, QUAT_SIZE);
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)){
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;
}
for (i = 0; i < size; i++) {
q = PySequence_GetItem(seq, i);
if (q == NULL) { // Failed to read sequence
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];
/* parsed well - now set in vector */
for(i= 0; i < size; i++)
self->quat[begin + i] = quat[i];
BaseMath_WriteCallback(self);
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----------------------
//------------------------obj + obj------------------------------
//addition
static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
{
float quat[4];
float quat[QUAT_SIZE];
QuaternionObject *quat1 = NULL, *quat2 = NULL;
if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
@@ -548,7 +629,7 @@ static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
{
int x;
float quat[4];
float quat[QUAT_SIZE];
QuaternionObject *quat1 = NULL, *quat2 = NULL;
if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
@@ -562,7 +643,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
return NULL;
for(x = 0; x < 4; x++) {
for(x = 0; x < QUAT_SIZE; x++) {
quat[x] = quat1->quat[x] - quat2->quat[x];
}
@@ -572,7 +653,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
//mulplication
static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
{
float quat[4], scalar;
float quat[QUAT_SIZE], scalar;
QuaternionObject *quat1 = NULL, *quat2 = NULL;
VectorObject *vec = NULL;
@@ -630,46 +711,52 @@ static PySequenceMethods Quaternion_SeqMethods = {
(binaryfunc) 0, /* sq_concat */
(ssizeargfunc) 0, /* sq_repeat */
(ssizeargfunc) Quaternion_item, /* sq_item */
(ssizessizeargfunc) Quaternion_slice, /* sq_slice */
(ssizessizeargfunc) NULL, /* sq_slice, deprecated */
(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 = {
(binaryfunc) Quaternion_add, /*nb_add*/
(binaryfunc) Quaternion_sub, /*nb_subtract*/
(binaryfunc) Quaternion_mul, /*nb_multiply*/
0, /*nb_remainder*/
0, /*nb_divmod*/
0, /*nb_power*/
(unaryfunc) 0, /*nb_negative*/
(unaryfunc) 0, /*tp_positive*/
(unaryfunc) 0, /*tp_absolute*/
(inquiry) 0, /*tp_bool*/
(unaryfunc) 0, /*nb_invert*/
0, /*nb_lshift*/
(binaryfunc)0, /*nb_rshift*/
0, /*nb_and*/
0, /*nb_xor*/
0, /*nb_or*/
0, /*nb_int*/
0, /*nb_reserved*/
0, /*nb_float*/
0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */
0, /* nb_inplace_remainder */
0, /* nb_inplace_power */
0, /* nb_inplace_lshift */
0, /* nb_inplace_rshift */
0, /* nb_inplace_and */
0, /* nb_inplace_xor */
0, /* nb_inplace_or */
0, /* nb_floor_divide */
0, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
0, /* nb_index */
(binaryfunc) Quaternion_add, /*nb_add*/
(binaryfunc) Quaternion_sub, /*nb_subtract*/
(binaryfunc) Quaternion_mul, /*nb_multiply*/
0, /*nb_remainder*/
0, /*nb_divmod*/
0, /*nb_power*/
(unaryfunc) 0, /*nb_negative*/
(unaryfunc) 0, /*tp_positive*/
(unaryfunc) 0, /*tp_absolute*/
(inquiry) 0, /*tp_bool*/
(unaryfunc) 0, /*nb_invert*/
0, /*nb_lshift*/
(binaryfunc)0, /*nb_rshift*/
0, /*nb_and*/
0, /*nb_xor*/
0, /*nb_or*/
0, /*nb_int*/
0, /*nb_reserved*/
0, /*nb_float*/
0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */
0, /* nb_inplace_remainder */
0, /* nb_inplace_power */
0, /* nb_inplace_lshift */
0, /* nb_inplace_rshift */
0, /* nb_inplace_and */
0, /* nb_inplace_xor */
0, /* nb_inplace_or */
0, /* nb_floor_divide */
0, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
0, /* nb_index */
};
static PyObject *Quaternion_getAxis( QuaternionObject * self, void *type )
@@ -694,16 +781,11 @@ static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type )
static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type )
{
int i;
float vec[3];
double mag = self->quat[0] * (Py_PI / 180);
mag = 2 * (saacos(mag));
mag = sin(mag / 2);
for(i = 0; i < 3; i++)
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
normalize_v3_v3(vec, self->quat+1);
/* 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) &&
EXPP_FloatsAreEqual(vec[1], 0.0f, 10) &&
EXPP_FloatsAreEqual(vec[2], 0.0f, 10) ){
@@ -715,94 +797,28 @@ static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type )
//----------------------------------mathutils.Quaternion() --------------
static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *listObject = NULL, *n, *q;
int size, i;
float quat[4];
double angle = 0.0f;
PyObject *seq= NULL;
float angle = 0.0f;
float quat[QUAT_SIZE]= {0.0f, 0.0f, 0.0f, 0.0f};
size = PyTuple_GET_SIZE(args);
if (size == 1 || size == 2) { //seq?
listObject = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(listObject)) {
size = PySequence_Length(listObject);
if ((size == 4 && PySequence_Length(args) !=1) ||
(size == 3 && PySequence_Length(args) !=2) || (size >4 || size < 3)) {
// invalid args/size
PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
if(size == 3){ //get angle in axis/angle
n = PySequence_GetItem(args, 1);
if(n == NULL) { // parsed item not a number or getItem fail
PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
if(!PyArg_ParseTuple(args, "|Of:mathutils.Quaternion", &seq, &angle))
return NULL;
angle = PyFloat_AsDouble(n);
Py_DECREF(n);
if (angle==-1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
}
}else{
listObject = PyTuple_GET_ITEM(args, 1);
if (size>1 && PySequence_Check(listObject)) {
size = PySequence_Length(listObject);
if (size != 3) {
// invalid args/size
PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
angle = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
if (angle==-1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
} else { // argument was not a sequence
PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
}
} else if (size == 0) { //returns a new empty quat
return newQuaternionObject(NULL, Py_NEW, NULL);
} else {
listObject = args;
}
if (size == 3) { // invalid quat size
if(PySequence_Length(args) != 2){
PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
switch(PyTuple_GET_SIZE(args)) {
case 0:
break;
case 1:
if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
return NULL;
}
}else{
if(size != 4){
PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
break;
case 2:
if (mathutils_array_parse(quat, 3, 3, seq, "mathutils.Quaternion()") == -1)
return NULL;
}
}
for (i=0; i<size; i++) { //parse
q = PySequence_GetItem(listObject, i);
if (q == NULL) { // Failed to read sequence
PyErr_SetString(PyExc_RuntimeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
quat[i] = PyFloat_AsDouble(q);
Py_DECREF(q);
if (quat[i]==-1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
return NULL;
}
}
if(size == 3) //calculate the quat based on axis/angle
axis_angle_to_quat(quat, quat, angle);
break;
/* PyArg_ParseTuple assures no more then 2 */
}
return newQuaternionObject(quat, Py_NEW, NULL);
}
@@ -855,10 +871,10 @@ PyTypeObject quaternion_Type = {
0, //tp_getattr
0, //tp_setattr
0, //tp_compare
(reprfunc) Quaternion_repr, //tp_repr
&Quaternion_NumMethods, //tp_as_number
&Quaternion_SeqMethods, //tp_as_sequence
0, //tp_as_mapping
(reprfunc) Quaternion_repr, //tp_repr
&Quaternion_NumMethods, //tp_as_number
&Quaternion_SeqMethods, //tp_as_sequence
&Quaternion_AsMapping, //tp_as_mapping
0, //tp_hash
0, //tp_call
0, //tp_str
@@ -914,7 +930,7 @@ PyObject *newQuaternionObject(float *quat, int type, PyTypeObject *base_type)
self->quat = quat;
self->wrapped = Py_WRAP;
}else if (type == Py_NEW){
self->quat = PyMem_Malloc(4 * sizeof(float));
self->quat = PyMem_Malloc(QUAT_SIZE * sizeof(float));
if(!quat) { //new empty
unit_qt(self->quat);
}else{

View File

@@ -40,55 +40,27 @@
#define SWIZZLE_AXIS 0x3
static PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat); /* utility func */
static PyObject *Vector_ToTupleExt(VectorObject *self, int ndigits);
//----------------------------------mathutils.Vector() ------------------
// Supports 2D, 3D, and 4D vector objects both int and float values
// accepted. Mixed float and int values accepted. Ints are parsed to float
static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *listObject = NULL;
int size, i;
float vec[4], f;
PyObject *v;
float vec[4]= {0.0f, 0.0f, 0.0f, 0.0f};
int size= 3; /* default to a 3D vector */
size = PyTuple_GET_SIZE(args); /* we know its a tuple because its an arg */
if (size == 1) {
listObject = PyTuple_GET_ITEM(args, 0);
if (PySequence_Check(listObject)) {
size = PySequence_Length(listObject);
} else { // Single argument was not a sequence
PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
switch(PyTuple_GET_SIZE(args)) {
case 0:
break;
case 1:
if((size=mathutils_array_parse(vec, 2, 4, PyTuple_GET_ITEM(args, 0), "mathutils.Vector()")) == -1)
return NULL;
}
} else if (size == 0) {
//returns a new empty 3d vector
return newVectorObject(NULL, 3, Py_NEW, type);
} else {
listObject = args;
}
if (size<2 || size>4) { // Invalid vector size
PyErr_SetString(PyExc_AttributeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
break;
default:
PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): more then a single arg given");
return NULL;
}
for (i=0; i<size; i++) {
v=PySequence_GetItem(listObject, i);
if (v==NULL) { // Failed to read sequence
PyErr_SetString(PyExc_RuntimeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
return NULL;
}
f= PyFloat_AsDouble(v);
if(f==-1 && PyErr_Occurred()) { // parsed item not a number
Py_DECREF(v);
PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
return NULL;
}
vec[i]= f;
Py_DECREF(v);
}
return newVectorObject(vec, size, Py_NEW, type);
}
@@ -101,7 +73,7 @@ static char Vector_Zero_doc[] =
" :return: an instance of itself\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Zero(VectorObject * self)
static PyObject *Vector_Zero(VectorObject *self)
{
int i;
for(i = 0; i < self->size; i++) {
@@ -125,7 +97,7 @@ static char Vector_Normalize_doc[] =
"\n"
" .. note:: Normalize works for vectors of all sizes, however 4D Vectors w axis is left untouched.\n";
static PyObject *Vector_Normalize(VectorObject * self)
static PyObject *Vector_Normalize(VectorObject *self)
{
int i;
float norm = 0.0f;
@@ -156,7 +128,7 @@ static char Vector_Resize2D_doc[] =
" :return: an instance of itself\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Resize2D(VectorObject * self)
static PyObject *Vector_Resize2D(VectorObject *self)
{
if(self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize2D(): cannot resize wrapped data - only python vectors\n");
@@ -186,7 +158,7 @@ static char Vector_Resize3D_doc[] =
" :return: an instance of itself\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Resize3D(VectorObject * self)
static PyObject *Vector_Resize3D(VectorObject *self)
{
if (self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize3D(): cannot resize wrapped data - only python vectors\n");
@@ -219,7 +191,7 @@ static char Vector_Resize4D_doc[] =
" :return: an instance of itself\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Resize4D(VectorObject * self)
static PyObject *Vector_Resize4D(VectorObject *self)
{
if(self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize4D(): cannot resize wrapped data - only python vectors");
@@ -248,37 +220,53 @@ static PyObject *Vector_Resize4D(VectorObject * self)
/*----------------------------Vector.toTuple() ------------------ */
static char Vector_ToTuple_doc[] =
".. method:: to_tuple(precision)\n"
".. method:: to_tuple(precision=-1)\n"
"\n"
" Return this vector as a tuple with.\n"
"\n"
" :arg precision: The number to round the value to in [0, 21].\n"
" :arg precision: The number to round the value to in [-1, 21].\n"
" :type precision: int\n"
" :return: the values of the vector rounded by *precision*\n"
" :rtype: tuple\n";
static PyObject *Vector_ToTuple(VectorObject * self, PyObject *value)
/* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Vector_ToTupleExt(VectorObject *self, int ndigits)
{
int ndigits= PyLong_AsSsize_t(value);
int x;
PyObject *ret;
int i;
if(ndigits > 22 || ndigits < 0) { /* accounts for non ints */
ret= PyTuple_New(self->size);
if(ndigits >= 0) {
for(i = 0; i < self->size; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->vec[i], ndigits)));
}
}
else {
for(i = 0; i < self->size; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->vec[i]));
}
}
return ret;
}
static PyObject *Vector_ToTuple(VectorObject *self, PyObject *args)
{
int ndigits= 0;
if(!PyArg_ParseTuple(args, "|i:to_tuple", &ndigits) || (ndigits > 22 || ndigits < 0)) {
PyErr_SetString(PyExc_TypeError, "vector.to_tuple(ndigits): ndigits must be between 0 and 21");
return NULL;
}
if(PyTuple_GET_SIZE(args)==0)
ndigits= -1;
if(!BaseMath_ReadCallback(self))
return NULL;
ret= PyTuple_New(self->size);
for(x = 0; x < self->size; x++) {
PyTuple_SET_ITEM(ret, x, PyFloat_FromDouble(double_round((double)self->vec[x], ndigits)));
}
return ret;
return Vector_ToTupleExt(self, ndigits);
}
/*----------------------------Vector.toTrackQuat(track, up) ---------------------- */
@@ -294,7 +282,7 @@ static char Vector_ToTrackQuat_doc[] =
" :return: rotation from the vector and the track and up axis."
" :rtype: :class:`Quaternion`\n";
static PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
static PyObject *Vector_ToTrackQuat(VectorObject *self, PyObject *args )
{
float vec[3], quat[4];
char *strack, *sup;
@@ -413,7 +401,7 @@ static char Vector_Reflect_doc[] =
" :return: The reflected vector matching the size of this vector.\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Reflect( VectorObject * self, VectorObject * value )
static PyObject *Vector_Reflect(VectorObject *self, VectorObject *value )
{
float mirror[3], vec[3];
float reflect[3] = {0.0f, 0.0f, 0.0f};
@@ -454,7 +442,7 @@ static char Vector_Cross_doc[] =
"\n"
" .. note:: both vectors must be 3D\n";
static PyObject *Vector_Cross( VectorObject * self, VectorObject * value )
static PyObject *Vector_Cross(VectorObject *self, VectorObject *value )
{
VectorObject *vecCross = NULL;
@@ -486,7 +474,7 @@ static char Vector_Dot_doc[] =
" :return: The dot product.\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Dot( VectorObject * self, VectorObject * value )
static PyObject *Vector_Dot(VectorObject *self, VectorObject *value )
{
double dot = 0.0;
int x;
@@ -520,7 +508,7 @@ static char Vector_Angle_doc[] =
" :rtype: float\n"
"\n"
" .. note:: Zero length vectors raise an :exc:`AttributeError`.\n";
static PyObject *Vector_Angle(VectorObject * self, VectorObject * value)
static PyObject *Vector_Angle(VectorObject *self, VectorObject *value)
{
double dot = 0.0f, angleRads, test_v1 = 0.0f, test_v2 = 0.0f;
int x, size;
@@ -573,7 +561,7 @@ static char Vector_Difference_doc[] =
"\n"
" .. note:: 2D vectors raise an :exc:`AttributeError`.\n";;
static PyObject *Vector_Difference( VectorObject * self, VectorObject * value )
static PyObject *Vector_Difference(VectorObject *self, VectorObject *value )
{
float quat[4], vec_a[3], vec_b[3];
@@ -607,7 +595,7 @@ static char Vector_Project_doc[] =
" :return projection: the parallel projection vector\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Project(VectorObject * self, VectorObject * value)
static PyObject *Vector_Project(VectorObject *self, VectorObject *value)
{
float vec[4];
double dot = 0.0f, dot2 = 0.0f;
@@ -655,7 +643,7 @@ static char Vector_Lerp_doc[] =
" :return: The interpolated rotation.\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Lerp(VectorObject * self, PyObject * args)
static PyObject *Vector_Lerp(VectorObject *self, PyObject *args)
{
VectorObject *vec2 = NULL;
float fac, ifac, vec[4];
@@ -692,7 +680,7 @@ static char Vector_copy_doc[] =
"\n"
" .. note:: use this to get a copy of a wrapped vector with no reference to the original data.\n";
static PyObject *Vector_copy(VectorObject * self)
static PyObject *Vector_copy(VectorObject *self)
{
if(!BaseMath_ReadCallback(self))
return NULL;
@@ -702,38 +690,29 @@ static PyObject *Vector_copy(VectorObject * self)
/*----------------------------print object (internal)-------------
print the object to screen */
static PyObject *Vector_repr(VectorObject * self)
static PyObject *Vector_repr(VectorObject *self)
{
int i;
char buffer[48], str[1024];
PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self))
return NULL;
BLI_strncpy(str,"[",1024);
for(i = 0; i < self->size; i++){
if(i < (self->size - 1)){
sprintf(buffer, "%.6f, ", self->vec[i]);
strcat(str,buffer);
}else{
sprintf(buffer, "%.6f", self->vec[i]);
strcat(str,buffer);
}
}
strcat(str, "](vector)");
return PyUnicode_FromString(str);
tuple= Vector_ToTupleExt(self, -1);
ret= PyUnicode_FromFormat("Vector(%R)", tuple);
Py_DECREF(tuple);
return ret;
}
/*---------------------SEQUENCE PROTOCOLS------------------------
----------------------------len(object)------------------------
sequence length*/
static int Vector_len(VectorObject * self)
static int Vector_len(VectorObject *self)
{
return self->size;
}
/*----------------------------object[]---------------------------
sequence accessor (get)*/
static PyObject *Vector_item(VectorObject * self, int i)
static PyObject *Vector_item(VectorObject *self, int i)
{
if(i<0) i= self->size-i;
@@ -750,10 +729,10 @@ static PyObject *Vector_item(VectorObject * self, int i)
}
/*----------------------------object[]-------------------------
sequence accessor (set)*/
static int Vector_ass_item(VectorObject * self, int i, PyObject * ob)
static int Vector_ass_item(VectorObject *self, int i, PyObject * ob)
{
float scalar= (float)PyFloat_AsDouble(ob);
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
float scalar;
if((scalar=PyFloat_AsDouble(ob))==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "vector[index] = x: index argument not a number\n");
return -1;
}
@@ -773,7 +752,7 @@ static int Vector_ass_item(VectorObject * self, int i, PyObject * ob)
/*----------------------------object[z:y]------------------------
sequence slice (get) */
static PyObject *Vector_slice(VectorObject * self, int begin, int end)
static PyObject *Vector_slice(VectorObject *self, int begin, int end)
{
PyObject *list = NULL;
int count;
@@ -795,7 +774,7 @@ static PyObject *Vector_slice(VectorObject * self, int begin, int end)
}
/*----------------------------object[z:y]------------------------
sequence slice (set) */
static int Vector_ass_slice(VectorObject * self, int begin, int end,
static int Vector_ass_slice(VectorObject *self, int begin, int end,
PyObject * seq)
{
int i, y, size = 0;
@@ -823,8 +802,7 @@ static int Vector_ass_slice(VectorObject * self, int begin, int end,
return -1;
}
scalar= (float)PyFloat_AsDouble(v);
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
if((scalar=PyFloat_AsDouble(v)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
Py_DECREF(v);
PyErr_SetString(PyExc_TypeError, "vector[begin:end] = []: sequence argument not a number\n");
return -1;
@@ -1117,14 +1095,13 @@ static PyObject *Vector_div(PyObject * v1, PyObject * v2)
if(!BaseMath_ReadCallback(vec1))
return NULL;
scalar = (float)PyFloat_AsDouble(v2);
if(scalar== -1.0f && PyErr_Occurred()) { /* parsed item not a number */
if((scalar=PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL;
}
if(scalar==0.0) { /* not a vector */
if(scalar==0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n");
return NULL;
}
@@ -1146,13 +1123,12 @@ static PyObject *Vector_idiv(PyObject * v1, PyObject * v2)
if(!BaseMath_ReadCallback(vec1))
return NULL;
scalar = (float)PyFloat_AsDouble(v2);
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
if((scalar=PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL;
}
if(scalar==0.0) { /* not a vector */
if(scalar==0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n");
return NULL;
}
@@ -1407,18 +1383,18 @@ static PyNumberMethods Vector_NumMethods = {
* vector axis, vector.x/y/z/w
*/
static PyObject *Vector_getAxis( VectorObject * self, void *type )
static PyObject *Vector_getAxis(VectorObject *self, void *type )
{
return Vector_item(self, GET_INT_FROM_POINTER(type));
}
static int Vector_setAxis( VectorObject * self, PyObject * value, void * type )
static int Vector_setAxis(VectorObject *self, PyObject * value, void * type )
{
return Vector_ass_item(self, GET_INT_FROM_POINTER(type), value);
}
/* vector.length */
static PyObject *Vector_getLength( VectorObject * self, void *type )
static PyObject *Vector_getLength(VectorObject *self, void *type )
{
double dot = 0.0f;
int i;
@@ -1432,25 +1408,24 @@ static PyObject *Vector_getLength( VectorObject * self, void *type )
return PyFloat_FromDouble(sqrt(dot));
}
static int Vector_setLength( VectorObject * self, PyObject * value )
static int Vector_setLength(VectorObject *self, PyObject * value )
{
double dot = 0.0f, param;
int i;
if(!BaseMath_ReadCallback(self))
return -1;
param= PyFloat_AsDouble( value );
if(param==-1.0 && PyErr_Occurred()) {
if((param=PyFloat_AsDouble(value)) == -1.0 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "length must be set to a number");
return -1;
}
if (param < 0) {
if (param < 0.0f) {
PyErr_SetString( PyExc_TypeError, "cannot set a vectors length to a negative value" );
return -1;
}
if (param==0) {
if (param == 0.0f) {
for(i = 0; i < self->size; i++){
self->vec[i]= 0;
}
@@ -1483,10 +1458,10 @@ static int Vector_setLength( VectorObject * self, PyObject * value )
/* Get a new Vector according to the provided swizzle. This function has little
error checking, as we are in control of the inputs: the closure is set by us
in Vector_createSwizzleGetSeter. */
static PyObject *Vector_getSwizzle(VectorObject * self, void *closure)
static PyObject *Vector_getSwizzle(VectorObject *self, void *closure)
{
size_t axisA;
size_t axisB;
size_t axis_to;
size_t axis_from;
float vec[MAX_DIMENSIONS];
unsigned int swizzleClosure;
@@ -1494,22 +1469,22 @@ static PyObject *Vector_getSwizzle(VectorObject * self, void *closure)
return NULL;
/* Unpack the axes from the closure into an array. */
axisA = 0;
axis_to = 0;
swizzleClosure = GET_INT_FROM_POINTER(closure);
while (swizzleClosure & SWIZZLE_VALID_AXIS)
{
axisB = swizzleClosure & SWIZZLE_AXIS;
if(axisB >= self->size) {
axis_from = swizzleClosure & SWIZZLE_AXIS;
if(axis_from >= self->size) {
PyErr_SetString(PyExc_AttributeError, "Error: vector does not have specified axis.");
return NULL;
}
vec[axisA] = self->vec[axisB];
vec[axis_to] = self->vec[axis_from];
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
axisA++;
axis_to++;
}
return newVectorObject(vec, axisA, Py_NEW, Py_TYPE(self));
return newVectorObject(vec, axis_to, Py_NEW, Py_TYPE(self));
}
/* Set the items of this vector using a swizzle.
@@ -1522,18 +1497,18 @@ static PyObject *Vector_getSwizzle(VectorObject * self, void *closure)
Returns 0 on success and -1 on failure. On failure, the vector will be
unchanged. */
static int Vector_setSwizzle(VectorObject * self, PyObject * value, void *closure)
static int Vector_setSwizzle(VectorObject *self, PyObject * value, void *closure)
{
VectorObject *vecVal = NULL;
PyObject *item;
size_t listLen;
size_t size_from;
float scalarVal;
size_t axisB;
size_t axisA;
size_t axis_from;
size_t axis_to;
unsigned int swizzleClosure;
float vecTemp[MAX_DIMENSIONS];
float tvec[MAX_DIMENSIONS];
float vec_assign[MAX_DIMENSIONS];
if(!BaseMath_ReadCallback(self))
return -1;
@@ -1541,95 +1516,48 @@ static int Vector_setSwizzle(VectorObject * self, PyObject * value, void *closur
/* Check that the closure can be used with this vector: even 2D vectors have
swizzles defined for axes z and w, but they would be invalid. */
swizzleClosure = GET_INT_FROM_POINTER(closure);
axis_from= 0;
while (swizzleClosure & SWIZZLE_VALID_AXIS)
{
axisA = swizzleClosure & SWIZZLE_AXIS;
if (axisA >= self->size)
axis_to = swizzleClosure & SWIZZLE_AXIS;
if (axis_to >= self->size)
{
PyErr_SetString(PyExc_AttributeError, "Error: vector does not have specified axis.\n");
return -1;
}
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
axis_from++;
}
if (VectorObject_Check(value))
{
/* Copy vector contents onto swizzled axes. */
vecVal = (VectorObject*) value;
axisB = 0;
swizzleClosure = GET_INT_FROM_POINTER(closure);
while (swizzleClosure & SWIZZLE_VALID_AXIS && axisB < vecVal->size)
{
axisA = swizzleClosure & SWIZZLE_AXIS;
if(axisB >= vecVal->size) {
PyErr_SetString(PyExc_AttributeError, "Error: vector does not have specified axis.");
return -1;
}
if (((scalarVal=PyFloat_AsDouble(value)) == -1 && PyErr_Occurred())==0) {
int i;
for(i=0; i < MAX_DIMENSIONS; i++)
vec_assign[i]= scalarVal;
vecTemp[axisA] = vecVal->vec[axisB];
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
axisB++;
}
if(axisB != vecVal->size) {
PyErr_SetString(PyExc_AttributeError, "Error: vector size does not match swizzle.\n");
return -1;
}
memcpy(self->vec, vecTemp, axisB * sizeof(float));
/* continue with BaseMathObject_WriteCallback at the end */
size_from= axis_from;
}
else if (PyList_Check(value))
{
/* Copy list contents onto swizzled axes. */
listLen = PyList_Size(value);
swizzleClosure = GET_INT_FROM_POINTER(closure);
axisB = 0;
while (swizzleClosure & SWIZZLE_VALID_AXIS && axisB < listLen)
{
item = PyList_GetItem(value, axisB);
scalarVal = (float)PyFloat_AsDouble(item);
if (scalarVal==-1.0 && PyErr_Occurred()) {
PyErr_SetString(PyExc_AttributeError, "Error: list item could not be used as a float.\n");
return -1;
}
axisA = swizzleClosure & SWIZZLE_AXIS;
vecTemp[axisA] = scalarVal;
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
axisB++;
}
if(axisB != listLen) {
PyErr_SetString(PyExc_AttributeError, "Error: list size does not match swizzle.\n");
return -1;
}
memcpy(self->vec, vecTemp, axisB * sizeof(float));
/* continue with BaseMathObject_WriteCallback at the end */
}
else if (((scalarVal = (float)PyFloat_AsDouble(value)) == -1.0 && PyErr_Occurred())==0)
{
/* Assign the same value to each axis. */
swizzleClosure = GET_INT_FROM_POINTER(closure);
while (swizzleClosure & SWIZZLE_VALID_AXIS)
{
axisA = swizzleClosure & SWIZZLE_AXIS;
self->vec[axisA] = scalarVal;
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
}
/* continue with BaseMathObject_WriteCallback at the end */
}
else {
PyErr_SetString( PyExc_TypeError, "Expected a Vector, list or scalar value." );
else if((size_from=mathutils_array_parse(vec_assign, 2, 4, value, "mathutils.Vector.**** = swizzle assignment")) == -1) {
return -1;
}
if(axis_from != size_from) {
PyErr_SetString(PyExc_AttributeError, "Error: vector size does not match swizzle.\n");
return -1;
}
/* Copy vector contents onto swizzled axes. */
axis_from = 0;
swizzleClosure = GET_INT_FROM_POINTER(closure);
while (swizzleClosure & SWIZZLE_VALID_AXIS)
{
axis_to = swizzleClosure & SWIZZLE_AXIS;
tvec[axis_to] = vec_assign[axis_from];
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
axis_from++;
}
memcpy(self->vec, tvec, axis_from * sizeof(float));
/* continue with BaseMathObject_WriteCallback at the end */
if(!BaseMath_WriteCallback(self))
return -1;
@@ -2079,7 +2007,7 @@ static char Vector_Negate_doc[] =
" :return: an instance of itself\n"
" :rtype: :class:`Vector`\n";
static PyObject *Vector_Negate(VectorObject * self)
static PyObject *Vector_Negate(VectorObject *self)
{
int i;
if(!BaseMath_ReadCallback(self))
@@ -2101,7 +2029,7 @@ static struct PyMethodDef Vector_methods[] = {
{"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc},
{"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize3D_doc},
{"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize4D_doc},
{"to_tuple", (PyCFunction) Vector_ToTuple, METH_O, Vector_ToTuple_doc},
{"to_tuple", (PyCFunction) Vector_ToTuple, METH_VARARGS, Vector_ToTuple_doc},
{"to_track_quat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc},
{"reflect", ( PyCFunction ) Vector_Reflect, METH_O, Vector_Reflect_doc},
{"cross", ( PyCFunction ) Vector_Cross, METH_O, Vector_Cross_doc},
@@ -2129,7 +2057,7 @@ PyTypeObject vector_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
/* For printing, in format "<module>.<name>" */
"vector", /* char *tp_name; */
sizeof( VectorObject ), /* int tp_basicsize; */
sizeof(VectorObject), /* int tp_basicsize; */
0, /* tp_itemsize; For allocation */
/* Methods to implement standard operations */

View File

@@ -98,12 +98,15 @@ void BPy_init_modules( void )
/* Needs to be first since this dir is needed for future modules */
char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
if(modpath) {
// printf("bpy: found module path '%s'.\n", modpath);
PyObject *sys_path= PySys_GetObject("path"); /* borrow */
PyObject *py_modpath= PyUnicode_FromString(modpath);
PyList_Insert(sys_path, 0, py_modpath); /* add first */
Py_DECREF(py_modpath);
}
else {
printf("bpy: couldnt find 'scripts/modules', blender probably wont start.\n");
}
/* stand alone utility modules not related to blender directly */
Geometry_Init();
Mathutils_Init();

View File

@@ -24,6 +24,8 @@
/* ****************************************** */
/* Drivers - PyExpression Evaluation */
#include <Python.h>
#include "DNA_anim_types.h"
#include "BLI_listbase.h"
@@ -32,8 +34,6 @@
#include "BKE_fcurve.h"
#include "BKE_global.h"
#include <Python.h>
/* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
PyObject *bpy_pydriver_Dict = NULL;

View File

@@ -56,8 +56,8 @@
static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self);
static Py_ssize_t pyrna_prop_collection_length( BPy_PropertyRNA *self );
static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self);
static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, short order_fallback);
/* bpyrna vector/euler/quat callbacks */
static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */
@@ -68,22 +68,33 @@ static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */
#define MATHUTILS_CB_SUBTYPE_QUAT 2
#define MATHUTILS_CB_SUBTYPE_COLOR 0
static int mathutils_rna_generic_check(BPy_PropertyRNA *self)
static int mathutils_rna_generic_check(BaseMathObject *bmo)
{
return self->prop?1:0;
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
return self->prop ? 1:0;
}
static int mathutils_rna_vector_get(BPy_PropertyRNA *self, int subtype, float *vec_from)
static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
{
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
if(self->prop==NULL)
return 0;
RNA_property_float_get_array(&self->ptr, self->prop, vec_from);
RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
/* Euler order exception */
if(subtype==MATHUTILS_CB_SUBTYPE_EUL) {
EulerObject *eul= (EulerObject *)bmo;
PropertyRNA *prop_eul_order= NULL;
eul->order= pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
}
return 1;
}
static int mathutils_rna_vector_set(BPy_PropertyRNA *self, int subtype, float *vec_to)
static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
{
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
float min, max;
if(self->prop==NULL)
return 0;
@@ -93,31 +104,46 @@ static int mathutils_rna_vector_set(BPy_PropertyRNA *self, int subtype, float *v
if(min != FLT_MIN || max != FLT_MAX) {
int i, len= RNA_property_array_length(&self->ptr, self->prop);
for(i=0; i<len; i++) {
CLAMP(vec_to[i], min, max);
CLAMP(bmo->data[i], min, max);
}
}
RNA_property_float_set_array(&self->ptr, self->prop, vec_to);
RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
/* Euler order exception */
if(subtype==MATHUTILS_CB_SUBTYPE_EUL) {
EulerObject *eul= (EulerObject *)bmo;
PropertyRNA *prop_eul_order= NULL;
short order= pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
if(order != eul->order) {
RNA_property_enum_set(&self->ptr, prop_eul_order, eul->order);
RNA_property_update(BPy_GetContext(), &self->ptr, prop_eul_order);
}
}
return 1;
}
static int mathutils_rna_vector_get_index(BPy_PropertyRNA *self, int subtype, float *vec_from, int index)
static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int subtype, int index)
{
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
if(self->prop==NULL)
return 0;
vec_from[index]= RNA_property_float_get_index(&self->ptr, self->prop, index);
bmo->data[index]= RNA_property_float_get_index(&self->ptr, self->prop, index);
return 1;
}
static int mathutils_rna_vector_set_index(BPy_PropertyRNA *self, int subtype, float *vec_to, int index)
static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int subtype, int index)
{
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
if(self->prop==NULL)
return 0;
RNA_property_float_clamp(&self->ptr, self->prop, &vec_to[index]);
RNA_property_float_set_index(&self->ptr, self->prop, index, vec_to[index]);
RNA_property_float_clamp(&self->ptr, self->prop, &bmo->data[index]);
RNA_property_float_set_index(&self->ptr, self->prop, index, bmo->data[index]);
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
return 1;
}
@@ -134,31 +160,35 @@ Mathutils_Callback mathutils_rna_array_cb = {
/* 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)
static int mathutils_rna_matrix_get(BaseMathObject *bmo, int subtype)
{
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
if(self->prop==NULL)
return 0;
RNA_property_float_get_array(&self->ptr, self->prop, mat_from);
RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
return 1;
}
static int mathutils_rna_matrix_set(BPy_PropertyRNA *self, int subtype, float *mat_to)
static int mathutils_rna_matrix_set(BaseMathObject *bmo, int subtype)
{
BPy_PropertyRNA *self= (BPy_PropertyRNA *)bmo->cb_user;
if(self->prop==NULL)
return 0;
/* can ignore clamping here */
RNA_property_float_set_array(&self->ptr, self->prop, mat_to);
RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
return 1;
}
Mathutils_Callback mathutils_rna_matrix_cb = {
(BaseMathCheckFunc) mathutils_rna_generic_check,
(BaseMathGetFunc) mathutils_rna_matrix_get,
(BaseMathSetFunc) mathutils_rna_matrix_set,
(BaseMathGetIndexFunc) NULL,
(BaseMathSetIndexFunc) NULL
mathutils_rna_generic_check,
mathutils_rna_matrix_get,
mathutils_rna_matrix_set,
NULL,
NULL
};
/* same as RNA_enum_value_from_id but raises an exception */
@@ -242,11 +272,16 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
case PROP_QUATERNION:
if(len==3) { /* euler */
if(is_thick) {
ret= newEulerObject(NULL, 0, Py_NEW, NULL); // TODO, get order from RNA
/* attempt to get order, only needed for thixk types since wrapped with update via callbacks */
PropertyRNA *prop_eul_order= NULL;
short order= pyrna_rotation_euler_order_get(ptr, &prop_eul_order, ROT_MODE_XYZ);
ret= newEulerObject(NULL, order, Py_NEW, NULL); // TODO, get order from RNA
RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
}
else {
PyObject *eul_cb= newEulerObject_cb(ret, 0, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
/* order will be updated from callback on use */
PyObject *eul_cb= newEulerObject_cb(ret, ROT_MODE_XYZ, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
Py_DECREF(ret); /* the euler owns now */
ret= eul_cb; /* return the euler instead */
}
@@ -297,6 +332,21 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
#endif
static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, short order_fallback)
{
/* attempt to get order */
if(*prop_eul_order==NULL)
*prop_eul_order= RNA_struct_find_property(ptr, "rotation_mode");
if(*prop_eul_order) {
short order= RNA_property_enum_get(ptr, *prop_eul_order);
if (order >= ROT_MODE_XYZ && order <= ROT_MODE_ZYX) /* could be quat or axisangle */
return order;
}
return order_fallback;
}
static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
{
return (a->ptr.data==b->ptr.data) ? 0 : -1;
@@ -431,7 +481,27 @@ static PyObject *pyrna_prop_repr( BPy_PropertyRNA *self )
static long pyrna_struct_hash( BPy_StructRNA *self )
{
return (long)self->ptr.data;
return _Py_HashPointer(self->ptr.data);
}
/* from python's meth_hash v3.1.2 */
static long pyrna_prop_hash(BPy_PropertyRNA *self)
{
long x,y;
if (self->ptr.data == NULL)
x = 0;
else {
x = _Py_HashPointer(self->ptr.data);
if (x == -1)
return -1;
}
y = _Py_HashPointer((void*)(self->prop));
if (y == -1)
return -1;
x ^= y;
if (x == -1)
x = -2;
return x;
}
/* use our own dealloc so we can free a property if we use one */
@@ -1346,7 +1416,7 @@ static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop, in
return -1;
}
if(!(value=PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: type is not a sequence"))) {
if(!(value=PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: assignment is not a sequence type"))) {
return -1;
}
@@ -1452,43 +1522,50 @@ static int prop_subscript_ass_array_int(BPy_PropertyRNA *self, Py_ssize_t keynum
static int pyrna_prop_array_ass_subscript( BPy_PropertyRNA *self, PyObject *key, PyObject *value )
{
/* char *keyname = NULL; */ /* not supported yet */
int ret= -1;
if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
PyErr_Format(PyExc_AttributeError, "bpy_prop_collection: attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
return -1;
ret= -1;
}
if (PyIndex_Check(key)) {
else if (PyIndex_Check(key)) {
Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return -1;
return prop_subscript_ass_array_int(self, i, value);
if (i == -1 && PyErr_Occurred()) {
ret= -1;
}
else {
ret= prop_subscript_ass_array_int(self, i, value);
}
}
else if (PySlice_Check(key)) {
int len= RNA_property_array_length(&self->ptr, self->prop);
Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
return -1;
if (slicelength <= 0) {
return 0;
if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0) {
ret= -1;
}
else if (slicelength <= 0) {
ret= 0; /* do nothing */
}
else if (step == 1) {
return prop_subscript_ass_array_slice(&self->ptr, self->prop, start, stop, len, value);
ret= prop_subscript_ass_array_slice(&self->ptr, self->prop, start, stop, len, value);
}
else {
PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
return -1;
ret= -1;
}
}
else {
PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
return -1;
ret= -1;
}
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
if(ret != -1) {
RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
}
return ret;
}
/* for slice only */
@@ -2229,28 +2306,35 @@ static PyObject *pyrna_struct_getattro( BPy_StructRNA *self, PyObject *pyname )
else {
PointerRNA newptr;
ListBase newlb;
short newtype;
int done= CTX_data_get(C, name, &newptr, &newlb);
int done= CTX_data_get(C, name, &newptr, &newlb, &newtype);
if(done==1) { /* found */
if (newptr.data) {
ret = pyrna_struct_CreatePyObject(&newptr);
}
else if (newlb.first) {
CollectionPointerLink *link;
PyObject *linkptr;
ret = PyList_New(0);
for(link=newlb.first; link; link=link->next) {
linkptr= pyrna_struct_CreatePyObject(&link->ptr);
PyList_Append(ret, linkptr);
Py_DECREF(linkptr);
switch(newtype) {
case CTX_DATA_TYPE_POINTER:
if(newptr.data == NULL) {
ret= Py_None;
Py_INCREF(ret);
}
}
else {
ret = Py_None;
Py_INCREF(ret);
else {
ret= pyrna_struct_CreatePyObject(&newptr);
}
break;
case CTX_DATA_TYPE_COLLECTION:
{
CollectionPointerLink *link;
PyObject *linkptr;
ret = PyList_New(0);
for(link=newlb.first; link; link=link->next) {
linkptr= pyrna_struct_CreatePyObject(&link->ptr);
PyList_Append(ret, linkptr);
Py_DECREF(linkptr);
}
}
break;
}
}
else if (done==-1) { /* found but not set */
@@ -3476,7 +3560,7 @@ PyTypeObject pyrna_prop_Type = {
/* More standard operations (here for binary compatibility) */
NULL, /* hashfunc tp_hash; */
( hashfunc ) pyrna_prop_hash, /* hashfunc tp_hash; */
NULL, /* ternaryfunc tp_call; */
NULL, /* reprfunc tp_str; */
@@ -3898,7 +3982,8 @@ static PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
{
BPy_StructRNA *pyrna= NULL;
/* note: don't rely on this to return None since NULL data with a valid type can often crash */
if (ptr->data==NULL && ptr->type==NULL) { /* Operator RNA has NULL data */
Py_RETURN_NONE;
}