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?

  • 3
    $\begingroup$ 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)) $\endgroup$
    – Nathan
    Commented Jan 25, 2022 at 18:53
  • $\begingroup$ Why are using drivers for such simple tasks? Python drivers will be slower than build-in modifiers. $\endgroup$
    – Crantisz
    Commented Jan 31, 2022 at 20:08

2 Answers 2


Do you mean a clamp?


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.

  • $\begingroup$ 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 $\endgroup$
    – Gorgious
    Commented Jan 26, 2022 at 10:18
  • $\begingroup$ @Gorgious Cheers! Do you have to have the code loaded in some IDE to find this stuff, or is there another way? $\endgroup$
    – Robin Betts
    Commented Jan 26, 2022 at 10:36
  • 2
    $\begingroup$ 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 :) $\endgroup$
    – Gorgious
    Commented Jan 26, 2022 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.

  • $\begingroup$ 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) $\endgroup$ Commented Jan 25, 2022 at 19:19
  • $\begingroup$ I see. I didn't get that from your question. In that case you do want Robin Betts answer. $\endgroup$ Commented Jan 25, 2022 at 20:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .