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