Blender  V3.3
bpy_gizmo_wrap.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
15 #include <Python.h>
16 
17 #include "BLI_utildefines.h"
18 
19 #include "WM_api.h"
20 #include "WM_types.h"
21 
22 #include "RNA_access.h"
23 #include "RNA_define.h"
24 #include "RNA_enum_types.h"
25 
26 #include "bpy_gizmo_wrap.h" /* own include */
27 #include "bpy_intern_string.h"
28 #include "bpy_rna.h"
29 
30 #include "../generic/py_capi_rna.h"
31 
32 /* we may want to add, but not now */
33 
34 /* -------------------------------------------------------------------- */
38 static bool bpy_gizmotype_target_property_def(wmGizmoType *gzt, PyObject *item)
39 {
40  /* NOTE: names based on `rna_rna.c`. */
41  PyObject *empty_tuple = PyTuple_New(0);
42 
43  struct {
44  char *id;
45  struct BPy_EnumProperty_Parse type_enum;
46  int array_length;
47  } params = {
48  .id = NULL, /* not optional */
49  .type_enum = {.items = rna_enum_property_type_items, .value = PROP_FLOAT},
50  .array_length = 1,
51  };
52 
53  static const char *const _keywords[] = {"id", "type", "array_length", NULL};
54  static _PyArg_Parser _parser = {
55  "|$" /* Optional keyword only arguments. */
56  "s" /* `id` */
57  "O&" /* `type` */
58  "i" /* `array_length` */
59  ":register_class",
60  _keywords,
61  0,
62  };
63  if (!_PyArg_ParseTupleAndKeywordsFast(empty_tuple,
64  item,
65  &_parser,
66  &params.id,
68  &params.type_enum,
69  &params.array_length)) {
70  goto fail;
71  }
72 
73  if (params.id == NULL) {
74  PyErr_SetString(PyExc_ValueError, "'id' argument not given");
75  goto fail;
76  }
77 
78  if ((params.array_length < 1) || (params.array_length > RNA_MAX_ARRAY_LENGTH)) {
79  PyErr_SetString(PyExc_ValueError, "'array_length' out of range");
80  goto fail;
81  }
82 
83  WM_gizmotype_target_property_def(gzt, params.id, params.type_enum.value, params.array_length);
84  Py_DECREF(empty_tuple);
85  return true;
86 
87 fail:
88  Py_DECREF(empty_tuple);
89  return false;
90 }
91 
93 {
94  PyTypeObject *py_class = gzt->rna_ext.data;
96 
97  /* only call this so pyrna_deferred_register_class gives a useful error
98  * WM_operatortype_append_ptr will call RNA_def_struct_identifier
99  * later */
101 
102  if (pyrna_deferred_register_class(gzt->srna, py_class) != 0) {
103  PyErr_Print(); /* failed to register operator props */
104  PyErr_Clear();
105  }
106 
107  /* Extract target property definitions from 'bl_target_properties' */
108  {
109  /* Picky developers will notice that 'bl_targets' won't work with inheritance
110  * get direct from the dict to avoid raising a load of attribute errors
111  * (yes this isn't ideal) - campbell. */
112  PyObject *py_class_dict = py_class->tp_dict;
113  PyObject *bl_target_properties = PyDict_GetItem(py_class_dict,
115 
116  /* Some widgets may only exist to activate operators. */
117  if (bl_target_properties != NULL) {
118  PyObject *bl_target_properties_fast;
119  if (!(bl_target_properties_fast = PySequence_Fast(bl_target_properties,
120  "bl_target_properties sequence"))) {
121  /* PySequence_Fast sets the error */
122  PyErr_Print();
123  PyErr_Clear();
124  return;
125  }
126 
127  const uint items_len = PySequence_Fast_GET_SIZE(bl_target_properties_fast);
128  PyObject **items = PySequence_Fast_ITEMS(bl_target_properties_fast);
129 
130  for (uint i = 0; i < items_len; i++) {
132  PyErr_Print();
133  PyErr_Clear();
134  break;
135  }
136  }
137 
138  Py_DECREF(bl_target_properties_fast);
139  }
140  }
141 }
142 
143 void 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 gizmogroups). */
154  if (gt->rna_ext.srna) {
156  }
157 #endif
158 
159  gzt->struct_size = sizeof(wmGizmo);
160 
162 }
163 
166 /* -------------------------------------------------------------------- */
171 {
172  PyTypeObject *py_class = 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  PyErr_Clear();
183  }
184 }
185 
186 void BPY_RNA_gizmogroup_wrapper(wmGizmoGroupType *gzgt, void *userdata)
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 = *((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 gizmogroups). */
197  if (gzgt->rna_ext.srna) {
199  }
200 #endif
201 
203 }
204 
unsigned int uint
Definition: BLI_sys_types.h:67
#define RNA_MAX_ARRAY_LENGTH
Definition: RNA_define.h:25
@ PROP_FLOAT
Definition: RNA_types.h:61
struct wmGizmo wmGizmo
Definition: WM_api.h:68
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.c:8230
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
int pyrna_enum_value_parse_string(PyObject *o, void *p)
Definition: py_capi_rna.c:194
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
Definition: rna_access.c:902
const char * RNA_struct_translation_context(const StructRNA *type)
Definition: rna_access.c:619
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
Definition: rna_define.c:1227
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
Definition: rna_define.c:1250
const EnumPropertyItem rna_enum_property_type_items[]
Definition: rna_rna.c:42
const struct EnumPropertyItem * items
Definition: py_capi_rna.h:58
StructRNA * srna
Definition: RNA_types.h:766
void * data
Definition: RNA_types.h:765
const char * idname
ExtensionRNA rna_ext
struct StructRNA * srna
ExtensionRNA rna_ext
const char * idname
struct StructRNA * srna
void WM_gizmotype_target_property_def(wmGizmoType *gzt, const char *idname, int data_type, int array_length)