[Bf-blender-cvs] [0aa2e4e] bake-cycles: Cycles-Bake: Saving the baked map externally working + more code

Dalai Felinto noreply at git.blender.org
Wed Apr 23 02:46:24 CEST 2014


Commit: 0aa2e4ecc18829ea24072ec97275200f8ec26281
Author: Dalai Felinto
Date:   Mon Jan 13 23:31:28 2014 -0200
https://developer.blender.org/rB0aa2e4ecc18829ea24072ec97275200f8ec26281

Cycles-Bake: Saving the baked map externally working + more code

How to test it:

bpy.ops.object.bake(type='UV', filepath='/tmp/bake.jpg', width=512,
height=512, is_save_external=True)

The object needs a UV for this to work.

*** Note:

Also we now separate whether we need to use the external baking or not.
Though, now that I think of it, we should probably always use external
when possible, even for UV. Because that's the only map that will make
sense that is in the 'always internal' group (object id and material id
doesn't make a lot of sense as baking options in my opinion).

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

M	source/blender/editors/object/object_bake_new.c
M	source/blender/makesdna/DNA_scene_types.h
M	source/blender/render/extern/include/RE_bake.h
M	source/blender/render/intern/source/bake_new.c
M	source/blender/render/intern/source/external_engine.c

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

diff --git a/source/blender/editors/object/object_bake_new.c b/source/blender/editors/object/object_bake_new.c
index d778996..525c0ff 100644
--- a/source/blender/editors/object/object_bake_new.c
+++ b/source/blender/editors/object/object_bake_new.c
@@ -121,8 +121,62 @@ static int bake_break(void *UNUSED(rjv))
 	return 0;
 }
 
+static bool bake_type_needs_external(Render *UNUSED(re), ScenePassType pass_type)
+{
+	if (ELEM3(pass_type, SCE_PASS_UV, SCE_PASS_INDEXOB, SCE_PASS_INDEXMA))
+		return false;
+	return true;
+}
+
+static bool write_external_bakepixels(const char *filepath, float *buffer, const int width, const int height, const int depth)
+{
+	ImBuf *ibuf = NULL;
+	short ok = FALSE;
+	unsigned char planes;
+
+	switch (depth) {
+		case 1:
+			planes = R_IMF_PLANES_BW;
+			break;
+		case 2:
+		case 3:
+			planes = R_IMF_PLANES_RGB;
+			break;
+		case 4:
+		default:
+			planes = R_IMF_PLANES_RGBA;
+			break;
+	}
+
+	/* create a new ImBuf */
+	ibuf = IMB_allocImBuf(width, height, planes, IB_rect);
+	if (!ibuf) return NULL;
+
+	/* populates the ImBuf */
+	IMB_buffer_byte_from_float((unsigned char *) ibuf->rect, buffer, ibuf->channels, ibuf->dither, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
+	                           FALSE, ibuf->x, ibuf->y, ibuf->x, ibuf->x);
+
+	/* setup the Imbuf*/
+	ibuf->ftype = JPG;
+
+	if ((ok=IMB_saveiff(ibuf, filepath, IB_rect))) {
+#ifndef WIN32
+		chmod(filepath, S_IRUSR | S_IWUSR);
+#endif
+		printf("%s saving bake map: '%s'\n", __func__, filepath);
+	}
+
+	/* garbage collection */
+	IMB_freeImBuf(ibuf);
+
+	if (ok) return true;
+	return false;
+}
+
 static int bake_exec(bContext *C, wmOperator *op)
 {
+	int op_result = OPERATOR_CANCELLED;
+	bool ok = false;
 	Scene *scene = CTX_data_scene(C);
 	Object *object = CTX_data_active_object(C);
 
@@ -132,10 +186,13 @@ static int bake_exec(bContext *C, wmOperator *op)
 
 	float *result;
 	BakePixel *pixel_array;
-	const int width = 64; //XXX get from elsewhere
-	const int height = 64; //XXX get from elsewhere
+	const int width = RNA_int_get(op->ptr, "width");
+	const int height = RNA_int_get(op->ptr, "height");
 	const int num_pixels = width * height;
-	const int depth = 1;
+	const int depth = RE_pass_depth(pass_type);
+	const bool is_external = RNA_boolean_get(op->ptr, "is_save_external");
+	char filepath[FILE_MAX];
+	RNA_string_get(op->ptr, "filepath", filepath);
 
 	RE_engine_bake_set_engine_parameters(re, CTX_data_main(C), scene);
 
@@ -147,16 +204,6 @@ static int bake_exec(bContext *C, wmOperator *op)
 	pixel_array = MEM_callocN(sizeof(BakePixel) * num_pixels, "bake pixels");
 	result = MEM_callocN(sizeof(float) * depth * num_pixels, "bake return pixels");
 
-#if 0
-	{
-		/* temporarily fill the result array with a normalized data */
-		int i;
-		for (i=0; i< num_pixels; i++) {
-			result[i] = (float)i / num_pixels;
-		}
-	}
-#endif
-
 	/* populate the pixel array with the face data */
 	RE_populate_bake_pixels(object, pixel_array, width, height);
 
@@ -216,23 +263,37 @@ static int bake_exec(bContext *C, wmOperator *op)
 	    e.g., do the image part? the cycle part? the blender internal changes? ...
 	 */
 
-	RE_engine_bake(re, object, pixel_array, num_pixels, depth, pass_type, result);
+	if (RE_engine_has_bake(re) && bake_type_needs_external(re, pass_type))
+		ok = RE_engine_bake(re, object, pixel_array, num_pixels, depth, pass_type, result);
+	else
+		ok = RE_internal_bake(re, object, pixel_array, num_pixels, depth, pass_type, result);
 
-#if 0
-	{
-		/* this is 90% likely working, but
-		    right now cycles is segfaulting on ~free, so
-		    we don't get as far as here */
-
-		int i = 0;
-		printf("RE_engine_bake output:\n");
-		printf("\n<result>\n\n");
-		for (i=0;i < num_pixels; i++) {
-			printf("%4.2f\n", result[i]);
+	if (!ok) {
+		BKE_report(op->reports, RPT_ERROR, "Problem baking object map");
+		op_result = OPERATOR_CANCELLED;
+	}
+	else {
+		/* save the result */
+		if (is_external) {
+			/* save it externally */
+			ok = write_external_bakepixels(filepath, result, width, height, depth);
+			if (!ok) {
+				char *error = NULL;
+				error = BLI_sprintfN("Problem saving baked map in \"%s\".", filepath);
+
+				BKE_report(op->reports, RPT_ERROR, error);
+				op_result = OPERATOR_CANCELLED;
+			}
+			else {
+				op_result = OPERATOR_FINISHED;
+			}
+		}
+		else {
+			/* save it internally */
+			BKE_report(op->reports, RPT_ERROR, "Only external baking supported at the moment");
+			op_result = OPERATOR_CANCELLED;
 		}
-		printf("\n</result>\n\n");
 	}
-#endif
 
 	MEM_freeN(pixel_array);
 	MEM_freeN(result);
@@ -295,7 +356,7 @@ static int bake_exec(bContext *C, wmOperator *op)
 	
 	return result;
 #endif
-	return OPERATOR_CANCELLED;
+	return op_result;
 }
 
 void OBJECT_OT_bake(wmOperatorType *ot)
@@ -312,4 +373,8 @@ void OBJECT_OT_bake(wmOperatorType *ot)
 
 	ot->prop = RNA_def_enum(ot->srna, "type", render_pass_type_items, SCE_PASS_COMBINED, "Type",
 	                        "Type of pass to bake, some of them may not be supported by the current render engine");
+	ot->prop = RNA_def_boolean(ot->srna, "is_save_external", true, "External", "Save the image externally (ignore face assigned Image datablocks)");
+	ot->prop = RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "Path", "Image filepath to use when saving externally");
+	ot->prop = RNA_def_int(ot->srna, "width", 512, 1, INT_MAX, "Width", "Horizontal dimension of the baking map", 64, 4096);
+	ot->prop = RNA_def_int(ot->srna, "height", 512, 1, INT_MAX, "Height", "Vertical dimension of the baking map", 64, 4096);
 }
diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h
index e36ce7d..34ca827 100644
--- a/source/blender/makesdna/DNA_scene_types.h
+++ b/source/blender/makesdna/DNA_scene_types.h
@@ -210,37 +210,39 @@ typedef struct SceneRenderLayer {
 #define SCE_LAY_NEG_ZMASK	0x80000
 
 /* srl->passflag */
-#define SCE_PASS_COMBINED				(1<<0)
-#define SCE_PASS_Z						(1<<1)
-#define SCE_PASS_RGBA					(1<<2)
-#define SCE_PASS_DIFFUSE				(1<<3)
-#define SCE_PASS_SPEC					(1<<4)
-#define SCE_PASS_SHADOW					(1<<5)
-#define SCE_PASS_AO						(1<<6)
-#define SCE_PASS_REFLECT				(1<<7)
-#define SCE_PASS_NORMAL					(1<<8)
-#define SCE_PASS_VECTOR					(1<<9)
-#define SCE_PASS_REFRACT				(1<<10)
-#define SCE_PASS_INDEXOB				(1<<11)
-#define SCE_PASS_UV						(1<<12)
-#define SCE_PASS_INDIRECT				(1<<13)
-#define SCE_PASS_MIST					(1<<14)
-#define SCE_PASS_RAYHITS				(1<<15)
-#define SCE_PASS_EMIT					(1<<16)
-#define SCE_PASS_ENVIRONMENT			(1<<17)
-#define SCE_PASS_INDEXMA				(1<<18)
-#define SCE_PASS_DIFFUSE_DIRECT			(1<<19)
-#define SCE_PASS_DIFFUSE_INDIRECT		(1<<20)
-#define SCE_PASS_DIFFUSE_COLOR			(1<<21)
-#define SCE_PASS_GLOSSY_DIRECT			(1<<22)
-#define SCE_PASS_GLOSSY_INDIRECT		(1<<23)
-#define SCE_PASS_GLOSSY_COLOR			(1<<24)
-#define SCE_PASS_TRANSM_DIRECT			(1<<25)
-#define SCE_PASS_TRANSM_INDIRECT		(1<<26)
-#define SCE_PASS_TRANSM_COLOR			(1<<27)
-#define SCE_PASS_SUBSURFACE_DIRECT		(1<<28)
-#define SCE_PASS_SUBSURFACE_INDIRECT	(1<<29)
-#define SCE_PASS_SUBSURFACE_COLOR		(1<<30)
+typedef enum ScenePassType{
+	SCE_PASS_COMBINED                 = (1<<0),
+	SCE_PASS_Z                        = (1<<1),
+	SCE_PASS_RGBA                     = (1<<2),
+	SCE_PASS_DIFFUSE                  = (1<<3),
+	SCE_PASS_SPEC                     = (1<<4),
+	SCE_PASS_SHADOW                   = (1<<5),
+	SCE_PASS_AO                       = (1<<6),
+	SCE_PASS_REFLECT                  = (1<<7),
+	SCE_PASS_NORMAL                   = (1<<8),
+	SCE_PASS_VECTOR                   = (1<<9),
+	SCE_PASS_REFRACT                  = (1<<10),
+	SCE_PASS_INDEXOB                  = (1<<11),
+	SCE_PASS_UV                       = (1<<12),
+	SCE_PASS_INDIRECT                 = (1<<13),
+	SCE_PASS_MIST                     = (1<<14),
+	SCE_PASS_RAYHITS                  = (1<<15),
+	SCE_PASS_EMIT                     = (1<<16),
+	SCE_PASS_ENVIRONMENT              = (1<<17),
+	SCE_PASS_INDEXMA                  = (1<<18),
+	SCE_PASS_DIFFUSE_DIRECT           = (1<<19),
+	SCE_PASS_DIFFUSE_INDIRECT         = (1<<20),
+	SCE_PASS_DIFFUSE_COLOR            = (1<<21),
+	SCE_PASS_GLOSSY_DIRECT            = (1<<22),
+	SCE_PASS_GLOSSY_INDIRECT          = (1<<23),
+	SCE_PASS_GLOSSY_COLOR             = (1<<24),
+	SCE_PASS_TRANSM_DIRECT            = (1<<25),
+	SCE_PASS_TRANSM_INDIRECT          = (1<<26),
+	SCE_PASS_TRANSM_COLOR             = (1<<27),
+	SCE_PASS_SUBSURFACE_DIRECT        = (1<<28),
+	SCE_PASS_SUBSURFACE_INDIRECT      = (1<<29),
+	SCE_PASS_SUBSURFACE_COLOR         = (1<<30),
+} ScenePassType;
 
 /* note, srl->passflag is treestore element 'nr' in outliner, short still... */
 
diff --git a/source/blender/render/extern/include/RE_bake.h b/source/blender/render/extern/include/RE_bake.h
index 52c0805..f2bf179 100644
--- a/source/blender/render/extern/include/RE_bake.h
+++ b/source/blender/render/extern/include/RE_bake.h
@@ -42,10 +42,13 @@ typedef struct BakePixel {
 } BakePixel;
 
 /* external_engine.c */
-int RE_engine_bake(struct Render *re, struct Object *object, struct BakePixel pixel_array[], int num_pixels, int depth, int pass_type, float result[]);
+bool RE_engine_has_bake(struct Render *re);
+
+bool RE_engine_bake(struct Render *re, struct Object *object, struct BakePixel pixel_array[], int num_pixels, int depth, ScenePassType pass_type, float result[]);
 
 /* bake.c */
-int RE_internal_bake(struct Render *re, struct Object *object, struct BakePixel pixel_array[], int num_pixels, int depth, int pass_type, float result[]);
+int RE_pass_depth(ScenePassType pass_type);
+bool RE_internal_bake(struct Render *re, struct Object *object, struct BakePixel pixel_array[], int num_pixels, int depth, ScenePassType pass_type, float result[]);
 
 void RE_populate_bake_pixels(struct Object *object, struct BakePixel pixel_array[], const int width, const int height);
 
diff --git a/source/blender/render/intern/source/bake_new.c b/source/blender/render/intern/source/bake_new.c
index 8dc4e50..1920521 100644
--- a/source/blender/render/intern/source/bake_new.c
+++ b/source/blender/render/intern/source/bake_new.c
@@ -165,7 +165,102 @@ void RE_populate_bake_pixels(Object *object, BakePixel pixel_array[], const int
 	zbuf_free

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list