# Is it possible to write code in driver that would limit the output value between zero and one?

I wrote this expression. This is my ternary operator. But I don't know how to complete it using else if.

if (location >= 1) else location


I want to do this because I need the output value to not be less than zero or more than one. I was hoping this would work, but it doesn't:

if(location >= 1 ) else if(location <= 0) else location


Is it possible to write code in driver that would limit the output value between zero and one?

• You should probably be looking at something like max(location, x), which returns the highest of the two operands. min() is similar, returning the lowest. To limit a value between 0 and 1, you could use max(0, min(1, var)) Jan 25 at 18:53
• Why are using drivers for such simple tasks? Python drivers will be slower than build-in modifiers. Jan 31 at 20:08

Do you mean a clamp?

max(min(location,1),0)

Thanks to @pyCod3R for their edit.. clamp(var, minval, maxval) turns out to be a 'Blender Provided Function' for drivers:

The docs for those functions aren't immediately obvious.. so thanks to @Gorgious, too, for finding the relevant chunks of code: this bit reveals a default 0 -> 1 range, when minval and maxval are not provided.

• FWIW these 3 "provided" functions are defined there and the actual clamp driver code is there and the math function is there, so the syntax is the one you wrote in your answer. :) I assume just calling clamp(var) automagically clamps between 0 and 1 Jan 26 at 10:18
• @Gorgious Cheers! Do you have to have the code loaded in some IDE to find this stuff, or is there another way? Jan 26 at 10:36
• The method I use is to clone the blender github (which is already a clone of the actual codebase) in visual studio code, then CTRL SHIFT F to find relevant definitions. Then you just have to fetch and pull regularly from VScode source control interface to be up to date. I find it's a pretty straightforward way to dip your toes in the source code :) Jan 26 at 10:54

The format you are looking for is location if 0 < location < 1 else 0

This uses both the ternary form v1 if condition else v2 and chained tests, since 0 < location < 1 is the same as location >= 0 and location <= 1. You can use the later instead if you find it more readable.

EDIT: as a result of a comment, I see that the actual value should be clamped to either zero or one. In that case, this answer gives the better form. You could do this with chaining, but Robin Betts' approach is much more readable.

Here is the changed version for completeness location if 0 < location < 1 else 0 if location < 0 else 1. This works because you can use value1 if condition else value2 in place of v2 in the original form.

• Thanks for the answer, but in your version one thing is missing - if the condition is not met, then we will always have 0. I want the output value = 0 if( location <= 0) and value = 1 if( location >= 1) Jan 25 at 19:19
• I see. I didn't get that from your question. In that case you do want Robin Betts answer. Jan 25 at 20:18