own patch [#27752] Python Callback (Scriptlink 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 *)"load_pre", NULL},
46         {(char *)"load_post", NULL},
47     {(char *)"save_pre", NULL},
48         {(char *)"save_post", NULL},
49         {NULL}
50 };
51
52 static PyStructSequence_Desc app_cb_info_desc= {
53         (char *)"bpy.app.handlers",     /* name */
54         (char *)"This module contains callbacks",    /* doc */
55         app_cb_info_fields,    /* fields */
56         (sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field)) - 1
57 };
58
59 /*
60 #if (BLI_CB_EVT_TOT != ((sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field))))
61 #  error "Callbacks are out of sync"
62 #endif
63 */
64
65 static PyObject *py_cb_array[BLI_CB_EVT_TOT]= {0};
66
67 static PyObject *make_app_cb_info(void)
68 {
69         PyObject *app_cb_info;
70         int pos= 0;
71
72         app_cb_info= PyStructSequence_New(&BlenderAppCbType);
73         if (app_cb_info == NULL) {
74                 return NULL;
75         }
76
77         for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
78                 if(app_cb_info_fields[pos].name == NULL) {
79                         Py_FatalError("invalid callback slots 1");
80                 }
81                 PyStructSequence_SET_ITEM(app_cb_info, pos, (py_cb_array[pos]= PyList_New(0)));
82         }
83         if(app_cb_info_fields[pos].name != NULL) {
84                 Py_FatalError("invalid callback slots 2");
85         }
86
87         return app_cb_info;
88 }
89
90 PyObject *BPY_app_handlers_struct(void)
91 {
92         PyObject *ret;
93
94         PyStructSequence_InitType(&BlenderAppCbType, &app_cb_info_desc);
95
96         ret= make_app_cb_info();
97
98         /* prevent user from creating new instances */
99         BlenderAppCbType.tp_init= NULL;
100         BlenderAppCbType.tp_new= NULL;
101
102         /* assign the C callbacks */
103         if(ret) {
104                 static bCallbackFuncStore funcstore_array[BLI_CB_EVT_TOT]= {{0}};
105                 bCallbackFuncStore *funcstore;
106                 int pos= 0;
107
108                 for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
109                         funcstore= &funcstore_array[pos];
110                         funcstore->func= bpy_app_generic_callback;
111                         funcstore->alloc= 0;
112                         funcstore->arg= SET_INT_IN_POINTER(pos);
113                         BLI_add_cb(funcstore, pos);
114                 }
115         }
116
117         return ret;
118 }
119
120 void BPY_app_handlers_reset(void)
121 {
122         int pos= 0;
123
124         for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) {
125                 PyList_SetSlice(py_cb_array[pos], 0, PY_SSIZE_T_MAX, NULL);
126         }
127 }
128
129 /* the actual callback - not necessarily called from py */
130 void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg)
131 {
132         PyObject *cb_list= py_cb_array[GET_INT_FROM_POINTER(arg)];
133         Py_ssize_t cb_list_len;
134         if((cb_list_len= PyList_GET_SIZE(cb_list)) > 0) {
135                 PyGILState_STATE gilstate= PyGILState_Ensure();
136
137                 PyObject* args= PyTuple_New(1); // save python creating each call
138                 PyObject* func;
139                 PyObject* ret;
140                 Py_ssize_t pos;
141
142                 /* setup arguments */
143                 if(id) {
144                         PointerRNA id_ptr;
145                         RNA_id_pointer_create(id, &id_ptr);
146                         PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr));
147                 }
148                 else {
149                         PyTuple_SET_ITEM(args, 0, Py_None);
150                         Py_INCREF(Py_None);
151                 }
152
153                 // Iterate the list and run the callbacks
154                 for (pos=0; pos < cb_list_len; pos++) {
155                         func= PyList_GET_ITEM(cb_list, pos);
156                         ret= PyObject_Call(func, args, NULL);
157                         if (ret==NULL) {
158                                 PyErr_Print();
159                                 PyErr_Clear();
160                         }
161                         else {
162                                 Py_DECREF(ret);
163                         }
164                 }
165
166                 Py_DECREF(args);
167
168                 PyGILState_Release(gilstate);
169         }
170 }