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, const UnitSettings *settings,
bool pad); 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. * 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); 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) double BKE_unit_value_scale(const UnitSettings *unit, const int unit_type, double value)
{ {
if (unit->system == USER_UNIT_NONE) { 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; v2 = ob->object_to_world().view<3, 3>() * v2;
} }
const size_t numstr_len = unit->system ? const size_t numstr_len =
BKE_unit_value_as_string(numstr, unit->system ?
sizeof(numstr), BKE_unit_value_as_string_scaled(
len_v3v3(v1, v2) * numstr, sizeof(numstr), len_v3v3(v1, v2), 3, B_UNIT_LENGTH, unit, false) :
unit->scale_length,
3,
B_UNIT_LENGTH,
unit,
false) :
SNPRINTF_RLEN(numstr, conv_float, len_v3v3(v1, v2)); 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); 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 *= 1.0f / float(n);
vmid = blender::math::transform_point(ob->object_to_world(), vmid); vmid = blender::math::transform_point(ob->object_to_world(), vmid);
const size_t numstr_len = unit->system ? const size_t numstr_len =
BKE_unit_value_as_string( unit->system ? BKE_unit_value_as_string_scaled(
numstr, numstr, sizeof(numstr), area, 3, B_UNIT_AREA, unit, false) :
sizeof(numstr),
double(area * unit->scale_length * unit->scale_length),
3,
B_UNIT_AREA,
unit,
false) :
SNPRINTF_RLEN(numstr, conv_float, area); SNPRINTF_RLEN(numstr, conv_float, area);
DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col); 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 { else {
double offset_val = double(RNA_float_get(op->ptr, "offset")); double offset_val = double(RNA_float_get(op->ptr, "offset"));
BKE_unit_value_as_string(offset_str, BKE_unit_value_as_string_scaled(
NUM_STR_REP_LEN, offset_str, NUM_STR_REP_LEN, offset_val, 3, B_UNIT_LENGTH, &sce->unit, true);
offset_val * sce->unit.scale_length,
3,
B_UNIT_LENGTH,
&sce->unit,
true);
} }
PropertyRNA *prop; 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); SNPRINTF(numstr, "%.*f", distance_precision, cut_len);
} }
else { else {
BKE_unit_value_as_string(numstr, BKE_unit_value_as_string_scaled(
sizeof(numstr), numstr, sizeof(numstr), cut_len, distance_precision, B_UNIT_LENGTH, unit, false);
double(cut_len * unit->scale_length),
distance_precision,
B_UNIT_LENGTH,
unit,
false);
} }
BLF_enable(blf_mono_font, BLF_ROTATION); 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; short strdrawlen = 0;
Scene *scene = CTX_data_scene(C); Scene *scene = CTX_data_scene(C);
const UnitSettings *unit = &scene->unit; const UnitSettings *unit = &scene->unit;
BKE_unit_value_as_string(str,
VOXEL_SIZE_EDIT_MAX_STR_LEN, BKE_unit_value_as_string_scaled(str, sizeof(str), cd->voxel_size, -3, B_UNIT_LENGTH, unit, true);
double(cd->voxel_size * unit->scale_length),
-3,
B_UNIT_LENGTH,
unit,
true);
strdrawlen = BLI_strlen_utf8(str); strdrawlen = BLI_strlen_utf8(str);
immUnbindProgram(); immUnbindProgram();

View file

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

View file

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

View file

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

View file

@ -1801,7 +1801,13 @@ typedef struct ToolSettings {
/** Display/Editing unit options for each scene. */ /** Display/Editing unit options for each scene. */
typedef struct UnitSettings { 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; float scale_length;
/** Imperial, metric etc. */ /** Imperial, metric etc. */
char system; char system;