From d2a19ae9e3bd73e560ddeb844b35f8811cbec602 Mon Sep 17 00:00:00 2001 From: pullusb Date: Thu, 10 Apr 2025 15:53:25 +0200 Subject: [PATCH] more robust reorder --- fn.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/fn.py b/fn.py index 10a68a5..adcc188 100644 --- a/fn.py +++ b/fn.py @@ -739,9 +739,31 @@ def clear_disconnected(fo): fo.inputs.remove(inp) def get_pairs(ng, fo): - ## pairs of connected socket indices (from nodegroup to fileout), ex: [(0, 3), (1, 0), (2, 1), (3, 2)] - pairs = [(si, next((i for i, inp in enumerate(fo.inputs) if inp == o.links[0].to_socket), None)) - for si, o in enumerate(ng.outputs) if o.is_linked and o.links[0].to_node == fo] + '''return a list of tuple, pairs of connected socket indices + from nodegroup to fileout. ex: [(0, 3), (1, 0), (2, 1), (3, 2)] + ''' + + ## Following version works well only if ng and fo have same number of sockets and all are connected. + # pairs = [(si, next((i for i, inp in enumerate(fo.inputs) if inp == o.links[0].to_socket), None)) + # for si, o in enumerate(ng.outputs) if o.is_linked and o.links[0].to_node == fo] + # return pairs + + ## A bit more robust : Do not count the unlinked socket + pairs = [] + str_ct = 0 + for si, o in enumerate(ng.outputs): + if o.is_linked and o.links[0].to_node == fo: + + dest_ct = 0 + for j, inp in enumerate(fo.inputs): + if inp.is_linked and inp.links[0].from_node == ng: + if inp == o.links[0].to_socket: + pairs.append((str_ct, dest_ct)) + dest_ct += 1 + # not incremented on non linked socket + + str_ct += 1 + ## not incremented on non-linked socket return pairs def reorder_fileout(fo, ng=None):