Versions Compared

Key

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

...

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

Code Block
# Get all objects and create an empty list
objects       = scene.getObjects()
activeObjects = []

# Loop through the objects and add them to the "activeObjects" when they are active rigid bodies
for entry in objects:
	dynamicsMode = entry.getParameter("Dynamics")
	if (dynamicsMode == "Active rigid body"):
		activeObjects.append(entry)

# The global variable is necessary to transfer the active objects from "SimulationPre" to "FramesPost".
scene.setGlobalVariableValue("activeObjects", activeObjects)

...

The output is formatted ("%.2f" % averageVelocity) and clips the result to two digits, e.g. 3.67 instead of 3.671472813

Code Block
languagepy
# Read out the global variable and make it local again
# Count how many objects are inside "activeObjects"
activeObjects  = scene.getGlobalVariableValue("activeObjects")
numOfObjects   = len(activeObjects)
objectVelocity = 0.0
 

# Go through the objects and add up their velocities
for entry in activeObjects:
	objectVelocity += entry.getVelocity().module()
 

# Calculate the average and print it
averageVelocity = objectVelocity / numOfObjects
 
scene.message("The average velocity is "+str("%.2f" % averageVelocity)+" m/s")