Blender  V2.93
rna_wm_gizmo_api.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 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "BLI_utildefines.h"
25 
26 #include "BKE_report.h"
27 
28 #include "RNA_define.h"
29 #include "RNA_enum_types.h"
30 
32 
33 #include "WM_api.h"
34 
35 #include "rna_internal.h" /* own include */
36 
37 #ifdef RNA_RUNTIME
38 
39 # include "BKE_context.h"
40 # include "UI_interface.h"
41 
42 # include "ED_gizmo_library.h"
43 
44 static void rna_gizmo_draw_preset_box(wmGizmo *gz, float matrix[16], int select_id)
45 {
46  ED_gizmo_draw_preset_box(gz, (float(*)[4])matrix, select_id);
47 }
48 
49 static void rna_gizmo_draw_preset_arrow(wmGizmo *gz, float matrix[16], int axis, int select_id)
50 {
51  ED_gizmo_draw_preset_arrow(gz, (float(*)[4])matrix, axis, select_id);
52 }
53 
54 static void rna_gizmo_draw_preset_circle(wmGizmo *gz, float matrix[16], int axis, int select_id)
55 {
56  ED_gizmo_draw_preset_circle(gz, (float(*)[4])matrix, axis, select_id);
57 }
58 
59 static void rna_gizmo_draw_preset_facemap(
60  wmGizmo *gz, struct bContext *C, struct Object *ob, int facemap, int select_id)
61 {
62  ED_gizmo_draw_preset_facemap(C, gz, ob, facemap, select_id);
63 }
64 
65 /* -------------------------------------------------------------------- */
69 static void rna_gizmo_target_set_prop(wmGizmo *gz,
70  ReportList *reports,
71  const char *target_propname,
72  PointerRNA *ptr,
73  const char *propname,
74  int index)
75 {
77  target_propname);
78  if (gz_prop_type == NULL) {
79  BKE_reportf(reports,
80  RPT_ERROR,
81  "Gizmo target property '%s.%s' not found",
82  gz->type->idname,
83  target_propname);
84  return;
85  }
86 
87  PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
88  if (prop == NULL) {
89  BKE_reportf(reports,
90  RPT_ERROR,
91  "Property '%s.%s' not found",
93  propname);
94  return;
95  }
96 
97  if (gz_prop_type->data_type != RNA_property_type(prop)) {
98  const int gizmo_type_index = RNA_enum_from_value(rna_enum_property_type_items,
99  gz_prop_type->data_type);
100  const int prop_type_index = RNA_enum_from_value(rna_enum_property_type_items,
101  RNA_property_type(prop));
102  BLI_assert((gizmo_type_index != -1) && (prop_type_index == -1));
103 
104  BKE_reportf(reports,
105  RPT_ERROR,
106  "Gizmo target '%s.%s' expects '%s', '%s.%s' is '%s'",
107  gz->type->idname,
108  target_propname,
109  rna_enum_property_type_items[gizmo_type_index].identifier,
111  propname,
112  rna_enum_property_type_items[prop_type_index].identifier);
113  return;
114  }
115 
116  if (RNA_property_array_check(prop)) {
117  if (index == -1) {
118  const int prop_array_length = RNA_property_array_length(ptr, prop);
119  if (gz_prop_type->array_length != prop_array_length) {
120  BKE_reportf(reports,
121  RPT_ERROR,
122  "Gizmo target property '%s.%s' expects an array of length %d, found %d",
123  gz->type->idname,
124  target_propname,
125  gz_prop_type->array_length,
126  prop_array_length);
127  return;
128  }
129  }
130  }
131  else {
132  if (gz_prop_type->array_length != 1) {
133  BKE_reportf(reports,
134  RPT_ERROR,
135  "Gizmo target property '%s.%s' expects an array of length %d",
136  gz->type->idname,
137  target_propname,
138  gz_prop_type->array_length);
139  return;
140  }
141  }
142 
143  if (index >= gz_prop_type->array_length) {
144  BKE_reportf(reports,
145  RPT_ERROR,
146  "Gizmo target property '%s.%s', index %d must be below %d",
147  gz->type->idname,
148  target_propname,
149  index,
150  gz_prop_type->array_length);
151  return;
152  }
153 
154  WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
155 }
156 
157 static PointerRNA rna_gizmo_target_set_operator(wmGizmo *gz,
158  ReportList *reports,
159  const char *opname,
160  int part_index)
161 {
163 
164  ot = WM_operatortype_find(opname, 0); /* print error next */
165  if (!ot || !ot->srna) {
166  BKE_reportf(
167  reports, RPT_ERROR, "%s '%s'", ot ? "unknown operator" : "operator missing srna", opname);
168  return PointerRNA_NULL;
169  }
170 
171  /* For the return value to be usable, we need 'PointerRNA.data' to be set. */
172  IDProperty *properties;
173  {
174  IDPropertyTemplate val = {0};
175  properties = IDP_New(IDP_GROUP, &val, "wmGizmoProperties");
176  }
177 
178  return *WM_gizmo_operator_set(gz, part_index, ot, properties);
179 }
180 
183 /* -------------------------------------------------------------------- */
187 static bool rna_gizmo_target_is_valid(wmGizmo *gz,
188  ReportList *reports,
189  const char *target_propname)
190 {
191  wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, target_propname);
192  if (gz_prop == NULL) {
193  BKE_reportf(reports,
194  RPT_ERROR,
195  "Gizmo target property '%s.%s' not found",
196  gz->type->idname,
197  target_propname);
198  return false;
199  }
200  return WM_gizmo_target_property_is_valid(gz_prop);
201 }
202 
205 #else
206 
208 {
209  /* Utility draw functions, since we don't expose new OpenGL drawing wrappers via Python yet.
210  * exactly how these should be exposed isn't totally clear.
211  * However it's probably good to have some high level API's for this anyway.
212  * Just note that this could be re-worked once tests are done.
213  */
214 
215  FunctionRNA *func;
216  PropertyRNA *parm;
217 
218  /* -------------------------------------------------------------------- */
219  /* Primitive Shapes */
220 
221  /* draw_preset_box */
222  func = RNA_def_function(srna, "draw_preset_box", "rna_gizmo_draw_preset_box");
223  RNA_def_function_ui_description(func, "Draw a box");
224  parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
227  RNA_def_property_ui_text(parm, "", "The matrix to transform");
228  RNA_def_int(func, "select_id", -1, -1, INT_MAX, "Zero when not selecting", "", -1, INT_MAX);
229 
230  /* draw_preset_box */
231  func = RNA_def_function(srna, "draw_preset_arrow", "rna_gizmo_draw_preset_arrow");
232  RNA_def_function_ui_description(func, "Draw a box");
233  parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
236  RNA_def_property_ui_text(parm, "", "The matrix to transform");
237  RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
238  RNA_def_int(func, "select_id", -1, -1, INT_MAX, "Zero when not selecting", "", -1, INT_MAX);
239 
240  func = RNA_def_function(srna, "draw_preset_circle", "rna_gizmo_draw_preset_circle");
241  RNA_def_function_ui_description(func, "Draw a box");
242  parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
245  RNA_def_property_ui_text(parm, "", "The matrix to transform");
246  RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
247  RNA_def_int(func, "select_id", -1, -1, INT_MAX, "Zero when not selecting", "", -1, INT_MAX);
248 
249  /* -------------------------------------------------------------------- */
250  /* Other Shapes */
251 
252  /* draw_preset_facemap */
253  func = RNA_def_function(srna, "draw_preset_facemap", "rna_gizmo_draw_preset_facemap");
254  RNA_def_function_ui_description(func, "Draw the face-map of a mesh object");
256  parm = RNA_def_pointer(func, "object", "Object", "", "Object");
258  parm = RNA_def_int(func, "face_map", 0, 0, INT_MAX, "Face map index", "", 0, INT_MAX);
260  RNA_def_int(func, "select_id", -1, -1, INT_MAX, "Zero when not selecting", "", -1, INT_MAX);
261 
262  /* -------------------------------------------------------------------- */
263  /* Property API */
264 
265  /* Define Properties */
266  /* note, 'target_set_handler' is defined in 'bpy_rna_gizmo.c' */
267  func = RNA_def_function(srna, "target_set_prop", "rna_gizmo_target_set_prop");
270  parm = RNA_def_string(func, "target", NULL, 0, "", "Target property");
272  /* similar to UILayout.prop */
273  parm = RNA_def_pointer(func, "data", "AnyType", "", "Data from which to take property");
275  parm = RNA_def_string(func, "property", NULL, 0, "", "Identifier of property in data");
277  RNA_def_int(func, "index", -1, -1, INT_MAX, "", "", -1, INT_MAX); /* RNA_NO_INDEX == -1 */
278 
279  func = RNA_def_function(srna, "target_set_operator", "rna_gizmo_target_set_operator");
282  "Operator to run when activating the gizmo "
283  "(overrides property targets)");
284  parm = RNA_def_string(func, "operator", NULL, 0, "", "Target operator");
286  RNA_def_int(func, "index", 0, 0, 255, "Part index", "", 0, 255);
287 
288  /* similar to UILayout.operator */
289  parm = RNA_def_pointer(
290  func, "properties", "OperatorProperties", "", "Operator properties to fill in");
292  RNA_def_function_return(func, parm);
293 
294  /* Access Properties */
295  /* note, 'target_get', 'target_set' is defined in 'bpy_rna_gizmo.c' */
296  func = RNA_def_function(srna, "target_is_valid", "rna_gizmo_target_is_valid");
298  parm = RNA_def_string(func, "property", NULL, 0, "", "Property identifier");
301  parm = RNA_def_boolean(func, "result", 0, "", "");
302  RNA_def_function_return(func, parm);
303 }
304 
306 {
307  /* nothing yet */
308 }
309 
310 #endif
struct IDProperty * IDP_New(const char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:907
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define UNUSED(x)
@ IDP_GROUP
Definition: DNA_ID.h:101
const int facemap[6][4]
Definition: Projections.cpp:57
@ PARM_RNAPTR
Definition: RNA_types.h:339
@ PARM_REQUIRED
Definition: RNA_types.h:337
@ FUNC_USE_REPORTS
Definition: RNA_types.h:578
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:577
@ PROP_FLOAT
Definition: RNA_types.h:75
@ PROP_NEVER_NULL
Definition: RNA_types.h:225
@ PROP_MATRIX
Definition: RNA_types.h:144
#define C
Definition: RandGen.cpp:39
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:723
bool RNA_property_array_check(PropertyRNA *prop)
Definition: rna_access.c:1223
PropertyType RNA_property_type(PropertyRNA *prop)
Definition: rna_access.c:1155
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:71
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1218
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
Definition: rna_access.c:1902
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4159
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4302
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1676
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4262
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
Definition: rna_define.c:1629
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4337
const int rna_matrix_dimsize_4x4[]
Definition: rna_define.c:1626
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1279
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4332
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:3675
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:3585
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:3771
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1547
const EnumPropertyItem rna_enum_object_axis_items[]
Definition: rna_object.c:302
const EnumPropertyItem rna_enum_property_type_items[]
Definition: rna_rna.c:56
void RNA_api_gizmo(StructRNA *srna)
void RNA_api_gizmogroup(StructRNA *UNUSED(srna))
const char * identifier
Definition: RNA_types.h:446
struct StructRNA * type
Definition: RNA_types.h:51
const char * idname
const struct wmGizmoType * type
struct StructRNA * srna
Definition: WM_types.h:802
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition: wm_gizmo.c:232
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)