Generate heightmap from Terrain object with code

I want to make a 2D simulation of objects growing on a heightmap and then generate 3D objects from the simulation to the Terrain object which the heightmap is generated from. I know how to do this for a fixed Terrain but I want the Terrain to be arbitrary. Which means that I would need to generate a heightmap when the user has specified which Terrain that should be used. Is there any way to generate a heightmap on runtime with code?

Sounds like you’re looking for Terrain.GetHeights.