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/Camera.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/Camera.c')
-rw-r--r-- | source/blender/python/api2_2x/Camera.c | 771 |
1 files changed, 186 insertions, 585 deletions
diff --git a/source/blender/python/api2_2x/Camera.c b/source/blender/python/api2_2x/Camera.c index f3f7ba6d4a8..81646cae727 100644 --- a/source/blender/python/api2_2x/Camera.c +++ b/source/blender/python/api2_2x/Camera.c @@ -38,78 +38,50 @@ static PyObject *M_Camera_New(PyObject *self, PyObject *args, PyObject *keywords) { char *type_str = "persp"; /* "persp" is type 0, "ortho" is type 1 */ - char *name_str = "Data"; + char *name_str = "CamData"; static char *kwlist[] = {"type_str", "name_str", NULL}; - C_Camera *cam; - PyObject *type, *name; - int type_int; + C_Camera *pycam; /* for Camera Data object wrapper in Python */ + Camera *blcam; /* for actual Camera Data we create in Blender */ char buf[21]; printf ("In Camera_New()\n"); if (!PyArg_ParseTupleAndKeywords(args, keywords, "|ss", kwlist, &type_str, &name_str)) - { - /* We expected string(s) (or nothing) as argument, but we didn't get it. */ - return (PythonReturnErrorObject (PyExc_AttributeError, + /* We expected string(s) (or nothing) as argument, but we didn't get that. */ + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected zero, one or two strings as arguments")); - } - if (strcmp (type_str, "persp") == 0) - type_int = EXPP_CAM_TYPE_PERSP; + blcam = add_camera(); /* first create the Camera Data in Blender */ + + if (blcam) /* now create the wrapper obj in Python */ + pycam = (C_Camera *)PyObject_NEW(C_Camera, &Camera_Type); else - { - if (strcmp (type_str, "ortho") == 0) - { - type_int = EXPP_CAM_TYPE_ORTHO; - } - else - { - return (PythonReturnErrorObject (PyExc_AttributeError, - "unknown camera type")); - } - } + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't create Camera Data in Blender")); - cam = (C_Camera *)CameraCreatePyObject(NULL); + if (pycam == NULL) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create Camera Data object")); - if (cam == NULL) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create Camera Data object")); - } - cam->linked = 0; /* only Camera Data, not linked */ + pycam->camera = blcam; /* link Python camera wrapper to Blender Camera */ - type = PyInt_FromLong(type_int); - if (type) - { - CameraSetAttr(cam, "type", type); - } + if (strcmp (type_str, "persp") == 0) /* default, no need to set, so */ + /*blcam->type = (short)EXPP_CAM_TYPE_PERSP*/; /* we comment this line */ + else if (strcmp (type_str, "ortho") == 0) + blcam->type = (short)EXPP_CAM_TYPE_ORTHO; else - { - Py_DECREF((PyObject *)cam); - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyString")); - } - - if (strcmp(name_str, "Data") == 0) - { - return (PyObject *)cam; - } + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "unknown camera type")); - PyOS_snprintf(buf, sizeof(buf), "%s", name_str); - name = PyString_FromString(buf); - if (name) - { - CameraSetAttr(cam, "name", name); - } - else - { - Py_DECREF((PyObject *)cam); - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyString")); + if (strcmp(name_str, "CamData") == 0) + return (PyObject *)pycam; + else { /* user gave us a name for the camera, use it */ + PyOS_snprintf(buf, sizeof(buf), "%s", name_str); + rename_id(&blcam->id, buf); /* proper way in Blender */ } - return (PyObject *)cam; + return (PyObject *)pycam; } /*****************************************************************************/ @@ -124,35 +96,31 @@ static PyObject *M_Camera_Get(PyObject *self, PyObject *args) printf ("In Camera_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 camera requested. */ + /* Use the name to search for the camera requested */ wanted_cam = NULL; cam_iter = G.main->camera.first; - while ((cam_iter) && (wanted_cam == NULL)) - { - if (strcmp (name, GetIdName (&(cam_iter->id))) == 0) - { - wanted_cam = (C_Camera *)CameraCreatePyObject(cam_iter); - cam_iter = cam_iter->id.next; + while ((cam_iter) && (wanted_cam == NULL)) { + + if (strcmp (name, cam_iter->id.name+2) == 0) { + wanted_cam = (C_Camera *)PyObject_NEW(C_Camera, &Camera_Type); + if (wanted_cam) wanted_cam->camera = cam_iter; } + + cam_iter = cam_iter->id.next; } - if (wanted_cam == NULL) - { - /* No camera exists with the name specified in the argument name. */ + if (wanted_cam == NULL) { /* Requested camera doesn't exist */ char error_msg[64]; PyOS_snprintf(error_msg, sizeof(error_msg), "Camera \"%s\" not found", name); - return (PythonReturnErrorObject (PyExc_NameError, error_msg)); + return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg)); } - wanted_cam->linked = 1; /* TRUE: linked to a Blender Camera Object */ - return ((PyObject*)wanted_cam); + return (PyObject*)wanted_cam; } /*****************************************************************************/ @@ -160,13 +128,13 @@ static PyObject *M_Camera_Get(PyObject *self, PyObject *args) /*****************************************************************************/ PyObject *M_Camera_Init (void) { - PyObject *module; + PyObject *submodule; printf ("In M_Camera_Init()\n"); + submodule = Py_InitModule3("Blender.Camera", + M_Camera_methods, M_Camera_doc); - module = Py_InitModule3("Camera", M_Camera_methods, M_Camera_doc); - - return (module); + return (submodule); } /*****************************************************************************/ @@ -174,137 +142,86 @@ PyObject *M_Camera_Init (void) /*****************************************************************************/ static PyObject *Camera_getName(C_Camera *self) { - PyObject *attr; - attr = PyDict_GetItemString(self->dict, "name"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + PyObject *attr = PyString_FromString(self->camera->id.name+2); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.name attribute")); } static PyObject *Camera_getType(C_Camera *self) -{ - PyObject *attr; +{ + PyObject *attr = PyInt_FromLong(self->camera->type); - attr = PyDict_GetItemString(self->dict, "type"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.type attribute")); } static PyObject *Camera_getMode(C_Camera *self) { - PyObject *attr; + PyObject *attr = PyInt_FromLong(self->camera->flag); - attr = PyDict_GetItemString(self->dict, "mode"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.Mode attribute")); } static PyObject *Camera_getLens(C_Camera *self) { - PyObject *attr; + PyObject *attr = PyFloat_FromDouble(self->camera->lens); - attr = PyDict_GetItemString(self->dict, "lens"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.lens attribute")); } static PyObject *Camera_getClipStart(C_Camera *self) { - PyObject *attr; + PyObject *attr = PyFloat_FromDouble(self->camera->clipsta); - attr = PyDict_GetItemString(self->dict, "clipStart"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.clipStart attribute")); } static PyObject *Camera_getClipEnd(C_Camera *self) { - PyObject *attr; + PyObject *attr = PyFloat_FromDouble(self->camera->clipend); - attr = PyDict_GetItemString(self->dict, "clipEnd"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.clipEnd attribute")); } static PyObject *Camera_getDrawSize(C_Camera *self) { - PyObject *attr; + PyObject *attr = PyFloat_FromDouble(self->camera->drawsize); - attr = PyDict_GetItemString(self->dict, "drawSize"); - if (attr) - { - Py_INCREF(attr); - return attr; - } - return (PythonReturnErrorObject (PyExc_RuntimeError, + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, "couldn't get Camera.drawSize attribute")); } static PyObject *Camera_rename(C_Camera *self, PyObject *args) { - char *name_str; - char buf[21]; - PyObject *name; + char *name; + char buf[21]; - if (!PyArg_ParseTuple(args, "s", &name_str)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + if (!PyArg_ParseTuple(args, "s", &name)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected string argument")); - } - - PyOS_snprintf(buf, sizeof(buf), "%s", name_str); - - if (self->linked) - { - /* update the Blender Camera, too */ - ID *tmp_id = &self->camera->id; - rename_id(tmp_id, buf); - PyOS_snprintf(buf, sizeof(buf), "%s", tmp_id->name+2);/* may have changed */ - } - name = PyString_FromString(buf); + PyOS_snprintf(buf, sizeof(buf), "%s", name); - 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 Camera.name attribute")); - } + rename_id(&self->camera->id, buf); Py_INCREF(Py_None); return Py_None; @@ -312,45 +229,19 @@ static PyObject *Camera_rename(C_Camera *self, PyObject *args) static PyObject *Camera_setType(C_Camera *self, PyObject *args) { - short value; - char *type_str; - PyObject *type; + char *type; - if (!PyArg_ParseTuple(args, "s", &type_str)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + if (!PyArg_ParseTuple(args, "s", &type)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected string argument")); - } - if (strcmp (type_str, "persp") == 0) - value = EXPP_CAM_TYPE_PERSP; - else if (strcmp (type_str, "ortho") == 0) - value = EXPP_CAM_TYPE_ORTHO; + if (strcmp (type, "persp") == 0) + self->camera->type = (short)EXPP_CAM_TYPE_PERSP; + else if (strcmp (type, "ortho") == 0) + self->camera->type = (short)EXPP_CAM_TYPE_ORTHO; else - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "unknown camera type")); - } - - type = PyInt_FromLong(value); - if (!type) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyInt Object")); - } - - if (PyDict_SetItemString(self->dict, "type", type) != 0) - { - Py_DECREF(type); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Camera.type attribute")); - } - - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->type = value; - } Py_INCREF(Py_None); return Py_None; @@ -364,42 +255,16 @@ static PyObject *Camera_setType(C_Camera *self, PyObject *args) static PyObject *Camera_setIntType(C_Camera *self, PyObject *args) { short value; - PyObject *type; - if (!PyArg_ParseTuple(args, "i", &value)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + if (!PyArg_ParseTuple(args, "h", &value)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected int argument: 0 or 1")); - } if (value == 0 || value == 1) - { - type = PyInt_FromLong(value); - } + self->camera->type = value; else - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected int argument: 0 or 1")); - } - - if (!type) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyInt Object")); - } - - if (PyDict_SetItemString(self->dict, "type", type) != 0) - { - Py_DECREF(type); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "could not set Camera.type attribute")); - } - - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->type = value; - } Py_INCREF(Py_None); return Py_None; @@ -409,56 +274,32 @@ static PyObject *Camera_setMode(C_Camera *self, PyObject *args) { char *mode_str1 = NULL, *mode_str2 = NULL; short flag = 0; - PyObject *mode; if (!PyArg_ParseTuple(args, "|ss", &mode_str1, &mode_str2)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected one or two strings as arguments")); - } - if (mode_str1 != NULL) - { + if (mode_str1 != NULL) { if (strcmp(mode_str1, "showLimits") == 0) - flag |= EXPP_CAM_MODE_SHOWLIMITS; + flag |= (short)EXPP_CAM_MODE_SHOWLIMITS; else if (strcmp(mode_str1, "showMist") == 0) - flag |= EXPP_CAM_MODE_SHOWMIST; + flag |= (short)EXPP_CAM_MODE_SHOWMIST; else - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "first argument is an unknown camera flag")); - } - if (mode_str2 != NULL) - { + if (mode_str2 != NULL) { if (strcmp(mode_str2, "showLimits") == 0) - flag |= EXPP_CAM_MODE_SHOWLIMITS; + flag |= (short)EXPP_CAM_MODE_SHOWLIMITS; else if (strcmp(mode_str2, "showMist") == 0) - flag |= EXPP_CAM_MODE_SHOWMIST; + flag |= (short)EXPP_CAM_MODE_SHOWMIST; else - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "second argument is an unknown camera flag")); - } } } - - mode = PyInt_FromLong(flag); - if (!mode) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyInt Object")); - } - if (PyDict_SetItemString(self->dict, "mode", mode) != 0) - { - Py_DECREF(mode); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Camera.mode attribute")); - } - - if (self->linked) /* update the Blender Camera, too */ - self->camera->flag = flag; + self->camera->flag = flag; Py_INCREF(Py_None); return Py_None; @@ -469,42 +310,16 @@ static PyObject *Camera_setMode(C_Camera *self, PyObject *args) static PyObject *Camera_setIntMode(C_Camera *self, PyObject *args) { short value; - PyObject *mode; if (!PyArg_ParseTuple(args, "h", &value)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected int argument in [0,3]")); - } if (value >= 0 && value <= 3) - { - mode = PyInt_FromLong(value); - } + self->camera->flag = value; else - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected int argument in [0,3]")); - } - - if (!mode) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyInt object")); - } - - if (PyDict_SetItemString(self->dict, "mode", mode) != 0) - { - Py_DECREF(mode); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "could not set Camera.mode attribute")); - } - - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->flag = value; - } Py_INCREF(Py_None); return Py_None; @@ -513,35 +328,14 @@ static PyObject *Camera_setIntMode(C_Camera *self, PyObject *args) static PyObject *Camera_setLens(C_Camera *self, PyObject *args) { float value; - PyObject *lens; if (!PyArg_ParseTuple(args, "f", &value)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected float argument")); - } - value = EXPP_ClampFloat (value, EXPP_CAM_LENS_MIN, EXPP_CAM_LENS_MAX); - lens = PyFloat_FromDouble(value); - if (!lens) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyFloat Object")); - } - - if (PyDict_SetItemString(self->dict, "lens", lens) != 0) - { - Py_DECREF(lens); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Camera.lens attribute")); - } + self->camera->lens = EXPP_ClampFloat (value, + EXPP_CAM_LENS_MIN, EXPP_CAM_LENS_MAX); - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->lens = value; - } - Py_INCREF(Py_None); return Py_None; } @@ -549,37 +343,14 @@ static PyObject *Camera_setLens(C_Camera *self, PyObject *args) static PyObject *Camera_setClipStart(C_Camera *self, PyObject *args) { float value; - PyObject *clipStart; if (!PyArg_ParseTuple(args, "f", &value)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected a float number as argument")); - } - - value = EXPP_ClampFloat (value, EXPP_CAM_CLIPSTART_MIN, - EXPP_CAM_CLIPSTART_MAX); - - clipStart = PyFloat_FromDouble(value); - if (!clipStart) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyFloat Object")); - } + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected float argument")); - if (PyDict_SetItemString(self->dict, "clipStart", clipStart) != 0) - { - Py_DECREF(clipStart); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Camera.clipStart attribute")); - } + self->camera->clipsta = EXPP_ClampFloat (value, + EXPP_CAM_CLIPSTART_MIN, EXPP_CAM_CLIPSTART_MAX); - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->clipsta = value; - } - Py_INCREF(Py_None); return Py_None; } @@ -587,35 +358,13 @@ static PyObject *Camera_setClipStart(C_Camera *self, PyObject *args) static PyObject *Camera_setClipEnd(C_Camera *self, PyObject *args) { float value; - PyObject *clipEnd; if (!PyArg_ParseTuple(args, "f", &value)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, - "expected a float number as argument")); - } - - value = EXPP_ClampFloat (value, EXPP_CAM_CLIPEND_MIN, EXPP_CAM_CLIPEND_MAX); - - clipEnd = PyFloat_FromDouble(value); - if (!clipEnd) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyFloat Object")); - } + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected float argument")); - if (PyDict_SetItemString(self->dict, "clipEnd", clipEnd) != 0) - { - Py_DECREF(clipEnd); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Camera.clipEnd attribute")); - } - - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->clipend = value; - } + self->camera->clipend = EXPP_ClampFloat (value, + EXPP_CAM_CLIPEND_MIN, EXPP_CAM_CLIPEND_MAX); Py_INCREF(Py_None); return Py_None; @@ -624,233 +373,103 @@ static PyObject *Camera_setClipEnd(C_Camera *self, PyObject *args) static PyObject *Camera_setDrawSize(C_Camera *self, PyObject *args) { float value; - PyObject *drawSize; if (!PyArg_ParseTuple(args, "f", &value)) - { - return (PythonReturnErrorObject (PyExc_AttributeError, + return (EXPP_ReturnPyObjError (PyExc_AttributeError, "expected a float number as argument")); - } - - value = EXPP_ClampFloat (value, EXPP_CAM_DRAWSIZE_MIN, - EXPP_CAM_DRAWSIZE_MAX); - drawSize = PyFloat_FromDouble(value); - if (!drawSize) - { - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyFloat Object")); - } - - if (PyDict_SetItemString(self->dict, "drawSize", drawSize) != 0) - { - Py_DECREF(drawSize); - return (PythonReturnErrorObject (PyExc_RuntimeError, - "couldn't set Camera.drawSize attribute")); - } - - if (self->linked) - { - /* update the Blender Camera, too */ - self->camera->drawsize = value; - } + self->camera->drawsize = EXPP_ClampFloat (value, + EXPP_CAM_DRAWSIZE_MIN, EXPP_CAM_DRAWSIZE_MAX); Py_INCREF(Py_None); return Py_None; } /*****************************************************************************/ -/* Function: CameraCreatePyObject */ -/* Description: This function will create a new C_Camera. If the Camera */ -/* struct passed to it is not NULL, it'll use its attributes. */ -/*****************************************************************************/ -PyObject *CameraCreatePyObject (Camera *blenderCam) -{ - PyObject *name, *type, *mode; - PyObject *lens, *clipStart, *clipEnd, *drawSize; - PyObject *Types, *persp, *ortho; - PyObject *Modes, *showLimits, *showMist; - C_Camera *cam; - - printf ("In CameraCreatePyObject\n"); - - cam = (C_Camera *)PyObject_NEW(C_Camera, &Camera_Type); - - if (cam == NULL) - { - return NULL; - } - - cam->dict = PyDict_New(); - - if (cam->dict == NULL) - { - Py_DECREF((PyObject *)cam); - return NULL; - } - - if (blenderCam == NULL) - { - /* Not linked to a Camera Object yet */ - name = PyString_FromString("Data"); - type = PyInt_FromLong(EXPP_CAM_TYPE); - mode = PyInt_FromLong(EXPP_CAM_MODE); - lens = PyFloat_FromDouble(EXPP_CAM_LENS); - clipStart = PyFloat_FromDouble(EXPP_CAM_CLIPSTART); - clipEnd = PyFloat_FromDouble(EXPP_CAM_CLIPEND); - drawSize = PyFloat_FromDouble(EXPP_CAM_DRAWSIZE); - } - else - { - /* Camera Object available, get its attributes directly */ - name = PyString_FromString(blenderCam->id.name+2); - type = PyInt_FromLong(blenderCam->type); - mode = PyInt_FromLong(blenderCam->flag); - lens = PyFloat_FromDouble(blenderCam->lens); - clipStart = PyFloat_FromDouble(blenderCam->clipsta); - clipEnd = PyFloat_FromDouble(blenderCam->clipend); - drawSize = PyFloat_FromDouble(blenderCam->drawsize); - } - - Types = constant_New(); - persp = PyInt_FromLong(EXPP_CAM_TYPE_PERSP); - ortho = PyInt_FromLong(EXPP_CAM_TYPE_ORTHO); - - Modes = constant_New(); - showLimits = PyInt_FromLong(EXPP_CAM_MODE_SHOWLIMITS); - showMist = PyInt_FromLong(EXPP_CAM_MODE_SHOWMIST); - - if (name == NULL || type == NULL || mode == NULL|| lens == NULL || - clipStart == NULL || clipEnd == NULL || drawSize == NULL || - Types == NULL || persp == NULL || ortho == NULL || - Modes == NULL || showLimits == NULL || showMist == NULL) - { - /* Some object creation has gone wrong. Clean up. */ - goto fail; - } - - if ((PyDict_SetItemString(cam->dict, "name", name) != 0) || - (PyDict_SetItemString(cam->dict, "type", type) != 0) || - (PyDict_SetItemString(cam->dict, "mode", mode) != 0) || - (PyDict_SetItemString(cam->dict, "lens", lens) != 0) || - (PyDict_SetItemString(cam->dict, "clipStart", clipStart) != 0) || - (PyDict_SetItemString(cam->dict, "clipEnd", clipEnd) != 0) || - (PyDict_SetItemString(cam->dict, "drawSize", drawSize) != 0) || - (PyDict_SetItemString(cam->dict, "Types", Types) != 0) || - (PyDict_SetItemString(cam->dict, "Modes", Modes) != 0) || - (PyDict_SetItemString(cam->dict, "__members__", - PyDict_Keys(cam->dict)) != 0)) - { - /* One or more value setting actions has gone wwrong. Clean up. */ - goto fail; - } - - if ((PyDict_SetItemString(((C_constant *)Types)->dict, - "persp", persp) != 0) || - (PyDict_SetItemString(((C_constant *)Types)->dict, - "ortho", ortho) != 0) || - (PyDict_SetItemString(((C_constant *)Modes)->dict, - "showLimits", showLimits) != 0) || - (PyDict_SetItemString(((C_constant *)Modes)->dict, - "showMist", showMist) != 0)) - { - /* One or more value setting actions has gone wwrong. Clean up. */ - goto fail; - } - - cam->camera = blenderCam; /* it's NULL when creating only camera "data" */ - return ((PyObject*)cam); - -fail: - Py_XDECREF(name); - Py_XDECREF(type); - Py_XDECREF(mode); - Py_XDECREF(lens); - Py_XDECREF(clipStart); - Py_XDECREF(clipEnd); - Py_XDECREF(drawSize); - Py_XDECREF(Types); - Py_XDECREF(persp); - Py_XDECREF(ortho); - Py_XDECREF(Modes); - Py_XDECREF(showLimits); - Py_XDECREF(showMist); - Py_DECREF(cam->dict); - Py_DECREF((PyObject *)cam); - return NULL; -} - -/*****************************************************************************/ /* Function: CameraDeAlloc */ /* Description: This is a callback function for the C_Camera type. It is */ /* the destructor function. */ /*****************************************************************************/ static void CameraDeAlloc (C_Camera *self) { - Py_DECREF(self->dict); PyObject_DEL (self); } /*****************************************************************************/ /* Function: CameraGetAttr */ /* Description: This is a callback function for the C_Camera type. It is */ -/* the function that accesses C_Camera member variables and */ +/* the function that accesses C_Camera "member variables" and */ /* methods. */ /*****************************************************************************/ -static PyObject* CameraGetAttr (C_Camera *cam, char *name) +static PyObject *CameraGetAttr (C_Camera *self, char *name) { - /* first try the attributes dictionary */ - if (cam->dict) - { - PyObject *v = PyDict_GetItemString(cam->dict, name); - if (v) - { - Py_INCREF(v); /* was a borrowed ref */ - return v; - } + PyObject *attr = Py_None; + + if (strcmp(name, "name") == 0) + attr = PyString_FromString(self->camera->id.name+2); + else if (strcmp(name, "type") == 0) + attr = PyInt_FromLong(self->camera->type); + else if (strcmp(name, "mode") == 0) + attr = PyInt_FromLong(self->camera->flag); + else if (strcmp(name, "lens") == 0) + attr = PyFloat_FromDouble(self->camera->lens); + else if (strcmp(name, "clipStart") == 0) + attr = PyFloat_FromDouble(self->camera->clipsta); + else if (strcmp(name, "clipEnd") == 0) + attr = PyFloat_FromDouble(self->camera->clipend); + else if (strcmp(name, "drawSize") == 0) + attr = PyFloat_FromDouble(self->camera->drawsize); + + else if (strcmp(name, "Types") == 0) { + attr = Py_BuildValue("{s:h,s:h}", "persp", EXPP_CAM_TYPE_PERSP, + "ortho", EXPP_CAM_TYPE_ORTHO); + } + + else if (strcmp(name, "Modes") == 0) { + attr = Py_BuildValue("{s:h,s:h}", "showLimits", EXPP_CAM_MODE_SHOWLIMITS, + "showMist", EXPP_CAM_MODE_SHOWMIST); + } + + else if (strcmp(name, "__members__") == 0) { + attr = Py_BuildValue("[s,s,s,s,s,s,s,s,s]", + "name", "type", "mode", "lens", "clipStart", + "clipEnd", "drawSize", "Types", "Modes"); } + if (!attr) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create PyObject")); + + if (attr != Py_None) return attr; /* member attribute found, return it */ + /* not an attribute, search the methods table */ - return Py_FindMethod(C_Camera_methods, (PyObject *)cam, name); + return Py_FindMethod(C_Camera_methods, (PyObject *)self, name); } /*****************************************************************************/ /* Function: CameraSetAttr */ /* Description: This is a callback function for the C_Camera type. It is the */ -/* function that changes Camera Data members values. If this */ -/* data is linked to a Blender Camera, it also gets updated. */ +/* function that sets Camera Data attributes (member variables).*/ /*****************************************************************************/ static int CameraSetAttr (C_Camera *self, char *name, PyObject *value) { PyObject *valtuple; PyObject *error = NULL; - if (self->dict == NULL) - { - return -1; - } - /* We're playing a trick on the Python API users here. Even if they use - * Camera.member = val instead of Camera.setMember(value), we end up using the + * Camera.member = val instead of Camera.setMember(val), we end up using the * function anyway, since it already has error checking, clamps to the right * interval and updates the Blender Camera structure when necessary. */ - valtuple = PyTuple_New(1); /* the set* functions expect a tuple */ +/* First we put "value" in a tuple, because we want to pass it to functions + * that only accept PyTuples. Using "N" doesn't increment value's ref count */ + valtuple = Py_BuildValue("(N)", value); - if (!valtuple) - { - return EXPP_intError(PyExc_MemoryError, + if (!valtuple) /* everything OK with our PyObject? */ + return EXPP_ReturnIntError(PyExc_MemoryError, "CameraSetAttr: 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, - "CameraSetAttr: couldn't fill tuple"); - } +/* Now we just compare "name" with all possible C_Camera member variables */ if (strcmp (name, "name") == 0) error = Camera_rename (self, valtuple); else if (strcmp (name, "type") == 0) @@ -865,24 +484,30 @@ static int CameraSetAttr (C_Camera *self, char *name, PyObject *value) error = Camera_setClipEnd (self, valtuple); else if (strcmp (name, "drawSize") == 0) error = Camera_setDrawSize (self, valtuple); - else - { - /* Error: no such member in the Camera Data structure */ - Py_DECREF(value); + + else { /* Error */ Py_DECREF(valtuple); - return (EXPP_intError (PyExc_KeyError, - "attribute not found")); - } - if (error == Py_None) - { - return 0; /* normal exit */ + if ((strcmp (name, "Types") == 0) || /* user tried to change a */ + (strcmp (name, "Modes") == 0)) /* constant dict type ... */ + return (EXPP_ReturnIntError (PyExc_AttributeError, + "constant dictionary -- cannot be changed")); + + else /* ... or no member with the given name was found */ + return (EXPP_ReturnIntError (PyExc_KeyError, + "attribute not found")); } - Py_DECREF(value); +/* valtuple won't be returned to the caller, so we need to DECREF it */ Py_DECREF(valtuple); - return -1; + if (error != Py_None) return -1; + +/* Py_None was incref'ed by the called Camera_set* function. We probably + * don't need to decref Py_None (!), but since Python/C API manual tells us + * to treat it like any other PyObject regarding ref counting ... */ + Py_DECREF(Py_None); + return 0; /* normal exit */ } /*****************************************************************************/ @@ -892,18 +517,7 @@ static int CameraSetAttr (C_Camera *self, char *name, PyObject *value) /*****************************************************************************/ static int CameraPrint(C_Camera *self, FILE *fp, int flags) { - char *lstate = "unlinked"; - char *name; - - if (self->linked) - { - lstate = "linked"; - } - - name = PyString_AsString(Camera_getName(self)); - - fprintf(fp, "[Camera \"%s\" (%s)]", name, lstate); - + fprintf(fp, "[Camera \"%s\"]", self->camera->id.name+2); return 0; } @@ -914,18 +528,5 @@ static int CameraPrint(C_Camera *self, FILE *fp, int flags) /*****************************************************************************/ static PyObject *CameraRepr (C_Camera *self) { - char buf[64]; - char *lstate = "unlinked"; - char *name; - - if (self->linked) - { - lstate = "linked"; - } - - name = PyString_AsString(Camera_getName(self)); - - PyOS_snprintf(buf, sizeof(buf), "[Camera \"%s\" (%s)]", name, lstate); - - return PyString_FromString(buf); + return PyString_FromString(self->camera->id.name+2); } |