← All ideas
  • Published 9 Oct 2018

Unit system and consistent defaults values in python.

Addon devs using python to define objects size always are facing with international units issues.

Expecting a single default value to generate same result whatever the unit system choosen. As it, it is not the case, but fortunately there is a simple solution to handle this and fix all those issues at once.

When going from/to internal <-> ui, unit transforms occurs this way:

  • Ui -> unit conversion -> size in meter / unit scale = internal
  • Internal * unit scale = size in meter -> unit conversion -> Ui

If we apply same rule to defaults, an object with a default set to 1 will then be scaled by choosen unit, eg: 0.3048 meter when unit is imperial feet.

Handling default values properly:

Unit conversion must not occur on default values !
We must only divide default by unit scale and always define default values as meter.
This way objects size will remains consistent and correct whatever the unit system is.

  • default -> size in meter / unit scale = Internal.

So a cube with 1 (meter) as default will remains the same cube when created using imperial feet as unit.

100%

Development Status: Open

Upvote 3 Downvote 0
stephen
9 Oct 2018

2 Comments

testure

Agreed, if the addon needs to have some explicit size with respect to a particular unit of measurement, the python API should provide developers with a way to invoke it intentionally.

1
toolplus.courier

Perhaps this unsuccessful attempt is similar to the one sought ...

I tried something like this:

unitinfo = get_current_units()   
size_value = 10
unit_valve = bu_to_unit(size_value, unitinfo[1])
default_value = str(unit_valve)            
base_value : StringProperty(name="Name", default=default_value)  

apply_value = float(base_value)  
unit_apply = unit_to_bu(apply_value , unitinfo[1])
(base_value_apply : FloatProperty(name="Name", default= unit_apply))  

based on jayaman python preview: youtube.com/watch

But i run into this error with PropertyGroup etc.

unitinfo = get_current_units()   
lenght = bpy.context.scene.unit_settings.length_unit
AttributeError: '_RestrictContext' object has no attribute 'scene'

archive.blender.org/wiki/index.php/Extensions:2.6/Py/API_Changes/

The unit system length can be shown in a label:

unitinfo = get_current_units()      
column.label(text=unitinfo[0])