31 const int pointers_num,
36#define FILEPATH_SAVE_ARG \
37 "Accepts one argument: " \
38 "the file being saved, an empty string for the startup-file."
39#define PYDOC_FILEPATH_SAVE_TYPE "\n\n:type: list[Callable[[str], None]]"
41#define FILEPATH_LOAD_ARG \
42 "Accepts one argument: " \
43 "the file being loaded, an empty string for the startup-file."
44#define PYDOC_FILEPATH_LOAD_TYPE "\n\n:type: list[Callable[[str], None]]"
46#define RENDER_STATS_ARG \
47 "Accepts one argument: " \
48 "the render progress as a string containing current frame, current sample, render time and " \
50#define PYDOC_RENDER_STATS_TYPE "\n\n:type: list[Callable[[str], None]]"
52#define DEPSGRAPH_UPDATE_ARG \
53 "Accepts one or two arguments: " \
54 "The scene data-block, and optionally the dependency graph being updated"
55#define PYDOC_DEPSGRAPH_UPDATE_TYPE \
57 "Callable[[bpy.types.Scene, bpy.types.Depsgraph], None] | " \
58 "Callable[[bpy.types.Scene], None]" \
62 "Accepts one argument: " \
63 "the scene data-block being rendered"
64#define PYDOC_RENDER_TYPE "\n\n:type: list[Callable[[bpy.types.Scene], None]]"
66#define OBJECT_BAKE_ARG \
67 "Accepts one argument: " \
68 "the object data-block being baked"
69#define PYDOC_OBJECT_BAKE_TYPE "\n\n:type: list[Callable[[bpy.types.Object], None]]"
71#define COMPOSITE_ARG \
72 "Accepts one argument: " \
73 "the scene data-block"
74#define PYDOC_COMPOSITE_TYPE "\n\n:type: list[Callable[[bpy.types.Scene], None]]"
76#define ANNOTATION_ARG \
77 "Accepts two arguments: " \
78 "the annotation data-block and dependency graph"
79#define PYDOC_ANNOTATION_TYPE \
80 "\n\n:type: list[Callable[[bpy.types.GreasePencil, bpy.types.Depsgraph], None]]"
82#define BLENDIMPORT_ARG \
83 "Accepts one argument: " \
84 "a BlendImportContext"
85#define PYDOC_BLENDIMPORT_TYPE "\n\n:type: list[Callable[[bpy.types.BlendImportContext], None]]"
88 "Accepts one argument: " \
89 "the scene data-block"
90#define PYDOC_SCENE_TYPE "\n\n:type: list[Callable[[bpy.types.Scene], None]]"
91#define PYDOC_HANDLER_TYPE_NONE "\n\n:type: list[Callable[[], None]]"
92#define PYDOC_HANDLER_TYPE_BOOL "\n\n:type: list[Callable[[bool], None]]"
99 "Called when a frame change is triggered for playback and rendering, "
100 "before any data is evaluated for the new frame. "
101 "This makes it possible to change data and relations (for example swap an object "
102 "to another mesh) for the new frame. Note that this handler is **not** to be used as 'before "
103 "the frame changes' event. The dependency graph is not available in this handler, as data "
104 "and relations may have been altered and the dependency graph has not yet been updated for "
106 {
"frame_change_post",
107 "Called after frame change for playback and rendering, after the data has been evaluated "
112 "on writing a render frame (directly after the frame is written). " RENDER_ARG
135 {
"depsgraph_update_pre",
137 {
"depsgraph_update_post",
140 {
"load_factory_preferences_post",
147 {
"object_bake_complete",
148 "on completing a bake job; will be called in the main "
150 {
"object_bake_cancel",
151 "on canceling a bake job; will be called in the main "
159 {
"animation_playback_pre",
161 {
"animation_playback_post",
167 {
"_extension_repos_update_pre",
"on changes to extension repos (before)"},
168 {
"_extension_repos_update_post",
"on changes to extension repos (after)"},
169 {
"_extension_repos_sync",
"on creating or synchronizing the active repository"},
170 {
"_extension_repos_files_clear",
171 "remove files from the repository directory (uses as a string argument)"},
174 {
"blend_import_post",
177 "just before Blender shuts down, while all data is still valid. "
178 "Accepts one boolean argument. True indicates either that a user has been using Blender and "
179 "exited, or that Blender is exiting in a circumstance that should be treated as if that were "
180 "the case. False indicates that Blender is running in background mode, or is exiting due to "
184#define APP_CB_OTHER_FIELDS 1
186 "Function decorator for callback functions not to be removed when loading new files"
194 ".. include:: include__bpy_app_handlers.rst\n",
200# if (BKE_CB_EVT_TOT != ARRAY_SIZE(app_cb_info_fields))
201# error "Callbacks are out of sync"
209#define PERMINENT_CB_ID "_bpy_persistent"
217 if (!PyArg_ParseTuple(args,
219 ":bpy.app.handlers.persistent",
225 if (PyFunction_Check(value)) {
226 PyObject **dict_ptr = _PyObject_GetDictPtr(value);
227 if (dict_ptr ==
nullptr) {
228 PyErr_SetString(PyExc_ValueError,
229 "bpy.app.handlers.persistent wasn't able to "
230 "get the dictionary from the function passed");
235 if (*dict_ptr ==
nullptr) {
236 *dict_ptr = PyDict_New();
245 PyErr_SetString(PyExc_ValueError,
"bpy.app.handlers.persistent expected a function");
252 PyVarObject_HEAD_INIT(
nullptr, 0)
254 PyVarObject_HEAD_INIT(&PyType_Type, 0)
274 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
312 PyObject *app_cb_info;
316 if (app_cb_info ==
nullptr) {
322 Py_FatalError(
"invalid callback slots 1");
324 PyStructSequence_SET_ITEM(app_cb_info, pos, (
py_cb_array[pos] = PyList_New(0)));
327 Py_FatalError(
"invalid callback slots 2");
331 PyStructSequence_SET_ITEM(app_cb_info, pos++, Py_NewRef((PyObject *)&
BPyPersistent_Type));
345 BLI_assert_msg(0,
"error initializing 'bpy.app.handlers.persistent'");
365 funcstore = &funcstore_array[pos];
367 funcstore->
alloc = 0;
378 PyGILState_STATE gilstate = PyGILState_Ensure();
384 PyList_SetSlice(
py_cb_array[pos], 0, PY_SSIZE_T_MAX,
nullptr);
396 for (i = PyList_GET_SIZE(ls) - 1; i >= 0; i--) {
397 PyObject *item = PyList_GET_ITEM(ls, i);
399 if (PyMethod_Check(item)) {
400 PyObject *item_test = PyMethod_GET_FUNCTION(item);
407 if (PyFunction_Check(item) && (dict_ptr = _PyObject_GetDictPtr(item)) && (*dict_ptr) &&
408 (PyDict_GetItem(*dict_ptr, perm_id_str) !=
nullptr))
415 PyList_SetSlice(ls, i, i + 1,
nullptr);
420 Py_DECREF(perm_id_str);
423 PyGILState_Release(gilstate);
426static PyObject *
choose_arguments(PyObject *func, PyObject *args_all, PyObject *args_single)
428 if (!PyFunction_Check(func)) {
431 PyCodeObject *code =
reinterpret_cast<PyCodeObject *
>(PyFunction_GetCode(func));
432 if (code->co_argcount == 1) {
441 const int pointers_num,
448 if (PyList_GET_SIZE(cb_list) != 0) {
449 const PyGILState_STATE gilstate = PyGILState_Ensure();
451 const int num_arguments = 2;
452 PyObject *args_all = PyTuple_New(num_arguments);
453 PyObject *args_single = PyTuple_New(1);
456 for (
int i = 0; i < pointers_num; ++i) {
460 for (
int i = pointers_num; i < num_arguments; ++i) {
461 PyTuple_SET_ITEM(args_all, i, Py_NewRef(Py_None));
464 if (pointers_num == 0) {
465 PyTuple_SET_ITEM(args_single, 0, Py_NewRef(Py_None));
474 Py_ssize_t cb_list_num = PyList_GET_SIZE(cb_list);
475 for (Py_ssize_t pos = 0; pos < cb_list_num; pos++) {
476 PyObject *func = PyList_GET_ITEM(cb_list, pos);
478 PyObject *
ret = PyObject_Call(func, args,
nullptr);
479 if (
ret ==
nullptr) {
502 const Py_ssize_t cb_list_num_next = PyList_GET_SIZE(cb_list);
503 if (cb_list_num != cb_list_num_next) [[unlikely]] {
504 if (cb_list_num_next + 1 == cb_list_num) {
506 if (!((pos < cb_list_num_next) && (PyList_GET_ITEM(cb_list, pos) == func))) {
510 else if (cb_list_num_next < cb_list_num) {
513 "Warning in bpy.app.handlers.%s[%d]:\n"
514 "Handlers must only ever remove themselves!\n",
519 cb_list_num = cb_list_num_next;
524 Py_DECREF(args_single);
526 PyGILState_Release(gilstate);
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define BLI_assert_msg(a, msg)
#define PYDOC_RENDER_STATS_TYPE
#define PYDOC_ANNOTATION_TYPE
#define PYDOC_OBJECT_BAKE_TYPE
#define PYDOC_HANDLER_TYPE_BOOL
#define FILEPATH_SAVE_ARG
#define APP_CB_OTHER_FIELDS
#define FILEPATH_LOAD_ARG
#define DEPSGRAPH_UPDATE_ARG
#define PYDOC_HANDLER_TYPE_NONE
#define PYDOC_FILEPATH_LOAD_TYPE
#define PYDOC_RENDER_TYPE
#define PYDOC_FILEPATH_SAVE_TYPE
#define PYDOC_COMPOSITE_TYPE
#define PYDOC_DEPSGRAPH_UPDATE_TYPE
#define PYDOC_BLENDIMPORT_TYPE
static PyTypeObject BlenderAppCbType
static PyStructSequence_Desc app_cb_info_desc
void BPY_app_handlers_reset(bool do_all)
void bpy_app_generic_callback(Main *main, PointerRNA **pointers, const int pointers_num, void *arg)
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
static PyStructSequence_Field app_cb_info_fields[]
static PyObject * make_app_cb_info()
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
PyObject * BPY_app_handlers_struct()
static PyObject * choose_arguments(PyObject *func, PyObject *args_all, PyObject *args_single)
PyObject * pyrna_struct_CreatePyObject_with_primitive_support(PointerRNA *ptr)
static PyObject * bpy_app_handlers_persistent_new(PyTypeObject *, PyObject *args, PyObject *)
static PyTypeObject BPyPersistent_Type
static PyObject * py_cb_array[BKE_CB_EVT_TOT]
header-only compatibility defines.
void(*) func(Main *, PointerRNA **, int pointers_num, void *arg)