Blender  V2.93
bpy_gizmo_wrap.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
29 #include <Python.h>
30 
31 #include "BLI_utildefines.h"
32 
33 #include "WM_api.h"
34 #include "WM_types.h"
35 
36 #include "RNA_access.h"
37 #include "RNA_define.h"
38 #include "RNA_enum_types.h"
39 
40 #include "bpy_gizmo_wrap.h" /* own include */
41 #include "bpy_intern_string.h"
42 #include "bpy_rna.h"
43 
44 /* we may want to add, but not now */
45 
46 /* -------------------------------------------------------------------- */
50 static bool bpy_gizmotype_target_property_def(wmGizmoType *gzt, PyObject *item)
51 {
52  /* Note: names based on 'rna_rna.c' */
53  PyObject *empty_tuple = PyTuple_New(0);
54 
55  struct {
56  char *id;
57  char *type_id;
58  int type;
59  int array_length;
60  } params = {
61  .id = NULL, /* not optional */
62  .type = PROP_FLOAT,
63  .type_id = NULL,
64  .array_length = 1,
65  };
66 
67  static const char *const _keywords[] = {"id", "type", "array_length", NULL};
68  static _PyArg_Parser _parser = {"|$ssi:register_class", _keywords, 0};
69  if (!_PyArg_ParseTupleAndKeywordsFast(
70  empty_tuple, item, &_parser, &params.id, &params.type_id, &params.array_length)) {
71  goto fail;
72  }
73 
74  if (params.id == NULL) {
75  PyErr_SetString(PyExc_ValueError, "'id' argument not given");
76  goto fail;
77  }
78 
79  if ((params.type_id != NULL) &&
81  rna_enum_property_type_items, params.type_id, &params.type, "'type' enum value") == -1) {
82  goto fail;
83  }
84  else {
86  }
87 
88  if ((params.array_length < 1 || params.array_length > RNA_MAX_ARRAY_LENGTH)) {
89  PyErr_SetString(PyExc_ValueError, "'array_length' out of range");
90  goto fail;
91  }
92 
93  WM_gizmotype_target_property_def(gzt, params.id, params.type, params.array_length);
94  Py_DECREF(empty_tuple);
95  return true;
96 
97 fail:
98  Py_DECREF(empty_tuple);
99  return false;
100 }
101 
103 {
104  PyTypeObject *py_class = gzt->rna_ext.data;
106 
107  /* only call this so pyrna_deferred_register_class gives a useful error
108  * WM_operatortype_append_ptr will call RNA_def_struct_identifier
109  * later */
111 
112  if (pyrna_deferred_register_class(gzt->srna, py_class) != 0) {
113  PyErr_Print(); /* failed to register operator props */
114  PyErr_Clear();
115  }
116 
117  /* Extract target property definitions from 'bl_target_properties' */
118  {
119  /* Picky developers will notice that 'bl_targets' won't work with inheritance
120  * get direct from the dict to avoid raising a load of attribute errors
121  * (yes this isn't ideal) - campbell. */
122  PyObject *py_class_dict = py_class->tp_dict;
123  PyObject *bl_target_properties = PyDict_GetItem(py_class_dict,
125 
126  /* Some widgets may only exist to activate operators. */
127  if (bl_target_properties != NULL) {
128  PyObject *bl_target_properties_fast;
129  if (!(bl_target_properties_fast = PySequence_Fast(bl_target_properties,
130  "bl_target_properties sequence"))) {
131  /* PySequence_Fast sets the error */
132  PyErr_Print();
133  PyErr_Clear();
134  return;
135  }
136 
137  const uint items_len = PySequence_Fast_GET_SIZE(bl_target_properties_fast);
138  PyObject **items = PySequence_Fast_ITEMS(bl_target_properties_fast);
139 
140  for (uint i = 0; i < items_len; i++) {
141  if (!bpy_gizmotype_target_property_def(gzt, items[i])) {
142  PyErr_Print();
143  PyErr_Clear();
144  break;
145  }
146  }
147 
148  Py_DECREF(bl_target_properties_fast);
149  }
150  }
151 }
152 
153 void BPY_RNA_gizmo_wrapper(wmGizmoType *gzt, void *userdata)
154 {
155  /* take care not to overwrite anything set in
156  * WM_gizmomaptype_group_link_ptr before opfunc() is called */
157  StructRNA *srna = gzt->srna;
158  *gzt = *((wmGizmoType *)userdata);
159  gzt->srna = srna; /* restore */
160 
161  /* don't do translations here yet */
162 #if 0
163  /* Use i18n context from rna_ext.srna if possible (py gizmogroups). */
164  if (gt->rna_ext.srna) {
166  }
167 #endif
168 
169  gzt->struct_size = sizeof(wmGizmo);
170 
172 }
173 
176 /* -------------------------------------------------------------------- */
181 {
182  PyTypeObject *py_class = gzgt->rna_ext.data;
184 
185  /* only call this so pyrna_deferred_register_class gives a useful error
186  * WM_operatortype_append_ptr will call RNA_def_struct_identifier
187  * later */
189 
190  if (pyrna_deferred_register_class(gzgt->srna, py_class) != 0) {
191  PyErr_Print(); /* failed to register operator props */
192  PyErr_Clear();
193  }
194 }
195 
196 void BPY_RNA_gizmogroup_wrapper(wmGizmoGroupType *gzgt, void *userdata)
197 {
198  /* take care not to overwrite anything set in
199  * WM_gizmomaptype_group_link_ptr before opfunc() is called */
200  StructRNA *srna = gzgt->srna;
201  *gzgt = *((wmGizmoGroupType *)userdata);
202  gzgt->srna = srna; /* restore */
203 
204  /* don't do translations here yet */
205 #if 0
206  /* Use i18n context from rna_ext.srna if possible (py gizmogroups). */
207  if (gzgt->rna_ext.srna) {
209  }
210 #endif
211 
213 }
214 
unsigned int uint
Definition: BLI_sys_types.h:83
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
#define RNA_MAX_ARRAY_LENGTH
Definition: RNA_define.h:39
@ PROP_FLOAT
Definition: RNA_types.h:75
struct wmGizmo wmGizmo
Definition: WM_api.h:74
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_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value, const char *error_prefix)
Definition: bpy_rna.c:804
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8122
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
Definition: rna_access.c:1044
const char * RNA_struct_translation_context(const StructRNA *type)
Definition: rna_access.c:756
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
Definition: rna_define.c:1249
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
Definition: rna_define.c:1272
const EnumPropertyItem rna_enum_property_type_items[]
Definition: rna_rna.c:56
StructRNA * srna
Definition: RNA_types.h:681
void * data
Definition: RNA_types.h:680
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)