Camera Follows Soft Body

Add the script to Simulation Flow (Ctrl/Cmd + F2) > FramesPost.

By default it is not possible to parent a camera or other objects to a soft body, because soft bodies do not expose any position data during a simulation. You can see this when you monitor a soft body's "Position" values: all XYZ values remain constant and only the initial position is displayed.

Scene Preparation

  • Create a ground object and set its "Dynamics" option to "Passive rigid body".
  • Add a rocket object ("Rocket01") and set its "Dynamics" option to "Soft body".
  • Add a null ("Null01") and a "Gravity" daemon.
  • Create a camera and move it until you see the object in the camera's focus.
  • Switch to camera view with the 5 key.
  • SceneCamera01 > Node Params > Node > Parent to > Null01
  • SceneCamera01 > Node Params > Camera > Link target > Null01

 

The script takes the soft body's vertex positions and uses them to calculate the rocket's centre of gravity. This position vector is then transferred to the null object.

# Get the soft body (also works with any other object: active rigid body or animated)
# Get the helper null, the (number of) vertices and define a null vector
softBody   = scene.getObject("Rocket01")
null       = scene.getObject("Null01")
vertexList = softBody.getVertices()
numVert    = float(len(vertexList))
pos        = Vector.new(0.0,0.0,0.0)

# Go through the soft body's vertices and add up all positions
for vertex in vertexList:
	pos += vertex.getPosition()

# Calculate the object's midpoint by averaging positions 
centreOfGravity = Vector.new(pos.getX() / numVert, pos.getY() / numVert, pos.getZ() / numVert)

# Apply the centre of gravity to the null object 
null.setParameter("Position", centreOfGravity)