Anim: Implement RNA function on the FCurve to bake keys #120783

Merged
Christoph Lendenfeld merged 3 commits from ChrisLend/blender:fcurve_bake_pyapi into main 2024-04-23 13:43:11 +02:00
1 changed files with 72 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include "DNA_anim_types.h"
#include "DNA_scene_types.h"
#include "ED_keyframes_edit.hh"
#include "rna_internal.hh" /* own include */
@ -64,8 +65,45 @@ static void rna_FCurve_convert_to_keyframes(FCurve *fcu, ReportList *reports, in
}
}
static void rna_FCurve_bake(FCurve *fcu,
ReportList *reports,
int start_frame,
int end_frame,
float step,
int remove_existing_as_int)
{
if (start_frame >= end_frame) {
BKE_reportf(reports,

Explain why this frame range is invalid, so that the recipient of this message knows how to fix it.

Explain why this frame range is invalid, so that the recipient of this message knows how to fix it.
RPT_ERROR,
"Invalid frame range (%d - %d). End Frame is larger than Start Frame",
start_frame,
end_frame);

I think it's better to make the naming of remove_existing and remove_option more similar. The name BakeCurveRemove is already not screaming "I'm an enum" at you, so I found the code a bit confusing until I realised that this is just a cast from the int to the enum type.

Maybe something like remove_option_int / remove_option, or remove_existing_as_int / remove_existing ?

I think it's better to make the naming of `remove_existing` and `remove_option` more similar. The name `BakeCurveRemove` is already not screaming "I'm an enum" at you, so I found the code a bit confusing until I realised that this is just a cast from the `int` to the enum type. Maybe something like `remove_option_int` / `remove_option`, or `remove_existing_as_int` / `remove_existing` ?
return;
}
const BakeCurveRemove remove_existing = BakeCurveRemove(remove_existing_as_int);
bake_fcurve(fcu, {start_frame, end_frame}, step, remove_existing);
WM_main_add_notifier(NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, nullptr);
}
#else
static const EnumPropertyItem channel_bake_remove_options[] = {
{int(BakeCurveRemove::REMOVE_NONE), "NONE", 0, "None", "Keep all keys"},
{int(BakeCurveRemove::REMOVE_IN_RANGE),
"IN_RANGE",
0,
"In Range",
"Remove all keys within the defined range"},
{int(BakeCurveRemove::REMOVE_OUT_RANGE),
"OUT_RANGE",
0,
"Outside Range",
"Remove all keys outside the defined range"},
{int(BakeCurveRemove::REMOVE_ALL), "ALL", 0, "All", "Remove all existing keys"},
{0, nullptr, 0, nullptr, nullptr},
};
void RNA_api_fcurves(StructRNA *srna)
{
FunctionRNA *func;
@ -92,6 +130,40 @@ void RNA_api_fcurves(StructRNA *srna)
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_int(func, "end", 0, MINAFRAME, MAXFRAME, "End Frame", "", MINAFRAME, MAXFRAME);
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
func = RNA_def_function(srna, "bake", "rna_FCurve_bake");
RNA_def_function_ui_description(func, "Place keys at even intervals on the existing curve.");
RNA_def_function_flag(func, FUNC_USE_REPORTS);
parm = RNA_def_int(func,
"start",
0,
MINAFRAME,
MAXFRAME,
"Start Frame",
"Frame at which to start baking",
MINAFRAME,
MAXFRAME);
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_int(func,
"end",
0,
MINAFRAME,
MAXFRAME,
"End Frame",
"Frame at which to end baking (inclusive)",
MINAFRAME,
MAXFRAME);
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
parm = RNA_def_float(
func, "step", 1, 0.01, FLT_MAX, "Step", "At which interval to add keys", 1, 16);
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_PYFUNC_OPTIONAL);
RNA_def_enum(func,
"remove",
channel_bake_remove_options,
int(BakeCurveRemove::REMOVE_IN_RANGE),
"Remove Options",
"Choose which keys should be automatically removed by the bake");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_PYFUNC_OPTIONAL);
}
void RNA_api_drivers(StructRNA * /*srna*/)