Blender V5.0
bpy_msgbus.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
9
10#include <Python.h>
11
14#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
16
18
19#include "BKE_context.hh"
20
21#include "WM_message.hh"
22
23#include "RNA_access.hh"
24
25#include "bpy_capi_utils.hh"
26#include "bpy_rna.hh"
27
28#include "bpy_msgbus.hh" /* own include */
29
30/* -------------------------------------------------------------------- */
33
34#define BPY_MSGBUS_RNA_MSGKEY_DOC \
35 " :arg key: Represents the type of data being subscribed to\n" \
36 "\n" \
37 " Arguments include\n" \
38 " - A property instance.\n" \
39 " - A struct type.\n" \
40 " - A tuple representing a (struct, property name) pair.\n" \
41 " :type key: :class:`bpy.types.Property` | " \
42 ":class:`bpy.types.Struct` | " \
43 "tuple[:class:`bpy.types.Struct`, str]\n"
44
55static int py_msgbus_rna_key_from_py(PyObject *py_sub,
56 wmMsgParams_RNA *msg_key_params,
57 const char *error_prefix)
58{
59
60 /* Allow common case, object rotation, location - etc. */
61 if (BaseMathObject_CheckExact(py_sub)) {
62 BaseMathObject *py_sub_math = (BaseMathObject *)py_sub;
63 if (py_sub_math->cb_user == nullptr) {
64 PyErr_Format(PyExc_TypeError, "%s: math argument has no owner", error_prefix);
65 return -1;
66 }
67 py_sub = py_sub_math->cb_user;
68 /* Common case will use BPy_PropertyRNA_Check below. */
69 }
70
71 if (BPy_PropertyRNA_Check(py_sub)) {
72 BPy_PropertyRNA *data_prop = (BPy_PropertyRNA *)py_sub;
73 PYRNA_PROP_CHECK_INT(data_prop);
74 msg_key_params->ptr = *data_prop->ptr;
75 msg_key_params->prop = data_prop->prop;
76 }
77 else if (BPy_StructRNA_Check(py_sub)) {
78 /* NOTE: this isn't typically used since we don't edit structs directly. */
79 BPy_StructRNA *data_srna = (BPy_StructRNA *)py_sub;
80 PYRNA_STRUCT_CHECK_INT(data_srna);
81 msg_key_params->ptr = *data_srna->ptr;
82 }
83 /* TODO: property / type, not instance. */
84 else if (PyType_Check(py_sub)) {
85 StructRNA *data_type = pyrna_struct_as_srna(py_sub, false, error_prefix);
86 if (data_type == nullptr) {
87 return -1;
88 }
89 msg_key_params->ptr.type = data_type;
90 }
91 else if (PyTuple_CheckExact(py_sub)) {
92 if (PyTuple_GET_SIZE(py_sub) == 2) {
93 PyObject *data_type_py = PyTuple_GET_ITEM(py_sub, 0);
94 PyObject *data_prop_py = PyTuple_GET_ITEM(py_sub, 1);
95 StructRNA *data_type = pyrna_struct_as_srna(data_type_py, false, error_prefix);
96 if (data_type == nullptr) {
97 return -1;
98 }
99 if (!PyUnicode_CheckExact(data_prop_py)) {
100 PyErr_Format(PyExc_TypeError, "%s: expected property to be a string", error_prefix);
101 return -1;
102 }
103 PointerRNA data_type_ptr{};
104 data_type_ptr.type = data_type;
105
106 const char *data_prop_str = PyUnicode_AsUTF8(data_prop_py);
107 PropertyRNA *data_prop = RNA_struct_find_property(&data_type_ptr, data_prop_str);
108
109 if (data_prop == nullptr) {
110 PyErr_Format(PyExc_TypeError,
111 "%s: struct %.200s does not contain property %.200s",
112 error_prefix,
113 RNA_struct_identifier(data_type),
114 data_prop_str);
115 return -1;
116 }
117
118 msg_key_params->ptr.type = data_type;
119 msg_key_params->prop = data_prop;
120 }
121 else {
122 PyErr_Format(PyExc_ValueError, "%s: Expected a pair (type, property_id)", error_prefix);
123 return -1;
124 }
125 }
126 return 0;
127}
128
130
131/* -------------------------------------------------------------------- */
134
135#define BPY_MSGBUS_USER_DATA_LEN 2
136
137/* Follow wmMsgNotifyFn spec */
139 wmMsgSubscribeKey * /*msg_key*/,
140 wmMsgSubscribeValue *msg_val)
141{
142 PyGILState_STATE gilstate;
143 bpy_context_set(C, &gilstate);
144 const bool is_write_ok = pyrna_write_check();
145 if (!is_write_ok) {
146 pyrna_write_set(true);
147 }
148
149 PyObject *user_data = static_cast<PyObject *>(msg_val->user_data);
150 BLI_assert(PyTuple_GET_SIZE(user_data) == BPY_MSGBUS_USER_DATA_LEN);
151
152 PyObject *callback_args = PyTuple_GET_ITEM(user_data, 0);
153 PyObject *callback_notify = PyTuple_GET_ITEM(user_data, 1);
154
155 PyObject *ret = PyObject_CallObject(callback_notify, callback_args);
156
157 if (ret == nullptr) {
158 PyC_Err_PrintWithFunc(callback_notify);
159 }
160 else {
161 if (ret != Py_None) {
162 PyErr_SetString(PyExc_ValueError, "the return value must be None");
163 PyC_Err_PrintWithFunc(callback_notify);
164 }
165 Py_DECREF(ret);
166 }
167
168 if (!is_write_ok) {
169 pyrna_write_set(false);
170 }
171 bpy_context_clear(C, &gilstate);
172}
173
174/* Follow wmMsgSubscribeValueFreeDataFn spec */
176 wmMsgSubscribeValue *msg_val)
177{
178 const PyGILState_STATE gilstate = PyGILState_Ensure();
179 Py_DECREF(msg_val->owner);
180 Py_DECREF(msg_val->user_data);
181 PyGILState_Release(gilstate);
182}
183
185
186/* -------------------------------------------------------------------- */
189
191 /* Wrap. */
192 bpy_msgbus_subscribe_rna_doc,
193 ".. function:: subscribe_rna(key, owner, args, notify, *, options=set())\n"
194 "\n"
195 " Register a message bus subscription. It will be cleared when another blend file is\n"
196 " loaded, or can be cleared explicitly via :func:`bpy.msgbus.clear_by_owner`.\n"
198 " :arg owner: Handle for this subscription (compared by identity).\n"
199 " :type owner: Any\n"
200 " :arg options: Change the behavior of the subscriber.\n"
201 "\n"
202 " - ``PERSISTENT`` when set, the subscriber will be kept when remapping ID data.\n"
203 "\n"
204 " :type options: set[str]\n"
205 "\n"
206 ".. note::\n"
207 "\n"
208 " All subscribers will be cleared on file-load. Subscribers can be re-registered on load,\n"
209 " see :mod:`bpy.app.handlers.load_post`.\n");
210static PyObject *bpy_msgbus_subscribe_rna(PyObject * /*self*/, PyObject *args, PyObject *kw)
211{
212 const char *error_prefix = "subscribe_rna";
213 PyObject *py_sub = nullptr;
214 PyObject *py_owner = nullptr;
215 PyObject *callback_args = nullptr;
216 PyObject *callback_notify = nullptr;
217
218 enum {
219 IS_PERSISTENT = (1 << 0),
220 };
221 PyObject *py_options = nullptr;
222 const EnumPropertyItem py_options_enum[] = {
223 {IS_PERSISTENT, "PERSISTENT", 0, ""},
224 {0, nullptr, 0, nullptr, nullptr},
225 };
226 int options = 0;
227
228 if (PyTuple_GET_SIZE(args) != 0) {
229 PyErr_Format(PyExc_TypeError, "%s: only keyword arguments are supported", error_prefix);
230 return nullptr;
231 }
232 static const char *_keywords[] = {
233 "key",
234 "owner",
235 "args",
236 "notify",
237 "options",
238 nullptr,
239 };
240 static _PyArg_Parser _parser = {
242 "O" /* `key` */
243 "O" /* `owner` */
244 "O!" /* `args` */
245 "O" /* `notify` */
246 "|$" /* Optional keyword only arguments. */
247 "O!" /* `options` */
248 ":subscribe_rna",
249 _keywords,
250 nullptr,
251 };
252 if (!_PyArg_ParseTupleAndKeywordsFast(args,
253 kw,
254 &_parser,
255 &py_sub,
256 &py_owner,
257 &PyTuple_Type,
258 &callback_args,
259 &callback_notify,
260 &PySet_Type,
261 &py_options))
262 {
263 return nullptr;
264 }
265
266 if (py_options &&
267 (pyrna_enum_bitfield_from_set(py_options_enum, py_options, &options, error_prefix) == -1))
268 {
269 return nullptr;
270 }
271
272 /* NOTE: we may want to have a way to pass this in. */
275 wmMsgParams_RNA msg_key_params = {{}};
276
277 wmMsgSubscribeValue msg_val_params = {nullptr};
278
279 if (py_msgbus_rna_key_from_py(py_sub, &msg_key_params, error_prefix) == -1) {
280 return nullptr;
281 }
282
283 if (!PyFunction_Check(callback_notify)) {
284 PyErr_Format(PyExc_TypeError,
285 "notify expects a function, found %.200s",
286 Py_TYPE(callback_notify)->tp_name);
287 return nullptr;
288 }
289
290 if (options != 0) {
291 if (options & IS_PERSISTENT) {
292 msg_val_params.is_persistent = true;
293 }
294 }
295
296 /* owner can be anything. */
297 {
298 msg_val_params.owner = py_owner;
299 Py_INCREF(py_owner);
300 }
301
302 {
303 PyObject *user_data = PyTuple_New(2);
304 PyTuple_SET_ITEMS(user_data, Py_NewRef(callback_args), Py_NewRef(callback_notify));
305 msg_val_params.user_data = user_data;
306 }
307
308 msg_val_params.notify = bpy_msgbus_notify;
310
311 WM_msg_subscribe_rna_params(mbus, &msg_key_params, &msg_val_params, __func__);
312
313 if (false) { /* For debugging. */
314 WM_msg_dump(mbus, __func__);
315 }
316
317 Py_RETURN_NONE;
318}
319
321 /* Wrap. */
322 bpy_msgbus_publish_rna_doc,
323 ".. function:: publish_rna(key)\n"
325 "\n"
326 " Notify subscribers of changes to this property\n"
327 " (this typically doesn't need to be called explicitly since changes will automatically "
328 "publish updates).\n"
329 " In some cases it may be useful to publish changes explicitly using more general keys.\n");
330static PyObject *bpy_msgbus_publish_rna(PyObject * /*self*/, PyObject *args, PyObject *kw)
331{
332 const char *error_prefix = "publish_rna";
333 PyObject *py_sub = nullptr;
334
335 if (PyTuple_GET_SIZE(args) != 0) {
336 PyErr_Format(PyExc_TypeError, "%s: only keyword arguments are supported", error_prefix);
337 return nullptr;
338 }
339 static const char *_keywords[] = {
340 "key",
341 nullptr,
342 };
343 static _PyArg_Parser _parser = {
345 "O" /* `key` */
346 ":publish_rna",
347 _keywords,
348 nullptr,
349 };
350 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &py_sub)) {
351 return nullptr;
352 }
353
354 /* NOTE: we may want to have a way to pass this in. */
357 wmMsgParams_RNA msg_key_params = {{}};
358
359 if (py_msgbus_rna_key_from_py(py_sub, &msg_key_params, error_prefix) == -1) {
360 return nullptr;
361 }
362
363 WM_msg_publish_rna_params(mbus, &msg_key_params);
364
365 Py_RETURN_NONE;
366}
367
369 /* Wrap. */
370 bpy_msgbus_clear_by_owner_doc,
371 ".. function:: clear_by_owner(owner)\n"
372 "\n"
373 " Clear all subscribers using this owner.\n");
374static PyObject *bpy_msgbus_clear_by_owner(PyObject * /*self*/, PyObject *py_owner)
375{
378 WM_msgbus_clear_by_owner(mbus, py_owner);
379 Py_RETURN_NONE;
380}
381
382#ifdef __GNUC__
383# ifdef __clang__
384# pragma clang diagnostic push
385# pragma clang diagnostic ignored "-Wcast-function-type"
386# else
387# pragma GCC diagnostic push
388# pragma GCC diagnostic ignored "-Wcast-function-type"
389# endif
390#endif
391
392static PyMethodDef BPy_msgbus_methods[] = {
393 {"subscribe_rna",
394 (PyCFunction)bpy_msgbus_subscribe_rna,
395 METH_VARARGS | METH_KEYWORDS,
396 bpy_msgbus_subscribe_rna_doc},
397 {"publish_rna",
398 (PyCFunction)bpy_msgbus_publish_rna,
399 METH_VARARGS | METH_KEYWORDS,
400 bpy_msgbus_publish_rna_doc},
401 {"clear_by_owner",
402 (PyCFunction)bpy_msgbus_clear_by_owner,
403 METH_O,
404 bpy_msgbus_clear_by_owner_doc},
405 {nullptr, nullptr, 0, nullptr},
406};
407
408#ifdef __GNUC__
409# ifdef __clang__
410# pragma clang diagnostic pop
411# else
412# pragma GCC diagnostic pop
413# endif
414#endif
415
416static PyModuleDef _bpy_msgbus_def = {
417 /*m_base*/ PyModuleDef_HEAD_INIT,
418 /*m_name*/ "msgbus",
419 /*m_doc*/ nullptr,
420 /*m_size*/ 0,
421 /*m_methods*/ BPy_msgbus_methods,
422 /*m_slots*/ nullptr,
423 /*m_traverse*/ nullptr,
424 /*m_clear*/ nullptr,
425 /*m_free*/ nullptr,
426};
427
429{
430 PyObject *submodule;
431
432 submodule = PyModule_Create(&_bpy_msgbus_def);
433
434 return submodule;
435}
436
wmMsgBus * CTX_wm_message_bus(const bContext *C)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define C
Definition RandGen.cpp:29
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate)
struct bContext * BPY_context_get()
static void bpy_msgbus_notify(bContext *C, wmMsgSubscribeKey *, wmMsgSubscribeValue *msg_val)
static void bpy_msgbus_subscribe_value_free_data(wmMsgSubscribeKey *, wmMsgSubscribeValue *msg_val)
static PyModuleDef _bpy_msgbus_def
#define BPY_MSGBUS_USER_DATA_LEN
PyDoc_STRVAR(bpy_msgbus_subscribe_rna_doc, ".. function:: subscribe_rna(key, owner, args, notify, *, options=set())\n" "\n" " Register a message bus subscription. It will be cleared when another blend file is\n" " loaded, or can be cleared explicitly via :func:`bpy.msgbus.clear_by_owner`.\n" "\n" BPY_MSGBUS_RNA_MSGKEY_DOC " :arg owner: Handle for this subscription (compared by identity).\n" " :type owner: Any\n" " :arg options: Change the behavior of the subscriber.\n" "\n" " - ``PERSISTENT`` when set, the subscriber will be kept when remapping ID data.\n" "\n" " :type options: set[str]\n" "\n" ".. note::\n" "\n" " All subscribers will be cleared on file-load. Subscribers can be re-registered on load,\n" " see :mod:`bpy.app.handlers.load_post`.\n")
static int py_msgbus_rna_key_from_py(PyObject *py_sub, wmMsgParams_RNA *msg_key_params, const char *error_prefix)
Definition bpy_msgbus.cc:55
static PyObject * bpy_msgbus_subscribe_rna(PyObject *, PyObject *args, PyObject *kw)
PyObject * BPY_msgbus_module()
static PyMethodDef BPy_msgbus_methods[]
#define BPY_MSGBUS_RNA_MSGKEY_DOC
Definition bpy_msgbus.cc:34
static PyObject * bpy_msgbus_clear_by_owner(PyObject *, PyObject *py_owner)
static PyObject * bpy_msgbus_publish_rna(PyObject *, PyObject *args, PyObject *kw)
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition bpy_rna.cc:8959
bool pyrna_write_check()
Definition bpy_rna.cc:457
void pyrna_write_set(bool val)
Definition bpy_rna.cc:464
#define PYRNA_STRUCT_CHECK_INT(obj)
Definition bpy_rna.hh:83
#define PYRNA_PROP_CHECK_INT(obj)
Definition bpy_rna.hh:94
#define BPy_StructRNA_Check(v)
Definition bpy_rna.hh:73
#define BPy_PropertyRNA_Check(v)
Definition bpy_rna.hh:75
CCL_NAMESPACE_BEGIN struct Options options
#define BaseMathObject_CheckExact(v)
Definition mathutils.hh:79
int pyrna_enum_bitfield_from_set(const EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
void PyC_Err_PrintWithFunc(PyObject *py_func)
header-only compatibility defines.
Py_DECREF(oname)
#define PY_ARG_PARSER_HEAD_COMPAT()
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
return ret
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
const char * RNA_struct_identifier(const StructRNA *type)
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:150
PropertyRNA * prop
Definition bpy_rna.hh:151
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:130
StructRNA * type
Definition RNA_types.hh:52
const PropertyRNA * prop
wmMsgSubscribeValueFreeDataFn free_data
void WM_msgbus_clear_by_owner(wmMsgBus *mbus, void *owner)
void WM_msg_dump(wmMsgBus *mbus, const char *info_str)
void WM_msg_subscribe_rna_params(wmMsgBus *mbus, const wmMsgParams_RNA *msg_key_params, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
void WM_msg_publish_rna_params(wmMsgBus *mbus, const wmMsgParams_RNA *msg_key_params)