How to make layouts and connections in the geometry node editor and shader editor in python code

I have two already made geometry nodes tree and shader tree, how do I convert them into python code. Because I don’t want to manually add geometry nodes and shader nodes every time I import data.
Thank you all very much!

You can make those Geo and Shader setups into Groups. Save those groups into your default startup file and Add Group >>>>
Or they can be saved into your asset library… You can drag and drop them onto objects from there.

This person is a good Python for Blender teacher…
https://www.youtube.com/@CGPython/featured

Good luck.

I wrote a script that will turn a node tree back into a Python script, if you want to try it.

It is set up for Shader node trees, but you could alter it to work with Geo Nodes as well.
It collects all the nodes, their locations, their links and all their values and outputs a Python file that will recreate the nodes when you run it in Blender.

Just replace the file location (on the ‘with open’ line) with a location on your PC:

import bpy
from contextlib import redirect_stdout

object = bpy.context.object
material = object.material_slots[0].material
nodes = material.node_tree.nodes
links = material.node_tree.links

with open('/USER/Documents/MaterialOutput.py', 'w') as text_output:
    with redirect_stdout(text_output):

        print(f'''# Setup
import bpy

# Name the Material
material_name = '{material.name}'

# Create the material, add a node tree and clear it
material = bpy.data.materials.new(name=material_name)
material.use_nodes = True
nodes = material.node_tree.nodes
links = material.node_tree.links
nodes.clear()
''')

        print('# Nodes')
        for node in nodes:
            name = node.name.replace(' ', '').replace('.','')
            type = node.bl_idname
            x = int(node.location[0])
            y = int(node.location[1])
            print(f"{name} = nodes.new(type='{type}')")
            print(f"{name}.location = ({x},{y})")
            print('')

        print('# Links')   
        for link in links:
            from_node = link.from_node.name.replace(' ', '').replace('.','')
            from_socket = link.from_socket.name
            to_node = link.to_node.name.replace(' ', '').replace('.','')
            to_socket = link.to_socket.path_from_id()[-4:]
            socket_index = int("".join(filter(str.isdigit, to_socket)))
            print(f"""links.new(
{from_node}.outputs['{from_socket}'],
{to_node}.inputs[{socket_index}] # {link.to_socket.name}
)""")
            print('')

        print('# Values') 
        for node in nodes:
            if not node.name == 'Material Output':
                for input in node.inputs:
                    if not input.is_linked:
                        name = node.name.replace(' ', '').replace('.','')
                        if input.type == 'VALUE':
                            print(f"{name}.inputs['{input.name}'].default_value = ({input.default_value})")
                        elif not input.type =='SHADER':
                            value = input.default_value
                            try:
                                print(f"{name}.inputs['{input.name}'].default_value = ({value[0]},{value[1]},{value[2]},{value[3]})")
                            except:
                                print(f"{name}.inputs['{input.name}'].default_value = ({value[0]},{value[1]},{value[2]})")
                print('')
        print('')
        
        print(f'''# Add the material to the object
object = bpy.context.object
object.data.materials.append(material)
''')

text_output.close()