From e5704d37de83191c08117bcc58bbc231554ae73d Mon Sep 17 00:00:00 2001 From: Matias Mendiola Date: Wed, 8 Nov 2023 16:05:54 +0100 Subject: [PATCH] GPv3: Switch Direction operator This Operator is similar to GPv2 GPENCIL_OT_stroke_flip to switch the stroke direction. Related to: [113590](113590) Pull Request: https://projects.blender.org/blender/blender/pulls/114007 --- scripts/startup/bl_ui/space_view3d.py | 1 + .../intern/grease_pencil_edit.cc | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index 9386f59d462..bd3d01bd02f 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -5838,6 +5838,7 @@ class VIEW3D_MT_edit_greasepencil_stroke(Menu): layout.separator() layout.operator("grease_pencil.cyclical_set", text="Toggle Cyclic").type = 'TOGGLE' + layout.operator("grease_pencil.stroke_switch_direction") class VIEW3D_MT_edit_greasepencil_point(Menu): diff --git a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc index fd0d3e01c3a..01b743ad5fc 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -1005,6 +1005,58 @@ static void GREASE_PENCIL_OT_set_active_material(wmOperatorType *ot) } /** \} */ +/* -------------------------------------------------------------------- */ +/** \name Switch Direction Operator + * \{ */ + +static int grease_pencil_stroke_switch_direction_exec(bContext *C, wmOperator *op) +{ + const Scene *scene = CTX_data_scene(C); + Object *object = CTX_data_active_object(C); + GreasePencil &grease_pencil = *static_cast(object->data); + + bool changed = false; + const Array drawings = retrieve_editable_drawings(*scene, grease_pencil); + threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) { + bke::CurvesGeometry &curves = info.drawing.strokes_for_write(); + + IndexMaskMemory memory; + const IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); + + if (selected_curves.is_empty()) { + return; + } + + /* Switch stroke direction. */ + curves.reverse_curves(selected_curves); + + changed = true; + }); + + if (changed) { + DEG_id_tag_update(&grease_pencil.id, ID_RECALC_GEOMETRY); + WM_event_add_notifier(C, NC_GEOM | ND_DATA, &grease_pencil); + } + + return OPERATOR_FINISHED; +} + +static void GREASE_PENCIL_OT_stroke_switch_direction(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Switch Direction"; + ot->idname = "GREASE_PENCIL_OT_stroke_switch_direction"; + ot->description = "Change direction of the points of the selected strokes"; + + /* Callbacks. */ + ot->exec = grease_pencil_stroke_switch_direction_exec; + ot->poll = editable_grease_pencil_poll; + + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/** \} */ + } // namespace blender::ed::greasepencil void ED_operatortypes_grease_pencil_edit() @@ -1017,6 +1069,7 @@ void ED_operatortypes_grease_pencil_edit() WM_operatortype_append(GREASE_PENCIL_OT_stroke_material_set); WM_operatortype_append(GREASE_PENCIL_OT_cyclical_set); WM_operatortype_append(GREASE_PENCIL_OT_set_active_material); + WM_operatortype_append(GREASE_PENCIL_OT_stroke_switch_direction); } void ED_keymap_grease_pencil(wmKeyConfig *keyconf)