Thursday, December 27, 2007

Poser Python script to make a figure breathe

Here is a Python script to automate the key frame insertion and posing of having a Poser figure breathe.
In this code, I also used the model JessicaCasual which comes with Poser 6 as in my blinking example.
This scripts adjusts the size of the chest and abdomen in a cyclical manner.




Here is the generated video. Please note that the blinking script was also used.

--- BEGIN Script ---


# Copyright (C) 2007 Hideyuki Inada. All rights reserved.
#
# Note: Please adjust the end frame of the animation using the UI.

# Parameters
nStartFrame = 0
nEndFrame = 300

nFramePerSecond = 30

sParameterName = "xScale"
sParameter2Name = "zScale"

nBreathingPerMinute = 16 # See ref 1 below
nCycleDurationInFrame = 60 * nFramePerSecond / nBreathingPerMinute
nHalfCycleDurationInFrame = int(nCycleDurationInFrame / 2)

# Helper method
def debugPrint(sArg):
print(sArg)


def insertKeyFrameAndSetValue(nFrame, oScene, oActor, sParameterName, nParameterValue):
oScene.SetFrame(nFrame)
oActor.SetParameter(sParameterName, nParameterValue)
oActor.AddKeyFrame(nFrame)

# main start
debugPrint("Script starting")
debugPrint("Running on Poser version " + poser.Version())

# Obtain the reference to the Scene object
sce = poser.Scene()

# Setting ranges
sce.SetOutputRange(nStartFrame, nEndFrame)

mm = sce.MovieMaker()
mm.SetOutputStartFrame(nStartFrame)
mm.SetOutputEndFrame(nEndFrame)

fig = sce.Figure("JessicaCasual")
actor = fig.Actor("Chest")
actor2 = fig.Actor("Abdomen")

# Setting poses and inserting key frames

insertKeyFrameAndSetValue(0, sce, actor, sParameterName, 0)

bLastState = 1
# See ref 2 if you can use the keyword "True" here in your environment
# In my enviroment, 1 is used since "NameError: name 'True' is not defined" was displayed
# during run-time

for i in range(nStartFrame, nEndFrame):
if (i % nHalfCycleDurationInFrame == 0):
if(bLastState == 1):
insertKeyFrameAndSetValue(i, sce, actor, sParameterName, 0.98)
insertKeyFrameAndSetValue(i, sce, actor, sParameter2Name, 0.98)

insertKeyFrameAndSetValue(i, sce, actor2, sParameterName, 0.98)
insertKeyFrameAndSetValue(i, sce, actor2, sParameter2Name, 0.98)

bLastState = 0
else:
insertKeyFrameAndSetValue(i, sce, actor, sParameterName, 1.02)
insertKeyFrameAndSetValue(i, sce, actor, sParameter2Name, 1.02)

insertKeyFrameAndSetValue(i, sce, actor2, sParameterName, 1.02)
insertKeyFrameAndSetValue(i, sce, actor2, sParameter2Name, 1.02)

bLastState = 1

debugPrint("Script completed")
# end of code

# References
# 1. http://answers.yahoo.com/question/index?qid=20070715141510AAaQ71b
# 2. http://docs.python.org/lib/node8.html



--- END Script ---

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 ---

Saturday, December 08, 2007

Blender scripts to evenly space objects

Here is the is the link to Blender scripts to evenly space Blender objects:

Download ZIP

In other words, the distance between two objects will be set to the same, and this distance will be calculated by evenly dividing the distance between the object which has the largest value for the axis and another object which has the smallest value for the axis.

How to install
Prerequisite: Please make sure that you have Blender 2.44 or above installed on your computer.

1. Unzip and copy these three scripts to "./blender/scripts" directory under your Blender installation.
For example, on MacOS, if you install Blender under /Application, the directory may be:
/Applications/blender-2.44-OSX-10.3-py2.3-powerpc/blender.app/Contents/MacOS/.blender/scripts

If you are using MS Windows, you may need to download the zipped version of Blender that contains ".blender/script" folder instead of using the installer to install Blender.

2. Display "Scripts Window" by selecting "Scripts Window" in the "Window type" icon at the top left of one of your windows.

3. In Scripts Window, Select "scripts | Update Menus" menu item.
This registers the script under the "Scripts | Object" menu.

4. Verify that "Evenly Space x" menu item is displayed under the "Scripts | Object" menu.

How to use
Prerequisite : Save your work before running the script!

1. Go to the 3D View window.
2. While in Object mode, select multiple objects that you want to evenly space in 3D View. You have to select three or more objects since you have to have a set of two distances to adjust.
For example, if you select only two objects, there is only one distance, and the script will exit as there is nothing to do.
The order that you select objects does not matter.
3. Go to the Scripts Window.
4. Select "Scripts | Object | Evenly Space x " menu item to evenly space along the x axis. location. Use "Evenly Space y" or "Evenly Space z" for y or z axes.

If you have questions, please email me at hideyuki _at_ gmail.com

Saturday, December 01, 2007

Blender object alignment scripts

Here is the link to the simple objects alignment scripts that I wrote.

http://www.capitolacomputing.com/blender/scripts/align_scripts.zip

Unlike the method discussed in the below link, which seemed to center the location between objects, my scripts align the objects to the last object selected:


http://wiki.blender.org/index.php/FAQ/Modelling/Mesh#How_do_I_align_objects_on_specific_axis.3F


How to install

1. Unzip and copy these three scripts to "./blender/scripts" directory under your Blender installation.
For example, on MacOS, if you install Blender under /Application, the directory may be:
/Applications/blender-2.44-OSX-10.3-py2.3-powerpc/blender.app/Contents/MacOS/.blender/scripts

If you are using MS Windows, you may need to download the zipped version of Blender that contains ".blender/script" folder instead of using the installer to install Blender.

2. Display "Scripts Window" by selecting "Scripts Window" in the "Window type" icon at the top left of one of your windows.

3. In Scripts Window, Select "scripts | Update Menus" menu item.
This registers the script under the "Scripts | Object" menu.

4. Verify that "Align x" menu item is displayed under the "Scripts | Object" menu.

How to use
1. Go to the 3D View window.
2. While in Object mode, select multiple objects that you want to align in 3D View.
In selecting, please remember that objects will be aligned to the object that is selected last when the script is run.
3. Go to the Scripts Window.
4. Select "Scripts | Object | Align x " menu item to align x location. Use "Align y" or "Align z" if you want to align y or z locations.

If you have questions, please email me at hideyuki@gmail.com