from __future__ import annotations import json import math import time from pathlib import Path from typing import Any import folder_paths import nodes as comfy_nodes import torch import torch.nn.functional as F from comfy_api.latest import io from comfy_execution.graph_utils import GraphBuilder from ..modules.motion_context.core import ( REENCODED_ANCHOR_FRAMES, apply_hires_anchor_continuity, apply_hires_continuity, apply_motion_context, apply_reencoded_anchor_motion_context, build_hard_motion_context, trim_motion_context_latent, ) from ..modules.motion_context.drift_control_av import apply_context_swap_drift_control from ..utils import log_node_info, log_stage_time, synchronize_execution_device from ..utils.h3_project import ( choose_h3_generation, compact_h3_task_segments, load_h3_latent, parse_tracks_info, safe_h3_project_name, save_h3_latent, save_h3_audio, ) from ..utils.h3_conditioning_cache import ( format_h3_conditioning_cache_stats, get_staged_h3_conditioning_cache, h3_conditioning_cache_matches, h3_conditioning_cache_path, h3_encoder_signature, load_h3_conditioning_cache, prepare_h3_conditioning_cache_pool, save_h3_conditioning_cache, stage_h3_conditioning_cache, touch_h3_conditioning_cache, ) from ..utils.minimax import ( expand_image_inputs, flatten_media_inputs, h3_phase_aligned_video_suffix, remove_output_files_by_prefix, ) from ..utils.prompt_override import build_minimax_prompt_override_json from ..utils.video import passthrough_video_media CATEGORY_MINIMAX = "EasyUse/MiniMax" CANVAS_MULTIPLE = 32 def _align_canvas_dimension(value: Any) -> int: """Floor a canvas side onto the H3 32px grid (CANVAS_MULTIPLE). The video VAE latent grid is pixels // 16 and the diffusion model patchifies that latent with a 2x2 patch, so every canvas side must be a multiple of 32. Anything else produces an odd latent dimension and crashes comfy's patchify_video reshape ("shape '[1, 24, ...]' is invalid for input of size N"). Floors instead of rounds so alignment never upsamples past the requested size. """ try: number = int(value) except (TypeError, ValueError): return CANVAS_MULTIPLE return max(CANVAS_MULTIPLE, number // CANVAS_MULTIPLE * CANVAS_MULTIPLE) BASE_SHORT_EDGE = 768 MAX_PIXELS = 768 * 1344 REF_IMAGE_SHORT_EDGE = 2048 FPS = 24 AUDIO_LATENT_FPS = 40 MAX_REF_IMAGES = 9 MAX_REF_VIDEOS = 3 MAX_REF_AUDIOS = 3 REFERENCE_BRIDGE_NODE_ID = "easy MiniMaxH3ReferenceToVideoBridge" TYPE_TRACKS_INFO = io.Custom(io_type="TRACKS_INFO") MULTITRACK_PROJECT_REFRESH_EVENT = "easy_multitrack_project_refresh" class EasyMinimaxPromptOverride(io.ComfyNode): """Assemble Moxie per-segment prompt override JSON.""" @classmethod def define_schema(cls) -> io.Schema: return io.Schema( node_id="easy minimaxPromptOverride", display_name="Moxie Prompt Override", category=CATEGORY_MINIMAX, description=( "Assemble per-segment Moxie prompts, durations, generation " "types, continuity modes, and track locks into a prompt_override " "JSON string." ), inputs=[ io.String.Input( "system_prompt", optional=True, force_input=True, multiline=True, tooltip=( "Optional system prompt. When non-empty, this value is " "written into every task segment's system_prompt in the " "resulting TRACKS_INFO." ), ), io.Autogrow.Input( "prompts", template=io.Autogrow.TemplatePrefix( input=io.String.Input( "prompt", multiline=True, dynamic_prompts=True, ), prefix="prompt_", min=1, max=100, ), tooltip=( "One prompt per Moxie clip. Use @图片N/@音频N/@视频N " "or /