Files
DumasNodes/nodes/refmod_apply.py
T
chris.dumas c9eb575e2f Squashed 'vendor/dumas_h3_refmod_picker/' content from commit c732f8b
git-subtree-dir: vendor/dumas_h3_refmod_picker
git-subtree-split: c732f8b8893bf4f00c6974c9b9941c763496fe48
2026-10-20 13:43:48 +00:00

184 lines
7.6 KiB
Python

"""
H3RefModApplySimple — identity-oriented RefMod apply node with one strength dial
"""
from __future__ import annotations
import random
from dataclasses import replace
from typing import Dict, List, Optional
from comfy_api.latest import io
# reference retention presets (master strength multiplier on Apply)
RETENTION = {
"fully_preserved": 1.0,
"partially_preserved": 0.7,
"attribute_transfer": 0.4,
"weak_reference": 0.15,
}
def _filter_legacy_ref_block(block: Optional[Dict],
use_video: bool,
use_audio: bool) -> Optional[Dict]:
"""Apply media toggles to ref blocks produced by older RefMod APIs."""
if block is None or (not use_video and not use_audio):
return None
kind = str(block.get("kind") or "").strip().lower()
has_audio = block.get("audio_latent") is not None and int(block.get("ref_audio_t") or 0) > 0
if kind == "audio":
return block if use_audio else None
if not use_video:
if use_audio and has_audio:
return {
"kind": "audio",
"ref_audio_t": int(block.get("ref_audio_t") or 0),
"audio_latent": block.get("audio_latent"),
}
return None
if not use_audio and has_audio:
filtered = dict(block)
filtered["kind"] = "video"
filtered["ref_audio_t"] = 0
filtered["audio_latent"] = None
return filtered
return block
def _build_ref_block(mod, eff: float, curve, use_video: bool, use_audio: bool) -> Optional[Dict]:
try:
return mod.ref_block(eff, curve=curve, use_video=use_video, use_audio=use_audio)
except TypeError as exc:
message = str(exc)
if "unexpected keyword argument 'use_video'" not in message and "unexpected keyword argument 'use_audio'" not in message:
raise
legacy_block = mod.ref_block(eff, curve=curve)
return _filter_legacy_ref_block(legacy_block, use_video=use_video, use_audio=use_audio)
def _ref_blocks(mods, retention, curve=None, seed=-1,
use_video: bool = True, use_audio: bool = True) -> List[Dict]:
"""Ref blocks for a loader bundle, scaled by row strength x retention.
``retention`` is a master strength multiplier: a float 0-1 (1.0 =
fully_preserved, 0.7 = partially_preserved, 0.4 = attribute_transfer,
0.15 = weak_reference), or one of those preset names for legacy
workflows saved with the old combo widget.
``curve`` (optional) is a per-frame strength spec — a ``(direction,
shape, value)`` tuple, a legacy preset name, per-frame values, control
points (see ``core.curve_strengths``) — applied on top of the row
strength. A flat/no curve keeps today's behavior.
``seed`` (default -1 = off) enables ref scrambling: with 2+ refs in the
bundle, the order is shuffled and a random subset kept, so a different
ref leads each run instead of the same one always "popping". Same seed
-> same scramble; connect/randomize the seed for per-run variation.
"""
if isinstance(retention, str):
factor = RETENTION.get(retention, 1.0)
else:
factor = float(retention)
items = [] if mods is None else list(mods)
if int(seed) >= 0 and len(items) > 1:
rng = random.Random(int(seed))
rng.shuffle(items)
keep = rng.randint(max(1, len(items) // 2), len(items))
items = items[:keep]
print(f"[H3RefModApply] scramble seed={int(seed)}: "
f"{len(mods)} refs -> kept {len(items)} (order shuffled)")
blocks = []
debug_rows = []
for mod, strength in items:
eff = min(1.0, max(0.0, strength * factor))
block = _build_ref_block(mod, eff, curve=curve, use_video=use_video, use_audio=use_audio)
if block is not None:
blocks.append(block)
debug_rows.append(f"{mod.name}@{eff:.2f}")
else:
debug_rows.append(f"{mod.name}@{eff:.2f} (skipped)")
if debug_rows:
print("[H3RefModApply] effective refs: " + ", ".join(debug_rows))
return blocks
class H3RefModApplySimple(io.ComfyNode):
"""Identity-oriented RefMod apply node with one strength dial."""
@classmethod
def define_schema(cls):
template = io.MatchType.Template(
"cond",
allowed_types=[io.Custom("MINIMAX_H3_COND"), io.Conditioning])
return io.Schema(
node_id="H3RefModApplySimple",
display_name="Apply H3 RefMod Simple",
description=(
"Apply one or more RefMods to a MiniMax H3 conditioning with a single "
"Strength control. This identity-oriented version uses a flat full-length "
"reference curve under the hood."
),
category="H3RefModPicker",
inputs=[
io.MatchType.Input("conditioning", template=template,
tooltip="MINIMAX_H3_COND (ComfyUI-MiniMaxH3 pack) or CONDITIONING "
"(core MiniMaxH3ReferenceToVideo)."),
io.Custom("H3_REF_MODS").Input("mods",
optional=True,
tooltip="Bundle of RefMods to inject. Leave unconnected to bypass unchanged."),
io.Boolean.Input("use_video", default=True,
tooltip="Apply the visual latent from each RefMod. Turn this off to use only embedded audio."),
io.Boolean.Input("use_audio", default=True,
tooltip="Apply the embedded audio latent from each RefMod when present. Turn this off for visual-only application."),
io.Float.Input("strength", default=1.0, min=0.0, max=1.0, step=0.01,
tooltip="Master reference strength. For identity work this is the main "
"dial: higher = tighter identity lock, lower = more freedom but "
"more drift."),
],
outputs=[
io.MatchType.Output(template=template, display_name="conditioning",
tooltip="The conditioning with the ref blocks injected, same type as the input."),
],
)
@classmethod
def execute(cls, conditioning, mods, use_video=True, use_audio=True, strength=1.0):
if mods is None:
print("[H3RefModApplySimple] no mods connected — bypassing conditioning unchanged")
return io.NodeOutput(conditioning)
if not use_video and not use_audio:
print("[H3RefModApplySimple] use_video=False and use_audio=False — bypassing conditioning unchanged")
return io.NodeOutput(conditioning)
blocks = _ref_blocks(mods, strength, ("constant", "linear", 1.0), seed=-1,
use_video=bool(use_video), use_audio=bool(use_audio))
if isinstance(conditioning, list):
out = []
for t in conditioning:
d = dict(t[1])
d["minimax_refs"] = list(d.get("minimax_refs", [])) + blocks
out.append([t[0], d])
print(f"[H3RefModApplySimple] strength={strength} "
f"({len(blocks)} ref block(s) injected)")
else:
out = replace(conditioning, refs=list(conditioning.refs) + blocks)
print(f"[H3RefModApplySimple] strength={strength} "
f"({len(blocks)} ref block(s) injected, {len(out.refs)} total)")
return io.NodeOutput(out)
NODE_CLASS_MAPPINGS = {
"H3RefModApplySimple": H3RefModApplySimple,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"H3RefModApplySimple": "Apply H3 RefMod Simple",
}
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]