0
$\begingroup$

my goal is to create a Geometry Node Setup for the currently selected object via Pythonscript looking like this:

enter image description here

While it's pretty easy to create and connect different nodes, I'm still having problems connecting Nodes to the Group-Input and Group-Output. The code I've created looks like this:

import bpy

# Get the selected object
selected_obj = bpy.context.object

# Create a new geometry node tree
geometry_node_tree = bpy.data.node_groups.new(name="Geometry Nodes", type='GeometryNodeTree')

# Get the geometry node tree
tree = geometry_node_tree.nodes

# Create the nodes
group_input_node = tree.new(type="NodeGroupInput")
group_input_node.location = (-200,0)
group_output_node = tree.new(type="NodeGroupOutput")
group_output_node.location = (400,0)
mesh_to_volume_node = tree.new(type="GeometryNodeMeshToVolume")
mesh_to_volume_node.location = (0,0)

# Set the settings for the nodes
mesh_to_volume_node.resolution_mode = 'VOXEL_SIZE'

# Connect the nodes
from_node_1 = group_input_node
to_node_1 = mesh_to_volume_node
geometry_node_tree.links.new(from_node_1.outputs[0], to_node_1.inputs["Mesh"])

from_node_2 = mesh_to_volume_node
to_node_2 = group_output_node
geometry_node_tree.links.new(from_node_2.outputs["Volume"], to_node_2.inputs[0])

# Create a new geometry nodes modifier and assign the node tree
geometry_nodes_modifier = selected_obj.modifiers.new(name="Geometry Nodes", type='NODES')
geometry_nodes_modifier.node_group = geometry_node_tree

The result I'm getting is this:

enter image description here

The problem is, as far as I understand it, that in comparison to usual Nodes like the Mesh to Volume Node, the Group Input and Group Output-Nodes determines its data type dynamically after being connected. If I connect them the way I did, their data type is empty and the connection to the Mesh to Volume Node doesn't work. Is there any way of determining the Input/Output type of those Nodes to Geometry?

Thanks in advance!

$\endgroup$

1 Answer 1

1
$\begingroup$

You have to create group inputs and outputs (at the node tree level) before assigning them.

For instance for the inputs (geometry):

geometry_node_tree.inputs.new("NodeSocketGeometry", "name")

and for the outputs (float):

geometry_node_tree.outputs.new("NodeSocketFloat", "name")

After that you can link as you did. For instance:

geometry_node_tree.links.new(from_node_1.outputs[0], to_node_1.inputs["Mesh"])

All the node socket types are in the list here (search in the page for "NodeSocket" to find the first one).

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .