""" H3RefModLoader — load one RefMod and optionally append it to another """ from __future__ import annotations from typing import Any, Dict, List, Tuple import os import folder_paths import server from ..py.refmod_core import H3RefMod, load_refmods_from_file, read_refmod_meta, refmod_capabilities from ..py.refmod_common import ( _prompt_hint, ) from ..py.refmod_common import ( refmods_dir, refmods_dirs, ) _MOD_CACHE: Dict[str, Tuple[H3RefMod, ...]] = {} _MOD_CACHE_MAX = 24 # cap: never pin more mods in RAM than this (FIFO eviction) _MOD_LIST_CACHE_KEY = None # (dirs, mtimes, sizes) signature of the last _list_mod_names() scan _MOD_LIST_CACHE_VAL = None _MOD_PATH_CACHE_KEY = None _MOD_PATH_CACHE_VAL: Dict[str, str] = {} _MOD_SKIP_DIRS = {"graph_presets", ".git", "__pycache__"} _MAX_WEIGHT = 10.0 # Mod storage lives in ComfyUI's models/ tree (created on first run) and is # registered as a first-class folder type so it shows up next to loras/unet. try: folder_paths.add_model_folder_path("refmods", refmods_dir()) except Exception: pass # ═══════════════════════════════════════════════════════════════════════════ # Mods folder helpers # ═══════════════════════════════════════════════════════════════════════════ def _iter_mod_paths(base_dir: str): """Yield ``(relative_stem, absolute_stem)`` for RefMods under ``base_dir``.""" for root, dirnames, filenames in os.walk(base_dir): dirnames[:] = sorted(d for d in dirnames if d not in _MOD_SKIP_DIRS) for fn in sorted(filenames): if not fn.endswith(".safetensors"): continue abs_stem = os.path.join(root, fn[:-len(".safetensors")]) rel_stem = os.path.relpath(abs_stem, base_dir).replace(os.sep, "/") yield rel_stem, abs_stem def _mod_scan_signature() -> tuple[str, List[str]]: mods_dirs = refmods_dirs() sig = [] for mods_dir in mods_dirs: for rel_stem, abs_stem in _iter_mod_paths(mods_dir): try: st = os.stat(abs_stem + ".safetensors") sig.append(f"{mods_dir}:{rel_stem}:{st.st_size}:{int(st.st_mtime)}") except OSError: pass return "\n".join(sig), mods_dirs def _root_display_labels(roots: List[str]) -> List[str]: labels: List[str] = [] counts: Dict[str, int] = {} for index, root in enumerate(roots, start=1): base = os.path.basename(os.path.normpath(root)) or f"root{index}" count = counts.get(base, 0) + 1 counts[base] = count labels.append(base if count == 1 else f"{base}#{count}") return labels def _refresh_mod_lookup() -> Dict[str, str]: global _MOD_LIST_CACHE_KEY, _MOD_LIST_CACHE_VAL, _MOD_PATH_CACHE_KEY, _MOD_PATH_CACHE_VAL key, mods_dirs = _mod_scan_signature() if key == _MOD_LIST_CACHE_KEY and key == _MOD_PATH_CACHE_KEY and _MOD_LIST_CACHE_VAL is not None: return _MOD_PATH_CACHE_VAL names_by_rel: Dict[str, List[Tuple[int, str]]] = {} for index, mods_dir in enumerate(mods_dirs): for rel_stem, abs_stem in _iter_mod_paths(mods_dir): meta = read_refmod_meta(abs_stem) if meta is None or meta.get("kind") not in ("image", "video", "audio", "bundle"): continue names_by_rel.setdefault(rel_stem, []).append((index, abs_stem)) labels = _root_display_labels(mods_dirs) lookup: Dict[str, str] = {} names: List[str] = [] for rel_stem in sorted(names_by_rel, key=str.lower): matches = names_by_rel[rel_stem] first_index, first_path = matches[0] lookup[rel_stem] = first_path names.append(rel_stem) for dup_index, dup_path in matches[1:]: display_name = f"[{labels[dup_index]}] {rel_stem}" suffix = 2 while display_name in lookup: display_name = f"[{labels[dup_index]} #{suffix}] {rel_stem}" suffix += 1 lookup[display_name] = dup_path names.append(display_name) _MOD_LIST_CACHE_KEY = key _MOD_LIST_CACHE_VAL = names _MOD_PATH_CACHE_KEY = key _MOD_PATH_CACHE_VAL = lookup return lookup def _list_mod_names() -> List[str]: """Available RefMod names across the search dirs (for the loader dropdown). Only entries with valid RefMod metadata (embedded in the safetensors header or a legacy sidecar .json) are listed, so other mod formats in models/mods/ (e.g. LTXMod files) don't show up. Called by INPUT_TYPES/VALIDATE_INPUTS on every prompt validation, so the result is cached until any mod file appears/disappears/changes (checked via cheap os.stat, not by re-reading every safetensors header). """ _refresh_mod_lookup() return _MOD_LIST_CACHE_VAL def _find_mod_path(name: str) -> str: lookup = _refresh_mod_lookup() if name in lookup: return lookup[name] mods_dirs = refmods_dirs() raise FileNotFoundError( f"RefMod '{name}' not found. Searched:\n" + "\n".join(f" - {d}/{name}.safetensors" for d in mods_dirs)) def _load_mods(name: str) -> List[H3RefMod]: if name in _MOD_CACHE: return list(_MOD_CACHE[name]) path = _find_mod_path(name) mods = tuple(load_refmods_from_file(path, device="cpu")) _MOD_CACHE[name] = mods if len(_MOD_CACHE) > _MOD_CACHE_MAX: _MOD_CACHE.pop(next(iter(_MOD_CACHE))) return list(mods) def _load_mod(name: str) -> H3RefMod: mods = _load_mods(name) for mod in mods: if mod.kind != "audio": return mod return mods[0] def _mod_capabilities(name: str) -> Dict[str, bool]: path = _find_mod_path(name) meta = read_refmod_meta(path) if meta is None: return {"has_visual": True, "has_audio": False} caps = refmod_capabilities(meta) return { "has_visual": bool(caps.get("has_visual", False)), "has_audio": bool(caps.get("has_audio", False)), } def _normalize_weight(weight: float) -> float: return min(_MAX_WEIGHT, max(0.0, float(weight))) def _expand_weight(weight: float) -> List[float]: clipped = _normalize_weight(weight) whole = int(clipped) remainder = clipped - whole strengths = [1.0] * whole if remainder > 1e-6: strengths.append(remainder) return strengths def _append_weighted_mod(rows: List[tuple[H3RefMod, float]], mod: H3RefMod, weight: float) -> float: clipped = _normalize_weight(weight) rows.extend((mod, strength) for strength in _expand_weight(clipped)) return clipped def _weight_display(weight: float) -> str: clipped = _normalize_weight(weight) whole = int(clipped) remainder = clipped - whole if whole <= 0: return f"{clipped:.2f}" if remainder <= 1e-6: return f"x{whole}" return f"x{whole} + {remainder:.2f}" class H3RefModLoader: """Load one RefMod and append it to an existing bundle.""" @classmethod def INPUT_TYPES(cls): return { "required": { "mod": (_list_mod_names(), {"tooltip": "The RefMod to load."}), "weight": ("FLOAT", {"default": 1.0, "min": 0.0, "max": _MAX_WEIGHT, "step": 0.01, "display": "number", "tooltip": "Legacy unified weight control kept for workflow compatibility. New graphs should use video_weight and audio_weight."}), "video_weight": ("FLOAT", {"default": 1.0, "min": 0.0, "max": _MAX_WEIGHT, "step": 0.01, "display": "number", "tooltip": "Unified video weight control. 0 skips video. 0..1 behaves like the old strength control. Values above 1 repeat the same visual RefMod as extra copies."}), "audio_weight": ("FLOAT", {"default": 1.0, "min": 0.0, "max": _MAX_WEIGHT, "step": 0.01, "display": "number", "tooltip": "Unified audio weight control. 0 skips audio. 0..1 behaves like the old strength control. Values above 1 repeat the same audio RefMod as extra copies."}), }, "optional": { "mods": ("H3_REF_MODS",), }, } RETURN_TYPES = ("H3_REF_MODS", "STRING") RETURN_NAMES = ("mods", "prompt_hint") FUNCTION = "load" CATEGORY = "H3RefModPicker" @classmethod def VALIDATE_INPUTS(cls, mod, **kwargs): if mod not in set(_list_mod_names()): return f"RefMod '{mod}' not found in any configured refmods folder. Run Create H3 RefMod first." return True def load(self, mod, weight=1.0, video_weight=1.0, audio_weight=1.0, mods=None, strength=None, audio_strength=None): rows = list(mods) if mods is not None else [] if strength is not None: weight = strength if audio_strength is not None: audio_weight = audio_strength if strength is not None or (weight != 1.0 and video_weight == 1.0 and audio_weight == 1.0): video_weight = weight audio_weight = weight loaded = _load_mods(mod) summaries = [] hint_rows = list(mods) if mods is not None else [] for item in loaded: current_weight = audio_weight if item.kind == "audio" else video_weight clipped = _append_weighted_mod(rows, item, current_weight) summaries.append(f"{item.name}({_weight_display(clipped)})") if clipped > 0.0: hint_rows.append((item, min(1.0, clipped))) print(f"[H3RefModLoader] {mod}: " + ", ".join(summaries)) hint = _prompt_hint(hint_rows) if hint: print(f"[H3RefModLoader] prompt_hint: {hint}") return (rows, hint) NODE_CLASS_MAPPINGS = { "H3RefModLoader": H3RefModLoader } NODE_DISPLAY_NAME_MAPPINGS = { "H3RefModLoader": "Load H3 RefMod Simple" } @server.PromptServer.instance.routes.get("/h3refmods/refmod-loader/capabilities") async def refmod_loader_capabilities(request): try: name = str(request.query.get("mod", "") or "").strip() if not name: return server.web.json_response({"ok": True, "has_visual": True, "has_audio": True}) caps = _mod_capabilities(name) return server.web.json_response({"ok": True, **caps}) except Exception as exc: return server.web.json_response({"ok": False, "error": str(exc)}, status=500)