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