The “range” function is used to transfer a value from one range, for example, from 0 to 1000, to another, for example, from -1 to 1. To quickly get a value in different ranges, we can define a simple function.
The equation for converting a value from one range to another is as follows:
Let’s define the function according to the equation:
1 2 3 4 5 |
def range(value, min_src=0.0, max_src=1.0, min_dest=0.0, max_dest=1.0): if max_src == min_src: return 0.0 else: return ((value - min_src) / (max_src - min_src)) * (max_dest - min_dest) + min_dest |
Now we can easily use it to get a value, for example, from the range 0 … 10 to the range -1 … 1 for the X coordinate of the active object position:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
print(bpy.context.object.location.x) # 6.05 print( range( bpy.context.object.location.x, min_src = 0, max_src = 10, min_dest = -1, max_dest = 1 ) ) # 0.21 |