Blender V5.0
bpy_gizmo_wrap.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
16
17#include <Python.h>
18
19#include "WM_types.hh"
20
21#include "RNA_access.hh"
22#include "RNA_define.hh"
23#include "RNA_enum_types.hh"
24
25#include "bpy_gizmo_wrap.hh" /* own include */
26#include "bpy_intern_string.hh"
27#include "bpy_rna.hh"
28
30#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
31
32/* we may want to add, but not now */
33
34/* -------------------------------------------------------------------- */
37
38static bool bpy_gizmotype_target_property_def(wmGizmoType *gzt, PyObject *item)
39{
40 /* NOTE: names based on `rna_rna.cc`. */
41 PyObject *empty_tuple = PyTuple_New(0);
42
43 struct {
44 char *id;
45 BPy_EnumProperty_Parse type_enum;
46 int array_length;
47 } params{};
48 params.id = nullptr; /* not optional */
49 params.type_enum.items = rna_enum_property_type_items;
50 params.type_enum.value = PROP_FLOAT;
51 params.array_length = 1;
52
53 static const char *const _keywords[] = {"id", "type", "array_length", nullptr};
54 static _PyArg_Parser _parser = {
56 "|$" /* Optional keyword only arguments. */
57 "s" /* `id` */
58 "O&" /* `type` */
59 "i" /* `array_length` */
60 ":register_class",
61 _keywords,
62 nullptr,
63 };
64 if (!_PyArg_ParseTupleAndKeywordsFast(empty_tuple,
65 item,
66 &_parser,
67 &params.id,
69 &params.type_enum,
70 &params.array_length))
71 {
72 goto fail;
73 }
74
75 if (params.id == nullptr) {
76 PyErr_SetString(PyExc_ValueError, "'id' argument not given");
77 goto fail;
78 }
79
80 if ((params.array_length < 1) || (params.array_length > RNA_MAX_ARRAY_LENGTH)) {
81 PyErr_SetString(PyExc_ValueError, "'array_length' out of range");
82 goto fail;
83 }
84
85 WM_gizmotype_target_property_def(gzt, params.id, params.type_enum.value, params.array_length);
86 Py_DECREF(empty_tuple);
87 return true;
88
89fail:
90 Py_DECREF(empty_tuple);
91 return false;
92}
93
95{
96 PyTypeObject *py_class = static_cast<PyTypeObject *>(gzt->rna_ext.data);
98
99 /* only call this so pyrna_deferred_register_class gives a useful error
100 * WM_operatortype_append_ptr will call RNA_def_struct_identifier
101 * later */
103
104 if (pyrna_deferred_register_class(gzt->srna, py_class) != 0) {
105 PyErr_Print(); /* failed to register operator props */
106 }
107
108 /* Extract target property definitions from 'bl_target_properties' */
109 {
110 /* NOTE(@ideasman42): Picky developers will notice that `bl_targets`
111 * won't work with inheritance get direct from the dict to avoid
112 * raising a load of attribute errors (yes this isn't ideal). */
113 PyObject *py_class_dict = py_class->tp_dict;
114 PyObject *bl_target_properties = PyDict_GetItem(py_class_dict,
116
117 /* Some widgets may only exist to activate operators. */
118 if (bl_target_properties != nullptr) {
119 PyObject *bl_target_properties_fast;
120 if (!(bl_target_properties_fast = PySequence_Fast(bl_target_properties,
121 "bl_target_properties sequence")))
122 {
123 /* PySequence_Fast sets the error */
124 PyErr_Print();
125 return;
126 }
127
128 const uint items_len = PySequence_Fast_GET_SIZE(bl_target_properties_fast);
129 PyObject **items = PySequence_Fast_ITEMS(bl_target_properties_fast);
130
131 for (uint i = 0; i < items_len; i++) {
132 if (!bpy_gizmotype_target_property_def(gzt, items[i])) {
133 PyErr_Print();
134 break;
135 }
136 }
137
138 Py_DECREF(bl_target_properties_fast);
139 }
140 }
141}
142
143void BPY_RNA_gizmo_wrapper(wmGizmoType *gzt, void *userdata)
144{
145 /* take care not to overwrite anything set in
146 * #WM_gizmomaptype_group_link_ptr before `opfunc()` is called. */
147 StructRNA *srna = gzt->srna;
148 *gzt = *((wmGizmoType *)userdata);
149 gzt->srna = srna; /* restore */
150
151/* don't do translations here yet */
152#if 0
153 /* Use i18n context from rna_ext.srna if possible (py gizmo-groups). */
154 if (gt->rna_ext.srna) {
156 }
157#endif
158
159 gzt->struct_size = sizeof(wmGizmo);
160
162}
163
165
166/* -------------------------------------------------------------------- */
169
171{
172 PyTypeObject *py_class = static_cast<PyTypeObject *>(gzgt->rna_ext.data);
174
175 /* only call this so pyrna_deferred_register_class gives a useful error
176 * WM_operatortype_append_ptr will call RNA_def_struct_identifier
177 * later */
179
180 if (pyrna_deferred_register_class(gzgt->srna, py_class) != 0) {
181 PyErr_Print(); /* failed to register operator props */
182 }
183}
184
186{
187 /* take care not to overwrite anything set in
188 * WM_gizmomaptype_group_link_ptr before opfunc() is called */
189 StructRNA *srna = gzgt->srna;
190 *gzgt = *((wmGizmoGroupType *)userdata);
191 gzgt->srna = srna; /* restore */
192
193/* don't do translations here yet */
194#if 0
195 /* Use i18n context from rna_ext.srna if possible (py gizmo-groups). */
196 if (gzgt->rna_ext.srna) {
199 }
200#endif
201
203}
204
unsigned int uint
#define RNA_MAX_ARRAY_LENGTH
Definition RNA_define.hh:24
@ PROP_FLOAT
Definition RNA_types.hh:164
void BPY_RNA_gizmo_wrapper(wmGizmoType *gzt, void *userdata)
static bool bpy_gizmotype_target_property_def(wmGizmoType *gzt, PyObject *item)
static void gizmo_properties_init(wmGizmoType *gzt)
static void gizmogroup_properties_init(wmGizmoGroupType *gzgt)
void BPY_RNA_gizmogroup_wrapper(wmGizmoGroupType *gzgt, void *userdata)
PyObject * bpy_intern_str_bl_target_properties
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition bpy_rna.cc:9281
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
int pyrna_enum_value_parse_string(PyObject *o, void *p)
header-only compatibility defines.
Py_DECREF(oname)
#define PY_ARG_PARSER_HEAD_COMPAT()
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
const char * RNA_struct_translation_context(const StructRNA *type)
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
const EnumPropertyItem rna_enum_property_type_items[]
Definition rna_rna.cc:43
StructRNA * srna
const char * idname
ExtensionRNA rna_ext
StructRNA * srna
ExtensionRNA rna_ext
const char * idname
i
Definition text_draw.cc:230
void WM_gizmotype_target_property_def(wmGizmoType *gzt, const char *idname, int data_type, int array_length)