I've been looking for a way to calculate the speed of an object or bone. On this same site a couple of people have made this same question but any of the scripts I've tried in the current version of Blender don't seem to work (anymore).
What I'd like to have is a driver/drivers that says/say how much an object or bone has moved every frame, so a comparison of the location of the distance of the previous frame to the current frame. This can be a single value or in separate X, Y and Z values, either would work.
Among the scripts I've tried this one seems like a good solution but sadly it mostly returns zeroes for me:
The numbers it pops out seem right but it doesn't seem to be updating at the right rate. I've added this file to the post.
Edit: After tetii's suggestion I did some more experimenting and did get the script to work, somewhat:
The file for this I've provided also, it's the second one. What I've done is simply copy over the driver to the Z-location of the cube. Now the driver for 'dx' is updating correctly, I just don't understand why. So it looks like the script from the linked post by Rich Sedman can work, there just seems to be some issue with the way it uses variables or I misunderstood something.
import bpy
attributes = {}
def drv_calc_change(frame, attr, value):
# Used to calculate the change of a property (eg, x-coord) between one frame and the next.
# Determine whether we already have a value for this attribute
if attr in attributes:
attribute = attributes[attr]
else:
# Not found - create a new record for it and store it
attribute = {'frame':frame, 'value':value}
attributes[attr] = attribute
# Calculate the difference
difference = value - attribute['value']
# If new frame then store the new value to use next time.
if frame != attribute['frame']:
attribute['frame'] = frame
attribute['value'] = value
attributes[attr] = attribute
return difference
if 'drv_calc_change' in bpy.app.driver_namespace:
del bpy.app.driver_namespace['drv_calc_change']
bpy.app.driver_namespace['drv_calc_change'] = drv_calc_change
self
attribute and change the expression todrv_calc_change(frame, "dx", self.location.x)
. $\endgroup$