3
$\begingroup$

May I reference the current property's value before an expression in an expression? This driver expression will generate random values between -1 and 1: 2.0 * (noise.random() - 0.5)

If I was to add that to the current location.x, for example, it seems like it would be: self.location[0] + 2.0 * (noise.random() - 0.5)

This however would keep adding to the location.x value on every frame rather than adding to the original value (before the expression). I'm used to After Effects expressions that use value for this.

Thank you!

$\endgroup$
1

1 Answer 1

3
$\begingroup$

I don't know if there is a way to cache variables within a driver natively, but if not you can add a custom function to the driver namespace which handles that. Run this script to install the function and just call it in the driver.

import bpy
from mathutils import noise

def relative_noise():
    obj = bpy.data.objects['Cube']

    if bpy.context.scene.frame_current == 0:
        # cache initial value at the desired frame
        relative_noise.locationx = obj.location.x
    else:
        # make noise relative to initial position
        obj.location.x = relative_noise.locationx + 2.0 * (noise.random() - 0.5)

    return obj.location.x

relative_noise.locationx = 0  # avoid a global

# add function to the driver namespace
bpy.app.driver_namespace['relative_noise'] = relative_noise

enter image description here

You can now place the object freely and it will wobble around that position.

$\endgroup$
7
  • $\begingroup$ when blender is closed and file reopened, is this saved or do we need to run the script again? $\endgroup$ Sep 6, 2023 at 10:57
  • 1
    $\begingroup$ oh u dont need to run it. u can just Register the script and it will run automatically when u open the blend file like here blender.stackexchange.com/a/299887/142292 $\endgroup$ Sep 6, 2023 at 11:47
  • 1
    $\begingroup$ Awesome, i didn't know! Is this registration tied to the blend file? $\endgroup$
    – taiyo
    Sep 6, 2023 at 12:08
  • 1
    $\begingroup$ yes only tied to this blend file 😁 $\endgroup$ Sep 6, 2023 at 12:09
  • 1
    $\begingroup$ Aaah, perfection :) $\endgroup$
    – taiyo
    Sep 6, 2023 at 12:20

You must log in to answer this question.

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