change clean method based on connection order

main
pullusb 2025-04-10 15:07:18 +02:00
parent af2d8e1ad4
commit 26bcb56815
1 changed files with 18 additions and 4 deletions

22
fn.py
View File

@ -738,6 +738,12 @@ def clear_disconnected(fo):
print(f'Deleting unlinked fileout slot: {inp.name}') print(f'Deleting unlinked fileout slot: {inp.name}')
fo.inputs.remove(inp) 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): def reorder_fileout(fo, ng=None):
if not ng: # get connected nodegroup if not ng: # get connected nodegroup
for s in fo.inputs: for s in fo.inputs:
@ -747,10 +753,18 @@ def reorder_fileout(fo, ng=None):
if not ng: if not ng:
print(f'No nodegroup to refer to filter {fo.name}') print(f'No nodegroup to refer to filter {fo.name}')
return 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: ## Order based on sockets indices
all_outnames = [s.name for s in fo.inputs] # same as [fs.path for fs in fo.file_slots] # for _ in range(len(ng.outputs)):
fo.inputs.move(all_outnames.index(s_name), ordered.index(s_name)) 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): def reorganise_NG_nodegroup(ng):
'''refit node content to avoid overlap''' '''refit node content to avoid overlap'''