1 # Apache License, Version 2.0
3 # ./blender.bin --background -noaudio --python tests/python/bl_pyapi_bpy_utils_units.py -- --verbose
6 from bpy.utils import units
8 class UnitsTesting(unittest.TestCase):
9 # From user typing to 'internal' Blender value.
11 # system, type, ref, input, value
13 ('IMPERIAL', 'LENGTH', "", "1ft", 0.3048),
14 ('IMPERIAL', 'LENGTH', "", "(1+1)ft", 0.3048 * 2),
15 ('IMPERIAL', 'LENGTH', "", "1mi4\"", 1609.344 + 0.0254 * 4),
16 ('METRIC', 'LENGTH', "", "0.005µm", 0.000001 * 0.005),
17 ('METRIC', 'LENGTH', "", "1e6km", 1000.0 * 1e6),
18 ('IMPERIAL', 'LENGTH', "", "1ft5cm", 0.3048 + 0.01 * 5),
19 ('METRIC', 'LENGTH', "", "1ft5cm", 0.3048 + 0.01 * 5),
20 # Using reference string to find a unit when none is given.
21 ('IMPERIAL', 'LENGTH', "33.3ft", "1", 0.3048),
22 ('METRIC', 'LENGTH', "33.3dm", "1", 0.1),
23 ('IMPERIAL', 'LENGTH', "33.3cm", "1", 0.3048), # ref unit is not in IMPERIAL system, default to feet...
24 ('IMPERIAL', 'LENGTH', "33.3ft", "1\"", 0.0254), # unused ref unit, since one is given already!
25 ('IMPERIAL', 'LENGTH', "", "1+1ft", 0.3048 * 2), # default unit taken from current string (feet).
26 ('METRIC', 'LENGTH', "", "1+1ft", 1.3048), # no metric units, we default to meters.
27 ('IMPERIAL', 'LENGTH', "", "3+1in+1ft", 0.3048 * 4 + 0.0254), # bigger unit becomes default one!
28 ('IMPERIAL', 'LENGTH', "", "(3+1)in+1ft", 0.3048 + 0.0254 * 4),
31 # From 'internal' Blender value to user-friendly printing
33 # system, type, prec, sep, compat, value, output
35 ('IMPERIAL', 'LENGTH', 3, False, False, 0.3048, "1'"),
36 ('IMPERIAL', 'LENGTH', 3, False, True, 0.3048, "1ft"),
37 ('IMPERIAL', 'LENGTH', 3, True, False, 0.3048 * 2 + 0.0254 * 5.5, "2' 5.5\""),
38 # Those next two fail, here again because precision ignores order magnitude :/
39 #('IMPERIAL', 'LENGTH', 3, False, False, 1609.344 * 1e6, "1000000mi"), # == 1000000.004mi!!!
40 #('IMPERIAL', 'LENGTH', 6, False, False, 1609.344 * 1e6, "1000000mi"), # == 1000000.003641mi!!!
41 ('METRIC', 'LENGTH', 3, True, False, 1000 * 2 + 0.001 * 15, "2km 1.5cm"),
42 ('METRIC', 'LENGTH', 3, True, False, 1234.56789, "1km 234.6m"),
43 # Note: precision seems basically unused when using multi units!
44 ('METRIC', 'LENGTH', 9, True, False, 1234.56789, "1km 234.6m"),
45 ('METRIC', 'LENGTH', 9, False, False, 1234.56789, "1.23456789km"),
46 ('METRIC', 'LENGTH', 9, True, False, 1000.000123456789, "1km 0.1mm"),
49 def test_units_inputs(self):
50 # Stolen from FBX addon!
51 def similar_values(v1, v2, e):
54 return ((abs(v1 - v2) / max(abs(v1), abs(v2))) <= e)
56 for usys, utype, ref, inpt, val in self.INPUT_TESTS:
57 opt_val = units.to_value(usys, utype, inpt, ref)
58 # Note: almostequal is not good here, precision is fixed on decimal digits, not variable with
59 # magnitude of numbers (i.e. 1609.4416 ~= 1609.4456 fails even at 5 of 'places'...).
60 self.assertTrue(similar_values(opt_val, val, 1e-7),
61 msg="%s, %s: \"%s\" (ref: \"%s\") => %f, expected %f"
62 "" % (usys, utype, inpt, ref, opt_val, val))
64 def test_units_outputs(self):
65 for usys, utype, prec, sep, compat, val, output in self.OUTPUT_TESTS:
66 opt_str = units.to_string(usys, utype, val, prec, sep, compat)
67 self.assertEqual(opt_str, output,
68 msg="%s, %s: %f (precision: %d, separate units: %d, compat units: %d) => "
69 "\"%s\", expected \"%s\"" % (usys, utype, val, prec, sep, compat, opt_str, output))
72 if __name__ == '__main__':
74 sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])