diff --git a/README.md b/README.md index 300ab83..5bff9f7 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,25 @@ from the installed upstream H3 Continuum pack. Install that pack alongside DumasNodes; DumasNodes does not copy or replace the upstream reference-image implementation. +### DUMAS H3 RefMod Save + +`DUMAS H3 RefMod Save` accepts the `H3_REF_MODS` bundle produced by the +MiniMax H3 RefMod nodes and adds explicit DUMAS-owned storage policy: + +- `Registered RefMods` is the default and saves to ComfyUI's first registered + `refmods` root, preserving normal loader discovery. +- `Input folder` saves beside a server-side source folder supplied through the + `input_folder` socket. Standard IMAGE/VIDEO tensors do not contain their + original filesystem path, so this path must be supplied explicitly. +- `Custom folder` saves to an explicitly supplied server-side folder. +- `Increment filename` is the default collision policy; `Replace` and `Fail` + are also available. + +All writes use the RefMod object's atomic save operation. The node reports the +resolved `.safetensors` paths through `saved_paths`. External folders are not +automatically added to the RefMod dropdown; register them as a ComfyUI +`refmods` root or load them through the upstream folder/library tooling. + ### DUMAS H3 Continuum Tiled Latent Upscale (Per Clip) `DumasH3ContinuumTiledLatentUpscalePerClip` is a replacement for the upstream diff --git a/__init__.py b/__init__.py index da8f9bc..b895706 100644 --- a/__init__.py +++ b/__init__.py @@ -27,6 +27,10 @@ from .dumas_h3_upscale_nodes import ( NODE_CLASS_MAPPINGS as H3_UPSCALE_NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as H3_UPSCALE_NODE_DISPLAY_NAME_MAPPINGS, ) +from .dumas_h3_refmod_nodes import ( + NODE_CLASS_MAPPINGS as H3_REFMOD_NODE_CLASS_MAPPINGS, + NODE_DISPLAY_NAME_MAPPINGS as H3_REFMOD_NODE_DISPLAY_NAME_MAPPINGS, +) NODE_CLASS_MAPPINGS = {} NODE_CLASS_MAPPINGS.update(JSON_NODE_CLASS_MAPPINGS) @@ -35,6 +39,7 @@ NODE_CLASS_MAPPINGS.update(VIDEO_NODE_CLASS_MAPPINGS) NODE_CLASS_MAPPINGS.update(MINIMAX_NODE_CLASS_MAPPINGS) NODE_CLASS_MAPPINGS.update(H3_PROMPT_NODE_CLASS_MAPPINGS) NODE_CLASS_MAPPINGS.update(H3_UPSCALE_NODE_CLASS_MAPPINGS) +NODE_CLASS_MAPPINGS.update(H3_REFMOD_NODE_CLASS_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS = {} NODE_DISPLAY_NAME_MAPPINGS.update(JSON_NODE_DISPLAY_NAME_MAPPINGS) @@ -43,6 +48,7 @@ NODE_DISPLAY_NAME_MAPPINGS.update(VIDEO_NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(MINIMAX_NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(H3_PROMPT_NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(H3_UPSCALE_NODE_DISPLAY_NAME_MAPPINGS) +NODE_DISPLAY_NAME_MAPPINGS.update(H3_REFMOD_NODE_DISPLAY_NAME_MAPPINGS) WEB_DIRECTORY = "./js" diff --git a/dumas_h3_refmod_nodes.py b/dumas_h3_refmod_nodes.py new file mode 100644 index 0000000..7ac2051 --- /dev/null +++ b/dumas_h3_refmod_nodes.py @@ -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", +} diff --git a/tests/test_dumas_h3_refmod_nodes.py b/tests/test_dumas_h3_refmod_nodes.py new file mode 100644 index 0000000..a67dcfd --- /dev/null +++ b/tests/test_dumas_h3_refmod_nodes.py @@ -0,0 +1,68 @@ +import importlib.util +import os +import sys +import tempfile +import types +import unittest + + +class FakeMod: + def __init__(self, name): + self.name = name + self.path = "" + + def save(self, path): + with open(path + ".safetensors", "wb") as handle: + handle.write(b"test") + return path + ".safetensors" + + +class DumasRefModSaveTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + folder_paths = types.ModuleType("folder_paths") + folder_paths.models_dir = tempfile.gettempdir() + folder_paths.get_folder_paths = lambda _name: [] + sys.modules.setdefault("folder_paths", folder_paths) + spec = importlib.util.spec_from_file_location( + "dumas_h3_refmod_nodes", "dumas_h3_refmod_nodes.py") + cls.module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cls.module) + + def test_input_folder_is_explicit_and_collision_safe(self): + with tempfile.TemporaryDirectory() as folder: + node = self.module.DumasH3RefModSave() + first = node.save([ (FakeMod("hero"), 1.0) ], + save_location="Input folder", input_folder=folder) + second = node.save([ (FakeMod("hero"), 1.0) ], + save_location="Input folder", input_folder=folder) + self.assertIn("hero.safetensors", first["result"][1]) + self.assertIn("hero_2.safetensors", second["result"][1]) + + def test_fail_policy_does_not_overwrite(self): + with tempfile.TemporaryDirectory() as folder: + node = self.module.DumasH3RefModSave() + node.save([ (FakeMod("hero"), 1.0) ], + save_location="Input folder", input_folder=folder) + with self.assertRaises(FileExistsError): + node.save([ (FakeMod("hero"), 1.0) ], + save_location="Input folder", input_folder=folder, + collision_policy="Fail") + + def test_duplicate_names_in_one_bundle_are_numbered(self): + with tempfile.TemporaryDirectory() as folder: + node = self.module.DumasH3RefModSave() + result = node.save([(FakeMod("hero"), 1.0), (FakeMod("hero"), 1.0)], + save_location="Input folder", input_folder=folder) + report = result["result"][1] + self.assertIn("hero.safetensors", report) + self.assertIn("hero_2.safetensors", report) + + def test_requires_folder_for_external_modes(self): + node = self.module.DumasH3RefModSave() + with self.assertRaisesRegex(ValueError, "input_folder"): + node.save([ (FakeMod("hero"), 1.0) ], save_location="Input folder") + + +if __name__ == "__main__": + unittest.main()