From 0304809a164fe438b30d89ad2ecb6d43273de5e1 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Sat, 21 Oct 2023 16:58:05 +0200 Subject: [PATCH 01/16] GPv3: Normalize Stroke operator This operator has the same functionality that GPv2 version. Note: The radius is set with the entry value. In GPv2 the radius was relative to the stroke thickness. Maybe we need review this. --- scripts/startup/bl_ui/space_view3d.py | 7 +- .../intern/grease_pencil_edit.cc | 129 +++++++++++++++++- 2 files changed, 133 insertions(+), 3 deletions(-) diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index 494e4dc3b36..fd8739703dd 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -5807,9 +5807,9 @@ class VIEW3D_MT_edit_greasepencil(Menu): layout = self.layout layout.menu("VIEW3D_MT_transform") layout.menu("VIEW3D_MT_mirror") - + layout.separator() - + layout.menu("VIEW3D_MT_edit_greasepencil_delete") @@ -5825,6 +5825,9 @@ class VIEW3D_MT_edit_greasepencil_stroke(Menu): layout.operator("grease_pencil.cyclical_set", text="Toggle Cyclic") + layout.separator() + layout.operator("grease_pencil.stroke_normalize", text="Normalize Thickness").mode = 'THICKNESS' + layout.operator("grease_pencil.stroke_normalize", text="Normalize Opacity").mode = 'OPACITY' class VIEW3D_MT_edit_curves(Menu): bl_label = "Curves" 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 7ba2f34d679..dbca7de81ae 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -26,6 +26,9 @@ #include "ED_grease_pencil.hh" #include "ED_screen.hh" +#include "UI_interface.hh" +#include "UI_resources.hh" + #include "WM_api.hh" namespace blender::ed::greasepencil { @@ -238,7 +241,8 @@ void gaussian_blur_1D(const GSpan src, using T = decltype(dummy); /* Reduces unnecessary code generation. */ if constexpr (std::is_same_v || std::is_same_v || - std::is_same_v) { + std::is_same_v) + { gaussian_blur_1D(src.typed(), iterations, influence, @@ -948,6 +952,128 @@ static void GREASE_PENCIL_OT_cyclical_set(wmOperatorType *ot) /** \} */ +/* -------------------------------------------------------------------- */ +/** \name Normalize stroke Operator + * \{ */ + +enum class NormalizeMode : int8_t { + THICKNESS, + OPACITY, +}; + +static const EnumPropertyItem prop_normalize_modes[] = { + {int(NormalizeMode::THICKNESS), + "THICKNESS", + 0, + "Thickness", + "Normalizes the stroke thickness by making all points use the same thickness value"}, + {int(NormalizeMode::OPACITY), + "OPACITY", + 0, + "OPacity", + "Normalizes the stroke opacity by making all points use the same opacity value"}, + {0, nullptr, 0, nullptr, nullptr}, +}; + +static void grease_pencil_stroke_normalize_ui(bContext * /*C*/, wmOperator *op) +{ + uiLayout *layout = op->layout; + uiLayout *row; + + const NormalizeMode mode = NormalizeMode(RNA_enum_get(op->ptr, "mode")); + + uiLayoutSetPropSep(layout, true); + uiLayoutSetPropDecorate(layout, false); + row = uiLayoutRow(layout, true); + uiItemR(row, op->ptr, "mode", UI_ITEM_NONE, nullptr, ICON_NONE); + + if (mode == NormalizeMode::THICKNESS) { + row = uiLayoutRow(layout, true); + uiItemR(row, op->ptr, "radius", UI_ITEM_NONE, nullptr, ICON_NONE); + } + else if (mode == NormalizeMode::OPACITY) { + row = uiLayoutRow(layout, true); + uiItemR(row, op->ptr, "opacity", UI_ITEM_NONE, nullptr, ICON_NONE); + } +} + +static int grease_pencil_stroke_normalize_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); + + const NormalizeMode mode = NormalizeMode(RNA_enum_get(op->ptr, "mode")); + const float radius = RNA_float_get(op->ptr, "radius"); + const float opacity = RNA_float_get(op->ptr, "opacity"); + + bool changed = false; + grease_pencil.foreach_editable_drawing( + scene->r.cfra, [&](int /*layer_index*/, bke::greasepencil::Drawing &drawing) { + bke::CurvesGeometry &curves = drawing.strokes_for_write(); + if (curves.points_num() == 0) { + return; + } + if (!ed::curves::has_anything_selected(curves)) { + return; + } + + MutableSpan radii = drawing.radii_for_write(); + MutableSpan opacities = drawing.opacities_for_write(); + const OffsetIndices points_by_curve = curves.points_by_curve(); + + IndexMaskMemory memory; + IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); + + selected_curves.foreach_index([&](const int curve_index) { + const IndexRange points = points_by_curve[curve_index]; + for (const int point_i : points) { + if (mode == NormalizeMode::THICKNESS) { + radii[point_i] = radius; + } + else if (mode == NormalizeMode::OPACITY) { + opacities[point_i] = opacity; + CLAMP(opacities[point_i], 0.0f, 1.0f); + } + } + }); + + 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_normalize(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Normalize Stroke"; + ot->idname = "GREASE_PENCIL_OT_stroke_normalize"; + ot->description = "Normalize stroke attributes"; + + /* Callbacks. */ + ot->invoke = WM_menu_invoke; + ot->exec = grease_pencil_stroke_normalize_exec; + ot->poll = editable_grease_pencil_poll; + ot->ui = grease_pencil_stroke_normalize_ui; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* props */ + ot->prop = RNA_def_enum( + ot->srna, "mode", prop_normalize_modes, 0, "Mode", "Attribute to be normalized"); + RNA_def_float(ot->srna, "opacity", 1.0f, 0.0f, 1.0f, "Opacity", "", 0.0f, 1.0f); + RNA_def_float(ot->srna, "radius", 0.05f, 0.0f, 1000.0f, "Radius", "Radius", 0.0f, 1000.0f); +} + +/** \} */ + } // namespace blender::ed::greasepencil void ED_operatortypes_grease_pencil_edit() @@ -959,6 +1085,7 @@ void ED_operatortypes_grease_pencil_edit() WM_operatortype_append(GREASE_PENCIL_OT_delete_frame); WM_operatortype_append(GREASE_PENCIL_OT_stroke_change_color); WM_operatortype_append(GREASE_PENCIL_OT_cyclical_set); + WM_operatortype_append(GREASE_PENCIL_OT_stroke_normalize); } void ED_keymap_grease_pencil(wmKeyConfig *keyconf) -- 2.30.2 From 24bb0a23d1c7cf8c0aa02fb4844f2a2bc33efffd Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Sat, 21 Oct 2023 23:44:44 +0200 Subject: [PATCH 02/16] GPv3: Cleanup code and remove duplicate checks --- .../grease_pencil/intern/grease_pencil_edit.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 dbca7de81ae..930a9ff6547 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -1011,10 +1011,11 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) grease_pencil.foreach_editable_drawing( scene->r.cfra, [&](int /*layer_index*/, bke::greasepencil::Drawing &drawing) { bke::CurvesGeometry &curves = drawing.strokes_for_write(); - if (curves.points_num() == 0) { - return; - } - if (!ed::curves::has_anything_selected(curves)) { + + IndexMaskMemory memory; + const IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); + + if (selected_curves.is_empty()) { return; } @@ -1022,9 +1023,6 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) MutableSpan opacities = drawing.opacities_for_write(); const OffsetIndices points_by_curve = curves.points_by_curve(); - IndexMaskMemory memory; - IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); - selected_curves.foreach_index([&](const int curve_index) { const IndexRange points = points_by_curve[curve_index]; for (const int point_i : points) { -- 2.30.2 From d8dc8599a2eaaeb2ce5345fa55fc0782af0c34cd Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Sat, 21 Oct 2023 23:46:13 +0200 Subject: [PATCH 03/16] GPv3: Fixes after first review --- .../editors/grease_pencil/intern/grease_pencil_edit.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 930a9ff6547..398c29cd234 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -970,7 +970,7 @@ static const EnumPropertyItem prop_normalize_modes[] = { {int(NormalizeMode::OPACITY), "OPACITY", 0, - "OPacity", + "Opacity", "Normalizes the stroke opacity by making all points use the same opacity value"}, {0, nullptr, 0, nullptr, nullptr}, }; @@ -1031,7 +1031,7 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) } else if (mode == NormalizeMode::OPACITY) { opacities[point_i] = opacity; - CLAMP(opacities[point_i], 0.0f, 1.0f); + math::clamp(opacities[point_i], 0.0f, 1.0f); } } }); -- 2.30.2 From 3572647f57753829f30d55e65d8c78a61ca7ac19 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Sun, 22 Oct 2023 10:23:15 +0200 Subject: [PATCH 04/16] GPv3: Remove loop and replace with fill Following Pratik Borhade advice, the loop has been replaced with fill function. --- .../intern/grease_pencil_edit.cc | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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 398c29cd234..55ca831fb5f 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -1019,20 +1019,17 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) return; } - MutableSpan radii = drawing.radii_for_write(); - MutableSpan opacities = drawing.opacities_for_write(); const OffsetIndices points_by_curve = curves.points_by_curve(); - selected_curves.foreach_index([&](const int curve_index) { - const IndexRange points = points_by_curve[curve_index]; - for (const int point_i : points) { - if (mode == NormalizeMode::THICKNESS) { - radii[point_i] = radius; - } - else if (mode == NormalizeMode::OPACITY) { - opacities[point_i] = opacity; - math::clamp(opacities[point_i], 0.0f, 1.0f); - } + if (mode == NormalizeMode::THICKNESS) { + MutableSpan radii = drawing.radii_for_write().slice( + points_by_curve[curve_index]); + radii.fill(radius); + } + if (mode == NormalizeMode::OPACITY) { + MutableSpan opacities = drawing.opacities_for_write().slice( + points_by_curve[curve_index]); + opacities.fill(opacity); } }); -- 2.30.2 From a9ead9638069f6daec4b135d83e61bcfd5fc5c3d Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 31 Oct 2023 17:27:09 +0100 Subject: [PATCH 05/16] Cleanup: Make format --- .../blender/editors/grease_pencil/intern/grease_pencil_edit.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 55ca831fb5f..f8e361a5f36 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -241,8 +241,7 @@ void gaussian_blur_1D(const GSpan src, using T = decltype(dummy); /* Reduces unnecessary code generation. */ if constexpr (std::is_same_v || std::is_same_v || - std::is_same_v) - { + std::is_same_v) { gaussian_blur_1D(src.typed(), iterations, influence, -- 2.30.2 From 73e806b14beb3735ef43f2c1aa82c9a895db1f22 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 31 Oct 2023 17:58:16 +0100 Subject: [PATCH 06/16] Cleanup: Revert format --- scripts/startup/bl_ui/space_view3d.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index fd8739703dd..cbb658c64c0 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -5807,9 +5807,9 @@ class VIEW3D_MT_edit_greasepencil(Menu): layout = self.layout layout.menu("VIEW3D_MT_transform") layout.menu("VIEW3D_MT_mirror") - + layout.separator() - + layout.menu("VIEW3D_MT_edit_greasepencil_delete") -- 2.30.2 From 443589b9994ef82ff7b7de9b129cedc19e3560c2 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 31 Oct 2023 18:00:08 +0100 Subject: [PATCH 07/16] GPv3: Use curve bke functions and avoid span read in loop --- .../intern/grease_pencil_edit.cc | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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 f8e361a5f36..939e5b99749 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -15,6 +15,7 @@ #include "BLI_stack.hh" #include "BKE_context.h" +#include "BKE_curves_utils.hh" #include "BKE_grease_pencil.hh" #include "RNA_access.hh" @@ -1019,19 +1020,15 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) } const OffsetIndices points_by_curve = curves.points_by_curve(); - selected_curves.foreach_index([&](const int curve_index) { - if (mode == NormalizeMode::THICKNESS) { - MutableSpan radii = drawing.radii_for_write().slice( - points_by_curve[curve_index]); - radii.fill(radius); - } - if (mode == NormalizeMode::OPACITY) { - MutableSpan opacities = drawing.opacities_for_write().slice( - points_by_curve[curve_index]); - opacities.fill(opacity); - } - }); + MutableSpan radii = drawing.radii_for_write(); + MutableSpan opacities = drawing.opacities_for_write(); + if (mode == NormalizeMode::THICKNESS) { + bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); + } + if (mode == NormalizeMode::OPACITY) { + bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); + } changed = true; }); -- 2.30.2 From a09b9baedf04666e46502fdf77913e55331b088b Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 31 Oct 2023 18:53:32 +0100 Subject: [PATCH 08/16] GPv3: Use switch instead of if --- .../grease_pencil/intern/grease_pencil_edit.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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 939e5b99749..0c6d5086d36 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -1023,13 +1023,18 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) MutableSpan radii = drawing.radii_for_write(); MutableSpan opacities = drawing.opacities_for_write(); - if (mode == NormalizeMode::THICKNESS) { - bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); + switch (mode) { + case NormalizeMode::THICKNESS: + bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); + changed = true; + break; + case NormalizeMode::OPACITY: + bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); + changed = true; + break; + default: + break; } - if (mode == NormalizeMode::OPACITY) { - bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); - } - changed = true; }); if (changed) { -- 2.30.2 From 78a4427ce6191916af2709f0523ae7d93e2a2467 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 31 Oct 2023 18:59:32 +0100 Subject: [PATCH 09/16] GPv3: Remove default case Now this is not required --- .../blender/editors/grease_pencil/intern/grease_pencil_edit.cc | 2 -- 1 file changed, 2 deletions(-) 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 0c6d5086d36..b69f65b7a18 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -1032,8 +1032,6 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); changed = true; break; - default: - break; } }); -- 2.30.2 From 6d749d8dee20d6694c0b9a8aafd4f70c9de97471 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Wed, 1 Nov 2023 10:45:26 +0100 Subject: [PATCH 10/16] GPv3: More cleanup to make it faster * Moved the enum inside operator definition. * Mutable span is only get if needed.. --- .../intern/grease_pencil_edit.cc | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) 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 b69f65b7a18..0786039867c 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -961,20 +961,6 @@ enum class NormalizeMode : int8_t { OPACITY, }; -static const EnumPropertyItem prop_normalize_modes[] = { - {int(NormalizeMode::THICKNESS), - "THICKNESS", - 0, - "Thickness", - "Normalizes the stroke thickness by making all points use the same thickness value"}, - {int(NormalizeMode::OPACITY), - "OPACITY", - 0, - "Opacity", - "Normalizes the stroke opacity by making all points use the same opacity value"}, - {0, nullptr, 0, nullptr, nullptr}, -}; - static void grease_pencil_stroke_normalize_ui(bContext * /*C*/, wmOperator *op) { uiLayout *layout = op->layout; @@ -1020,18 +1006,20 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) } const OffsetIndices points_by_curve = curves.points_by_curve(); - MutableSpan radii = drawing.radii_for_write(); - MutableSpan opacities = drawing.opacities_for_write(); - switch (mode) { - case NormalizeMode::THICKNESS: + case NormalizeMode::THICKNESS: { + + MutableSpan radii = drawing.radii_for_write(); bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); changed = true; break; - case NormalizeMode::OPACITY: + } + case NormalizeMode::OPACITY: { + MutableSpan opacities = drawing.opacities_for_write(); bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); changed = true; break; + } } }); @@ -1045,6 +1033,20 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) static void GREASE_PENCIL_OT_stroke_normalize(wmOperatorType *ot) { + static const EnumPropertyItem prop_normalize_modes[] = { + {int(NormalizeMode::THICKNESS), + "THICKNESS", + 0, + "Thickness", + "Normalizes the stroke thickness by making all points use the same thickness value"}, + {int(NormalizeMode::OPACITY), + "OPACITY", + 0, + "Opacity", + "Normalizes the stroke opacity by making all points use the same opacity value"}, + {0, nullptr, 0, nullptr, nullptr}, + }; + /* Identifiers. */ ot->name = "Normalize Stroke"; ot->idname = "GREASE_PENCIL_OT_stroke_normalize"; -- 2.30.2 From 18f93dddcce5b4e2b406bc02e2a202a06c1cf23f Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Wed, 1 Nov 2023 10:46:58 +0100 Subject: [PATCH 11/16] Cleanup: Remove extra blank line --- .../blender/editors/grease_pencil/intern/grease_pencil_edit.cc | 1 - 1 file changed, 1 deletion(-) 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 0786039867c..0e2c051036e 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -1008,7 +1008,6 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) const OffsetIndices points_by_curve = curves.points_by_curve(); switch (mode) { case NormalizeMode::THICKNESS: { - MutableSpan radii = drawing.radii_for_write(); bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); changed = true; -- 2.30.2 From 7f9854dd4187eeb55e3a464d5cd4b8ad7844129b Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Wed, 1 Nov 2023 12:10:29 +0100 Subject: [PATCH 12/16] GPv3: Split Normalize op in two operators * GREASE_PENCIL_OT_set_opacity * GREASE_PENCIL_OT_set_thickness --- scripts/startup/bl_ui/space_view3d.py | 5 +- .../intern/grease_pencil_edit.cc | 140 +++++++++--------- 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index cbb658c64c0..fd2a36002eb 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -5826,8 +5826,9 @@ class VIEW3D_MT_edit_greasepencil_stroke(Menu): layout.operator("grease_pencil.cyclical_set", text="Toggle Cyclic") layout.separator() - layout.operator("grease_pencil.stroke_normalize", text="Normalize Thickness").mode = 'THICKNESS' - layout.operator("grease_pencil.stroke_normalize", text="Normalize Opacity").mode = 'OPACITY' + layout.operator("grease_pencil.set_thickness") + layout.operator("grease_pencil.set_opacity") + class VIEW3D_MT_edit_curves(Menu): bl_label = "Curves" 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 0e2c051036e..64f567f759b 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -953,44 +953,73 @@ static void GREASE_PENCIL_OT_cyclical_set(wmOperatorType *ot) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Normalize stroke Operator +/** \name Set stroke Thickness * \{ */ -enum class NormalizeMode : int8_t { - THICKNESS, - OPACITY, -}; - -static void grease_pencil_stroke_normalize_ui(bContext * /*C*/, wmOperator *op) -{ - uiLayout *layout = op->layout; - uiLayout *row; - - const NormalizeMode mode = NormalizeMode(RNA_enum_get(op->ptr, "mode")); - - uiLayoutSetPropSep(layout, true); - uiLayoutSetPropDecorate(layout, false); - row = uiLayoutRow(layout, true); - uiItemR(row, op->ptr, "mode", UI_ITEM_NONE, nullptr, ICON_NONE); - - if (mode == NormalizeMode::THICKNESS) { - row = uiLayoutRow(layout, true); - uiItemR(row, op->ptr, "radius", UI_ITEM_NONE, nullptr, ICON_NONE); - } - else if (mode == NormalizeMode::OPACITY) { - row = uiLayoutRow(layout, true); - uiItemR(row, op->ptr, "opacity", UI_ITEM_NONE, nullptr, ICON_NONE); - } -} - -static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) +static int grease_pencil_set_thickness_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); - const NormalizeMode mode = NormalizeMode(RNA_enum_get(op->ptr, "mode")); const float radius = RNA_float_get(op->ptr, "radius"); + + bool changed = false; + grease_pencil.foreach_editable_drawing( + scene->r.cfra, [&](int /*layer_index*/, bke::greasepencil::Drawing &drawing) { + bke::CurvesGeometry &curves = drawing.strokes_for_write(); + + IndexMaskMemory memory; + const IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); + + if (selected_curves.is_empty()) { + return; + } + + const OffsetIndices points_by_curve = curves.points_by_curve(); + MutableSpan radii = drawing.radii_for_write(); + bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); + 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_set_thickness(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Set Thickness"; + ot->idname = "GREASE_PENCIL_OT_set_thickness"; + ot->description = "Set all stroke points to same thickness"; + + /* Callbacks. */ + ot->exec = grease_pencil_set_thickness_exec; + ot->poll = editable_grease_pencil_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* props */ + ot->prop = RNA_def_float( + ot->srna, "radius", 0.05f, 0.0f, 1000.0f, "Radius", "Radius", 0.0f, 1000.0f); +} + +/** \} */ +/* -------------------------------------------------------------------- */ +/** \name Set stroke Opacity + * \{ */ + +static int grease_pencil_set_opacity_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); + const float opacity = RNA_float_get(op->ptr, "opacity"); bool changed = false; @@ -1006,20 +1035,9 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) } const OffsetIndices points_by_curve = curves.points_by_curve(); - switch (mode) { - case NormalizeMode::THICKNESS: { - MutableSpan radii = drawing.radii_for_write(); - bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); - changed = true; - break; - } - case NormalizeMode::OPACITY: { - MutableSpan opacities = drawing.opacities_for_write(); - bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); - changed = true; - break; - } - } + MutableSpan opacities = drawing.opacities_for_write(); + bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); + changed = true; }); if (changed) { @@ -1030,41 +1048,22 @@ static int grease_pencil_stroke_normalize_exec(bContext *C, wmOperator *op) return OPERATOR_FINISHED; } -static void GREASE_PENCIL_OT_stroke_normalize(wmOperatorType *ot) +static void GREASE_PENCIL_OT_set_opacity(wmOperatorType *ot) { - static const EnumPropertyItem prop_normalize_modes[] = { - {int(NormalizeMode::THICKNESS), - "THICKNESS", - 0, - "Thickness", - "Normalizes the stroke thickness by making all points use the same thickness value"}, - {int(NormalizeMode::OPACITY), - "OPACITY", - 0, - "Opacity", - "Normalizes the stroke opacity by making all points use the same opacity value"}, - {0, nullptr, 0, nullptr, nullptr}, - }; - /* Identifiers. */ - ot->name = "Normalize Stroke"; - ot->idname = "GREASE_PENCIL_OT_stroke_normalize"; - ot->description = "Normalize stroke attributes"; + ot->name = "Set Opacity"; + ot->idname = "GREASE_PENCIL_OT_set_opacity"; + ot->description = "Set all stroke points to same opacity"; /* Callbacks. */ - ot->invoke = WM_menu_invoke; - ot->exec = grease_pencil_stroke_normalize_exec; + ot->exec = grease_pencil_set_opacity_exec; ot->poll = editable_grease_pencil_poll; - ot->ui = grease_pencil_stroke_normalize_ui; /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* props */ - ot->prop = RNA_def_enum( - ot->srna, "mode", prop_normalize_modes, 0, "Mode", "Attribute to be normalized"); - RNA_def_float(ot->srna, "opacity", 1.0f, 0.0f, 1.0f, "Opacity", "", 0.0f, 1.0f); - RNA_def_float(ot->srna, "radius", 0.05f, 0.0f, 1000.0f, "Radius", "Radius", 0.0f, 1000.0f); + ot->prop = RNA_def_float(ot->srna, "opacity", 1.0f, 0.0f, 1.0f, "Opacity", "", 0.0f, 1.0f); } /** \} */ @@ -1080,7 +1079,8 @@ void ED_operatortypes_grease_pencil_edit() WM_operatortype_append(GREASE_PENCIL_OT_delete_frame); WM_operatortype_append(GREASE_PENCIL_OT_stroke_change_color); WM_operatortype_append(GREASE_PENCIL_OT_cyclical_set); - WM_operatortype_append(GREASE_PENCIL_OT_stroke_normalize); + WM_operatortype_append(GREASE_PENCIL_OT_set_thickness); + WM_operatortype_append(GREASE_PENCIL_OT_set_opacity); } void ED_keymap_grease_pencil(wmKeyConfig *keyconf) -- 2.30.2 From 56f7a111f729f846324b01fe4f6703711baef46e Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Wed, 1 Nov 2023 16:15:26 +0100 Subject: [PATCH 13/16] GPv3: Add `_uniform_` to the operator name --- scripts/startup/bl_ui/space_view3d.py | 4 +-- .../intern/grease_pencil_edit.cc | 28 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index fd2a36002eb..2b611bdcda8 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -5826,8 +5826,8 @@ class VIEW3D_MT_edit_greasepencil_stroke(Menu): layout.operator("grease_pencil.cyclical_set", text="Toggle Cyclic") layout.separator() - layout.operator("grease_pencil.set_thickness") - layout.operator("grease_pencil.set_opacity") + layout.operator("grease_pencil.set_uniform_thickness") + layout.operator("grease_pencil.set_uniform_opacity") class VIEW3D_MT_edit_curves(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 64f567f759b..eec704995b9 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -953,10 +953,10 @@ static void GREASE_PENCIL_OT_cyclical_set(wmOperatorType *ot) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Set stroke Thickness +/** \name Set stroke uniform Thickness * \{ */ -static int grease_pencil_set_thickness_exec(bContext *C, wmOperator *op) +static int grease_pencil_set_uniform_thickness_exec(bContext *C, wmOperator *op) { const Scene *scene = CTX_data_scene(C); Object *object = CTX_data_active_object(C); @@ -990,15 +990,15 @@ static int grease_pencil_set_thickness_exec(bContext *C, wmOperator *op) return OPERATOR_FINISHED; } -static void GREASE_PENCIL_OT_set_thickness(wmOperatorType *ot) +static void GREASE_PENCIL_OT_set_uniform_thickness(wmOperatorType *ot) { /* Identifiers. */ - ot->name = "Set Thickness"; - ot->idname = "GREASE_PENCIL_OT_set_thickness"; + ot->name = "Set Uniform Thickness"; + ot->idname = "GREASE_PENCIL_OT_set_uniform_thickness"; ot->description = "Set all stroke points to same thickness"; /* Callbacks. */ - ot->exec = grease_pencil_set_thickness_exec; + ot->exec = grease_pencil_set_uniform_thickness_exec; ot->poll = editable_grease_pencil_poll; /* flags */ @@ -1011,10 +1011,10 @@ static void GREASE_PENCIL_OT_set_thickness(wmOperatorType *ot) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Set stroke Opacity +/** \name Set stroke uniform Opacity * \{ */ -static int grease_pencil_set_opacity_exec(bContext *C, wmOperator *op) +static int grease_pencil_set_uniform_opacity_exec(bContext *C, wmOperator *op) { const Scene *scene = CTX_data_scene(C); Object *object = CTX_data_active_object(C); @@ -1048,15 +1048,15 @@ static int grease_pencil_set_opacity_exec(bContext *C, wmOperator *op) return OPERATOR_FINISHED; } -static void GREASE_PENCIL_OT_set_opacity(wmOperatorType *ot) +static void GREASE_PENCIL_OT_set_uniform_opacity(wmOperatorType *ot) { /* Identifiers. */ - ot->name = "Set Opacity"; - ot->idname = "GREASE_PENCIL_OT_set_opacity"; + ot->name = "Set Uniform Opacity"; + ot->idname = "GREASE_PENCIL_OT_set_uniform_opacity"; ot->description = "Set all stroke points to same opacity"; /* Callbacks. */ - ot->exec = grease_pencil_set_opacity_exec; + ot->exec = grease_pencil_set_uniform_opacity_exec; ot->poll = editable_grease_pencil_poll; /* flags */ @@ -1079,8 +1079,8 @@ void ED_operatortypes_grease_pencil_edit() WM_operatortype_append(GREASE_PENCIL_OT_delete_frame); WM_operatortype_append(GREASE_PENCIL_OT_stroke_change_color); WM_operatortype_append(GREASE_PENCIL_OT_cyclical_set); - WM_operatortype_append(GREASE_PENCIL_OT_set_thickness); - WM_operatortype_append(GREASE_PENCIL_OT_set_opacity); + WM_operatortype_append(GREASE_PENCIL_OT_set_uniform_thickness); + WM_operatortype_append(GREASE_PENCIL_OT_set_uniform_opacity); } void ED_keymap_grease_pencil(wmKeyConfig *keyconf) -- 2.30.2 From b4a2847f23af9f761d4d3d651ec65200bbcc0cf3 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Thu, 2 Nov 2023 17:42:35 +0100 Subject: [PATCH 14/16] GPv3: Update to new multiframe API --- .../intern/grease_pencil_edit.cc | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) 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 21f1cfa6879..413b954e9e9 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -974,22 +974,22 @@ static int grease_pencil_set_uniform_thickness_exec(bContext *C, wmOperator *op) const float radius = RNA_float_get(op->ptr, "radius"); bool changed = false; - grease_pencil.foreach_editable_drawing( - scene->r.cfra, [&](int /*layer_index*/, bke::greasepencil::Drawing &drawing) { - bke::CurvesGeometry &curves = drawing.strokes_for_write(); + 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); + IndexMaskMemory memory; + const IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); - if (selected_curves.is_empty()) { - return; - } + if (selected_curves.is_empty()) { + return; + } - const OffsetIndices points_by_curve = curves.points_by_curve(); - MutableSpan radii = drawing.radii_for_write(); - bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); - changed = true; - }); + const OffsetIndices points_by_curve = curves.points_by_curve(); + MutableSpan radii = info.drawing.radii_for_write(); + bke::curves::fill_points(points_by_curve, selected_curves, radius, radii); + changed = true; + }); if (changed) { DEG_id_tag_update(&grease_pencil.id, ID_RECALC_GEOMETRY); @@ -1032,22 +1032,22 @@ static int grease_pencil_set_uniform_opacity_exec(bContext *C, wmOperator *op) const float opacity = RNA_float_get(op->ptr, "opacity"); bool changed = false; - grease_pencil.foreach_editable_drawing( - scene->r.cfra, [&](int /*layer_index*/, bke::greasepencil::Drawing &drawing) { - bke::CurvesGeometry &curves = drawing.strokes_for_write(); + 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); + IndexMaskMemory memory; + const IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); - if (selected_curves.is_empty()) { - return; - } + if (selected_curves.is_empty()) { + return; + } - const OffsetIndices points_by_curve = curves.points_by_curve(); - MutableSpan opacities = drawing.opacities_for_write(); - bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); - changed = true; - }); + const OffsetIndices points_by_curve = curves.points_by_curve(); + MutableSpan opacities = info.drawing.opacities_for_write(); + bke::curves::fill_points(points_by_curve, selected_curves, opacity, opacities); + changed = true; + }); if (changed) { DEG_id_tag_update(&grease_pencil.id, ID_RECALC_GEOMETRY); -- 2.30.2 From 4336411c84a8f06929bb335748e587e89af62612 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 7 Nov 2023 15:57:25 +0100 Subject: [PATCH 15/16] GPv3: Rename Radius to Thickness Also divide Thickness by 2 because radius is half thickness. --- .../editors/grease_pencil/intern/grease_pencil_edit.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 413b954e9e9..bcdf16aa7dd 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -971,7 +971,8 @@ static int grease_pencil_set_uniform_thickness_exec(bContext *C, wmOperator *op) Object *object = CTX_data_active_object(C); GreasePencil &grease_pencil = *static_cast(object->data); - const float radius = RNA_float_get(op->ptr, "radius"); + /* Radius is half of the thickness. */ + const float radius = RNA_float_get(op->ptr, "thickness") * 0.5f; bool changed = false; const Array drawings = retrieve_editable_drawings(*scene, grease_pencil); @@ -1015,7 +1016,7 @@ static void GREASE_PENCIL_OT_set_uniform_thickness(wmOperatorType *ot) /* props */ ot->prop = RNA_def_float( - ot->srna, "radius", 0.05f, 0.0f, 1000.0f, "Radius", "Radius", 0.0f, 1000.0f); + ot->srna, "thickness", 0.1f, 0.0f, 1000.0f, "Thickness", "Thickness", 0.0f, 1000.0f); } /** \} */ -- 2.30.2 From ad6962b2c9d337c56a429a067506468fc1fb5ce4 Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Wed, 8 Nov 2023 12:00:33 +0100 Subject: [PATCH 16/16] Cleanup: Remove unused include --- .../blender/editors/grease_pencil/intern/grease_pencil_edit.cc | 3 --- 1 file changed, 3 deletions(-) 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 bcdf16aa7dd..f06539c2918 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -27,9 +27,6 @@ #include "ED_grease_pencil.hh" #include "ED_screen.hh" -#include "UI_interface.hh" -#include "UI_resources.hh" - #include "WM_api.hh" namespace blender::ed::greasepencil { -- 2.30.2