Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2011-06-24 20:54:30 +0400
committerCampbell Barton <ideasman42@gmail.com>2011-06-24 20:54:30 +0400
commit12e02fd4746308746e2f9e316a3b5e8bcd5f2896 (patch)
tree5d56ceb9eb0fadb020297604ca3dbce663190524 /source/blender/python
parent74520bd1ef6ab4264f5e6a24269f7ac3b85b4e23 (diff)
own patch [#27752] Python Callback (Scriptlink functionality)
Python: * adds bpy.app.handlers which contains lists, each for an event type: render_pre, render_post, load_pre, load_post, save_pre, save_post * each list item needs to be a callable object which takes 1 argument (the ID). * callbacks are cleared on file load. Example: def MyFunc(scene): print("Callback:", data) bpy.app.handlers.render_post.append(MyFunc) C: * This patch adds a generic C callback api which is currently only used by python. * Unlike python callbacks these are not cleared on file load.
Diffstat (limited to 'source/blender/python')
-rw-r--r--source/blender/python/BPY_extern.h2
-rw-r--r--source/blender/python/intern/CMakeLists.txt2
-rw-r--r--source/blender/python/intern/bpy_app.c6
-rw-r--r--source/blender/python/intern/bpy_app_handlers.c170
-rw-r--r--source/blender/python/intern/bpy_app_handlers.h35
5 files changed, 215 insertions, 0 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index ae5253d07a0..cd5c8e53ef7 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -83,6 +83,8 @@ void BPY_text_free_code(struct Text *text);
void BPY_modules_update(struct bContext *C); // XXX - annoying, need this for pointers that get out of date
void BPY_modules_load_user(struct bContext *C);
+void BPY_app_handlers_reset(void);
+
void BPY_driver_reset(void);
float BPY_driver_exec(struct ChannelDriver *driver);
diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index 4e2d9642bc3..287ba45a1cf 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -42,6 +42,7 @@ set(INC_SYS
set(SRC
bpy.c
bpy_app.c
+ bpy_app_handlers.c
bpy_driver.c
bpy_interface.c
bpy_intern_string.c
@@ -59,6 +60,7 @@ set(SRC
bpy.h
bpy_app.h
+ bpy_app_handlers.h
bpy_driver.h
bpy_intern_string.h
bpy_operator.h
diff --git a/source/blender/python/intern/bpy_app.c b/source/blender/python/intern/bpy_app.c
index 41de1171aaa..079d5223f58 100644
--- a/source/blender/python/intern/bpy_app.c
+++ b/source/blender/python/intern/bpy_app.c
@@ -30,6 +30,7 @@
#include <Python.h>
#include "bpy_app.h"
+#include "bpy_app_handlers.h"
#include "bpy_driver.h"
#include "BLI_path_util.h"
@@ -74,6 +75,9 @@ static PyStructSequence_Field app_info_fields[]= {
{(char *)"build_cxxflags", (char *)"C++ compiler flags"},
{(char *)"build_linkflags", (char *)"Binary linking flags"},
{(char *)"build_system", (char *)"Build system used"},
+
+ /* submodules */
+ {(char *)"handlers", (char *)"Application handler callbacks"},
{NULL}
};
@@ -140,6 +144,8 @@ static PyObject *make_app_info(void)
SetStrItem("Unknown");
#endif
+ SetObjItem(BPY_app_handlers_struct());
+
#undef SetIntItem
#undef SetStrItem
#undef SetObjItem
diff --git a/source/blender/python/intern/bpy_app_handlers.c b/source/blender/python/intern/bpy_app_handlers.c
new file mode 100644
index 00000000000..a944e88ad2f
--- /dev/null
+++ b/source/blender/python/intern/bpy_app_handlers.c
@@ -0,0 +1,170 @@
+/*
+ * $Id:
+ *
+ * ***** BEGIN GPL 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.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/intern/bpy_app_handlers.c
+ * \ingroup pythonintern
+ */
+
+#include <Python.h>
+#include "BLI_utildefines.h"
+#include "BLI_callbacks.h"
+
+#include "RNA_types.h"
+#include "RNA_access.h"
+#include "bpy_rna.h"
+#include "bpy_app_handlers.h"
+
+void bpy_app_generic_callback(struct Main *main, struct ID *id, void *arg);
+
+static PyTypeObject BlenderAppCbType;
+
+static PyStructSequence_Field app_cb_info_fields[]= {
+ {(char *)"render_pre", NULL},
+ {(char *)"render_post", NULL},
+ {(char *)"load_pre", NULL},
+ {(char *)"load_post", NULL},
+ {(char *)"save_pre", NULL},
+ {(char *)"save_post", NULL},
+ {NULL}
+};
+
+static PyStructSequence_Desc app_cb_info_desc= {
+ (char *)"bpy.app.handlers", /* name */
+ (char *)"This module contains callbacks", /* doc */
+ app_cb_info_fields, /* fields */
+ (sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field)) - 1
+};
+
+/*
+#if (BLI_CB_EVT_TOT != ((sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field))))
+# error "Callbacks are out of sync"
+#endif
+*/
+
+static PyObject *py_cb_array[BLI_CB_EVT_TOT]= {0};
+
+static PyObject *make_app_cb_info(void)
+{
+ PyObject *app_cb_info;
+ int pos= 0;
+
+ app_cb_info= PyStructSequence_New(&BlenderAppCbType);
+ if (app_cb_info == NULL) {
+ return NULL;
+ }
+
+ for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
+ if(app_cb_info_fields[pos].name == NULL) {
+ Py_FatalError("invalid callback slots 1");
+ }
+ PyStructSequence_SET_ITEM(app_cb_info, pos, (py_cb_array[pos]= PyList_New(0)));
+ }
+ if(app_cb_info_fields[pos].name != NULL) {
+ Py_FatalError("invalid callback slots 2");
+ }
+
+ return app_cb_info;
+}
+
+PyObject *BPY_app_handlers_struct(void)
+{
+ PyObject *ret;
+
+ PyStructSequence_InitType(&BlenderAppCbType, &app_cb_info_desc);
+
+ ret= make_app_cb_info();
+
+ /* prevent user from creating new instances */
+ BlenderAppCbType.tp_init= NULL;
+ BlenderAppCbType.tp_new= NULL;
+
+ /* assign the C callbacks */
+ if(ret) {
+ static bCallbackFuncStore funcstore_array[BLI_CB_EVT_TOT]= {{0}};
+ bCallbackFuncStore *funcstore;
+ int pos= 0;
+
+ for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
+ funcstore= &funcstore_array[pos];
+ funcstore->func= bpy_app_generic_callback;
+ funcstore->alloc= 0;
+ funcstore->arg= SET_INT_IN_POINTER(pos);
+ BLI_add_cb(funcstore, pos);
+ }
+ }
+
+ return ret;
+}
+
+void BPY_app_handlers_reset(void)
+{
+ int pos= 0;
+
+ for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
+ PyList_SetSlice(py_cb_array[pos], 0, PY_SSIZE_T_MAX, NULL);
+ }
+}
+
+/* the actual callback - not necessarily called from py */
+void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg)
+{
+ PyObject *cb_list= py_cb_array[GET_INT_FROM_POINTER(arg)];
+ Py_ssize_t cb_list_len;
+ if((cb_list_len= PyList_GET_SIZE(cb_list)) > 0) {
+ PyGILState_STATE gilstate= PyGILState_Ensure();
+
+ PyObject* args= PyTuple_New(1); // save python creating each call
+ PyObject* func;
+ PyObject* ret;
+ Py_ssize_t pos;
+
+ /* setup arguments */
+ if(id) {
+ PointerRNA id_ptr;
+ RNA_id_pointer_create(id, &id_ptr);
+ PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr));
+ }
+ else {
+ PyTuple_SET_ITEM(args, 0, Py_None);
+ Py_INCREF(Py_None);
+ }
+
+ // Iterate the list and run the callbacks
+ for (pos=0; pos < cb_list_len; pos++) {
+ func= PyList_GET_ITEM(cb_list, pos);
+ ret= PyObject_Call(func, args, NULL);
+ if (ret==NULL) {
+ PyErr_Print();
+ PyErr_Clear();
+ }
+ else {
+ Py_DECREF(ret);
+ }
+ }
+
+ Py_DECREF(args);
+
+ PyGILState_Release(gilstate);
+ }
+}
diff --git a/source/blender/python/intern/bpy_app_handlers.h b/source/blender/python/intern/bpy_app_handlers.h
new file mode 100644
index 00000000000..5fdca826c3f
--- /dev/null
+++ b/source/blender/python/intern/bpy_app_handlers.h
@@ -0,0 +1,35 @@
+/*
+ * $Id:
+ *
+ * ***** BEGIN GPL 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.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/intern/bpy_app_handlers.h
+ * \ingroup pythonintern
+ */
+
+#ifndef BPY_APP_HANDLERS_H
+#define BPY_APP_HANDLERS_H
+
+PyObject *BPY_app_handlers_struct(void);
+void BPY_app_handlers_clear(void);
+
+#endif // BPY_APP_HANDLERS_H