GameBlender - game to Ipo

This tutorial describes how to transfer motion data from game engine to animation. It makes possible to use game engine for physics simulation (gravitation, collisions) and also to record player's activities.

I used an old example with jumping ball, to which I added two scripts:

Initialization

Script recIpo.py runs once at game startup, so it has sensor for one-time run.

Content of recIpo.py script:

import Blender

# get recorded object and its name
contr = GameLogic.getCurrentController()
gameObj = contr.getOwner()
name = gameObj.getName()[2:]

# create ipo object for recording
obj = Blender.Object.Get(name)
ipo = Blender.Ipo.New('Object','recIpo')
obj.setIpo(ipo)

# create curves for position
locx = ipo.addCurve('LocX')
locy = ipo.addCurve('LocY')
locz = ipo.addCurve('LocZ')

# save curves to global variable
GameLogic.rec = [gameObj, locx, locy, locz]

Script description:

Recording

Script recFrame.py runs every 1/25 second during game, sensor is set to required frequency  (f = 2). Frequency depends on accuracy of animation we want to achieve.

We have to know game time to record position. We can do this by defining property with Timer type to game object.

Content of recFrame.py script:

import Blender

# get object
contr = GameLogic.getCurrentController()
obj = contr.getOwner()

# get object's position and actual frame
pos = obj.getPosition()
frame = 25*obj.time

# if exists global variable for recording
if hasattr(GameLogic, 'rec'):

  # record actual position to curves
  GameLogic.rec[1].addBezier((frame, pos[0]))
  GameLogic.rec[1].update()
  GameLogic.rec[2].addBezier((frame, pos[1]))
  GameLogic.rec[2].update()
  GameLogic.rec[3].addBezier((frame, pos[2]))
  GameLogic.rec[3].update()

Script description:

Result

Resulting Ipo curves are immediately usable for animation.

Given approach can be extended to record data for more objects or to record other data from game object.

Recording tool

Example with scripts and recorded Ipo curves is in file recIpo.zip. This file contains advanced version of data recording - one object "Recorder" (with near sensor) records positions and also orientation of all objects in the scene, which have defined property ipo and are in defined distance from recorder. To use recording in your files just append Recorder object and add ipo property to objects which should be recorded.

Property maxRot defines min/max value for rotations. It reduces "jumps" in rotations (transition through 180 degrees) - allows it only if rotation value reaches maxRot value.

To main page