Blender  V3.3
rna_wm_gizmo_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include "BLI_utildefines.h"
11 
12 #include "BKE_report.h"
13 
14 #include "RNA_define.h"
15 #include "RNA_enum_types.h"
16 
18 
19 #include "WM_api.h"
20 
21 #include "rna_internal.h" /* own include */
22 
23 #ifdef RNA_RUNTIME
24 
25 # include "BKE_context.h"
26 # include "UI_interface.h"
27 
28 # include "ED_gizmo_library.h"
29 
30 static void rna_gizmo_draw_preset_box(wmGizmo *gz, float matrix[16], int select_id)
31 {
32  ED_gizmo_draw_preset_box(gz, (float(*)[4])matrix, select_id);
33 }
34 
35 static void rna_gizmo_draw_preset_arrow(wmGizmo *gz, float matrix[16], int axis, int select_id)
36 {
37  ED_gizmo_draw_preset_arrow(gz, (float(*)[4])matrix, axis, select_id);
38 }
39 
40 static void rna_gizmo_draw_preset_circle(wmGizmo *gz, float matrix[16], int axis, int select_id)
41 {
42  ED_gizmo_draw_preset_circle(gz, (float(*)[4])matrix, axis, select_id);
43 }
44 
45 static void rna_gizmo_draw_preset_facemap(
46  wmGizmo *gz, struct bContext *C, struct Object *ob, int facemap, int select_id)
47 {
48  ED_gizmo_draw_preset_facemap(C, gz, ob, facemap, select_id);
49 }
50 
51 /* -------------------------------------------------------------------- */
55 static void rna_gizmo_target_set_prop(wmGizmo *gz,
56  ReportList *reports,
57  const char *target_propname,
58  PointerRNA *ptr,
59  const char *propname,
60  int index)
61 {
63  target_propname);
64  if (gz_prop_type == NULL) {
65  BKE_reportf(reports,
66  RPT_ERROR,
67  "Gizmo target property '%s.%s' not found",
68  gz->type->idname,
69  target_propname);
70  return;
71  }
72 
73  PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
74  if (prop == NULL) {
75  BKE_reportf(reports,
76  RPT_ERROR,
77  "Property '%s.%s' not found",
79  propname);
80  return;
81  }
82 
83  if (gz_prop_type->data_type != RNA_property_type(prop)) {
84  const int gizmo_type_index = RNA_enum_from_value(rna_enum_property_type_items,
85  gz_prop_type->data_type);
86  const int prop_type_index = RNA_enum_from_value(rna_enum_property_type_items,
87  RNA_property_type(prop));
88  BLI_assert((gizmo_type_index != -1) && (prop_type_index == -1));
89 
90  BKE_reportf(reports,
91  RPT_ERROR,
92  "Gizmo target '%s.%s' expects '%s', '%s.%s' is '%s'",
93  gz->type->idname,
94  target_propname,
95  rna_enum_property_type_items[gizmo_type_index].identifier,
97  propname,
98  rna_enum_property_type_items[prop_type_index].identifier);
99  return;
100  }
101 
102  if (RNA_property_array_check(prop)) {
103  if (index == -1) {
104  const int prop_array_length = RNA_property_array_length(ptr, prop);
105  if (gz_prop_type->array_length != prop_array_length) {
106  BKE_reportf(reports,
107  RPT_ERROR,
108  "Gizmo target property '%s.%s' expects an array of length %d, found %d",
109  gz->type->idname,
110  target_propname,
111  gz_prop_type->array_length,
112  prop_array_length);
113  return;
114  }
115  }
116  }
117  else {
118  if (gz_prop_type->array_length != 1) {
119  BKE_reportf(reports,
120  RPT_ERROR,
121  "Gizmo target property '%s.%s' expects an array of length %d",
122  gz->type->idname,
123  target_propname,
124  gz_prop_type->array_length);
125  return;
126  }
127  }
128 
129  if (index >= gz_prop_type->array_length) {
130  BKE_reportf(reports,
131  RPT_ERROR,
132  "Gizmo target property '%s.%s', index %d must be below %d",
133  gz->type->idname,
134  target_propname,
135  index,
136  gz_prop_type->array_length);
137  return;
138  }
139 
140  WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
141 }
142 
143 static PointerRNA rna_gizmo_target_set_operator(wmGizmo *gz,
144  ReportList *reports,
145  const char *opname,
146  int part_index)
147 {
149 
150  ot = WM_operatortype_find(opname, 0); /* print error next */
151  if (!ot || !ot->srna) {
152  BKE_reportf(
153  reports, RPT_ERROR, "%s '%s'", ot ? "unknown operator" : "operator missing srna", opname);
154  return PointerRNA_NULL;
155  }
156 
157  /* For the return value to be usable, we need 'PointerRNA.data' to be set. */
158  IDProperty *properties;
159  {
160  IDPropertyTemplate val = {0};
161  properties = IDP_New(IDP_GROUP, &val, "wmGizmoProperties");
162  }
163 
164  return *WM_gizmo_operator_set(gz, part_index, ot, properties);
165 }
166 
169 /* -------------------------------------------------------------------- */
173 static bool rna_gizmo_target_is_valid(wmGizmo *gz,
174  ReportList *reports,
175  const char *target_propname)
176 {
177  wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, target_propname);
178  if (gz_prop == NULL) {
179  BKE_reportf(reports,
180  RPT_ERROR,
181  "Gizmo target property '%s.%s' not found",
182  gz->type->idname,
183  target_propname);
184  return false;
185  }
186  return WM_gizmo_target_property_is_valid(gz_prop);
187 }
188 
191 #else
192 
194 {
195  /* Utility draw functions, since we don't expose new OpenGL drawing wrappers via Python yet.
196  * exactly how these should be exposed isn't totally clear.
197  * However it's probably good to have some high level API's for this anyway.
198  * Just note that this could be re-worked once tests are done.
199  */
200 
201  FunctionRNA *func;
202  PropertyRNA *parm;
203 
204  /* -------------------------------------------------------------------- */
205  /* Primitive Shapes */
206 
207  /* draw_preset_box */
208  func = RNA_def_function(srna, "draw_preset_box", "rna_gizmo_draw_preset_box");
209  RNA_def_function_ui_description(func, "Draw a box");
210  parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
213  RNA_def_property_ui_text(parm, "", "The matrix to transform");
214  RNA_def_int(func,
215  "select_id",
216  -1,
217  -1,
218  INT_MAX,
219  "ID to use when gizmo is selectable. Use -1 when not selecting",
220  "",
221  -1,
222  INT_MAX);
223 
224  /* draw_preset_box */
225  func = RNA_def_function(srna, "draw_preset_arrow", "rna_gizmo_draw_preset_arrow");
226  RNA_def_function_ui_description(func, "Draw a box");
227  parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
230  RNA_def_property_ui_text(parm, "", "The matrix to transform");
231  RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
232  RNA_def_int(func,
233  "select_id",
234  -1,
235  -1,
236  INT_MAX,
237  "ID to use when gizmo is selectable. Use -1 when not selecting",
238  "",
239  -1,
240  INT_MAX);
241 
242  func = RNA_def_function(srna, "draw_preset_circle", "rna_gizmo_draw_preset_circle");
243  RNA_def_function_ui_description(func, "Draw a box");
244  parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
247  RNA_def_property_ui_text(parm, "", "The matrix to transform");
248  RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
249  RNA_def_int(func,
250  "select_id",
251  -1,
252  -1,
253  INT_MAX,
254  "ID to use when gizmo is selectable. Use -1 when not selecting",
255  "",
256  -1,
257  INT_MAX);
258 
259  /* -------------------------------------------------------------------- */
260  /* Other Shapes */
261 
262  /* draw_preset_facemap */
263  func = RNA_def_function(srna, "draw_preset_facemap", "rna_gizmo_draw_preset_facemap");
264  RNA_def_function_ui_description(func, "Draw the face-map of a mesh object");
266  parm = RNA_def_pointer(func, "object", "Object", "", "Object");
268  parm = RNA_def_int(func, "face_map", 0, 0, INT_MAX, "Face map index", "", 0, INT_MAX);
270  RNA_def_int(func,
271  "select_id",
272  -1,
273  -1,
274  INT_MAX,
275  "ID to use when gizmo is selectable. Use -1 when not selecting",
276  "",
277  -1,
278  INT_MAX);
279 
280  /* -------------------------------------------------------------------- */
281  /* Property API */
282 
283  /* Define Properties */
284  /* NOTE: 'target_set_handler' is defined in `bpy_rna_gizmo.c`. */
285  func = RNA_def_function(srna, "target_set_prop", "rna_gizmo_target_set_prop");
288  parm = RNA_def_string(func, "target", NULL, 0, "", "Target property");
290  /* similar to UILayout.prop */
291  parm = RNA_def_pointer(func, "data", "AnyType", "", "Data from which to take property");
293  parm = RNA_def_string(func, "property", NULL, 0, "", "Identifier of property in data");
295  RNA_def_int(func, "index", -1, -1, INT_MAX, "", "", -1, INT_MAX); /* RNA_NO_INDEX == -1 */
296 
297  func = RNA_def_function(srna, "target_set_operator", "rna_gizmo_target_set_operator");
300  "Operator to run when activating the gizmo "
301  "(overrides property targets)");
302  parm = RNA_def_string(func, "operator", NULL, 0, "", "Target operator");
304  RNA_def_int(func, "index", 0, 0, 255, "Part index", "", 0, 255);
305 
306  /* similar to UILayout.operator */
307  parm = RNA_def_pointer(
308  func, "properties", "OperatorProperties", "", "Operator properties to fill in");
310  RNA_def_function_return(func, parm);
311 
312  /* Access Properties */
313  /* NOTE: 'target_get', 'target_set' is defined in `bpy_rna_gizmo.c`. */
314  func = RNA_def_function(srna, "target_is_valid", "rna_gizmo_target_is_valid");
316  parm = RNA_def_string(func, "property", NULL, 0, "", "Property identifier");
319  parm = RNA_def_boolean(func, "result", 0, "", "");
320  RNA_def_function_return(func, parm);
321 }
322 
324 {
325  /* nothing yet */
326 }
327 
328 #endif
struct IDProperty * IDP_New(char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:887
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED(x)
@ IDP_GROUP
Definition: DNA_ID.h:141
const int facemap[6][4]
Definition: Projections.cpp:43
@ PARM_RNAPTR
Definition: RNA_types.h:354
@ PARM_REQUIRED
Definition: RNA_types.h:352
@ FUNC_USE_REPORTS
Definition: RNA_types.h:663
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:662
@ PROP_FLOAT
Definition: RNA_types.h:61
@ PROP_NEVER_NULL
Definition: RNA_types.h:239
@ PROP_MATRIX
Definition: RNA_types.h:158
#define C
Definition: RandGen.cpp:25
void ED_gizmo_draw_preset_circle(const struct wmGizmo *gz, float mat[4][4], int axis, int select_id)
void ED_gizmo_draw_preset_arrow(const struct wmGizmo *gz, float mat[4][4], int axis, int select_id)
void ED_gizmo_draw_preset_box(const struct wmGizmo *gz, float mat[4][4], int select_id)
void ED_gizmo_draw_preset_facemap(const bContext *C, const struct wmGizmo *gz, Object *ob, const int facemap, int select_id)
const char * RNA_struct_identifier(const StructRNA *type)
Definition: rna_access.c:586
bool RNA_property_array_check(PropertyRNA *prop)
Definition: rna_access.c:1080
PropertyType RNA_property_type(PropertyRNA *prop)
Definition: rna_access.c:1010
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:61
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:717
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1075
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
Definition: rna_access.c:1736
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4170
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4312
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1645
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4273
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
Definition: rna_define.c:1598
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4347
const int rna_matrix_dimsize_4x4[]
Definition: rna_define.c:1595
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1257
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4342
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3687
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3597
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1518
const EnumPropertyItem rna_enum_object_axis_items[]
Definition: rna_object.c:297
const EnumPropertyItem rna_enum_property_type_items[]
Definition: rna_rna.c:42
void RNA_api_gizmo(StructRNA *srna)
void RNA_api_gizmogroup(StructRNA *UNUSED(srna))
const char * identifier
Definition: RNA_types.h:461
struct StructRNA * type
Definition: RNA_types.h:37
const char * idname
const struct wmGizmoType * type
struct StructRNA * srna
Definition: WM_types.h:969
PointerRNA * ptr
Definition: wm_files.c:3480
wmOperatorType * ot
Definition: wm_files.c:3479
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition: wm_gizmo.c:203
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
void WM_gizmo_target_property_def_rna_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, PointerRNA *ptr, PropertyRNA *prop, int index)
const wmGizmoPropertyType * WM_gizmotype_target_property_find(const wmGizmoType *gzt, const char *idname)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)