diff options
Diffstat (limited to 'source/blender/python/api2_2x/Camera.c')
-rw-r--r-- | source/blender/python/api2_2x/Camera.c | 1050 |
1 files changed, 846 insertions, 204 deletions
diff --git a/source/blender/python/api2_2x/Camera.c b/source/blender/python/api2_2x/Camera.c index 20d74ea3c18..f3f7ba6d4a8 100644 --- a/source/blender/python/api2_2x/Camera.c +++ b/source/blender/python/api2_2x/Camera.c @@ -1,289 +1,931 @@ -/* TODO: 'print dir(CameraObject)' doesn't work, attribs are not recognized; - * how to assign methods to objects? PyMethod_New() ? Maybe separate - * CameraModule and Camera; found: check Extending ... section 2.2.3 - * correctly handle references; - * Make all functions but module init's static ? - * 'print Blender.Camera.__doc__' doesn't work (still gives 'None')*/ +/* + * + * ***** 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 "Camera.h" -#include <Python.h> -#include <stdio.h> +/*****************************************************************************/ +/* Function: M_Camera_New */ +/* Python equivalent: Blender.Camera.New */ +/*****************************************************************************/ +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"; + static char *kwlist[] = {"type_str", "name_str", NULL}; + C_Camera *cam; + PyObject *type, *name; + int type_int; + char buf[21]; -#include <BKE_main.h> -#include <BKE_global.h> -#include <BKE_object.h> -#include <DNA_camera_types.h> + printf ("In Camera_New()\n"); -#include "gen_utils.h" -#include "modules.h" + 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, + "expected zero, one or two strings as arguments")); + } -/*****************************************************************************/ -/* Python API function prototypes for the Camera module. */ -/******************************************************************************/ -PyObject *Camera_New (PyObject *self, PyObject *args, PyObject *keywords); -PyObject *Camera_Get (PyObject *self, PyObject *args); + if (strcmp (type_str, "persp") == 0) + type_int = EXPP_CAM_TYPE_PERSP; + else + { + if (strcmp (type_str, "ortho") == 0) + { + type_int = EXPP_CAM_TYPE_ORTHO; + } + else + { + return (PythonReturnErrorObject (PyExc_AttributeError, + "unknown camera type")); + } + } -/*****************************************************************************/ -/* The following string definitions are used for documentation strings. */ -/* In Python these will be written to the console when doing a */ -/* Blender.Camera.__doc__ */ -/*****************************************************************************/ -char CameraModule_doc[] = -"The Blender Camera module\n\n\ -This module provides access to **Camera** objects in Blender\n\n\ -Example::\n\n\ - from Blender import Camera, Object, Scene\n\ - c = Camera.New('ortho') # create new ortho camera data\n\ - c.lens = 35.0 # set lens value\n\ - cur = Scene.getCurrent() # get current Scene\n\ - ob = Object.New('Camera') # make camera object\n\ - ob.link(c) # link camera data with this object\n\ - cur.link(ob) # link object into scene\n\ - cur.setCurrentCamera(ob) # make this camera the active\n"; - -char Camera_New_doc[] = -"(type) - returns a new Camera object of type 'type', \ -which can be 'persp' or 'ortho'.\n\ -() - returns a new Camera object of type 'persp'."; - -char Camera_Get_doc[] = -"(name) - return the camera with the name 'name', \ -returns None if not found.\n If 'name' is not specified, \ -it returns a list of all cameras in the\ncurrent scene."; + cam = (C_Camera *)CameraCreatePyObject(NULL); -/*****************************************************************************/ -/* Python BlenderCamera structure definition. */ -/*****************************************************************************/ -typedef struct { - PyObject_HEAD - struct Camera *camera; -} BlenCamera; + if (cam == NULL) + { + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create Camera Data object")); + } + cam->linked = 0; /* only Camera Data, not linked */ -/*****************************************************************************/ -/* PythonTypeCamera callback function prototypes */ -/*****************************************************************************/ -void CameraDeAlloc (BlenCamera *cam); -PyObject* CameraGetAttr (BlenCamera *cam, char *name); -int CameraSetAttr (BlenCamera *cam, char *name, PyObject *v); -PyObject* CameraRepr (BlenCamera *cam); + type = PyInt_FromLong(type_int); + if (type) + { + CameraSetAttr(cam, "type", type); + } + else + { + Py_DECREF((PyObject *)cam); + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyString")); + } + + if (strcmp(name_str, "Data") == 0) + { + return (PyObject *)cam; + } + + 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")); + } + + return (PyObject *)cam; +} /*****************************************************************************/ -/* Python TypeCamera structure definition. */ +/* Function: M_Camera_Get */ +/* Python equivalent: Blender.Camera.Get */ /*****************************************************************************/ -static PyTypeObject camera_type = +static PyObject *M_Camera_Get(PyObject *self, PyObject *args) { - PyObject_HEAD_INIT(&PyType_Type) - 0, /* ob_size */ - "Camera", /* tp_name */ - sizeof (BlenCamera), /* tp_basicsize */ - 0, /* tp_itemsize */ - /* methods */ - (destructor)CameraDeAlloc, /* tp_dealloc */ - 0, /* tp_print */ - (getattrfunc)CameraGetAttr, /* tp_getattr */ - (setattrfunc)CameraSetAttr, /* tp_setattr */ - 0, /* tp_compare */ - (reprfunc)CameraRepr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_as_hash */ - 0, - 0, - 0, - 0, - 0, - 0, - CameraModule_doc, /* tp_doc Isn't working for some reason */ - 0, -}; + char *name; + Camera *cam_iter; + C_Camera *wanted_cam; + + printf ("In Camera_Get()\n"); + if (!PyArg_ParseTuple(args, "s", &name)) + { + return (PythonReturnErrorObject (PyExc_AttributeError, + "expected string argument")); + } + + /* 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; + } + } + + if (wanted_cam == NULL) + { + /* No camera exists with the name specified in the argument name. */ + char error_msg[64]; + PyOS_snprintf(error_msg, sizeof(error_msg), + "Camera \"%s\" not found", name); + return (PythonReturnErrorObject (PyExc_NameError, error_msg)); + } + + wanted_cam->linked = 1; /* TRUE: linked to a Blender Camera Object */ + return ((PyObject*)wanted_cam); +} /*****************************************************************************/ -/* Python method structure definition. */ +/* Function: M_Camera_Init */ /*****************************************************************************/ -struct PyMethodDef Camera_methods[] = { - {"New",(PyCFunction)Camera_New, METH_VARARGS|METH_KEYWORDS, Camera_New_doc}, - {"Get", Camera_Get, METH_VARARGS, Camera_Get_doc}, - {"get", Camera_Get, METH_VARARGS, Camera_Get_doc}, - {NULL, NULL, 0, NULL} -}; +PyObject *M_Camera_Init (void) +{ + PyObject *module; + + printf ("In M_Camera_Init()\n"); + + module = Py_InitModule3("Camera", M_Camera_methods, M_Camera_doc); + + return (module); +} /*****************************************************************************/ -/* Function: Camera_New */ -/* Python equivalent: Blender.Camera.New */ +/* Python C_Camera methods: */ /*****************************************************************************/ -PyObject *Camera_New(PyObject *self, PyObject *args, PyObject *keywords) +static PyObject *Camera_getName(C_Camera *self) { - char *type_str = "persp"; /* "persp" is type 0, "ortho" is type 1 */ - static char *kwlist[] = {"type_str", NULL}; - Camera * cam; - BlenCamera * blen_camera; + PyObject *attr; + attr = PyDict_GetItemString(self->dict, "name"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't get Camera.name attribute")); +} - printf ("In Camera_New()\n"); +static PyObject *Camera_getType(C_Camera *self) +{ + PyObject *attr; + + attr = PyDict_GetItemString(self->dict, "type"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't get Camera.type attribute")); +} - if (!PyArg_ParseTupleAndKeywords(args, keywords, "|s", kwlist, &type_str)) - /* We expected a string (or nothing) as an argument, but we didn't get one. */ +static PyObject *Camera_getMode(C_Camera *self) +{ + PyObject *attr; + + attr = PyDict_GetItemString(self->dict, "mode"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't get Camera.Mode attribute")); +} + +static PyObject *Camera_getLens(C_Camera *self) +{ + PyObject *attr; + + attr = PyDict_GetItemString(self->dict, "lens"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't get Camera.lens attribute")); +} + +static PyObject *Camera_getClipStart(C_Camera *self) +{ + PyObject *attr; + + attr = PyDict_GetItemString(self->dict, "clipStart"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't get Camera.clipStart attribute")); +} + +static PyObject *Camera_getClipEnd(C_Camera *self) +{ + PyObject *attr; + + attr = PyDict_GetItemString(self->dict, "clipEnd"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't get Camera.clipEnd attribute")); +} + +static PyObject *Camera_getDrawSize(C_Camera *self) +{ + PyObject *attr; + + attr = PyDict_GetItemString(self->dict, "drawSize"); + if (attr) + { + Py_INCREF(attr); + return attr; + } + return (PythonReturnErrorObject (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; + + if (!PyArg_ParseTuple(args, "s", &name_str)) + { return (PythonReturnErrorObject (PyExc_AttributeError, - "expected string (or empty) argument")); + "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); + + if (!name) + { + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyString Object")); + } - cam = add_camera(); - cam->id.us = 0; /* new camera: no user yet */ + if (PyDict_SetItemString(self->dict, "name", name) != 0) + { + Py_DECREF(name); + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't set Camera.name attribute")); + } - if (StringEqual (type_str, "persp")) - cam->type = 0; - else if (StringEqual (type_str, "ortho")) - cam->type = 1; + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *Camera_setType(C_Camera *self, PyObject *args) +{ + short value; + char *type_str; + PyObject *type; + + if (!PyArg_ParseTuple(args, "s", &type_str)) + { + return (PythonReturnErrorObject (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; else + { return (PythonReturnErrorObject (PyExc_AttributeError, - "unknown camera type")); + "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")); + } - blen_camera = (BlenCamera*)PyObject_NEW(BlenCamera, &camera_type); - blen_camera->camera = cam; + if (self->linked) + { + /* update the Blender Camera, too */ + self->camera->type = value; + } - return ((PyObject*)blen_camera); + Py_INCREF(Py_None); + return Py_None; } -/*****************************************************************************/ -/* Function: Camera_Get */ -/* Python equivalent: Blender.Camera.Get */ -/*****************************************************************************/ -PyObject *Camera_Get(PyObject *self, PyObject *args) +/* This one is 'private'. It is not really a method, just a helper function for + * when script writers use Camera.type = t instead of Camera.setType(t), since in + * the first case t should be an int and in the second a string. So while the + * method setType expects a string ('persp' or 'ortho') or an empty argument, + * this function should receive an int (0 or 1). */ +static PyObject *Camera_setIntType(C_Camera *self, PyObject *args) { - char * name; - struct Camera * cam_iter; - BlenCamera * blen_camera; + short value; + PyObject *type; - printf ("In Camera_Get()\n"); - if (!PyArg_ParseTuple(args, "s", &name)) + if (!PyArg_ParseTuple(args, "i", &value)) { return (PythonReturnErrorObject (PyExc_AttributeError, - "expected string argument")); + "expected int argument: 0 or 1")); } - /* Use the name to search for the camera requested. */ - blen_camera = NULL; - cam_iter = G.main->camera.first; - while ((cam_iter) && (blen_camera == NULL)) + if (value == 0 || value == 1) + { + type = PyInt_FromLong(value); + } + else + { + return (PythonReturnErrorObject (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; +} + +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, + "expected one or two strings as arguments")); + } + + if (mode_str1 != NULL) { - if (StringEqual (name, GetIdName (&(cam_iter->id)))) + if (strcmp(mode_str1, "showLimits") == 0) + flag |= EXPP_CAM_MODE_SHOWLIMITS; + else if (strcmp(mode_str1, "showMist") == 0) + flag |= EXPP_CAM_MODE_SHOWMIST; + else + { + return (PythonReturnErrorObject (PyExc_AttributeError, + "first argument is an unknown camera flag")); + } + + if (mode_str2 != NULL) { - blen_camera = (BlenCamera*)PyObject_NEW(BlenCamera, &camera_type); - blen_camera->camera = cam_iter; + if (strcmp(mode_str2, "showLimits") == 0) + flag |= EXPP_CAM_MODE_SHOWLIMITS; + else if (strcmp(mode_str2, "showMist") == 0) + flag |= EXPP_CAM_MODE_SHOWMIST; + else + { + return (PythonReturnErrorObject (PyExc_AttributeError, + "second argument is an unknown camera flag")); + } } - cam_iter = cam_iter->id.next; + } + + mode = PyInt_FromLong(flag); + if (!mode) + { + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyInt Object")); } - if (blen_camera == NULL) + if (PyDict_SetItemString(self->dict, "mode", mode) != 0) { - /* No camera exists with the name specified in the argument name. */ - char error_msg[256]; - sprintf(error_msg, "camera \"%s\" not found", name); - return (PythonReturnErrorObject (PyExc_AttributeError, error_msg)); + Py_DECREF(mode); + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't set Camera.mode attribute")); } - return ((PyObject*)blen_camera); + if (self->linked) /* update the Blender Camera, too */ + self->camera->flag = flag; + + Py_INCREF(Py_None); + return Py_None; } -/*****************************************************************************/ -/* Function: initCamera */ -/*****************************************************************************/ -PyObject *initCamera (void) +/* Another helper function, for the same reason. + * (See comment before Camera_setIntType above). */ +static PyObject *Camera_setIntMode(C_Camera *self, PyObject *args) { - PyObject * module; + short value; + PyObject *mode; + + if (!PyArg_ParseTuple(args, "h", &value)) + { + return (PythonReturnErrorObject (PyExc_AttributeError, + "expected int argument in [0,3]")); + } - printf ("In initCamera()\n"); + if (value >= 0 && value <= 3) + { + mode = PyInt_FromLong(value); + } + else + { + return (PythonReturnErrorObject (PyExc_AttributeError, + "expected int argument in [0,3]")); + } - module = Py_InitModule("Camera", Camera_methods); + if (!mode) + { + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyInt object")); + } - return (module); + 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; +} + +static PyObject *Camera_setLens(C_Camera *self, PyObject *args) +{ + float value; + PyObject *lens; + + if (!PyArg_ParseTuple(args, "f", &value)) + { + return (PythonReturnErrorObject (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")); + } + + if (self->linked) + { + /* update the Blender Camera, too */ + self->camera->lens = value; + } + + Py_INCREF(Py_None); + return Py_None; +} + +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")); + } + + if (PyDict_SetItemString(self->dict, "clipStart", clipStart) != 0) + { + Py_DECREF(clipStart); + return (PythonReturnErrorObject (PyExc_RuntimeError, + "couldn't set Camera.clipStart attribute")); + } + + if (self->linked) + { + /* update the Blender Camera, too */ + self->camera->clipsta = value; + } + + Py_INCREF(Py_None); + return Py_None; +} + +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")); + } + + 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; + } + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *Camera_setDrawSize(C_Camera *self, PyObject *args) +{ + float value; + PyObject *drawSize; + + if (!PyArg_ParseTuple(args, "f", &value)) + { + return (PythonReturnErrorObject (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; + } + + Py_INCREF(Py_None); + return Py_None; } /*****************************************************************************/ /* Function: CameraCreatePyObject */ -/* Description: This function will create a new BlenCamera from an existing */ -/* Camera structure. */ +/* 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 (struct Camera *cam) +PyObject *CameraCreatePyObject (Camera *blenderCam) { - BlenCamera * blen_camera; + PyObject *name, *type, *mode; + PyObject *lens, *clipStart, *clipEnd, *drawSize; + PyObject *Types, *persp, *ortho; + PyObject *Modes, *showLimits, *showMist; + C_Camera *cam; printf ("In CameraCreatePyObject\n"); - blen_camera = (BlenCamera*)PyObject_NEW(BlenCamera, &camera_type); + 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; + } - blen_camera->camera = cam; - return ((PyObject*)blen_camera); + 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 BlenCamera type. It is */ +/* Description: This is a callback function for the C_Camera type. It is */ /* the destructor function. */ /*****************************************************************************/ -void CameraDeAlloc (BlenCamera *cam) +static void CameraDeAlloc (C_Camera *self) { - PyObject_DEL (cam); + Py_DECREF(self->dict); + PyObject_DEL (self); } /*****************************************************************************/ /* Function: CameraGetAttr */ -/* Description: This is a callback function for the BlenCamera type. It is */ -/* the function that retrieves any value from Blender and */ -/* passes it to Python. */ +/* Description: This is a callback function for the C_Camera type. It is */ +/* the function that accesses C_Camera member variables and */ +/* methods. */ /*****************************************************************************/ -PyObject* CameraGetAttr (BlenCamera *cam, char *name) +static PyObject* CameraGetAttr (C_Camera *cam, char *name) { - struct Camera * camera; - - camera = cam->camera; - if (StringEqual (name, "lens")) - return (PyFloat_FromDouble(camera->lens)); - if (StringEqual (name, "clipStart")) - return (PyFloat_FromDouble(camera->clipsta)); - if (StringEqual (name, "clipEnd")) - return (PyFloat_FromDouble(camera->clipend)); - if (StringEqual (name, "type")) - return (PyInt_FromLong(camera->type)); - if (StringEqual (name, "mode")) - return (PyInt_FromLong(camera->flag)); - - printf ("Unknown variable.\n"); - Py_INCREF (Py_None); - return (Py_None); + /* 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; + } + } + + /* not an attribute, search the methods table */ + return Py_FindMethod(C_Camera_methods, (PyObject *)cam, name); } /*****************************************************************************/ /* Function: CameraSetAttr */ -/* Description: This is a callback function for the BlenCamera type. It is */ -/* the function that retrieves any value from Python and sets */ -/* it accordingly in Blender. */ +/* 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. */ /*****************************************************************************/ -int CameraSetAttr (BlenCamera *cam, char *name, PyObject *value) +static int CameraSetAttr (C_Camera *self, char *name, PyObject *value) { - struct Camera * camera; - - camera = cam->camera; - if (StringEqual (name, "lens")) - return (!PyArg_Parse (value, "f", &(camera->lens))); - if (StringEqual (name, "clipStart")) - return (!PyArg_Parse (value, "f", &(camera->clipsta))); - if (StringEqual (name, "clipEnd")) - return (!PyArg_Parse (value, "f", &(camera->clipend))); - if (StringEqual (name, "type")) - return (!PyArg_Parse (value, "i", &(camera->type))); - if (StringEqual (name, "mode")) - return (!PyArg_Parse (value, "i", &(camera->flag))); - - printf ("Unknown variable.\n"); - return (0); + 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 + * 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 */ + + if (!valtuple) + { + return EXPP_intError(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"); + } + + if (strcmp (name, "name") == 0) + error = Camera_rename (self, valtuple); + else if (strcmp (name, "type") == 0) + error = Camera_setIntType (self, valtuple); /* special case */ + else if (strcmp (name, "mode") == 0) + error = Camera_setIntMode (self, valtuple); /* special case */ + else if (strcmp (name, "lens") == 0) + error = Camera_setLens (self, valtuple); + else if (strcmp (name, "clipStart") == 0) + error = Camera_setClipStart (self, valtuple); + else if (strcmp (name, "clipEnd") == 0) + 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); + Py_DECREF(valtuple); + return (EXPP_intError (PyExc_KeyError, + "attribute not found")); + } + + if (error == Py_None) + { + return 0; /* normal exit */ + } + + Py_DECREF(value); + Py_DECREF(valtuple); + + return -1; } + +/*****************************************************************************/ +/* Function: CameraPrint */ +/* Description: This is a callback function for the C_Camera type. It */ +/* builds a meaninful string to 'print' camera objects. */ +/*****************************************************************************/ +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); + + return 0; +} + /*****************************************************************************/ /* Function: CameraRepr */ -/* Description: This is a callback function for the BlenCamera type. It */ +/* Description: This is a callback function for the C_Camera type. It */ /* builds a meaninful string to represent camera objects. */ /*****************************************************************************/ -PyObject* CameraRepr (BlenCamera *self) +static PyObject *CameraRepr (C_Camera *self) { - static char s[256]; - sprintf (s, "[Camera \"%s\"]", self->camera->id.name+2); - return Py_BuildValue("s", s); + 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); } |