[Bf-blender-cvs] [747648b195d] temp-gpencil-bezier-v2: GPencil: Add stroke/curve conversion operators

Falk David noreply at git.blender.org
Thu Jan 21 13:26:38 CET 2021


Commit: 747648b195d7d9add77d56b44ab4b8bcbc958a88
Author: Falk David
Date:   Thu Jan 21 12:41:18 2021 +0100
Branches: temp-gpencil-bezier-v2
https://developer.blender.org/rB747648b195d7d9add77d56b44ab4b8bcbc958a88

GPencil: Add stroke/curve conversion operators

===================================================================

M	source/blender/blenkernel/BKE_gpencil_curve.h
M	source/blender/blenkernel/intern/gpencil_curve.c
M	source/blender/editors/gpencil/gpencil_edit_curve.c
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c
M	source/blender/makesdna/DNA_gpencil_types.h

===================================================================

diff --git a/source/blender/blenkernel/BKE_gpencil_curve.h b/source/blender/blenkernel/BKE_gpencil_curve.h
index 2d42bb36949..c861326c345 100644
--- a/source/blender/blenkernel/BKE_gpencil_curve.h
+++ b/source/blender/blenkernel/BKE_gpencil_curve.h
@@ -45,11 +45,10 @@ void BKE_gpencil_convert_curve(struct Main *bmain,
 
 struct bGPDcurve *BKE_gpencil_stroke_editcurve_generate(struct bGPDstroke *gps,
                                                         const float error_threshold,
-                                                        const float corner_angle,
-                                                        const float stroke_radius);
-void BKE_gpencil_stroke_editcurve_update(struct bGPdata *gpd,
-                                         struct bGPDlayer *gpl,
-                                         struct bGPDstroke *gps);
+                                                        const float corner_angle);
+void BKE_gpencil_stroke_editcurve_update(struct bGPDstroke *gps,
+                                         const float threshold,
+                                         const float corner_angle);
 void BKE_gpencil_editcurve_stroke_sync_selection(struct bGPDstroke *gps, struct bGPDcurve *gpc);
 void BKE_gpencil_stroke_editcurve_sync_selection(struct bGPDstroke *gps, struct bGPDcurve *gpc);
 void BKE_gpencil_strokes_selected_update_editcurve(struct bGPdata *gpd);
diff --git a/source/blender/blenkernel/intern/gpencil_curve.c b/source/blender/blenkernel/intern/gpencil_curve.c
index be14d74de7a..76ca6301b61 100644
--- a/source/blender/blenkernel/intern/gpencil_curve.c
+++ b/source/blender/blenkernel/intern/gpencil_curve.c
@@ -566,8 +566,7 @@ void BKE_gpencil_convert_curve(Main *bmain,
 /** \name Edit-Curve Kernel Functions
  * \{ */
 
-static bGPDcurve *gpencil_stroke_editcurve_generate_edgecases(bGPDstroke *gps,
-                                                              const float stroke_radius)
+static bGPDcurve *gpencil_stroke_editcurve_generate_edgecases(bGPDstroke *gps)
 {
   BLI_assert(gps->totpoints < 3);
 
@@ -577,6 +576,8 @@ static bGPDcurve *gpencil_stroke_editcurve_generate_edgecases(bGPDstroke *gps,
     bGPDcurve_point *cpt = &editcurve->curve_points[0];
     BezTriple *bezt = &cpt->bezt;
 
+    /* This is not the actual radius, but it is good enough for what we need. */
+    float stroke_radius = gps->thickness / 1000.0f;
     /* Handles are twice as long as the radius of the point. */
     float offset = (pt->pressure * stroke_radius) * 2.0f;
 
@@ -644,11 +645,10 @@ static bGPDcurve *gpencil_stroke_editcurve_generate_edgecases(bGPDstroke *gps,
  */
 bGPDcurve *BKE_gpencil_stroke_editcurve_generate(bGPDstroke *gps,
                                                  const float error_threshold,
-                                                 const float corner_angle,
-                                                 const float stroke_radius)
+                                                 const float corner_angle)
 {
   if (gps->totpoints < 3) {
-    return gpencil_stroke_editcurve_generate_edgecases(gps, stroke_radius);
+    return gpencil_stroke_editcurve_generate_edgecases(gps);
   }
 #define POINT_DIM 9
 
@@ -756,7 +756,7 @@ bGPDcurve *BKE_gpencil_stroke_editcurve_generate(bGPDstroke *gps,
 /**
  * Updates the editcurve for a stroke. Frees the old curve if one exists and generates a new one.
  */
-void BKE_gpencil_stroke_editcurve_update(bGPdata *gpd, bGPDlayer *gpl, bGPDstroke *gps)
+void BKE_gpencil_stroke_editcurve_update(bGPDstroke *gps, const float threshold, const float corner_angle)
 {
   if (gps == NULL || gps->totpoints < 0) {
     return;
@@ -766,11 +766,7 @@ void BKE_gpencil_stroke_editcurve_update(bGPdata *gpd, bGPDlayer *gpl, bGPDstrok
     BKE_gpencil_free_stroke_editcurve(gps);
   }
 
-  float defaultpixsize = 1000.0f / gpd->pixfactor;
-  float stroke_radius = ((gps->thickness + gpl->line_change) / defaultpixsize) / 2.0f;
-
-  bGPDcurve *editcurve = BKE_gpencil_stroke_editcurve_generate(
-      gps, gpd->curve_edit_threshold, gpd->curve_edit_corner_angle, stroke_radius);
+  bGPDcurve *editcurve = BKE_gpencil_stroke_editcurve_generate(gps, threshold, corner_angle);
   if (editcurve == NULL) {
     return;
   }
@@ -1365,14 +1361,14 @@ void BKE_gpencil_strokes_selected_update_editcurve(bGPdata *gpd)
 
           /* Generate the curve if there is none or the stroke was changed */
           if (gps->editcurve == NULL) {
-            BKE_gpencil_stroke_editcurve_update(gpd, gpl, gps);
+            // BKE_gpencil_stroke_editcurve_update(gpd, gpl, gps);
             /* Continue if curve could not be generated. */
             if (gps->editcurve == NULL) {
               continue;
             }
           }
           else if (gps->editcurve->flag & GP_CURVE_NEEDS_STROKE_UPDATE) {
-            BKE_gpencil_stroke_editcurve_update(gpd, gpl, gps);
+            // BKE_gpencil_stroke_editcurve_update(gpd, gpl, gps);
           }
           /* Update the selection from the stroke to the curve. */
           BKE_gpencil_editcurve_stroke_sync_selection(gps, gps->editcurve);
diff --git a/source/blender/editors/gpencil/gpencil_edit_curve.c b/source/blender/editors/gpencil/gpencil_edit_curve.c
index 60d1d2169b4..570a03b3808 100644
--- a/source/blender/editors/gpencil/gpencil_edit_curve.c
+++ b/source/blender/editors/gpencil/gpencil_edit_curve.c
@@ -54,89 +54,9 @@
 
 #include "gpencil_intern.h"
 
-/* Poll callback for checking if there is an active layer and we are in curve edit mode. */
-static bool gpencil_curve_edit_mode_poll(bContext *C)
-{
-  Object *ob = CTX_data_active_object(C);
-  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
-    return false;
-  }
-  bGPdata *gpd = (bGPdata *)ob->data;
-  if (!GPENCIL_CURVE_EDIT_SESSIONS_ON(gpd)) {
-    return false;
-  }
-
-  bGPDlayer *gpl = BKE_gpencil_layer_active_get(gpd);
-  return (gpl != NULL);
-}
-
-static int gpencil_stroke_enter_editcurve_mode_exec(bContext *C, wmOperator *op)
-{
-  Object *ob = CTX_data_active_object(C);
-  bGPdata *gpd = ob->data;
-
-  float error_threshold = RNA_float_get(op->ptr, "error_threshold");
-  gpd->curve_edit_threshold = error_threshold;
-
-  if (ELEM(NULL, gpd)) {
-    return OPERATOR_CANCELLED;
-  }
-
-  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
-    LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
-      if (gpf == gpl->actframe) {
-        LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
-          /* only allow selected and non-converted strokes to be transformed */
-          if ((gps->flag & GP_STROKE_SELECT && gps->editcurve == NULL) ||
-              (gps->editcurve != NULL && gps->editcurve->flag & GP_CURVE_NEEDS_STROKE_UPDATE)) {
-            BKE_gpencil_stroke_editcurve_update(gpd, gpl, gps);
-            /* Update the selection from the stroke to the curve. */
-            BKE_gpencil_editcurve_stroke_sync_selection(gps, gps->editcurve);
-            gps->flag |= GP_STROKE_NEEDS_CURVE_UPDATE;
-            BKE_gpencil_stroke_geometry_update(gpd, gps);
-          }
-        }
-      }
-    }
-  }
-
-  gpd->flag |= GP_DATA_CURVE_EDIT_MODE;
-
-  /* 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_enter_editcurve_mode(wmOperatorType *ot)
-{
-  PropertyRNA *prop;
-
-  /* identifiers */
-  ot->name = "Enter curve edit mode";
-  ot->idname = "GPENCIL_OT_stroke_enter_editcurve_mode";
-  ot->description = "Called to transform a stroke into a curve";
-
-  /* api callbacks */
-  ot->exec = gpencil_stroke_enter_editcurve_mode_exec;
-  ot->poll = gpencil_active_layer_poll;
-
-  /* flags */
-  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
-
-  /* properties */
-  prop = RNA_def_float(ot->srna,
-                       "error_threshold",
-                       0.1f,
-                       FLT_MIN,
-                       100.0f,
-                       "Error Threshold",
-                       "Threshold on the maximum deviation from the actual stroke",
-                       FLT_MIN,
-                       10.f);
-  RNA_def_property_ui_range(prop, FLT_MIN, 10.0f, 0.1f, 5);
-}
+/* -------------------------------------------------------------------- */
+/** \name Set handle type operator
+ * \{ */
 
 static int gpencil_editcurve_set_handle_type_exec(bContext *C, wmOperator *op)
 {
@@ -202,7 +122,7 @@ void GPENCIL_OT_stroke_editcurve_set_handle_type(wmOperatorType *ot)
   /* api callbacks */
   ot->invoke = WM_menu_invoke;
   ot->exec = gpencil_editcurve_set_handle_type_exec;
-  ot->poll = gpencil_curve_edit_mode_poll;
+  ot->poll = gpencil_active_layer_poll;
 
   /* flags */
   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
@@ -210,5 +130,147 @@ void GPENCIL_OT_stroke_editcurve_set_handle_type(wmOperatorType *ot)
   /* properties */
   ot->prop = RNA_def_enum(ot->srna, "type", editcurve_handle_type_items, 1, "Type", "Spline type");
 }
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Make curve from stroke operator
+ * \{ */
+
+static int gpencil_stroke_make_curve_exec(bContext *C, wmOperator *op)
+{
+  Object *ob = CTX_data_active_object(C);
+  bGPdata *gpd = ob->data;
+  const float threshold = RNA_float_get(op->ptr, "threshold");
+  const float corner_angle = RNA_float_get(op->ptr, "corner_angle");
+
+  if (ELEM(NULL, gpd)) {
+    return OPERATOR_CANCELLED;
+  }
+
+  bool changed = false;
+  GP_EDITABLE_STROKES_BEGIN (gps_iter, C, gpl, gps) {
+    if (!GPENCIL_STROKE_IS_CURVE(gps)) {
+      if (gps->flag & GP_STROKE_SELECT) {
+        BKE_gpencil_stroke_editcurve_update(gps, threshold, corner_angle);
+        if (gps->editcurve != NULL) {
+          bGPDcurve *gpc = gps->editcurve;
+          BKE_gpencil_stroke_geometry_update(gpd, gps);
+
+          /* Select all curve points. */
+          for (uint32_t i = 0; i < gpc->tot_curve_points; i++) {
+            bGPDcurve_point *pt = &gpc->curve_points[i];
+            pt->flag &= ~GP_CURVE_POINT_SELECT;
+            BEZT_SEL_ALL(&pt->bezt);
+          }
+          gpc->flag &= ~GP_CURVE_SELECT;
+
+          /* Deselect stroke points. */
+          

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list