more robust reorder

main
pullusb 2025-04-10 15:53:25 +02:00
parent cc1646081b
commit d2a19ae9e3
1 changed files with 25 additions and 3 deletions

28
fn.py
View File

@ -739,9 +739,31 @@ def clear_disconnected(fo):
fo.inputs.remove(inp) fo.inputs.remove(inp)
def get_pairs(ng, fo): def get_pairs(ng, fo):
## pairs of connected socket indices (from nodegroup to fileout), ex: [(0, 3), (1, 0), (2, 1), (3, 2)] '''return a list of tuple, pairs of connected socket indices
pairs = [(si, next((i for i, inp in enumerate(fo.inputs) if inp == o.links[0].to_socket), None)) from nodegroup to fileout. ex: [(0, 3), (1, 0), (2, 1), (3, 2)]
for si, o in enumerate(ng.outputs) if o.is_linked and o.links[0].to_node == fo] '''
## 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 return pairs
def reorder_fileout(fo, ng=None): def reorder_fileout(fo, ng=None):