Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Code Block
languagepy
# 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)