2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
2025-05-17 09:18:03 +10:00
|
|
|
* This file defines a #PyStructSequence accessed via `bpy.app.handlers`,
|
2011-11-05 08:21:12 +00:00
|
|
|
* which exposes various lists that the script author can add callback
|
2025-05-17 09:18:03 +10:00
|
|
|
* functions into (called via blenders generic BLI_cb API)
|
2011-06-24 16:54:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <Python.h>
|
2019-09-05 15:52:38 +02:00
|
|
|
|
2024-02-09 19:29:34 +01:00
|
|
|
#include "BKE_callbacks.hh"
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2023-08-10 22:40:27 +02:00
|
|
|
#include "RNA_access.hh"
|
2023-03-09 10:46:49 +11:00
|
|
|
|
2024-09-24 12:05:13 +02:00
|
|
|
#include "bpy_app_handlers.hh"
|
2024-09-24 11:30:38 +02:00
|
|
|
#include "bpy_rna.hh"
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2024-09-24 17:07:49 +02:00
|
|
|
#include "BPY_extern.hh"
|
2012-09-15 01:52:28 +00:00
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
void bpy_app_generic_callback(Main *main,
|
2023-06-03 08:36:28 +10:00
|
|
|
PointerRNA **pointers,
|
2022-03-28 11:06:01 +11:00
|
|
|
const int pointers_num,
|
2019-09-09 10:25:04 +02:00
|
|
|
void *arg);
|
2011-06-24 16:54:30 +00:00
|
|
|
|
|
|
|
|
static PyTypeObject BlenderAppCbType;
|
|
|
|
|
|
2023-03-09 10:46:49 +11:00
|
|
|
#define FILEPATH_SAVE_ARG \
|
|
|
|
|
"Accepts one argument: " \
|
|
|
|
|
"the file being saved, an empty string for the startup-file."
|
|
|
|
|
#define FILEPATH_LOAD_ARG \
|
|
|
|
|
"Accepts one argument: " \
|
|
|
|
|
"the file being loaded, an empty string for the startup-file."
|
2024-05-28 17:07:27 +02:00
|
|
|
#define RENDER_STATS_ARG \
|
|
|
|
|
"Accepts one argument: " \
|
|
|
|
|
"the render stats (render/saving time plus in background mode frame/used [peak] memory)."
|
2021-06-02 17:15:05 +10:00
|
|
|
/**
|
2024-02-09 19:29:34 +01:00
|
|
|
* See `BKE_callbacks.hh` #eCbEvent declaration for the policy on naming.
|
2021-06-02 17:15:05 +10:00
|
|
|
*/
|
2011-12-26 12:26:11 +00:00
|
|
|
static PyStructSequence_Field app_cb_info_fields[] = {
|
2020-06-02 16:33:35 +02:00
|
|
|
{"frame_change_pre",
|
|
|
|
|
"Called after frame change for playback and rendering, before any data is evaluated for the "
|
|
|
|
|
"new frame. This makes it possible to change data and relations (for example swap an object "
|
|
|
|
|
"to another mesh) for the new frame. Note that this handler is **not** to be used as 'before "
|
|
|
|
|
"the frame changes' event. The dependency graph is not available in this handler, as data "
|
|
|
|
|
"and relations may have been altered and the dependency graph has not yet been updated for "
|
|
|
|
|
"that."},
|
|
|
|
|
{"frame_change_post",
|
|
|
|
|
"Called after frame change for playback and rendering, after the data has been evaluated "
|
|
|
|
|
"for the new frame."},
|
2019-12-20 10:42:57 +11:00
|
|
|
{"render_pre", "on render (before)"},
|
|
|
|
|
{"render_post", "on render (after)"},
|
|
|
|
|
{"render_write", "on writing a render frame (directly after the frame is written)"},
|
2024-05-28 17:07:27 +02:00
|
|
|
{"render_stats", "on printing render statistics. " RENDER_STATS_ARG},
|
2019-12-20 10:42:57 +11:00
|
|
|
{"render_init", "on initialization of a render job"},
|
|
|
|
|
{"render_complete", "on completion of render job"},
|
|
|
|
|
{"render_cancel", "on canceling a render job"},
|
2023-03-09 10:46:49 +11:00
|
|
|
|
|
|
|
|
{"load_pre", "on loading a new blend file (before)." FILEPATH_LOAD_ARG},
|
|
|
|
|
{"load_post", "on loading a new blend file (after). " FILEPATH_LOAD_ARG},
|
|
|
|
|
{"load_post_fail", "on failure to load a new blend file (after). " FILEPATH_LOAD_ARG},
|
|
|
|
|
|
|
|
|
|
{"save_pre", "on saving a blend file (before). " FILEPATH_SAVE_ARG},
|
|
|
|
|
{"save_post", "on saving a blend file (after). " FILEPATH_SAVE_ARG},
|
|
|
|
|
{"save_post_fail", "on failure to save a blend file (after). " FILEPATH_SAVE_ARG},
|
|
|
|
|
|
2019-12-20 10:42:57 +11:00
|
|
|
{"undo_pre", "on loading an undo step (before)"},
|
|
|
|
|
{"undo_post", "on loading an undo step (after)"},
|
|
|
|
|
{"redo_pre", "on loading a redo step (before)"},
|
|
|
|
|
{"redo_post", "on loading a redo step (after)"},
|
|
|
|
|
{"depsgraph_update_pre", "on depsgraph update (pre)"},
|
|
|
|
|
{"depsgraph_update_post", "on depsgraph update (post)"},
|
|
|
|
|
{"version_update", "on ending the versioning code"},
|
|
|
|
|
{"load_factory_preferences_post", "on loading factory preferences (after)"},
|
|
|
|
|
{"load_factory_startup_post", "on loading factory startup (after)"},
|
2021-05-16 03:33:10 +09:00
|
|
|
{"xr_session_start_pre", "on starting an xr session (before)"},
|
2022-03-07 12:31:36 +01:00
|
|
|
{"annotation_pre", "on drawing an annotation (before)"},
|
|
|
|
|
{"annotation_post", "on drawing an annotation (after)"},
|
Expose background job info to Python
Add `bpy.app.is_job_running(job_type)` as high-level indicator. Job
types currently exposed are `WM_JOB_TYPE_RENDER`,
`WM_JOB_TYPE_RENDER_PREVIEW`, and `WM_JOB_TYPE_OBJECT_BAKE`, as strings
with the `WM_JOB_TYPE_` prefix removed. The functions can be polled by
Python code to determine whether such background work is still ongoing
or not.
Furthermore, new app handles are added for
`object_bake_{pre,complete,canceled}`, which are called respectively
before an object baking job starts, completes sucessfully, and stops due
to a cancellation.
Motivation: There are various cases where Python can trigger the
execution of a background job, without getting notification that that
background job is done. As a result, it's hard to do things like
cleanups, or auto-quitting Blender after the work is done.
The approach in this commit can easily be extended with other job types,
when the need arises. The rendering of asset previews is one that's
likely to be added sooner than later, as there have already been
requests about this.
Reviewed By: campbellbarton
Differential Revision: https://developer.blender.org/D14587
2022-06-02 11:20:17 +02:00
|
|
|
{"object_bake_pre", "before starting a bake job"},
|
|
|
|
|
{"object_bake_complete", "on completing a bake job; will be called in the main thread"},
|
|
|
|
|
{"object_bake_cancel", "on canceling a bake job; will be called in the main thread"},
|
2022-05-31 10:22:43 +02:00
|
|
|
{"composite_pre", "on a compositing background job (before)"},
|
|
|
|
|
{"composite_post", "on a compositing background job (after)"},
|
|
|
|
|
{"composite_cancel", "on a compositing background job (cancel)"},
|
2023-06-26 12:11:25 +02:00
|
|
|
{"animation_playback_pre", "on starting animation playback"},
|
|
|
|
|
{"animation_playback_post", "on ending animation playback"},
|
2024-01-18 11:25:48 +11:00
|
|
|
{"translation_update_post", "on translation settings update"},
|
2023-08-09 20:15:34 +10:00
|
|
|
/* NOTE(@ideasman42): This avoids bad-level calls into BPY API
|
|
|
|
|
* but should not be considered part of the public Python API.
|
|
|
|
|
* If there is a compelling reason to make these public, the leading `_` can be removed. */
|
|
|
|
|
{"_extension_repos_update_pre", "on changes to extension repos (before)"},
|
|
|
|
|
{"_extension_repos_update_post", "on changes to extension repos (after)"},
|
2024-02-06 18:51:01 +11:00
|
|
|
{"_extension_repos_sync", "on creating or synchronizing the active repository"},
|
2024-03-21 11:35:29 +11:00
|
|
|
{"_extension_repos_files_clear",
|
|
|
|
|
"remove files from the repository directory (uses as a string argument)"},
|
Python API: Add link/append pre/post handlers.
The `pre` handler is called after blender internal code is done populating
the link/append context with data to be processed, and before this data
starts being linked from library files.
The `post` handler is called after blender is done linking, and
potentailly appending and/or instantiating, the requested data and all
of their dependencies.
Both handlers are called with a single argument, the link/append
context.
An new RNA sets of wrappers have been added to expose relevant info from
these internal C++ structures.
NOTE: !113658 is very similar (but tied to asset drag & drop), whereas
this PR is more general (these could probably live hand-in-hand / side-
by-side).
Implements #122357
Pull Request: https://projects.blender.org/blender/blender/pulls/128279
-----------------
Some quick py example code:
```python
import bpy
def my_handler_pre(lapp_context):
print("About to {}:\n\t".format("link" if "LINK" in lapp_context.options else "append"),
"\n\t".join("{} '{}', from libs ['{}']".format(item.id_type, item.name,
"', '".join([l.filepath for l in item.source_libraries]))
for item in lapp_context.import_items))
def my_handler_post(lapp_context):
print("{}:\n\t".format("Linked" if "LINK" in lapp_context.options else "Appended"),
"\n\t".join("{} '{}', from lib '{}'".format(item.id.id_type, item.id.name, item.source_library.filepath)
for item in lapp_context.import_items))
bpy.app.handlers.link_append_pre.append(my_handler_pre)
bpy.app.handlers.link_append_post.append(my_handler_post)
```
2024-10-02 16:44:38 +02:00
|
|
|
{"blend_import_pre",
|
|
|
|
|
"on linking or appending data (before), get a single `BlendImportContext` parameter"},
|
|
|
|
|
{"blend_import_post",
|
|
|
|
|
"on linking or appending data (after), get a single `BlendImportContext` parameter"},
|
Expose background job info to Python
Add `bpy.app.is_job_running(job_type)` as high-level indicator. Job
types currently exposed are `WM_JOB_TYPE_RENDER`,
`WM_JOB_TYPE_RENDER_PREVIEW`, and `WM_JOB_TYPE_OBJECT_BAKE`, as strings
with the `WM_JOB_TYPE_` prefix removed. The functions can be polled by
Python code to determine whether such background work is still ongoing
or not.
Furthermore, new app handles are added for
`object_bake_{pre,complete,canceled}`, which are called respectively
before an object baking job starts, completes sucessfully, and stops due
to a cancellation.
Motivation: There are various cases where Python can trigger the
execution of a background job, without getting notification that that
background job is done. As a result, it's hard to do things like
cleanups, or auto-quitting Blender after the work is done.
The approach in this commit can easily be extended with other job types,
when the need arises. The rendering of asset previews is one that's
likely to be added sooner than later, as there have already been
requests about this.
Reviewed By: campbellbarton
Differential Revision: https://developer.blender.org/D14587
2022-06-02 11:20:17 +02:00
|
|
|
|
2011-11-03 06:53:52 +00:00
|
|
|
/* sets the permanent tag */
|
2019-01-03 15:52:07 +11:00
|
|
|
#define APP_CB_OTHER_FIELDS 1
|
2019-12-20 10:42:57 +11:00
|
|
|
{"persistent",
|
|
|
|
|
"Function decorator for callback functions not to be removed when loading new files"},
|
2011-11-03 06:53:52 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
{nullptr},
|
2011-06-24 16:54:30 +00:00
|
|
|
};
|
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
static PyStructSequence_Desc app_cb_info_desc = {
|
2024-08-19 11:37:20 +10:00
|
|
|
/*name*/ "bpy.app.handlers",
|
|
|
|
|
/*doc*/ "This module contains callback lists",
|
|
|
|
|
/*fields*/ app_cb_info_fields,
|
|
|
|
|
/*n_in_sequence*/ ARRAY_SIZE(app_cb_info_fields) - 1,
|
2011-06-24 16:54:30 +00:00
|
|
|
};
|
|
|
|
|
|
2012-03-03 20:36:09 +00:00
|
|
|
#if 0
|
2019-09-05 15:52:38 +02:00
|
|
|
# if (BKE_CB_EVT_TOT != ARRAY_SIZE(app_cb_info_fields))
|
2012-03-03 20:36:09 +00:00
|
|
|
# error "Callbacks are out of sync"
|
|
|
|
|
# endif
|
2011-06-24 16:54:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
2022-12-17 15:58:30 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Permanent Tagging Code
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2011-11-03 09:13:47 +00:00
|
|
|
#define PERMINENT_CB_ID "_bpy_persistent"
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *bpy_app_handlers_persistent_new(PyTypeObject * /*type*/,
|
2011-11-03 09:13:47 +00:00
|
|
|
PyObject *args,
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject * /*kwds*/)
|
2011-11-03 06:53:52 +00:00
|
|
|
{
|
|
|
|
|
PyObject *value;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "O:bpy.app.handlers.persistent", &value)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-03 06:53:52 +00:00
|
|
|
if (PyFunction_Check(value)) {
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject **dict_ptr = _PyObject_GetDictPtr(value);
|
2023-07-21 02:18:59 +02:00
|
|
|
if (dict_ptr == nullptr) {
|
2011-11-03 06:53:52 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2011-11-03 09:13:47 +00:00
|
|
|
"bpy.app.handlers.persistent wasn't able to "
|
2011-11-03 06:53:52 +00:00
|
|
|
"get the dictionary from the function passed");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-11-03 06:53:52 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-07 12:41:06 +02:00
|
|
|
/* set id */
|
2023-07-21 02:18:59 +02:00
|
|
|
if (*dict_ptr == nullptr) {
|
2020-08-07 12:41:06 +02:00
|
|
|
*dict_ptr = PyDict_New();
|
2011-11-03 06:53:52 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-07 12:41:06 +02:00
|
|
|
PyDict_SetItemString(*dict_ptr, PERMINENT_CB_ID, Py_None);
|
|
|
|
|
|
2011-11-03 06:53:52 +00:00
|
|
|
Py_INCREF(value);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, "bpy.app.handlers.persistent expected a function");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-11-03 06:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-07 22:34:35 +11:00
|
|
|
/** Dummy type because decorators can't be a #PyCFunction. */
|
2011-11-03 09:13:47 +00:00
|
|
|
static PyTypeObject BPyPersistent_Type = {
|
2017-05-27 15:34:55 -04:00
|
|
|
#if defined(_MSC_VER)
|
2023-07-21 02:18:59 +02:00
|
|
|
/*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
|
2011-11-03 12:01:18 +00:00
|
|
|
#else
|
2023-07-16 11:29:50 -06:00
|
|
|
/*ob_base*/ PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2011-11-03 12:01:18 +00:00
|
|
|
#endif
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_name*/ "persistent",
|
|
|
|
|
/*tp_basicsize*/ 0,
|
|
|
|
|
/*tp_itemsize*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_dealloc*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_vectorcall_offset*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_getattr*/ nullptr,
|
|
|
|
|
/*tp_setattr*/ nullptr,
|
|
|
|
|
/*tp_as_async*/ nullptr,
|
|
|
|
|
/*tp_repr*/ nullptr,
|
|
|
|
|
/*tp_as_number*/ nullptr,
|
|
|
|
|
/*tp_as_sequence*/ nullptr,
|
|
|
|
|
/*tp_as_mapping*/ nullptr,
|
|
|
|
|
/*tp_hash*/ nullptr,
|
|
|
|
|
/*tp_call*/ nullptr,
|
|
|
|
|
/*tp_str*/ nullptr,
|
|
|
|
|
/*tp_getattro*/ nullptr,
|
|
|
|
|
/*tp_setattro*/ nullptr,
|
|
|
|
|
/*tp_as_buffer*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_doc*/ nullptr,
|
|
|
|
|
/*tp_traverse*/ nullptr,
|
|
|
|
|
/*tp_clear*/ nullptr,
|
|
|
|
|
/*tp_richcompare*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_weaklistoffset*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_iter*/ nullptr,
|
|
|
|
|
/*tp_iternext*/ nullptr,
|
|
|
|
|
/*tp_methods*/ nullptr,
|
|
|
|
|
/*tp_members*/ nullptr,
|
|
|
|
|
/*tp_getset*/ nullptr,
|
|
|
|
|
/*tp_base*/ nullptr,
|
|
|
|
|
/*tp_dict*/ nullptr,
|
|
|
|
|
/*tp_descr_get*/ nullptr,
|
|
|
|
|
/*tp_descr_set*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_dictoffset*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_init*/ nullptr,
|
|
|
|
|
/*tp_alloc*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_new*/ bpy_app_handlers_persistent_new,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_free*/ nullptr,
|
|
|
|
|
/*tp_is_gc*/ nullptr,
|
|
|
|
|
/*tp_bases*/ nullptr,
|
|
|
|
|
/*tp_mro*/ nullptr,
|
|
|
|
|
/*tp_cache*/ nullptr,
|
|
|
|
|
/*tp_subclasses*/ nullptr,
|
|
|
|
|
/*tp_weaklist*/ nullptr,
|
|
|
|
|
/*tp_del*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_version_tag*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_finalize*/ nullptr,
|
|
|
|
|
/*tp_vectorcall*/ nullptr,
|
2011-11-03 09:13:47 +00:00
|
|
|
};
|
2011-11-03 06:53:52 +00:00
|
|
|
|
2022-12-17 15:58:30 +11:00
|
|
|
/** \} */
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *py_cb_array[BKE_CB_EVT_TOT] = {nullptr};
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
static PyObject *make_app_cb_info()
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
|
|
|
|
PyObject *app_cb_info;
|
2014-05-11 16:22:05 +10:00
|
|
|
int pos;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
app_cb_info = PyStructSequence_New(&BlenderAppCbType);
|
2023-07-21 02:18:59 +02:00
|
|
|
if (app_cb_info == nullptr) {
|
|
|
|
|
return nullptr;
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
for (pos = 0; pos < BKE_CB_EVT_TOT; pos++) {
|
2023-07-21 02:18:59 +02:00
|
|
|
if (app_cb_info_fields[pos].name == nullptr) {
|
2011-06-24 16:54:30 +00:00
|
|
|
Py_FatalError("invalid callback slots 1");
|
|
|
|
|
}
|
2011-12-26 12:26:11 +00:00
|
|
|
PyStructSequence_SET_ITEM(app_cb_info, pos, (py_cb_array[pos] = PyList_New(0)));
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
2023-07-21 02:18:59 +02:00
|
|
|
if (app_cb_info_fields[pos + APP_CB_OTHER_FIELDS].name != nullptr) {
|
2011-06-24 16:54:30 +00:00
|
|
|
Py_FatalError("invalid callback slots 2");
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-03 06:53:52 +00:00
|
|
|
/* custom function */
|
2011-11-03 09:13:47 +00:00
|
|
|
PyStructSequence_SET_ITEM(app_cb_info, pos++, (PyObject *)&BPyPersistent_Type);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-06-24 16:54:30 +00:00
|
|
|
return app_cb_info;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
PyObject *BPY_app_handlers_struct()
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
|
|
|
|
PyObject *ret;
|
|
|
|
|
|
2017-05-27 15:34:55 -04:00
|
|
|
#if defined(_MSC_VER)
|
2011-12-26 12:26:11 +00:00
|
|
|
BPyPersistent_Type.ob_base.ob_base.ob_type = &PyType_Type;
|
2011-11-03 12:01:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
2011-11-03 09:13:47 +00:00
|
|
|
if (PyType_Ready(&BPyPersistent_Type) < 0) {
|
2021-07-15 18:23:28 +10:00
|
|
|
BLI_assert_msg(0, "error initializing 'bpy.app.handlers.persistent'");
|
2011-11-03 09:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-24 16:54:30 +00:00
|
|
|
PyStructSequence_InitType(&BlenderAppCbType, &app_cb_info_desc);
|
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
ret = make_app_cb_info();
|
2011-06-24 16:54:30 +00:00
|
|
|
|
|
|
|
|
/* prevent user from creating new instances */
|
2023-07-21 02:18:59 +02:00
|
|
|
BlenderAppCbType.tp_init = nullptr;
|
|
|
|
|
BlenderAppCbType.tp_new = nullptr;
|
2023-05-24 11:21:18 +10:00
|
|
|
/* Without this we can't do `set(sys.modules)` #29635. */
|
|
|
|
|
BlenderAppCbType.tp_hash = (hashfunc)_Py_HashPointer;
|
2011-06-24 16:54:30 +00:00
|
|
|
|
|
|
|
|
/* assign the C callbacks */
|
2011-10-13 01:29:08 +00:00
|
|
|
if (ret) {
|
2023-07-21 02:18:59 +02:00
|
|
|
static bCallbackFuncStore funcstore_array[BKE_CB_EVT_TOT] = {{nullptr}};
|
2011-06-24 16:54:30 +00:00
|
|
|
bCallbackFuncStore *funcstore;
|
2011-12-26 12:26:11 +00:00
|
|
|
int pos = 0;
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
for (pos = 0; pos < BKE_CB_EVT_TOT; pos++) {
|
2011-12-26 12:26:11 +00:00
|
|
|
funcstore = &funcstore_array[pos];
|
|
|
|
|
funcstore->func = bpy_app_generic_callback;
|
|
|
|
|
funcstore->alloc = 0;
|
2018-09-19 12:05:58 +10:00
|
|
|
funcstore->arg = POINTER_FROM_INT(pos);
|
2023-07-21 02:18:59 +02:00
|
|
|
BKE_callback_add(funcstore, eCbEvent(pos));
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 14:25:58 +10:00
|
|
|
void BPY_app_handlers_reset(const bool do_all)
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
2011-12-26 12:26:11 +00:00
|
|
|
int pos = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-03 06:53:52 +00:00
|
|
|
if (do_all) {
|
2019-09-05 15:52:38 +02:00
|
|
|
for (pos = 0; pos < BKE_CB_EVT_TOT; pos++) {
|
2011-11-03 06:53:52 +00:00
|
|
|
/* clear list */
|
2023-07-21 02:18:59 +02:00
|
|
|
PyList_SetSlice(py_cb_array[pos], 0, PY_SSIZE_T_MAX, nullptr);
|
2011-11-03 06:53:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* save string conversion thrashing */
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *perm_id_str = PyUnicode_FromString(PERMINENT_CB_ID);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
for (pos = 0; pos < BKE_CB_EVT_TOT; pos++) {
|
2011-11-03 06:53:52 +00:00
|
|
|
/* clear only items without PERMINENT_CB_ID */
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *ls = py_cb_array[pos];
|
2011-11-03 06:53:52 +00:00
|
|
|
Py_ssize_t i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
for (i = PyList_GET_SIZE(ls) - 1; i >= 0; i--) {
|
2022-04-26 13:46:06 +10:00
|
|
|
PyObject *item = PyList_GET_ITEM(ls, i);
|
|
|
|
|
|
|
|
|
|
if (PyMethod_Check(item)) {
|
|
|
|
|
PyObject *item_test = PyMethod_GET_FUNCTION(item);
|
|
|
|
|
if (item_test) {
|
|
|
|
|
item = item_test;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-26 13:46:06 +10:00
|
|
|
PyObject **dict_ptr;
|
|
|
|
|
if (PyFunction_Check(item) && (dict_ptr = _PyObject_GetDictPtr(item)) && (*dict_ptr) &&
|
2023-07-21 02:18:59 +02:00
|
|
|
(PyDict_GetItem(*dict_ptr, perm_id_str) != nullptr))
|
2012-03-16 21:39:56 +00:00
|
|
|
{
|
2011-11-03 06:53:52 +00:00
|
|
|
/* keep */
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* remove */
|
2022-03-08 13:48:31 +11:00
|
|
|
// PySequence_DelItem(ls, i); /* more obvious but slower */
|
2023-07-21 02:18:59 +02:00
|
|
|
PyList_SetSlice(ls, i, i + 1, nullptr);
|
2011-11-03 06:53:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-03 06:53:52 +00:00
|
|
|
Py_DECREF(perm_id_str);
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-10-13 17:44:27 +02:00
|
|
|
PyGILState_Release(gilstate);
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 10:25:04 +02:00
|
|
|
static PyObject *choose_arguments(PyObject *func, PyObject *args_all, PyObject *args_single)
|
|
|
|
|
{
|
|
|
|
|
if (!PyFunction_Check(func)) {
|
|
|
|
|
return args_all;
|
|
|
|
|
}
|
|
|
|
|
PyCodeObject *code = (PyCodeObject *)PyFunction_GetCode(func);
|
|
|
|
|
if (code->co_argcount == 1) {
|
|
|
|
|
return args_single;
|
|
|
|
|
}
|
|
|
|
|
return args_all;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-24 16:54:30 +00:00
|
|
|
/* the actual callback - not necessarily called from py */
|
2023-07-21 10:59:54 +10:00
|
|
|
void bpy_app_generic_callback(Main * /*main*/,
|
2023-06-03 08:36:28 +10:00
|
|
|
PointerRNA **pointers,
|
2022-03-28 11:06:01 +11:00
|
|
|
const int pointers_num,
|
2019-09-09 10:25:04 +02:00
|
|
|
void *arg)
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
2018-09-19 12:05:58 +10:00
|
|
|
PyObject *cb_list = py_cb_array[POINTER_AS_INT(arg)];
|
2012-03-20 20:37:40 +00:00
|
|
|
if (PyList_GET_SIZE(cb_list) > 0) {
|
2020-08-20 16:10:13 +10:00
|
|
|
const PyGILState_STATE gilstate = PyGILState_Ensure();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-09 10:25:04 +02:00
|
|
|
const int num_arguments = 2;
|
|
|
|
|
PyObject *args_all = PyTuple_New(num_arguments); /* save python creating each call */
|
|
|
|
|
PyObject *args_single = PyTuple_New(1);
|
2012-03-16 21:39:56 +00:00
|
|
|
PyObject *func;
|
|
|
|
|
PyObject *ret;
|
2011-06-24 16:54:30 +00:00
|
|
|
Py_ssize_t pos;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-06-24 16:54:30 +00:00
|
|
|
/* setup arguments */
|
2022-03-28 11:06:01 +11:00
|
|
|
for (int i = 0; i < pointers_num; ++i) {
|
2023-03-09 10:46:49 +11:00
|
|
|
PyTuple_SET_ITEM(
|
|
|
|
|
args_all, i, pyrna_struct_CreatePyObject_with_primitive_support(pointers[i]));
|
2019-09-09 10:25:04 +02:00
|
|
|
}
|
2022-03-28 11:06:01 +11:00
|
|
|
for (int i = pointers_num; i < num_arguments; ++i) {
|
2024-10-12 00:20:55 +11:00
|
|
|
PyTuple_SET_ITEM(args_all, i, Py_NewRef(Py_None));
|
2019-09-09 10:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (pointers_num == 0) {
|
2024-10-12 00:20:55 +11:00
|
|
|
PyTuple_SET_ITEM(args_single, 0, Py_NewRef(Py_None));
|
2019-09-09 10:25:04 +02:00
|
|
|
}
|
2011-06-24 16:54:30 +00:00
|
|
|
else {
|
2023-03-09 10:46:49 +11:00
|
|
|
PyTuple_SET_ITEM(
|
|
|
|
|
args_single, 0, pyrna_struct_CreatePyObject_with_primitive_support(pointers[0]));
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-20 20:37:40 +00:00
|
|
|
/* Iterate the list and run the callbacks
|
2021-07-03 23:08:40 +10:00
|
|
|
* NOTE: don't store the list size since the scripts may remove themselves. */
|
2012-03-20 20:37:40 +00:00
|
|
|
for (pos = 0; pos < PyList_GET_SIZE(cb_list); pos++) {
|
2011-12-26 12:26:11 +00:00
|
|
|
func = PyList_GET_ITEM(cb_list, pos);
|
2019-09-09 10:25:04 +02:00
|
|
|
PyObject *args = choose_arguments(func, args_all, args_single);
|
2023-07-21 02:18:59 +02:00
|
|
|
ret = PyObject_Call(func, args, nullptr);
|
|
|
|
|
if (ret == nullptr) {
|
2016-10-20 13:32:52 +02:00
|
|
|
/* Don't set last system variables because they might cause some
|
|
|
|
|
* dangling pointers to external render engines (when exception
|
|
|
|
|
* happens during rendering) which will break logic of render pipeline
|
|
|
|
|
* which expects to be the only user of render engine when rendering
|
2024-01-19 15:44:27 +11:00
|
|
|
* is finished. */
|
|
|
|
|
|
|
|
|
|
/* Note the handler called, the exception itself typically has the function name. */
|
|
|
|
|
PySys_WriteStderr("Error in bpy.app.handlers.%s[%d]:\n",
|
|
|
|
|
app_cb_info_fields[POINTER_AS_INT(arg)].name,
|
|
|
|
|
int(pos));
|
2016-10-20 13:32:52 +02:00
|
|
|
PyErr_PrintEx(0);
|
2011-06-24 16:54:30 +00:00
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Py_DECREF(ret);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-09 10:25:04 +02:00
|
|
|
Py_DECREF(args_all);
|
2019-09-11 15:52:07 +02:00
|
|
|
Py_DECREF(args_single);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-06-24 16:54:30 +00:00
|
|
|
PyGILState_Release(gilstate);
|
|
|
|
|
}
|
|
|
|
|
}
|