[Bf-blender-cvs] [cedf2f8] temp-material-remap: refactor remapping into mesh/curve API calls

Campbell Barton noreply at git.blender.org
Mon Apr 27 23:20:12 CEST 2015


Commit: cedf2f85f11eb769bfde4fde8e068c50a8f151ae
Author: Campbell Barton
Date:   Tue Apr 28 07:19:47 2015 +1000
Branches: temp-material-remap
https://developer.blender.org/rBcedf2f85f11eb769bfde4fde8e068c50a8f151ae

refactor remapping into mesh/curve API calls

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

M	source/blender/blenkernel/BKE_curve.h
M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/curve.c
M	source/blender/blenkernel/intern/material.c
M	source/blender/blenkernel/intern/mesh.c

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

diff --git a/source/blender/blenkernel/BKE_curve.h b/source/blender/blenkernel/BKE_curve.h
index ec3c044..eda4271 100644
--- a/source/blender/blenkernel/BKE_curve.h
+++ b/source/blender/blenkernel/BKE_curve.h
@@ -90,6 +90,9 @@ void BKE_curve_translate(struct Curve *cu, float offset[3], const bool do_keys);
 void BKE_curve_material_index_remove(struct Curve *cu, int index);
 void BKE_curve_material_index_clear(struct Curve *cu);
 int BKE_curve_material_index_validate(struct Curve *cu);
+void BKE_curve_material_remap(
+        struct Curve *cu,
+        const unsigned int *remap, unsigned int remap_len);
 
 ListBase    *BKE_curve_nurbs_get(struct Curve *cu);
 
diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index c0d8902..ebf4384 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -108,6 +108,7 @@ void BKE_mesh_to_curve_nurblist(struct DerivedMesh *dm, struct ListBase *nurblis
 void BKE_mesh_to_curve(struct Scene *scene, struct Object *ob);
 void BKE_mesh_material_index_remove(struct Mesh *me, short index);
 void BKE_mesh_material_index_clear(struct Mesh *me);
+void BKE_mesh_material_remap(struct Mesh *me, const unsigned int *remap, unsigned int remap_len);
 void BKE_mesh_smooth_flag_set(struct Object *meshOb, int enableSmooth);
 
 const char *BKE_mesh_cmp(struct Mesh *me1, struct Mesh *me2, float thresh);
diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c
index 5fcdc9c..cd59e30 100644
--- a/source/blender/blenkernel/intern/curve.c
+++ b/source/blender/blenkernel/intern/curve.c
@@ -4492,6 +4492,54 @@ int BKE_curve_material_index_validate(Curve *cu)
 	}
 }
 
+void BKE_curve_material_remap(Curve *cu, const unsigned int *remap, unsigned int remap_len)
+{
+	const int curvetype = BKE_curve_type_get(cu);
+	const short remap_len_short = (short)remap_len;
+
+#define MAT_NR_REMAP(n) \
+	if (n < remap_len_short) { \
+		BLI_assert(n >= 0 && remap[n] < remap_len_short); \
+		n = remap[n]; \
+	} ((void)0)
+
+	if (curvetype == OB_FONT) {
+		struct CharInfo *strinfo;
+		int charinfo_len, i;
+
+		if (cu->editfont) {
+			EditFont *ef = cu->editfont;
+			strinfo = ef->textbufinfo;
+			charinfo_len = ef->len;
+		}
+		else {
+			strinfo = cu->strinfo;
+			charinfo_len = cu->len_wchar;
+		}
+
+		for (i = 0; i <= charinfo_len; i++) {
+			if (strinfo[i].mat_nr > 0) {
+				strinfo[i].mat_nr -= 1;
+				MAT_NR_REMAP(strinfo[i].mat_nr);
+				strinfo[i].mat_nr += 1;
+			}
+		}
+	}
+	else {
+		Nurb *nu;
+		ListBase *nurbs = BKE_curve_editNurbs_get(cu);
+
+		if (nurbs) {
+			for (nu = nurbs->first; nu; nu = nu->next) {
+				MAT_NR_REMAP(nu->mat_nr);
+			}
+		}
+	}
+
+#undef MAT_NR_REMAP
+
+}
+
 void BKE_curve_rect_from_textbox(const struct Curve *cu, const struct TextBox *tb, struct rctf *r_rect)
 {
 	r_rect->xmin = cu->xof + tb->x;
diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c
index 252b675..ed0dafb 100644
--- a/source/blender/blenkernel/intern/material.c
+++ b/source/blender/blenkernel/intern/material.c
@@ -930,7 +930,6 @@ void BKE_material_remap_object(Object *ob, const unsigned int *remap)
 {
 	Material ***matar = give_matarar(ob);
 	const short *totcol_p = give_totcolp(ob);
-	const short mat_nr_max = ob->totcol;
 
 	BLI_array_permute(ob->mat, ob->totcol, remap);
 
@@ -942,75 +941,17 @@ void BKE_material_remap_object(Object *ob, const unsigned int *remap)
 		BLI_array_permute(*matar, *totcol_p, remap);
 	}
 
-#define MAT_NR_REMAP(n) \
-	if (n < mat_nr_max) { \
-		BLI_assert(n >= 0); \
-		BLI_assert(remap[n] < mat_nr_max); \
-		n = remap[n]; \
-	} ((void)0)
-
-
 	/* Now reassign the new material order to the affected items in the object*/
 	if (ob->type == OB_MESH) {
-		if (ob->mode == OB_MODE_EDIT) {
-			BMEditMesh *em = BKE_editmesh_from_object(ob);
-
-			if (em) {
-				BMIter iter;
-				BMFace *efa;
-
-				BM_ITER_MESH(efa, &iter, em->bm, BM_FACES_OF_MESH) {
-					MAT_NR_REMAP(efa->mat_nr);
-				}
-			}
-		}
-		else {
-			Mesh *me = ob->data;
-			for (int index = 0; index < me->totpoly; index++) {
-				MAT_NR_REMAP(me->mpoly[index].mat_nr);
-			}
-		}
+		BKE_mesh_material_remap(ob->data, remap, ob->totcol);
 	}
-	else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
-		Nurb *nu;
-		ListBase *nurbs = BKE_curve_editNurbs_get((Curve *)ob->data);
-
-		if (nurbs) {
-			for (nu = nurbs->first; nu; nu = nu->next) {
-				MAT_NR_REMAP(nu->mat_nr);
-			}
-		}
-	}
-	else if (ob->type == OB_FONT) {
-		Curve *cu = ob->data;
-		struct CharInfo *strinfo;
-		int charinfo_len;
-
-		if (cu->editfont) {
-			EditFont *ef = cu->editfont;
-			strinfo = ef->textbufinfo;
-			charinfo_len = ef->len;
-		}
-		else {
-			strinfo = cu->strinfo;
-			charinfo_len = cu->len_wchar;
-		}
-
-		for (int i = 0; i <= charinfo_len; i++) {
-			if (strinfo[i].mat_nr > 0) {
-				strinfo[i].mat_nr -= 1;
-				MAT_NR_REMAP(strinfo[i].mat_nr);
-				strinfo[i].mat_nr += 1;
-			}
-		}
+	else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
+		BKE_curve_material_remap(ob->data, remap, ob->totcol);
 	}
 	else {
 		/* add support for this object data! */
 		BLI_assert(matar == NULL);
 	}
-
-#undef MAT_NR_REMAP
-
 }
 
 
diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index 87325a7..12379be 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -1773,6 +1773,36 @@ void BKE_mesh_material_index_clear(Mesh *me)
 	}
 }
 
+void BKE_mesh_material_remap(Mesh *me, const unsigned int *remap, unsigned int remap_len)
+{
+	const short remap_len_short = (short)remap_len;
+
+#define MAT_NR_REMAP(n) \
+	if (n < remap_len_short) { \
+		BLI_assert(n >= 0 && remap[n] < remap_len_short); \
+		n = remap[n]; \
+	} ((void)0)
+
+	if (me->edit_btmesh) {
+		BMEditMesh *em = me->edit_btmesh;
+		BMIter iter;
+		BMFace *efa;
+
+		BM_ITER_MESH(efa, &iter, em->bm, BM_FACES_OF_MESH) {
+			MAT_NR_REMAP(efa->mat_nr);
+		}
+	}
+	else {
+		int i;
+		for (i = 0; i < me->totpoly; i++) {
+			MAT_NR_REMAP(me->mpoly[i].mat_nr);
+		}
+	}
+
+#undef MAT_NR_REMAP
+
+}
+
 void BKE_mesh_smooth_flag_set(Object *meshOb, int enableSmooth) 
 {
 	Mesh *me = meshOb->data;




More information about the Bf-blender-cvs mailing list