3

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!

1 Answers1

3

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.

taiyo
  • 3,384
  • 1
  • 3
  • 18