Blender V4.3
rna_wm_gizmo_api.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
9#include <cstdio>
10#include <cstdlib>
11
12#include "BLI_utildefines.h"
13
14#include "BLT_translation.hh"
15
16#include "BKE_report.hh"
17
18#include "RNA_define.hh"
19#include "RNA_enum_types.hh"
20
22
23#include "WM_api.hh"
24
25#include "rna_internal.hh" /* own include */
26
27#ifdef RNA_RUNTIME
28
29# include "BKE_context.hh"
30# include "UI_interface.hh"
31
32# include "ED_gizmo_library.hh"
33
34static void rna_gizmo_draw_preset_box(wmGizmo *gz, const float matrix[16], int select_id)
35{
36 ED_gizmo_draw_preset_box(gz, (const float(*)[4])matrix, select_id);
37}
38
39static void rna_gizmo_draw_preset_arrow(wmGizmo *gz,
40 const float matrix[16],
41 int axis,
42 int select_id)
43{
44 ED_gizmo_draw_preset_arrow(gz, (const float(*)[4])matrix, axis, select_id);
45}
46
47static void rna_gizmo_draw_preset_circle(wmGizmo *gz,
48 const float matrix[16],
49 int axis,
50 int select_id)
51{
52 ED_gizmo_draw_preset_circle(gz, (const float(*)[4])matrix, axis, select_id);
53}
54
55/* -------------------------------------------------------------------- */
59static void rna_gizmo_target_set_prop(wmGizmo *gz,
60 ReportList *reports,
61 const char *target_propname,
63 const char *propname,
64 int index)
65{
67 target_propname);
68 if (gz_prop_type == nullptr) {
69 BKE_reportf(reports,
71 "Gizmo target property '%s.%s' not found",
72 gz->type->idname,
73 target_propname);
74 return;
75 }
76
77 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
78 if (prop == nullptr) {
79 BKE_reportf(reports,
81 "Property '%s.%s' not found",
83 propname);
84 return;
85 }
86
87 if (gz_prop_type->data_type != RNA_property_type(prop)) {
88 const int gizmo_type_index = RNA_enum_from_value(rna_enum_property_type_items,
89 gz_prop_type->data_type);
90 const int prop_type_index = RNA_enum_from_value(rna_enum_property_type_items,
91 RNA_property_type(prop));
92 BLI_assert((gizmo_type_index != -1) && (prop_type_index == -1));
93
94 BKE_reportf(reports,
96 "Gizmo target '%s.%s' expects '%s', '%s.%s' is '%s'",
97 gz->type->idname,
98 target_propname,
101 propname,
103 return;
104 }
105
106 if (RNA_property_array_check(prop)) {
107 if (index == -1) {
108 const int prop_array_length = RNA_property_array_length(ptr, prop);
109 if (gz_prop_type->array_length != prop_array_length) {
110 BKE_reportf(reports,
111 RPT_ERROR,
112 "Gizmo target property '%s.%s' expects an array of length %d, found %d",
113 gz->type->idname,
114 target_propname,
115 gz_prop_type->array_length,
116 prop_array_length);
117 return;
118 }
119 }
120 }
121 else {
122 if (gz_prop_type->array_length != 1) {
123 BKE_reportf(reports,
124 RPT_ERROR,
125 "Gizmo target property '%s.%s' expects an array of length %d",
126 gz->type->idname,
127 target_propname,
128 gz_prop_type->array_length);
129 return;
130 }
131 }
132
133 if (index >= gz_prop_type->array_length) {
134 BKE_reportf(reports,
135 RPT_ERROR,
136 "Gizmo target property '%s.%s', index %d must be below %d",
137 gz->type->idname,
138 target_propname,
139 index,
140 gz_prop_type->array_length);
141 return;
142 }
143
144 WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
145}
146
147static PointerRNA rna_gizmo_target_set_operator(wmGizmo *gz,
148 ReportList *reports,
149 const char *opname,
150 int part_index)
151{
153
154 ot = WM_operatortype_find(opname, false); /* print error next */
155 if (!ot || !ot->srna) {
156 BKE_reportf(reports,
157 RPT_ERROR,
158 "%s '%s'",
159 ot ? RPT_("Operator missing srna") : RPT_("Unknown operator"),
160 opname);
161 return PointerRNA_NULL;
162 }
163
164 /* For the return value to be usable, we need 'PointerRNA.data' to be set. */
165 IDProperty *properties = blender::bke::idprop::create_group("wmGizmoProperties").release();
166
167 return *WM_gizmo_operator_set(gz, part_index, ot, properties);
168}
169
172/* -------------------------------------------------------------------- */
176static bool rna_gizmo_target_is_valid(wmGizmo *gz,
177 ReportList *reports,
178 const char *target_propname)
179{
180 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, target_propname);
181 if (gz_prop == nullptr) {
182 BKE_reportf(reports,
183 RPT_ERROR,
184 "Gizmo target property '%s.%s' not found",
185 gz->type->idname,
186 target_propname);
187 return false;
188 }
189 return WM_gizmo_target_property_is_valid(gz_prop);
190}
191
194#else
195
197{
198 /* Utility draw functions, since we don't expose new OpenGL drawing wrappers via Python yet.
199 * exactly how these should be exposed isn't totally clear.
200 * However it's probably good to have some high level API's for this anyway.
201 * Just note that this could be re-worked once tests are done.
202 */
203
204 FunctionRNA *func;
205 PropertyRNA *parm;
206
207 /* -------------------------------------------------------------------- */
208 /* Primitive Shapes */
209
210 /* draw_preset_box */
211 func = RNA_def_function(srna, "draw_preset_box", "rna_gizmo_draw_preset_box");
212 RNA_def_function_ui_description(func, "Draw a box");
213 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
216 RNA_def_property_ui_text(parm, "", "The matrix to transform");
217 RNA_def_int(func,
218 "select_id",
219 -1,
220 -1,
221 INT_MAX,
222 "ID to use when gizmo is selectable. Use -1 when not selecting.",
223 "",
224 -1,
225 INT_MAX);
226
227 /* draw_preset_box */
228 func = RNA_def_function(srna, "draw_preset_arrow", "rna_gizmo_draw_preset_arrow");
229 RNA_def_function_ui_description(func, "Draw a box");
230 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
233 RNA_def_property_ui_text(parm, "", "The matrix to transform");
234 RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
235 RNA_def_int(func,
236 "select_id",
237 -1,
238 -1,
239 INT_MAX,
240 "ID to use when gizmo is selectable. Use -1 when not selecting.",
241 "",
242 -1,
243 INT_MAX);
244
245 func = RNA_def_function(srna, "draw_preset_circle", "rna_gizmo_draw_preset_circle");
246 RNA_def_function_ui_description(func, "Draw a box");
247 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
250 RNA_def_property_ui_text(parm, "", "The matrix to transform");
251 RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
252 RNA_def_int(func,
253 "select_id",
254 -1,
255 -1,
256 INT_MAX,
257 "ID to use when gizmo is selectable. Use -1 when not selecting.",
258 "",
259 -1,
260 INT_MAX);
261
262 /* -------------------------------------------------------------------- */
263 /* Other Shapes */
264
265 /* -------------------------------------------------------------------- */
266 /* Property API */
267
268 /* Define Properties */
269 /* NOTE: 'target_set_handler' is defined in `bpy_rna_gizmo.cc`. */
270 func = RNA_def_function(srna, "target_set_prop", "rna_gizmo_target_set_prop");
273 parm = RNA_def_string(func, "target", nullptr, 0, "", "Target property");
275 /* similar to UILayout.prop */
276 parm = RNA_def_pointer(func, "data", "AnyType", "", "Data from which to take property");
278 parm = RNA_def_string(func, "property", nullptr, 0, "", "Identifier of property in data");
280 RNA_def_int(func, "index", -1, -1, INT_MAX, "", "", -1, INT_MAX); /* RNA_NO_INDEX == -1 */
281
282 func = RNA_def_function(srna, "target_set_operator", "rna_gizmo_target_set_operator");
285 "Operator to run when activating the gizmo "
286 "(overrides property targets)");
287 parm = RNA_def_string(func, "operator", nullptr, 0, "", "Target operator");
289 RNA_def_int(func, "index", 0, 0, 255, "Part index", "", 0, 255);
290
291 /* similar to UILayout.operator */
292 parm = RNA_def_pointer(
293 func, "properties", "OperatorProperties", "", "Operator properties to fill in");
295 RNA_def_function_return(func, parm);
296
297 /* Access Properties */
298 /* NOTE: 'target_get', 'target_set' is defined in `bpy_rna_gizmo.cc`. */
299 func = RNA_def_function(srna, "target_is_valid", "rna_gizmo_target_is_valid");
301 parm = RNA_def_string(func, "property", nullptr, 0, "", "Property identifier");
304 parm = RNA_def_boolean(func, "result", false, "", "");
305 RNA_def_function_return(func, parm);
306}
307
309{
310 /* nothing yet */
311}
312
313#endif
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition BLI_assert.h:50
#define RPT_(msgid)
@ PARM_RNAPTR
Definition RNA_types.hh:399
@ PARM_REQUIRED
Definition RNA_types.hh:397
@ FUNC_USE_REPORTS
Definition RNA_types.hh:680
@ PROP_FLOAT
Definition RNA_types.hh:67
PropertyFlag
Definition RNA_types.hh:201
@ PROP_NEVER_NULL
Definition RNA_types.hh:266
@ PROP_MATRIX
Definition RNA_types.hh:168
constexpr PointerRNA PointerRNA_NULL
Definition RNA_types.hh:45
void ED_gizmo_draw_preset_box(const wmGizmo *gz, const float mat[4][4], int select_id)
void ED_gizmo_draw_preset_circle(const wmGizmo *gz, const float mat[4][4], int axis, int select_id)
void ED_gizmo_draw_preset_arrow(const wmGizmo *gz, const float mat[4][4], int axis, int select_id)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRefNull prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
bool RNA_property_array_check(PropertyRNA *prop)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
PropertyType RNA_property_type(PropertyRNA *prop)
const char * RNA_struct_identifier(const StructRNA *type)
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
const int rna_matrix_dimsize_4x4[]
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
void RNA_def_function_flag(FunctionRNA *func, int flag)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
const EnumPropertyItem rna_enum_object_axis_items[]
const EnumPropertyItem rna_enum_property_type_items[]
Definition rna_rna.cc:46
void RNA_api_gizmo(StructRNA *srna)
void RNA_api_gizmogroup(StructRNA *)
const char * identifier
Definition RNA_types.hh:506
StructRNA * type
Definition RNA_types.hh:41
const char * idname
const wmGizmoType * type
StructRNA * srna
Definition WM_types.hh:1080
PointerRNA * ptr
Definition wm_files.cc:4126
wmOperatorType * ot
Definition wm_files.cc:4125
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition wm_gizmo.cc:203
const wmGizmoPropertyType * WM_gizmotype_target_property_find(const wmGizmoType *gzt, const char *idname)
bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
void WM_gizmo_target_property_def_rna_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, PointerRNA *ptr, PropertyRNA *prop, int index)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)