268 lines
9.4 KiB
Python
268 lines
9.4 KiB
Python
"""DUMAS H3 RefMod browser and picker.
|
|
|
|
The picker consumes the ``H3_REF_MODS`` type from the maintained MiniMax H3
|
|
RefMod implementation. It intentionally does not duplicate the upstream
|
|
extract/apply implementation; it only discovers files, presents them safely,
|
|
and loads the selected RefMod into a compatible bundle.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
from typing import Dict, List
|
|
|
|
import folder_paths
|
|
|
|
|
|
_SKIP_DIRS = {".git", "__pycache__", "graph_presets"}
|
|
_MAX_WEIGHT = 10.0
|
|
_CACHE = {}
|
|
|
|
|
|
def _roots() -> List[str]:
|
|
roots = []
|
|
try:
|
|
roots.extend(folder_paths.get_folder_paths("refmods"))
|
|
except Exception:
|
|
pass
|
|
fallback = os.path.join(folder_paths.models_dir, "refmods")
|
|
roots.append(fallback)
|
|
result = []
|
|
seen = set()
|
|
for root in roots:
|
|
root = os.path.realpath(os.path.abspath(root))
|
|
if root not in seen and os.path.isdir(root):
|
|
seen.add(root)
|
|
result.append(root)
|
|
return result
|
|
|
|
|
|
def register_refmod_root(path: str) -> str:
|
|
"""Register an explicitly selected external folder for this process."""
|
|
root = os.path.realpath(os.path.abspath(str(path or "").strip()))
|
|
if not os.path.isdir(root):
|
|
raise ValueError(f"RefMod folder does not exist: {root}")
|
|
try:
|
|
folder_paths.add_model_folder_path("refmods", root)
|
|
except Exception:
|
|
pass
|
|
return root
|
|
|
|
|
|
def _inside(path: str, root: str) -> bool:
|
|
try:
|
|
return os.path.commonpath((os.path.realpath(path), os.path.realpath(root))) == os.path.realpath(root)
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def _safe_file(path: str) -> str:
|
|
raw = os.path.abspath(os.path.expanduser(str(path or "").strip()))
|
|
for root in _roots():
|
|
if _inside(raw, root) and os.path.isfile(raw):
|
|
return os.path.realpath(raw)
|
|
raise ValueError("RefMod path is not inside a registered refmods folder.")
|
|
|
|
|
|
def _read_meta(path_no_ext: str):
|
|
try:
|
|
from safetensors import safe_open
|
|
with safe_open(path_no_ext + ".safetensors", framework="pt") as handle:
|
|
metadata = handle.metadata() or {}
|
|
for key in ("refmod_meta", "audio_refmod_meta"):
|
|
if key in metadata:
|
|
return json.loads(metadata[key])
|
|
except Exception:
|
|
return None
|
|
return None
|
|
|
|
|
|
def _capabilities(meta):
|
|
if not isinstance(meta, dict):
|
|
return {"kind": "unknown", "has_visual": False, "has_audio": False}
|
|
kind = str(meta.get("kind", "unknown"))
|
|
if kind == "bundle":
|
|
members = meta.get("members") or []
|
|
return {"kind": kind,
|
|
"has_visual": any(x.get("kind") in ("image", "video") for x in members if isinstance(x, dict)),
|
|
"has_audio": any(x.get("kind") == "audio" for x in members if isinstance(x, dict))}
|
|
if kind in ("image", "video"):
|
|
return {"kind": kind, "has_visual": True,
|
|
"has_audio": int(meta.get("ref_audio_t", 0) or 0) > 0}
|
|
return {"kind": kind, "has_visual": False, "has_audio": kind == "audio"}
|
|
|
|
|
|
def _preview(path_no_ext):
|
|
stem = os.path.basename(path_no_ext)
|
|
for suffix in ("_visual", "_Visual", "_video", "_Video", "_audio", "_Audio"):
|
|
if stem.endswith(suffix):
|
|
stem = stem[:-len(suffix)]
|
|
break
|
|
directory = os.path.dirname(path_no_ext)
|
|
for ext in (".png", ".jpg", ".jpeg", ".webp"):
|
|
candidate = os.path.join(directory, stem + ext)
|
|
if os.path.isfile(candidate):
|
|
return candidate
|
|
return None
|
|
|
|
|
|
def _list_entries(current):
|
|
dirs, mods = [], []
|
|
for entry in sorted(os.scandir(current), key=lambda item: item.name.lower()):
|
|
if entry.name.startswith(".") or entry.name in _SKIP_DIRS:
|
|
continue
|
|
try:
|
|
if entry.is_dir(follow_symlinks=False):
|
|
dirs.append({"name": entry.name, "path": os.path.realpath(entry.path)})
|
|
elif entry.is_file(follow_symlinks=False) and entry.name.lower().endswith(".safetensors"):
|
|
stem = entry.path[:-len(".safetensors")]
|
|
meta = _read_meta(stem)
|
|
caps = _capabilities(meta)
|
|
if caps["kind"] not in ("image", "video", "audio", "bundle"):
|
|
continue
|
|
mods.append({
|
|
"name": entry.name,
|
|
"path": os.path.realpath(entry.path),
|
|
"preview_path": _preview(stem),
|
|
**caps,
|
|
"description": str((meta or {}).get("description", "") or ""),
|
|
"concept_type": str((meta or {}).get("concept_type", "generic") or "generic"),
|
|
})
|
|
except OSError:
|
|
continue
|
|
return dirs, mods
|
|
|
|
|
|
def browser_root():
|
|
roots = _roots()
|
|
if not roots:
|
|
root = os.path.realpath(os.path.join(folder_paths.models_dir, "refmods"))
|
|
os.makedirs(root, exist_ok=True)
|
|
return root
|
|
return roots[0]
|
|
|
|
|
|
def list_browser_dir(path=""):
|
|
roots = _roots()
|
|
if not roots:
|
|
root = browser_root()
|
|
roots = [root]
|
|
current = browser_root() if not path else _safe_directory(path)
|
|
if os.path.realpath(current) in {os.path.realpath(x) for x in roots}:
|
|
dirs, mods = [], []
|
|
for root in roots:
|
|
d, m = _list_entries(root)
|
|
dirs.extend(d)
|
|
mods.extend(m)
|
|
parent = None
|
|
current = browser_root()
|
|
else:
|
|
dirs, mods = _list_entries(current)
|
|
parent_candidate = os.path.dirname(current.rstrip(os.sep))
|
|
parent = parent_candidate if any(_inside(parent_candidate, root) for root in roots) else None
|
|
return {"root": browser_root(), "current_path": current, "parent_path": parent,
|
|
"dirs": dirs, "mods": mods}
|
|
|
|
|
|
def _safe_directory(path):
|
|
raw = os.path.realpath(os.path.abspath(os.path.expanduser(str(path or ""))))
|
|
if any(_inside(raw, root) for root in _roots()) and os.path.isdir(raw):
|
|
return raw
|
|
raise ValueError("RefMod folder is not inside a registered refmods root.")
|
|
|
|
|
|
def _find_loader_api():
|
|
"""Find the maintained RefMod reader already loaded by ComfyUI."""
|
|
for module in list(sys.modules.values()):
|
|
if module is None:
|
|
continue
|
|
loader = getattr(module, "load_refmods_from_file", None)
|
|
if callable(loader):
|
|
return loader
|
|
raise RuntimeError(
|
|
"No MiniMax H3 RefMod reader is loaded. Install/load the maintained "
|
|
"MiniMax H3 RefMod node pack alongside DumasNodes."
|
|
)
|
|
|
|
|
|
def _expand_weight(weight):
|
|
value = min(_MAX_WEIGHT, max(0.0, float(weight)))
|
|
whole = int(value)
|
|
remainder = round(value - whole, 6)
|
|
result = [1.0] * whole
|
|
if remainder > 1e-6:
|
|
result.append(remainder)
|
|
return result
|
|
|
|
|
|
def _hint(rows):
|
|
return "; ".join(
|
|
f"{getattr(mod, 'concept_type', 'generic')}: {getattr(mod, 'description', '')}"
|
|
for mod, strength in rows if strength > 0 and getattr(mod, "description", "")
|
|
)
|
|
|
|
|
|
class DumasH3RefModPicker:
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"mod_path": ("STRING", {"default": "", "tooltip": "Choose a RefMod from the DUMAS browser."}),
|
|
"video_weight": ("FLOAT", {"default": 1.0, "min": 0.0, "max": _MAX_WEIGHT, "step": 0.01}),
|
|
"audio_weight": ("FLOAT", {"default": 1.0, "min": 0.0, "max": _MAX_WEIGHT, "step": 0.01}),
|
|
},
|
|
"optional": {"mods": ("H3_REF_MODS",)},
|
|
}
|
|
|
|
RETURN_TYPES = ("H3_REF_MODS", "STRING")
|
|
RETURN_NAMES = ("mods", "prompt_hint")
|
|
FUNCTION = "pick"
|
|
CATEGORY = "Dumas/MiniMax H3/RefMod"
|
|
|
|
def pick(self, mod_path="", video_weight=1.0, audio_weight=1.0, mods=None):
|
|
rows = list(mods or [])
|
|
selected = str(mod_path or "").strip()
|
|
if not selected or selected.lower() == "(none)":
|
|
return rows, _hint(rows)
|
|
path = _safe_file(selected)
|
|
loader = _find_loader_api()
|
|
loaded = loader(path[:-len(".safetensors")], device="cpu")
|
|
for mod in loaded:
|
|
weight = audio_weight if getattr(mod, "kind", "") == "audio" else video_weight
|
|
for strength in _expand_weight(weight):
|
|
rows.append((mod, strength))
|
|
return rows, _hint(rows)
|
|
|
|
|
|
NODE_CLASS_MAPPINGS = {"DumasH3RefModPicker": DumasH3RefModPicker}
|
|
NODE_DISPLAY_NAME_MAPPINGS = {"DumasH3RefModPicker": "DUMAS H3 RefMod Visual Picker"}
|
|
|
|
|
|
try:
|
|
from server import PromptServer, web
|
|
|
|
@PromptServer.instance.routes.get("/dumas/h3-refmod-browser/root")
|
|
async def dumas_refmod_root(request):
|
|
return web.json_response({"ok": True, "root": browser_root()})
|
|
|
|
@PromptServer.instance.routes.get("/dumas/h3-refmod-browser/list")
|
|
async def dumas_refmod_list(request):
|
|
try:
|
|
return web.json_response({"ok": True, **list_browser_dir(request.query.get("path", ""))})
|
|
except ValueError as exc:
|
|
return web.json_response({"ok": False, "error": str(exc)}, status=404)
|
|
|
|
@PromptServer.instance.routes.get("/dumas/h3-refmod-browser/file")
|
|
async def dumas_refmod_file(request):
|
|
try:
|
|
path = _safe_file(request.query.get("path", ""))
|
|
if os.path.splitext(path)[1].lower() not in (".png", ".jpg", ".jpeg", ".webp"):
|
|
raise ValueError("Unsupported preview type")
|
|
return web.FileResponse(path, headers={"Cache-Control": "no-cache"})
|
|
except ValueError as exc:
|
|
return web.json_response({"ok": False, "error": str(exc)}, status=404)
|
|
except Exception:
|
|
pass
|