[Bf-blender-cvs] [a5d66391ab7] greasepencil-object: GP: Mew Merge Strokes operator

Antonioya noreply at git.blender.org
Thu Dec 27 17:48:39 CET 2018


Commit: a5d66391ab75be2dcd7477fad59bce1ecd926bd3
Author: Antonioya
Date:   Thu Dec 27 17:48:28 2018 +0100
Branches: greasepencil-object
https://developer.blender.org/rBa5d66391ab75be2dcd7477fad59bce1ecd926bd3

GP: Mew Merge Strokes operator

This operator allows to create a new stroke joining several stroke selected points of different strokes.

The new stroke will use the current material.

To use, first select the points to be merged. Optionally can remove the old points and strokes.

The operator is available in Edit mode in the Specials menu and Stroke menu.

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

M	release/scripts/startup/bl_ui/properties_grease_pencil_common.py
M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/gpencil/CMakeLists.txt
M	source/blender/editors/gpencil/gpencil_intern.h
A	source/blender/editors/gpencil/gpencil_merge.c
M	source/blender/editors/gpencil/gpencil_ops.c

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

diff --git a/release/scripts/startup/bl_ui/properties_grease_pencil_common.py b/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
index be335469418..8e29e03a8f6 100644
--- a/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
+++ b/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
@@ -217,6 +217,7 @@ class GreasePencilStrokeEditPanel:
         col.separator()
 
         row = col.row(align=True)
+        row.operator("gpencil.stroke_merge", text="Merge")
         row.operator("gpencil.stroke_join", text="Join").type = 'JOIN'
         row.operator("gpencil.stroke_join", text="& Copy").type = 'JOINCOPY'
 
diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 93f772785a5..9c2f25b4fb1 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -3935,6 +3935,7 @@ class VIEW3D_MT_edit_gpencil(Menu):
 
         layout.operator_menu_enum("gpencil.stroke_separate", "mode", text="Separate...")
         layout.operator("gpencil.stroke_split", text="Split")
+        layout.operator("gpencil.stroke_merge", text="Merge")
         layout.operator_menu_enum("gpencil.stroke_join", "type", text="Join...")
         layout.operator("gpencil.stroke_flip", text="Flip Direction")
 
@@ -5558,6 +5559,7 @@ class VIEW3D_MT_gpencil_edit_specials(Menu):
 
         layout.separator()
 
+        layout.operator("gpencil.stroke_merge", text="Merge")
         layout.operator("gpencil.stroke_join", text="Join").type = 'JOIN'
         layout.operator("gpencil.stroke_join", text="Join & Copy").type = 'JOINCOPY'
         layout.operator("gpencil.stroke_flip", text="Flip Direction")
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt
index cea2e5d4269..576e16077d2 100644
--- a/source/blender/editors/gpencil/CMakeLists.txt
+++ b/source/blender/editors/gpencil/CMakeLists.txt
@@ -49,6 +49,7 @@ set(SRC
 	gpencil_convert.c
 	gpencil_data.c
 	gpencil_edit.c
+	gpencil_merge.c
 	gpencil_interpolate.c
 	gpencil_primitive.c
 	gpencil_ops.c
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index 991cd52f523..6a944645f5f 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -388,6 +388,10 @@ enum {
 	GP_STROKE_CURVE = 4,
 };
 
+enum {
+	GP_MERGE_STROKE = -1,
+	GP_MERGE_POINT = 1
+};
 
 void GPENCIL_OT_stroke_arrange(struct wmOperatorType *ot);
 void GPENCIL_OT_stroke_change_color(struct wmOperatorType *ot);
@@ -402,6 +406,7 @@ 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_stroke_merge(struct wmOperatorType *ot);
 
 void GPENCIL_OT_brush_presets_create(struct wmOperatorType *ot);
 
diff --git a/source/blender/editors/gpencil/gpencil_merge.c b/source/blender/editors/gpencil/gpencil_merge.c
new file mode 100644
index 00000000000..eed34f6831b
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_merge.c
@@ -0,0 +1,577 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2019, Blender Foundation.
+ * This is a new part of Blender
+ *
+ * Contributor(s): Antonio Vazquez
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ *
+ * Operators for merge Grease Pencil strokes
+ */
+
+ /** \file blender/editors/gpencil/gpencil_merge.c
+  *  \ingroup edgpencil
+  */
+
+
+#include <stdio.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_ghash.h"
+#include "BLI_math.h"
+
+#include "DNA_gpencil_types.h"
+
+#include "BKE_brush.h"
+#include "BKE_context.h"
+#include "BKE_gpencil.h"
+#include "BKE_material.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "ED_gpencil.h"
+#include "ED_object.h"
+#include "ED_screen.h"
+#include "ED_view3d.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
+
+#include "gpencil_intern.h"
+
+typedef struct tGPencilPointCache {
+	float factor;  /* value to sort */
+	float pos2d[2];
+	bGPDstroke *gps;
+	float x, y, z;
+	float pressure;
+	float strength;
+} tGPencilPointCache;
+
+/* helper function to sort strokes around center */
+static int gpencil_sort_points(const void *a1, const void *a2)
+{
+	const tGPencilPointCache *ps1 = a1, *ps2 = a2;
+
+	if (ps1->factor < ps2->factor) return -1;
+	else if (ps1->factor > ps2->factor) return 1;
+
+	return 0;
+}
+
+static void gpencil_insert_points_to_stroke(
+	bGPDstroke *gps, tGPencilPointCache *points_array, int totpoints)
+{
+	tGPencilPointCache *point_elem = NULL;
+
+	for (int i = 0; i < totpoints; i++) {
+		point_elem = &points_array[i];
+		bGPDspoint *pt_dst = &gps->points[i];
+
+		copy_v3_v3(&pt_dst->x, &point_elem->x);
+		pt_dst->pressure = point_elem->pressure;
+		pt_dst->strength = point_elem->strength;
+		pt_dst->uv_fac = 1.0f;
+		pt_dst->uv_rot = 0;
+		pt_dst->flag |= GP_SPOINT_SELECT;
+	}
+
+}
+
+static bGPDstroke *gpencil_prepare_stroke(bContext *C, wmOperator *op, int totpoints)
+{
+	ToolSettings *ts = CTX_data_tool_settings(C);
+	Depsgraph *depsgraph = CTX_data_depsgraph(C);
+	Object *ob = CTX_data_active_object(C);
+	bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
+
+	int cfra_eval = (int)DEG_get_ctime(depsgraph);
+
+	const bool back = RNA_boolean_get(op->ptr, "back");
+	const bool additive = RNA_boolean_get(op->ptr, "additive");
+	const bool cyclic = RNA_boolean_get(op->ptr, "cyclic");
+
+	Paint *paint = &ts->gp_paint->paint;
+	/* if not exist, create a new one */
+	if (paint->brush == NULL) {
+		/* create new brushes */
+		BKE_brush_gpencil_presets(C);
+	}
+	Brush *brush = paint->brush;
+
+	/* frame */
+	short add_frame_mode;
+	if (additive) {
+		add_frame_mode = GP_GETFRAME_ADD_COPY;
+	}
+	else {
+		add_frame_mode = GP_GETFRAME_ADD_NEW;
+	}
+	bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, cfra_eval, add_frame_mode);
+
+	/* stroke */
+	bGPDstroke *gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
+	gps->totpoints = totpoints;
+	gps->inittime = 0.0f;
+	gps->thickness = brush->size;
+	gps->flag |= GP_STROKE_RECALC_CACHES;
+	gps->flag |= GP_STROKE_SELECT;
+	gps->flag |= GP_STROKE_3DSPACE;
+	gps->mat_nr = ob->actcol - 1;
+
+	/* allocate memory for points */
+	gps->points = MEM_callocN(sizeof(bGPDspoint) * totpoints, "gp_stroke_points");
+	/* initialize triangle memory to dummy data */
+	gps->tot_triangles = 0;
+	gps->triangles = NULL;
+	gps->flag |= GP_STROKE_RECALC_CACHES;
+
+	if (cyclic) {
+		gps->flag |= GP_STROKE_CYCLIC;
+	}
+
+	/* add new stroke to frame */
+	if (back) {
+		BLI_addhead(&gpf->strokes, gps);
+	}
+	else {
+		BLI_addtail(&gpf->strokes, gps);
+	}
+
+	return gps;
+}
+
+static void gpencil_get_elements_len(bContext *C, int *totstrokes, int *totpoints)
+{
+	bGPDspoint *pt;
+	int i;
+
+	/* count number of strokes and selected points */
+	CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
+	{
+		if (gps->flag & GP_STROKE_SELECT) {
+			*totstrokes += 1;
+			for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+				if (pt->flag & GP_SPOINT_SELECT) {
+					*totpoints += 1;
+				}
+			}
+		}
+	}
+	CTX_DATA_END;
+}
+
+static void gpencil_dissolve_points(bContext *C)
+{
+	bGPDstroke *gps, *gpsn;
+
+	CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+	{
+		bGPDframe *gpf = gpl->actframe;
+		if (gpf == NULL) {
+			continue;
+		}
+
+		for (gps = gpf->strokes.first; gps; gps = gpsn) {
+			gpsn = gps->next;
+			gp_stroke_delete_tagged_points(gpf, gps, gpsn, GP_SPOINT_TAG, false);
+		}
+	}
+	CTX_DATA_END;
+}
+
+/* Calc a factor of each selected point and fill an array with all the data.
+ *
+ * The factor is calculated using an imaginary circle, using the angle relative
+ * to this circle and the distance to the calculated center of the selected points.
+ *
+ * All the data is saved to be sorted and used later.
+ */
+static void gpencil_calc_points_factor(
+			bContext *C, const int mode, int totpoints,
+			const bool clear_point, const bool clear_stroke,
+			tGPencilPointCache *original_array)
+{
+	bGPDspoint *pt;
+	int i;
+	int idx = 0;
+
+	/* create selected point array an fill it */
+	bGPDstroke **gps_array = MEM_callocN(sizeof(bGPDstroke *) * totpoints, __func__);
+	bGPDspoint *pt_array = MEM_callocN(sizeof(bGPDspoint) * totpoints, __func__);
+
+	CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+	{
+		bGPDframe *gpf = gpl->actframe;
+		if (gpf == NULL) {
+			continue;
+		}
+		for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
+			if (gps->flag & GP_STROKE_SELECT) {
+				for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+					if (clear_stroke) {
+						pt->flag |= GP_SPOINT_TAG;
+					}
+					else {
+						pt->flag &= ~GP_SPOINT_TAG;
+					}
+
+					if (pt->flag & GP_SPOINT_SELECT) {
+						bGPDspoint *pt2 = &pt_array[idx];
+						copy_v3_v3(&pt2->x, &pt->x);
+						pt2->pressure = pt->pressure;
+						pt2->strength = pt->strength;
+						pt->flag &= ~GP_SPOINT_SELECT;
+						if (clear_point) {
+							pt->flag |= GP_SPOINT_TAG;
+						}
+
+						/* save stroke */
+						gps_array[idx] = gps;
+
+						idx++;
+					}
+				}
+				gps->flag &= ~GP_STROKE_SELECT;
+			}
+		}
+	}
+	CTX_DATA_END;
+
+	/* project in 2d plane */
+	int direction = 0;
+	float(

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list