Fix unit scale use when when units were disabled

UnitSettings::scale_length was used to scale reported
values for operators (translate, shrink-fatten, voxel-size).

This isn't expected as the value isn't editable when the unit-system
is set to None.

Add BKE_unit_value_as_string_scaled utility function for clarity &
to ensure scaling is only applied when it should be.
This commit is contained in:
Campbell Barton 2025-01-08 21:07:23 +11:00
parent 361e98b09e
commit e949ff7334
10 changed files with 51 additions and 55 deletions

View file

@ -37,6 +37,17 @@ size_t BKE_unit_value_as_string(char *str,
const UnitSettings *settings,
bool pad);
/**
* A version of #BKE_unit_value_as_string with the `value` scaled by #BKE_unit_value_scale.
*/
size_t BKE_unit_value_as_string_scaled(char *str,
int str_maxncpy,
double value,
int prec,
int type,
const UnitSettings *settings,
bool pad);
/**
* Replace units with values, used before python button evaluation.
*

View file

@ -1886,6 +1886,18 @@ size_t BKE_unit_value_as_string(char *str,
return unit_as_string_main(str, str_maxncpy, value, prec, type, do_split, pad, units);
}
size_t BKE_unit_value_as_string_scaled(char *str,
int str_maxncpy,
double value,
int prec,
int type,
const UnitSettings *settings,
bool pad)
{
return BKE_unit_value_as_string(
str, str_maxncpy, BKE_unit_value_scale(settings, type, value), prec, type, settings, pad);
}
double BKE_unit_value_scale(const UnitSettings *unit, const int unit_type, double value)
{
if (unit->system == USER_UNIT_NONE) {

View file

@ -362,15 +362,10 @@ void DRW_text_edit_mesh_measure_stats(const ARegion *region,
v2 = ob->object_to_world().view<3, 3>() * v2;
}
const size_t numstr_len = unit->system ?
BKE_unit_value_as_string(numstr,
sizeof(numstr),
len_v3v3(v1, v2) *
unit->scale_length,
3,
B_UNIT_LENGTH,
unit,
false) :
const size_t numstr_len =
unit->system ?
BKE_unit_value_as_string_scaled(
numstr, sizeof(numstr), len_v3v3(v1, v2), 3, B_UNIT_LENGTH, unit, false) :
SNPRINTF_RLEN(numstr, conv_float, len_v3v3(v1, v2));
DRW_text_cache_add(dt, co, numstr, numstr_len, 0, edge_tex_sep, txt_flag, col);
@ -502,15 +497,9 @@ void DRW_text_edit_mesh_measure_stats(const ARegion *region,
vmid *= 1.0f / float(n);
vmid = blender::math::transform_point(ob->object_to_world(), vmid);
const size_t numstr_len = unit->system ?
BKE_unit_value_as_string(
numstr,
sizeof(numstr),
double(area * unit->scale_length * unit->scale_length),
3,
B_UNIT_AREA,
unit,
false) :
const size_t numstr_len =
unit->system ? BKE_unit_value_as_string_scaled(
numstr, sizeof(numstr), area, 3, B_UNIT_AREA, unit, false) :
SNPRINTF_RLEN(numstr, conv_float, area);
DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col);

View file

@ -133,13 +133,8 @@ static void edbm_bevel_update_status_text(bContext *C, wmOperator *op)
}
else {
double offset_val = double(RNA_float_get(op->ptr, "offset"));
BKE_unit_value_as_string(offset_str,
NUM_STR_REP_LEN,
offset_val * sce->unit.scale_length,
3,
B_UNIT_LENGTH,
&sce->unit,
true);
BKE_unit_value_as_string_scaled(
offset_str, NUM_STR_REP_LEN, offset_val, 3, B_UNIT_LENGTH, &sce->unit, true);
}
PropertyRNA *prop;

View file

@ -514,13 +514,8 @@ static void knifetool_draw_visible_distances(const KnifeTool_OpData *kcd)
SNPRINTF(numstr, "%.*f", distance_precision, cut_len);
}
else {
BKE_unit_value_as_string(numstr,
sizeof(numstr),
double(cut_len * unit->scale_length),
distance_precision,
B_UNIT_LENGTH,
unit,
false);
BKE_unit_value_as_string_scaled(
numstr, sizeof(numstr), cut_len, distance_precision, B_UNIT_LENGTH, unit, false);
}
BLF_enable(blf_mono_font, BLF_ROTATION);

View file

@ -320,13 +320,8 @@ static void voxel_size_edit_draw(const bContext *C, ARegion * /*region*/, void *
short strdrawlen = 0;
Scene *scene = CTX_data_scene(C);
const UnitSettings *unit = &scene->unit;
BKE_unit_value_as_string(str,
VOXEL_SIZE_EDIT_MAX_STR_LEN,
double(cd->voxel_size * unit->scale_length),
-3,
B_UNIT_LENGTH,
unit,
true);
BKE_unit_value_as_string_scaled(str, sizeof(str), cd->voxel_size, -3, B_UNIT_LENGTH, unit, true);
strdrawlen = BLI_strlen_utf8(str);
immUnbindProgram();

View file

@ -215,13 +215,7 @@ static void ruler_item_as_string(
BLI_snprintf(numstr, numstr_size, "%.*f", prec, ruler_len);
}
else {
BKE_unit_value_as_string(numstr,
numstr_size,
double(ruler_len * unit->scale_length),
prec,
B_UNIT_LENGTH,
unit,
false);
BKE_unit_value_as_string(numstr, numstr_size, ruler_len, prec, B_UNIT_LENGTH, unit, false);
}
}
}

View file

@ -114,10 +114,10 @@ static void applyShrinkFatten(TransInfo *t)
}
else {
/* Default header print. */
if (unit != nullptr) {
if (unit->system != USER_UNIT_NONE) {
char unit_str[64];
BKE_unit_value_as_string(
unit_str, sizeof(unit_str), distance * unit->scale_length, 4, B_UNIT_LENGTH, unit, true);
BKE_unit_value_as_string_scaled(
unit_str, sizeof(unit_str), distance, 4, B_UNIT_LENGTH, unit, true);
fmt::format_to(fmt::appender(str), "{}", unit_str);
}
else {

View file

@ -187,9 +187,8 @@ static void translate_dist_to_str(char *r_str,
const float val,
const UnitSettings *unit)
{
if (unit) {
BKE_unit_value_as_string(
r_str, r_str_maxncpy, val * unit->scale_length, 4, B_UNIT_LENGTH, unit, false);
if (unit && (unit->system != USER_UNIT_NONE)) {
BKE_unit_value_as_string_scaled(r_str, r_str_maxncpy, val, 4, B_UNIT_LENGTH, unit, false);
}
else {
/* Check range to prevent string buffer overflow. */

View file

@ -1801,7 +1801,13 @@ typedef struct ToolSettings {
/** Display/Editing unit options for each scene. */
typedef struct UnitSettings {
/** Maybe have other unit conversions? */
/* Maybe have other unit conversions? */
/**
* Spatial scale.
* - This must not be used when `system == USER_UNIT_NONE`.
* - Typically the scale should be applied using #BKE_unit_value_scale
* which supports different kinds of users and checks a none unit system.
*/
float scale_length;
/** Imperial, metric etc. */
char system;