Using the script link frame change event

Hi All,

I had some luck with the frameChange event.

I am now able to detect rewind, store data in the registry so my event code can know something about it’s past, manipulate an object’s position based upon a frame number.

Here is the code:
paste it into a text window, enable script link for the default cube, click new to select the frameChange code in the text window


import Blender as B
from Blender import Mathutils as Math
INC_VALUE_X = 0.1
NAME = 'Cube'      # Name of object to process.
ob = B.Object.Get( NAME )
#print "Operating on object:" + ob.name
 
# Fetch our storage dictionary from the registry.
RegistryKey = 'ap_Registry_' + NAME
#print RegistryKey
myDict= B.Registry.GetKey(RegistryKey)
if not myDict:  
 # Our dictionary does NOT exist, let's create one.
 myDict = {}
 print 'Creating new dictionary values!'
 
# Default the values in the dictionary that this script will be managing.
 dictionaryPath = RegistryKey + "_LocX"
 myDict[dictionaryPath] = 0.0
 dictionaryPath = RegistryKey + "_LocY"
 myDict[dictionaryPath] = 0.0
 dictionaryPath = RegistryKey + "_LocZ"
 myDict[dictionaryPath] = 0.0
 
#Fetch the values from the dictionary.
dictionaryPath = RegistryKey + "_LocX"
localLocX = myDict[dictionaryPath]
dictionaryPath = RegistryKey + "_LocY"
localLocY = myDict[dictionaryPath]
dictionaryPath = RegistryKey + "_LocZ"
localLocZ = myDict[dictionaryPath]
 
# Process the values based on where we are on the timeline.
frame = B.Get('curframe')
if frame == 1:
  # Reset our value on rewind.
  localLocX = 0.0
  localLocY = 0.0
  localLocZ = 0.0
else:
 # Non rewind processing.
 if (frame < 100):
  localLocX = localLocX + INC_VALUE_X
  print "plus"
 elif (frame < 200):
  localLocX = localLocX - INC_VALUE_X
  print "minus"
 else:
   print "idle"
 
# Return the values to the dictionary.
dictionaryPath = RegistryKey + "_LocX"
myDict[dictionaryPath] = localLocX
dictionaryPath = RegistryKey + "_LocY"
myDict[dictionaryPath] = localLocY
dictionaryPath = RegistryKey + "_LocZ"
myDict[dictionaryPath] = localLocZ
 
# Update our object with the new values.
ob.setLocation (localLocX, localLocY, localLocZ)
 
# Return our dictionary back to the registry.
B.Registry.SetKey(RegistryKey, myDict)
 
# No need to update the scene, that is done after this frameChange event is processed by the system.

Add a timeline, click rewind, then play. Or try ALT-A in a 3D window.

The cube should move from left to right, then back again, wait, then repeat. Also take a look at the console window. You can see the 3 different states this script detects. they are displayed in the console as the object moves through time.

I realize I don’t need to store the values in the dictionary when they are available within the object itself. But other values could be used as parameters to tell us something about the object that is non-standard. Perhaps a tooltip or status?

My next goal would be for the cube to scale up and down once it reaches > 200 frames.

I know I could add another elif to handle > 200 frames processing, but what is the python code for scaling an object?

obj.size?

What about rotating it as well…?