Python/CurveMapping: add Curve Mapping functions to add/remove curve points,

evaluate the curve and update after changes.
This commit is contained in:
Brecht Van Lommel 2012-05-15 18:34:00 +00:00
parent 37552ac167
commit 7aa21d677a
3 changed files with 93 additions and 13 deletions

@ -33,6 +33,7 @@
struct CurveMapping; struct CurveMapping;
struct CurveMap; struct CurveMap;
struct CurveMapPoint;
struct Scopes; struct Scopes;
struct ImBuf; struct ImBuf;
struct rctf; struct rctf;
@ -52,12 +53,14 @@ void curvemapping_set_black_white(struct CurveMapping *cumap, con
#define CURVEMAP_SLOPE_NEGATIVE 0 #define CURVEMAP_SLOPE_NEGATIVE 0
#define CURVEMAP_SLOPE_POSITIVE 1 #define CURVEMAP_SLOPE_POSITIVE 1
void curvemap_reset(struct CurveMap *cuma, struct rctf *clipr, int preset, int slope); void curvemap_reset(struct CurveMap *cuma, struct rctf *clipr, int preset, int slope);
void curvemap_remove(struct CurveMap *cuma, int flag); void curvemap_remove(struct CurveMap *cuma, int flag);
void curvemap_insert(struct CurveMap *cuma, float x, float y); void curvemap_remove_point(struct CurveMap *cuma, struct CurveMapPoint *cmp);
void curvemap_sethandle(struct CurveMap *cuma, int type); struct CurveMapPoint *curvemap_insert(struct CurveMap *cuma, float x, float y);
void curvemap_sethandle(struct CurveMap *cuma, int type);
void curvemapping_changed(struct CurveMapping *cumap, int rem_doubles); void curvemapping_changed(struct CurveMapping *cumap, int rem_doubles);
void curvemapping_changed_all(struct CurveMapping *cumap);
/* single curve, no table check */ /* single curve, no table check */
float curvemap_evaluateF(struct CurveMap *cuma, float value); float curvemap_evaluateF(struct CurveMap *cuma, float value);

@ -146,6 +146,32 @@ void curvemapping_set_black_white(CurveMapping *cumap, const float black[3], con
/* ***************** operations on single curve ************* */ /* ***************** operations on single curve ************* */
/* ********** NOTE: requires curvemapping_changed() call after ******** */ /* ********** NOTE: requires curvemapping_changed() call after ******** */
/* remove specified point */
void curvemap_remove_point(CurveMap *cuma, CurveMapPoint *point)
{
CurveMapPoint *cmp;
int a, b, removed = 0;
/* must have 2 points minimum */
if (cuma->totpoint <= 2)
return;
cmp = MEM_mallocN((cuma->totpoint) * sizeof(CurveMapPoint), "curve points");
/* well, lets keep the two outer points! */
for (a = 0, b = 0; a < cuma->totpoint; a++) {
if (&cuma->curve[a] != point) {
cmp[b] = cuma->curve[a];
b++;
}
else removed++;
}
MEM_freeN(cuma->curve);
cuma->curve = cmp;
cuma->totpoint -= removed;
}
/* removes with flag set */ /* removes with flag set */
void curvemap_remove(CurveMap *cuma, int flag) void curvemap_remove(CurveMap *cuma, int flag)
{ {
@ -168,9 +194,10 @@ void curvemap_remove(CurveMap *cuma, int flag)
cuma->totpoint -= removed; cuma->totpoint -= removed;
} }
void curvemap_insert(CurveMap *cuma, float x, float y) CurveMapPoint *curvemap_insert(CurveMap *cuma, float x, float y)
{ {
CurveMapPoint *cmp = MEM_callocN((cuma->totpoint + 1) * sizeof(CurveMapPoint), "curve points"); CurveMapPoint *cmp = MEM_callocN((cuma->totpoint + 1) * sizeof(CurveMapPoint), "curve points");
CurveMapPoint *newcmp = NULL;
int a, b, foundloc = 0; int a, b, foundloc = 0;
/* insert fragments of the old one and the new point to the new curve */ /* insert fragments of the old one and the new point to the new curve */
@ -181,6 +208,7 @@ void curvemap_insert(CurveMap *cuma, float x, float y)
cmp[a].y = y; cmp[a].y = y;
cmp[a].flag = CUMA_SELECT; cmp[a].flag = CUMA_SELECT;
foundloc = 1; foundloc = 1;
newcmp = &cmp[a];
} }
else { else {
cmp[a].x = cuma->curve[b].x; cmp[a].x = cuma->curve[b].x;
@ -195,6 +223,8 @@ void curvemap_insert(CurveMap *cuma, float x, float y)
/* free old curve and replace it with new one */ /* free old curve and replace it with new one */
MEM_freeN(cuma->curve); MEM_freeN(cuma->curve);
cuma->curve = cmp; cuma->curve = cmp;
return newcmp;
} }
void curvemap_reset(CurveMap *cuma, rctf *clipr, int preset, int slope) void curvemap_reset(CurveMap *cuma, rctf *clipr, int preset, int slope)
@ -670,6 +700,20 @@ void curvemapping_changed(CurveMapping *cumap, int rem_doubles)
curvemap_make_table(cuma, clipr); curvemap_make_table(cuma, clipr);
} }
void curvemapping_changed_all(CurveMapping *cumap)
{
int a, cur = cumap->cur;
for (a = 0; a < CM_TOT; a++) {
if (cumap->cm[a].curve) {
cumap->cur = a;
curvemapping_changed(cumap, 0);
}
}
cumap->cur = cur;
}
/* table should be verified */ /* table should be verified */
float curvemap_evaluateF(CurveMap *cuma, float value) float curvemap_evaluateF(CurveMap *cuma, float value)
{ {

@ -320,18 +320,14 @@ static void rna_def_curvemappoint(BlenderRNA *brna)
srna = RNA_def_struct(brna, "CurveMapPoint", NULL); srna = RNA_def_struct(brna, "CurveMapPoint", NULL);
RNA_def_struct_ui_text(srna, "CurveMapPoint", "Point of a curve used for a curve mapping"); RNA_def_struct_ui_text(srna, "CurveMapPoint", "Point of a curve used for a curve mapping");
/* not editable for now, need to have CurveMapping to do curvemapping_changed */
prop = RNA_def_property(srna, "location", PROP_FLOAT, PROP_XYZ); prop = RNA_def_property(srna, "location", PROP_FLOAT, PROP_XYZ);
RNA_def_property_float_sdna(prop, NULL, "x"); RNA_def_property_float_sdna(prop, NULL, "x");
RNA_def_property_array(prop, 2); RNA_def_property_array(prop, 2);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Location", "X/Y coordinates of the curve point"); RNA_def_property_ui_text(prop, "Location", "X/Y coordinates of the curve point");
prop = RNA_def_property(srna, "handle_type", PROP_ENUM, PROP_NONE); prop = RNA_def_property(srna, "handle_type", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "flag"); RNA_def_property_enum_bitflag_sdna(prop, NULL, "flag");
RNA_def_property_enum_items(prop, prop_handle_type_items); RNA_def_property_enum_items(prop, prop_handle_type_items);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Handle Type", "Curve interpolation at this point: Bezier or vector"); RNA_def_property_ui_text(prop, "Handle Type", "Curve interpolation at this point: Bezier or vector");
prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE); prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
@ -339,10 +335,38 @@ static void rna_def_curvemappoint(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Select", "Selection state of the curve point"); RNA_def_property_ui_text(prop, "Select", "Selection state of the curve point");
} }
static void rna_def_curvemap_points_api(BlenderRNA *brna, PropertyRNA *cprop)
{
StructRNA *srna;
PropertyRNA *parm;
FunctionRNA *func;
RNA_def_property_srna(cprop, "CurveMapPoints");
srna = RNA_def_struct(brna, "CurveMapPoints", NULL);
RNA_def_struct_sdna(srna, "CurveMap");
RNA_def_struct_ui_text(srna, "Curve Map Point", "Collection of Curve Map Points");
func = RNA_def_function(srna, "new", "curvemap_insert");
RNA_def_function_ui_description(func, "Add point to CurveMap");
parm = RNA_def_float(func, "position", 0.0f, -FLT_MAX, FLT_MAX, "Position", "Position to add point", -FLT_MAX, FLT_MAX);
RNA_def_property_flag(parm, PROP_REQUIRED);
parm = RNA_def_float(func, "value", 0.0f, -FLT_MAX, FLT_MAX, "Value", "Value of point", -FLT_MAX, FLT_MAX);
RNA_def_property_flag(parm, PROP_REQUIRED);
parm = RNA_def_pointer(func, "point", "CurveMapPoint", "", "New point");
RNA_def_function_return(func, parm);
func = RNA_def_function(srna, "remove", "curvemap_remove_point");
RNA_def_function_ui_description(func, "Delete point from CurveMap");
parm = RNA_def_pointer(func, "point", "CurveMapPoint", "", "PointElement to remove");
RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
}
static void rna_def_curvemap(BlenderRNA *brna) static void rna_def_curvemap(BlenderRNA *brna)
{ {
StructRNA *srna; StructRNA *srna;
PropertyRNA *prop; PropertyRNA *prop, *parm;
FunctionRNA *func;
static EnumPropertyItem prop_extend_items[] = { static EnumPropertyItem prop_extend_items[] = {
{0, "HORIZONTAL", 0, "Horizontal", ""}, {0, "HORIZONTAL", 0, "Horizontal", ""},
{CUMA_EXTEND_EXTRAPOLATE, "EXTRAPOLATED", 0, "Extrapolated", ""}, {CUMA_EXTEND_EXTRAPOLATE, "EXTRAPOLATED", 0, "Extrapolated", ""},
@ -352,24 +376,30 @@ static void rna_def_curvemap(BlenderRNA *brna)
srna = RNA_def_struct(brna, "CurveMap", NULL); srna = RNA_def_struct(brna, "CurveMap", NULL);
RNA_def_struct_ui_text(srna, "CurveMap", "Curve in a curve mapping"); RNA_def_struct_ui_text(srna, "CurveMap", "Curve in a curve mapping");
/* not editable for now, need to have CurveMapping to do curvemapping_changed */
prop = RNA_def_property(srna, "extend", PROP_ENUM, PROP_NONE); prop = RNA_def_property(srna, "extend", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_bitflag_sdna(prop, NULL, "flag"); RNA_def_property_enum_bitflag_sdna(prop, NULL, "flag");
RNA_def_property_enum_items(prop, prop_extend_items); RNA_def_property_enum_items(prop, prop_extend_items);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "Extend", "Extrapolate the curve or extend it horizontally"); RNA_def_property_ui_text(prop, "Extend", "Extrapolate the curve or extend it horizontally");
prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE); prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
RNA_def_property_collection_sdna(prop, NULL, "curve", "totpoint"); RNA_def_property_collection_sdna(prop, NULL, "curve", "totpoint");
RNA_def_property_struct_type(prop, "CurveMapPoint"); RNA_def_property_struct_type(prop, "CurveMapPoint");
RNA_def_property_ui_text(prop, "Points", ""); RNA_def_property_ui_text(prop, "Points", "");
rna_def_curvemap_points_api(brna, prop);
func = RNA_def_function(srna, "evaluate", "curvemap_evaluateF");
RNA_def_function_ui_description(func, "Evaluate curve at given location");
parm = RNA_def_float(func, "position", 0.0f, -FLT_MAX, FLT_MAX, "Position", "Position to evaluate curve at", -FLT_MAX, FLT_MAX);
RNA_def_property_flag(parm, PROP_REQUIRED);
parm = RNA_def_float(func, "value", 0.0f, -FLT_MAX, FLT_MAX, "Value", "Value of curve at given location", -FLT_MAX, FLT_MAX);
RNA_def_function_return(func, parm);
} }
static void rna_def_curvemapping(BlenderRNA *brna) static void rna_def_curvemapping(BlenderRNA *brna)
{ {
StructRNA *srna; StructRNA *srna;
PropertyRNA *prop; PropertyRNA *prop;
FunctionRNA *func;
srna = RNA_def_struct(brna, "CurveMapping", NULL); srna = RNA_def_struct(brna, "CurveMapping", NULL);
RNA_def_struct_ui_text(srna, "CurveMapping", RNA_def_struct_ui_text(srna, "CurveMapping",
@ -423,6 +453,9 @@ static void rna_def_curvemapping(BlenderRNA *brna)
RNA_def_property_range(prop, -1000.0f, 1000.0f); RNA_def_property_range(prop, -1000.0f, 1000.0f);
RNA_def_property_ui_text(prop, "White Level", "For RGB curves, the color that white is mapped to"); RNA_def_property_ui_text(prop, "White Level", "For RGB curves, the color that white is mapped to");
RNA_def_property_float_funcs(prop, NULL, "rna_CurveMapping_white_level_set", NULL); RNA_def_property_float_funcs(prop, NULL, "rna_CurveMapping_white_level_set", NULL);
func = RNA_def_function(srna, "update", "curvemapping_changed_all");
RNA_def_function_ui_description(func, "Update curve mapping after making changes");
} }
static void rna_def_color_ramp_element(BlenderRNA *brna) static void rna_def_color_ramp_element(BlenderRNA *brna)