From 26bcb5681505ef3c953e95db08c0022d9d2cd809 Mon Sep 17 00:00:00 2001 From: pullusb Date: Thu, 10 Apr 2025 15:07:18 +0200 Subject: [PATCH] change clean method based on connection order --- fn.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/fn.py b/fn.py index 2e7a647..10a68a5 100644 --- a/fn.py +++ b/fn.py @@ -738,6 +738,12 @@ def clear_disconnected(fo): print(f'Deleting unlinked fileout slot: {inp.name}') 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 pairs + def reorder_fileout(fo, ng=None): if not ng: # get connected nodegroup for s in fo.inputs: @@ -747,10 +753,18 @@ def reorder_fileout(fo, ng=None): if not ng: print(f'No nodegroup to refer to filter {fo.name}') return - ordered = [o.links[0].to_socket.name for o in ng.outputs if o.is_linked and o.is_linked and o.links[0].to_node == fo] - for s_name in ordered: - all_outnames = [s.name for s in fo.inputs] # same as [fs.path for fs in fo.file_slots] - fo.inputs.move(all_outnames.index(s_name), ordered.index(s_name)) + + ## Order based on sockets indices + # for _ in range(len(ng.outputs)): + for _ in range(len(fo.inputs)): + # print(f'reorder iteration {_}') + pairs = get_pairs(ng, fo) # build index pairs + pair = next((p for p in pairs if p[0] is not None and p[1] is not None and p[0] != p[1]), None) + if pair is None: + ## if pair is None, we have iterated over all pairs without index difference (ordered) + break + print(pair) + fo.inputs.move(pair[1], pair[0]) def reorganise_NG_nodegroup(ng): '''refit node content to avoid overlap'''