Blender V4.5
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 PyErr_Clear();
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 PyErr_Clear();
127 return;
128 }
129
130 const uint items_len = PySequence_Fast_GET_SIZE(bl_target_properties_fast);
131 PyObject **items = PySequence_Fast_ITEMS(bl_target_properties_fast);
132
133 for (uint i = 0; i < items_len; i++) {
134 if (!bpy_gizmotype_target_property_def(gzt, items[i])) {
135 PyErr_Print();
136 PyErr_Clear();
137 break;
138 }
139 }
140
141 Py_DECREF(bl_target_properties_fast);
142 }
143 }
144}
145
146void BPY_RNA_gizmo_wrapper(wmGizmoType *gzt, void *userdata)
147{
148 /* take care not to overwrite anything set in
149 * #WM_gizmomaptype_group_link_ptr before `opfunc()` is called. */
150 StructRNA *srna = gzt->srna;
151 *gzt = *((wmGizmoType *)userdata);
152 gzt->srna = srna; /* restore */
153
154/* don't do translations here yet */
155#if 0
156 /* Use i18n context from rna_ext.srna if possible (py gizmo-groups). */
157 if (gt->rna_ext.srna) {
159 }
160#endif
161
162 gzt->struct_size = sizeof(wmGizmo);
163
165}
166
168
169/* -------------------------------------------------------------------- */
172
174{
175 PyTypeObject *py_class = static_cast<PyTypeObject *>(gzgt->rna_ext.data);
177
178 /* only call this so pyrna_deferred_register_class gives a useful error
179 * WM_operatortype_append_ptr will call RNA_def_struct_identifier
180 * later */
182
183 if (pyrna_deferred_register_class(gzgt->srna, py_class) != 0) {
184 PyErr_Print(); /* failed to register operator props */
185 PyErr_Clear();
186 }
187}
188
190{
191 /* take care not to overwrite anything set in
192 * WM_gizmomaptype_group_link_ptr before opfunc() is called */
193 StructRNA *srna = gzgt->srna;
194 *gzgt = *((wmGizmoGroupType *)userdata);
195 gzgt->srna = srna; /* restore */
196
197/* don't do translations here yet */
198#if 0
199 /* Use i18n context from rna_ext.srna if possible (py gizmo-groups). */
200 if (gzgt->rna_ext.srna) {
203 }
204#endif
205
207}
208
unsigned int uint
#define RNA_MAX_ARRAY_LENGTH
Definition RNA_define.hh:23
@ PROP_FLOAT
Definition RNA_types.hh:152
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:9171
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
int pyrna_enum_value_parse_string(PyObject *o, void *p)
header-only compatibility defines.
#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
Definition RNA_types.hh:909
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)