1
$\begingroup$

I have this code that creates a bunch of Obj Info nodes and joins them with a join geometry node. It is supposed to end by linking the Join Geometry node to the Group output node but it's failing to do that:

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
group_output.location.y = 0

#Create group input
group_input = nodes.new('NodeGroupInput')
group_input.location.x = -500
group_input.location.y = 0

# 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
    node.transform_space = 'RELATIVE'
    links.new(node.outputs['Geometry'], join_geo.inputs['Geometry'])
    
links.new(join_geo.outputs['Geometry'], group_output.inputs['Geometry'])

I assume this is because the Group Output node doesn't have an input with the geometry type, since it's empty by default.

How can I give the Group Output node that Input type when I create it? Or alternatively, how can I connect the Join Geometry 'Geometry' output to the empty socket?

I've tried copying the code from the scripting tab but manually linking the nodes just generates code to link 2 coordinates and it doesn't work in the script. Creating the Input and setting it to Geometry on the Group Output also seems to create code that doesn't work when I copy and paste it after the creation of the Group Output node

$\endgroup$
6
  • 1
    $\begingroup$ Is your code AI generated? $\endgroup$ Commented Jun 16, 2024 at 13:53
  • $\begingroup$ Well, it clearly is. You should mark AI-generated content as such, as there are a number of problems with such content. For one, I just don't read AI-generated code because it's toxic, the hallucinations poison your intuition. Another problem is you asked chatGPT something, but the result you got back doesn't reflect your actual intentions. If you really want to create a node tree from scratch, I suggest reading my answer (specifically the linked 3rd rev) here. $\endgroup$ Commented Jun 16, 2024 at 14:05
  • $\begingroup$ Otherwise it's a duplicate of blender.stackexchange.com/questions/302753/… I think. $\endgroup$ Commented Jun 16, 2024 at 14:06
  • $\begingroup$ I don't see how that's a duplicate. I don't have a Input to begin with, that's why I can't connect them together $\endgroup$ Commented Jun 16, 2024 at 14:42
  • 1
    $\begingroup$ Something like group.interface.new_socket(name="Geometry", in_out="INPUT", socket_type="NodeSocketGeometry") see docs.blender.org/api/current/… $\endgroup$ Commented Jun 16, 2024 at 16:45

1 Answer 1

1
$\begingroup$

After messing with The missing line was nt.outputs.new('NodeSocketGeometry', 'Geometry'). I have also added the counter part for the Group Input node nt.inputs.new('NodeSocketGeometry', 'Geometry') so that I could link it to the Join Geometry node as well.

I don't really know much about coding so I don't know if this is doing something badly but it seems to work so far. The final code is here if anyone is interested:

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'])
 

enter image description here enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Hello, it looks like it could be extremelyy straightforward if you put your objects in a collection and use a collection info node instead ? $\endgroup$ Commented Jun 16, 2024 at 18:05
  • $\begingroup$ @Gorgious It sure would have been nice to know that node existed before dedicating 4h to try an make this work lol Well, I can see a few cases where the object won't be in the same collection so at least there's that. It might be useful at some point $\endgroup$ Commented Jun 16, 2024 at 18:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.