Today I wasted a bit of time coding a remap function that in languages like actionscript or processing is included in the library.
Is there anything similar to this in mathutils or somewhere in bpy ?
import bpy from mathutils import Vector def remap(current, lower_old, upper_old, lower_new, upper_new): # spreads must return difference between the values old_tup = lower_old, upper_old new_tup = lower_new, upper_new # reusing a nicely coded Vector math utility :) old_min = Vector((0.0, 0.0, lower_old)) old_max = Vector((0.0, 0.0, upper_old)) new_min = Vector((0.0, 0.0, lower_new)) new_max = Vector((0.0, 0.0, upper_new)) # fast and saves room! spread_old = (old_max-old_min).length spread_new = (new_max-new_min).length factor_remap = spread_new / spread_old current_vector = Vector((0.0, 0.0, current)) remap_temp = (current_vector-old_min).length remapped = (remap_temp * factor_remap) + new_min # take care of some potential rounding issues, if the receiver doesn't clamp. if remapped < lower_new: return lower_new if remapped > upper_new: return upper_new return remapped