patch [#28218] During-render callback functionality
[blender.git] / source / blender / python / intern / bpy_app_handlers.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/python/intern/bpy_app_handlers.c
26  *  \ingroup pythonintern
27  */
28
29 #include <Python.h>
30 #include "BLI_utildefines.h"
31 #include "BLI_callbacks.h"
32
33 #include "RNA_types.h"
34 #include "RNA_access.h"
35 #include "bpy_rna.h"
36 #include "bpy_app_handlers.h"
37
38 void bpy_app_generic_callback(struct Main *main, struct ID *id, void *arg);
39
40 static PyTypeObject BlenderAppCbType;
41
42 static PyStructSequence_Field app_cb_info_fields[]= {
43         {(char *)"render_pre", NULL},
44         {(char *)"render_post", NULL},
45         {(char *)"render_stats", NULL},
46         {(char *)"load_pre", NULL},
47         {(char *)"load_post", NULL},
48         {(char *)"save_pre", NULL},
49         {(char *)"save_post", NULL},
50         {NULL}
51 };
52
53 static PyStructSequence_Desc app_cb_info_desc= {
54         (char *)"bpy.app.handlers",     /* name */
55         (char *)"This module contains callbacks",    /* doc */
56         app_cb_info_fields,    /* fields */
57         (sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field)) - 1
58 };
59
60 /*
61 #if (BLI_CB_EVT_TOT != ((sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field))))
62 #  error "Callbacks are out of sync"
63 #endif
64 */
65
66 static PyObject *py_cb_array[BLI_CB_EVT_TOT]= {0};
67
68 static PyObject *make_app_cb_info(void)
69 {
70         PyObject *app_cb_info;
71         int pos= 0;
72
73         app_cb_info= PyStructSequence_New(&BlenderAppCbType);
74         if (app_cb_info == NULL) {
75                 return NULL;
76         }
77
78         for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
79                 if(app_cb_info_fields[pos].name == NULL) {
80                         Py_FatalError("invalid callback slots 1");
81                 }
82                 PyStructSequence_SET_ITEM(app_cb_info, pos, (py_cb_array[pos]= PyList_New(0)));
83         }
84         if(app_cb_info_fields[pos].name != NULL) {
85                 Py_FatalError("invalid callback slots 2");
86         }
87
88         return app_cb_info;
89 }
90
91 PyObject *BPY_app_handlers_struct(void)
92 {
93         PyObject *ret;
94
95         PyStructSequence_InitType(&BlenderAppCbType, &app_cb_info_desc);
96
97         ret= make_app_cb_info();
98
99         /* prevent user from creating new instances */
100         BlenderAppCbType.tp_init= NULL;
101         BlenderAppCbType.tp_new= NULL;
102
103         /* assign the C callbacks */
104         if(ret) {
105                 static bCallbackFuncStore funcstore_array[BLI_CB_EVT_TOT]= {{0}};
106                 bCallbackFuncStore *funcstore;
107                 int pos= 0;
108
109                 for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
110                         funcstore= &funcstore_array[pos];
111                         funcstore->func= bpy_app_generic_callback;
112                         funcstore->alloc= 0;
113                         funcstore->arg= SET_INT_IN_POINTER(pos);
114                         BLI_add_cb(funcstore, pos);
115                 }
116         }
117
118         return ret;
119 }
120
121 void BPY_app_handlers_reset(void)
122 {
123         int pos= 0;
124
125         for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
126                 PyList_SetSlice(py_cb_array[pos], 0, PY_SSIZE_T_MAX, NULL);
127         }
128 }
129
130 /* the actual callback - not necessarily called from py */
131 void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg)
132 {
133         PyObject *cb_list= py_cb_array[GET_INT_FROM_POINTER(arg)];
134         Py_ssize_t cb_list_len;
135         if((cb_list_len= PyList_GET_SIZE(cb_list)) > 0) {
136                 PyGILState_STATE gilstate= PyGILState_Ensure();
137
138                 PyObject* args= PyTuple_New(1); // save python creating each call
139                 PyObject* func;
140                 PyObject* ret;
141                 Py_ssize_t pos;
142
143                 /* setup arguments */
144                 if(id) {
145                         PointerRNA id_ptr;
146                         RNA_id_pointer_create(id, &id_ptr);
147                         PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr));
148                 }
149                 else {
150                         PyTuple_SET_ITEM(args, 0, Py_None);
151                         Py_INCREF(Py_None);
152                 }
153
154                 // Iterate the list and run the callbacks
155                 for (pos=0; pos < cb_list_len; pos++) {
156                         func= PyList_GET_ITEM(cb_list, pos);
157                         ret= PyObject_Call(func, args, NULL);
158                         if (ret==NULL) {
159                                 PyErr_Print();
160                                 PyErr_Clear();
161                         }
162                         else {
163                                 Py_DECREF(ret);
164                         }
165                 }
166
167                 Py_DECREF(args);
168
169                 PyGILState_Release(gilstate);
170         }
171 }