Final Code if anyone is interested in this kind of tool. The loop is from Markus von Broady in the other answer and some other ai script and copy-pasted code from a couple other pages:
import bpy
# Create a new Geometry Nodes tree
nt = bpy.data.node_groups.new(name='NewGeometryNodesTree', type='GeometryNodeTree')
# Retrieve active object
active_object = bpy.context.active_object
# Create Geometry Nodes modifier on the active object
modifier = active_object.modifiers.new(name="Geometry Nodes", type='NODES')
modifier.node_group = nt
# Access the node tree within the modifier
nodes = modifier.node_group.nodes
links = modifier.node_group.links
#Create group output
group_output = nodes.new('NodeGroupOutput')
group_output.location.x = 600
# Add a Geometry output to the Group Output node
nt.outputs.new('NodeSocketGeometry', 'Geometry')
#Create group input
group_input = nodes.new('NodeGroupInput')
group_input.location.x = -500
# Add a Geometry input to the Group input node
nt.inputs.new('NodeSocketGeometry', 'Geometry')
# Create nodes and set up the node tree
join_geo = nodes.new('GeometryNodeJoinGeometry')
join_geo.location.x = 300
for i, ob in enumerate(bpy.context.selected_objects):
if ob == active_object:
continue
node = nodes.new('GeometryNodeObjectInfo')
node.location = (-170 * (i % 2), i * 100)
node.inputs['Object'].default_value = ob
#Comment next line to have them set to Original Instead
node.transform_space = 'RELATIVE'
links.new(node.outputs['Geometry'], join_geo.inputs['Geometry'])
links.new(join_geo.outputs['Geometry'], group_output.inputs['Geometry'])
#Commenting the last line prevents the original geometry from being included
links.new(group_input.outputs['Geometry'], join_geo.inputs['Geometry'])
It seems to work fine so far but I'm no programmer so I can't know if it's doing stuff poorly
node = D.node_groups['Geometry Nodes'].nodes.new('GeometryNodeObjectInfo')
and set its attribute to an objectnode.inputs['Object'].default_value = D.objects['Cylinder.003']
$\endgroup$