29 const int pointers_num,
34#define FILEPATH_SAVE_ARG \
35 "Accepts one argument: " \
36 "the file being saved, an empty string for the startup-file."
37#define FILEPATH_LOAD_ARG \
38 "Accepts one argument: " \
39 "the file being loaded, an empty string for the startup-file."
40#define RENDER_STATS_ARG \
41 "Accepts one argument: " \
42 "the render stats (render/saving time plus in background mode frame/used [peak] memory)."
48 "Called after frame change for playback and rendering, before any data is evaluated for the "
49 "new frame. This makes it possible to change data and relations (for example swap an object "
50 "to another mesh) for the new frame. Note that this handler is **not** to be used as 'before "
51 "the frame changes' event. The dependency graph is not available in this handler, as data "
52 "and relations may have been altered and the dependency graph has not yet been updated for "
55 "Called after frame change for playback and rendering, after the data has been evaluated "
56 "for the new frame."},
57 {
"render_pre",
"on render (before)"},
58 {
"render_post",
"on render (after)"},
59 {
"render_write",
"on writing a render frame (directly after the frame is written)"},
61 {
"render_init",
"on initialization of a render job"},
62 {
"render_complete",
"on completion of render job"},
63 {
"render_cancel",
"on canceling a render job"},
67 {
"load_post_fail",
"on failure to load a new blend file (after). " FILEPATH_LOAD_ARG},
71 {
"save_post_fail",
"on failure to save a blend file (after). " FILEPATH_SAVE_ARG},
73 {
"undo_pre",
"on loading an undo step (before)"},
74 {
"undo_post",
"on loading an undo step (after)"},
75 {
"redo_pre",
"on loading a redo step (before)"},
76 {
"redo_post",
"on loading a redo step (after)"},
77 {
"depsgraph_update_pre",
"on depsgraph update (pre)"},
78 {
"depsgraph_update_post",
"on depsgraph update (post)"},
79 {
"version_update",
"on ending the versioning code"},
80 {
"load_factory_preferences_post",
"on loading factory preferences (after)"},
81 {
"load_factory_startup_post",
"on loading factory startup (after)"},
82 {
"xr_session_start_pre",
"on starting an xr session (before)"},
83 {
"annotation_pre",
"on drawing an annotation (before)"},
84 {
"annotation_post",
"on drawing an annotation (after)"},
85 {
"object_bake_pre",
"before starting a bake job"},
86 {
"object_bake_complete",
"on completing a bake job; will be called in the main thread"},
87 {
"object_bake_cancel",
"on canceling a bake job; will be called in the main thread"},
88 {
"composite_pre",
"on a compositing background job (before)"},
89 {
"composite_post",
"on a compositing background job (after)"},
90 {
"composite_cancel",
"on a compositing background job (cancel)"},
91 {
"animation_playback_pre",
"on starting animation playback"},
92 {
"animation_playback_post",
"on ending animation playback"},
93 {
"translation_update_post",
"on translation settings update"},
97 {
"_extension_repos_update_pre",
"on changes to extension repos (before)"},
98 {
"_extension_repos_update_post",
"on changes to extension repos (after)"},
99 {
"_extension_repos_sync",
"on creating or synchronizing the active repository"},
100 {
"_extension_repos_files_clear",
101 "remove files from the repository directory (uses as a string argument)"},
103 "on linking or appending data (before), get a single `BlendImportContext` parameter"},
104 {
"blend_import_post",
105 "on linking or appending data (after), get a single `BlendImportContext` parameter"},
108#define APP_CB_OTHER_FIELDS 1
110 "Function decorator for callback functions not to be removed when loading new files"},
117 "This module contains callback lists",
123# if (BKE_CB_EVT_TOT != ARRAY_SIZE(app_cb_info_fields))
124# error "Callbacks are out of sync"
132#define PERMINENT_CB_ID "_bpy_persistent"
140 if (!PyArg_ParseTuple(args,
"O:bpy.app.handlers.persistent", &value)) {
144 if (PyFunction_Check(value)) {
145 PyObject **dict_ptr = _PyObject_GetDictPtr(value);
146 if (dict_ptr ==
nullptr) {
147 PyErr_SetString(PyExc_ValueError,
148 "bpy.app.handlers.persistent wasn't able to "
149 "get the dictionary from the function passed");
154 if (*dict_ptr ==
nullptr) {
155 *dict_ptr = PyDict_New();
164 PyErr_SetString(PyExc_ValueError,
"bpy.app.handlers.persistent expected a function");
171 PyVarObject_HEAD_INIT(
nullptr, 0)
173 PyVarObject_HEAD_INIT(&PyType_Type, 0)
193 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
231 PyObject *app_cb_info;
235 if (app_cb_info ==
nullptr) {
241 Py_FatalError(
"invalid callback slots 1");
246 Py_FatalError(
"invalid callback slots 2");
264 BLI_assert_msg(0,
"error initializing 'bpy.app.handlers.persistent'");
284 funcstore = &funcstore_array[
pos];
286 funcstore->
alloc = 0;
297 PyGILState_STATE gilstate;
300 gilstate = PyGILState_Ensure();
317 for (i = PyList_GET_SIZE(ls) - 1; i >= 0; i--) {
318 PyObject *item = PyList_GET_ITEM(ls, i);
320 if (PyMethod_Check(item)) {
321 PyObject *item_test = PyMethod_GET_FUNCTION(item);
328 if (PyFunction_Check(item) && (dict_ptr = _PyObject_GetDictPtr(item)) && (*dict_ptr) &&
329 (PyDict_GetItem(*dict_ptr, perm_id_str) !=
nullptr))
336 PyList_SetSlice(ls, i, i + 1,
nullptr);
341 Py_DECREF(perm_id_str);
344 PyGILState_Release(gilstate);
347static PyObject *
choose_arguments(PyObject *func, PyObject *args_all, PyObject *args_single)
349 if (!PyFunction_Check(func)) {
352 PyCodeObject *code = (PyCodeObject *)PyFunction_GetCode(func);
353 if (code->co_argcount == 1) {
362 const int pointers_num,
366 if (PyList_GET_SIZE(cb_list) > 0) {
367 const PyGILState_STATE gilstate = PyGILState_Ensure();
369 const int num_arguments = 2;
370 PyObject *args_all = PyTuple_New(num_arguments);
371 PyObject *args_single = PyTuple_New(1);
377 for (
int i = 0; i < pointers_num; ++i) {
381 for (
int i = pointers_num; i < num_arguments; ++i) {
382 PyTuple_SET_ITEM(args_all, i, Py_NewRef(Py_None));
385 if (pointers_num == 0) {
386 PyTuple_SET_ITEM(args_single, 0, Py_NewRef(Py_None));
395 for (
pos = 0;
pos < PyList_GET_SIZE(cb_list);
pos++) {
396 func = PyList_GET_ITEM(cb_list,
pos);
398 ret = PyObject_Call(func, args,
nullptr);
399 if (
ret ==
nullptr) {
407 PySys_WriteStderr(
"Error in bpy.app.handlers.%s[%d]:\n",
419 Py_DECREF(args_single);
421 PyGILState_Release(gilstate);
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
#define BLI_assert_msg(a, msg)
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
PyObject * BPY_app_handlers_struct()
static PyStructSequence_Desc app_cb_info_desc
#define FILEPATH_SAVE_ARG
#define APP_CB_OTHER_FIELDS
static PyTypeObject BPyPersistent_Type
#define FILEPATH_LOAD_ARG
static PyObject * make_app_cb_info()
void BPY_app_handlers_reset(const bool do_all)
static PyObject * bpy_app_handlers_persistent_new(PyTypeObject *, PyObject *args, PyObject *)
static PyStructSequence_Field app_cb_info_fields[]
static PyObject * py_cb_array[BKE_CB_EVT_TOT]
static PyTypeObject BlenderAppCbType
static PyObject * choose_arguments(PyObject *func, PyObject *args_all, PyObject *args_single)
void bpy_app_generic_callback(Main *main, PointerRNA **pointers, const int pointers_num, void *arg)
PyObject * pyrna_struct_CreatePyObject_with_primitive_support(PointerRNA *ptr)
void(* func)(Main *, PointerRNA **, int num_pointers, void *arg)