diff options
author | Willian Padovani Germano <wpgermano@gmail.com> | 2003-05-23 08:34:55 +0400 |
---|---|---|
committer | Willian Padovani Germano <wpgermano@gmail.com> | 2003-05-23 08:34:55 +0400 |
commit | 9edf1c08a6cfdf77e484478143c5ac9e947d25d2 (patch) | |
tree | d68525abbe1773f49060b96536931512e60216e2 /source/blender | |
parent | 0773536829482ac79032efc6926be6a2e5da8813 (diff) |
* Fixed bug in BPY_interface.c (exppython):
Found that syntax errors in scripts were giving SIGSEGV, my mistake.
* Added new helper type: rgbTuple.
This is used to represent and deal with rgb color triplets in modules
like Material and Lamp. Updated Lamp module to use it.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/python/api2_2x/Lamp.c | 47 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Lamp.h | 13 | ||||
-rw-r--r-- | source/blender/python/api2_2x/NMesh.c | 2 | ||||
-rw-r--r-- | source/blender/python/api2_2x/NMesh.h | 2 | ||||
-rw-r--r-- | source/blender/python/api2_2x/constant.c | 21 | ||||
-rw-r--r-- | source/blender/python/api2_2x/constant.h | 2 | ||||
-rw-r--r-- | source/blender/python/api2_2x/rgbTuple.c | 400 | ||||
-rw-r--r-- | source/blender/python/api2_2x/rgbTuple.h | 59 |
8 files changed, 513 insertions, 33 deletions
diff --git a/source/blender/python/api2_2x/Lamp.c b/source/blender/python/api2_2x/Lamp.c index 6d5da492fe4..f6537280f30 100644 --- a/source/blender/python/api2_2x/Lamp.c +++ b/source/blender/python/api2_2x/Lamp.c @@ -53,7 +53,7 @@ static PyObject *M_Lamp_New(PyObject *self, PyObject *args, PyObject *keywords) bl_lamp = add_lamp(); /* first create in Blender */ if (bl_lamp) /* now create the wrapper obj in Python */ - py_lamp = (C_Lamp *)PyObject_NEW(C_Lamp, &Lamp_Type); + py_lamp = (C_Lamp *)Lamp_createPyObject(bl_lamp); else return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't create Lamp Data in Blender")); @@ -110,11 +110,11 @@ static PyObject *M_Lamp_Get(PyObject *self, PyObject *args) C_Lamp *wanted_lamp = NULL; while ((lamp_iter) && (wanted_lamp == NULL)) { - if (strcmp (name, lamp_iter->id.name+2) == 0) { - wanted_lamp = (C_Lamp *)PyObject_NEW(C_Lamp, &Lamp_Type); - if (wanted_lamp) wanted_lamp->lamp = lamp_iter; - } - lamp_iter = lamp_iter->id.next; + + if (strcmp (name, lamp_iter->id.name+2) == 0) + wanted_lamp = (C_Lamp *)Lamp_createPyObject(lamp_iter); + + lamp_iter = lamp_iter->id.next; } if (wanted_lamp == NULL) { /* Requested lamp doesn't exist */ @@ -179,6 +179,7 @@ PyObject *M_Lamp_Init (void) PyObject *Lamp_createPyObject (Lamp *lamp) { C_Lamp *pylamp; + float *rgb[3]; pylamp = (C_Lamp *)PyObject_NEW (C_Lamp, &Lamp_Type); @@ -188,6 +189,12 @@ PyObject *Lamp_createPyObject (Lamp *lamp) pylamp->lamp = lamp; + rgb[0] = &lamp->r; + rgb[1] = &lamp->g; + rgb[2] = &lamp->b; + + pylamp->color = rgbTuple_New(rgb); + return (PyObject *)pylamp; } @@ -384,9 +391,14 @@ static PyObject *Lamp_getQuad2(C_Lamp *self) "couldn't get Lamp.quad2 attribute")); } +static PyObject *Lamp_getCol(C_Lamp *self) +{ + return rgbTuple_getCol((C_rgbTuple *)self->color); +} + static PyObject *Lamp_setName(C_Lamp *self, PyObject *args) { - char *name; + char *name = NULL; char buf[21]; if (!PyArg_ParseTuple(args, "s", &name)) @@ -755,6 +767,11 @@ static PyObject *Lamp_setQuad2(C_Lamp *self, PyObject *args) return Py_None; } +static PyObject *Lamp_setCol(C_Lamp *self, PyObject *args) +{ + return rgbTuple_setCol((C_rgbTuple *)self->color, args); +} + /*****************************************************************************/ /* Function: LampDeAlloc */ /* Description: This is a callback function for the C_Lamp type. It is */ @@ -771,7 +788,7 @@ static void LampDeAlloc (C_Lamp *self) /* the function that accesses C_Lamp member variables and */ /* methods. */ /*****************************************************************************/ -static PyObject* LampGetAttr (C_Lamp *self, char *name) +static PyObject *LampGetAttr (C_Lamp *self, char *name) { PyObject *attr = Py_None; @@ -793,6 +810,8 @@ static PyObject* LampGetAttr (C_Lamp *self, char *name) attr = PyFloat_FromDouble(self->lamp->g); else if (strcmp(name, "B") == 0) attr = PyFloat_FromDouble(self->lamp->b); + else if (strcmp(name, "col") == 0) + attr = EXPP_incr_ret(self->color); else if (strcmp(name, "energy") == 0) attr = PyFloat_FromDouble(self->lamp->energy); else if (strcmp(name, "dist") == 0) @@ -837,13 +856,13 @@ static PyObject* LampGetAttr (C_Lamp *self, char *name) } else if (strcmp(name, "__members__") == 0) { - /* 22 entries */ - attr = Py_BuildValue("[s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s]", + /* 23 entries */ + attr = Py_BuildValue("[s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s]", "name", "type", "mode", "samples", "bufferSize", "haloStep", "R", "G", "B", "energy", "dist", "spotSize", "spotBlend", "clipStart", "clipEnd", "bias", "softness", "haloInt", "quad1", "quad2", - "Types", "Modes"); + "Types", "Modes", "col"); } if (!attr) @@ -913,7 +932,9 @@ static int LampSetAttr (C_Lamp *self, char *name, PyObject *value) error = Lamp_setQuad1 (self, valtuple); else if (strcmp (name, "quad2") == 0) error = Lamp_setQuad2 (self, valtuple); - + else if (strcmp (name, "col") == 0) + error = Lamp_setCol (self, valtuple); + else { /* Error */ Py_DECREF(valtuple); @@ -923,7 +944,7 @@ static int LampSetAttr (C_Lamp *self, char *name, PyObject *value) "constant dictionary -- cannot be changed")); else /* ... or no member with the given name was found */ - return (EXPP_ReturnIntError (PyExc_KeyError, + return (EXPP_ReturnIntError (PyExc_AttributeError, "attribute not found")); } diff --git a/source/blender/python/api2_2x/Lamp.h b/source/blender/python/api2_2x/Lamp.h index 456d2587061..a03c464390f 100644 --- a/source/blender/python/api2_2x/Lamp.h +++ b/source/blender/python/api2_2x/Lamp.h @@ -43,6 +43,7 @@ #include <DNA_lamp_types.h> #include "constant.h" +#include "rgbTuple.h" #include "gen_utils.h" #include "modules.h" @@ -165,6 +166,8 @@ struct PyMethodDef M_Lamp_methods[] = { typedef struct { PyObject_HEAD Lamp *lamp; + PyObject *color; + } C_Lamp; /*****************************************************************************/ @@ -187,6 +190,7 @@ static PyObject *Lamp_getSoftness(C_Lamp *self); static PyObject *Lamp_getHaloInt(C_Lamp *self); static PyObject *Lamp_getQuad1(C_Lamp *self); static PyObject *Lamp_getQuad2(C_Lamp *self); +static PyObject *Lamp_getCol(C_Lamp *self); static PyObject *Lamp_setName(C_Lamp *self, PyObject *args); static PyObject *Lamp_setType(C_Lamp *self, PyObject *args); static PyObject *Lamp_setIntType(C_Lamp *self, PyObject *args); @@ -206,6 +210,8 @@ static PyObject *Lamp_setSoftness(C_Lamp *self, PyObject *args); static PyObject *Lamp_setHaloInt(C_Lamp *self, PyObject *args); static PyObject *Lamp_setQuad1(C_Lamp *self, PyObject *args); static PyObject *Lamp_setQuad2(C_Lamp *self, PyObject *args); +static PyObject *Lamp_setCol(C_Lamp *self, PyObject *args); + static PyObject *Lamp_setColorComponent(C_Lamp *self, char *key, PyObject *args); @@ -217,8 +223,7 @@ static PyMethodDef C_Lamp_methods[] = { {"getName", (PyCFunction)Lamp_getName, METH_NOARGS, "() - return Lamp name"}, {"getType", (PyCFunction)Lamp_getType, METH_NOARGS, - "() - return Lamp type -\n\t\ -'Lamp':0, 'Sun':1, 'Spot':2, 'Hemi':3"}, + "() - return Lamp type - 'Lamp':0, 'Sun':1, 'Spot':2, 'Hemi':3"}, {"getMode", (PyCFunction)Lamp_getMode, METH_NOARGS, "() - return Lamp mode flags (or'ed value)"}, {"getSamples", (PyCFunction)Lamp_getSamples, METH_NOARGS, @@ -249,6 +254,8 @@ static PyMethodDef C_Lamp_methods[] = { "() - return light intensity value #1 for a Quad Lamp"}, {"getQuad2", (PyCFunction)Lamp_getQuad2, METH_NOARGS, "() - return light intensity value #2 for a Quad Lamp"}, + {"getCol", (PyCFunction)Lamp_getCol, METH_NOARGS, + "() - return light rgb color triplet"}, {"setName", (PyCFunction)Lamp_setName, METH_VARARGS, "(str) - rename Lamp"}, {"setType", (PyCFunction)Lamp_setType, METH_VARARGS, @@ -281,6 +288,8 @@ static PyMethodDef C_Lamp_methods[] = { "(float) - change light intensity value #1 for a Quad Lamp"}, {"setQuad2", (PyCFunction)Lamp_setQuad2, METH_VARARGS, "(float) - change light intensity value #2 for a Quad Lamp"}, + {"setCol", (PyCFunction)Lamp_setCol, METH_VARARGS, + "(f,f,f) or ([f,f,f]) - change light's rgb color triplet"}, {0} }; diff --git a/source/blender/python/api2_2x/NMesh.c b/source/blender/python/api2_2x/NMesh.c index 03f07b0feb5..08eeddb35e6 100644 --- a/source/blender/python/api2_2x/NMesh.c +++ b/source/blender/python/api2_2x/NMesh.c @@ -46,7 +46,7 @@ void mesh_update(Mesh *mesh) static void NMCol_dealloc(PyObject *self) { - PyMem_DEL(self); + PyMem_DEL(self); /* XXX PyObject_Del ?*/ } static C_NMCol *newcol (char r, char g, char b, char a) diff --git a/source/blender/python/api2_2x/NMesh.h b/source/blender/python/api2_2x/NMesh.h index e19e2c4f4bb..a1d31876080 100644 --- a/source/blender/python/api2_2x/NMesh.h +++ b/source/blender/python/api2_2x/NMesh.h @@ -172,7 +172,7 @@ typedef struct { short mode; short flag; unsigned char transp; - PyObject *image; /* Image; was DataBlock *tpage */ + PyObject *image; /* Image; was DataBlock *tpage -- PyObj is wrong, change it*/ char mat_nr, smooth; } C_NMFace; /* an NMesh face */ diff --git a/source/blender/python/api2_2x/constant.c b/source/blender/python/api2_2x/constant.c index 30c020ed61e..7640940903d 100644 --- a/source/blender/python/api2_2x/constant.c +++ b/source/blender/python/api2_2x/constant.c @@ -102,16 +102,12 @@ static PyObject *new_const(void) constant = (C_constant *)PyObject_NEW(C_constant, &constant_Type); if (constant == NULL) - { return (PythonReturnErrorObject (PyExc_MemoryError, "couldn't create constant object")); - } if ((constant->dict = PyDict_New()) == NULL) - { return (PythonReturnErrorObject (PyExc_MemoryError, "couldn't create constant object's dictionary")); - } return (PyObject *)constant; } @@ -122,9 +118,7 @@ static PyObject *new_const(void) void constant_insert(C_constant *self, char *key, PyObject *value) { if (self->dict) - { PyDict_SetItemString(self->dict, key, value); - } } /*****************************************************************************/ @@ -144,23 +138,21 @@ static void constantDeAlloc (C_constant *self) /* the function that accesses C_constant member variables and */ /* methods. */ /*****************************************************************************/ -static PyObject* constantGetAttr (C_constant *self, char *name) +static PyObject *constantGetAttr (C_constant *self, char *name) { if (self->dict) { PyObject *v; if (!strcmp(name, "__members__")) - { return PyDict_Keys(self->dict); - } v = PyDict_GetItemString(self->dict, name); - if (v) - { + if (v) { Py_INCREF(v); /* was a borrowed ref */ return v; } + return (PythonReturnErrorObject (PyExc_AttributeError, "attribute not found")); } @@ -180,11 +172,10 @@ static int constantLength(C_constant *self) static PyObject *constantSubscript(C_constant *self, PyObject *key) { - if (self->dict) - { + if (self->dict) { PyObject *v = PyDict_GetItem(self->dict, key); - if (v) - { + + if (v) { Py_INCREF(v); return v; } diff --git a/source/blender/python/api2_2x/constant.h b/source/blender/python/api2_2x/constant.h index 3bb21dff28a..ead902d9255 100644 --- a/source/blender/python/api2_2x/constant.h +++ b/source/blender/python/api2_2x/constant.h @@ -37,7 +37,7 @@ #include "gen_utils.h" -/* Objects of <type 'constant'> are instanced inside many other Blender Python +/* Objects of <type 'constant'> are used inside many other Blender Python * objects, so this header file must contain only 'public' declarations */ /*****************************************************************************/ diff --git a/source/blender/python/api2_2x/rgbTuple.c b/source/blender/python/api2_2x/rgbTuple.c new file mode 100644 index 00000000000..14a8476a8fe --- /dev/null +++ b/source/blender/python/api2_2x/rgbTuple.c @@ -0,0 +1,400 @@ +/* + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * This is a new part of Blender. + * + * Contributor(s): Willian P. Germano + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#include "rgbTuple.h" + +/* This file is heavily based on the old bpython Constant object code in + Blender */ + +/*****************************************************************************/ +/* Python rgbTuple_Type callback function prototypes: */ +/*****************************************************************************/ +static void rgbTupleDeAlloc (C_rgbTuple *self); +static PyObject *rgbTupleGetAttr (C_rgbTuple *self, char *name); +static int rgbTupleSetAttr (C_rgbTuple *self, char *name, PyObject *v); +static int rgbTuplePrint(C_rgbTuple *self, FILE *fp, int flags); +static PyObject *rgbTupleRepr (C_rgbTuple *self); + +static int rgbTupleLength(C_rgbTuple *self); + +static PyObject *rgbTupleSubscript(C_rgbTuple *self, PyObject *key); +static int rgbTupleAssSubscript(C_rgbTuple *self, PyObject *who, + PyObject *cares); + +static PyObject *rgbTupleItem(C_rgbTuple *self, int i); +static int rgbTupleAssItem(C_rgbTuple *self, int i, PyObject *ob); +static PyObject *rgbTupleSlice(C_rgbTuple *self, int begin, int end); +static int rgbTupleAssSlice(C_rgbTuple *self, int begin, int end, PyObject *seq); + +/*****************************************************************************/ +/* Python rgbTuple_Type Mapping Methods table: */ +/*****************************************************************************/ +static PyMappingMethods rgbTupleAsMapping = +{ + (inquiry)rgbTupleLength, /* mp_length */ + (binaryfunc)rgbTupleSubscript, /* mp_subscript */ + (objobjargproc)rgbTupleAssSubscript, /* mp_ass_subscript */ +}; + +/*****************************************************************************/ +/* Python rgbTuple_Type Sequence Methods table: */ +/*****************************************************************************/ +static PySequenceMethods rgbTupleAsSequence = +{ + (inquiry) rgbTupleLength, /* sq_length */ + (binaryfunc) 0, /* sq_concat */ + (intargfunc) 0, /* sq_repeat */ + (intargfunc) rgbTupleItem, /* sq_item */ + (intintargfunc) rgbTupleSlice, /* sq_slice */ + (intobjargproc) rgbTupleAssItem, /* sq_ass_item */ + (intintobjargproc) rgbTupleAssSlice, /* sq_ass_slice */ +}; + +/*****************************************************************************/ +/* Python rgbTuple_Type structure definition: */ +/*****************************************************************************/ +PyTypeObject rgbTuple_Type = +{ + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "rgbTuple", /* tp_name */ + sizeof (C_rgbTuple), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)rgbTupleDeAlloc, /* tp_dealloc */ + (printfunc)rgbTuplePrint, /* tp_print */ + (getattrfunc)rgbTupleGetAttr, /* tp_getattr */ + (setattrfunc)rgbTupleSetAttr, /* tp_setattr */ + 0, /* tp_compare */ + (reprfunc)rgbTupleRepr, /* tp_repr */ + 0, /* tp_as_number */ + &rgbTupleAsSequence, /* tp_as_sequence */ + &rgbTupleAsMapping, /* tp_as_mapping */ + 0, /* tp_as_hash */ + 0,0,0,0,0,0, + 0, /* tp_doc */ + 0,0,0,0,0,0, + 0, /* tp_methods */ + 0, /* tp_members */ +}; + +/*****************************************************************************/ +/* Function: rgbTuple_New */ +/*****************************************************************************/ +PyObject *rgbTuple_New(float *rgb[3]) +{ /* this is the static one */ + C_rgbTuple *rgbTuple; + + printf ("In rgbTuple_New()\n"); + + rgbTuple = (C_rgbTuple *)PyObject_NEW(C_rgbTuple, &rgbTuple_Type); + + if (rgbTuple == NULL) + return EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create rgbTuple object"); + + rgbTuple->rgb[0] = rgb[0]; + rgbTuple->rgb[1] = rgb[1]; + rgbTuple->rgb[2] = rgb[2]; + + return (PyObject *)rgbTuple; +} + +/*****************************************************************************/ +/* Functions: rgbTuple_getCol and rgbTuple_setCol */ +/* Description: These functions get/set rgb color triplet values. The */ +/* get function returns a tuple, the set one accepts three */ +/* floats (separated or in a tuple) as arguments. */ +/*****************************************************************************/ +PyObject *rgbTuple_getCol (C_rgbTuple *self) +{ + PyObject *list = PyList_New (3); + + if (!list) return EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create PyList"); + + PyList_SET_ITEM (list, 0, Py_BuildValue ("f", *(self->rgb[0]) )); + PyList_SET_ITEM (list, 1, Py_BuildValue ("f", *(self->rgb[0]) )); + PyList_SET_ITEM (list, 2, Py_BuildValue ("f", *(self->rgb[0]) )); + + return list; +} + +PyObject *rgbTuple_setCol (C_rgbTuple *self, PyObject *args) +{ + int ok; + float r = 0, g = 0, b = 0; + + if (PyObject_Length (args) == 3) + ok = PyArg_ParseTuple (args, "fff", &r, &g, &b); + + else ok = PyArg_ParseTuple (args, "|(fff)", &r, &g, &b); + + if (!ok) + return EXPP_ReturnPyObjError (PyExc_TypeError, + "expected [f,f,f] or f,f,f as arguments (or nothing)"); + + *(self->rgb[0]) = EXPP_ClampFloat (r, 0.0, 1.0); + *(self->rgb[1]) = EXPP_ClampFloat (g, 0.0, 1.0); + *(self->rgb[2]) = EXPP_ClampFloat (b, 0.0, 1.0); + + return EXPP_incr_ret (Py_None); +} + +/*****************************************************************************/ +/* Function: rgbTupleDeAlloc */ +/* Description: This is a callback function for the C_rgbTuple type. It is */ +/* the destructor function. */ +/*****************************************************************************/ +static void rgbTupleDeAlloc (C_rgbTuple *self) +{ + PyObject_DEL (self); +} + +/*****************************************************************************/ +/* Function: rgbTupleGetAttr */ +/* Description: This is a callback function for the C_rgbTuple type. It is */ +/* the function that accesses C_rgbTuple member variables and */ +/* methods. */ +/*****************************************************************************/ +static PyObject* rgbTupleGetAttr (C_rgbTuple *self, char *name) +{ + int i; + + if (strcmp(name, "__members__") == 0) + return Py_BuildValue("[s,s,s]", "R", "G", "B"); + + else if (!strcmp(name, "R") || !strcmp(name, "r")) i = 0; + else if (!strcmp(name, "G") || !strcmp(name, "g")) i = 1; + else if (!strcmp(name, "B") || !strcmp(name, "b")) i = 2; + else + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "attribute not found")); + + return Py_BuildValue("f", *(self->rgb[i])); +} + +/*****************************************************************************/ +/* Function: rgbTupleSetAttr */ +/* Description: This is a callback function for the C_rgbTuple type. It is */ +/* the function that changes C_rgbTuple member variables. */ +/*****************************************************************************/ +static int rgbTupleSetAttr (C_rgbTuple *self, char *name, PyObject *v) +{ + float value; + + if (!PyArg_Parse (v, "f", &value)) + return EXPP_ReturnIntError (PyExc_TypeError, + "expected float argument"); + + value = EXPP_ClampFloat(value, 0.0, 1.0); + + if (!strcmp(name, "R") || !strcmp(name, "r")) + *(self->rgb[0]) = value; + + else if (!strcmp(name, "G") || !strcmp(name, "g")) + *(self->rgb[1]) = value; + + else if (!strcmp(name, "B") || !strcmp(name, "b")) + *(self->rgb[2]) = value; + + else return (EXPP_ReturnIntError (PyExc_AttributeError, + "attribute not found")); + + return 0; +} + +/*****************************************************************************/ +/* Section: rgbTuple as Mapping */ +/* These functions provide code to access rgbTuple objects as */ +/* mappings. */ +/*****************************************************************************/ +static int rgbTupleLength(C_rgbTuple *self) +{ + return 3; +} + +static PyObject *rgbTupleSubscript(C_rgbTuple *self, PyObject *key) +{ + char *name = NULL; + int i; + + if (PyNumber_Check(key)) return rgbTupleItem(self, (int)PyInt_AsLong(key)); + + if (!PyArg_ParseTuple(key, "s", &name)) + return EXPP_ReturnPyObjError (PyExc_TypeError, + "expected int or string argument"); + + if (!strcmp(name, "R") || !strcmp(name, "r")) i = 0; + else if (!strcmp(name, "G") || !strcmp(name, "g")) i = 1; + else if (!strcmp(name, "B") || !strcmp(name, "b")) i = 2; + else + return EXPP_ReturnPyObjError (PyExc_AttributeError, name); + + return Py_BuildValue("f", *(self->rgb[i])); +} + +static int rgbTupleAssSubscript(C_rgbTuple *self, PyObject *key, PyObject *v) +{ + char *name = NULL; + int i; + + if (!PyNumber_Check(v)) return EXPP_ReturnIntError(PyExc_TypeError, + "value to assign must be a number"); + + if (PyNumber_Check(key)) + return rgbTupleAssItem(self, (int)PyInt_AsLong(key), v); + + if (!PyArg_Parse(key, "s", &name)) + return EXPP_ReturnIntError (PyExc_TypeError, + "expected int or string argument"); + + if (!strcmp(name, "R") || !strcmp(name, "r")) i = 0; + else if (!strcmp(name, "G") || !strcmp(name, "g")) i = 1; + else if (!strcmp(name, "B") || !strcmp(name, "b")) i = 2; + else + return EXPP_ReturnIntError (PyExc_AttributeError, name); + + *(self->rgb[i]) = EXPP_ClampFloat(PyFloat_AsDouble(v), 0.0, 1.0); + + return 0; +} + +/*****************************************************************************/ +/* Section: rgbTuple as Sequence */ +/* These functions provide code to access rgbTuple objects as */ +/* sequences. */ +/*****************************************************************************/ +static PyObject *rgbTupleItem(C_rgbTuple *self, int i) +{ + if (i < 0 || i >= 3) + return EXPP_ReturnPyObjError (PyExc_IndexError, + "array index out of range"); + + return Py_BuildValue("f", *(self->rgb[i])); +} + +static PyObject *rgbTupleSlice(C_rgbTuple *self, int begin, int end) +{ + PyObject *list; + int count; + + if (begin < 0) begin = 0; + if (end > 3) end = 3; + if (begin > end) begin = end; + + list = PyList_New(end - begin); + + for (count = begin; count < end; count++) + PyList_SetItem(list, count - begin, + PyFloat_FromDouble(*(self->rgb[count]))); + + return list; +} + +static int rgbTupleAssItem(C_rgbTuple *self, int i, PyObject *ob) +{ + if (i < 0 || i >= 3) + return EXPP_ReturnIntError(PyExc_IndexError, + "array assignment index out of range"); + + if (!PyNumber_Check(ob)) + return EXPP_ReturnIntError(PyExc_IndexError, + "color component must be a number"); +/* XXX this check above is probably ... */ + *(self->rgb[i]) = EXPP_ClampFloat(PyFloat_AsDouble(ob), 0.0, 1.0); + + return 0; +} + +static int rgbTupleAssSlice(C_rgbTuple *self, int begin, int end, PyObject *seq) +{ + int count; + + 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"); + + if (PySequence_Length(seq) != (end - begin)) + return EXPP_ReturnIntError(PyExc_TypeError, + "size mismatch in slice assignment"); + + for (count = begin; count < end; count++) { + float value; + PyObject *ob = PySequence_GetItem(seq, count); + + if (!PyArg_Parse(ob, "f", &value)) { + Py_DECREF(ob); + return -1; + } + + *(self->rgb[count]) = EXPP_ClampFloat(value, 0.0, 1.0); + + Py_DECREF(ob); + } + + return 0; +} + +/*****************************************************************************/ +/* Function: rgbTuplePrint */ +/* Description: This is a callback function for the C_rgbTuple type. It */ +/* builds a meaninful string to 'print' rgbTuple objects. */ +/*****************************************************************************/ +static int rgbTuplePrint(C_rgbTuple *self, FILE *fp, int flags) +{ + fprintf(fp, "[%f, %f, %f]", + *(self->rgb[0]), *(self->rgb[1]), *(self->rgb[2])); + return 0; +} + +/*****************************************************************************/ +/* Function: rgbTupleRepr */ +/* Description: This is a callback function for the C_rgbTuple type. It */ +/* builds a meaninful string to represent rgbTuple objects. */ +/*****************************************************************************/ +static PyObject *rgbTupleRepr (C_rgbTuple *self) +{ + float r, g, b; + char buf[64]; + + r = *(self->rgb[0]); + g = *(self->rgb[1]); + b = *(self->rgb[2]); + + PyOS_snprintf(buf, sizeof(buf), "[%f, %f, %f]", r, g, b); + + return PyString_FromString(buf); +} diff --git a/source/blender/python/api2_2x/rgbTuple.h b/source/blender/python/api2_2x/rgbTuple.h new file mode 100644 index 00000000000..a2ca43d3ae8 --- /dev/null +++ b/source/blender/python/api2_2x/rgbTuple.h @@ -0,0 +1,59 @@ +/* + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * This is a new part of Blender. + * + * Contributor(s): Willian P. Germano + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#ifndef EXPP_rgbTuple_H +#define EXPP_rgbTuple_H + +#include <Python.h> +#include <stdio.h> + +#include "gen_utils.h" + +/* Objects of <type 'rgbTuple'> are used inside other Blender Python + * objects, so this header file must contain only 'public' declarations */ + +/*****************************************************************************/ +/* Python C_rgbTuple structure definition: */ +/*****************************************************************************/ +typedef struct { + PyObject_HEAD + float *rgb[3]; /* array of three pointers to floats */ + +} C_rgbTuple; + +/*****************************************************************************/ +/* Python API function prototypes for the rgbTuple helper module. */ +/*****************************************************************************/ +PyObject *rgbTuple_New (float *rgb[3]); +PyObject *rgbTuple_getCol (C_rgbTuple *self); +PyObject *rgbTuple_setCol (C_rgbTuple *self, PyObject *args); + +#endif /* EXPP_rgbTuple_H */ |