Revision: 37795
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=37795
Author:   campbellbarton
Date:     2011-06-24 16:54:30 +0000 (Fri, 24 Jun 2011)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/blender.c
    trunk/blender/source/blender/blenlib/BLI_callbacks.h
    trunk/blender/source/blender/blenlib/CMakeLists.txt
    trunk/blender/source/blender/python/BPY_extern.h
    trunk/blender/source/blender/python/intern/CMakeLists.txt
    trunk/blender/source/blender/python/intern/bpy_app.c
    trunk/blender/source/blender/render/intern/source/pipeline.c
    trunk/blender/source/blender/windowmanager/intern/wm_files.c
    trunk/blender/source/blender/windowmanager/intern/wm_init_exit.c
    trunk/blender/source/creator/creator.c

Added Paths:
-----------
    trunk/blender/source/blender/blenlib/intern/callbacks.c
    trunk/blender/source/blender/python/intern/bpy_app_handlers.c
    trunk/blender/source/blender/python/intern/bpy_app_handlers.h

Modified: trunk/blender/source/blender/blenkernel/intern/blender.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/blender.c    2011-06-24 
15:10:34 UTC (rev 37794)
+++ trunk/blender/source/blender/blenkernel/intern/blender.c    2011-06-24 
16:54:30 UTC (rev 37795)
@@ -64,6 +64,7 @@
 #include "BLI_dynstr.h"
 #include "BLI_path_util.h"
 #include "BLI_utildefines.h"
+#include "BLI_callbacks.h"
 
 #include "IMB_imbuf.h"
 
@@ -110,6 +111,9 @@
        BKE_spacetypes_free();          /* after free main, it uses space 
callbacks */
        
        IMB_exit();
+
+       BLI_cb_finalize();
+
        seq_stripelem_cache_destruct();
        
        free_nodesystem();      

Modified: trunk/blender/source/blender/blenlib/BLI_callbacks.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_callbacks.h        2011-06-24 
15:10:34 UTC (rev 37794)
+++ trunk/blender/source/blender/blenlib/BLI_callbacks.h        2011-06-24 
16:54:30 UTC (rev 37795)
@@ -1,8 +1,6 @@
 /*
  * $Id:
  *
- * These callbacks are needed in the lib
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -29,7 +27,7 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/blenlib/intern/BLI_callbacks.h
+/** \file blender/blenlib/BLI_callbacks.h
  *  \ingroup bli
  */
 
@@ -37,8 +35,38 @@
 #ifndef BLI_CALLBACKS_H
 #define BLI_CALLBACKS_H
 
-// This is blenlib internal only
-void callLocalErrorCallBack(const char* msg);
+struct bContext;
+struct Main;
+struct ID;
 
+typedef enum {
+       BLI_CB_EVT_RENDER_PRE,
+       BLI_CB_EVT_RENDER_POST,
+       BLI_CB_EVT_LOAD_PRE,
+       BLI_CB_EVT_LOAD_POST,
+       BLI_CB_EVT_SAVE_PRE,
+       BLI_CB_EVT_SAVE_POST,
+       BLI_CB_EVT_TOT
+} eCbEvent;
+
+
+typedef struct {
+       struct bCallbackFuncStore *next, *prev;
+       void (* func)(struct Main *, struct ID *, void *arg);
+       void *arg;
+       short alloc;
+} bCallbackFuncStore;
+
+
+void BLI_exec_cb(struct Main *main, struct ID *self, eCbEvent evt);
+void BLI_add_cb(bCallbackFuncStore *funcstore, eCbEvent evt);
+
 #endif
 
+
+void BLI_cb_init(void);
+void BLI_cb_finalize(void);
+
+
+/* This is blenlib internal only, unrelated to above */
+void callLocalErrorCallBack(const char* msg);

Modified: trunk/blender/source/blender/blenlib/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/blenlib/CMakeLists.txt 2011-06-24 15:10:34 UTC 
(rev 37794)
+++ trunk/blender/source/blender/blenlib/CMakeLists.txt 2011-06-24 16:54:30 UTC 
(rev 37795)
@@ -52,6 +52,7 @@
        intern/DLRB_tree.c
        intern/boxpack2d.c
        intern/bpath.c
+       intern/callbacks.c
        intern/cpu.c
        intern/dynlib.c
        intern/edgehash.c

Added: trunk/blender/source/blender/blenlib/intern/callbacks.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/callbacks.c                     
        (rev 0)
+++ trunk/blender/source/blender/blenlib/intern/callbacks.c     2011-06-24 
16:54:30 UTC (rev 37795)
@@ -0,0 +1,70 @@
+/*
+ * $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): Blender Foundation (2011)
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "BLI_utildefines.h"
+#include "BLI_listbase.h"
+#include "BLI_callbacks.h"
+
+#include "MEM_guardedalloc.h"
+
+static ListBase callback_slots[BLI_CB_EVT_TOT]= {{0}};
+
+void BLI_exec_cb(struct Main *main, struct ID *self, eCbEvent evt)
+{
+       ListBase *lb= &callback_slots[evt];
+       bCallbackFuncStore *funcstore;
+
+       for(funcstore= (bCallbackFuncStore *)lb->first; funcstore; funcstore= 
(bCallbackFuncStore *)funcstore->next) {
+               funcstore->func(main, self, funcstore->arg);
+       }
+}
+
+void BLI_add_cb(bCallbackFuncStore *funcstore, eCbEvent evt)
+{
+       ListBase *lb= &callback_slots[evt];
+       BLI_addtail(lb, funcstore);
+}
+
+void BLI_cb_init(void)
+{
+       /* do nothing */
+}
+
+/* call on application exit */
+void BLI_cb_finalize(void)
+{
+       eCbEvent evt;
+       for(evt= 0; evt < BLI_CB_EVT_TOT; evt++) {
+               ListBase *lb= &callback_slots[evt];
+               bCallbackFuncStore *funcstore;
+               bCallbackFuncStore *funcstore_next;
+               for(funcstore= (bCallbackFuncStore *)lb->first; funcstore; 
funcstore= funcstore_next) {
+                       funcstore_next= (bCallbackFuncStore *)funcstore->next;
+                       BLI_remlink(lb, funcstore);
+                       if(funcstore->alloc) {
+                               MEM_freeN(funcstore);
+                       }
+               }
+       }
+}

Modified: trunk/blender/source/blender/python/BPY_extern.h
===================================================================
--- trunk/blender/source/blender/python/BPY_extern.h    2011-06-24 15:10:34 UTC 
(rev 37794)
+++ trunk/blender/source/blender/python/BPY_extern.h    2011-06-24 16:54:30 UTC 
(rev 37795)
@@ -83,6 +83,8 @@
 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);
 

Modified: trunk/blender/source/blender/python/intern/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/python/intern/CMakeLists.txt   2011-06-24 
15:10:34 UTC (rev 37794)
+++ trunk/blender/source/blender/python/intern/CMakeLists.txt   2011-06-24 
16:54:30 UTC (rev 37795)
@@ -42,6 +42,7 @@
 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 @@
 
        bpy.h
        bpy_app.h
+       bpy_app_handlers.h
        bpy_driver.h
        bpy_intern_string.h
        bpy_operator.h

Modified: trunk/blender/source/blender/python/intern/bpy_app.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_app.c        2011-06-24 
15:10:34 UTC (rev 37794)
+++ trunk/blender/source/blender/python/intern/bpy_app.c        2011-06-24 
16:54:30 UTC (rev 37795)
@@ -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 @@
        {(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 @@
        SetStrItem("Unknown");
 #endif
 
+       SetObjItem(BPY_app_handlers_struct());
+
 #undef SetIntItem
 #undef SetStrItem
 #undef SetObjItem

Added: trunk/blender/source/blender/python/intern/bpy_app_handlers.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_app_handlers.c               
                (rev 0)
+++ trunk/blender/source/blender/python/intern/bpy_app_handlers.c       
2011-06-24 16:54:30 UTC (rev 37795)
@@ -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++) {

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to