GP: New Smooth operator
authorAntonioya <blendergit@gmail.com>
Thu, 13 Dec 2018 18:49:13 +0000 (19:49 +0100)
committerAntonioya <blendergit@gmail.com>
Thu, 13 Dec 2018 18:49:13 +0000 (19:49 +0100)
Smooth a stroke, in edit mode, similar how mesh operator works with meshes.

release/scripts/startup/bl_ui/space_view3d.py
source/blender/editors/gpencil/gpencil_edit.c
source/blender/editors/gpencil/gpencil_intern.h
source/blender/editors/gpencil/gpencil_ops.c

index 4fc247cc8f548b9099b1b5c59411120fea06134f..470af8dc45fede0b559af4b2268638e91c9f6d96 100644 (file)
@@ -3740,6 +3740,7 @@ class VIEW3D_MT_edit_gpencil(Menu):
         layout.separator()
 
         layout.operator("gpencil.duplicate_move", text="Duplicate")
+        layout.operator("gpencil.stroke_smooth", text="Smooth")
         layout.operator("gpencil.stroke_subdivide", text="Subdivide")
         layout.menu("VIEW3D_MT_gpencil_simplify")
 
@@ -5347,6 +5348,7 @@ class VIEW3D_MT_gpencil_edit_specials(Menu):
         layout.menu("VIEW3D_MT_assign_material")
         layout.separator()
 
+        layout.operator("gpencil.stroke_smooth", text="Smooth")
         layout.operator("gpencil.stroke_subdivide", text="Subdivide")
         layout.operator("gpencil.stroke_simplify_fixed", text="Simplify")
         layout.operator("gpencil.stroke_simplify", text="Simplify Adaptative")
index 57fac1d27dc8f80a38a769271bae640816fb7117..ce5ebaf4b8f3b1efaf3fb3af6624db02e715eff6 100644 (file)
@@ -3591,3 +3591,90 @@ void GPENCIL_OT_stroke_split(wmOperatorType *ot)
        /* flags */
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
+
+static int gp_stroke_smooth_exec(bContext *C, wmOperator *op)
+{
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
+       const int repeat = RNA_int_get(op->ptr, "repeat");
+       float factor = RNA_float_get(op->ptr, "factor");
+       const bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
+       const bool smooth_position = RNA_boolean_get(op->ptr, "smooth_position");
+       const bool smooth_thickness = RNA_boolean_get(op->ptr, "smooth_thickness");
+       const bool smooth_strength = RNA_boolean_get(op->ptr, "smooth_strength");
+       const bool smooth_uv = RNA_boolean_get(op->ptr, "smooth_uv");
+
+       /* sanity checks */
+       if (ELEM(NULL, gpd))
+               return OPERATOR_CANCELLED;
+
+       /* Go through each editable + selected stroke */
+       GP_EDITABLE_STROKES_BEGIN(gpstroke_iter, C, gpl, gps)
+       {
+               if (gps->flag & GP_STROKE_SELECT) {
+                       if (factor > 0.0f) {
+                               for (int r = 0; r < repeat; r++) {
+                                       for (int i = 0; i < gps->totpoints; i++) {
+                                               bGPDspoint *pt = &gps->points[i];
+                                               if ((only_selected) && ((pt->flag & GP_SPOINT_SELECT) == 0)) {
+                                                       continue;
+                                               }
+
+                                               /* perform smoothing */
+                                               if (smooth_position) {
+                                                       BKE_gpencil_smooth_stroke(gps, i, factor);
+                                               }
+                                               if (smooth_strength) {
+                                                       BKE_gpencil_smooth_stroke_strength(gps, i, factor);
+                                               }
+                                               if (smooth_thickness) {
+                                                       /* thickness need to repeat process several times */
+                                                       for (int r2 = 0; r2 < r * 10; r2++) {
+                                                               BKE_gpencil_smooth_stroke_thickness(gps, i, factor);
+                                                       }
+                                               }
+                                               if (smooth_uv) {
+                                                       BKE_gpencil_smooth_stroke_uv(gps, i, factor);
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       GP_EDITABLE_STROKES_END(gpstroke_iter);
+
+       /* notifiers */
+       DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+
+       return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_stroke_smooth(wmOperatorType *ot)
+{
+       PropertyRNA *prop;
+
+       /* identifiers */
+       ot->name = "Smooth Stroke";
+       ot->idname = "GPENCIL_OT_stroke_smooth";
+       ot->description = "Smooth selected strokes";
+
+       /* api callbacks */
+       ot->exec = gp_stroke_smooth_exec;
+       ot->poll = gp_active_layer_poll;
+
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+       /* properties */
+       prop = RNA_def_int(ot->srna, "repeat", 1, 1, 10, "Repeat", "", 1, 5);
+       RNA_def_property_flag(prop, PROP_SKIP_SAVE);
+
+       RNA_def_float(ot->srna, "factor", 0.5f, 0.0f, 2.0f, "Factor", "", 0.0f, 2.0f);
+       RNA_def_boolean(ot->srna, "only_selected", true, "Selected Points",
+               "Smooth only selected points in the stroke");
+       RNA_def_boolean(ot->srna, "smooth_position", true, "Position", "");
+       RNA_def_boolean(ot->srna, "smooth_thickness", true, "Thickness", "");
+       RNA_def_boolean(ot->srna, "smooth_strength", false, "Strength", "");
+       RNA_def_boolean(ot->srna, "smooth_uv", false, "UV", "");
+
+}
index f9951c2561ae034e4c1ed7a57e6afad848a8b422..fa806999d25397de232d6974bb1fc2bf44f8d3e0 100644 (file)
@@ -388,6 +388,7 @@ void GPENCIL_OT_stroke_simplify(struct wmOperatorType *ot);
 void GPENCIL_OT_stroke_simplify_fixed(struct wmOperatorType *ot);
 void GPENCIL_OT_stroke_separate(struct wmOperatorType *ot);
 void GPENCIL_OT_stroke_split(struct wmOperatorType *ot);
+void GPENCIL_OT_stroke_smooth(struct wmOperatorType *ot);
 
 void GPENCIL_OT_brush_presets_create(struct wmOperatorType *ot);
 
index 01221f57f05ddeb8af99925be193f7042ab33e70..3eaf72121a8e36b97539b15c2d68a41df0bc6f24 100644 (file)
@@ -308,6 +308,7 @@ void ED_operatortypes_gpencil(void)
        WM_operatortype_append(GPENCIL_OT_stroke_simplify_fixed);
        WM_operatortype_append(GPENCIL_OT_stroke_separate);
        WM_operatortype_append(GPENCIL_OT_stroke_split);
+       WM_operatortype_append(GPENCIL_OT_stroke_smooth);
 
        WM_operatortype_append(GPENCIL_OT_brush_presets_create);