Add DUMAS H3 RefMod storage node
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
"""DUMAS MiniMax H3 RefMod storage helpers.
|
||||
|
||||
This module deliberately works with the ``H3_REF_MODS`` object produced by
|
||||
the upstream MiniMax H3 RefMod nodes. It adds DUMAS-owned persistence policy
|
||||
without copying or shadowing the upstream extractor/loader implementation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def _registered_refmod_root():
|
||||
import folder_paths
|
||||
|
||||
try:
|
||||
roots = list(folder_paths.get_folder_paths("refmods"))
|
||||
except Exception:
|
||||
roots = []
|
||||
return os.path.realpath(roots[0] if roots else os.path.join(folder_paths.models_dir, "refmods"))
|
||||
|
||||
|
||||
def _safe_name(value, fallback="refmod"):
|
||||
value = str(value or "").strip()
|
||||
value = value.replace("\\", "/").split("/")[-1]
|
||||
value = re.sub(r"[^A-Za-z0-9._ -]+", "_", value).strip(" .")
|
||||
return value or fallback
|
||||
|
||||
|
||||
def _safe_destination(root, subfolder, filename):
|
||||
root = os.path.realpath(root)
|
||||
parts = [p for p in str(subfolder or "").replace("\\", "/").split("/") if p]
|
||||
if any(p in (".", "..") for p in parts):
|
||||
raise ValueError("RefMod subfolder must not contain '.' or '..'.")
|
||||
path = os.path.realpath(os.path.join(root, *parts, filename))
|
||||
try:
|
||||
inside = os.path.commonpath((root, path)) == root
|
||||
except ValueError:
|
||||
inside = False
|
||||
if not inside:
|
||||
raise ValueError("RefMod destination must remain inside the selected folder.")
|
||||
return path
|
||||
|
||||
|
||||
def _next_path(path, reserved=()):
|
||||
stem, ext = os.path.splitext(path)
|
||||
index = 2
|
||||
candidate = path
|
||||
reserved = {os.path.normcase(item) for item in reserved}
|
||||
while (os.path.normcase(candidate) in reserved or os.path.exists(candidate)
|
||||
or os.path.exists(candidate + ".safetensors") or os.path.exists(candidate + ".json")):
|
||||
candidate = f"{stem}_{index}{ext}"
|
||||
index += 1
|
||||
return candidate
|
||||
|
||||
|
||||
def _mod_path(mod):
|
||||
return str(getattr(mod, "path", "") or "")
|
||||
|
||||
|
||||
class DumasH3RefModSave:
|
||||
"""Save upstream MiniMax H3 RefMods using an explicit DUMAS policy.
|
||||
|
||||
``input_folder`` is intentionally explicit: standard ComfyUI IMAGE and
|
||||
VIDEO tensors do not retain their source filesystem path. A folder-aware
|
||||
loader can connect its path here when it exposes one.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"mods": ("H3_REF_MODS",),
|
||||
"filename_prefix": ("STRING", {"default": ""}),
|
||||
"subfolder": ("STRING", {"default": ""}),
|
||||
"save_location": (["Registered RefMods", "Input folder", "Custom folder"],
|
||||
{"default": "Registered RefMods"}),
|
||||
"collision_policy": (["Increment filename", "Replace", "Fail"],
|
||||
{"default": "Increment filename"}),
|
||||
},
|
||||
"optional": {
|
||||
"input_folder": ("STRING", {"default": "", "forceInput": True}),
|
||||
"custom_output_folder": ("STRING", {"default": "", "forceInput": True}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("H3_REF_MODS", "STRING")
|
||||
RETURN_NAMES = ("mods", "saved_paths")
|
||||
FUNCTION = "save"
|
||||
OUTPUT_NODE = True
|
||||
CATEGORY = "Dumas/MiniMax H3/RefMod"
|
||||
DESCRIPTION = (
|
||||
"Save H3 RefMods atomically. Registered RefMods is the portable default; "
|
||||
"Input folder and Custom folder are explicit server-side destinations."
|
||||
)
|
||||
|
||||
def save(self, mods, filename_prefix="", subfolder="",
|
||||
save_location="Registered RefMods", collision_policy="Increment filename",
|
||||
input_folder="", custom_output_folder=""):
|
||||
if not mods:
|
||||
raise ValueError("The H3 RefMod bundle is empty; nothing to save.")
|
||||
if save_location == "Registered RefMods":
|
||||
root = _registered_refmod_root()
|
||||
os.makedirs(root, exist_ok=True)
|
||||
elif save_location == "Input folder":
|
||||
raw_folder = str(input_folder or "").strip()
|
||||
if not raw_folder:
|
||||
raise ValueError("Input folder mode requires a connected input_folder path.")
|
||||
root = os.path.realpath(raw_folder)
|
||||
elif save_location == "Custom folder":
|
||||
raw_folder = str(custom_output_folder or "").strip()
|
||||
if not raw_folder:
|
||||
raise ValueError("Custom folder mode requires a connected custom_output_folder path.")
|
||||
root = os.path.realpath(raw_folder)
|
||||
else:
|
||||
raise ValueError(f"Unknown RefMod save location: {save_location!r}")
|
||||
|
||||
if not os.path.isdir(root):
|
||||
raise ValueError(f"RefMod destination folder does not exist: {root}")
|
||||
if not os.access(root, os.W_OK):
|
||||
raise ValueError(f"RefMod destination folder is not writable: {root}")
|
||||
|
||||
planned = []
|
||||
used = set()
|
||||
for mod, _strength in mods:
|
||||
name = _safe_name(f"{filename_prefix}{getattr(mod, 'name', 'refmod')}")
|
||||
path = _safe_destination(root, subfolder, name)
|
||||
if collision_policy == "Increment filename":
|
||||
path = _next_path(path, used)
|
||||
elif os.path.normcase(path) in used:
|
||||
path = _next_path(path, used)
|
||||
elif collision_policy == "Fail" and (os.path.exists(path) or os.path.exists(path + ".safetensors")):
|
||||
raise FileExistsError(f"RefMod already exists: {path}. Choose another name or policy.")
|
||||
elif collision_policy != "Replace":
|
||||
raise ValueError(f"Unknown collision policy: {collision_policy!r}")
|
||||
|
||||
source = _mod_path(mod)
|
||||
if source and os.path.normcase(os.path.realpath(source)) == os.path.normcase(path):
|
||||
raise ValueError("RefMod destination is the source file; choose another folder or name.")
|
||||
used.add(os.path.normcase(path))
|
||||
planned.append((mod, path))
|
||||
|
||||
saved = []
|
||||
for mod, path in planned:
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
if not hasattr(mod, "save"):
|
||||
raise TypeError("Connected H3_REF_MODS contains an object without a save() method.")
|
||||
saved.append(mod.save(path))
|
||||
|
||||
report = "\n".join(saved)
|
||||
print(f"[DUMAS H3 RefMod Save] {save_location}; {collision_policy}; saved {len(saved)} file(s):\n{report}")
|
||||
return {"ui": {"text": saved}, "result": (mods, report)}
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"DumasH3RefModSave": DumasH3RefModSave,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"DumasH3RefModSave": "DUMAS H3 RefMod Save",
|
||||
}
|
||||
Reference in New Issue
Block a user