Struggling to export selected objects with different origins

I’m writing a script that selects all objects with a certain name, and exports the selected objects with an origin based upon a bounding box of said objects.

The part where I’m struggling with is the last step - what method is there to ‘Export Selected with Origin’ as it were?

import rhinoscriptsyntax as rs
import os

rs.EnableRedraw(False)

# Get all objects
objs = rs.AllObjects(select=True)

# Create an empty list of names to add unique names to
namedelements = []

# Check if any objects were selected
if objs:
    for obj in objs:
        # Get the name of each object
        name = rs.ObjectName(obj)
        if name:
            namedelements.append(name) if name not in namedelements else namedelements
else:
    print("No objects selected.")

# Create a bounding box around each named set of objects, in order to create a new origin with which to export to (X and Y only... Z remains the same as world Z)
    grouped = rs.ObjectsByName(name)
    bounding_box = rs.BoundingBox(grouped)
    if bounding_box:
        brep = rs.AddBox(bounding_box)
        min_point = bounding_box[0]  # The minimum corner
        max_point = bounding_box[6]  # The maximum corner
        centre = [(min_point[0] + max_point[0]) / 2,
            (min_point[1] + max_point[1]) / 2,
            (min_point[2] + max_point[2]) / 2]
        filepath=r'"C:\{}.3dm"'.format(name)
        ip = rs.coerce3dpoint([centre[0], centre[1], 2])
        rs.Command('_-ExportWithOrigin {} {}'.format(ip,filepath))
 
rs.EnableRedraw(True)

Any help greatly apprecaited.
Working on Rhino 7 & Python 2.7

By “export” do you mean, save the selected objects in a new .3dm file?

The code doesn’t do anything with named elements that I can see. And what’s x, is that provided by a Grasshopper component Input Param?

Apologies, I think that’s an error, x should be name (the object’s name) - I’ve corrected the code above.

…and yes, at the end of the script I’m trying to save the selected objects in a new .3dm file with a base point centred on the named object(s).

No problem. Thats still just going to use what ever the last value of name happened to be (and will hit a NameError if objs is empty).

As for the exporting, I can’t remember what the commands are exactly within rs.command, but I would prompt the user to save the current document (or just save it for them).

I’d try either using the clip board and rs.command again to copy and paste, or in RhinoPython, just rs.CopyObjects(objs).