[Bf-blender-cvs] [a8852ade8aa] master: GPencil: New Normalize Weights operator

Antonioya noreply at git.blender.org
Tue Apr 16 13:14:26 CEST 2019


Commit: a8852ade8aa65cc591d1fa20d732bb766aefe60a
Author: Antonioya
Date:   Tue Apr 16 10:47:30 2019 +0200
Branches: master
https://developer.blender.org/rBa8852ade8aa65cc591d1fa20d732bb766aefe60a

GPencil: New Normalize Weights operator

This works similar to mesh operator, but using Stroke and Points data.

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

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

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 3099888461b..eaadf35b8cc 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -4095,6 +4095,9 @@ class VIEW3D_MT_weight_gpencil(Menu):
     def draw(self, context):
         layout = self.layout
 
+        layout.operator("gpencil.vertex_group_normalize", text="Normalize")
+
+        layout.separator()
         layout.operator("gpencil.vertex_group_invert", text="Invert")
         layout.operator("gpencil.vertex_group_smooth", text="Smooth")
 
diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c
index 710e8a9bc1c..81da4ab8bc9 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -1873,6 +1873,69 @@ void GPENCIL_OT_vertex_group_smooth(wmOperatorType *ot)
 	RNA_def_int(ot->srna, "repeat", 1, 1, 10000, "Iterations", "", 1, 200);
 }
 
+/* normalize */
+static int gpencil_vertex_group_normalize_exec(bContext *C, wmOperator *UNUSED(op))
+{
+	ToolSettings *ts = CTX_data_tool_settings(C);
+	Object *ob = CTX_data_active_object(C);
+
+	/* sanity checks */
+	if (ELEM(NULL, ts, ob, ob->data))
+		return OPERATOR_CANCELLED;
+
+	MDeformVert *dvert;
+	const int def_nr = ob->actdef - 1;
+	if (!BLI_findlink(&ob->defbase, def_nr))
+		return OPERATOR_CANCELLED;
+
+	CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
+	{
+		/* look for max value */
+		float maxvalue = 0.0f;
+		for (int i = 0; i < gps->totpoints; i++) {
+			dvert = &gps->dvert[i];
+			MDeformWeight *dw = defvert_find_index(dvert, def_nr);
+			if ((dw != NULL) &&	(dw->weight > maxvalue)) {
+				maxvalue = dw->weight;
+			}
+		}
+
+		/* normalize weights */
+		if (maxvalue > 0.0f) {
+			for (int i = 0; i < gps->totpoints; i++) {
+				dvert = &gps->dvert[i];
+				MDeformWeight *dw = defvert_find_index(dvert, def_nr);
+				if (dw != NULL) {
+					dw->weight = dw->weight / maxvalue;
+				}
+			}
+		}
+	}
+	CTX_DATA_END;
+
+	/* notifiers */
+	bGPdata *gpd = ob->data;
+	DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
+	WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED | ND_SPACE_PROPERTIES, NULL);
+
+	return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_vertex_group_normalize(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Normalize Vertex Group";
+	ot->idname = "GPENCIL_OT_vertex_group_normalize";
+	ot->description = "Normalize weights to the active vertex group";
+
+	/* api callbacks */
+	ot->poll = gpencil_vertex_group_weight_poll;
+	ot->exec = gpencil_vertex_group_normalize_exec;
+
+	/* flags */
+	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+}
+
 /****************************** Join ***********************************/
 
 /* userdata for joined_gpencil_fix_animdata_cb() */
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index aa47319e3d9..84b45593244 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -496,6 +496,7 @@ void GPENCIL_OT_vertex_group_select(struct wmOperatorType *ot);
 void GPENCIL_OT_vertex_group_deselect(struct wmOperatorType *ot);
 void GPENCIL_OT_vertex_group_invert(struct wmOperatorType *ot);
 void GPENCIL_OT_vertex_group_smooth(struct wmOperatorType *ot);
+void GPENCIL_OT_vertex_group_normalize(struct wmOperatorType *ot);
 
 /* color handle */
 void GPENCIL_OT_lock_layer(struct wmOperatorType *ot);
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index c01da39bcd8..28f13ef0277 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -322,6 +322,7 @@ void ED_operatortypes_gpencil(void)
 	WM_operatortype_append(GPENCIL_OT_vertex_group_deselect);
 	WM_operatortype_append(GPENCIL_OT_vertex_group_invert);
 	WM_operatortype_append(GPENCIL_OT_vertex_group_smooth);
+	WM_operatortype_append(GPENCIL_OT_vertex_group_normalize);
 
 	/* color handle */
 	WM_operatortype_append(GPENCIL_OT_lock_layer);



More information about the Bf-blender-cvs mailing list