I am trying to access the values of the built-in nodes input and output sockets. I am using the .inputs and .outputs properties, but am running into issues if I try to access these by name (vs. by index). Some nodes have unique names for each input and output socket. But some don’t.
Here is my sample node tree:
Here is some code I have to get a list of the input and output socket names of the nodes in my node tree (I am mostly looking at built in nodes here):
import bpy
def debug(node):
print()
print(node.type)
for input in node.inputs:
print(input.name, ">>", input.type)
for output in node.outputs:
print(output.name, ">>", output.type)
NodeTree = "Geometry Nodes" # This would have to be set to your actual node tree name
node_group = bpy.data.node_groups[NodeTree]
for node in node_group.nodes:
debug(node)
The result for the REALIZE_INSTANCES node has unique names for the inputs and outputs:
REALIZE_INSTANCES
Geometry >> GEOMETRY
Selection >> BOOLEAN
Realize All >> BOOLEAN
Depth >> INT
Preserve Normals >> BOOLEAN
Geometry >> GEOMETRY
But the MATH node does not:
MATH
Value >> VALUE
Value >> VALUE
Value >> VALUE
Value >> VALUE
So, if I want to access the default value and whether a socket is linked using these names, I use a script something like this:
import bpy
def debug(node, socket_name):
print()
print(node.type)
print(node.inputs[socket_name].default_value)
print(node.inputs[socket_name].is_linked)
def loop_through_nodes(node_tree, node_type, socket_name):
node_group = bpy.data.node_groups[node_tree]
for node in node_group.nodes:
if node.type == node_type:
debug(node, socket_name)
NODE_TREE = "Geometry Nodes"
NODE_TYPE = "REALIZE_INSTANCES"
SOCKET_NAME = "Depth"
loop_through_nodes(NODE_TREE, NODE_TYPE, SOCKET_NAME)
NODE_TYPE = "MATH"
SOCKET_NAME = "Value"
loop_through_nodes(NODE_TREE, NODE_TYPE, SOCKET_NAME)
Which gives me this result:
REALIZE_INSTANCES
2
False
MATH
2.0
False
How would I access the specific input sockets on the MATH node if they don’t have unique names? I know I can use index values like the following:
print(node.inputs[2].default_value)
print(node.inputs[2].is_linked)
But that seems kind of fragile. I have read about the .interface.items_tree but as far as I can tell, that only applies to node groups, and not individual, built-in nodes.
Thanks for taking the time to look at this. Any help (even a nudge in the right direction) is appreciated.
