[Bf-blender-cvs] [ba42932] bake-cycles: Cycles-Bake: get the mesh with modifiers applied

Dalai Felinto noreply at git.blender.org
Wed Apr 23 02:47:11 CEST 2014


Commit: ba42932aa3a601a867c2f63dac8ec5c20da45de9
Author: Dalai Felinto
Date:   Tue Feb 11 00:10:11 2014 -0200
https://developer.blender.org/rBba42932aa3a601a867c2f63dac8ec5c20da45de9

Cycles-Bake: get the mesh with modifiers applied

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

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/mesh.c
M	source/blender/editors/object/object_bake_new.c
M	source/blender/makesrna/intern/rna_main_api.c
M	source/blender/render/extern/include/RE_bake.h
M	source/blender/render/intern/source/bake_new.c

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

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index 4b346b2..587dea5 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -127,6 +127,9 @@ bool BKE_mesh_uv_cdlayer_rename(struct Mesh *me, const char *old_name, const cha
 
 float (*BKE_mesh_vertexCos_get(struct Mesh *me, int *r_numVerts))[3];
 
+struct Mesh *BKE_mesh_new_from_object(struct Main *bmain, struct Scene *sce, struct Object *ob,
+                                      int apply_modifiers, int settings, int calc_tessface, int calc_undeformed);
+
 /* vertex level transformations & checks (no derived mesh) */
 
 bool BKE_mesh_minmax(struct Mesh *me, float r_min[3], float r_max[3]);
diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index 7d8d98a..da4c331 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -54,6 +54,8 @@
 #include "BKE_modifier.h"
 #include "BKE_multires.h"
 #include "BKE_key.h"
+#include "BKE_mball.h"
+#include "BKE_depsgraph.h"
 /* these 2 are only used by conversion functions */
 #include "BKE_curve.h"
 /* -- */
@@ -2086,3 +2088,220 @@ void BKE_mesh_mselect_active_set(Mesh *me, int index, int type)
 	BLI_assert((me->mselect[me->totselect - 1].index == index) &&
 	           (me->mselect[me->totselect - 1].type  == type));
 }
+
+/* settings: 1 - preview, 2 - render */
+Mesh *BKE_mesh_new_from_object(
+        Main *bmain, Scene *sce, Object *ob,
+        int apply_modifiers, int settings, int calc_tessface, int calc_undeformed)
+{
+	Mesh *tmpmesh;
+	Curve *tmpcu = NULL, *copycu;
+	Object *tmpobj = NULL;
+	int render = settings == eModifierMode_Render, i;
+	int cage = !apply_modifiers;
+
+	/* perform the mesh extraction based on type */
+	switch (ob->type) {
+		case OB_FONT:
+		case OB_CURVE:
+		case OB_SURF:
+		{
+			ListBase dispbase = {NULL, NULL};
+			DerivedMesh *derivedFinal = NULL;
+			int uv_from_orco;
+
+			/* copies object and modifiers (but not the data) */
+			tmpobj = BKE_object_copy_ex(bmain, ob, true);
+			tmpcu = (Curve *)tmpobj->data;
+			tmpcu->id.us--;
+
+			/* if getting the original caged mesh, delete object modifiers */
+			if (cage)
+				BKE_object_free_modifiers(tmpobj);
+
+			/* copies the data */
+			copycu = tmpobj->data = BKE_curve_copy((Curve *) ob->data);
+
+			/* temporarily set edit so we get updates from edit mode, but
+			 * also because for text datablocks copying it while in edit
+			 * mode gives invalid data structures */
+			copycu->editfont = tmpcu->editfont;
+			copycu->editnurb = tmpcu->editnurb;
+
+			/* get updated display list, and convert to a mesh */
+			BKE_displist_make_curveTypes_forRender(sce, tmpobj, &dispbase, &derivedFinal, false, render);
+
+			copycu->editfont = NULL;
+			copycu->editnurb = NULL;
+
+			tmpobj->derivedFinal = derivedFinal;
+
+			/* convert object type to mesh */
+			uv_from_orco = (tmpcu->flag & CU_UV_ORCO) != 0;
+			BKE_mesh_from_nurbs_displist(tmpobj, &dispbase, uv_from_orco);
+
+			tmpmesh = tmpobj->data;
+
+			BKE_displist_free(&dispbase);
+
+			/* BKE_mesh_from_nurbs changes the type to a mesh, check it worked.
+			 * if it didn't the curve did not have any segments or otherwise 
+			 * would have generated an empty mesh */
+			if (tmpobj->type != OB_MESH) {
+				BKE_libblock_free_us(G.main, tmpobj);
+				return NULL;
+			}
+
+			BKE_mesh_texspace_copy_from_object(tmpmesh, ob);
+
+			BKE_libblock_free_us(bmain, tmpobj);
+			break;
+		}
+
+		case OB_MBALL:
+		{
+			/* metaballs don't have modifiers, so just convert to mesh */
+			Object *basis_ob = BKE_mball_basis_find(sce, ob);
+			/* todo, re-generatre for render-res */
+			/* metaball_polygonize(scene, ob) */
+
+			if (ob != basis_ob)
+				return NULL;  /* only do basis metaball */
+
+			tmpmesh = BKE_mesh_add(bmain, "Mesh");
+			/* BKE_mesh_add gives us a user count we don't need */
+			tmpmesh->id.us--;
+
+			if (render) {
+				ListBase disp = {NULL, NULL};
+				/* TODO(sergey): This is gonna to work for until EvaluationContext
+				 *               only contains for_render flag. As soon as CoW is
+				 *               implemented, this is to be rethinked.
+				 */
+				EvaluationContext eval_ctx = {0};
+				eval_ctx.for_render = render;
+				BKE_displist_make_mball_forRender(&eval_ctx, sce, ob, &disp);
+				BKE_mesh_from_metaball(&disp, tmpmesh);
+				BKE_displist_free(&disp);
+			}
+			else {
+				ListBase disp = {NULL, NULL};
+				if (ob->curve_cache) {
+					disp = ob->curve_cache->disp;
+				}
+				BKE_mesh_from_metaball(&disp, tmpmesh);
+			}
+
+			BKE_mesh_texspace_copy_from_object(tmpmesh, ob);
+
+			break;
+
+		}
+		case OB_MESH:
+			/* copies object and modifiers (but not the data) */
+			if (cage) {
+				/* copies the data */
+				tmpmesh = BKE_mesh_copy_ex(bmain, ob->data);
+				/* if not getting the original caged mesh, get final derived mesh */
+			}
+			else {
+				/* Make a dummy mesh, saves copying */
+				DerivedMesh *dm;
+				/* CustomDataMask mask = CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL; */
+				CustomDataMask mask = CD_MASK_MESH; /* this seems more suitable, exporter,
+			                                         * for example, needs CD_MASK_MDEFORMVERT */
+
+				if (calc_undeformed)
+					mask |= CD_MASK_ORCO;
+
+				/* Write the display mesh into the dummy mesh */
+				if (render)
+					dm = mesh_create_derived_render(sce, ob, mask);
+				else
+					dm = mesh_create_derived_view(sce, ob, mask);
+
+				tmpmesh = BKE_mesh_add(bmain, "Mesh");
+				DM_to_mesh(dm, tmpmesh, ob, mask);
+				dm->release(dm);
+			}
+
+			/* BKE_mesh_add/copy gives us a user count we don't need */
+			tmpmesh->id.us--;
+
+			break;
+		default:
+			/* "Object does not have geometry data") */
+			return NULL;
+	}
+
+	/* Copy materials to new mesh */
+	switch (ob->type) {
+		case OB_SURF:
+		case OB_FONT:
+		case OB_CURVE:
+			tmpmesh->totcol = tmpcu->totcol;
+
+			/* free old material list (if it exists) and adjust user counts */
+			if (tmpcu->mat) {
+				for (i = tmpcu->totcol; i-- > 0; ) {
+					/* are we an object material or data based? */
+
+					tmpmesh->mat[i] = ob->matbits[i] ? ob->mat[i] : tmpcu->mat[i];
+
+					if (tmpmesh->mat[i]) {
+						tmpmesh->mat[i]->id.us++;
+					}
+				}
+			}
+			break;
+
+#if 0
+		/* Crashes when assigning the new material, not sure why */
+		case OB_MBALL:
+			tmpmb = (MetaBall *)ob->data;
+			tmpmesh->totcol = tmpmb->totcol;
+
+			/* free old material list (if it exists) and adjust user counts */
+			if (tmpmb->mat) {
+				for (i = tmpmb->totcol; i-- > 0; ) {
+					tmpmesh->mat[i] = tmpmb->mat[i]; /* CRASH HERE ??? */
+					if (tmpmesh->mat[i]) {
+						tmpmb->mat[i]->id.us++;
+					}
+				}
+			}
+			break;
+#endif
+
+		case OB_MESH:
+			if (!cage) {
+				Mesh *origmesh = ob->data;
+				tmpmesh->flag = origmesh->flag;
+				tmpmesh->mat = MEM_dupallocN(origmesh->mat);
+				tmpmesh->totcol = origmesh->totcol;
+				tmpmesh->smoothresh = origmesh->smoothresh;
+				if (origmesh->mat) {
+					for (i = origmesh->totcol; i-- > 0; ) {
+						/* are we an object material or data based? */
+						tmpmesh->mat[i] = ob->matbits[i] ? ob->mat[i] : origmesh->mat[i];
+
+						if (tmpmesh->mat[i]) {
+							tmpmesh->mat[i]->id.us++;
+						}
+					}
+				}
+			}
+			break;
+	} /* end copy materials */
+
+	if (calc_tessface) {
+		/* cycles and exporters rely on this still */
+		BKE_mesh_tessface_ensure(tmpmesh);
+	}
+
+	/* make sure materials get updated in objects */
+	test_object_materials(bmain, &tmpmesh->id);
+
+	return tmpmesh;
+}
+
diff --git a/source/blender/editors/object/object_bake_new.c b/source/blender/editors/object/object_bake_new.c
index 3ab587d..b694ea6 100644
--- a/source/blender/editors/object/object_bake_new.c
+++ b/source/blender/editors/object/object_bake_new.c
@@ -185,10 +185,12 @@ static bool is_data_pass(ScenePassType pass_type)
 
 static int bake_exec(bContext *C, wmOperator *op)
 {
+	Main *bmain = CTX_data_main(C);
 	int op_result = OPERATOR_CANCELLED;
 	bool ok = false;
 	Scene *scene = CTX_data_scene(C);
 	Object *object = CTX_data_active_object(C);
+	Mesh *me = NULL;
 
 	int pass_type = RNA_enum_get(op->ptr, "type");
 
@@ -204,9 +206,10 @@ static int bake_exec(bContext *C, wmOperator *op)
 	const bool is_external = RNA_boolean_get(op->ptr, "is_save_external");
 	const bool is_linear = is_data_pass(pass_type);
 	char filepath[FILE_MAX];
+	int need_undeformed = 0;
 	RNA_string_get(op->ptr, "filepath", filepath);
 
-	RE_engine_bake_set_engine_parameters(re, CTX_data_main(C), scene);
+	RE_engine_bake_set_engine_parameters(re, bmain, scene);
 
 	G.is_break = FALSE;
 
@@ -251,8 +254,18 @@ static int bake_exec(bContext *C, wmOperator *op)
 	 		elif ... (vertex color?)
 	 */
 
+
+	/* get the mesh as it arrives in the renderer */
+	//XXX Cycles needs that sometimes, will leave it always on for now
+	//bool need_undeformed = mesh->need_attribute(scene, ATTR_STD_GENERATED);
+	need_undeformed = 1;
+
+	//int apply_modifiers, int settings (1=preview, 2=render), int calc_tessface, int calc_undeformed
+	me = BKE_mesh_new_from_object(bmain, scene, object, 1, 2, 1, need_undeformed);
+	//TODO delete the mesh afterwards
+
 	/* populate the pixel array with the face data */
-	RE_populate_bake_pixels(object, pixel_array, width, height);
+	RE_populate_bake_pixels(me, pixel_array, width, height);
 
 	if (RE_engine_has_bake(re))
 		ok = RE_engine_bake(re, object, pixel_array, num_pixels, depth, pass_type, result);
diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c
index f708037..d4509c1 100644
--- a/source/blender/makesrna/intern/rna_main_api.c
+++ b/source/blender/makesrna/intern/rna_main_api.c
@@ -292,216 +292,19 @@ Mesh *rna_Main_meshes_new_from_object(
         Main *bmain, ReportList *reports, Scene *sce,
         Object *ob, int apply_modifiers, int settings, int calc_tessface, int calc_undeformed)
 {
-	Mesh *tmpmesh;
-	Curve *tmpcu = NULL, *copycu;
-	Object *tmpobj = NULL;
-	const bool use_render_resolution = (settings == eModifierMode_Render);
-	int i;
-	int cage = !apply_modifiers;
-
-	/* perform the mesh extraction based on type */
 	switch (ob->type) {
 		case OB_FONT:
 		

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list