diff options
author | Joseph Gilbert <ascotan@gmail.com> | 2005-05-20 23:28:04 +0400 |
---|---|---|
committer | Joseph Gilbert <ascotan@gmail.com> | 2005-05-20 23:28:04 +0400 |
commit | 7586eb28a14c1283fdac8d485edf46cabd6219ad (patch) | |
tree | 774a811c3dcb7a49113e062d91cf0eb047b2a7fb /source/blender/python/api2_2x/euler.c | |
parent | d99f64b82346da82f4f1a179c6f3b647f90d44ed (diff) |
-rewrite and bugfixes
----------------------------------
Here's my changelog:
-fixed Rand() so that it doesn't seed everytime and should generate better random numbers
- changed a few error return types to something more appropriate
- clean up of uninitialized variables & removal of unneccessary objects
- NMesh returns wrapped vectors now
- World returns wrapped matrices now
- Object.getEuler() and Object.getBoundingBox() return Wrapped data when data is present
- Object.getMatrix() returns wrapped data if it's worldspace, 'localspace' returns a new matrix
- Vector, Euler, Mat, Quat, call all now internally wrap object without destroying internal datablocks
- Removed memory allocation (unneeded) from all methods
- Vector's resize methods are only applicable to new vectors not wrapped data.
- Matrix(), Quat(), Euler(), Vector() now accepts ANY sequence list, including tuples, list, or a self object to copy - matrices accept multiple sequences
- Fixed Slerp() so that it now works correctly values are clamped between 0 and 1
- Euler.rotate does internal rotation now
- Slice assignment now works better for all types
- Vector * Vector and Quat * Quat are defined and return the DOT product
- Mat * Vec and Vec * Mat are defined now
- Moved #includes to .c file from headers. Also fixed prototypes in mathutils
- Added new helper functions for incref'ing to genutils
- Major cleanup of header files includes - include Mathutils.h for access to math types
- matrix.toQuat() and .toEuler() now fixed take appropriate matrix sizes
- Matrix() with no parameters now returns an identity matrix by default not a zero matrix
- printf() now prints with 6 digits instead of 4
- printf() now prints output with object descriptor
- Matrices now support [x][y] assignment (e.g. matrix[x][y] = 5.4)
- Matrix[index] = value now expectes a sequence not an integer. This will now set a ROW of the matrix through a sequence. index cannot go above the row size of the matrix.
- slice operations on matrices work with sequences now (rows of the matrix) example: mymatrix[0:2] returns a list of 2 wrapped vectors with access to the matrix data.
- slice assignment will no longer modify the data if the assignment operation fails
- fixed error in matrix * scalar multiplication
- euler.toMatrix(), toQuat() no longer causes "creep" from repeated use
- Wrapped data will generate wrapped objects when toEuler(), toQuat(), toMatrix() is used
- Quats can be created with angle/axis, axis/angle
- 4x4 matrices can be multiplied by 3D vectors (by popular demand :))
- vec *quat / quat * vec is now defined
- vec.magnitude alias for vec.length
- all self, internal methods return a pointer to self now so you can do print vector.internalmethod() or vector.internalmethod().nextmethod() (no more print matrix.inverse() returning 'none')
- these methods have been deprecated (still functioning but suggested to use the corrected functionality):
* CopyVec() - replaced by Vector() functionality
* CopyMat() - replaced by Matrix() functionality
* CopyQuat() - replace by Quaternion() functionality
* CopyEuler() - replaced by Euler() functionality
* RotateEuler() - replaced by Euler.rotate() funtionality
* MatMultVec() - replaced by matrix * vector
* VecMultMat() - replaced by vector * matrix
- New struct containers references to python object data or internally allocated blender data for wrapping
* Explaination here: math structs now function as a 'simple wrapper' or a 'py_object' - data that is created on the fly will now be a 'py_object' with its memory managed by python
* otherwise if the data is returned by blender's G.main then the math object is a 'simple wrapper' and data can be accessed directly from the struct just like other python objects.
Diffstat (limited to 'source/blender/python/api2_2x/euler.c')
-rw-r--r-- | source/blender/python/api2_2x/euler.c | 546 |
1 files changed, 301 insertions, 245 deletions
diff --git a/source/blender/python/api2_2x/euler.c b/source/blender/python/api2_2x/euler.c index 6b72460ccd4..20f3895442b 100644 --- a/source/blender/python/api2_2x/euler.c +++ b/source/blender/python/api2_2x/euler.c @@ -29,329 +29,385 @@ * ***** END GPL/BL DUAL LICENSE BLOCK ***** */ -#include "euler.h" +#include <BLI_arithb.h> +#include <BKE_utildefines.h> +#include "Mathutils.h" +#include "gen_utils.h" -//doc strings +//-------------------------DOC STRINGS --------------------------- char Euler_Zero_doc[] = "() - set all values in the euler to 0"; -char Euler_Unique_doc[] = - "() - sets the euler rotation a unique shortest arc rotation - tests for gimbal lock"; -char Euler_ToMatrix_doc[] = - "() - returns a rotation matrix representing the euler rotation"; -char Euler_ToQuat_doc[] = - "() - returns a quaternion representing the euler rotation"; - -//methods table +char Euler_Unique_doc[] ="() - sets the euler rotation a unique shortest arc rotation - tests for gimbal lock"; +char Euler_ToMatrix_doc[] = "() - returns a rotation matrix representing the euler rotation"; +char Euler_ToQuat_doc[] = "() - returns a quaternion representing the euler rotation"; +char Euler_Rotate_doc[] = "() - rotate a euler by certain amount around an axis of rotation"; +//-----------------------METHOD DEFINITIONS ---------------------- struct PyMethodDef Euler_methods[] = { - {"zero", ( PyCFunction ) Euler_Zero, METH_NOARGS, - Euler_Zero_doc}, - {"unique", ( PyCFunction ) Euler_Unique, METH_NOARGS, - Euler_Unique_doc}, - {"toMatrix", ( PyCFunction ) Euler_ToMatrix, METH_NOARGS, - Euler_ToMatrix_doc}, - {"toQuat", ( PyCFunction ) Euler_ToQuat, METH_NOARGS, - Euler_ToQuat_doc}, + {"zero", (PyCFunction) Euler_Zero, METH_NOARGS, Euler_Zero_doc}, + {"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc}, + {"toMatrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc}, + {"toQuat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc}, + {"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, Euler_Rotate_doc}, {NULL, NULL, 0, NULL} }; - -/*****************************/ -// Euler Python Object -/*****************************/ - -//euler methods -PyObject *Euler_ToQuat( EulerObject * self ) +//-----------------------------METHODS---------------------------- +//----------------------------Euler.toQuat()---------------------- +//return a quaternion representation of the euler +PyObject *Euler_ToQuat(EulerObject * self) { - float *quat; + float eul[3]; + float quat[4]; int x; - for( x = 0; x < 3; x++ ) { - self->eul[x] *= ( float ) ( Py_PI / 180 ); - } - quat = PyMem_Malloc( 4 * sizeof( float ) ); - EulToQuat( self->eul, quat ); - for( x = 0; x < 3; x++ ) { - self->eul[x] *= ( float ) ( 180 / Py_PI ); + for(x = 0; x < 3; x++) { + eul[x] = self->eul[x] * ((float)Py_PI / 180); } - return ( PyObject * ) newQuaternionObject( quat ); + EulToQuat(eul, quat); + if(self->data.blend_data) + return (PyObject *) newQuaternionObject(quat, Py_WRAP); + else + return (PyObject *) newQuaternionObject(quat, Py_NEW); } - -PyObject *Euler_ToMatrix( EulerObject * self ) +//----------------------------Euler.toMatrix()--------------------- +//return a matrix representation of the euler +PyObject *Euler_ToMatrix(EulerObject * self) { - float *mat; + float eul[3]; + float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; int x; - for( x = 0; x < 3; x++ ) { - self->eul[x] *= ( float ) ( Py_PI / 180 ); - } - mat = PyMem_Malloc( 3 * 3 * sizeof( float ) ); - EulToMat3( self->eul, ( float ( * )[3] ) mat ); - for( x = 0; x < 3; x++ ) { - self->eul[x] *= ( float ) ( 180 / Py_PI ); + for(x = 0; x < 3; x++) { + eul[x] = self->eul[x] * ((float)Py_PI / 180); } - return ( PyObject * ) newMatrixObject( mat, 3, 3 ); + EulToMat3(eul, (float (*)[3]) mat); + if(self->data.blend_data) + return (PyObject *) newMatrixObject(mat, 3, 3 , Py_WRAP); + else + return (PyObject *) newMatrixObject(mat, 3, 3 , Py_NEW); } - -PyObject *Euler_Unique( EulerObject * self ) +//----------------------------Euler.unique()----------------------- +//sets the x,y,z values to a unique euler rotation +PyObject *Euler_Unique(EulerObject * self) { - float heading, pitch, bank; - float pi2 = ( float ) Py_PI * 2.0f; - float piO2 = ( float ) Py_PI / 2.0f; - float Opi2 = 1.0f / pi2; + double heading, pitch, bank; + double pi2 = Py_PI * 2.0f; + double piO2 = Py_PI / 2.0f; + double Opi2 = 1.0f / pi2; //radians - heading = self->eul[0] * ( float ) ( Py_PI / 180 ); - pitch = self->eul[1] * ( float ) ( Py_PI / 180 ); - bank = self->eul[2] * ( float ) ( Py_PI / 180 ); + heading = self->eul[0] * (float)Py_PI / 180; + pitch = self->eul[1] * (float)Py_PI / 180; + bank = self->eul[2] * (float)Py_PI / 180; //wrap heading in +180 / -180 - pitch += ( float ) Py_PI; - pitch -= ( float ) floor( pitch * Opi2 ) * pi2; - pitch -= ( float ) Py_PI; - - - if( pitch < -piO2 ) { - pitch = ( float ) -Py_PI - pitch; - heading += ( float ) Py_PI; - bank += ( float ) Py_PI; - } else if( pitch > piO2 ) { - pitch = ( float ) Py_PI - pitch; - heading += ( float ) Py_PI; - bank += ( float ) Py_PI; + pitch += Py_PI; + pitch -= floor(pitch * Opi2) * pi2; + pitch -= Py_PI; + + + if(pitch < -piO2) { + pitch = -Py_PI - pitch; + heading += Py_PI; + bank += Py_PI; + } else if(pitch > piO2) { + pitch = Py_PI - pitch; + heading += Py_PI; + bank += Py_PI; } //gimbal lock test - if( fabs( pitch ) > piO2 - 1e-4 ) { + if(fabs(pitch) > piO2 - 1e-4) { heading += bank; bank = 0.0f; } else { - bank += ( float ) Py_PI; - bank -= ( float ) ( floor( bank * Opi2 ) ) * pi2; - bank -= ( float ) Py_PI; + bank += Py_PI; + bank -= (floor(bank * Opi2)) * pi2; + bank -= Py_PI; } - heading += ( float ) Py_PI; - heading -= ( float ) ( floor( heading * Opi2 ) ) * pi2; - heading -= ( float ) Py_PI; + heading += Py_PI; + heading -= (floor(heading * Opi2)) * pi2; + heading -= Py_PI; //back to degrees - self->eul[0] = heading * ( float ) ( 180 / Py_PI ); - self->eul[1] = pitch * ( float ) ( 180 / Py_PI ); - self->eul[2] = bank * ( float ) ( 180 / Py_PI ); + self->eul[0] = heading * 180 / (float)Py_PI; + self->eul[1] = pitch * 180 / (float)Py_PI; + self->eul[2] = bank * 180 / (float)Py_PI; - return EXPP_incr_ret( Py_None ); + return (PyObject*)self; } - -PyObject *Euler_Zero( EulerObject * self ) +//----------------------------Euler.zero()------------------------- +//sets the euler to 0,0,0 +PyObject *Euler_Zero(EulerObject * self) { self->eul[0] = 0.0; self->eul[1] = 0.0; self->eul[2] = 0.0; - return EXPP_incr_ret( Py_None ); + return (PyObject*)self; } - -static void Euler_dealloc( EulerObject * 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 :) +PyObject *Euler_Rotate(EulerObject * self, PyObject *args) { - /* since we own this memory... */ - PyMem_Free( self->eul ); + float angle = 0.0f; + char *axis; + int x; - PyObject_DEL( self ); -} + if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){ + return EXPP_ReturnPyObjError(PyExc_TypeError, + "euler.rotate():expected angle (float) and axis (x,y,z)"); + } + if(!STREQ3(axis,"x","y","z")){ + return EXPP_ReturnPyObjError(PyExc_TypeError, + "euler.rotate(): expected axis to be 'x', 'y' or 'z'"); + } + + //covert to radians + angle *= ((float)Py_PI / 180); + for(x = 0; x < 3; x++) { + self->eul[x] *= ((float)Py_PI / 180); + } + euler_rot(self->eul, angle, *axis); + //convert back from radians + for(x = 0; x < 3; x++) { + self->eul[x] *= (180 / (float)Py_PI); + } -static PyObject *Euler_getattr( EulerObject * self, char *name ) + return (PyObject*)self; +} +//----------------------------dealloc()(internal) ------------------ +//free the py_object +static void Euler_dealloc(EulerObject * self) { - if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) { - return PyFloat_FromDouble( self->eul[name[0] - 'x'] ); + //only free py_data + if(self->data.py_data){ + PyMem_Free(self->data.py_data); } - return Py_FindMethod( Euler_methods, ( PyObject * ) self, name ); + PyObject_DEL(self); } - -static int Euler_setattr( EulerObject * self, char *name, PyObject * e ) +//----------------------------getattr()(internal) ------------------ +//object.attribute access (get) +static PyObject *Euler_getattr(EulerObject * self, char *name) { - float val; + int x; - if( !PyArg_Parse( e, "f", &val ) ) - return EXPP_ReturnIntError( PyExc_TypeError, - "unable to parse float argument\n" ); + if(STREQ(name,"x")){ + return PyFloat_FromDouble(self->eul[0]); + }else if(STREQ(name, "y")){ + return PyFloat_FromDouble(self->eul[1]); + }else if(STREQ(name, "z")){ + return PyFloat_FromDouble(self->eul[2]); + } - if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) { - self->eul[name[0] - 'x'] = val; - return 0; - } else - return -1; + return Py_FindMethod(Euler_methods, (PyObject *) self, name); } - -/* Eulers Sequence methods */ -static PyObject *Euler_item( EulerObject * self, int i ) +//----------------------------setattr()(internal) ------------------ +//object.attribute access (set) +static int Euler_setattr(EulerObject * self, char *name, PyObject * e) { - if( i < 0 || i >= 3 ) - return EXPP_ReturnPyObjError( PyExc_IndexError, - "array index out of range\n" ); + PyObject *f = NULL; - return Py_BuildValue( "f", self->eul[i] ); -} + f = PyNumber_Float(e); + if(f == NULL) { // parsed item not a number + return EXPP_ReturnIntError(PyExc_TypeError, + "euler.attribute = x: argument not a number\n"); + } + + if(STREQ(name,"x")){ + self->eul[0] = PyFloat_AS_DOUBLE(f); + }else if(STREQ(name, "y")){ + self->eul[1] = PyFloat_AS_DOUBLE(f); + }else if(STREQ(name, "z")){ + self->eul[2] = PyFloat_AS_DOUBLE(f); + }else{ + Py_DECREF(f); + return EXPP_ReturnIntError(PyExc_AttributeError, + "euler.attribute = x: unknown attribute\n"); + } -static PyObject *Euler_slice( EulerObject * self, int begin, int end ) + Py_DECREF(f); + return 0; +} +//----------------------------print object (internal)-------------- +//print the object to screen +static PyObject *Euler_repr(EulerObject * self) { - PyObject *list; - int count; + int i; + char buffer[48], str[1024]; + + BLI_strncpy(str,"[",1024); + for(i = 0; i < 3; i++){ + if(i < (2)){ + sprintf(buffer, "%.6f, ", self->eul[i]); + strcat(str,buffer); + }else{ + sprintf(buffer, "%.6f", self->eul[i]); + strcat(str,buffer); + } + } + strcat(str, "](euler)"); - if( begin < 0 ) - begin = 0; - if( end > 3 ) - end = 3; - if( begin > end ) - begin = end; + return EXPP_incr_ret(PyString_FromString(str)); +} +//---------------------SEQUENCE PROTOCOLS------------------------ +//----------------------------len(object)------------------------ +//sequence length +static int Euler_len(EulerObject * self) +{ + return 3; +} +//----------------------------object[]--------------------------- +//sequence accessor (get) +static PyObject *Euler_item(EulerObject * self, int i) +{ + if(i < 0 || i >= 3) + return EXPP_ReturnPyObjError(PyExc_IndexError, + "euler[attribute]: array index out of range\n"); - list = PyList_New( end - begin ); + return Py_BuildValue("f", self->eul[i]); - for( count = begin; count < end; count++ ) { - PyList_SetItem( list, count - begin, - PyFloat_FromDouble( self->eul[count] ) ); - } - return list; } - -static int Euler_ass_item( EulerObject * self, int i, PyObject * ob ) +//----------------------------object[]------------------------- +//sequence accessor (set) +static int Euler_ass_item(EulerObject * self, int i, PyObject * ob) { - if( i < 0 || i >= 3 ) - return EXPP_ReturnIntError( PyExc_IndexError, - "array assignment index out of range\n" ); + PyObject *f = NULL; - if( !PyNumber_Check( ob ) ) - return EXPP_ReturnIntError( PyExc_IndexError, - "Euler member must be a number\n" ); + f = PyNumber_Float(ob); + if(f == NULL) { // parsed item not a number + return EXPP_ReturnIntError(PyExc_TypeError, + "euler[attribute] = x: argument not a number\n"); + } - if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) { - return EXPP_ReturnIntError( PyExc_TypeError, - "int or float expected\n" ); - } else { - self->eul[i] = ( float ) PyFloat_AsDouble( ob ); + if(i < 0 || i >= 3){ + Py_DECREF(f); + return EXPP_ReturnIntError(PyExc_IndexError, + "euler[attribute] = x: array assignment index out of range\n"); } + self->eul[i] = PyFloat_AS_DOUBLE(f); + Py_DECREF(f); return 0; } - -static int Euler_ass_slice( EulerObject * self, int begin, int end, - PyObject * seq ) +//----------------------------object[z:y]------------------------ +//sequence slice (get) +static PyObject *Euler_slice(EulerObject * self, int begin, int end) { - int count, z; - - if( begin < 0 ) - begin = 0; - if( end > 3 ) - end = 3; - if( begin > end ) - begin = end; - - if( !PySequence_Check( seq ) ) - return EXPP_ReturnIntError( PyExc_TypeError, - "illegal argument type for built-in operation\n" ); - if( PySequence_Length( seq ) != ( end - begin ) ) - return EXPP_ReturnIntError( PyExc_TypeError, - "size mismatch in slice assignment\n" ); - - z = 0; - for( count = begin; count < end; count++ ) { - PyObject *ob = PySequence_GetItem( seq, z ); - z++; - - if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) { - Py_DECREF( ob ); - return -1; - } else { - if( !PyArg_Parse( ob, "f", &self->eul[count] ) ) { - Py_DECREF( ob ); - return -1; - } - } + PyObject *list = NULL; + int count; + + CLAMP(begin, 0, 3); + CLAMP(end, 0, 3); + begin = MIN2(begin,end); + + list = PyList_New(end - begin); + for(count = begin; count < end; count++) { + PyList_SetItem(list, count - begin, + PyFloat_FromDouble(self->eul[count])); } - return 0; -} -static PyObject *Euler_repr( EulerObject * self ) + return list; +} +//----------------------------object[z:y]------------------------ +//sequence slice (set) +static int Euler_ass_slice(EulerObject * self, int begin, int end, + PyObject * seq) { - int i, maxindex = 3 - 1; - char ftoa[24]; - PyObject *str1, *str2; - - str1 = PyString_FromString( "[" ); - - for( i = 0; i < maxindex; i++ ) { - sprintf( ftoa, "%.4f, ", self->eul[i] ); - str2 = PyString_FromString( ftoa ); - if( !str1 || !str2 ) - goto error; - PyString_ConcatAndDel( &str1, str2 ); - } + int i, y, size = 0; + float eul[3]; - sprintf( ftoa, "%.4f]\n", self->eul[maxindex] ); - str2 = PyString_FromString( ftoa ); - if( !str1 || !str2 ) - goto error; - PyString_ConcatAndDel( &str1, str2 ); + CLAMP(begin, 0, 3); + CLAMP(end, 0, 3); + begin = MIN2(begin,end); - if( str1 ) - return str1; + size = PySequence_Length(seq); + if(size != (end - begin)){ + return EXPP_ReturnIntError(PyExc_TypeError, + "euler[begin:end] = []: size mismatch in slice assignment\n"); + } - error: - Py_XDECREF( str1 ); - Py_XDECREF( str2 ); - return EXPP_ReturnPyObjError( PyExc_MemoryError, - "couldn't create PyString!\n" ); -} + for (i = 0; i < size; i++) { + PyObject *e, *f; + e = PySequence_GetItem(seq, i); + if (e == NULL) { // Failed to read sequence + return EXPP_ReturnIntError(PyExc_RuntimeError, + "euler[begin:end] = []: unable to read sequence\n"); + } + f = PyNumber_Float(e); + if(f == NULL) { // parsed item not a number + Py_DECREF(e); + return EXPP_ReturnIntError(PyExc_TypeError, + "euler[begin:end] = []: sequence argument not a number\n"); + } + eul[i] = PyFloat_AS_DOUBLE(f); + EXPP_decr2(f,e); + } + //parsed well - now set in vector + for(y = 0; y < 3; y++){ + self->eul[begin + y] = eul[y]; + } + return 0; +} +//-----------------PROTCOL DECLARATIONS-------------------------- static PySequenceMethods Euler_SeqMethods = { - ( inquiry ) 0, /* sq_length */ - ( binaryfunc ) 0, /* sq_concat */ - ( intargfunc ) 0, /* sq_repeat */ - ( intargfunc ) Euler_item, /* sq_item */ - ( intintargfunc ) Euler_slice, /* sq_slice */ - ( intobjargproc ) Euler_ass_item, /* sq_ass_item */ - ( intintobjargproc ) Euler_ass_slice, /* sq_ass_slice */ + (inquiry) Euler_len, /* sq_length */ + (binaryfunc) 0, /* sq_concat */ + (intargfunc) 0, /* sq_repeat */ + (intargfunc) Euler_item, /* sq_item */ + (intintargfunc) Euler_slice, /* sq_slice */ + (intobjargproc) Euler_ass_item, /* sq_ass_item */ + (intintobjargproc) Euler_ass_slice, /* sq_ass_slice */ }; - +//------------------PY_OBECT DEFINITION-------------------------- PyTypeObject euler_Type = { - PyObject_HEAD_INIT( NULL ) - 0, /*ob_size */ - "euler", /*tp_name */ - sizeof( EulerObject ), /*tp_basicsize */ - 0, /*tp_itemsize */ - ( destructor ) Euler_dealloc, /*tp_dealloc */ - ( printfunc ) 0, /*tp_print */ - ( getattrfunc ) Euler_getattr, /*tp_getattr */ - ( setattrfunc ) Euler_setattr, /*tp_setattr */ - 0, /*tp_compare */ - ( reprfunc ) Euler_repr, /*tp_repr */ - 0, /*tp_as_number */ - &Euler_SeqMethods, /*tp_as_sequence */ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size */ + "euler", /*tp_name */ + sizeof(EulerObject), /*tp_basicsize */ + 0, /*tp_itemsize */ + (destructor) Euler_dealloc, /*tp_dealloc */ + (printfunc) 0, /*tp_print */ + (getattrfunc) Euler_getattr, /*tp_getattr */ + (setattrfunc) Euler_setattr, /*tp_setattr */ + 0, /*tp_compare */ + (reprfunc) Euler_repr, /*tp_repr */ + 0, /*tp_as_number */ + &Euler_SeqMethods, /*tp_as_sequence */ }; - -PyObject *newEulerObject( float *eul ) +//------------------------newEulerObject (internal)------------- +//creates a new euler object +/*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER + (i.e. it was allocated elsewhere by MEM_mallocN()) + pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON + (i.e. it must be created here with PyMEM_malloc())*/ +PyObject *newEulerObject(float *eul, int type) { EulerObject *self; int x; euler_Type.ob_type = &PyType_Type; - - self = PyObject_NEW( EulerObject, &euler_Type ); - - /* - we own the self->eul memory and will free it later. - if we received an input arg, copy to our internal array - */ - - self->eul = PyMem_Malloc( 3 * sizeof( float ) ); - if( ! self->eul ) - return EXPP_ReturnPyObjError( PyExc_MemoryError, - "newEulerObject:PyMem_Malloc failed" ); - - if( !eul ) { - for( x = 0; x < 3; x++ ) { - self->eul[x] = 0.0f; - } - } else{ - for( x = 0; x < 3; x++){ - self->eul[x] = eul[x]; + self = PyObject_NEW(EulerObject, &euler_Type); + self->data.blend_data = NULL; + self->data.py_data = NULL; + + if(type == Py_WRAP){ + self->data.blend_data = eul; + self->eul = self->data.blend_data; + }else if (type == Py_NEW){ + self->data.py_data = PyMem_Malloc(3 * sizeof(float)); + self->eul = self->data.py_data; + if(!eul) { //new empty + for(x = 0; x < 3; x++) { + self->eul[x] = 0.0f; + } + }else{ + for(x = 0; x < 3; x++){ + self->eul[x] = eul[x]; + } } + }else{ //bad type + return NULL; } - - return ( PyObject * ) self; + return (PyObject *) EXPP_incr_ret((PyObject *)self); } + |