Main Page   Modules   Compound List   File List   Compound Members   File Members   Related Pages  

Camera-doxy.c

Go to the documentation of this file.
00001 /* 
00002  *
00003  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version. The Blender
00009  * Foundation also sells licenses for use in proprietary software under
00010  * the Blender License.  See http://www.blender.org/BL/ for information
00011  * about this.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * This is a new part of Blender.
00026  *
00027  * Contributor(s): Willian P. Germano
00028  *
00029  * ***** END GPL/BL DUAL LICENSE BLOCK *****
00030 */
00031 
00044 #include "Camera.h"
00045 
00052 
00064 static PyObject *M_Camera_New(PyObject *self, PyObject *args, PyObject *kwords)
00065 {
00066   char        *type_str = "persp"; /* "persp" is type 0, "ortho" is type 1 */
00067   char        *name_str = "CamData";
00068   static char *kwlist[] = {"type_str", "name_str", NULL};
00069   BPy_Camera  *pycam; /* for Camera Data object wrapper in Python */
00070   Camera      *blcam; /* for actual Camera Data we create in Blender */
00071   char        buf[21];
00072 
00073   printf ("In Camera_New()\n");
00074 
00075   if (!PyArg_ParseTupleAndKeywords(args, kwords, "|ss", kwlist,
00076                                    &type_str, &name_str))
00077   /* We expected string(s) (or nothing) as argument, but we didn't get that. */
00078     return (EXPP_ReturnPyObjError (PyExc_AttributeError,
00079             "expected zero, one or two strings as arguments"));
00080 
00081   blcam = add_camera(); /* first create the Camera Data in Blender */
00082 
00083   if (blcam) /* now create the wrapper obj in Python */
00084     pycam = (BPy_Camera *)PyObject_NEW(BPy_Camera, &Camera_Type);
00085   else
00086     return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00087                             "couldn't create Camera Data in Blender"));
00088 
00089   if (pycam == NULL)
00090     return (EXPP_ReturnPyObjError (PyExc_MemoryError,
00091                             "couldn't create Camera Data object"));
00092 
00093   pycam->camera = blcam; /* link Python camera wrapper to Blender Camera */
00094 
00095   if (strcmp (type_str, "persp") == 0) /* default, no need to set, so */
00096     /*blcam->type = (short)EXPP_CAM_TYPE_PERSP*/; /* we comment this line */
00097   else if (strcmp (type_str, "ortho") == 0)
00098     blcam->type = (short)EXPP_CAM_TYPE_ORTHO;
00099   else
00100     return (EXPP_ReturnPyObjError (PyExc_AttributeError,
00101             "unknown camera type"));
00102 
00103   if (strcmp(name_str, "CamData") == 0)
00104     return (PyObject *)pycam;
00105   else { /* user gave us a name for the camera, use it */
00106     PyOS_snprintf(buf, sizeof(buf), "%s", name_str);
00107     rename_id(&blcam->id, buf); /* proper way in Blender */
00108   }
00109 
00110   return (PyObject *)pycam;
00111 }
00112 
00126 static PyObject *M_Camera_Get(PyObject *self, PyObject *args)
00127 {
00128   char   *name = NULL;
00129   Camera *cam_iter;
00130 
00131   if (!PyArg_ParseTuple(args, "|s", &name))
00132     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00133             "expected string argument (or nothing)"));
00134 
00135   cam_iter = G.main->camera.first;
00136 
00137   if (name) { /* (name) - Search camera by name */
00138 
00139     BPy_Camera *wanted_cam = NULL;
00140 
00141     while ((cam_iter) && (wanted_cam == NULL)) {
00142       if (strcmp (name, cam_iter->id.name+2) == 0) {
00143         wanted_cam = (BPy_Camera *)PyObject_NEW(BPy_Camera, &Camera_Type);
00144         if (wanted_cam) wanted_cam->camera = cam_iter;
00145       }
00146       cam_iter = cam_iter->id.next;
00147     }
00148 
00149     if (wanted_cam == NULL) { /* Requested camera doesn't exist */
00150       char error_msg[64];
00151       PyOS_snprintf(error_msg, sizeof(error_msg),
00152                       "Camera \"%s\" not found", name);
00153       return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg));
00154     }
00155 
00156     return (PyObject *)wanted_cam;
00157   }
00158 
00159   else { /* () - return a list of all cameras in the scene */
00160     int index = 0;
00161     PyObject *camlist, *pystr;
00162 
00163     camlist = PyList_New (BLI_countlist (&(G.main->camera)));
00164 
00165     if (camlist == NULL)
00166       return (PythonReturnErrorObject (PyExc_MemoryError,
00167               "couldn't create PyList"));
00168 
00169     while (cam_iter) {
00170       pystr = PyString_FromString (cam_iter->id.name+2);
00171 
00172       if (!pystr)
00173         return (PythonReturnErrorObject (PyExc_MemoryError,
00174                   "couldn't create PyString"));
00175 
00176       PyList_SET_ITEM (camlist, index, pystr);
00177 
00178       cam_iter = cam_iter->id.next;
00179       index++;
00180     }
00181 
00182     return (camlist);
00183   }
00184 }
00195 PyObject *Camera_Init (void)
00196 {
00197   PyObject  *submodule;
00198 
00199         Camera_Type.ob_type = &PyType_Type;
00200 
00201         submodule = Py_InitModule3("Blender.Camera",
00202                   M_Camera_methods, M_Camera_doc);
00203 
00204   return submodule;
00205 }
00206 
00207 /* Three Python Camera_Type helper functions needed by the Object module: */
00208 
00218 PyObject *Camera_CreatePyObject (Camera *cam)
00219 {
00220   BPy_Camera *pycam;
00221 
00222   pycam = (BPy_Camera *)PyObject_NEW (BPy_Camera, &Camera_Type);
00223 
00224   if (!pycam)
00225     return EXPP_ReturnPyObjError (PyExc_MemoryError,
00226             "couldn't create BPy_Camera object");
00227 
00228   pycam->camera = cam;
00229 
00230   return (PyObject *)pycam;
00231 }
00232 
00242 int Camera_CheckPyObject (PyObject *pyobj)
00243 {
00244   return (pyobj->ob_type == &Camera_Type);
00245 }
00246 
00256 Camera *Camera_FromPyObject (PyObject *pyobj)
00257 {
00258   return ((BPy_Camera *)pyobj)->camera;
00259 }
00260 
00261 /*****************************************************************************/
00262 /* Python BPy_Camera methods:                                                */
00263 /*****************************************************************************/
00264 
00273 
00280 static PyObject *Camera_getName(BPy_Camera *self)
00281 {
00282   PyObject *attr = PyString_FromString(self->camera->id.name+2);
00283 
00284   if (attr) return attr;
00285 
00286   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00287                                    "couldn't get Camera.name attribute"));
00288 }
00289 
00296 static PyObject *Camera_getType(BPy_Camera *self)
00297 {
00298   PyObject *attr = PyInt_FromLong(self->camera->type);
00299 
00300   if (attr) return attr;
00301 
00302   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00303                                    "couldn't get Camera.type attribute"));
00304 }
00305 
00312 static PyObject *Camera_getMode(BPy_Camera *self)
00313 {
00314   PyObject *attr = PyInt_FromLong(self->camera->flag);
00315 
00316   if (attr) return attr;
00317 
00318   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00319                                    "couldn't get Camera.Mode attribute"));
00320 }
00321 
00328 static PyObject *Camera_getLens(BPy_Camera *self)
00329 {
00330   PyObject *attr = PyFloat_FromDouble(self->camera->lens);
00331 
00332   if (attr) return attr;
00333 
00334   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00335                                    "couldn't get Camera.lens attribute"));
00336 }
00337 
00344 static PyObject *Camera_getClipStart(BPy_Camera *self)
00345 {
00346   PyObject *attr = PyFloat_FromDouble(self->camera->clipsta);
00347 
00348   if (attr) return attr;
00349 
00350   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00351                                    "couldn't get Camera.clipStart attribute"));
00352 }
00353 
00359 static PyObject *Camera_getClipEnd(BPy_Camera *self)
00360 {
00361   PyObject *attr = PyFloat_FromDouble(self->camera->clipend);
00362 
00363   if (attr) return attr;
00364 
00365   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00366                                    "couldn't get Camera.clipEnd attribute"));
00367 }
00368 
00374 static PyObject *Camera_getDrawSize(BPy_Camera *self)
00375 {
00376   PyObject *attr = PyFloat_FromDouble(self->camera->drawsize);
00377 
00378   if (attr) return attr;
00379 
00380   return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
00381                                    "couldn't get Camera.drawSize attribute"));
00382 }
00383 
00389 static PyObject *Camera_setName(BPy_Camera *self, PyObject *args)
00390 {
00391   char *name;
00392   char buf[21];
00393 
00394   if (!PyArg_ParseTuple(args, "s", &name))
00395     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00396                                      "expected string argument"));
00397 
00398   PyOS_snprintf(buf, sizeof(buf), "%s", name);
00399 
00400   rename_id(&self->camera->id, buf);
00401 
00402   Py_INCREF(Py_None);
00403   return Py_None;
00404 }
00405 
00411 static PyObject *Camera_setType(BPy_Camera *self, PyObject *args)
00412 {
00413   char *type;
00414 
00415   if (!PyArg_ParseTuple(args, "s", &type))
00416     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00417                                      "expected string argument"));
00418 
00419   if (strcmp (type, "persp") == 0)
00420     self->camera->type = (short)EXPP_CAM_TYPE_PERSP;
00421   else if (strcmp (type, "ortho") == 0)
00422     self->camera->type = (short)EXPP_CAM_TYPE_ORTHO;  
00423   else
00424     return (EXPP_ReturnPyObjError (PyExc_AttributeError,
00425                                      "unknown camera type"));
00426 
00427   Py_INCREF(Py_None);
00428   return Py_None;
00429 }
00430 
00431 /* This one is 'private'. It is not really a method, just a helper function for
00432  * when script writers use Camera.type = t instead of Camera.setType(t), since in
00433  * the first case t should be an int and in the second a string. So while the
00434  * method setType expects a string ('persp' or 'ortho') or an empty argument,
00435  * this function should receive an int (0 or 1). */
00436 
00444 static PyObject *Camera_setIntType(BPy_Camera *self, PyObject *args)
00445 {
00446   short value;
00447 
00448   if (!PyArg_ParseTuple(args, "h", &value))
00449     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00450                                      "expected int argument: 0 or 1"));
00451 
00452   if (value == 0 || value == 1)
00453     self->camera->type = value;
00454   else
00455     return (EXPP_ReturnPyObjError (PyExc_ValueError,
00456                                      "expected int argument: 0 or 1"));
00457 
00458   Py_INCREF(Py_None);
00459   return Py_None;
00460 }
00461 
00473 static PyObject *Camera_setMode(BPy_Camera *self, PyObject *args)
00474 {
00475   char *mode_str1 = NULL, *mode_str2 = NULL;
00476   short flag = 0;
00477 
00478   if (!PyArg_ParseTuple(args, "|ss", &mode_str1, &mode_str2))
00479     return (EXPP_ReturnPyObjError (PyExc_AttributeError,
00480             "expected zero, one or two strings as arguments"));
00481 
00482   if (mode_str1 != NULL) {
00483     if (strcmp(mode_str1, "showLimits") == 0)
00484       flag |= (short)EXPP_CAM_MODE_SHOWLIMITS;
00485     else if (strcmp(mode_str1, "showMist") == 0)
00486       flag |= (short)EXPP_CAM_MODE_SHOWMIST;
00487     else
00488       return (EXPP_ReturnPyObjError (PyExc_AttributeError,
00489                               "first argument is an unknown camera flag"));
00490 
00491     if (mode_str2 != NULL) {
00492       if (strcmp(mode_str2, "showLimits") == 0)
00493         flag |= (short)EXPP_CAM_MODE_SHOWLIMITS;
00494       else if (strcmp(mode_str2, "showMist") == 0)
00495         flag |= (short)EXPP_CAM_MODE_SHOWMIST;
00496       else
00497         return (EXPP_ReturnPyObjError (PyExc_AttributeError,
00498                               "second argument is an unknown camera flag"));
00499     }
00500   }
00501 
00502   self->camera->flag = flag;
00503 
00504   Py_INCREF(Py_None);
00505   return Py_None;
00506 }
00507 
00508 /* Another helper function, for the same reason.
00509  * (See comment before Camera_setIntType above). */
00510 
00518 static PyObject *Camera_setIntMode(BPy_Camera *self, PyObject *args)
00519 {
00520   short value;
00521 
00522   if (!PyArg_ParseTuple(args, "h", &value))
00523     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00524                                      "expected int argument in [0,3]"));
00525 
00526   if (value >= 0 && value <= 3)
00527     self->camera->flag = value;
00528   else
00529     return (EXPP_ReturnPyObjError (PyExc_ValueError,
00530                                      "expected int argument in [0,3]"));
00531 
00532   Py_INCREF(Py_None);
00533   return Py_None;
00534 }
00535 
00541 static PyObject *Camera_setLens(BPy_Camera *self, PyObject *args)
00542 {
00543   float value;
00544   
00545   if (!PyArg_ParseTuple(args, "f", &value))
00546     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00547                                      "expected float argument"));
00548   
00549   self->camera->lens = EXPP_ClampFloat (value,
00550                   EXPP_CAM_LENS_MIN, EXPP_CAM_LENS_MAX);
00551   
00552   Py_INCREF(Py_None);
00553   return Py_None;
00554 }
00555 
00561 static PyObject *Camera_setClipStart(BPy_Camera *self, PyObject *args)
00562 {
00563   float value;
00564   
00565   if (!PyArg_ParseTuple(args, "f", &value))
00566     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00567                                      "expected float argument"));
00568 
00569   self->camera->clipsta = EXPP_ClampFloat (value,
00570                   EXPP_CAM_CLIPSTART_MIN, EXPP_CAM_CLIPSTART_MAX);
00571   
00572   Py_INCREF(Py_None);
00573   return Py_None;
00574 }
00575 
00581 static PyObject *Camera_setClipEnd(BPy_Camera *self, PyObject *args)
00582 {
00583   float value;
00584   
00585   if (!PyArg_ParseTuple(args, "f", &value))
00586     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00587                                      "expected float argument"));
00588 
00589   self->camera->clipend = EXPP_ClampFloat (value,
00590                   EXPP_CAM_CLIPEND_MIN, EXPP_CAM_CLIPEND_MAX);
00591 
00592   Py_INCREF(Py_None);
00593   return Py_None;
00594 }
00595 
00601 static PyObject *Camera_setDrawSize(BPy_Camera *self, PyObject *args)
00602 {
00603   float value;
00604   
00605   if (!PyArg_ParseTuple(args, "f", &value))
00606     return (EXPP_ReturnPyObjError (PyExc_TypeError,
00607                                      "expected a float number as argument"));
00608 
00609   self->camera->drawsize = EXPP_ClampFloat (value,
00610                   EXPP_CAM_DRAWSIZE_MIN, EXPP_CAM_DRAWSIZE_MAX);
00611 
00612   Py_INCREF(Py_None);
00613   return Py_None;
00614 }
00615 
00626 
00631 static void Camera_Dealloc (BPy_Camera *self)
00632 {
00633   PyObject_DEL (self);
00634 }
00635 
00643 static PyObject *Camera_GetAttr (BPy_Camera *self, char *name)
00644 {
00645   PyObject *attr = Py_None;
00646 
00647   if (strcmp(name, "name") == 0)
00648     attr = PyString_FromString(self->camera->id.name+2);
00649   else if (strcmp(name, "type") == 0)
00650     attr = PyInt_FromLong(self->camera->type);
00651   else if (strcmp(name, "mode") == 0)
00652     attr = PyInt_FromLong(self->camera->flag);
00653   else if (strcmp(name, "lens") == 0)
00654     attr = PyFloat_FromDouble(self->camera->lens);
00655   else if (strcmp(name, "clipStart") == 0)
00656     attr = PyFloat_FromDouble(self->camera->clipsta);
00657   else if (strcmp(name, "clipEnd") == 0)
00658     attr = PyFloat_FromDouble(self->camera->clipend);
00659   else if (strcmp(name, "drawSize") == 0)
00660     attr = PyFloat_FromDouble(self->camera->drawsize);
00661 
00662   else if (strcmp(name, "Types") == 0) {
00663     attr = Py_BuildValue("{s:h,s:h}", "persp", EXPP_CAM_TYPE_PERSP,
00664                                       "ortho", EXPP_CAM_TYPE_ORTHO);
00665   }
00666 
00667   else if (strcmp(name, "Modes") == 0) {
00668     attr = Py_BuildValue("{s:h,s:h}", "showLimits", EXPP_CAM_MODE_SHOWLIMITS,
00669                                   "showMist", EXPP_CAM_MODE_SHOWMIST);
00670   }
00671 
00672   else if (strcmp(name, "__members__") == 0) {
00673     attr = Py_BuildValue("[s,s,s,s,s,s,s,s,s]",
00674                     "name", "type", "mode", "lens", "clipStart",
00675                     "clipEnd", "drawSize", "Types", "Modes");
00676   }
00677 
00678   if (!attr)
00679     return (EXPP_ReturnPyObjError (PyExc_MemoryError,
00680                       "couldn't create PyObject"));
00681 
00682   if (attr != Py_None) return attr; /* member attribute found, return it */
00683 
00684   /* not an attribute, search the methods table */
00685   return Py_FindMethod(BPy_Camera_methods, (PyObject *)self, name);
00686 }
00687 
00695 static int Camera_SetAttr (BPy_Camera *self, char *name, PyObject *value)
00696 {
00697   PyObject *valtuple; 
00698   PyObject *error = NULL;
00699 
00700 /* We're playing a trick on the Python API users here.  Even if they use
00701  * Camera.member = val instead of Camera.setMember(val), we end up using the
00702  * function anyway, since it already has error checking, clamps to the right
00703  * interval and updates the Blender Camera structure when necessary. */
00704 
00705 /* First we put "value" in a tuple, because we want to pass it to functions
00706  * that only accept PyTuples. Using "N" doesn't increment value's ref count */
00707   valtuple = Py_BuildValue("(N)", value);
00708 
00709   if (!valtuple) /* everything OK with our PyObject? */
00710     return EXPP_ReturnIntError(PyExc_MemoryError,
00711                          "CameraSetAttr: couldn't create PyTuple");
00712 
00713 /* Now we just compare "name" with all possible BPy_Camera member variables */
00714   if (strcmp (name, "name") == 0)
00715     error = Camera_setName (self, valtuple);
00716   else if (strcmp (name, "type") == 0)
00717     error = Camera_setIntType (self, valtuple); /* special case */
00718   else if (strcmp (name, "mode") == 0)
00719     error = Camera_setIntMode (self, valtuple); /* special case */
00720   else if (strcmp (name, "lens") == 0)
00721     error = Camera_setLens (self, valtuple);
00722   else if (strcmp (name, "clipStart") == 0)
00723     error = Camera_setClipStart (self, valtuple);
00724   else if (strcmp (name, "clipEnd") == 0)
00725     error = Camera_setClipEnd (self, valtuple);
00726   else if (strcmp (name, "drawSize") == 0)
00727     error = Camera_setDrawSize (self, valtuple);
00728 
00729   else { /* Error */
00730     Py_DECREF(valtuple);
00731 
00732     if ((strcmp (name, "Types") == 0) || /* user tried to change a */
00733         (strcmp (name, "Modes") == 0))   /* constant dict type ... */
00734       return (EXPP_ReturnIntError (PyExc_AttributeError,
00735                    "constant dictionary -- cannot be changed"));
00736 
00737     else /* ... or no member with the given name was found */
00738       return (EXPP_ReturnIntError (PyExc_KeyError,
00739                    "attribute not found"));
00740   }
00741 
00742 /* valtuple won't be returned to the caller, so we need to DECREF it */
00743   Py_DECREF(valtuple);
00744 
00745   if (error != Py_None) return -1;
00746 
00747 /* Py_None was incref'ed by the called Camera_set* function. We probably
00748  * don't need to decref Py_None (!), but since Python/C API manual tells us
00749  * to treat it like any other PyObject regarding ref counting ... */
00750   Py_DECREF(Py_None);
00751   return 0; /* normal exit */
00752 }
00753 
00765 static int Camera_Compare (BPy_Camera *a, BPy_Camera *b)
00766 {
00767   Camera *pa = a->camera, *pb = b->camera;
00768   return (pa == pb) ? 0:-1;
00769 }
00770 
00778 static int Camera_Print(BPy_Camera *self, FILE *fp, int flags)
00779 { 
00780   fprintf(fp, "[Camera \"%s\"]", self->camera->id.name+2);
00781   return 0;
00782 }
00783 
00791 static PyObject *Camera_Repr (BPy_Camera *self)
00792 {
00793   return PyString_FromString(self->camera->id.name+2);
00794 }
00795 

Generated on Mon Jun 2 16:32:08 2003 for Blender by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001