4
$\begingroup$

I want to figure out how to set up up drivers that alternate from zero to one every frame. Id also love to know how to do it if i wanted to alternate every 4 frames or possibly value 1 for 4 frames and value 0 for 2 frames or so.

These are the types of patterns i want to know how to create

01010101, 00100100

$\endgroup$
2
  • 2
    $\begingroup$ you can do it like this: "frame % 2" (paste this is the drivers field). this other one is "(frame+2) % 3 > 1" $\endgroup$
    – Chris
    Aug 29, 2021 at 16:02
  • $\begingroup$ You shouldn't have deleted your answer Chris, it was only one small change away from being complete. $\endgroup$ Aug 29, 2021 at 16:43

2 Answers 2

10
$\begingroup$

Python console.

Further to other answers, will elaborate on using the python console to test driver expressions

For a 1 every Nth frame, using modulus as elaborated on in other answers,

not frame % N

which is the equivalent of

frame % N == 0

Test run in the console, f for frame. A boolean when used in an expression is converted to an integer. 1 is True.

>>> for f in range(1, 10):
...     f, f % 4, int(not f % 2), int(not f % 3), int(not f % 4)
...     
(1, 1, 0, 0, 0)
(2, 2, 1, 0, 0)
(3, 3, 0, 1, 0)
(4, 0, 1, 0, 1)
(5, 1, 0, 0, 0)
(6, 2, 1, 1, 0)
(7, 3, 0, 0, 0)
(8, 0, 1, 0, 1)
(9, 1, 0, 1, 0)

replace N with a number for your driver expression, eg every 4th

not frame % 4 

Note this can be typed directly into the property field. Prefix the expression with hash # the first time eg #not frame % 4 to signify it as a driver (turns purple on entering).

Since these are booleans, every 4th or every 13th

not frame % 4 or not frame % 13

For a repeating pattern 000011 it may be simpler to shift the frame to make frame 1 calculate as 0

>>> for f in range(1, 10):
...     f, (f - 1) % 6, int((f - 1) % 6 > 3)
...     
(1, 0, 0)
(2, 1, 0)
(3, 2, 0)
(4, 3, 0)
(5, 4, 1)
(6, 5, 1)
(7, 0, 0)
(8, 1, 0)
(9, 2, 0)

Expression

(frame - 1) % 6 > 3

which is the same as

 (frame - 1) % 6 // 4

using mods partner in crime to integer divide modulus result by 4. Only 4 and 5 will produce 1.

$\endgroup$
4
$\begingroup$

Because you are using the frame number, there are two ways you can create the driver.

The easier way is to go to the input field that you want the driver attached to and type in a python expression, using #frame to represent the frame number.

The harder way is to go to the input field that you want the driver attached to, right click, and select Add Driver. In this case you use frame without the '#' character. Delete the existing 'var' entry and type the python expression into the Expression input.

I'm going to type frame in the expressions below. Substitute #frame if you're typing into the input field; Substitute frame if you're in the driver editor.

The trick is to use Python's mod operator, which is represented by the percent sign (%) in Python. Mod returns the remainder after an integer division. If you type 7 % 3, python will return 1, since 7 divided by 3 is 2, but 2 * 3 is 6, so 7 - 6 = 1.

The other thing to know is that Python uses 0 to represent false and 1 to represent true, so logical expressions in Python, such as x > y return integer values.

If you want to generate a repeating sequence of N zeros followed by a 1, you can use the following expression frame % (N + 1) > 0. This is because the mod operator only returns 0 when the number is exactly divisible by the second operand.

Alternating 0s and 1s is a special case, since the mod operator only returns 0 or 1 when 2 is the second operator, so, putting this all together,

for alternating 0 and 1, use frame % 2. For two 0s followed by a 1, use frame % 3 > 0 as your expression:

As an immediate driver:

directly typed in driver

As an expression in the driver editor:

as an expression in the driver editor

$\endgroup$
2
  • 1
    $\begingroup$ Note: If you add an immediate driver, using #frame; when you left click on the field again, blender will show you the expression without the #. This explains why my first screen shot showed an obviously working driver with what appears to be an invalid expression. $\endgroup$ Aug 29, 2021 at 17:44
  • 3
    $\begingroup$ Why the downvote? $\endgroup$
    – Blunder
    Aug 29, 2021 at 17:58

You must log in to answer this question.

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