git clone https://git.lucas.co/hou-control.git
hc: ensure select next/prev component falls back to top-left when no selection exists
python3.11libs/hc/hcnetworkeditor.py | 35 ++++++++++++++++++++---------------
1 file changed, 20 insertions(+), 15 deletions(-)
diff --git a/python3.11libs/hc/hcnetworkeditor.py b/python3.11libs/hc/hcnetworkeditor.py
index 4eb4a96..a0ad466 100644
--- a/python3.11libs/hc/hcnetworkeditor.py
+++ b/python3.11libs/hc/hcnetworkeditor.py
@@ -322,8 +322,8 @@ class HCNetworkEditor(HCPathTab):
def selectComponent(self, direction):
"""Select the connected component closest to the current selection in
the given spatial direction. direction is 'next' (to the right) or
- 'prev' (to the left). When two components share the same center x, the
- one with the higher y wins the tiebreaker."""
+ 'prev' (to the left). If no selection exists, defaults to the
+ top-left most component."""
components = self.connectedComponents()
if not components:
return
@@ -333,26 +333,31 @@ class HCNetworkEditor(HCPathTab):
ys = [n.position()[1] for n in comp]
cx = (min(xs) + max(xs)) / 2.0
cy = (min(ys) + max(ys)) / 2.0
- # Negate cy so higher y sorts earlier in ascending order.
+ # Negate cy so higher y (top) sorts earlier in ascending order.
+ # Ascending cx (left) sorts earlier.
return (cx, -cy)
- current = self.hou_tab.currentNode()
selected_set = set(self.hou_tab.pwd().selectedChildren())
ref_comp = None
- if current is not None:
- for comp in components:
- if current in comp:
- ref_comp = comp
- break
- if ref_comp is None and selected_set:
- for comp in components:
- if any(n in selected_set for n in comp):
- ref_comp = comp
- break
+
+ # Only try to find a reference component if we have a selection
+ if selected_set:
+ current = self.hou_tab.currentNode()
+ if current is not None:
+ for comp in components:
+ if current in comp:
+ ref_comp = comp
+ break
+ if ref_comp is None:
+ for comp in components:
+ if any(n in selected_set for n in comp):
+ ref_comp = comp
+ break
if ref_comp is None:
+ # Sort all components and pick the top-left one (first in sorted list)
components.sort(key=sort_key)
- target = components[0] if direction == 'next' else components[-1]
+ target = components[0]
else:
ref_key = sort_key(ref_comp)
if direction == 'next':