[Bf-blender-cvs] [3f64a0df1de] greasepencil-object: Add Set Origin to Geometry

Antonio Vazquez noreply at git.blender.org
Sun Jul 23 20:10:06 CEST 2017


Commit: 3f64a0df1de1b03618ae543557e0a223ba83d85f
Author: Antonio Vazquez
Date:   Sun Jul 23 20:09:54 2017 +0200
Branches: greasepencil-object
https://developer.blender.org/rB3f64a0df1de1b03618ae543557e0a223ba83d85f

Add Set Origin to Geometry

New option to set origin at the center of the strokes of the current frame.

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

M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/editors/object/object_transform.c

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

diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 2862979bd03..b0907caa748 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -139,6 +139,7 @@ void BKE_gpencil_palettecolor_delete_allstrokes(struct bContext *C, struct Palet
 
 /* object boundbox */
 struct BoundBox *BKE_gpencil_boundbox_get(struct Object *ob);
+void BKE_gpencil_centroid_3D(struct bGPdata *gpd, float r_centroid[3]);
 
 /* modifiers */
 void ED_gpencil_reset_modifiers(struct Object *ob);
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 96acde51f6f..bc021bfc67f 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -1563,6 +1563,7 @@ void static gpencil_minmax(bGPdata *gpd, float min[3], float max[3])
 	int i;
 	bGPDspoint *pt;
 	bGPDframe *gpf;
+	INIT_MINMAX(min, max);
 
 	for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
 		gpf= gpl->actframe;
@@ -1577,6 +1578,14 @@ void static gpencil_minmax(bGPdata *gpd, float min[3], float max[3])
 	}
 }
 
+void BKE_gpencil_centroid_3D(bGPdata *gpd, float r_centroid[3])
+{
+	float min[3], max[3], tot[3];
+	gpencil_minmax(gpd, min, max);
+	add_v3_v3v3(tot, min, max);
+	mul_v3_v3fl(r_centroid, tot, 0.5f);
+}
+
 /* create bounding box values */
 static void boundbox_gpencil(Object *ob)
 {
diff --git a/source/blender/editors/object/object_transform.c b/source/blender/editors/object/object_transform.c
index 2cd97e3c732..72673ca9dad 100644
--- a/source/blender/editors/object/object_transform.c
+++ b/source/blender/editors/object/object_transform.c
@@ -1013,52 +1013,63 @@ static int object_origin_set_exec(bContext *C, wmOperator *op)
 			}
 			else if (ob->type == OB_GPENCIL) {
 				bGPdata *gpd = ob->gpd;
-
-				if ((gpd) && (centermode == ORIGIN_TO_CURSOR)) {
-					bGPDspoint *pt;
-					float imat[3][3], bmat[3][3];
-					float offset_global[3];
-					float offset_local[3];
-					int i;
-
-					sub_v3_v3v3(offset_global, cursor, ob->obmat[3]);
-					copy_m3_m4(bmat, obact->obmat);
-					invert_m3_m3(imat, bmat);
-					mul_m3_v3(imat, offset_global);
-					mul_v3_m3v3(offset_local, imat, offset_global);
-
-					float diff_mat[4][4];
-					float inverse_diff_mat[4][4];
-
-					/* recalculate all strokes (all layers are considered without evaluating lock attributtes) */
-					for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
-						/* calculate difference matrix */
-						ED_gpencil_parent_location(obact, gpd, gpl, diff_mat);
-						/* undo matrix */
-						invert_m4_m4(inverse_diff_mat, diff_mat);
-						for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
-							for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
-								/* skip strokes that are invalid for current view */
-								if (ED_gpencil_stroke_can_use(C, gps) == false)
-									continue;
-
-								for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
-									float mpt[3];
-									mul_v3_m4v3(mpt, inverse_diff_mat, &pt->x);
-									sub_v3_v3(mpt, offset_local);
-									mul_v3_m4v3(&pt->x, diff_mat, mpt);
+				float gpcenter[3];
+				if (gpd) {
+					if (centermode == ORIGIN_TO_GEOMETRY) {
+						zero_v3(gpcenter);
+						BKE_gpencil_centroid_3D(gpd, gpcenter);
+						add_v3_v3(gpcenter, ob->obmat[3]);
+					}
+					if (centermode == ORIGIN_TO_CURSOR) {
+						copy_v3_v3(gpcenter, cursor);
+					}
+					if ((centermode == ORIGIN_TO_GEOMETRY) || (centermode == ORIGIN_TO_CURSOR)) {
+						bGPDspoint *pt;
+						float imat[3][3], bmat[3][3];
+						float offset_global[3];
+						float offset_local[3];
+						int i;
+
+						sub_v3_v3v3(offset_global, gpcenter, ob->obmat[3]);
+						copy_m3_m4(bmat, obact->obmat);
+						invert_m3_m3(imat, bmat);
+						mul_m3_v3(imat, offset_global);
+						mul_v3_m3v3(offset_local, imat, offset_global);
+
+						float diff_mat[4][4];
+						float inverse_diff_mat[4][4];
+
+						/* recalculate all strokes (all layers are considered without evaluating lock attributtes) */
+						for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+							/* calculate difference matrix */
+							ED_gpencil_parent_location(obact, gpd, gpl, diff_mat);
+							/* undo matrix */
+							invert_m4_m4(inverse_diff_mat, diff_mat);
+							for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
+								for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
+									/* skip strokes that are invalid for current view */
+									if (ED_gpencil_stroke_can_use(C, gps) == false)
+										continue;
+
+									for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+										float mpt[3];
+										mul_v3_m4v3(mpt, inverse_diff_mat, &pt->x);
+										sub_v3_v3(mpt, offset_local);
+										mul_v3_m4v3(&pt->x, diff_mat, mpt);
+									}
 								}
 							}
 						}
-					}
-					BKE_gpencil_batch_cache_dirty(gpd);
+						BKE_gpencil_batch_cache_dirty(gpd);
 
-					tot_change++;
-					ob->id.tag |= LIB_TAG_DOIT;
-					do_inverse_offset = true;
-				}
-				else {
-					BKE_report(op->reports, RPT_WARNING, "Grease Pencil Object does not support this set origin option");
+						tot_change++;
+						copy_v3_v3(ob->loc, gpcenter);
+						ob->id.tag |= LIB_TAG_DOIT;
+						do_inverse_offset = true;
+					}
+					else {
+						BKE_report(op->reports, RPT_WARNING, "Grease Pencil Object does not support this set origin option");
+					}
 				}
 			}




More information about the Bf-blender-cvs mailing list