diff options
author | Willian Padovani Germano <wpgermano@gmail.com> | 2003-06-07 07:49:45 +0400 |
---|---|---|
committer | Willian Padovani Germano <wpgermano@gmail.com> | 2003-06-07 07:49:45 +0400 |
commit | ad61dd37ef059b14b8e929d14d64306dcc4ba207 (patch) | |
tree | 7d7beaf7af3256225151c532321537602c37f893 | |
parent | b499fac975115444e18bcaa3ac6ec5a82592ffaa (diff) |
* Improved error checking in BPY_interface.c:
Applied a fix suggested by Guignot
* Added part of Module Blender.Scene.
* Added doxygen comments to Camera.c .
-rw-r--r-- | source/blender/python/BPY_interface.c | 80 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Blender.c | 1 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Camera.c | 360 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Camera.h | 11 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Scene.c | 571 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Scene.h | 66 | ||||
-rw-r--r-- | source/blender/python/api2_2x/modules.h | 7 |
7 files changed, 962 insertions, 134 deletions
diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c index 87b83a502c9..fefa80e9c21 100644 --- a/source/blender/python/BPY_interface.c +++ b/source/blender/python/BPY_interface.c @@ -175,7 +175,13 @@ void BPY_Err_Handle(Text *text) PyErr_Restore(exception, err, tb); // takes away reference! PyErr_Print(); tb = PySys_GetObject("last_traceback"); - Py_XINCREF(tb); + + if (!tb) { + printf("\nCan't get traceback\n"); + return; + } + + Py_INCREF(tb); /* From old bpython BPY_main.c: * 'check traceback objects and look for last traceback in the @@ -422,41 +428,6 @@ int BPY_call_importloader(char *name) return (0); } -/* XXX THE 3 FUNCTIONS BELOW ARE IMPLEMENTED IN DRAW.C */ - -/*****************************************************************************/ -/* Description: */ -/* Notes: Not implemented yet */ -/*****************************************************************************/ -//int BPY_spacetext_is_pywin(struct SpaceText *st) -//{ -// /* No printf is done here because it is called with every mouse move */ -// return (0); -//} - -/*****************************************************************************/ -/* Description: */ -/* Notes: Not implemented yet */ -/*****************************************************************************/ -//void BPY_spacetext_do_pywin_draw(struct SpaceText *st) -//{ -// printf ("In BPY_spacetext_do_pywin_draw\n"); -// return; -//} - -/*****************************************************************************/ -/* Description: */ -/* Notes: Not implemented yet */ -/*****************************************************************************/ -//void BPY_spacetext_do_pywin_event(struct SpaceText *st, -// unsigned short event, -// short val) -//{ -// printf ("In BPY_spacetext_do_pywin_event(st=?, event=%d, val=%d)\n", -// event, val); -// return; -//} - /*****************************************************************************/ /* Private functions */ /*****************************************************************************/ @@ -476,7 +447,37 @@ PyObject * RunPython(Text *text, PyObject *globaldict) * to speed-up execution if the user executes the script multiple times */ if (!text->compiled) { /* if it wasn't already compiled, do it now */ - buf = txt_to_buf(text); + +#ifdef BLENDER_SANDBOX_MODE + +/* IGNORE THIS ALL FOR A WHILE, IT'S VERY INCOMPLETE AND WILL CHANGE + * CONSIDERABLY IN THE NEXT COMMIT. THE ifdef won't stay, either. */ + +/* The import statement is a security risk, so we don't allow it in + * SANDBOX MODE. Instead, we import all needed modules ourselves and + * substitute all 'import' and '__import__' statements in the code by + * '#mport' and '#_import__', resp., making their lines become comments + * in Python (to let scripts run without import errors). */ + +/* Disable importing only for the safest sandbox mode */ + + txt_move_bof(text, 0); /* move to the beginning of the script */ + +/* Search all occurrences of 'import' in the script */ +/* XXX Also check for from ... import ... */ + while (txt_find_string (text, "import")) { + char *line = text->sell->line; + + if (text->curc > 1) /* is it '__import__' ? */ + if (strncmp (&line[text->curc - 2], + "__import__", 10) == 0) text->curc -= 2; + + line[text->curc] = '#'; /* change them to '#mport' or '#_import__' */ + } + +#endif + + buf = txt_to_buf(text); text->compiled = Py_CompileString(buf, GetName(text), Py_file_input); @@ -488,6 +489,11 @@ PyObject * RunPython(Text *text, PyObject *globaldict) } } +#ifdef BLENDER_SANDBOX_MODE + //save the script as a dict entry and call the eval code for it + //then return + PyDict_SetItemString(globaldict, "_SB_code", text->compiled); +#endif return PyEval_EvalCode(text->compiled, globaldict, globaldict); } diff --git a/source/blender/python/api2_2x/Blender.c b/source/blender/python/api2_2x/Blender.c index 042785d9a9c..d1fd7c93cb6 100644 --- a/source/blender/python/api2_2x/Blender.c +++ b/source/blender/python/api2_2x/Blender.c @@ -214,6 +214,7 @@ void M_Blender_Init (void) dict = PyModule_GetDict (module); g_blenderdict = dict; + PyDict_SetItemString (dict, "Scene", M_Scene_Init()); PyDict_SetItemString (dict, "Object", M_Object_Init()); PyDict_SetItemString (dict, "Types", M_Types_Init()); PyDict_SetItemString (dict, "NMesh", M_NMesh_Init()); diff --git a/source/blender/python/api2_2x/Camera.c b/source/blender/python/api2_2x/Camera.c index c788c8ff840..65fccb655e5 100644 --- a/source/blender/python/api2_2x/Camera.c +++ b/source/blender/python/api2_2x/Camera.c @@ -29,6 +29,18 @@ * ***** END GPL/BL DUAL LICENSE BLOCK ***** */ +/** + * \file Camera.c + * \ingroup scripts + * \brief Blender.Camera Module and Camera Data PyObject implementation. + * + * Note: Parameters between "<" and ">" are optional. But if one of them is + * given, all preceding ones must be given, too. Of course, this only relates + * to the Python functions and methods described here and only inside Python + * code. [ This will go to another file later, probably the main exppython + * doc file]. + */ + #include <BKE_main.h> #include <BKE_global.h> #include <BKE_object.h> @@ -137,8 +149,8 @@ static PyMethodDef BPy_Camera_methods[] = { {"getType", (PyCFunction)Camera_getType, METH_NOARGS, "() - Return Camera type - 'persp':0, 'ortho':1"}, {"getMode", (PyCFunction)Camera_getMode, METH_NOARGS, - "() - Return Camera mode flags (or'ed value) -\n\t\ -'showLimits':1, 'showMist':2"}, + "() - Return Camera mode flags (or'ed value) -\n\t" + "'showLimits':1, 'showMist':2"}, {"getLens", (PyCFunction)Camera_getLens, METH_NOARGS, "() - Return Camera lens value"}, {"getClipStart", (PyCFunction)Camera_getClipStart, METH_NOARGS, @@ -202,10 +214,24 @@ PyTypeObject Camera_Type = 0, /* tp_members */ }; -/*****************************************************************************/ -/* Function: M_Camera_New */ -/* Python equivalent: Blender.Camera.New */ -/*****************************************************************************/ +/** + * \defgroup Camera_Module Blender.Camera module functions + * + */ + +/*@{*/ + +/** + * \brief Python module function: Blender.Camera.New() + * + * This is the .New() function of the Blender.Camera submodule. It creates + * new Camera Data in Blender and returns its Python wrapper object. The + * parameters are optional and default to type = 'persp' and name = 'CamData'. + * \param <type> - string: The Camera type: 'persp' or 'ortho'; + * \param <name> - string: The Camera Data name. + * \return A new Camera PyObject. + */ + static PyObject *M_Camera_New(PyObject *self, PyObject *args, PyObject *kwords) { char *type_str = "persp"; /* "persp" is type 0, "ortho" is type 1 */ @@ -255,33 +281,38 @@ static PyObject *M_Camera_New(PyObject *self, PyObject *args, PyObject *kwords) return (PyObject *)pycam; } -/*****************************************************************************/ -/* Function: M_Camera_Get */ -/* Python equivalent: Blender.Camera.Get */ -/* Description: Receives a string and returns the camera data obj */ -/* whose name matches the string. If no argument is */ -/* passed in, a list of all camera data names in the */ -/* current scene is returned. */ -/*****************************************************************************/ +/** + * \brief Python module function: Blender.Camera.Get() + * + * This is the .Get() function of the Blender.Camera submodule. It searches + * the list of current Camera Data objects and returns a Python wrapper for + * the one with the name provided by the user. If called with no arguments, + * it returns a list of all current Camera Data object names in Blender. + * \param <name> - string: The name of an existing Blender Camera Data object. + * \return () - A list with the names of all current Camera Data objects;\n + * \return (name) - A Python wrapper for the Camera Data called 'name' + * in Blender. + */ + static PyObject *M_Camera_Get(PyObject *self, PyObject *args) { char *name = NULL; Camera *cam_iter; - if (!PyArg_ParseTuple(args, "|s", &name)) + if (!PyArg_ParseTuple(args, "|s", &name)) return (EXPP_ReturnPyObjError (PyExc_TypeError, "expected string argument (or nothing)")); cam_iter = G.main->camera.first; - if (name) { /* (name) - Search camera by name */ + if (name) { /* (name) - Search camera by name */ BPy_Camera *wanted_cam = NULL; while ((cam_iter) && (wanted_cam == NULL)) { if (strcmp (name, cam_iter->id.name+2) == 0) { wanted_cam = (BPy_Camera *)PyObject_NEW(BPy_Camera, &Camera_Type); - if (wanted_cam) wanted_cam->camera = cam_iter; + if (wanted_cam) wanted_cam->camera = cam_iter; } cam_iter = cam_iter->id.next; } @@ -294,9 +325,9 @@ static PyObject *M_Camera_Get(PyObject *self, PyObject *args) } return (PyObject *)wanted_cam; - } + } - else { /* () - return a list of all cameras in the scene */ + else { /* () - return a list of all cameras in the scene */ int index = 0; PyObject *camlist, *pystr; @@ -306,27 +337,32 @@ static PyObject *M_Camera_Get(PyObject *self, PyObject *args) return (PythonReturnErrorObject (PyExc_MemoryError, "couldn't create PyList")); - while (cam_iter) { + while (cam_iter) { pystr = PyString_FromString (cam_iter->id.name+2); - if (!pystr) - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyString")); + if (!pystr) + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyString")); - PyList_SET_ITEM (camlist, index, pystr); + PyList_SET_ITEM (camlist, index, pystr); cam_iter = cam_iter->id.next; index++; - } + } - return (camlist); - } + return (camlist); + } } +/*@}*/ + +/** + * \brief Initializes the Blender.Camera submodule + * + * This function is used by Blender_Init() in Blender.c to register the + * Blender.Camera submodule in the main Blender module. + * \return PyObject*: The initialized submodule. + */ -/*****************************************************************************/ -/* Function: M_Camera_Init */ -/*****************************************************************************/ -/* Needed by the Blender module, to register the Blender.Camera submodule */ PyObject *M_Camera_Init (void) { PyObject *submodule; @@ -343,49 +379,77 @@ PyObject *M_Camera_Init (void) /* Three Python Camera_Type helper functions needed by the Object module: */ -/*****************************************************************************/ -/* Function: Camera_CreatePyObject */ -/* Description: This function will create a new BPy_Camera from an existing */ -/* Blender camera structure. */ -/*****************************************************************************/ +/** + * \brief Creates a new Python wrapper from an existing Blender Camera Data obj + * + * This is also used in Object.c when defining the object.data member variable + * for an Object of type 'Camera'. + * \param cam - Camera*: A pointer to an existing Blender Camera Data object. + * \return PyObject*: The Camera Data wrapper created. + */ + PyObject *Camera_CreatePyObject (Camera *cam) { - BPy_Camera *pycam; + BPy_Camera *pycam; - pycam = (BPy_Camera *)PyObject_NEW (BPy_Camera, &Camera_Type); + pycam = (BPy_Camera *)PyObject_NEW (BPy_Camera, &Camera_Type); - if (!pycam) - return EXPP_ReturnPyObjError (PyExc_MemoryError, - "couldn't create BPy_Camera object"); + if (!pycam) + return EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create BPy_Camera object"); - pycam->camera = cam; + pycam->camera = cam; - return (PyObject *)pycam; + return (PyObject *)pycam; } -/*****************************************************************************/ -/* Function: Camera_CheckPyObject */ -/* Description: This function returns true when the given PyObject is of the */ -/* type Camera. Otherwise it will return false. */ -/*****************************************************************************/ +/** + * \brief Checks if the given object is of type BPy_Camera + * + * This is also used in Object.c when handling the object.data member variable + * for an object of type 'Camera'. + * \param pyobj - PyObject*: A pointer to a Camera PyObject. + * \return int: True or false. + */ + int Camera_CheckPyObject (PyObject *pyobj) { - return (pyobj->ob_type == &Camera_Type); + return (pyobj->ob_type == &Camera_Type); } -/*****************************************************************************/ -/* Function: Camera_FromPyObject */ -/* Description: This function returns the Blender camera from the given */ -/* PyObject. */ -/*****************************************************************************/ +/** + * \brief Returns the Blender Camera object from the given PyObject + * + * This is also used in Object.c when handling the object.data member variable + * for an object of type 'Camera'. + * \param pyobj - PyObject*: A pointer to a Camera PyObject. + * \return Camera*: A pointer to the wrapped Blender Camera Data object. + */ + Camera *Camera_FromPyObject (PyObject *pyobj) { - return ((BPy_Camera *)pyobj)->camera; + return ((BPy_Camera *)pyobj)->camera; } /*****************************************************************************/ /* Python BPy_Camera methods: */ /*****************************************************************************/ + +/** + * \defgroup Camera_Methods Camera Method Functions + * + * These are the Camera PyObject method functions. They are used to get and + * set values for the Camera Data member variables. + */ + +/*@{*/ + +/** + * \brief Camera PyMethod getName + * + * \return string: The Camera Data name. + */ + static PyObject *Camera_getName(BPy_Camera *self) { PyObject *attr = PyString_FromString(self->camera->id.name+2); @@ -396,6 +460,12 @@ static PyObject *Camera_getName(BPy_Camera *self) "couldn't get Camera.name attribute")); } +/** + * \brief Camera PyMethod getType + * + * \return int: The Camera Data type. + */ + static PyObject *Camera_getType(BPy_Camera *self) { PyObject *attr = PyInt_FromLong(self->camera->type); @@ -406,6 +476,12 @@ static PyObject *Camera_getType(BPy_Camera *self) "couldn't get Camera.type attribute")); } +/** + * \brief Camera PyMethod getMode + * + * \return int: The Camera Data mode flags. + */ + static PyObject *Camera_getMode(BPy_Camera *self) { PyObject *attr = PyInt_FromLong(self->camera->flag); @@ -416,6 +492,12 @@ static PyObject *Camera_getMode(BPy_Camera *self) "couldn't get Camera.Mode attribute")); } +/** + * \brief Camera PyMethod getLens + * + * \return float: The Camera Data lens value + */ + static PyObject *Camera_getLens(BPy_Camera *self) { PyObject *attr = PyFloat_FromDouble(self->camera->lens); @@ -426,6 +508,12 @@ static PyObject *Camera_getLens(BPy_Camera *self) "couldn't get Camera.lens attribute")); } +/** + * \brief Camera PyMethod getClipStart + * + * \return float: The Camera Data clip start value. + */ + static PyObject *Camera_getClipStart(BPy_Camera *self) { PyObject *attr = PyFloat_FromDouble(self->camera->clipsta); @@ -436,6 +524,11 @@ static PyObject *Camera_getClipStart(BPy_Camera *self) "couldn't get Camera.clipStart attribute")); } +/** + * \brief Camera PyMethod getClipEnd + * \return float: The Camera Data clip end value. + */ + static PyObject *Camera_getClipEnd(BPy_Camera *self) { PyObject *attr = PyFloat_FromDouble(self->camera->clipend); @@ -446,6 +539,11 @@ static PyObject *Camera_getClipEnd(BPy_Camera *self) "couldn't get Camera.clipEnd attribute")); } +/** + * \brief Camera method getDrawSize + * \return float: The Camera Data draw size value. + */ + static PyObject *Camera_getDrawSize(BPy_Camera *self) { PyObject *attr = PyFloat_FromDouble(self->camera->drawsize); @@ -456,6 +554,11 @@ static PyObject *Camera_getDrawSize(BPy_Camera *self) "couldn't get Camera.drawSize attribute")); } +/** + * \brief Camera PyMethod setName + * \param name - string: The new Camera Data name. + */ + static PyObject *Camera_setName(BPy_Camera *self, PyObject *args) { char *name; @@ -473,6 +576,11 @@ static PyObject *Camera_setName(BPy_Camera *self, PyObject *args) return Py_None; } +/** + * \brief Camera PyMethod setType + * \param type - string: The new Camera Data type: 'persp' or 'ortho'. + */ + static PyObject *Camera_setType(BPy_Camera *self, PyObject *args) { char *type; @@ -498,6 +606,14 @@ static PyObject *Camera_setType(BPy_Camera *self, PyObject *args) * 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). */ + +/** + * \brief Internal helper function + * + * This one is not a PyMethod. It is just an internal helper function. + * \param type - int: The Camera Data type as an int. + */ + static PyObject *Camera_setIntType(BPy_Camera *self, PyObject *args) { short value; @@ -516,6 +632,17 @@ static PyObject *Camera_setIntType(BPy_Camera *self, PyObject *args) return Py_None; } +/** + * \brief Camera PyMethod setMode + * + * There are two mode flags for Cameras: 'showLimits' and 'showMist'. + * Both can be set at the same time, by providing two arguments to this + * method. To clear a flag, call setMode without the respective flag string + * in the argument list. For example: .setMode() clears both flags. + * \param mode1 - <string>: The first mode flag to set; + * \param mode2 - <string>: The second mode flag to set. + */ + static PyObject *Camera_setMode(BPy_Camera *self, PyObject *args) { char *mode_str1 = NULL, *mode_str2 = NULL; @@ -553,6 +680,14 @@ static PyObject *Camera_setMode(BPy_Camera *self, PyObject *args) /* Another helper function, for the same reason. * (See comment before Camera_setIntType above). */ + +/** + * \brief Internal helper function + * + * This one is not a PyMethod. It is just an internal helper function. + * \param mode - int: The Camera Data mode as an int. + */ + static PyObject *Camera_setIntMode(BPy_Camera *self, PyObject *args) { short value; @@ -571,6 +706,11 @@ static PyObject *Camera_setIntMode(BPy_Camera *self, PyObject *args) return Py_None; } +/** + * \brief Camera PyMethod setLens + * \param lens - float: The new Camera Data lens value. + */ + static PyObject *Camera_setLens(BPy_Camera *self, PyObject *args) { float value; @@ -586,6 +726,11 @@ static PyObject *Camera_setLens(BPy_Camera *self, PyObject *args) return Py_None; } +/** + * \brief Camera PyMethod setClipStart + * \param clipStart - float: The new Camera Data clip start value. + */ + static PyObject *Camera_setClipStart(BPy_Camera *self, PyObject *args) { float value; @@ -601,6 +746,11 @@ static PyObject *Camera_setClipStart(BPy_Camera *self, PyObject *args) return Py_None; } +/** + * \brief Camera PyMethod setClipEnd + * \param clipEnd - float: The new Camera Data clip end value. + */ + static PyObject *Camera_setClipEnd(BPy_Camera *self, PyObject *args) { float value; @@ -616,6 +766,11 @@ static PyObject *Camera_setClipEnd(BPy_Camera *self, PyObject *args) return Py_None; } +/** + * \brief Camera PyMethod setDrawSize + * \param drawSize - float: The new Camera Data draw size value. + */ + static PyObject *Camera_setDrawSize(BPy_Camera *self, PyObject *args) { float value; @@ -631,22 +786,33 @@ static PyObject *Camera_setDrawSize(BPy_Camera *self, PyObject *args) return Py_None; } -/*****************************************************************************/ -/* Function: Camera_DeAlloc */ -/* Description: This is a callback function for the BPy_Camera type. It is */ -/* the destructor function. */ -/*****************************************************************************/ +/*@}*/ + +/** + * \defgroup Camera_callbacks Callback functions for the Camera PyType + * + * These callbacks are called by the Python interpreter when dealing with + * PyObjects of type Camera. + */ + +/*@{*/ + +/** + * \brief The Camera PyType destructor + */ + static void Camera_DeAlloc (BPy_Camera *self) { PyObject_DEL (self); } -/*****************************************************************************/ -/* Function: Camera_GetAttr */ -/* Description: This is a callback function for the BPy_Camera type. It is */ -/* the function that accesses BPy_Camera "member variables" and */ -/* methods. */ -/*****************************************************************************/ +/** + * \brief The Camera PyType attribute getter + * + * This is the callback called when a user tries to retrieve the contents of + * Camera PyObject data members. Ex. in Python: "print mycamera.lens". + */ + static PyObject *Camera_GetAttr (BPy_Camera *self, char *name) { PyObject *attr = Py_None; @@ -692,12 +858,13 @@ static PyObject *Camera_GetAttr (BPy_Camera *self, char *name) return Py_FindMethod(BPy_Camera_methods, (PyObject *)self, name); } -/*****************************************************************************/ -/* Function: Camera_SetAttr */ -/* Description: This is a callback function for the BPy_Camera type. It is */ -/* the function that sets Camera Data attributes (member */ -/* variables). */ -/*****************************************************************************/ +/** + * \brief The Camera PyType attribute setter + * + * This is the callback called when the user tries to change the value of some + * Camera data member. Ex. in Python: "mycamera.lens = 45.0". + */ + static int Camera_SetAttr (BPy_Camera *self, char *name, PyObject *value) { PyObject *valtuple; @@ -757,37 +924,46 @@ static int Camera_SetAttr (BPy_Camera *self, char *name, PyObject *value) return 0; /* normal exit */ } -/*****************************************************************************/ -/* Function: Camera_Compare */ -/* Description: This is a callback function for the BPy_Camera type. It */ -/* compares two Camera_Type objects. Only the "==" and "!=" */ -/* comparisons are meaninful. Returns 0 for equality and -1 if */ -/* they don't point to the same Blender Camera struct. */ -/* In Python it becomes 1 if they are equal, 0 otherwise. */ -/*****************************************************************************/ +/** + * \brief The Camera PyType compare function + * + * This function compares two given Camera PyObjects, returning 0 for equality + * and -1 otherwise. In Python it becomes 1 if they are equal and 0 case not. + * The comparison is done with their pointers to Blender Camera Data objects, + * so any two wrappers pointing to the same Blender Camera Data will be + * considered the same Camera PyObject. Currently, only the "==" and "!=" + * comparisons are meaninful -- the "<", "<=", ">" or ">=" are not. + */ + static int Camera_Compare (BPy_Camera *a, BPy_Camera *b) { - Camera *pa = a->camera, *pb = b->camera; - return (pa == pb) ? 0:-1; + Camera *pa = a->camera, *pb = b->camera; + return (pa == pb) ? 0:-1; } -/*****************************************************************************/ -/* Function: Camera_Print */ -/* Description: This is a callback function for the BPy_Camera type. It */ -/* builds a meaninful string to 'print' camera objects. */ -/*****************************************************************************/ +/** + * \brief The Camera PyType print callback + * + * This function is called when the user tries to print a PyObject of type + * Camera. It builds a string with the name of the wrapped Blender Camera. + */ + static int Camera_Print(BPy_Camera *self, FILE *fp, int flags) { fprintf(fp, "[Camera \"%s\"]", self->camera->id.name+2); return 0; } -/*****************************************************************************/ -/* Function: Camera_Repr */ -/* Description: This is a callback function for the BPy_Camera type. It */ -/* builds a meaninful string to represent camera objects. */ -/*****************************************************************************/ +/** + * \brief The Camera PyType repr callback + * + * This function is called when the statement "repr(mycamera)" is executed in + * Python. Repr gives a string representation of a PyObject. + */ + static PyObject *Camera_Repr (BPy_Camera *self) { return PyString_FromString(self->camera->id.name+2); } + +/*@}*/ diff --git a/source/blender/python/api2_2x/Camera.h b/source/blender/python/api2_2x/Camera.h index c4e2598ba89..e7ab77555a4 100644 --- a/source/blender/python/api2_2x/Camera.h +++ b/source/blender/python/api2_2x/Camera.h @@ -39,6 +39,12 @@ #include "constant.h" #include "gen_utils.h" +/* The Camera PyType Object defined in Camera.c */ +PyTypeObject Camera_Type; + +#define BPy_Camera_Check(v) \ + ((v)->ob_type == &Camera_Type) /* for type checking */ + /*****************************************************************************/ /* Python BPy_Camera structure definition: */ /*****************************************************************************/ @@ -47,11 +53,6 @@ typedef struct { Camera *camera; } BPy_Camera; -PyTypeObject Camera_Type; /* The Camera PyType Object */ - -#define BPy_Camera_Check(v) \ - ((v)->ob_type == &Camera_Type) /* for type checking */ - /*****************************************************************************/ /* Python Camera_Type helper functions needed by Blender (the Init function) */ /* and Object modules. */ diff --git a/source/blender/python/api2_2x/Scene.c b/source/blender/python/api2_2x/Scene.c new file mode 100644 index 00000000000..fc4900cadf0 --- /dev/null +++ b/source/blender/python/api2_2x/Scene.c @@ -0,0 +1,571 @@ +/* + * + * ***** 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 ***** +*/ + +/** + * \file Scene.c + * \ingroup scripts + * \brief Blender.Scene Module and Scene PyObject implementation. + * + * Note: Parameters between "<" and ">" are optional. But if one of them is + * given, all preceding ones must be given, too. Of course, this only relates + * to the Python functions and methods described here and only inside Python + * code. [ This will go to another file later, probably the main exppython + * doc file]. + */ + +#include <BKE_main.h> +#include <BKE_global.h> +#include <BKE_scene.h> +#include <BKE_library.h> +#include <BLI_blenlib.h> + +#include "Scene.h" + +/*****************************************************************************/ +/* Python BPy_Scene defaults: */ +/*****************************************************************************/ + +/*****************************************************************************/ +/* Python API function prototypes for the Scene module. */ +/*****************************************************************************/ +static PyObject *M_Scene_New (PyObject *self, PyObject *args, + PyObject *keywords); +static PyObject *M_Scene_Get (PyObject *self, PyObject *args); +static PyObject *M_Scene_getCurrent (PyObject *self); +static PyObject *M_Scene_unlink (PyObject *self, PyObject *arg); + +/*****************************************************************************/ +/* The following string definitions are used for documentation strings. */ +/* In Python these will be written to the console when doing a */ +/* Blender.Scene.__doc__ */ +/*****************************************************************************/ +static char M_Scene_doc[] = ""; + +static char M_Scene_New_doc[] = ""; + +static char M_Scene_Get_doc[] = ""; + +static char M_Scene_getCurrent_doc[] = ""; + +static char M_Scene_unlink_doc[] = ""; + +/*****************************************************************************/ +/* Python method structure definition for Blender.Scene module: */ +/*****************************************************************************/ +struct PyMethodDef M_Scene_methods[] = { + {"New",(PyCFunction)M_Scene_New, METH_VARARGS|METH_KEYWORDS, + M_Scene_New_doc}, + {"Get", M_Scene_Get, METH_VARARGS, M_Scene_Get_doc}, + {"get", M_Scene_Get, METH_VARARGS, M_Scene_Get_doc}, + {"getCurrent",(PyCFunction)M_Scene_getCurrent, + METH_NOARGS, M_Scene_getCurrent_doc}, + {"unlink", M_Scene_unlink, METH_VARARGS, M_Scene_unlink_doc}, + {NULL, NULL, 0, NULL} +}; + +/*****************************************************************************/ +/* Python BPy_Scene methods declarations: */ +/*****************************************************************************/ +static PyObject *Scene_getName(BPy_Scene *self); +static PyObject *Scene_setName(BPy_Scene *self, PyObject *args); + +/*****************************************************************************/ +/* Python BPy_Scene methods table: */ +/*****************************************************************************/ +static PyMethodDef BPy_Scene_methods[] = { + /* name, method, flags, doc */ + {"getName", (PyCFunction)Scene_getName, METH_NOARGS, + "() - Return Scene name"}, + {"setName", (PyCFunction)Scene_setName, METH_VARARGS, + "(str) - Change Scene name"}, + {0} +}; + +/*****************************************************************************/ +/* Python Scene_Type callback function prototypes: */ +/*****************************************************************************/ +static void Scene_DeAlloc (BPy_Scene *self); +static int Scene_Print (BPy_Scene *self, FILE *fp, int flags); +static int Scene_SetAttr (BPy_Scene *self, char *name, PyObject *v); +static int Scene_Compare (BPy_Scene *a, BPy_Scene *b); +static PyObject *Scene_GetAttr (BPy_Scene *self, char *name); +static PyObject *Scene_Repr (BPy_Scene *self); + +/*****************************************************************************/ +/* Python Scene_Type structure definition: */ +/*****************************************************************************/ +PyTypeObject Scene_Type = +{ + PyObject_HEAD_INIT(NULL) + 0, /* ob_size */ + "Scene", /* tp_name */ + sizeof (BPy_Scene), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)Scene_DeAlloc, /* tp_dealloc */ + (printfunc)Scene_Print, /* tp_print */ + (getattrfunc)Scene_GetAttr, /* tp_getattr */ + (setattrfunc)Scene_SetAttr, /* tp_setattr */ + (cmpfunc)Scene_Compare, /* tp_compare */ + (reprfunc)Scene_Repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_as_hash */ + 0,0,0,0,0,0, + 0, /* tp_doc */ + 0,0,0,0,0,0, + BPy_Scene_methods, /* tp_methods */ + 0, /* tp_members */ +}; + +/** + * \defgroup Scene_Module Blender.Scene module functions + * + */ + +/*@{*/ + +/** + * \brief Python module function: Blender.Scene.New() + * + * This is the .New() function of the Blender.Scene submodule. It creates + * new Scene in Blender and returns its Python wrapper object. The + * parameter is optional and defaults to name = 'Scene'. + * \param <name> - string: The Scene name. + * \return A new Scene PyObject. + */ + +static PyObject *M_Scene_New(PyObject *self, PyObject *args, PyObject *kword) +{ + char *name = "Scene"; + char *kw[] = {"name", NULL}; + PyObject *pyscene; /* for the Scene object wrapper in Python */ + Scene *blscene; /* for the actual Scene we create in Blender */ + + printf ("In Scene_New()\n"); + + if (!PyArg_ParseTupleAndKeywords(args, kword, "|s", kw, &name)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected a string or an empty argument list")); + + blscene = add_scene(name); /* first create the Scene in Blender */ + + if (blscene) /* now create the wrapper obj in Python */ + pyscene = Scene_CreatePyObject (blscene); + else + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't create Scene obj in Blender")); + + if (pyscene == NULL) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create Scene PyObject")); + + return pyscene; +} + +/** + * \brief Python module function: Blender.Scene.Get() + * + * This is the .Get() function of the Blender.Scene submodule. It searches + * the list of current Scene objects and returns a Python wrapper for + * the one with the name provided by the user. If called with no arguments, + * it returns a list of all current Scene object names in Blender. + * \param <name> - string: The name of an existing Blender Scene object. + * \return () - A list with the names of all current Scene objects;\n + * \return (name) - A Python wrapper for the Scene called 'name' + * in Blender. + */ + +static PyObject *M_Scene_Get(PyObject *self, PyObject *args) +{ + char *name = NULL; + Scene *scene_iter; + + if (!PyArg_ParseTuple(args, "|s", &name)) + return (EXPP_ReturnPyObjError (PyExc_TypeError, + "expected string argument (or nothing)")); + + scene_iter = G.main->scene.first; + + if (name) { /* (name) - Search scene by name */ + + PyObject *wanted_scene = NULL; + + while ((scene_iter) && (wanted_scene == NULL)) { + + if (strcmp (name, scene_iter->id.name+2) == 0) + wanted_scene = Scene_CreatePyObject (scene_iter); + + scene_iter = scene_iter->id.next; + } + + if (wanted_scene == NULL) { /* Requested scene doesn't exist */ + char error_msg[64]; + PyOS_snprintf(error_msg, sizeof(error_msg), + "Scene \"%s\" not found", name); + return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg)); + } + + return wanted_scene; + } + + else { /* () - return a list of all scenes in Blender */ + int index = 0; + PyObject *scenelist, *pystr; + + scenelist = PyList_New (BLI_countlist (&(G.main->scene))); + + if (scenelist == NULL) + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyList")); + + while (scene_iter) { + pystr = PyString_FromString (scene_iter->id.name+2); + + if (!pystr) + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyString")); + + PyList_SET_ITEM (scenelist, index, pystr); + + scene_iter = scene_iter->id.next; + index++; + } + + return scenelist; + } +} + +/** + * \brief Python module function: Blender.Scene.getCurrent() + * + * \return A Python wrapper for the currently active scene. + */ + +PyObject *M_Scene_getCurrent (PyObject *self) +{ + return Scene_CreatePyObject ((Scene *)G.scene); +} + +/** + * \brief Python module function: Blender.Scene.unlink() + * + * This function actually frees the Blender Scene object linked to this + * Python wrapper. It calls free_libblock(), which calls free_scene(), + * where all objects linked to this scene have their user counts decremented. + * But there's no garbage collecting of objects in Blender yet. + * NOTE: a SystemError is raised if the user tries to remove the currently + * active Scene. Letting it be done would crash Blender. + * \param pyobj BPy_Scene*: A Scene PyObject wrapper. + */ + +PyObject *M_Scene_unlink (PyObject *self, PyObject *arg) +{ + PyObject *pyobj; + Scene *scene; + + if (!PyArg_ParseTuple (arg, "O!", &Scene_Type, &pyobj)) + return EXPP_ReturnPyObjError (PyExc_TypeError, + "expected Scene PyType object"); + + scene = ((BPy_Scene *)pyobj)->scene; + + if (scene == G.scene) + return EXPP_ReturnPyObjError (PyExc_SystemError, + "current Scene cannot be removed!"); + + free_libblock(&G.main->scene, scene); + + Py_INCREF(Py_None); + return Py_None; +} + +/*@}*/ + +/** + * \brief Initializes the Blender.Scene submodule + * + * This function is used by Blender_Init() in Blender.c to register the + * Blender.Scene submodule in the main Blender module. + * \return PyObject*: The initialized submodule. + */ + +PyObject *M_Scene_Init (void) +{ + PyObject *submodule; + + printf ("In M_Scene_Init()\n"); + + Scene_Type.ob_type = &PyType_Type; + + submodule = Py_InitModule3("Blender.Scene", + M_Scene_methods, M_Scene_doc); + + return submodule; +} + +/** + * \brief Creates a new Python wrapper from an existing Blender Scene obj + * + * \param scene - Scene*: A pointer to an existing Blender Scene object. + * \return PyObject*: The Scene wrapper created. + */ + +PyObject *Scene_CreatePyObject (Scene *scene) +{ + BPy_Scene *pyscene; + + pyscene = (BPy_Scene *)PyObject_NEW (BPy_Scene, &Scene_Type); + + if (!pyscene) + return EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create BPy_Scene object"); + + pyscene->scene = scene; + + return (PyObject *)pyscene; +} + +/** + * \brief Checks if the given object is of type BPy_Scene + * + * \param pyobj - PyObject*: A pointer to a Scene PyObject. + * \return int: True or false. + */ + +int Scene_CheckPyObject (PyObject *pyobj) +{ + return (pyobj->ob_type == &Scene_Type); +} + +/** + * \brief Returns the Blender Scene object from the given PyObject + * + * \param pyobj - PyObject*: A pointer to a Scene PyObject. + * \return Scene*: A pointer to the wrapped Blender Scene object. + */ + +Scene *Scene_FromPyObject (PyObject *pyobj) +{ + return ((BPy_Scene *)pyobj)->scene; +} + +/*****************************************************************************/ +/* Python BPy_Scene methods: */ +/*****************************************************************************/ + +/** + * \defgroup Scene_Methods Scene Method Functions + * + * These are the Scene PyObject method functions. They are used to get and + * set values for the Scene member variables. + */ + +/*@{*/ + +/** + * \brief Scene PyMethod getName + * + * \return string: The Scene name. + */ + +static PyObject *Scene_getName(BPy_Scene *self) +{ + PyObject *attr = PyString_FromString(self->scene->id.name+2); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Scene.name attribute")); +} + +/** + * \brief Scene PyMethod setName + * \param name - string: The new Scene name. + */ + +static PyObject *Scene_setName(BPy_Scene *self, PyObject *args) +{ + char *name; + char buf[21]; + + if (!PyArg_ParseTuple(args, "s", &name)) + return (EXPP_ReturnPyObjError (PyExc_TypeError, + "expected string argument")); + + PyOS_snprintf(buf, sizeof(buf), "%s", name); + + rename_id(&self->scene->id, buf); + + Py_INCREF(Py_None); + return Py_None; +} + +/*@}*/ + +/** + * \defgroup Scene_callbacks Callback functions for the Scene PyType + * + * These callbacks are called by the Python interpreter when dealing with + * PyObjects of type Scene. + */ + +/*@{*/ + +/** + * \brief The Scene PyType destructor + */ + +static void Scene_DeAlloc (BPy_Scene *self) +{ + PyObject_DEL (self); +} + +/** + * \brief The Scene PyType attribute getter + * + * This is the callback called when a user tries to retrieve the contents of + * Scene PyObject data members. Ex. in Python: "print myscene.lens". + */ + +static PyObject *Scene_GetAttr (BPy_Scene *self, char *name) +{ + PyObject *attr = Py_None; + + if (strcmp(name, "name") == 0) + attr = PyString_FromString(self->scene->id.name+2); + + 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(BPy_Scene_methods, (PyObject *)self, name); +} + +/** + * \brief The Scene PyType attribute setter + * + * This is the callback called when the user tries to change the value of some + * Scene data member. Ex. in Python: "myscene.lens = 45.0". + */ + +static int Scene_SetAttr (BPy_Scene *self, char *name, PyObject *value) +{ + PyObject *valtuple; + PyObject *error = NULL; + +/* We're playing a trick on the Python API users here. Even if they use + * Scene.member = val instead of Scene.setMember(val), we end up using the + * function anyway, since it already has error checking, clamps to the right + * interval and updates the Blender Scene structure when necessary. */ + +/* 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) /* everything OK with our PyObject? */ + return EXPP_ReturnIntError(PyExc_MemoryError, + "SceneSetAttr: couldn't create PyTuple"); + +/* Now we just compare "name" with all possible BPy_Scene member variables */ + if (strcmp (name, "name") == 0) + error = Scene_setName (self, valtuple); + + else { /* Error: no member with the given name was found */ + Py_DECREF(valtuple); + return (EXPP_ReturnIntError (PyExc_AttributeError, name)); + } + +/* valtuple won't be returned to the caller, so we need to DECREF it */ + Py_DECREF(valtuple); + + if (error != Py_None) return -1; + +/* Py_None was incref'ed by the called Scene_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 */ +} + +/** + * \brief The Scene PyType compare function + * + * This function compares two given Scene PyObjects, returning 0 for equality + * and -1 otherwise. In Python it becomes 1 if they are equal and 0 case not. + * The comparison is done with their pointers to Blender Scene objects, + * so any two wrappers pointing to the same Blender Scene will be + * considered the same Scene PyObject. Currently, only the "==" and "!=" + * comparisons are meaninful -- the "<", "<=", ">" or ">=" are not. + */ + +static int Scene_Compare (BPy_Scene *a, BPy_Scene *b) +{ + Scene *pa = a->scene, *pb = b->scene; + return (pa == pb) ? 0:-1; +} + +/** + * \brief The Scene PyType print callback + * + * This function is called when the user tries to print a PyObject of type + * Scene. It builds a string with the name of the wrapped Blender Scene. + */ + +static int Scene_Print(BPy_Scene *self, FILE *fp, int flags) +{ + fprintf(fp, "[Scene \"%s\"]", self->scene->id.name+2); + return 0; +} + +/** + * \brief The Scene PyType repr callback + * + * This function is called when the statement "repr(myscene)" is executed in + * Python. Repr gives a string representation of a PyObject. + */ + +static PyObject *Scene_Repr (BPy_Scene *self) +{ + return PyString_FromString(self->scene->id.name+2); +} + +/*@}*/ diff --git a/source/blender/python/api2_2x/Scene.h b/source/blender/python/api2_2x/Scene.h new file mode 100644 index 00000000000..4f3df06f1ed --- /dev/null +++ b/source/blender/python/api2_2x/Scene.h @@ -0,0 +1,66 @@ +/* + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * This is a new part of Blender. + * + * Contributor(s): Willian P. Germano + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#ifndef EXPP_SCENE_H +#define EXPP_SCENE_H + +#include <Python.h> +#include <DNA_scene_types.h> + +#include "constant.h" +#include "gen_utils.h" + +/* The Scene PyType Object defined in Scene.c */ +PyTypeObject Scene_Type; + +#define BPy_Scene_Check(v) \ + ((v)->ob_type == &Scene_Type) /* for type checking */ + +/*****************************************************************************/ +/* Python BPy_Scene structure definition: */ +/*****************************************************************************/ +typedef struct { + PyObject_HEAD + Scene *scene; + +} BPy_Scene; + +/*****************************************************************************/ +/* Python Scene_Type helper functions needed by Blender (the Init function) */ +/* and Object modules. */ +/*****************************************************************************/ +PyObject *M_Scene_Init (void); +PyObject *Scene_CreatePyObject (Scene *cam); +Scene *Scene_FromPyObject (PyObject *pyobj); +int Scene_CheckPyObject (PyObject *pyobj); + + +#endif /* EXPP_SCENE_H */ diff --git a/source/blender/python/api2_2x/modules.h b/source/blender/python/api2_2x/modules.h index dcfb2b1658d..05dc1a5f152 100644 --- a/source/blender/python/api2_2x/modules.h +++ b/source/blender/python/api2_2x/modules.h @@ -34,6 +34,7 @@ #include <Python.h> +#include <DNA_scene_types.h> #include <DNA_object_types.h> #include <DNA_camera_types.h> #include <DNA_lamp_types.h> @@ -62,6 +63,12 @@ PyObject * M_ObjectCreatePyObject (struct Object *obj); int M_ObjectCheckPyObject (PyObject *py_obj); struct Object * M_ObjectFromPyObject (PyObject *py_obj); +/* Scene */ +PyObject * M_Scene_Init (void); +PyObject * Scene_CreatePyObject (struct Scene *sce); +Scene * Scene_FromPyObject (PyObject *pyobj); +int Scene_CheckPyObject (PyObject *pyobj); + /* Types */ PyObject * M_Types_Init (void); |