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