Fix hierarchical autocomplete: select directory segments, not files; stop when direction subdir is found
This commit is contained in:
@@ -16,22 +16,57 @@ def _discover_directories():
|
||||
return []
|
||||
|
||||
candidates = set()
|
||||
for root, subdirs, _ in os.walk(base_dir, followlinks=True):
|
||||
|
||||
for root, dirs, files in os.walk(base_dir, followlinks=True):
|
||||
rel = os.path.relpath(root, base_dir)
|
||||
subdirs_lower = {s.lower() for s in subdirs}
|
||||
if VALID_DIRECTIONS & subdirs_lower or VALID_MODALITIES & subdirs_lower:
|
||||
if rel == ".":
|
||||
continue
|
||||
if rel == ".":
|
||||
continue
|
||||
|
||||
dirs_lower = {d.lower() for d in dirs}
|
||||
|
||||
if VALID_DIRECTIONS & dirs_lower:
|
||||
candidates.add(rel)
|
||||
elif VALID_MODALITIES & dirs_lower:
|
||||
candidates.add(rel)
|
||||
|
||||
return sorted(candidates)
|
||||
|
||||
|
||||
def _resolve_target_dir(base_dir, directory, direction):
|
||||
if directory and (not isinstance(directory, str) or directory.strip()):
|
||||
path = os.path.join(base_dir, directory)
|
||||
def _discover_children(parent_rel):
|
||||
base_dir = folder_paths.get_input_directory()
|
||||
if parent_rel:
|
||||
parent_abs = os.path.join(base_dir, parent_rel)
|
||||
else:
|
||||
path = base_dir
|
||||
parent_abs = base_dir
|
||||
|
||||
if not os.path.isdir(parent_abs):
|
||||
return []
|
||||
|
||||
children = set()
|
||||
try:
|
||||
for name in os.listdir(parent_abs):
|
||||
full = os.path.join(parent_abs, name)
|
||||
if os.path.isdir(full):
|
||||
rel = os.path.relpath(full, base_dir)
|
||||
children.add(rel)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
my_children = set()
|
||||
for child in children:
|
||||
child_parts = child.split("/")
|
||||
parent_parts = parent_rel.split("/") if parent_rel else []
|
||||
if len(child_parts) == len(parent_parts) + 1:
|
||||
my_children.add(child_parts[-1])
|
||||
|
||||
return sorted(my_children)
|
||||
|
||||
|
||||
def _resolve_target_dir(base_dir, directory, direction):
|
||||
if not directory or not (isinstance(directory, str) and directory.strip()):
|
||||
raise ValueError("directory must be a non-empty string")
|
||||
|
||||
path = os.path.join(base_dir, directory.strip())
|
||||
|
||||
if direction and direction.strip():
|
||||
path = os.path.join(path, direction.strip())
|
||||
@@ -42,7 +77,8 @@ def _resolve_target_dir(base_dir, directory, direction):
|
||||
def _list_image_files(target_dir):
|
||||
try:
|
||||
files = [
|
||||
f for f in sorted(os.listdir(target_dir))
|
||||
f
|
||||
for f in sorted(os.listdir(target_dir))
|
||||
if os.path.isfile(os.path.join(target_dir, f))
|
||||
and os.path.splitext(f)[1].lower() in SUPPORTED_EXTENSIONS
|
||||
]
|
||||
@@ -58,14 +94,12 @@ def _resize_image(image, target_w, target_h):
|
||||
return image, orig_w, orig_h
|
||||
|
||||
if target_w > 0 and target_h == 0:
|
||||
fw = target_w
|
||||
fh = max(1, int(orig_h * (target_w / orig_w)))
|
||||
return image.resize((fw, fh), Image.Resampling.LANCZOS), fw, fh
|
||||
return image.resize((target_w, fh), Image.Resampling.LANCZOS), target_w, fh
|
||||
|
||||
if target_h > 0 and target_w == 0:
|
||||
fh = target_h
|
||||
fw = max(1, int(orig_w * (target_h / orig_h)))
|
||||
return image.resize((fw, fh), Image.Resampling.LANCZOS), fw, fh
|
||||
return image.resize((fw, target_h), Image.Resampling.LANCZOS), fw, target_h
|
||||
|
||||
scale = max(target_w / orig_w, target_h / orig_h)
|
||||
new_w = max(1, int(orig_w * scale))
|
||||
@@ -98,7 +132,9 @@ class CompassImageLoader:
|
||||
RETURN_NAMES = ("IMAGE", "path", "width", "height", "frame_count")
|
||||
FUNCTION = "load_images"
|
||||
|
||||
def load_images(self, directory, direction, modality, frame=None, width=0, height=0):
|
||||
def load_images(
|
||||
self, directory, direction, modality, frame=None, width=0, height=0
|
||||
):
|
||||
base_dir = folder_paths.get_input_directory()
|
||||
target_dir = _resolve_target_dir(base_dir, directory, direction)
|
||||
modality_path = os.path.join(target_dir, modality)
|
||||
@@ -110,7 +146,6 @@ class CompassImageLoader:
|
||||
if not files:
|
||||
raise RuntimeError(f"No images found in: {modality_path}")
|
||||
|
||||
# Frame selection
|
||||
if frame is None or str(frame).strip() == "":
|
||||
selected_files = files
|
||||
output_path = modality_path
|
||||
@@ -131,7 +166,6 @@ class CompassImageLoader:
|
||||
selected_files = [files[index]]
|
||||
output_path = os.path.join(modality_path, files[index])
|
||||
|
||||
# Load and process images
|
||||
tensors = []
|
||||
final_w, final_h = 0, 0
|
||||
|
||||
@@ -150,7 +184,9 @@ class CompassImageLoader:
|
||||
return (image_batch, output_path, final_w, final_h, len(selected_files))
|
||||
|
||||
@classmethod
|
||||
def IS_CHANGED(cls, directory, direction, modality, frame=None, width=0, height=0):
|
||||
def IS_CHANGED(
|
||||
cls, directory, direction, modality, frame=None, width=0, height=0
|
||||
):
|
||||
import hashlib
|
||||
|
||||
base_dir = folder_paths.get_input_directory()
|
||||
@@ -162,7 +198,9 @@ class CompassImageLoader:
|
||||
|
||||
files = _list_image_files(modality_path)
|
||||
m = hashlib.sha256()
|
||||
m.update(f"{directory}|{direction}|{modality}|{frame}|{width}|{height}".encode())
|
||||
m.update(
|
||||
f"{directory}|{direction}|{modality}|{frame}|{width}|{height}".encode()
|
||||
)
|
||||
|
||||
if frame is None or str(frame).strip() == "":
|
||||
for f in files:
|
||||
|
||||
Reference in New Issue
Block a user