Blender V5.3
editmesh_add_gizmo.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
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_mesh_types.h"
14#include "DNA_object_types.h"
15#include "DNA_scene_types.h"
16
17#include "BKE_context.hh"
18#include "BKE_editmesh.hh"
19#include "BKE_object_types.hh"
20#include "BKE_scene.hh"
21
22#include "BLI_math_geom_c.hh"
23#include "BLI_math_matrix_c.hh"
24#include "BLI_math_vector_c.hh"
25
26#include "ED_gizmo_library.hh"
27#include "ED_gizmo_utils.hh"
28#include "ED_mesh.hh"
29#include "ED_object.hh"
30#include "ED_screen.hh"
31#include "ED_undo.hh"
32#include "ED_view3d.hh"
33
34#include "RNA_access.hh"
35#include "RNA_define.hh"
36
37#include "WM_api.hh"
38#include "WM_types.hh"
39
40#include "UI_resources.hh"
41
42#include "mesh_intern.hh" /* own include */
43
44namespace blender {
45
46/* -------------------------------------------------------------------- */
49
58 const float mval[2],
59 float r_location[3],
60 float r_rotation[3][3])
61{
62
63 Scene *scene = CTX_data_scene(C);
64 ARegion *region = CTX_wm_region(C);
65 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
66
67 bool use_mouse_project = true; /* TODO: make optional */
68
69 const float4x4 cursor_matrix = scene->cursor.matrix<float4x4>();
70 float orient_matrix[3][3];
71
72 const float dots[3] = {
73 dot_v3v3(rv3d->viewinv[2], cursor_matrix[0]),
74 dot_v3v3(rv3d->viewinv[2], cursor_matrix[1]),
75 dot_v3v3(rv3d->viewinv[2], cursor_matrix[2]),
76 };
77 const int axis = axis_dominant_v3_single(dots);
78
79 copy_v3_v3(orient_matrix[0], cursor_matrix[(axis + 1) % 3]);
80 copy_v3_v3(orient_matrix[1], cursor_matrix[(axis + 2) % 3]);
81 copy_v3_v3(orient_matrix[2], cursor_matrix[axis]);
82
83 if (dot_v3v3(rv3d->viewinv[2], orient_matrix[2]) < 0.0f) {
84 negate_v3(orient_matrix[2]);
85 }
86 if (is_negative_m3(orient_matrix)) {
87 swap_v3_v3(orient_matrix[0], orient_matrix[1]);
88 }
89
90 if (use_mouse_project) {
91 float plane[4];
92 plane_from_point_normal_v3(plane, cursor_matrix[3], orient_matrix[2]);
93 if (ED_view3d_win_to_3d_on_plane(region, plane, mval, true, r_location)) {
94 copy_m3_m3(r_rotation, orient_matrix);
95 return;
96 }
97 }
98
99 /* fallback */
100 copy_v3_v3(r_location, cursor_matrix[3]);
101 copy_m3_m3(r_rotation, orient_matrix);
102}
103
105
106/* -------------------------------------------------------------------- */
109
118
125{
126 wmOperator *op = ggd->data.op;
127 if (op == WM_operator_last_redo(ggd->data.context)) {
128 ED_undo_operator_repeat(ggd->data.context, op);
129 }
130}
131
133{
134 wmOperator *op = ggd->data.op;
135 UNUSED_VARS(op);
136/* For now don't read back from the operator. */
137#if 0
138 RNA_property_float_get_array(op->ptr, ggd->data.prop_matrix, &ggd->cage->matrix_offset[0][0]);
139#endif
140}
141
142/* translate callbacks */
144 wmGizmoProperty *gz_prop,
145 void *value_p)
146{
148 wmOperator *op = ggd->data.op;
149 float *value = static_cast<float *>(value_p);
150 BLI_assert(gz_prop->type->array_length == 16);
151 UNUSED_VARS_NDEBUG(gz_prop);
152
153 if (value_p != ggd->cage->matrix_offset) {
155 static_cast<float (*)[4]>(value_p), ggd->cage->matrix_basis, ggd->cage->matrix_offset);
156 RNA_property_float_get_array(op->ptr, ggd->data.prop_matrix, value);
157 }
158}
159
161 wmGizmoProperty *gz_prop,
162 const void *value)
163{
165 wmOperator *op = ggd->data.op;
166
167 BLI_assert(gz_prop->type->array_length == 16);
168 UNUSED_VARS_NDEBUG(gz_prop);
169
170 float mat[4][4];
171 mul_m4_m4m4(mat, ggd->cage->matrix_basis, static_cast<const float (*)[4]>(value));
172
173 if (is_negative_m4(mat)) {
174 negate_mat3_m4(mat);
175 }
176
177 RNA_property_float_set_array(op->ptr, ggd->data.prop_matrix, &mat[0][0]);
178
180}
181
183{
185 C, gzgt, "MESH_OT_primitive_cube_add_gizmo");
186}
187
189{
190 GizmoPlacementGroup *ggd = static_cast<GizmoPlacementGroup *>(gzgroup->customdata);
191
192 /* Initial size. */
193 {
194 wmGizmo *gz = ggd->cage;
196
197 /* TODO: support zero scaled matrix in 'GIZMO_GT_cage_3d'. */
198 gz->matrix_offset[0][0] = 0.01;
199 gz->matrix_offset[1][1] = 0.01;
200 gz->matrix_offset[2][2] = 0.01;
201 gz->matrix_offset[3][3] = 1.0f;
202 }
203
204 /* Start off dragging. */
205 {
206 wmWindow *win = CTX_wm_window(C);
207 ARegion *region = CTX_wm_region(C);
208 wmGizmo *gz = ggd->cage;
209
210 {
211 float mat3[3][3];
212 float location[3];
213 float mval[2] = {
214 float(win->runtime->eventstate->xy[0] - region->winrct.xmin),
215 float(win->runtime->eventstate->xy[1] - region->winrct.ymin),
216 };
217 calc_initial_placement_point_from_view(const_cast<bContext *>(C), mval, location, mat3);
218 copy_m4_m3(gz->matrix_basis, mat3);
219 copy_v3_v3(gz->matrix_basis[3], location);
220 }
221
222 if (true) {
223 wmGizmoMap *gzmap = gzgroup->parent_gzmap;
225 const_cast<bContext *>(C),
226 ggd->cage,
228 win->runtime->eventstate);
229 }
230 }
231}
232
234{
236
237 if (op == nullptr || !STREQ(op->type->idname, "MESH_OT_primitive_cube_add_gizmo")) {
238 return;
239 }
240
242 gzgroup->customdata = ggd;
243
244 const wmGizmoType *gzt_cage = WM_gizmotype_find("GIZMO_GT_cage_3d", true);
245
246 ggd->cage = WM_gizmo_new_ptr(gzt_cage, gzgroup, nullptr);
247
249
250 RNA_enum_set(ggd->cage->ptr,
251 "transform",
254
256
257 ggd->data.context = const_cast<bContext *>(C);
258 ggd->data.op = op;
259 ggd->data.prop_matrix = RNA_struct_find_property(op->ptr, "matrix");
260
262
263 /* Setup property callbacks */
264 {
268 params.range_get_fn = nullptr;
269 params.user_data = nullptr;
270 WM_gizmo_target_property_def_func(ggd->cage, "matrix", &params);
271 }
272
274}
275
276static void gizmo_mesh_placement_draw_prepare(const bContext * /*C*/, wmGizmoGroup *gzgroup)
277{
278 GizmoPlacementGroup *ggd = static_cast<GizmoPlacementGroup *>(gzgroup->customdata);
279 if (ggd->data.op->next) {
280 ggd->data.op = WM_operator_last_redo(ggd->data.context);
281 }
283}
284
286{
287 gzgt->name = "Mesh Add Bounds";
288 gzgt->idname = "MESH_GGT_add_bounds";
289
291
294
298}
299
301
302/* -------------------------------------------------------------------- */
309
311{
312 Object *obedit = CTX_data_edit_object(C);
314 float matrix[4][4];
315
316 /* Get the matrix that defines the cube bounds (as set by the gizmo cage). */
317 {
318 PropertyRNA *prop_matrix = RNA_struct_find_property(op->ptr, "matrix");
319 if (RNA_property_is_set(op->ptr, prop_matrix)) {
320 RNA_property_float_get_array(op->ptr, prop_matrix, &matrix[0][0]);
321 invert_m4_m4(obedit->runtime->world_to_object.ptr(), obedit->object_to_world().ptr());
322 mul_m4_m4m4(matrix, obedit->world_to_object().ptr(), matrix);
323 }
324 else {
325 /* For the first update the widget may not set the matrix. */
326 return OPERATOR_FINISHED;
327 }
328 }
329
330 const bool calc_uvs = RNA_boolean_get(op->ptr, "calc_uvs");
331
332 if (calc_uvs) {
333 ED_mesh_uv_ensure(id_cast<Mesh *>(obedit->data), nullptr);
334 }
335
337 op,
338 "verts.out",
339 false,
340 "create_cube matrix=%m4 size=%f calc_uvs=%b",
341 matrix,
342 1.0f,
343 calc_uvs))
344 {
345 return OPERATOR_CANCELLED;
346 }
347
349 /* TODO(@ideasman42): maintain UV sync for newly created data. */
351
352 EDBMUpdate_Params params{};
353 params.calc_looptris = true;
354 params.calc_normals = false;
355 params.is_destructive = true;
356 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
357
358 return OPERATOR_FINISHED;
359}
360
362 wmOperator *op,
363 const wmEvent * /*event*/)
364{
365 View3D *v3d = CTX_wm_view3d(C);
366
368 if (ret & OPERATOR_FINISHED) {
369 /* Setup gizmos */
370 if (v3d && ((v3d->gizmo_flag & V3D_GIZMO_HIDE) == 0)) {
371 wmGizmoGroupType *gzgt = WM_gizmogrouptype_find("MESH_GGT_add_bounds", false);
373 Main *bmain = CTX_data_main(C);
375 }
376 }
377 }
378
379 return ret;
380}
381
383{
384 /* identifiers */
385 ot->name = "Add Cube";
386 ot->description = "Construct a cube mesh";
387 ot->idname = "MESH_OT_primitive_cube_add_gizmo";
388
389 /* API callbacks. */
393
394 /* flags */
396
399
400 /* hidden props */
402 ot->srna, "matrix", 4, 4, nullptr, 0.0f, 0.0f, "Matrix", "", 0.0f, 0.0f);
404
406}
407
409
410} // namespace blender
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define STREQ(a, b)
void add_generic_props(wmOperatorType *ot, bool do_editmode)
void add_mesh_props(wmOperatorType *ot)
void get_color_3fv(int colorid, float col[3])
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_gizmo_modal_set_from_setup(wmGizmoMap *gzmap, bContext *C, wmGizmo *gz, int part_index, const wmEvent *event)
Definition wm_gizmo.cc:415
bool ED_undo_operator_repeat(bContext *C, wmOperator *op)
Definition ed_undo.cc:879
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool WM_gizmo_group_type_ensure_ptr(wmGizmoGroupType *gzgt)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
MINLINE void copy_v3_v3(float r[3], const float a[3])
static wmOperatorStatus add_primitive_cube_gizmo_exec(bContext *C, wmOperator *op)
static void gizmo_placement_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void negate_mat3_m4(float R[4][4])
void copy_m4_m3(float m1[4][4], const float m2[3][3])
const wmGizmoType * WM_gizmotype_find(const StringRef idname, bool quiet)
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:256
bool is_negative_m4(const float mat[4][4])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
wmWindow * CTX_wm_window(const bContext *C)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
static void gizmo_placement_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value)
wmGizmoGroupType * WM_gizmogrouptype_find(const StringRef idname, bool quiet)
@ ED_GIZMO_CAGE_XFORM_FLAG_SCALE
@ ED_GIZMO_CAGE_XFORM_FLAG_TRANSLATE
@ ED_GIZMO_CAGE_XFORM_FLAG_SCALE_SIGNED
MatBase< float, 4, 4 > float4x4
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
static void gizmo_mesh_placement_draw_prepare(const bContext *, wmGizmoGroup *gzgroup)
@ ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z
bool EDBM_op_call_and_selectf(BMesh *bm, wmOperator *op, const char *select_slot_out, const bool select_extend, const char *fmt,...)
static bool gizmo_mesh_placement_poll(const bContext *C, wmGizmoGroupType *gzgt)
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void swap_v3_v3(float a[3], float b[3])
BMesh const char void * data
bool ED_view3d_win_to_3d_on_plane(const ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
Object * CTX_data_edit_object(const bContext *C)
MINLINE int axis_dominant_v3_single(const float vec[3])
static void calc_initial_placement_point_from_view(bContext *C, const float mval[2], float r_location[3], float r_rotation[3][3])
MINLINE void negate_v3(float r[3])
static wmOperatorStatus add_primitive_cube_gizmo_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static void MESH_GGT_add_bounds(wmGizmoGroupType *gzgt)
@ WM_GIZMO_DRAW_VALUE
static void gizmo_placement_exec(GizmoPlacementGroup *ggd)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void EDBM_selectmode_flush_ex(BMesh *bm, short selectmode)
Main * CTX_data_main(const bContext *C)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
static void gizmo_mesh_placement_modal_from_setup(const bContext *C, wmGizmoGroup *gzgroup)
wmOperator * WM_operator_last_redo(const bContext *C)
void zero_m4(float m[4][4])
bool EDBM_uvselect_clear(BMesh *bm)
Scene * CTX_data_scene(const bContext *C)
bool ED_operator_editmesh_view3d(bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
PropertyRNA * RNA_def_float_matrix(StructOrFunctionRNA *cont_, const char *identifier, const int rows, const int columns, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void WM_gizmo_group_type_reinit_ptr(Main *bmain, wmGizmoGroupType *gzgt)
static void gizmo_mesh_placement_setup(const bContext *C, wmGizmoGroup *gzgroup)
wmGizmo * WM_gizmo_new_ptr(const wmGizmoType *gzt, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:84
static void gizmo_mesh_placement_update_from_op(GizmoPlacementGroup *ggd)
bool is_negative_m3(const float mat[3][3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ARegion * CTX_wm_region(const bContext *C)
bool ED_gizmo_poll_or_unlink_delayed_from_operator(const bContext *C, wmGizmoGroupType *gzgt, const char *idname)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
void MESH_OT_primitive_cube_add_gizmo(wmOperatorType *ot)
View3D * CTX_wm_view3d(const bContext *C)
wmGizmoGroupType * WM_gizmogrouptype_append(void(*wtfunc)(wmGizmoGroupType *))
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct blender::GizmoPlacementGroup::@166055250271344200267264006066216377220073264101 data
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
View3DCursor cursor
eView3D_GizmoFlag gizmo_flag
wmGizmoMapType_Params gzmap_params
wmGizmoGroupFnDrawPrepare draw_prepare
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoPropertyFnSet value_set_fn
wmGizmoPropertyFnRangeGet range_get_fn
wmGizmoPropertyFnGet value_get_fn
const wmGizmoPropertyType * type
float matrix_basis[4][4]
float matrix_offset[4][4]
wmGizmoGroup * parent_gzgroup
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct wmOperatorType * type
bke::WindowRuntime * runtime