Blender V5.3
bpy_rna_callback.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
11
12#include <Python.h>
13
16
17#include "DNA_space_types.h"
18
19#include "RNA_access.hh"
20#include "RNA_enum_types.hh"
21#include "RNA_prototypes.hh"
22
23#include "BKE_screen.hh"
24
25#include "WM_api.hh"
26
27#include "ED_space_api.hh"
28
29#include "BPY_extern.hh" /* For public API. */
30
31#include "bpy_capi_utils.hh"
32#include "bpy_rna.hh"
33#include "bpy_rna_callback.hh" /* Own include. */
34
35namespace blender {
36
37/* Use this to stop other capsules from being mis-used. */
38static const char *rna_capsule_id = "RNA_HANDLE";
39static const char *rna_capsule_id_invalid = "RNA_HANDLE_REMOVED";
40
42 {REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
43 {REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
44 {REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
45 {REGION_DRAW_BACKDROP, "BACKDROP", 0, "Backdrop", ""},
46 {0, nullptr, 0, nullptr, nullptr},
47};
48
49static void cb_region_draw(const bContext *C, ARegion * /*region*/, void *customdata)
50{
51 PyGILState_STATE gilstate;
52 bpy_context_set(const_cast<bContext *>(C), &gilstate);
53
54 PyObject *cb_func, *cb_args, *result;
55
56 cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
57 cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
58 result = PyObject_CallObject(cb_func, cb_args);
59
60 if (result) {
61 Py_DECREF(result);
62 }
63 else {
64 PyErr_Print();
65 }
66
67 bpy_context_clear(const_cast<bContext *>(C), &gilstate);
68}
69
70/* We could make generic utility */
71static PyObject *PyC_Tuple_CopySized(PyObject *src, int len_dst)
72{
73 PyObject *dst = PyTuple_New(len_dst);
74 const int len_src = PyTuple_GET_SIZE(src);
75 BLI_assert(len_src <= len_dst);
76 for (int i = 0; i < len_src; i++) {
77 PyObject *item = PyTuple_GET_ITEM(src, i);
78 PyTuple_SET_ITEM(dst, i, item);
79 Py_INCREF(item);
80 }
81 return dst;
82}
83
85 const int2 &xy,
86 const float2 & /*tilt*/,
87 void *customdata)
88{
89 PyGILState_STATE gilstate;
90 bpy_context_set(C, &gilstate);
91
92 PyObject *cb_func, *cb_args, *result;
93 cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
94 cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
95
96 const int cb_args_len = PyTuple_GET_SIZE(cb_args);
97
98 PyObject *cb_args_xy = PyTuple_New(2);
99 PyTuple_SET_ITEMS(cb_args_xy, PyLong_FromLong(xy.x), PyLong_FromLong(xy.y));
100
101 PyObject *cb_args_with_xy = PyC_Tuple_CopySized(cb_args, cb_args_len + 1);
102 PyTuple_SET_ITEM(cb_args_with_xy, cb_args_len, cb_args_xy);
103
104 result = PyObject_CallObject(cb_func, cb_args_with_xy);
105
106 Py_DECREF(cb_args_with_xy);
107
108 if (result) {
109 Py_DECREF(result);
110 }
111 else {
112 PyErr_Print();
113 }
114
115 bpy_context_clear(C, &gilstate);
116}
117
118#if 0
119PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
120{
121 void *handle;
122
123 PyObject *cb_func, *cb_args;
124 char *cb_event_str = nullptr;
125 int cb_event;
126
127 if (!PyArg_ParseTuple(args,
128 "O" /* `callback` */
129 "O!" /* `args` */
130 "|" /* Optional arguments. */
131 "s" /* `event` */
132 ":bpy_struct.callback_add",
133 &cb_func,
134 &PyTuple_Type,
135 &cb_args,
136 &cb_event_str))
137 {
138 return nullptr;
139 }
140
141 if (!PyCallable_Check(cb_func)) {
142 PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
143 return nullptr;
144 }
145
146 if (RNA_struct_is_a(self->ptr.type, RNA_Region)) {
147 if (cb_event_str) {
149 region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") == -1)
150 {
151 return nullptr;
152 }
153 }
154 else {
155 cb_event = REGION_DRAW_POST_PIXEL;
156 }
157
159 ((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
160 Py_INCREF(args);
161 }
162 else {
163 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
164 return nullptr;
165 }
166
167 return PyCapsule_New((void *)handle, rna_capsule_id, nullptr);
168}
169
170PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
171{
172 PyObject *py_handle;
173 void *handle;
174 void *customdata;
175
176 if (!PyArg_ParseTuple(args,
177 "O!" /* `handle` */
178 ":callback_remove",
179 &PyCapsule_Type,
180 &py_handle))
181 {
182 return nullptr;
183 }
184
185 handle = PyCapsule_GetPointer(py_handle, rna_capsule_id);
186
187 if (handle == nullptr) {
188 PyErr_SetString(PyExc_ValueError,
189 "callback_remove(handle): null handle given, invalid or already removed");
190 return nullptr;
191 }
192
193 if (RNA_struct_is_a(self->ptr.type, RNA_Region)) {
194 customdata = ED_region_draw_cb_customdata(handle);
195 Py_DECREF((PyObject *)customdata);
196
197 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
198 }
199 else {
200 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
201 return nullptr;
202 }
203
204 /* don't allow reuse */
205 PyCapsule_SetName(py_handle, rna_capsule_id_invalid);
206
207 Py_RETURN_NONE;
208}
209#endif
210
211/* reverse of rna_Space_refine() */
213{
214 if (srna == RNA_SpaceView3D) {
215 return SPACE_VIEW3D;
216 }
217 if (srna == RNA_SpaceGraphEditor) {
218 return SPACE_GRAPH;
219 }
220 if (srna == RNA_SpaceOutliner) {
221 return SPACE_OUTLINER;
222 }
223 if (srna == RNA_SpaceProperties) {
224 return SPACE_PROPERTIES;
225 }
226 if (srna == RNA_SpaceFileBrowser) {
227 return SPACE_FILE;
228 }
229 if (srna == RNA_SpaceImageEditor) {
230 return SPACE_IMAGE;
231 }
232 if (srna == RNA_SpaceInfo) {
233 return SPACE_INFO;
234 }
235 if (srna == RNA_SpaceSequenceEditor) {
236 return SPACE_SEQ;
237 }
238 if (srna == RNA_SpaceTextEditor) {
239 return SPACE_TEXT;
240 }
241 if (srna == RNA_SpaceDopeSheetEditor) {
242 return SPACE_ACTION;
243 }
244 if (srna == RNA_SpaceNLA) {
245 return SPACE_NLA;
246 }
247 if (srna == RNA_SpaceNodeEditor) {
248 return SPACE_NODE;
249 }
250 if (srna == RNA_SpaceConsole) {
251 return SPACE_CONSOLE;
252 }
253 if (srna == RNA_SpacePreferences) {
254 return SPACE_USERPREF;
255 }
256 if (srna == RNA_SpaceClipEditor) {
257 return SPACE_CLIP;
258 }
259 if (srna == RNA_SpaceSpreadsheet) {
260 return SPACE_SPREADSHEET;
261 }
262 if (srna == RNA_SpaceProject) {
263 return SPACE_PROJECT;
264 }
265 return SPACE_EMPTY;
266}
267
268static void cb_rna_capsule_destructor(PyObject *capsule)
269{
270 PyObject *args = static_cast<PyObject *>(PyCapsule_GetContext(capsule));
271 Py_DECREF(args);
272}
273
274PyObject *pyrna_callback_classmethod_add(PyObject * /*self*/, PyObject *args)
275{
276 void *handle;
277 PyObject *cls;
278 PyObject *cb_func, *cb_args;
279 StructRNA *srna;
280
281 if (PyTuple_GET_SIZE(args) < 2) {
282 PyErr_SetString(PyExc_ValueError, "handler_add(handler): expected at least 2 args");
283 return nullptr;
284 }
285
286 cls = PyTuple_GET_ITEM(args, 0);
287 if (!(srna = pyrna_struct_as_srna(cls, false, "handler_add"))) {
288 return nullptr;
289 }
290 cb_func = PyTuple_GET_ITEM(args, 1);
291 if (!PyCallable_Check(cb_func)) {
292 PyErr_SetString(PyExc_TypeError, "first argument isn't callable");
293 return nullptr;
294 }
295
296 /* class specific callbacks */
297
298 if (srna == RNA_WindowManager) {
299 struct {
300 BPy_EnumProperty_Parse space_type_enum;
301 BPy_EnumProperty_Parse region_type_enum;
302 } params{};
303 params.space_type_enum.items = rna_enum_space_type_items;
304 params.space_type_enum.value = SPACE_TYPE_ANY;
305 params.region_type_enum.items = rna_enum_region_type_items;
306 params.region_type_enum.value = RGN_TYPE_ANY;
307
308 if (!PyArg_ParseTuple(args,
309 "O" /* `cls` */
310 "O" /* `callback` */
311 "O!" /* `args` */
312 "|" /* Optional arguments. */
313 "O&" /* `space_type` */
314 "O&" /* `region_type` */
315 ":WindowManager.draw_cursor_add",
316 &cls,
317 &cb_func, /* already assigned, no matter */
318 &PyTuple_Type,
319 &cb_args,
321 &params.space_type_enum,
323 &params.region_type_enum))
324 {
325 return nullptr;
326 }
327
328 handle = WM_paint_cursor_activate(params.space_type_enum.value,
329 params.region_type_enum.value,
330 nullptr,
332 static_cast<void *>(args));
333 }
334 else if (RNA_struct_is_a(srna, RNA_Space)) {
335 struct {
336 BPy_EnumProperty_Parse region_type_enum;
337 BPy_EnumProperty_Parse event_enum;
338 } params{};
339 params.region_type_enum.items = rna_enum_region_type_items;
340 params.event_enum.items = region_draw_mode_items;
341
342 if (!PyArg_ParseTuple(args,
343 "O" /* `cls` */
344 "O" /* `callback` */
345 "O!" /* `args` */
346 "O&" /* `region_type` */
347 "O&" /* `draw_type` */
348 ":Space.draw_handler_add",
349 &cls,
350 &cb_func, /* already assigned, no matter */
351 &PyTuple_Type,
352 &cb_args,
354 &params.region_type_enum,
356 &params.event_enum))
357 {
358 return nullptr;
359 }
360
361 const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
362 if (spaceid == SPACE_EMPTY) {
363 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
364 return nullptr;
365 }
366
367 SpaceType *st = BKE_spacetype_from_id(spaceid);
368 ARegionType *art = BKE_regiontype_from_id(st, params.region_type_enum.value);
369 if (art == nullptr) {
370 PyErr_Format(
371 PyExc_TypeError, "region type %R not in space", params.region_type_enum.value_orig);
372 return nullptr;
373 }
375 art, cb_region_draw, static_cast<void *>(args), params.event_enum.value);
376 }
377 else {
378 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
379 return nullptr;
380 }
381
382 /* Keep the 'args' reference as long as the callback exists.
383 * This reference is decremented in #BPY_callback_screen_free and #BPY_callback_wm_free. */
384 Py_INCREF(args);
385
386 PyObject *ret = PyCapsule_New(handle, rna_capsule_id, nullptr);
387 /* Failure to create `ret` means the system is likely out of memory. */
388 BLI_assert(ret != nullptr);
389
390 /* Store 'args' in context as well for simple access. */
391 PyCapsule_SetDestructor(ret, cb_rna_capsule_destructor);
392 PyCapsule_SetContext(ret, args);
393 Py_INCREF(args);
394
395 return ret;
396}
397
398PyObject *pyrna_callback_classmethod_remove(PyObject * /*self*/, PyObject *args)
399{
400 PyObject *cls;
401 PyObject *py_handle;
402 void *handle;
403 StructRNA *srna;
404 bool capsule_clear = false;
405 bool handle_removed = false;
406
407 if (PyTuple_GET_SIZE(args) < 2) {
408 PyErr_SetString(PyExc_ValueError, "callback_remove(handler): expected at least 2 args");
409 return nullptr;
410 }
411
412 cls = PyTuple_GET_ITEM(args, 0);
413 if (!(srna = pyrna_struct_as_srna(cls, false, "callback_remove"))) {
414 return nullptr;
415 }
416 py_handle = PyTuple_GET_ITEM(args, 1);
417 handle = PyCapsule_GetPointer(py_handle, rna_capsule_id);
418 if (handle == nullptr) {
419 PyErr_SetString(PyExc_ValueError,
420 "callback_remove(handler): null handler given, invalid or already removed");
421 return nullptr;
422 }
423
424 if (srna == RNA_WindowManager) {
425 if (!PyArg_ParseTuple(args,
426 "O" /* `cls` */
427 "O!" /* `handler` */
428 ":WindowManager.draw_cursor_remove",
429 &cls,
430 &PyCapsule_Type,
431 &py_handle))
432 {
433 return nullptr;
434 }
435 handle_removed = WM_paint_cursor_end(static_cast<wmPaintCursor *>(handle));
436 capsule_clear = true;
437 }
438 else if (RNA_struct_is_a(srna, RNA_Space)) {
439 const char *error_prefix = "Space.draw_handler_remove";
440 struct {
441 BPy_EnumProperty_Parse region_type_enum;
442 } params{};
443 params.region_type_enum.items = rna_enum_region_type_items;
444
445 if (!PyArg_ParseTuple(args,
446 "O" /* `cls` */
447 "O!" /* `handler` */
448 "O&" /* `region_type` */
449 ":Space.draw_handler_remove",
450 &cls,
451 &PyCapsule_Type,
452 &py_handle, /* already assigned, no matter */
454 &params.region_type_enum))
455 {
456 return nullptr;
457 }
458
459 const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
460 if (spaceid == SPACE_EMPTY) {
461 PyErr_Format(PyExc_TypeError,
462 "%s: unknown space type '%.200s'",
463 error_prefix,
465 return nullptr;
466 }
467
468 SpaceType *st = BKE_spacetype_from_id(spaceid);
469 ARegionType *art = BKE_regiontype_from_id(st, params.region_type_enum.value);
470 if (art == nullptr) {
471 PyErr_Format(PyExc_TypeError,
472 "%s: region type %R not in space",
473 error_prefix,
474 params.region_type_enum.value_orig);
475 return nullptr;
476 }
477 handle_removed = ED_region_draw_cb_exit(art, handle);
478 capsule_clear = true;
479 }
480 else {
481 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
482 return nullptr;
483 }
484
485 /* When `handle_removed == false`: Blender has already freed the data
486 * (freeing screen data when loading a new file for example).
487 * This will have already decremented the user, so don't decrement twice. */
488 if (handle_removed == true) {
489 /* The handle has been removed, so decrement its custom-data. */
490 PyObject *handle_args = static_cast<PyObject *>(PyCapsule_GetContext(py_handle));
491 Py_DECREF(handle_args);
492 }
493
494 /* don't allow reuse */
495 if (capsule_clear) {
496 PyCapsule_Destructor destructor_fn = PyCapsule_GetDestructor(py_handle);
497 if (destructor_fn) {
498 destructor_fn(py_handle);
499 PyCapsule_SetDestructor(py_handle, nullptr);
500 }
501 PyCapsule_SetName(py_handle, rna_capsule_id_invalid);
502 }
503
504 Py_RETURN_NONE;
505}
506
507/* -------------------------------------------------------------------- */
510
511static void cb_customdata_free(void *customdata)
512{
513 PyGILState_STATE gilstate = PyGILState_Ensure();
514
515 PyObject *tuple = static_cast<PyObject *>(customdata);
516 Py_DECREF(tuple);
517
518 PyGILState_Release(gilstate);
519}
520
522{
524 art, reinterpret_cast<void *>(cb_region_draw), cb_customdata_free);
525}
526
531
533
534} // namespace blender
#define RGN_TYPE_ANY
#define SPACE_TYPE_ANY
#define REGION_DRAW_POST_VIEW
#define REGION_DRAW_BACKDROP
#define REGION_DRAW_POST_PIXEL
#define REGION_DRAW_PRE_VIEW
#define C
Definition RandGen.cpp:29
BMesh Iterators.
void BPY_callback_screen_free(ARegionType *art)
static void cb_rna_capsule_destructor(PyObject *capsule)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const EnumPropertyItem rna_enum_space_type_items[]
Definition rna_space.cc:91
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
static const char * rna_capsule_id
static PyObject * PyC_Tuple_CopySized(PyObject *src, int len_dst)
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
PyObject * pyrna_callback_classmethod_add(PyObject *, PyObject *args)
static void cb_region_draw(const bContext *C, ARegion *, void *customdata)
static const EnumPropertyItem region_draw_mode_items[]
static eSpace_Type rna_Space_refine_reverse(StructRNA *srna)
ARegionType * BKE_regiontype_from_id(const SpaceType *st, int regionid)
Definition screen.cc:364
SpaceType * BKE_spacetype_from_id(int spaceid)
Definition screen.cc:354
static void cb_wm_cursor_draw(bContext *C, const int2 &xy, const float2 &, void *customdata)
wmPaintCursor * WM_paint_cursor_activate(short space_type, short region_type, wmPaintCursorPoll poll, wmPaintCursorDraw draw, void *customdata)
PyObject * pyrna_callback_classmethod_remove(PyObject *, PyObject *args)
const char * RNA_struct_identifier(const StructRNA *type)
const EnumPropertyItem rna_enum_region_type_items[]
Definition rna_screen.cc:28
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition bpy_rna.cc:9338
void BPY_callback_wm_free(wmWindowManager *wm)
bool bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate) ATTR_NONNULL(1)
void ED_region_draw_cb_remove_by_type(ARegionType *art, void *draw_fn, void(*free)(void *))
void WM_paint_cursor_remove_by_type(wmWindowManager *wm, wmPaintCursorDraw draw_fn, void(*free)(void *))
static void cb_customdata_free(void *customdata)
static const char * rna_capsule_id_invalid
int pyrna_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value, const char *error_prefix)
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
int pyrna_enum_value_parse_string(PyObject *o, void *p)
bool WM_paint_cursor_end(wmPaintCursor *handle)
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)