Alright, here’s what I have so far. Usable, but very limited.
The variables ‘staFra’, ‘endFra’, and ‘mag’ are the only ones that do anything right now. Lot’s of stuff to do yet.
Select a camera, and run the script. It’ll tell you if you didn’t have one selected, or if you had more than one object selected and didn’t realize it. Don’t worry, it’ll let you know if it was successful or not. When run, it uses itself as a script link set to FrameChanged. So after you run it once, that’s all you need to worry about.
When playing the animation (only 3d views right now, no renders) if the frame is between staFra and endFra it gets the camera’s location, and gives it a new, random one, based on what value you gave the variable mag. Even a value of 1 creates quite a shake, so keep it small.
Also, if you don’t plan on animating the camera, it’s important to set at least one location keyframe. Try it without and you’ll see why.
It’s also important to note that as of right now the link has to be deleted manually. So, if you decide to use it on a different camera, or decide to change any of the values (staFra, endFra, mag) remember to delete it (Script Panel) and rerun the script!
I noticed while playing with it that it doesn’t always redraw the 3d windows depending on what Screen you’re using. SCR:2-Model and SCR:3-Material worked for me, but the others didn’t. I have no idea why.
Because of this I may change the script link so that it is an object based one, as opposed to scene. To be honest I don’t know what the pros/cons are of either.
Enough rambling.
import Blender
from Blender import Object
from Blender import Draw
from Blender import Scene
from Blender import Registry
from Blender import Mathutils
def update_Registry():
#Global Variables
g = {}
g["cam"] = cam #DO NOT EDIT
g["staFra"] = 21 #Frame you would like the camera to start shaking
g["endFra"] = 51 #Frame you would like the camera to stop shaking
g["freq"] = 0 #Speed at which camera shakes
g["mag"] = 1 #Maximum amount (positive or negative) camera can be repositioned during shake.
#Should be a fairly low number, a value of 5 even is overkill. So I'd recommend
#a value < 5.
g["taperLength"] = 0 #Number of frames camera shake will taper
g["taperAmount"] = 0 #Amount shake will lesson with each frame while tapering
Blender.Registry.SetKey('cam_shake', g, False)
rdict = Registry.GetKey('cam_shake', False)
if rdict:
try:
cam = rdict['cam']
staFra = rdict['staFra']
endFra = rdict['endFra']
freq = rdict['freq']
mag = rdict['mag']
taperLength = rdict['taperLength']
taperAmount = rdict['taperAmount']
except: update_Registry()
def shake_camera():
curFra = Blender.Get("curframe")
if curFra >= staFra and curFra <= endFra:
#Gets location of each axis of Camera, then uses that location
#to generate a new, random location
x, y, z = cam.getLocation()
#Generating random numbers
ranNumX = Mathutils.Rand(-mag, mag) + x
ranNumY = Mathutils.Rand(-mag, mag) + y
ranNumZ = Mathutils.Rand(-mag, mag) + z
#Setting new location
cam.setLocation(ranNumX, ranNumY, ranNumZ)
Blender.Redraw()
Scene.GetCurrent()
def grab_cam():
#Grab camera object, store its name.
#This way after the script is applied to a camera, the camera
#doesn't actually have to stay selected to affect the camera.
for o in Object.GetSelected():
o = o.getName()
cam = Object.Get(o)
Draw.PupMenu("Success!%t|Link set.")
return cam
def check_cam():
#Checks to make sure the object selected is a camera, it not, aborts.
#Script also aborts if more than one camera is selcted.
count = 0
if Object.GetSelected() == []:
Draw.PupMenu("Error!%t|No cameras selected! Script aborting.")
Draw.Exit()
return False
for o in Object.GetSelected():
count += 1
for o in Object.GetSelected():
if count > 1:
Draw.PupMenu("Error!%t|Too many objects selected! Script aborting.")
Draw.Exit()
return False
elif o.getType() != 'Camera':
Draw.PupMenu("Error!%t|Selected object not camera! Script aborting.")
Draw.Exit()
return False
#Link this script to the scene with event of framechange
def plugscript():
sc = Scene.GetCurrent()
sc.addScriptLink("camera_shake.py", "FrameChanged")
#Checks to see if the script is already linked to the scene
def link_check():
sc = Scene.GetCurrent()
sc = sc.getScriptLinks("FrameChanged")
if sc == ['camera_shake.py']:
return False
#Checks to see if the script link has been set. If it has calls the shake_camera() function.
if link_check() == False:
shake_camera()
#Checks to see if the script link has been set. If it hasn't it sets it.
if link_check() != False:
runFlag = check_cam()
if runFlag != False:
cam = grab_cam()
update_Registry()
plugscript()