Blender V4.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
17#include <Python.h>
18
19#include "BLI_utildefines.h"
20
21#include "WM_types.hh"
22
23#include "RNA_access.hh"
24#include "RNA_define.hh"
25#include "RNA_enum_types.hh"
26
27#include "bpy_gizmo_wrap.hh" /* own include */
28#include "bpy_intern_string.hh"
29#include "bpy_rna.hh"
30
33
34/* we may want to add, but not now */
35
36/* -------------------------------------------------------------------- */
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 = {
58 "|$" /* Optional keyword only arguments. */
59 "s" /* `id` */
60 "O&" /* `type` */
61 "i" /* `array_length` */
62 ":register_class",
63 _keywords,
64 nullptr,
65 };
66 if (!_PyArg_ParseTupleAndKeywordsFast(empty_tuple,
67 item,
68 &_parser,
69 &params.id,
71 &params.type_enum,
72 &params.array_length))
73 {
74 goto fail;
75 }
76
77 if (params.id == nullptr) {
78 PyErr_SetString(PyExc_ValueError, "'id' argument not given");
79 goto fail;
80 }
81
82 if ((params.array_length < 1) || (params.array_length > RNA_MAX_ARRAY_LENGTH)) {
83 PyErr_SetString(PyExc_ValueError, "'array_length' out of range");
84 goto fail;
85 }
86
87 WM_gizmotype_target_property_def(gzt, params.id, params.type_enum.value, params.array_length);
88 Py_DECREF(empty_tuple);
89 return true;
90
91fail:
92 Py_DECREF(empty_tuple);
93 return false;
94}
95
97{
98 PyTypeObject *py_class = static_cast<PyTypeObject *>(gzt->rna_ext.data);
100
101 /* only call this so pyrna_deferred_register_class gives a useful error
102 * WM_operatortype_append_ptr will call RNA_def_struct_identifier
103 * later */
105
106 if (pyrna_deferred_register_class(gzt->srna, py_class) != 0) {
107 PyErr_Print(); /* failed to register operator props */
108 PyErr_Clear();
109 }
110
111 /* Extract target property definitions from 'bl_target_properties' */
112 {
113 /* Picky developers will notice that 'bl_targets' won't work with inheritance
114 * get direct from the dict to avoid raising a load of attribute errors
115 * (yes this isn't ideal) - campbell. */
116 PyObject *py_class_dict = py_class->tp_dict;
117 PyObject *bl_target_properties = PyDict_GetItem(py_class_dict,
119
120 /* Some widgets may only exist to activate operators. */
121 if (bl_target_properties != nullptr) {
122 PyObject *bl_target_properties_fast;
123 if (!(bl_target_properties_fast = PySequence_Fast(bl_target_properties,
124 "bl_target_properties sequence")))
125 {
126 /* PySequence_Fast sets the error */
127 PyErr_Print();
128 PyErr_Clear();
129 return;
130 }
131
132 const uint items_len = PySequence_Fast_GET_SIZE(bl_target_properties_fast);
133 PyObject **items = PySequence_Fast_ITEMS(bl_target_properties_fast);
134
135 for (uint i = 0; i < items_len; i++) {
136 if (!bpy_gizmotype_target_property_def(gzt, items[i])) {
137 PyErr_Print();
138 PyErr_Clear();
139 break;
140 }
141 }
142
143 Py_DECREF(bl_target_properties_fast);
144 }
145 }
146}
147
148void BPY_RNA_gizmo_wrapper(wmGizmoType *gzt, void *userdata)
149{
150 /* take care not to overwrite anything set in
151 * WM_gizmomaptype_group_link_ptr before opfunc() is called */
152 StructRNA *srna = gzt->srna;
153 *gzt = *((wmGizmoType *)userdata);
154 gzt->srna = srna; /* restore */
155
156/* don't do translations here yet */
157#if 0
158 /* Use i18n context from rna_ext.srna if possible (py gizmo-groups). */
159 if (gt->rna_ext.srna) {
161 }
162#endif
163
164 gzt->struct_size = sizeof(wmGizmo);
165
167}
168
171/* -------------------------------------------------------------------- */
176{
177 PyTypeObject *py_class = static_cast<PyTypeObject *>(gzgt->rna_ext.data);
179
180 /* only call this so pyrna_deferred_register_class gives a useful error
181 * WM_operatortype_append_ptr will call RNA_def_struct_identifier
182 * later */
184
185 if (pyrna_deferred_register_class(gzgt->srna, py_class) != 0) {
186 PyErr_Print(); /* failed to register operator props */
187 PyErr_Clear();
188 }
189}
190
192{
193 /* take care not to overwrite anything set in
194 * WM_gizmomaptype_group_link_ptr before opfunc() is called */
195 StructRNA *srna = gzgt->srna;
196 *gzgt = *((wmGizmoGroupType *)userdata);
197 gzgt->srna = srna; /* restore */
198
199/* don't do translations here yet */
200#if 0
201 /* Use i18n context from rna_ext.srna if possible (py gizmo-groups). */
202 if (gzgt->rna_ext.srna) {
205 }
206#endif
207
209}
210
unsigned int uint
#define RNA_MAX_ARRAY_LENGTH
Definition RNA_define.hh:23
@ PROP_FLOAT
Definition RNA_types.hh:67
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:8442
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:46
StructRNA * srna
Definition RNA_types.hh:780
const char * idname
ExtensionRNA rna_ext
StructRNA * srna
ExtensionRNA rna_ext
const char * idname
void WM_gizmotype_target_property_def(wmGizmoType *gzt, const char *idname, int data_type, int array_length)