[Bf-blender-cvs] [da8f16e] master: FCurve RNA API: add funcs to convert to samples/to keyframes.

Bastien Montagne noreply at git.blender.org
Fri Jan 16 17:28:47 CET 2015


Commit: da8f16e288d667e8b8c487b3192f962d71dec3aa
Author: Bastien Montagne
Date:   Fri Jan 16 16:12:03 2015 +0100
Branches: master
https://developer.blender.org/rBda8f16e288d667e8b8c487b3192f962d71dec3aa

FCurve RNA API: add funcs to convert to samples/to keyframes.

So far, we had an operator to 'bake' keyframe curves into samples, but no
way to make the fcurve editable again (i.e. to convert it back into a keyframes one).

Needed to fix mocap addon (see T43259).

Also, fixed a glitch in `fcurve_store_samples()`, since given end frame is included in range,
it is valid to give same start and end frame (in case you want a single point in samples,
not much practical cases, but...).

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

M	source/blender/blenkernel/intern/fcurve.c
M	source/blender/makesrna/intern/rna_fcurve.c
M	source/blender/makesrna/intern/rna_fcurve_api.c
M	source/blender/makesrna/intern/rna_internal.h

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

diff --git a/source/blender/blenkernel/intern/fcurve.c b/source/blender/blenkernel/intern/fcurve.c
index b50e563..4648d70 100644
--- a/source/blender/blenkernel/intern/fcurve.c
+++ b/source/blender/blenkernel/intern/fcurve.c
@@ -820,7 +820,7 @@ void fcurve_store_samples(FCurve *fcu, void *data, int start, int end, FcuSample
 		printf("Error: No F-Curve with F-Curve Modifiers to Bake\n");
 		return;
 	}
-	if (start >= end) {
+	if (start > end) {
 		printf("Error: Frame range for Sampled F-Curve creation is inappropriate\n");
 		return;
 	}
diff --git a/source/blender/makesrna/intern/rna_fcurve.c b/source/blender/makesrna/intern/rna_fcurve.c
index 3b90781..6c89325 100644
--- a/source/blender/makesrna/intern/rna_fcurve.c
+++ b/source/blender/makesrna/intern/rna_fcurve.c
@@ -1948,6 +1948,10 @@ static void rna_def_fcurve(BlenderRNA *brna)
 	parm = RNA_def_pointer(func, "data", "AnyType", "Data",
 	                       "Data containing the property controlled by given FCurve");
 	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_RNAPTR | PROP_NEVER_NULL);
+
+
+	/* Functions */
+	RNA_api_fcurves(srna);
 }
 
 /* *********************** */
diff --git a/source/blender/makesrna/intern/rna_fcurve_api.c b/source/blender/makesrna/intern/rna_fcurve_api.c
index ab96f6f..8551ca6 100644
--- a/source/blender/makesrna/intern/rna_fcurve_api.c
+++ b/source/blender/makesrna/intern/rna_fcurve_api.c
@@ -39,6 +39,7 @@
 #include "RNA_define.h"
 
 #include "DNA_anim_types.h"
+#include "DNA_scene_types.h"
 
 #include "rna_internal.h"  /* own include */
 
@@ -49,8 +50,115 @@
 #include "BKE_animsys.h"
 #include "BKE_fcurve.h"
 
+#include "BLI_math.h"
+
+static void rna_FCurve_convert_to_samples(FCurve *fcu, ReportList *reports, int start, int end)
+{
+	/* XXX fcurve_store_samples uses end frame included, which is not consistent with usual behavior in Blender,
+	 * nor python slices, etc. Let have public py API be consistent here at least. */
+	end--;
+	if (start > end) {
+		BKE_reportf(reports, RPT_ERROR, "Invalid frame range (%d - %d)", start, end + 1);
+	}
+	else if (fcu->fpt) {
+		BKE_report(reports, RPT_WARNING, "FCurve has already sample points");
+	}
+	else if (!fcu->bezt) {
+		BKE_report(reports, RPT_WARNING, "FCurve has no keyframes");
+	}
+	else {
+		fcurve_store_samples(fcu, NULL, start, end, fcurve_samplingcb_evalcurve);
+		WM_main_add_notifier(NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
+	}
+}
+
+static void rna_FCurve_convert_to_keyframes(FCurve *fcu, ReportList *reports, int start, int end)
+{
+	if (start >= end) {
+		BKE_reportf(reports, RPT_ERROR, "Invalid frame range (%d - %d)", start, end);
+	}
+	else if (fcu->bezt) {
+		BKE_report(reports, RPT_WARNING, "FCurve has already keyframes");
+	}
+	else if (!fcu->fpt) {
+		BKE_report(reports, RPT_WARNING, "FCurve has no sample points");
+	}
+	else {
+		BezTriple *bezt;
+		FPoint *fpt = fcu->fpt;
+		int tot_kf = end - start;
+		int tot_sp = fcu->totvert;
+
+		bezt = fcu->bezt = MEM_callocN(sizeof(*fcu->bezt) * (size_t)tot_kf, __func__);
+		fcu->totvert = tot_kf;
+
+		/* Get first sample point to 'copy' as keyframe. */
+		for (; tot_sp && (fpt->vec[0] < (float)start); fpt++, tot_sp--);
+
+		/* Add heading dummy flat points if needed. */
+		for (; tot_kf && (fpt->vec[0] > (float)start); start++, bezt++, tot_kf--) {
+			/* Linear interpolation, of course. */
+			bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
+			bezt->ipo = BEZT_IPO_LIN;
+			bezt->h1 = bezt->h2 = HD_AUTO_ANIM;
+			bezt->vec[1][0] = (float)start;
+			bezt->vec[1][1] = fpt->vec[1];
+		}
+
+		/* Copy actual sample points. */
+		for (; tot_kf && tot_sp; start++, bezt++, tot_kf--, fpt++, tot_sp--) {
+			/* Linear interpolation, of course. */
+			bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
+			bezt->ipo = BEZT_IPO_LIN;
+			bezt->h1 = bezt->h2 = HD_AUTO_ANIM;
+			copy_v2_v2(bezt->vec[1], fpt->vec);
+		}
+
+		/* Add leading dummy flat points if needed. */
+		for (fpt--; tot_kf; start++, bezt++, tot_kf--) {
+			/* Linear interpolation, of course. */
+			bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
+			bezt->ipo = BEZT_IPO_LIN;
+			bezt->h1 = bezt->h2 = HD_AUTO_ANIM;
+			bezt->vec[1][0] = (float)start;
+			bezt->vec[1][1] = fpt->vec[1];
+		}
+
+		MEM_SAFE_FREE(fcu->fpt);
+
+		/* Not strictly needed since we use linear interpolation, but better be consistent here. */
+		calchandles_fcurve(fcu);
+		WM_main_add_notifier(NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
+	}
+}
+
 #else
 
+void RNA_api_fcurves(StructRNA *srna)
+{
+	FunctionRNA *func;
+	PropertyRNA *parm;
+
+	func = RNA_def_function(srna, "convert_to_samples", "rna_FCurve_convert_to_samples");
+	RNA_def_function_ui_description(func,
+	                                "Convert current FCurve from keyframes to sample points, if necessary");
+	RNA_def_function_flag(func, FUNC_USE_REPORTS);
+	parm = RNA_def_int(func, "start", 0, MINAFRAME, MAXFRAME, "Start Frame", "", MINAFRAME, MAXFRAME);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	parm = RNA_def_int(func, "end", 0, MINAFRAME, MAXFRAME, "End Frame", "", MINAFRAME, MAXFRAME);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+
+	func = RNA_def_function(srna, "convert_to_keyframes", "rna_FCurve_convert_to_keyframes");
+	RNA_def_function_ui_description(func,
+	                                "Convert current FCurve from sample points to keyframes (linear interpolation), "
+	                                "if necessary");
+	RNA_def_function_flag(func, FUNC_USE_REPORTS);
+	parm = RNA_def_int(func, "start", 0, MINAFRAME, MAXFRAME, "Start Frame", "", MINAFRAME, MAXFRAME);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	parm = RNA_def_int(func, "end", 0, MINAFRAME, MAXFRAME, "End Frame", "", MINAFRAME, MAXFRAME);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+}
+
 void RNA_api_drivers(StructRNA *UNUSED(srna))
 {
 /*	FunctionRNA *func; */
diff --git a/source/blender/makesrna/intern/rna_internal.h b/source/blender/makesrna/intern/rna_internal.h
index 64a50d4..bd1d969 100644
--- a/source/blender/makesrna/intern/rna_internal.h
+++ b/source/blender/makesrna/intern/rna_internal.h
@@ -259,6 +259,7 @@ void RNA_api_armature_edit_bone(StructRNA *srna);
 void RNA_api_bone(StructRNA *srna);
 void RNA_api_camera(StructRNA *srna);
 void RNA_api_curve(StructRNA *srna);
+void RNA_api_fcurves(StructRNA *srna);
 void RNA_api_drivers(StructRNA *srna);
 void RNA_api_image(struct StructRNA *srna);
 void RNA_api_lattice(struct StructRNA *srna);




More information about the Bf-blender-cvs mailing list