Place objects on the center of the ground plane and maintain structure via python script I am trying to do what more or less is described in this thread How to place objects on the center of the ground plane via python? the accepted solution seems to work fine if you have only one object in the scene. However, in my case my scene is structured from individual objects thus this leads to the result all the objects to be scrambled together in the center of the plane as you can see below:

Initially my scene is like this:

By using the GUI I was able to center all the objects and maintaining the mesh structure by following these https://blender.stackexchange.com/a/26452 instructions.

Thus, I would like to know how I can achieve that in a python script.
Thanks.
A way to do it:

Loops overs selected meshes to get each individual center in world coordinates
Then calculates overall center
Loops again over them to shift their vertices considering the overall center

The script:
import bpy
from mathutils import Vector

# Choose the axis you want to center on
center_x = True
center_y = True
center_z = False

# Get objects
objects = bpy.context.selected_objects

centers = []

# Loop over meshes to get their centers
for obj in [o for o in objects if o.type == 'MESH']:
    world_matrix = obj.matrix_world
    vertices = obj.data.vertices
    # Get object center in world coordinates
    center = sum([world_matrix @ Vector(v.co) for v in vertices], Vector()) / len(vertices)
    centers.append(center)

# Calculate overall center
center = sum([c for c in centers], Vector()) / len(centers)

# Keep axis you want
center.x = center.x if center_x else 0
center.y = center.y if center_y else 0
center.z = center.z if center_z else 0

# Loop over meshes to shift them
for obj in [o for o in objects if o.type == 'MESH']:
    vertices = obj.data.vertices
    world_matrix_inv = obj.matrix_world.inverted()
    # Calculate the shift in object coordinates
    delta = world_matrix_inv @ center
    # Shift vertices
    for v in obj.data.vertices:
        v.co = v.co - delta