# How can I convert two numbers to 0 - 1 range in Driver Scripted Expression?

I have a shapekey driven by a bone. I want to configure the shapekey's driver such that scaling the bone to 0.3 results in a shapekey value of 1, and scaling the bone to 1 results in a shapekey value of 0.

In other words:

Shapekey range: 0 - 1

Bone scale range: 1 - 0.3

I'm sure this is simple, but everything I've tried hasn't worked.

For Hulifier's solution, here is how it looks in my particular use case:

# $$\frac{v-s}{k-s}$$

• s = Range start.
• k = Range end.
• v = Value.

The result is a value between 0 and 1 if v is inside the range.

• s = 1.
• k = 0.3.
• v = the bone scale.
• Wow, it worked perfectly!!! Thank you so much!! I have updated my original question with an image of the solution (for my particular use case). Apr 22 at 21:14

Although slightly more time consuming to set up, you can also do this with just the F-Curve of the driver since it's a simple value mapping.
Doing it this way will make your rig more lightweight than using a Python expression since the graph curve will be evaluated anyway.

The images and shortcut keys below are for Blender 2.79, but work the same on 2.80 and up.

• Set the driver type to Sum Values.
• Make a Transform Channel variable for the Local X Scale of the bone (easier to understand than having to write the property explicitly down to .scale[0]).
• So the values of all variables of the driver (just one variable, in this case) will be summed, and the result will be used as the input "frame" (the time) within the F-Curve of the driver. Let's edit the F-Curve to change how it maps the input.

• Set the curve extrapolation mode (Shift+E) to Constant Extrapolation.

• Select both keyframes and press (T) for the Keyframe Interpolation dialog, set them to Linear keys (instead of the default of Bezier), which are faster to process.

• Place one keyframe on frame 0.3, value 1.0.

• The other keyframe goes on frame 1.0, value 0.0.

So when the input is 0.3 you get an output of 1.0, when the input is 1.0 you get an output of 0.0.

PS you can use the View tab to place the 2D cursor with precision and use the (I) key to create new keyframes on the driver curve at that cursor location.

• Thanks so much for this great solution!!!! I find it very intuitive and used it to drive the values of the shapekeys on my character. Since I want to scale the bone in any axis to drive the shapekey, I right-clicked the bone's Scale channel and chose "Copy as New Driver", then on the shapekey I chose "Paste Driver" and followed your instructions. I've also added a "Limit Scale" constraint (with "Affect Transform" checked) to the control bone to avoid overshooting my scale values (although the Range Min/Max settings of the shapekey itself prevent keyframing values outside of the 0-1 range). Apr 25 at 17:48
• @Copperplate good call, the Limit Scale should make it more intuitive for the animator (even if it'll be yourself). PS the driver value is also limited by the graph curve, it's that Shift + E constant extrapolation part (if the input is less than 0.3 then the output stays at 1.0, and if the input is bigger than 1.0 it stays at zero). BTW, I said "use the (T) dialog to set the keyframes as Linear instead of the default of Bezier", but Bezier keys have their uses too, like adding some easing to the driver -- might be useful with some shape keys that require soft nuanced changes. Good luck! Apr 26 at 18:46
• Thanks for that additional info! Strangely, the "Driver Value" within the Drivers' window shows as if it exceeds the min/max threshold set by the graph curve, however you are right that it actually doesn't exceed it, because increasing / decreasing the thresholds within the shapekey's "Range Min/Max" settings have no effect (unless the extrapolation of the driver curve is set to something other than Constant). Not sure if the behavior is like that in Blender 2.79 but in Blender 3.1 at least... I now know not to trust the Driver Value! :-) Apr 27 at 19:22