File size: 29,416 Bytes
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdffd22
 
50d8c92
 
1d7083d
50d8c92
 
 
40e7b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
1d7083d
 
 
 
 
 
 
 
 
 
 
edc97dc
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9e5be6
edc97dc
e9e5be6
 
 
 
 
 
 
 
 
 
edc97dc
 
e9e5be6
edc97dc
 
 
e9e5be6
 
 
 
 
 
 
 
 
edc97dc
 
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9e5be6
edc97dc
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
 
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
fdffd22
50d8c92
 
 
 
 
 
 
fdffd22
e9e5be6
50d8c92
 
 
edc97dc
 
 
e9e5be6
50d8c92
 
fdffd22
50d8c92
 
 
 
 
 
 
edc97dc
e9e5be6
edc97dc
 
e9e5be6
50d8c92
 
fdffd22
50d8c92
edc97dc
50d8c92
fdffd22
50d8c92
 
 
 
 
 
 
c753618
e9e5be6
edc97dc
 
 
40e7b97
c753618
 
 
edc97dc
40e7b97
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
e9e5be6
 
edc97dc
e9e5be6
 
 
 
 
 
 
 
edc97dc
e9e5be6
 
 
 
 
edc97dc
 
 
 
 
 
e9e5be6
edc97dc
 
 
 
 
 
 
e9e5be6
 
 
 
50d8c92
 
 
 
 
 
edc97dc
2b388f0
 
50d8c92
 
 
edc97dc
 
 
 
 
 
 
40e7b97
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
40e7b97
e9e5be6
50d8c92
fdffd22
50d8c92
 
c753618
50d8c92
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
c753618
fdffd22
edc97dc
50d8c92
 
fdffd22
50d8c92
c753618
edc97dc
50d8c92
 
 
 
 
 
 
1d7083d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
 
 
 
 
 
 
1d7083d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
"""HTC RefMod Builder. Writes one visual RefMod into models/refmods.

The rail does not read this node's output by a wire. It reads the file,
the same way a still is a file in h3_refs. `name` is the string to put in
the row's `file` field.

Sources are named in `mod_plan` as filenames under `h3_refs`, not wired in
as IMAGE sockets. A DOM panel cannot fill a socket, and a fixed set of
sockets cannot be empty gracefully -- which is the whole reason the other
packs need a separate node to toggle their inputs on and off. Several
stills and a clip become one latent stacked along time, not a version-5
bundle: `load_visual` reads a bundle's first visual member and drops the
rest, so a bundle would silently throw away most of a character reference.

The `images` / `video` sockets still work, for graphs built before the
panel existed. A non-empty `mod_plan` outranks them.
"""
from __future__ import annotations

import torch

from . import media as _media
from . import refmod as _refmod
from comfy_api.latest import io

TAG = "HandTieClips"

# CP-10. `ref_resolution` (appended last): encode short edge for non-identity roles.
REF_RESOLUTIONS = ("auto", "512", "768", "1024")
# Token cap for a video member when max_tokens is 0 ("no limit"). A 5 s video RefMod
# with no cap measured ~49.5k prefix tokens (+131% of a 5 s 768p generate, OOM risk);
# 5120 is the research's budget (user 12-02: adopt). Applies when a RefMod is BUILT:
# files already written keep their tokens. 0 restores "no limit".
REFMOD_VIDEO_TOKEN_CAP = 5120


def _short_edge(canvas, edge):
    """`canvas` `[N,H,W,C]` scaled down so its short edge is `edge` px (32-px grid);
    unchanged when `edge` is 0 or the canvas is already that small."""
    edge = int(edge or 0)
    h, w = int(canvas.shape[1]), int(canvas.shape[2])
    if edge <= 0 or min(h, w) <= edge:
        return canvas
    s = edge / float(min(h, w))
    th = max(32, int(round(h * s / 32.0)) * 32)
    tw = max(32, int(round(w * s / 32.0)) * 32)
    out = torch.nn.functional.interpolate(canvas.movedim(-1, 1).float(), size=(th, tw),
                                          mode="bicubic", align_corners=False)
    return out.movedim(1, -1).clamp(0, 1).to(canvas.dtype)


def _v3_accepted(fn, kwargs):
    """The kwargs a V1 hook's signature accepts (all of them if it takes **kw)."""
    import inspect as _inspect
    params = _inspect.signature(fn).parameters
    if any(p.kind is p.VAR_KEYWORD for p in params.values()):
        return dict(kwargs)
    return {k: v for k, v in kwargs.items() if k in params}


class _HTCRefModBuilderImpl:


    # Executes on its own, the way PlagueKind's RefMod Save node does.
    # Without this the builder only runs when `name` feeds a node that is
    # itself executed, so a queue with nothing wired to it silently does
    # nothing and reads as "the build did not happen". Safe, because
    # `build=stop` returns before the VAE is touched.

    @classmethod
    def IS_CHANGED(cls, mod_plan="", build="stop", **_kw):
        # A stopped builder is inert, so it must not re-run when a source
        # file changes on disk. A started one must.
        if str(build) != "start":
            return "stopped"
        try:
            stills, clip, _voice = _refmod.parse_mod_plan(mod_plan)
        except ValueError:
            return str(mod_plan)
        names = list(stills) + ([clip["file"]] if clip else [])
        if _voice:
            names.append(_voice)
        return _media.stamp(names)

    def _isolated(self, canvas, index, count, masking):
        """`canvas` with optional subject crop + backdrop replacement."""
        if not masking:
            return canvas
        wired = masking.get("wired")
        if wired is not None:
            if wired.shape[0] == 1:
                pick = wired
            elif count == 1 and wired.shape[0] == canvas.shape[0]:
                pick = wired  # one clip, a mask per frame
            else:
                pick = wired[index:index + 1]
            m = _refmod.mask_to_canvas(pick, canvas.shape[2], canvas.shape[1],
                                       fit_mode=masking.get("fit_mode", "crop"))
        else:
            m = _refmod.refine_cutout(
                _refmod.cutout_masks(canvas, backend=masking.get("backend", "auto_rembg")),
                canvas)
        if m.shape[0] not in (1, canvas.shape[0]):
            m = m[:1]
        kept = float(m.mean())
        print(f"[{TAG}] RefMod source {index + 1}: subject covers {kept:.0%} of "
              f"the canvas", flush=True)
        if kept < 0.02:
            print(f"[{TAG}] RefMod source {index + 1}: the mask keeps almost "
                  f"nothing, so the backdrop is left as it was", flush=True)
            return canvas
        if float(masking.get("crop_margin") or 0.0) > 0.0:
            canvas, m = _refmod.crop_to_subject(canvas, m, margin=float(masking["crop_margin"]))
            canvas, m = _refmod.ensure_min_size(canvas, m)
        return _refmod.isolate_subject(
            canvas, m, masking["retention"], mode=masking.get("bg_mode", "blur_bg"))

    def _encode_audio_member(self, audio_vae, audio_obj, voice_name, voice_win):
        if audio_vae is None:
            return None
        wav_dict = audio_obj
        if wav_dict is None and voice_name:
            v_start, v_end = voice_win
            wav_dict, _auto = _media.load_voice(voice_name, start=v_start, end=v_end)
        if wav_dict is None:
            return None
        try:
            from comfy_extras.nodes_minimax_h3 import _encode_ref_audio
            z, ref_t = _encode_ref_audio(audio_vae, wav_dict)
            return {"kind": "audio", "latent": z, "ref_audio_t": ref_t,
                    "sample_rate": int(getattr(audio_vae, "audio_sample_rate", 32000)),
                    "name": voice_name or "voice"}
        except Exception as exc:
            print(f"[{TAG}] RefMod audio encode skipped: {exc!r}", flush=True)
            return None

    def _encode_plan(self, vae, stills, clip, masking=None, fit_mode="crop",
                     separate=True, max_tokens=0, short_edge=0):
        """Encode stills/clip either as separate v5 bundle members (native aspect) or stacked."""
        count = len(stills) + (1 if clip else 0)
        if separate:
            members = []
            shapes = []
            for i, name in enumerate(stills):
                px = _media.load_image(name)
                if px is None:
                    _refmod._fail(_refmod.source_problem(name, ("image",)) or f"mod_plan source {name} could not be read")
                sw, sh = _refmod.canvas_size(px)
                canvas = self._isolated(_refmod.to_canvas(px, sw, sh, fit_mode=fit_mode), i, count, masking)
                # Ensure 32px dimensions if crop_to_subject narrowed canvas
                cw, ch = _refmod.canvas_size(canvas)
                if (cw, ch) != (int(canvas.shape[2]), int(canvas.shape[1])):
                    canvas = _refmod.to_canvas(canvas, cw, ch, fit_mode="crop")
                canvas = _short_edge(canvas, short_edge)
                z = vae.encode(canvas)
                self._check_encode(z, name)
                z = _refmod.fit_token_budget(z, max_tokens, name)
                looks = _refmod.looks_from(canvas[:1])
                members.append({
                    "kind": "image",
                    "name": name,
                    "latent": z,
                    "looks": looks,
                    "source": "image",
                    "source_shape": "x".join(str(int(n)) for n in z.shape[2:]),
                })
                shapes.append(f"{name} {int(canvas.shape[2])}x{int(canvas.shape[1])}")
            if clip:
                px = _media.load_video(clip["file"], max_frames=clip["frames"] or None,
                                       start=clip["start"], end=clip["end"])
                if px is None:
                    _refmod._fail(_refmod.source_problem(clip['file'], ("video",)) or f"mod_plan source {clip['file']} could not be read")
                px = _refmod.snap_video_frames(px)
                vw, vh = _refmod.canvas_size(px)
                canvas = self._isolated(_refmod.to_canvas(px, vw, vh, fit_mode=fit_mode), count - 1, count, masking)
                canvas = _short_edge(canvas, short_edge)
                z = vae.encode(canvas)
                self._check_encode(z, clip["file"])
                z = _refmod.fit_token_budget(z, max_tokens or REFMOD_VIDEO_TOKEN_CAP, clip["file"])
                looks = _refmod.looks_from(canvas, clip=True)
                members.append({
                    "kind": "video",
                    "name": clip["file"],
                    "latent": z,
                    "looks": looks,
                    "source": "video",
                    "source_shape": "x".join(str(int(n)) for n in z.shape[2:]),
                })
                shapes.append(f"{clip['file']} {int(px.shape[0])}f")
            print(f"[{TAG}] RefMod v5 separate members: {', '.join(shapes)}", flush=True)
            return members

        first = _media.load_image(stills[0]) if stills else None
        if first is None:
            first = _media.load_video(clip["file"], max_frames=1,
                                      start=clip["start"], end=clip["end"])
            if first is None:
                _refmod._fail(_refmod.source_problem(clip['file'], ("video",)) or f"mod_plan source {clip['file']} could not be read")
        tw, th = _refmod.canvas_size(first)
        parts, shapes, looks = [], [], []
        for i, name in enumerate(stills):
            px = _media.load_image(name)
            if px is None:
                _refmod._fail(_refmod.source_problem(name, ("image",)) or f"mod_plan source {name} could not be read")
            canvas = self._isolated(_refmod.to_canvas(px, tw, th, fit_mode=fit_mode), i, count, masking)
            if (int(canvas.shape[2]), int(canvas.shape[1])) != (tw, th):
                canvas = _refmod.to_canvas(canvas, tw, th, fit_mode=fit_mode)
            z = vae.encode(canvas)
            self._check_encode(z, name)
            parts.append(z)
            looks.append(_refmod.looks_from(canvas[:1]))
            shapes.append(f"{name} 1f")
        if clip:
            px = _media.load_video(clip["file"], max_frames=clip["frames"] or None,
                                   start=clip["start"], end=clip["end"])
            if px is None:
                _refmod._fail(_refmod.source_problem(clip['file'], ("video",)) or f"mod_plan source {clip['file']} could not be read")
            px = _refmod.snap_video_frames(px)
            canvas = self._isolated(_refmod.to_canvas(px, tw, th, fit_mode=fit_mode), count - 1,
                                    count, masking)
            if (int(canvas.shape[2]), int(canvas.shape[1])) != (tw, th):
                canvas = _refmod.to_canvas(canvas, tw, th, fit_mode=fit_mode)
            z = vae.encode(canvas)
            self._check_encode(z, clip["file"])
            parts.append(z)
            looks.append(_refmod.looks_from(canvas, clip=True))
            shapes.append(f"{clip['file']} {int(px.shape[0])}f")
        print(f"[{TAG}] RefMod sources on a {tw}x{th} canvas ({fit_mode}): "
              f"{', '.join(shapes)}", flush=True)
        return _refmod.stack_latents(parts, "mod_plan"), torch.cat(looks, dim=0)

    def _check_encode(self, z, where):
        if z.ndim != 5:
            _refmod._fail(
                f"H3 VAE encode of {where} returned {tuple(z.shape)}, expected "
                f"[1,24,T,H,W]. Connect the H3 video VAE.")

    def build(self, vae, name, mod_plan="", images=None, video=None, max_tokens=0,
              subfolder="", save=True, build="stop", description="",
              subject_mask="off", background_retention=0.0,
              layout="separate_pictures", role="identity", default_step_curve="full",
              fit_mode="crop", background_mode="blur_bg", crop_margin=0.0,
              mask=None, audio=None, audio_vae=None, ref_resolution="auto", **_retired):
        # `_retired` takes `mode`, `pool`, `latent_frames` and `refine_steps`
        # from a prompt saved before training mode was removed. Pooling
        # averaged a face away; encode is the only mode that ever worked.
        description = _refmod._clean_desc_text(description)
        # A non-identity role (style, background, wardrobe, helper) can be encoded
        # smaller: fewer tokens, nothing lost that the role uses. Identity keeps the
        # full size whatever is set.
        _short = (int(ref_resolution) if str(ref_resolution) in REF_RESOLUTIONS[1:]
                  and str(role) != "identity" else 0)
        stem = name
        folder = (subfolder or "").strip().replace("\\", "/").strip("/")
        if folder:
            if any(p in ("", ".", "..") for p in folder.split("/")):
                raise ValueError(f"{TAG}: subfolder {subfolder!r} leaves the refmods folder")
            stem = f"{folder}/{name}"
        if str(build) != "start":
            print(f"[{TAG}] RefMod builder is stopped. {stem} was not written.",
                  flush=True)
            try:
                shown = _refmod._clean_name(stem) or str(name or "").strip()
            except ValueError:
                shown = str(name or "").strip()
            return (shown,)

        stills, clip, voice = _refmod.parse_mod_plan(mod_plan)
        if voice:
            problem = _refmod.source_problem(voice, ("audio",))
            if problem:
                _refmod._fail(problem)
        v_win = _refmod.voice_window(mod_plan) if voice else (0.0, 0.0)

        mask_mode = str(subject_mask or "off").strip().lower()
        is_auto_mask = mask_mode in ("auto", "auto_rembg", "auto_sam")
        masking, mask_meta = None, {}
        retention = float(background_retention or 0.0)
        if mask is not None or is_auto_mask:
            if mask is not None:
                wired = mask if mask.dim() == 3 else mask.unsqueeze(0)
                if stills or clip:
                    count = len(stills) + (1 if clip else 0)
                    ok = wired.shape[0] in (1, count) or (
                        count == 1 and clip and not stills)
                else:
                    count = 1
                    ok = True
                if not ok:
                    raise ValueError(
                        f"{TAG}: the mask batch has {wired.shape[0]} masks for "
                        f"{count} sources. Wire one mask for all of them, or "
                        f"one per source, stills then the clip.")
                if is_auto_mask:
                    print(f"[{TAG}] a mask is wired, so subject_mask={mask_mode} is not run", flush=True)
                masking = {"wired": wired, "retention": retention, "fit_mode": fit_mode,
                           "bg_mode": background_mode, "crop_margin": float(crop_margin or 0.0)}
                mask_meta = {"source": "wired", "fill": background_mode,
                             "background_retention": retention, "crop_margin": float(crop_margin or 0.0)}
            else:
                backend = "auto_sam" if mask_mode == "auto_sam" else "auto_rembg"
                masking = {"wired": None, "retention": retention, "backend": backend,
                           "fit_mode": fit_mode, "bg_mode": background_mode,
                           "crop_margin": float(crop_margin or 0.0)}
                mask_meta = {"source": backend, "model": _refmod.CUTOUT_MODEL,
                             "fill": background_mode, "background_retention": retention,
                             "crop_margin": float(crop_margin or 0.0)}
            if retention >= 1.0:
                print(f"[{TAG}] background_retention=1 keeps the whole "
                      f"background, so the mask changes nothing", flush=True)

        plan_used = {}
        if stills or clip or voice:
            plan_used = {"stills": list(stills)}
            if clip:
                plan_used["clip"] = dict(clip)
            if voice:
                v_start, v_end = v_win
                plan_used["voice"] = ({"file": voice, "start": v_start, "end": v_end}
                                      if v_start or v_end else voice)
            if images is not None or video is not None:
                print(f"[{TAG}] mod_plan names sources, so the images/video "
                      f"sockets are ignored", flush=True)

        audio_member = self._encode_audio_member(audio_vae, audio, voice, v_win)
        want_separate = (str(layout or "separate_pictures").strip() == "separate_pictures"
                         and (len(stills) > 1 or audio_member is not None or (stills and clip)))

        if (stills or clip) and want_separate:
            vis_members = self._encode_plan(
                vae, stills, clip, masking, fit_mode=fit_mode, separate=True, max_tokens=max_tokens,
                short_edge=_short)
            if audio_member is not None:
                vis_members.append(audio_member)
            if save:
                root = _refmod.register_folder()
                written = _refmod.save_v5_bundle(
                    root, stem, vis_members,
                    description=description, concept_type=role,
                    default_step_curve=default_step_curve,
                    plan=plan_used, mask=mask_meta, compact_png=True,
                )
                tok = sum(_refmod.frame_tokens(m["latent"].shape[4] * 16, m["latent"].shape[3] * 16) * m["latent"].shape[2]
                          for m in vis_members if m["kind"] != "audio")
                print(f"[{TAG}] wrote v5 RefMod bundle {written} ({len(vis_members)} member(s), "
                      f"{tok} visual tokens, role={role}, curve={default_step_curve}"
                      + (", audio latent" if audio_member else (f", voice {voice}" if voice else "")) + ")",
                      flush=True)
                return (written,)
            return (_refmod._clean_name(stem),)

        if stills or clip:
            full, looks = self._encode_plan(
                vae, stills, clip, masking, fit_mode=fit_mode, separate=False,
                short_edge=_short)
            source = "video" if clip else ("image" if len(stills) == 1 else "images")
        else:
            if images is None and video is None:
                raise ValueError(
                    f"{TAG}: add a still or a clip in the RefMod panel, or "
                    f"connect the images / video sockets.")
            if images is not None and video is not None:
                raise ValueError(f"{TAG}: connect images or a video, not both.")
            if video is not None:
                frames = _refmod.snap_video_frames(video)
                source = "video"
            else:
                frames = images[:1]
                source = "image"
            canvas = self._isolated(_refmod.fit_canvas(frames, fit_mode=fit_mode), 0, 1, masking)
            canvas = _short_edge(canvas, _short)
            full = vae.encode(canvas)
            self._check_encode(full, stem)
            looks = _refmod.looks_from(canvas, clip=video is not None)

        source_shape = "x".join(str(int(n)) for n in full.shape[2:])
        latent = _refmod.fit_token_budget(full, max_tokens, name)
        kind = "image" if int(latent.shape[2]) == 1 else "video"
        tokens = (int(latent.shape[2]) * (int(latent.shape[3]) // 2)
                  * (int(latent.shape[4]) // 2))
        if audio_member is not None:
            vis_members = [{
                "kind": kind, "name": stem, "latent": latent, "looks": looks,
                "source": source, "source_shape": source_shape,
            }, audio_member]
            if save:
                root = _refmod.register_folder()
                written = _refmod.save_v5_bundle(
                    root, stem, vis_members, description=description, concept_type=role,
                    default_step_curve=default_step_curve, plan=plan_used, mask=mask_meta,
                    compact_png=True)
                print(f"[{TAG}] wrote v5 RefMod bundle {written} ({kind} + audio latent, {tokens} visual tokens)", flush=True)
                return (written,)
            return (_refmod._clean_name(stem),)

        meta = _refmod.metadata_for(
            stem, kind, latent, "encode", source, source_shape, "encode", 0,
            plan=plan_used, description=description, mask=mask_meta,
            looks=int(looks.shape[0]), concept_type=role, default_step_curve=default_step_curve)
        if save:
            root = _refmod.register_folder()
            written = _refmod.save_latent(root, stem, latent, meta, looks=looks)
            print(f"[{TAG}] wrote RefMod {written} ({kind}, {meta['latent_t']}x"
                  f"{meta['latent_h']}x{meta['latent_w']}, {tokens} tokens, "
                  f"{meta['looks']} kept for Qwen, role={role}"
                  + (f", voice {voice}" if voice else "") + ")",
                  flush=True)
            return (written,)
        print(f"[{TAG}] RefMod {stem} was not saved (save is off)", flush=True)
        return (_refmod._clean_name(stem),)


class HTCRefModBuilder(io.ComfyNode, _HTCRefModBuilderImpl):
    """V3 schema for HTCRefModBuilder; the implementation lives in _HTCRefModBuilderImpl.

    Also inherits _HTCRefModBuilderImpl so existing callers of the V1 API (HTCRefModBuilder().build(...),
    HTCRefModBuilder.<helper>) keep working; core sees only the V3 hooks below."""

    @classmethod
    def define_schema(cls):
        return io.Schema(
            node_id='HTCRefModBuilder',
            display_name='HTC RefMod Builder',
            category="Hand Tie Clips",
            description="Retired from the node menu — use the RefMod tab inside HandTieClips. Kept for legacy workflows & Studio background builds.",
            is_output_node=True,
            is_deprecated=True,
            inputs=[
                io.Vae.Input('vae', tooltip="The H3 video VAE."),
                io.String.Input(
                    'name',
                    default="hero",
                    tooltip="File name for the RefMod under models/refmods. Pick it on a REFS row by "
                                "this name.",
                ),
                io.String.Input(
                    'mod_plan',
                    optional=True,
                    default="",
                    multiline=True,
                    tooltip="The stills, clip and voice for this RefMod, written by the panel.",
                ),
                io.Image.Input(
                    'images',
                    optional=True,
                    tooltip="Optional still to build from, instead of the panel.",
                ),
                io.Image.Input(
                    'video',
                    optional=True,
                    tooltip="Optional clip to build from, instead of the panel.",
                ),
                io.Int.Input(
                    'max_tokens',
                    optional=True,
                    default=0,
                    min=0,
                    max=1048576,
                    tooltip="Memory guard. Above this, near-duplicate frames are dropped. 0 is no "
                                "limit.",
                ),
                io.String.Input(
                    'subfolder',
                    optional=True,
                    default="",
                    tooltip="Optional folder under models/refmods.",
                ),
                io.Boolean.Input(
                    'save',
                    optional=True,
                    default=True,
                    tooltip="Write the file. Off only reports the name.",
                ),
                io.Combo.Input(
                    'build',
                    options=["stop", "start"],
                    optional=True,
                    default="stop",
                    tooltip="start builds the RefMod on this queue. stop leaves it alone, so the chain "
                                "can run without rebuilding.",
                ),
                io.String.Input(
                    'description',
                    optional=True,
                    default="",
                    tooltip="One sentence describing what the RefMod shows. Fills an empty description "
                                "on a REFS row.",
                ),
                io.Combo.Input(
                    'subject_mask',
                    options=["off", "auto_rembg", "auto_sam", "auto"],
                    optional=True,
                    default="off",
                    tooltip="auto_rembg or auto_sam cuts the subject out of each source and fills/blurs "
                                "the background before encoding. A wired mask wins.",
                ),
                io.Float.Input(
                    'background_retention',
                    optional=True,
                    default=0.0,
                    min=0.0,
                    max=1.0,
                    step=0.05,
                    tooltip="How much background a mask keeps. 0 removes/blurs it; 1 keeps it all.",
                ),
                io.Combo.Input(
                    'layout',
                    options=["separate_pictures", "stacked_video"],
                    optional=True,
                    default="separate_pictures",
                    tooltip="separate_pictures (v5 bundle, recommended): each still keeps its own natural "
                                "aspect ratio and binds as an independent <Picture N> (never eats a <Video> slot). "
                                "stacked_video: concatenates sources onto one canvas along time.",
                ),
                io.Combo.Input(
                    'role',
                    options=list(_refmod.CONCEPT_ROLES),
                    optional=True,
                    default="identity",
                    tooltip="What this RefMod represents (identity, wardrobe, bodyshape, helper, pose_motion, background, style).",
                ),
                io.Combo.Input(
                    'default_step_curve',
                    options=list(_refmod.STEP_CURVES),
                    optional=True,
                    default="full",
                    tooltip="Default denoising curve (full, early_lock for helpers/bodyshape, late_detail, fade, mid_peak).",
                ),
                io.Combo.Input(
                    'fit_mode',
                    options=["crop", "pad", "stretch"],
                    optional=True,
                    default="crop",
                    tooltip="How mismatched ratios fit a shared video canvas (crop preserves proportions, never squishes).",
                ),
                io.Combo.Input(
                    'background_mode',
                    options=["blur_bg", "pull_push", "neutral_gray"],
                    optional=True,
                    default="blur_bg",
                    tooltip="How masked background pixels are handled: blur_bg (zero color halos), pull_push, or neutral_gray.",
                ),
                io.Float.Input(
                    'crop_margin',
                    optional=True,
                    default=0.0,
                    min=0.0,
                    max=2.5,
                    step=0.05,
                    tooltip="Optional subject-centered auto-crop margin when masking is active (e.g. 1.6x; 0 = keep full framing).",
                ),
                io.Mask.Input(
                    'mask',
                    optional=True,
                    tooltip="Optional mask of the subject, white = keep. One for all sources, or one "
                                              "per source.",
                ),
                io.Audio.Input(
                    'audio',
                    optional=True,
                    tooltip="Optional voice/sound reference to embed in a v5 bundle (needs audio_vae).",
                ),
                io.Vae.Input(
                    'audio_vae',
                    optional=True,
                    tooltip="Optional H3 Audio VAE to pre-encode the RefMod voice into a portable v5 bundle.",
                ),
                io.Combo.Input(
                    'ref_resolution',
                    options=list(REF_RESOLUTIONS),
                    optional=True,
                    default="auto",
                    tooltip="Encode size (short edge, px) for a non-identity role (style, background, "
                            "wardrobe, helper): fewer tokens. auto = full size. An identity RefMod "
                            "always keeps full size.",
                ),
            ],
            outputs=[
                io.String.Output(display_name='name'),
            ],
        )

    @classmethod
    def execute(cls, **kwargs):
        result = _HTCRefModBuilderImpl().build(**kwargs)
        if isinstance(result, dict):
            return io.NodeOutput.from_dict(result)
        return io.NodeOutput(*result) if result is not None else io.NodeOutput()

    @classmethod
    def fingerprint_inputs(cls, **kwargs):
        return _HTCRefModBuilderImpl.IS_CHANGED(**_v3_accepted(_HTCRefModBuilderImpl.IS_CHANGED, kwargs))


NODE_CLASS_MAPPINGS = {"HTCRefModBuilder": HTCRefModBuilder}
NODE_DISPLAY_NAME_MAPPINGS = {"HTCRefModBuilder": "HTC RefMod Builder"}