bl_pyapi_bpy_utils_units.py 3.9 KB

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