Add DUMAS MiniMax H3 custom nodes
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
"""UI-backed sequence prompt construction for H3 Continuum."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Any, Mapping
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
REFERENCE_IMAGES_V39_TYPE = "H3_CONTINUUM_REFERENCE_IMAGES_V39"
|
||||
|
||||
|
||||
def _continuum_reference_bundle_type():
|
||||
"""Find the upstream H3 bundle class without copying its implementation."""
|
||||
candidates = [
|
||||
module for name, module in list(sys.modules.items())
|
||||
if module is not None and name.endswith(".v3.reference_images_v39")
|
||||
]
|
||||
try:
|
||||
import importlib
|
||||
candidates.append(importlib.import_module("v3.reference_images_v39"))
|
||||
except Exception:
|
||||
pass
|
||||
for module in candidates:
|
||||
bundle = getattr(module, "ReferenceImagesV39Bundle", None)
|
||||
if bundle is not None:
|
||||
return bundle
|
||||
raise RuntimeError(
|
||||
"DUMAS H3 Sequence Prompt Builder requires the original "
|
||||
"ComfyUI-H3-Continuum reference_images_v39 module to be installed."
|
||||
)
|
||||
|
||||
|
||||
MAX_CLIPS = 16
|
||||
DEFAULT_CLIP_SECONDS = 4.0
|
||||
|
||||
|
||||
def _number(value: Any, name: str, *, minimum: float = 0.0) -> float:
|
||||
if isinstance(value, bool):
|
||||
raise ValueError(f"{name} must be a number")
|
||||
try:
|
||||
result = float(value)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValueError(f"{name} must be a number") from exc
|
||||
if result < minimum:
|
||||
raise ValueError(f"{name} must be >= {minimum}")
|
||||
return result
|
||||
|
||||
|
||||
def default_sequence(clip_count: int = 2, clip_seconds: float = DEFAULT_CLIP_SECONDS) -> dict[str, Any]:
|
||||
"""Return a portable editor state with one slot per clip."""
|
||||
|
||||
count = int(clip_count)
|
||||
if count < 1 or count > MAX_CLIPS:
|
||||
raise ValueError(f"clip_count must be between 1 and {MAX_CLIPS}")
|
||||
duration = _number(clip_seconds, "clip_seconds", minimum=0.1)
|
||||
return {
|
||||
"version": 1,
|
||||
"clips": [
|
||||
{
|
||||
"duration": duration,
|
||||
"slots": [{"start": 0.0, "end": duration, "text": ""}],
|
||||
}
|
||||
for _ in range(count)
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def normalize_sequence(value: Any, *, clip_count: int, default_seconds: float) -> dict[str, Any]:
|
||||
"""Validate and normalize editor JSON without changing user-authored text."""
|
||||
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
value = json.loads(value or "{}")
|
||||
except json.JSONDecodeError as exc:
|
||||
raise ValueError(f"timeline_json is not valid JSON: {exc.msg}") from exc
|
||||
if not isinstance(value, Mapping):
|
||||
raise ValueError("timeline_json must be a JSON object")
|
||||
raw_clips = value.get("clips", [])
|
||||
if not isinstance(raw_clips, list):
|
||||
raise ValueError("timeline_json.clips must be a list")
|
||||
count = int(clip_count)
|
||||
if count < 1 or count > MAX_CLIPS:
|
||||
raise ValueError(f"clip_count must be between 1 and {MAX_CLIPS}")
|
||||
fallback_duration = _number(default_seconds, "default_chunk_seconds", minimum=0.1)
|
||||
clips: list[dict[str, Any]] = []
|
||||
for clip_index in range(count):
|
||||
raw_clip = raw_clips[clip_index] if clip_index < len(raw_clips) else {}
|
||||
if not isinstance(raw_clip, Mapping):
|
||||
raise ValueError(f"clip {clip_index + 1} must be an object")
|
||||
# Continuum projects use one chunk duration for every clip. Older
|
||||
# editor state may still contain per-clip durations; ignore them so
|
||||
# the single node control remains authoritative.
|
||||
duration = fallback_duration
|
||||
raw_slots = raw_clip.get("slots", [])
|
||||
if not isinstance(raw_slots, list):
|
||||
raise ValueError(f"clip {clip_index + 1} slots must be a list")
|
||||
slots: list[dict[str, Any]] = []
|
||||
for slot_index, raw_slot in enumerate(raw_slots):
|
||||
if not isinstance(raw_slot, Mapping):
|
||||
raise ValueError(f"clip {clip_index + 1} slot {slot_index + 1} must be an object")
|
||||
start = _number(raw_slot.get("start", 0), f"clip {clip_index + 1} slot {slot_index + 1} start")
|
||||
end = _number(raw_slot.get("end", duration), f"clip {clip_index + 1} slot {slot_index + 1} end")
|
||||
if end <= start:
|
||||
raise ValueError(f"clip {clip_index + 1} slot {slot_index + 1} end must be after start")
|
||||
if end > duration + 1e-6:
|
||||
raise ValueError(f"clip {clip_index + 1} slot {slot_index + 1} ends after the clip duration")
|
||||
text = str(raw_slot.get("text", "")).strip()
|
||||
if text:
|
||||
slots.append({"start": start, "end": end, "text": text})
|
||||
slots.sort(key=lambda slot: (slot["start"], slot["end"]))
|
||||
if slots:
|
||||
# The final shot owns the end of every uniform-duration chunk;
|
||||
# there must be no unassigned tail on the editor track.
|
||||
slots[-1]["end"] = duration
|
||||
clips.append({"duration": duration, "slots": slots})
|
||||
return {"version": 1, "clips": clips}
|
||||
|
||||
|
||||
def format_sequence_prompt(
|
||||
*,
|
||||
shared_directive: str,
|
||||
sequence: Mapping[str, Any],
|
||||
soundscape: str = "",
|
||||
music: str = "",
|
||||
) -> str:
|
||||
"""Format uniform clips as chunk blocks with relative shot timings.
|
||||
|
||||
Continuum treats square-bracket ranges as physical/logical chunk markers.
|
||||
Shot timings belong inside each chunk and must therefore use plain
|
||||
``start-end s:`` lines rather than creating additional Timeline sections.
|
||||
"""
|
||||
|
||||
directive = str(shared_directive or "").strip()
|
||||
sound = str(soundscape or "").strip() or "N/A"
|
||||
music_text = str(music or "").strip() or "N/A"
|
||||
sections: list[str] = []
|
||||
cursor = 0.0
|
||||
for clip in sequence["clips"]:
|
||||
duration = float(clip["duration"])
|
||||
chunk_lines = [f"[{cursor:g}-{cursor + duration:g}s]"]
|
||||
if directive:
|
||||
chunk_lines.append(directive)
|
||||
for slot in clip["slots"]:
|
||||
start = float(slot["start"])
|
||||
end = float(slot["end"])
|
||||
chunk_lines.append(f"{start:g}-{end:g}s: {slot['text']}")
|
||||
chunk_lines.append(f"overall_soundscape: {sound}")
|
||||
chunk_lines.append(f"non_diegetic_music: {music_text}")
|
||||
sections.append("\n".join(chunk_lines))
|
||||
cursor += duration
|
||||
return "\n\n".join(sections).strip()
|
||||
|
||||
|
||||
class DumasH3SequencePromptBuilder:
|
||||
"""Build one Timeline prompt from clip tabs and clip-local time slots."""
|
||||
|
||||
DESCRIPTION = (
|
||||
"DUMAS H3 prompt editor. Define clips and local time slots in the UI; "
|
||||
"the node emits one absolute Timeline prompt for Sequence Prompt."
|
||||
)
|
||||
CATEGORY = "DUMAS H3/Prompt"
|
||||
RETURN_TYPES = ("STRING", "INT", "FLOAT", REFERENCE_IMAGES_V39_TYPE)
|
||||
RETURN_NAMES = ("sequence_prompt", "chunk_count", "chunk_duration", "reference_images")
|
||||
FUNCTION = "build"
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
state = json.dumps(default_sequence(), separators=(",", ":"))
|
||||
return {
|
||||
"required": {
|
||||
"clip_count": ("INT", {"default": 2, "min": 1, "max": MAX_CLIPS, "step": 1}),
|
||||
"default_chunk_seconds": ("FLOAT", {"default": DEFAULT_CLIP_SECONDS, "min": 0.1, "max": 30.0, "step": 0.1}),
|
||||
"shared_directive": ("STRING", {"default": "", "multiline": True}),
|
||||
"timeline_json": ("STRING", {"default": state, "multiline": True}),
|
||||
"soundscape": ("STRING", {"default": "", "multiline": True}),
|
||||
"music": ("STRING", {"default": "", "multiline": True}),
|
||||
},
|
||||
"optional": {
|
||||
f"reference_image_{index}": ("IMAGE", {
|
||||
"display_name": f"Reference Image {index} (@R{index})",
|
||||
"tooltip": f"Fixed Reference Image {index}; use @R{index} in a clip prompt.",
|
||||
}) for index in range(1, 10)
|
||||
}
|
||||
}
|
||||
|
||||
def build(
|
||||
self,
|
||||
clip_count,
|
||||
default_chunk_seconds,
|
||||
shared_directive,
|
||||
timeline_json,
|
||||
soundscape,
|
||||
music,
|
||||
**kwargs,
|
||||
):
|
||||
sequence = normalize_sequence(
|
||||
timeline_json,
|
||||
clip_count=int(clip_count),
|
||||
default_seconds=float(default_chunk_seconds),
|
||||
)
|
||||
selectors = []
|
||||
directive = str(shared_directive or "").strip()
|
||||
sound = str(soundscape or "").strip() or "N/A"
|
||||
music_text = str(music or "").strip() or "N/A"
|
||||
for reference_index in range(1, 10):
|
||||
chunks = []
|
||||
for chunk_index, clip in enumerate(sequence["clips"], start=1):
|
||||
# Match against the same per-chunk content emitted by
|
||||
# format_sequence_prompt, not only the editable shot lines.
|
||||
text = "\n".join([
|
||||
directive,
|
||||
*(str(slot.get("text", "")) for slot in clip["slots"]),
|
||||
f"overall_soundscape: {sound}",
|
||||
f"non_diegetic_music: {music_text}",
|
||||
])
|
||||
if kwargs.get(f"reference_image_{reference_index}") is not None and re.search(
|
||||
rf"@(R{reference_index}|image{reference_index})(?![A-Za-z0-9_])",
|
||||
text,
|
||||
re.IGNORECASE,
|
||||
):
|
||||
chunks.append(chunk_index)
|
||||
selectors.append(",".join(map(str, chunks)) or "off")
|
||||
images = tuple(kwargs.get(f"reference_image_{index}") for index in range(1, 10))
|
||||
reference_bundle = _continuum_reference_bundle_type()(
|
||||
images=images,
|
||||
reference_use="Per chunk",
|
||||
selectors=tuple(selectors),
|
||||
)
|
||||
return (
|
||||
format_sequence_prompt(
|
||||
shared_directive=shared_directive,
|
||||
sequence=sequence,
|
||||
soundscape=soundscape,
|
||||
music=music,
|
||||
),
|
||||
len(sequence["clips"]),
|
||||
float(default_chunk_seconds),
|
||||
reference_bundle,
|
||||
)
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {"DumasH3SequencePromptBuilder": DumasH3SequencePromptBuilder}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"DumasH3SequencePromptBuilder": "DUMAS H3 Sequence Prompt Builder",
|
||||
}
|
||||
Reference in New Issue
Block a user