4
$\begingroup$

I am trying to remove the unconnected sockets of Group Output node using python, I tried this -

import bpy

obj = bpy.context.active_object
for mat in obj.data.materials:
    node_tree = mat.node_tree.nodes
    for node in node_tree:
        if node.name.startswith('Textures'):
            group_nodes = node.node_tree.nodes
            for node in group_nodes:
                if node.type == 'GROUP_OUTPUT':
                    output_node = node
                    sockets = output_node.inputs
                    for socket in sockets:
                        if socket.is_linked == False:
                            sockets.remove(socket)

But I am getting this error - RuntimeError: Error: Unable to remove socket from built-in node If anyone can help explain me why this method does not work, and how to remove it

Screenshot

$\endgroup$
1

1 Answer 1

4
$\begingroup$

With the Blender 4.0 they made a changed they way how node sockets can be added removed, linked etc. - https://developer.blender.org/docs/release_notes/4.0/python_api/#node-groups

import bpy

for node_group in bpy.data.node_groups:
    if "Textures" in node_group.name:
        group_output = next((node for node in node_group.nodes 
                                if node.type == 'GROUP_OUTPUT'), None)

        if group_output:
            # Collect names of unlinked inputs, skipping empty names
            unlinked_socket_names = [
                socket.name
                for socket in group_output.inputs
                if not socket.links and socket.name.strip() != ""
            ]

            print(f"Unlinked sockets in '{node_group.name}': {unlinked_socket_names}")
        else:
            print(f"No Group Output node found in '{node_group.name}'")

        #Remove unsed sockets
        for item in list(node_group.interface.items_tree):
            if (item.item_type == 'SOCKET' and item.in_out == 'OUTPUT' and item.name in unlinked_socket_names):
                node_group.interface.remove(item)
$\endgroup$
1
  • 1
    $\begingroup$ Glad you found the solution. Thank you for sharing :) $\endgroup$ Commented Feb 23 at 14:07

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.