You can add custom functions instead of simple expressions in your driver. There you can realise more complex behaviours.
Head to the Scripting
tab and run this script (see docs):
import bpy
from mathutils import noise
def blink_longer():
# still blinking
if blink_longer.time > 0:
blink_longer.time -= 1
return True
# new blink?
elif noise.random() > 0.97:
blink_longer.time = 3 # max blink time
return True
# not blinking
else:
return False
blink_longer.time = 0 # avoid a global to track blinking
# add blink function to the driver namespace
bpy.app.driver_namespace['blink_longer'] = blink_longer
Side note: I added an attribute to the function to avoid a global, but globals will work too.
Now in your driver expression, just call the function blink_longer()
et voila, longer blinking.
Edit: Based on @HarryMcKenzie's comment, some notes about usage: because driver functions drive something (i.e. values) they must return the driven value. In the screenshot i added a custom function
def func():
print("func")
to the driver namespace by running the script (note that this action works as func
itself is valid python). But using this function in a driver results in ERROR: Invalid Python expression
which my be confusing. By checking the error with Window > Toggle System Console
one can see that "func" is correctly printed (thus func
has been called) and after that a type error occures. The driven weight value of the shape key is a number but func
returns implicitly None
, hence the error. func
needs to return something number compatible in this case. The blink_longer
function returns True/False
which is ok as True/False
are coerced to $1/0$ respectively. After fixing errors, run the namespace script again to override the old function definition and then refresh the driver expression.