diff options
author | Willian Padovani Germano <wpgermano@gmail.com> | 2003-05-08 07:06:46 +0400 |
---|---|---|
committer | Willian Padovani Germano <wpgermano@gmail.com> | 2003-05-08 07:06:46 +0400 |
commit | 1e891f844aa02a59269d74d0dc0283e78a7ebda5 (patch) | |
tree | e1fa359b455a5146aff95ffc5423a2b370dd8fdf /source/blender/python/api2_2x/Image.c | |
parent | f20e95b73a3f06d7b59c67e33c2061cbc8a8594f (diff) |
* Added submodule Window, including FileSelector and ImageSelector:
Most of the code comes from bpython/intern/opy_window.c, but two
new functions were added, to access the file and image selector
windows in Blender.
* Added submodules Draw (gui) and BGL (OpenGL wrapper):
The code comes from bpython/intern/opy_draw.c, with minor changes
to integrate it in the new implementation.
* Made changes to Camera, Lamp and Image submodules:
The implementation was improved. These files should be good
starting points for interested new coders to look at, now.
* Renamed interface.[ch] to EXPP_interface.[ch] to avoid conflict:
There is another interface.h file in source/blender/include.
Diffstat (limited to 'source/blender/python/api2_2x/Image.c')
-rw-r--r-- | source/blender/python/api2_2x/Image.c | 380 |
1 files changed, 132 insertions, 248 deletions
diff --git a/source/blender/python/api2_2x/Image.c b/source/blender/python/api2_2x/Image.c index 4d8ae7480c2..03e9557c6e9 100644 --- a/source/blender/python/api2_2x/Image.c +++ b/source/blender/python/api2_2x/Image.c @@ -39,8 +39,8 @@ static PyObject *M_Image_New(PyObject *self, PyObject *args, PyObject *keywords) { printf ("In Image_New() - unimplemented in 2.25\n"); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } /*****************************************************************************/ @@ -51,14 +51,12 @@ static PyObject *M_Image_Get(PyObject *self, PyObject *args) { char *name; Image *img_iter; - C_Image *wanted_img; + C_Image *wanted_img; printf ("In Image_Get()\n"); if (!PyArg_ParseTuple(args, "s", &name)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected string argument")); - } /* Use the name to search for the image requested. */ wanted_img = NULL; @@ -66,10 +64,12 @@ static PyObject *M_Image_Get(PyObject *self, PyObject *args) while ((img_iter) && (wanted_img == NULL)) { - if (strcmp (name, GetIdName (&(img_iter->id))) == 0) - wanted_img = (C_Image *)ImageCreatePyObject(img_iter); + if (strcmp (name, img_iter->id.name+2) == 0) { + wanted_img = (C_Image *)PyObject_NEW(C_Image, &Image_Type); + if (wanted_img) wanted_img->image = img_iter; + } - img_iter = img_iter->id.next; + img_iter = img_iter->id.next; } if (wanted_img == NULL) { @@ -77,34 +77,38 @@ static PyObject *M_Image_Get(PyObject *self, PyObject *args) char error_msg[64]; PyOS_snprintf(error_msg, sizeof(error_msg), "Image \"%s\" not found", name); - return (PythonReturnErrorObject (PyExc_NameError, error_msg)); + return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg)); } - return ((PyObject*)wanted_img); + return (PyObject*)wanted_img; } static PyObject *M_Image_Load(PyObject *self, PyObject *args) { - char *fname; - Image *img_ptr; - C_Image *img; + char *fname; + Image *img_ptr; + C_Image *img; printf ("In Image_Load()\n"); - + if (!PyArg_ParseTuple(args, "s", &fname)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected string argument")); - } - - img_ptr = add_image(fname); - if (!img_ptr) - return (PythonReturnErrorObject (PyExc_IOError, - "couldn't load image")); + + img = (C_Image *)PyObject_NEW(C_Image, &Image_Type); + + if (!img) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create PyObject Image_Type")); - img = (C_Image *)ImageCreatePyObject(img_ptr); + img_ptr = add_image(fname); + if (!img_ptr) + return (EXPP_ReturnPyObjError (PyExc_IOError, + "couldn't load image")); - return (PyObject *)img; + img->image = img_ptr; + + return (PyObject *)img; } /*****************************************************************************/ @@ -126,186 +130,75 @@ PyObject *M_Image_Init (void) /*****************************************************************************/ static PyObject *Image_getName(C_Image *self) { - PyObject *attr; - attr = PyDict_GetItemString(self->dict, "name"); - if (attr) { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't get Image.name attribute")); + PyObject *attr = PyString_FromString(self->image->id.name+2); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Image.name attribute")); } static PyObject *Image_getFilename(C_Image *self) { - PyObject *attr; - attr = PyDict_GetItemString(self->dict, "filename"); - if (attr) { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't get Image.filename attribute")); + PyObject *attr = PyString_FromString(self->image->name); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Image.filename attribute")); } static PyObject *Image_rename(C_Image *self, PyObject *args) { - char *name_str; - char buf[21]; - ID *tmp_id; - PyObject *name; - - if (!PyArg_ParseTuple(args, "s", &name_str)) - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected string argument")); - - PyOS_snprintf(buf, sizeof(buf), "%s", name_str); - - /* update the Blender Image, too */ - tmp_id = &self->image->id; - rename_id(tmp_id, buf); - PyOS_snprintf(buf, sizeof(buf), "%s", tmp_id->name+2);/* may have changed */ - - name = PyString_FromString(buf); - - if (!name) - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyString Object")); - - if (PyDict_SetItemString(self->dict, "name", name) != 0) { - Py_DECREF(name); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Image.name attribute")); - } - - Py_INCREF(Py_None); - return Py_None; + char *name; + char buf[21]; + + if (!PyArg_ParseTuple(args, "s", &name)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected string argument")); + + PyOS_snprintf(buf, sizeof(buf), "%s", name); + + rename_id(&self->image->id, buf); + + Py_INCREF(Py_None); + return Py_None; } static PyObject *Image_setXRep(C_Image *self, PyObject *args) { - short value; - PyObject *rep; - - if (!PyArg_ParseTuple(args, "h", &value)) - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected int argument in [1,16]")); - - if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX) - rep = PyInt_FromLong(value); - else - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected int argument in [1,16]")); - - if (!rep) - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyInt Object")); - - if (PyDict_SetItemString(self->dict, "xrep", rep) != 0) { - Py_DECREF(rep); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "could not set Image.xrep attribute")); - } - - /* update the Blender Image, too */ - self->image->xrep = value; - - Py_INCREF(Py_None); - return Py_None; + short value; + + if (!PyArg_ParseTuple(args, "h", &value)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument in [1,16]")); + + if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX) + self->image->xrep = value; + else + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument in [1,16]")); + + Py_INCREF(Py_None); + return Py_None; } static PyObject *Image_setYRep(C_Image *self, PyObject *args) { - short value; - PyObject *rep; - - if (!PyArg_ParseTuple(args, "h", &value)) - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected int argument in [1,16]")); - - if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX) - rep = PyInt_FromLong(value); - else - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected int argument in [1,16]")); - - if (!rep) - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyInt Object")); - - if (PyDict_SetItemString(self->dict, "yrep", rep) != 0) { - Py_DECREF(rep); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "could not set Image.yrep attribute")); - } - - /* update the Blender Image, too */ - self->image->yrep = value; - - Py_INCREF(Py_None); - return Py_None; -} + short value; -/*****************************************************************************/ -/* Function: ImageCreatePyObject */ -/* Description: This function will create a new C_Image. If the Image */ -/* struct passed to it is not NULL, it'll use its attributes. */ -/*****************************************************************************/ -PyObject *ImageCreatePyObject (Image *blenderImage) -{ - PyObject *name, *filename, *xrep, *yrep; - C_Image *img; - - printf ("In ImageCreatePyObject\n"); - - img = (C_Image *)PyObject_NEW(C_Image, &Image_Type); - - if (img == NULL) - return NULL; - - img->dict = PyDict_New(); - - if (img->dict == NULL) { - Py_DECREF((PyObject *)img); - return NULL; - } - /*branch currently unused*/ - if (blenderImage == NULL) { /* Not linked to an Image Object yet */ - name = PyString_FromString("DATA"); - filename = PyString_FromString(""); - xrep = PyInt_FromLong(EXPP_IMAGE_REP); /* rep default is 1, of course */ - yrep = PyInt_FromLong(EXPP_IMAGE_REP); - } - else { /* Image Object available, get its attributes directly */ - name = PyString_FromString(blenderImage->id.name+2); - filename = PyString_FromString(blenderImage->name); - xrep = PyInt_FromLong(blenderImage->xrep); - yrep = PyInt_FromLong(blenderImage->yrep); - } - - if (name == NULL || filename == NULL || - xrep == NULL || yrep == NULL) - goto fail; - - if ((PyDict_SetItemString(img->dict, "name", name) != 0) || - (PyDict_SetItemString(img->dict, "filename", filename) != 0) || - (PyDict_SetItemString(img->dict, "xrep", xrep) != 0) || - (PyDict_SetItemString(img->dict, "yrep", yrep) != 0) || - (PyDict_SetItemString(img->dict, "__members__", - PyDict_Keys(img->dict)) != 0)) - goto fail; - - img->image = blenderImage; /* it's NULL when creating only image "data" */ - return ((PyObject*)img); - -fail: - Py_XDECREF(name); - Py_XDECREF(filename); - Py_XDECREF(xrep); - Py_XDECREF(yrep); - Py_DECREF(img->dict); - Py_DECREF((PyObject *)img); - return NULL; + if (!PyArg_ParseTuple(args, "h", &value)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument in [1,16]")); + + if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX) + self->image->yrep = value; + else + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument in [1,16]")); + + Py_INCREF(Py_None); + return Py_None; } /*****************************************************************************/ @@ -315,7 +208,6 @@ fail: /*****************************************************************************/ static void ImageDeAlloc (C_Image *self) { - Py_DECREF(self->dict); PyObject_DEL (self); } @@ -325,18 +217,31 @@ static void ImageDeAlloc (C_Image *self) /* the function that accesses C_Image member variables and */ /* methods. */ /*****************************************************************************/ -static PyObject* ImageGetAttr (C_Image *img, char *name) -{/* first try the attributes dictionary */ - if (img->dict) { - PyObject *v = PyDict_GetItemString(img->dict, name); - if (v) { - Py_INCREF(v); /* was a borrowed ref */ - return v; - } - } - -/* not an attribute, search the methods table */ - return Py_FindMethod(C_Image_methods, (PyObject *)img, name); +static PyObject* ImageGetAttr (C_Image *self, char *name) +{ + PyObject *attr = Py_None; + + if (strcmp(name, "name") == 0) + attr = PyString_FromString(self->image->id.name+2); + else if (strcmp(name, "filename") == 0) + attr = PyString_FromString(self->image->name); + else if (strcmp(name, "xrep") == 0) + attr = PyInt_FromLong(self->image->xrep); + else if (strcmp(name, "yrep") == 0) + attr = PyInt_FromLong(self->image->yrep); + + else if (strcmp(name, "__members__") == 0) + attr = Py_BuildValue("[s,s,s,s]", + "name", "filename", "xrep", "yrep"); + + if (!attr) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create PyObject")); + + if (attr != Py_None) return attr; /* attribute found, return its value */ + + /* not an attribute, search the methods table */ + return Py_FindMethod(C_Image_methods, (PyObject *)self, name); } /*****************************************************************************/ @@ -347,48 +252,39 @@ static PyObject* ImageGetAttr (C_Image *img, char *name) /*****************************************************************************/ static int ImageSetAttr (C_Image *self, char *name, PyObject *value) { - PyObject *valtuple; - PyObject *error = NULL; - - if (self->dict == NULL) return -1; + PyObject *valtuple; + PyObject *error = NULL; /* We're playing a trick on the Python API users here. Even if they use * Image.member = val instead of Image.setMember(value), we end up using the * function anyway, since it already has error checking, clamps to the right * interval and updates the Blender Image structure when necessary. */ - valtuple = PyTuple_New(1); /* the set* functions expect a tuple */ + valtuple = Py_BuildValue("(N)", value); /* the set* functions expect a tuple */ - if (!valtuple) - return EXPP_intError(PyExc_MemoryError, - "ImageSetAttr: couldn't create PyTuple"); + if (!valtuple) + return EXPP_ReturnIntError(PyExc_MemoryError, + "ImageSetAttr: couldn't create PyTuple"); - if (PyTuple_SetItem(valtuple, 0, value) != 0) { - Py_DECREF(value); /* PyTuple_SetItem incref's value even when it fails */ - Py_DECREF(valtuple); - return EXPP_intError(PyExc_RuntimeError, - "ImageSetAttr: couldn't fill tuple"); - } - - if (strcmp (name, "name") == 0) + if (strcmp (name, "name") == 0) error = Image_rename (self, valtuple); - else if (strcmp (name, "xrep") == 0) - error = Image_setXRep (self, valtuple); - else if (strcmp (name, "yrep") == 0) - error = Image_setYRep (self, valtuple); - else { /* Error: no such member in the Image Data structure */ - Py_DECREF(value); - Py_DECREF(valtuple); - return (EXPP_intError (PyExc_KeyError, - "attribute not found or immutable")); - } - - if (error == Py_None) return 0; /* normal exit */ - - Py_DECREF(value); - Py_DECREF(valtuple); - - return -1; + else if (strcmp (name, "xrep") == 0) + error = Image_setXRep (self, valtuple); + else if (strcmp (name, "yrep") == 0) + error = Image_setYRep (self, valtuple); + else { /* Error: no such member in the Image Data structure */ + Py_DECREF(value); + Py_DECREF(valtuple); + return (EXPP_ReturnIntError (PyExc_KeyError, + "attribute not found or immutable")); + } + + Py_DECREF(valtuple); + + if (error != Py_None) return -1; + + Py_DECREF(Py_None); /* incref'ed by the called set* function */ + return 0; /* normal exit */ } /*****************************************************************************/ @@ -398,12 +294,7 @@ static int ImageSetAttr (C_Image *self, char *name, PyObject *value) /*****************************************************************************/ static int ImagePrint(C_Image *self, FILE *fp, int flags) { - char *name; - - name = PyString_AsString(Image_getName(self)); - - fprintf(fp, "[Image \"%s\"]", name); - + fprintf(fp, "[Image \"%s\"]", self->image->id.name+2); return 0; } @@ -414,12 +305,5 @@ static int ImagePrint(C_Image *self, FILE *fp, int flags) /*****************************************************************************/ static PyObject *ImageRepr (C_Image *self) { - char buf[64]; - char *name; - - name = PyString_AsString(Image_getName(self)); - - PyOS_snprintf(buf, sizeof(buf), "[Image \"%s\"]", name); - - return PyString_FromString(buf); + return PyString_FromString(self->image->id.name+2); } |