# Is it possible to use a piecewise function in property drivers?

I have an object with keyframes on its Z-rotation so it rotates about its vertical axis. I want to have a driver on its X-position that takes the Z-rotation value, performs a modulo-operation on it to bring it into the interval [0°, 120°), and then sets the X-position according to this piecewise definition:

If(
0° ≤ var <  60°,  1 / 2 - 1 / sqrt(3) cos(var - 30°),
60° ≤ var < 120°, -1 / 2 + 1 / sqrt(3) sin(var)
)


I figured out how to add a driver to one property and how to add a reference to another property, but I can't get it to work with a conditional statement inside the driver. The expression (1/2)-(1/sqrt(3))*cos(var-pi/6) on its own works as expected, as does (-1/2)+(1/sqrt(3))*sin(var), but if var < (1/3*pi): ((1/2)-(1/sqrt(3))*cos(var-pi/6)) else: ((-1/2)+(1/sqrt(3))*sin(var)) leads to the error message "Invalid Python expression".

This is my first time using Python, so I might just be misunderstanding the Python documentation on IF statements. Is it possible to get this working? Or are conditionals not allowed in drivers?

• if .... else: ... is a statement and has no return value. For the driver, you need an expression that evaluates to a value. Python has also a ternary conditional operator, which is equivalent to the C ? a : b operator in C/C++/Java/Javascript. Have a look here: stackoverflow.com/questions/394809/… Jan 31 at 16:36

((1/2)-(1/sqrt(3))*cos(var-pi/6)) if var < (1/3*pi) else ((-1/2)+(1/sqrt(3))*sin(var))

a if condition else b