[Bf-blender-cvs] [572b147d5b1] greasepencil-object: WIP: New operator to convert 2.7 grease pencil files

Antonio Vazquez noreply at git.blender.org
Wed May 23 16:38:18 CEST 2018


Commit: 572b147d5b1f5f143dc501bbe9cb2b46870c55b5
Author: Antonio Vazquez
Date:   Wed May 23 16:37:32 2018 +0200
Branches: greasepencil-object
https://developer.blender.org/rB572b147d5b1f5f143dc501bbe9cb2b46870c55b5

WIP: New operator to convert 2.7 grease pencil files

The operator convert the old 2.7x files to the new materials of 2.8.

This code is not defined in versioning code to avoid conversion of annotations.

Still pending some issues.

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

M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/blenloader/intern/versioning_280.c
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/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 8f2314e1737..c02b0d2bcfc 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -545,6 +545,10 @@ void BKE_gpencil_stroke_weights_duplicate(bGPDstroke *gps_src, bGPDstroke *gps_d
 	}
 	BLI_assert(gps_src->totpoints == gps_dst->totpoints);
 
+	if ((gps_src->dvert == NULL) || (gps_dst->dvert == NULL)){
+		return;
+	}
+
 	for (int i = 0; i < gps_src->totpoints; i++) {
 		MDeformVert *dvert_src = &gps_src->dvert[i];
 		MDeformVert *dvert_dst = &gps_dst->dvert[i];
@@ -554,6 +558,7 @@ void BKE_gpencil_stroke_weights_duplicate(bGPDstroke *gps_src, bGPDstroke *gps_d
 		else {
 			dvert_dst->dw = NULL;
 		}
+
 	}
 }
 
diff --git a/source/blender/blenloader/intern/versioning_280.c b/source/blender/blenloader/intern/versioning_280.c
index 1b77abcf78f..3ff66aaf060 100644
--- a/source/blender/blenloader/intern/versioning_280.c
+++ b/source/blender/blenloader/intern/versioning_280.c
@@ -846,89 +846,6 @@ void do_versions_after_linking_280(Main *main)
 				}
 			}
 		}
-
-
-		/* Grease Pencil Object */
-		/* Convert grease pencil datablock to GP object */
-#if 0 /* XXX: Needs review - maybe we don't want to do this, as annotations could cause havok on cycles files! */
-		for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
-			if (scene->gpd) {
-				Object *ob;
-				ViewLayer *view_layer = scene->view_layers.first; /* Weak, but at least it goes somewhere... */
-				if (view_layer == NULL) {
-					view_layer = BKE_view_layer_add(scene, "Viewport");
-					printf("added scene layer again - %p\n", view_layer);
-				}
-
-				ob = BKE_object_add_for_data(main, scene, view_layer, OB_GPENCIL, "GP_Scene", &scene->gpd->id, false);
-				zero_v3(ob->loc);
-
-				/* convert grease pencil palettes (version >= 2.78)  to materials and weights */
-				bGPdata *gpd = scene->gpd;
-				for (const bGPDpalette *palette = gpd->palettes.first; palette; palette = palette->next) {
-					for (bGPDpalettecolor *palcolor = palette->colors.first; palcolor; palcolor = palcolor->next) {
-
-						/* create material slot */
-						BKE_object_material_slot_add(ob);
-						Material *ma = BKE_material_add_gpencil(main, palcolor->info);
-						assign_material(ob, ma, ob->totcol, BKE_MAT_ASSIGN_EXISTING);
-
-						/* copy color settings */
-						MaterialGPencilStyle *gp_style = ma->gp_style;
-						copy_v4_v4(gp_style->rgb, palcolor->color);
-						copy_v4_v4(gp_style->fill, palcolor->fill);
-						gp_style->flag = palcolor->flag;
-
-						/* fix strokes */
-						for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
-							for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
-								for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
-									if (STREQ(gps->colorname, palcolor->info)) {
-										gps->mat_nr = ob->totcol - 1;
-									}
-									/* create weights array */
-									gps->dvert = MEM_callocN(sizeof(gps->dvert) * gps->totpoints, "gp_stroke_weights");
-								}
-							}
-						}
-					}
-				}
-
-				/* set cache as dirty */
-				BKE_gpencil_batch_cache_dirty(ob->data);
-
-				scene->gpd = NULL;
-			}
-		}
-
-		/* Handle object-linked grease pencil datablocks */
-		for (Object *ob = main->object.first; ob; ob = ob->id.next) {
-			if (ob->gpd) {
-				if (ob->type == OB_GPENCIL) {
-					/* GP Object - remap the links */
-					ob->data = ob->gpd;
-					ob->gpd = NULL;
-				}
-				else if (ob->type == OB_EMPTY) {
-					/* Empty with GP data - This should be able to be converted
-					 * to a GP object with little data loss
-					 */
-					ob->data = ob->gpd;
-					ob->gpd = NULL;
-					ob->type = OB_GPENCIL;
-				}
-				else {
-					/* FIXME: What to do in this case?
-					 *
-					 * We cannot create new objects for these, as we don't have a scene & scene layer
-					 * to put them into from here...
-					 */
-					printf("WARNING: Old Grease Pencil data ('%s') still exists on Object '%s'\n",
-						ob->gpd->id.name + 2, ob->id.name + 2);
-				}
-			}
-		}
-#endif
 	}
 
 #ifdef USE_COLLECTION_COMPAT_28
diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c
index 553728fd77f..aa76b44b0df 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -2394,4 +2394,111 @@ void GPENCIL_OT_color_choose(wmOperatorType *ot)
 	RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Index of Color", 0, INT_MAX);
 }
 
+/* ***************** Convert old 2.7 files to 2.8 ************************ */
+static int gpencil_convert_old_files_poll(bContext *C)
+{
+	Scene *scene = CTX_data_scene(C);
+
+	return (int) (scene->gpd != NULL);
+}
+
+static int gpencil_convert_old_files_exec(bContext *C, wmOperator *op)
+{
+	Main *bmain = CTX_data_main(C);
+	Scene *scene = CTX_data_scene(C);
+	ViewLayer *view_layer = CTX_data_view_layer(C);
+
+	/* Convert grease pencil scene datablock to GP object */
+	if ((scene->gpd) && (view_layer != NULL)) {
+		Object *ob;
+		ob = BKE_object_add_for_data(bmain, view_layer, OB_GPENCIL, "GP_Scene", &scene->gpd->id, false);
+		zero_v3(ob->loc);
+
+		/* convert grease pencil palettes (version >= 2.78)  to materials and weights */
+		bGPdata *gpd = scene->gpd;
+		for (const bGPDpalette *palette = gpd->palettes.first; palette; palette = palette->next) {
+			for (bGPDpalettecolor *palcolor = palette->colors.first; palcolor; palcolor = palcolor->next) {
+
+				/* create material slot */
+				BKE_object_material_slot_add(ob);
+				Material *ma = BKE_material_add_gpencil(bmain, palcolor->info);
+				assign_material(ob, ma, ob->totcol, BKE_MAT_ASSIGN_EXISTING);
+
+				/* copy color settings */
+				MaterialGPencilStyle *gp_style = ma->gp_style;
+				copy_v4_v4(gp_style->rgb, palcolor->color);
+				copy_v4_v4(gp_style->fill, palcolor->fill);
+				gp_style->flag = palcolor->flag;
+
+				/* fix strokes */
+				for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+					for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
+						for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
+							if (STREQ(gps->colorname, palcolor->info)) {
+								gps->mat_nr = ob->totcol - 1;
+								/* create weights array */
+								gps->dvert = MEM_callocN(sizeof(gps->dvert) * gps->totpoints, "gp_stroke_weights");
+							}
+						}
+					}
+				}
+			}
+		}
+
+		/* set cache as dirty */
+		BKE_gpencil_batch_cache_dirty(ob->data);
+
+		scene->gpd = NULL;
+	}
+
+#if 0 /* GPXX */
+	/* Handle object-linked grease pencil datablocks */
+	for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
+		if (ob->gpd) {
+			if (ob->type == OB_GPENCIL) {
+				/* GP Object - remap the links */
+				ob->data = ob->gpd;
+				ob->gpd = NULL;
+			}
+			else if (ob->type == OB_EMPTY) {
+				/* Empty with GP data - This should be able to be converted
+				* to a GP object with little data loss
+				*/
+				ob->data = ob->gpd;
+				ob->gpd = NULL;
+				ob->type = OB_GPENCIL;
+			}
+			else {
+				/* FIXME: What to do in this case?
+				*
+				* We cannot create new objects for these, as we don't have a scene & scene layer
+				* to put them into from here...
+				*/
+				printf("WARNING: Old Grease Pencil data ('%s') still exists on Object '%s'\n",
+					ob->gpd->id.name + 2, ob->id.name + 2);
+			}
+		}
+	}
+#endif
+
+	/* notifiers */
+	WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+
+	return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_convert_old_files(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Convert 2.7 Grease Pencil File";
+	ot->idname = "GPENCIL_OT_convert_old_files";
+	ot->description = "Convert 2.7x grease pencil files to 2.8";
+
+	/* callbacks */
+	ot->exec = gpencil_convert_old_files_exec;
+	ot->poll = gpencil_convert_old_files_poll;
+
+	/* flags */
+	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+}
 
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index 2e44d332d5a..5ccbb9e1003 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -417,6 +417,8 @@ void GPENCIL_OT_color_unlock_all(struct wmOperatorType *ot);
 void GPENCIL_OT_color_select(struct wmOperatorType *ot);
 void GPENCIL_OT_color_choose(struct wmOperatorType *ot);
 
+/* convert old 2.7 files to 2.8 */
+void GPENCIL_OT_convert_old_files(struct wmOperatorType *ot);
 
 /* ****************************************************** */
 /* FILTERED ACTION DATA - TYPES  ---> XXX DEPRECEATED OLD ANIM SYSTEM CODE! */
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index 1a0185d6d75..2a604ac6190 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -847,7 +847,6 @@ void ED_operatortypes_gpencil(void)
 	WM_operatortype_append(GPENCIL_OT_color_select);
 	WM_operatortype_append(GPENCIL_OT_color_choose);
 
-
 	/* Editing (Time) --------------- */
 	
 	/* Interpolation */
@@ -857,6 +856,10 @@ void ED_operatortypes_gpencil(void)
 
 	/* Primitives */
 	WM_operatortype_append(GPENCIL_OT_primitive);
+
+	/* convert old 2.7 files to 2.8 */
+	WM_operatortype_append(GPENCIL_OT_convert_old_files);
+
 }
 
 void ED_operatormacros_gpencil(void)



More information about the Bf-blender-cvs mailing list