Thursday, December 27, 2007

Poser python script to make a figure blink

Here is a Python script that I wrote to automate the insertion of key frames in order to avoid the manual work in making a Poser figure blink.
In this code, I used the model JessicaCasual which comes with Poser 6, and the blinking parameter is associated with her Head, so if you want to use this code for other figures, please tweak them as needed.

Please also note that I am using both Spline and Linear curves.
Spline is used when you are closing her eyes, and Linear is used between two key frames where eyes are open.

If you do not do this, then depending on how key frames are set, the value of the "Blink" parameter can go out of the 0 to 1 range due to the way the curve are interpolated from a few key frames which can mess up the animation.

Here is the generated animation that I posted to Google Video. http://video.google.com/videoplay?docid=-7199780183159438675

Hide Inada

--- BEGIN Script ---


# Copyright (C) Hideyuki Inada 2007. All rights reserved.
# Adjust the end frame of the animation manually in the UI for now.

sce = poser.Scene()
fig = sce.Figure("JessicaCasual")
head = fig.Actor("Head")

startFrame = 0
endFrame = 300

sce.SetOutputRange(startFrame, endFrame)

mm = sce.MovieMaker()
mm.SetOutputStartFrame(startFrame)
mm.SetOutputEndFrame(endFrame)

# start off by opening the eye
i = 0
sce.SetFrame(i)
head.SetParameter("Blink", 0)
head.AddKeyFrame(i)

nPrevFrame = 0

for i in range(startFrame, endFrame):
if ( (i % 20 == 0) and (i % 60 != 0) ):
# Open

head.SetRangeLinear(nPrevFrame, i)
for j in range(i, i+2):
sce.SetFrame(j)
head.SetParameter("Blink", 0)
head.AddKeyFrame(j)
nSplineFrameStart = i + 1

# Close
j = i + 5
sce.SetFrame(j)
head.SetParameter("Blink", 1)
head.AddKeyFrame(j)

# Open
for j in range(i+9, i+11):
sce.SetFrame(j)
head.SetParameter("Blink", 0)
head.AddKeyFrame(j)

head.SetRangeSpline(nSplineFrameStart, i + 9)
nPrevFrame = i + 10



--- END Script ---