Wednesday, January 02, 2008

Demo Blender Python script to automate the keyframe insertion

Here is a demo Blender Python script to automate the keyframe insertion.
In this demo, keyframes are created at the object level, or IPO level (instead of vertex level).
In the sample video below, I used the monkey mesh model called Suzanne which comes with Blender.
This scripts adjusts the location and rotation at each keyframe.





--- BEGIN Script ---


#!BPY

"""
Name: 'Automated IPO keyframe insertion demo'
Blender: 244
Group: 'Animation'
Tooltip: 'Demo for automated IPO keyframe insertion using Python'
"""

__author__ = "Hide Winston Inada"
__version__ = "0.9 2008-1-03"

__bpydoc__ = """\
This script is a demo to show automated IPO keyframe insertion using Python.
"""

# $Id: $
# ***** BEGIN GPL LICENSE BLOCK *****
# Script Copyright (C) 2007, Hide Winston Inada, hideyuki _at_ gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# ***** END GPL LICENCE BLOCK *****

import math

import Blender
from Blender import Window
from Blender import NMesh
from Blender import Object

import bpy

# Variables
nNewStartFrame = 1
nNewCurrentFrame = 1
nNewEndFrame = 120

nNoFrames = nNewEndFrame - nNewStartFrame + 1
nFirstKeyFrame = nNewStartFrame - 1 + nNoFrames/4
nSecondKeyFrame = nNewStartFrame - 1 + (nNoFrames/4)*2
nThirdKeyFrame = nNewStartFrame - 1 + (nNoFrames/4)*3

# Debug print out
print ("")
print ("Starting demo for automated IPO keyframe insertion using Python")
print ("Configurable parameter listing:")
print ("Start frame : " + str(nNewStartFrame))
print ("End frame : " + str(nNewEndFrame))
print ("Current frame : " + str(nNewCurrentFrame))
print ("Number of frames : " + str(nNoFrames))
print ("First keyframe : " + str(nFirstKeyFrame))
print ("Second keyframe : " + str(nSecondKeyFrame))
print ("Third keyframe : " + str(nThirdKeyFrame))
# Debug print out end

activeScene = bpy.data.scenes.active
activeObject = activeScene.objects.active

# Setting start, end, current frames
nOriginalStartFrame = Blender.Get('staframe')
# print("Original start frame : " + str(nOriginalStartFrame))

nOriginalCurrentFrame = Blender.Get('curframe')
# print("Original current frame : " + str(nOriginalCurrentFrame))

nOriginalEndFrame = Blender.Get('endframe')
# print("Original end frame : " + str(nOriginalEndFrame))

Blender.Set('curframe', nNewCurrentFrame)

cxt = activeScene.getRenderingContext()
cxt.startFrame(nNewStartFrame)
cxt.endFrame(nNewEndFrame)

Window.RedrawAll()

# Verifying to see if frames are set correctly
nResult = Blender.Get('staframe')
if(nNewStartFrame != nResult) :
print("Error in setting the start frame")

nResult = Blender.Get('endframe')
if(nNewEndFrame != nResult) :
print("Error in setting the end frame")

nResult = Blender.Get('curframe')
if(nNewCurrentFrame != nResult) :
print("Error in setting the current frame")

# Keyframe insertion
obj = activeObject

Blender.Set('curframe', nNewStartFrame)
obj.LocX = 0
obj.LocY = 0
obj.LocZ = 0
obj.RotZ = math.radians(90)
obj.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

Blender.Set('curframe', nNewEndFrame)
obj.LocX = 0
obj.LocY = 0
obj.LocZ = 0
obj.RotZ = math.radians(360+90)
obj.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

Blender.Set('curframe', nFirstKeyFrame)
obj.LocX = 4
obj.LocY = 0
obj.LocZ = 0
obj.RotZ = math.radians(180)
obj.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

Blender.Set('curframe', nSecondKeyFrame)
obj.LocX = 4
obj.LocY = 4
obj.LocZ = 0
obj.RotZ = math.radians(270)
obj.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

Blender.Set('curframe', nThirdKeyFrame)
obj.LocX = 0
obj.LocY = 4
obj.LocZ = 0
obj.RotZ = math.radians(360)
obj.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

# Finalization
print("Completed")

# References
# 1. For setting start, end frames, see:
# http://blenderartists.org/forum/archive/index.php/t-62993.html




--- END Script ---