Blender V5.3
bpy_app_timers.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_timer.hh"
10
11#include <Python.h>
12
13#include <algorithm>
14
15#include "bpy_app_timers.hh"
16
18#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
19
20namespace blender {
21
22static double handle_returned_value(PyObject *function, PyObject *ret)
23{
24 if (ret == Py_None) {
25 return -1;
26 }
27
28 double value = PyFloat_AsDouble(ret);
29 if (value == -1.0f && PyErr_Occurred()) {
30 PyErr_Clear();
31 printf("Error: 'bpy.app.timers' callback ");
32 PyObject_Print(function, stdout, Py_PRINT_RAW);
33 printf(" did not return None or float.\n");
34 return -1;
35 }
36
37 value = std::max(value, 0.0);
38
39 return value;
40}
41
42static double py_timer_execute(uintptr_t /*uuid*/, void *user_data)
43{
44 PyGILState_STATE gilstate = PyGILState_Ensure();
45
46 PyObject *function = static_cast<PyObject *>(user_data);
47
48 double ret;
49 if (PyObject *py_ret = PyObject_CallObject(function, nullptr)) {
50 ret = handle_returned_value(function, py_ret);
51 Py_DECREF(py_ret);
52 }
53 else {
54 PyErr_PrintEx(0);
55 ret = -1;
56 }
57
58 PyGILState_Release(gilstate);
59
60 return ret;
61}
62
63static void py_timer_free(uintptr_t /*uuid*/, void *user_data)
64{
65 PyGILState_STATE gilstate = PyGILState_Ensure();
66
67 PyObject *function = static_cast<PyObject *>(user_data);
68 Py_DECREF(function);
69
70 PyGILState_Release(gilstate);
71}
72
74 /* Wrap. */
75 bpy_app_timers_register_doc,
76 ".. function:: register(function, *, first_interval=0, persistent=False)\n"
77 "\n"
78 " Add a new function that will be called after the specified amount of seconds.\n"
79 " The function gets no arguments and is expected to return either None or a float.\n"
80 " If ``None`` is returned, the timer will be unregistered.\n"
81 " A returned number specifies the delay until the function is called again.\n"
82 " ``functools.partial`` can be used to assign some parameters.\n"
83 "\n"
84 " :param function: The function that should called.\n"
85 " :type function: Callable[[], float | None]\n"
86 " :param first_interval: Seconds until the callback should be called the first time.\n"
87 " :type first_interval: float\n"
88 " :param persistent: Don't remove timer when a new file is loaded.\n"
89 " :type persistent: bool\n");
90static PyObject *bpy_app_timers_register(PyObject * /*self*/, PyObject *args, PyObject *kw)
91{
92 PyObject *function;
93 double first_interval = 0;
94 int persistent = false;
95
96 static const char *_keywords[] = {"function", "first_interval", "persistent", nullptr};
97 static _PyArg_Parser _parser = {
98 "O" /* `function` */
99 "|$" /* Optional, keyword only arguments. */
100 "d" /* `first_interval` */
101 "p" /* `persistent` */
102 ":register",
103 _keywords,
104 nullptr,
105 };
106 if (!_PyArg_ParseTupleAndKeywordsFast(
107 args, kw, &_parser, &function, &first_interval, &persistent))
108 {
109 return nullptr;
110 }
111
112 if (!PyCallable_Check(function)) {
113 PyErr_SetString(PyExc_TypeError, "function is not callable");
114 return nullptr;
115 }
116
117 Py_INCREF(function);
119 intptr_t(function), py_timer_execute, function, py_timer_free, first_interval, persistent);
120 Py_RETURN_NONE;
121}
122
124 /* Wrap. */
125 bpy_app_timers_unregister_doc,
126 ".. function:: unregister(function)\n"
127 "\n"
128 " Unregister timer.\n"
129 "\n"
130 " :param function: Function to unregister.\n"
131 " :type function: Callable[[], float | None]\n");
132static PyObject *bpy_app_timers_unregister(PyObject * /*self*/, PyObject *function)
133{
134 if (!BLI_timer_unregister(intptr_t(function))) {
135 PyErr_SetString(PyExc_ValueError, "function is not registered");
136 return nullptr;
137 }
138 Py_RETURN_NONE;
139}
140
142 /* Wrap. */
143 bpy_app_timers_is_registered_doc,
144 ".. function:: is_registered(function)\n"
145 "\n"
146 " Check if this function is registered as a timer.\n"
147 "\n"
148 " :param function: Function to check.\n"
149 " :type function: Callable[[], float | None]\n"
150 " :return: True when this function is registered, otherwise False.\n"
151 " :rtype: bool\n");
152static PyObject *bpy_app_timers_is_registered(PyObject * /*self*/, PyObject *function)
153{
154 const bool ret = BLI_timer_is_registered(intptr_t(function));
155 return PyBool_FromLong(ret);
156}
157
158#ifdef __GNUC__
159# ifdef __clang__
160# pragma clang diagnostic push
161# pragma clang diagnostic ignored "-Wcast-function-type"
162# else
163# pragma GCC diagnostic push
164# pragma GCC diagnostic ignored "-Wcast-function-type"
165# endif
166#endif
167
168static PyMethodDef M_AppTimers_methods[] = {
169 {"register",
170 reinterpret_cast<PyCFunction>(bpy_app_timers_register),
171 METH_VARARGS | METH_KEYWORDS,
172 bpy_app_timers_register_doc},
173 {"unregister",
174 static_cast<PyCFunction>(bpy_app_timers_unregister),
175 METH_O,
176 bpy_app_timers_unregister_doc},
177 {"is_registered",
178 static_cast<PyCFunction>(bpy_app_timers_is_registered),
179 METH_O,
180 bpy_app_timers_is_registered_doc},
181 {nullptr, nullptr, 0, nullptr},
182};
183
184#ifdef __GNUC__
185# ifdef __clang__
186# pragma clang diagnostic pop
187# else
188# pragma GCC diagnostic pop
189# endif
190#endif
191
192static PyModuleDef M_AppTimers_module_def = {
193 /*m_base*/ PyModuleDef_HEAD_INIT,
194 /*m_name*/ "bpy.app.timers",
195 /*m_doc*/ nullptr,
196 /*m_size*/ 0,
197 /*m_methods*/ M_AppTimers_methods,
198 /*m_slots*/ nullptr,
199 /*m_traverse*/ nullptr,
200 /*m_clear*/ nullptr,
201 /*m_free*/ nullptr,
202};
203
205{
206 PyObject *sys_modules = PyImport_GetModuleDict();
207 PyObject *mod = PyModule_Create(&M_AppTimers_module_def);
208 PyC_Module_AddToSysModules(sys_modules, mod);
209 return mod;
210}
211
212} // namespace blender
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
static void py_timer_free(uintptr_t, void *user_data)
PyObject * BPY_app_timers_module()
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyModuleDef M_AppTimers_module_def
bool BLI_timer_is_registered(uintptr_t uuid)
Definition BLI_timer.cc:75
static double handle_returned_value(PyObject *function, PyObject *ret)
static double py_timer_execute(uintptr_t, void *user_data)
void BLI_timer_register(uintptr_t uuid, BLI_timer_func func, void *user_data, BLI_timer_data_free user_data_free, double first_interval, bool persistent)
Definition BLI_timer.cc:36
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
bool BLI_timer_unregister(uintptr_t uuid)
Definition BLI_timer.cc:63
int PyC_Module_AddToSysModules(PyObject *sys_modules, PyObject *module)
static PyObject * bpy_app_timers_unregister(PyObject *, PyObject *function)
static PyObject * bpy_app_timers_register(PyObject *, PyObject *args, PyObject *kw)
static PyMethodDef M_AppTimers_methods[]
static PyObject * bpy_app_timers_is_registered(PyObject *, PyObject *function)
_W64 int intptr_t
Definition BKE_text.h:119
header-only compatibility defines.