2
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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/… $\endgroup$
    – Blunder
    Commented Jan 31 at 16:36

1 Answer 1

5
$\begingroup$

try

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

it is a ternary conditional operator.

it works like this:

a if condition else b

also look here: https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator

$\endgroup$

You must log in to answer this question.

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