# How can i get the change in value of an objects rotation between the current and previous frame, and use it in a driver

I want to use the change in rotation between the current and previous frames to rotate another object.

EG object A was at 30 degrees in Z axis last frame,

and is now at 35 degrees in Z axis on the current frame,

so has changed by +5 degrees,

so i would want object B to rotate by +5 degrees.

How can i get Z rotation value from the previous frame, and use it in a driver?

I have practically no python knowledge but from other similar questions this might be the only way, but i don't understand how to actually make a script work with my driver.

import bpy, copy

#get current frame
thisFrame = bpy.context.scene.frame_current

#go back one frame
bpy.context.scene.frame_set(thisFrame -1)

#get z rotation of current object
print ("rotation on frame")
print (bpy.context.scene.frame_current)
print (bpy.context.object.rotation_euler[2])

#set custom property ZrotLast to be the same as z rotation
last =copy.copy( bpy.context.object.rotation_euler)
bpy.context.object["ZrotLast"] = last[2]

#go back to current frame
bpy.context.scene.frame_set(thisFrame)

#set custom property ZrotCurrent to be the same as z rotation
bpy.context.object["ZrotCurrent"] = bpy.context.object.rotation_euler[2]

#get z rotation of current object
print ("rotation on frame")
print (bpy.context.scene.frame_current)
print (bpy.context.object.rotation_euler[2])

#work out difference between current and last frame
bpy.context.object["ZrotDelta"] = bpy.context.object["ZrotCurrent"] - bpy.context.object["ZrotLast"]


Please note DO NOT try changing bpy.context.scene.frame_current to change the frame! it will appear to work but it seems like nothing in the frame updates so you will get the same rotation information as the original frame, use bpy.context.scene.frame_set and it works correctly.