-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWanLoop_FullNodes.py
More file actions
457 lines (402 loc) · 29.1 KB
/
Copy pathWanLoop_FullNodes.py
File metadata and controls
457 lines (402 loc) · 29.1 KB
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
# ==============================
# WanAnimateToVideoCustom 节点(独立精简版)
# 支持 pose/face 强度控制、尾帧分段掩码、中性灰混合、上下文模式等
# ==============================
import torch
import logging
import node_helpers
import nodes
import comfy.utils
def _wan_4n1_video_to_latent(video_frames):
"""视频帧数转潜在空间帧数(Wan 编码器每4帧压缩为1帧)"""
if video_frames < 1:
return 1
return (video_frames - 1) // 4 + 1
class WanAnimateToVideoCustom:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"positive": ("CONDITIONING", {"tooltip": "正向提示词 conditioning。Positive prompt conditioning."}),
"negative": ("CONDITIONING", {"tooltip": "负向提示词 conditioning。Negative prompt conditioning."}),
"vae": ("VAE", {"tooltip": "Wan 模型的 VAE,用于编码参考图和窗口帧。VAE for the Wan model, used to encode reference images and window frames."}),
"width": ("INT", {"default": 832, "min": 16, "max": nodes.MAX_RESOLUTION, "step": 16, "tooltip": "生成视频的宽度(像素),必须是 16 的倍数。Width of the generated video (pixels), must be a multiple of 16."}),
"height": ("INT", {"default": 480, "min": 16, "max": nodes.MAX_RESOLUTION, "step": 16, "tooltip": "生成视频的高度(像素),必须是 16 的倍数。Height of the generated video (pixels), must be a multiple of 16."}),
"length": ("INT", {"default": 77, "min": 1, "max": nodes.MAX_RESOLUTION, "step": 4, "tooltip": "生成视频的帧数(像素帧),必须是 4 的倍数。Number of frames for the generated video (pixel frames), must be a multiple of 4."}),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 4096, "tooltip": "批量大小,通常保持为 1。Batch size, usually keep at 1."}),
"continue_motion_max_frames": ("INT", {"default": 5, "min": 1, "max": nodes.MAX_RESOLUTION, "step": 1, "tooltip": "从上一块携带的最大帧数(RGB 图像),用于块间接续。Maximum frames carried from the previous chunk (RGB images), used for inter-chunk continuity."}),
"video_frame_offset": ("INT", {"default": 0, "min": 0, "max": nodes.MAX_RESOLUTION, "step": 1, "tooltip": "当前 chunk 的帧偏移量,从上一块的 video_frame_offset 输出接入。Frame offset of the current chunk, connected from the previous chunk's video_frame_offset output."}),
"transition_width": ("INT", {"default": 0, "min": 0, "max": 128, "step": 4, "tooltip": "fix 模式下黑帧区域的过渡区宽度,0=禁用过渡。Transition zone width for black frame areas in fix mode, 0=disable transition."}),
"mode": (["fix", "legacy", "vanilla"], {"default": "vanilla", "tooltip": "掩码模式:vanilla=官方行为(fix/legacy不动),fix=黑帧检测+过渡,legacy=尾帧渐变。Mask mode: vanilla=official behavior, fix=black frame detection+transition, legacy=tail frame fade."}),
"tail_frame_count": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1, "tooltip": "legacy 模式下尾帧处理帧数,0=禁用。Number of tail frames to process in legacy mode, 0=disabled."}),
"tail_start_strength": ("FLOAT", {"default": 0, "min": 0.0, "max": 1.0, "step": 0.05, "tooltip": "legacy 模式尾帧起始强度。Tail frame start strength in legacy mode."}),
"tail_end_strength": ("FLOAT", {"default": 0, "min": 0.0, "max": 1.0, "step": 0.05, "tooltip": "legacy 模式尾帧结束强度。Tail frame end strength in legacy mode."}),
"ref_mode": (["原模式", "兼容模式"], {"default": "原模式", "tooltip": "原模式=内部1+4n排列后批量编码(接selected_images);兼容模式=逐帧独立编码(接selected_images,兼容EverAnimate LoRA)。Original mode=internal 1+4n arrangement then batch encode; Compatible mode=per-frame independent encode (compatible with EverAnimate LoRA)."}),
},
"optional": {
"clip_vision_output": ("CLIP_VISION_OUTPUT", {"tooltip": "CLIP Vision 输出,用于参考图语义理解。CLIP Vision output for reference image semantic understanding."}),
"reference_image": ("IMAGE", {"tooltip": "参考图输入。接参考图选择器的 selected_images(排序后的原始图片,节点内部自动处理1+4n或逐帧编码)。Reference image input. Connect to selected_images from ReferenceImageSelector (node handles 1+4n or per-frame encoding internally)."}),
"face_video": ("IMAGE", {"tooltip": "面部视频帧序列,用于面部引导。Face video frame sequence for face guidance."}),
"pose_video": ("IMAGE", {"tooltip": "姿态视频帧序列,用于姿态引导。Pose video frame sequence for pose guidance."}),
"continue_motion": ("IMAGE", {"tooltip": "上一块的末尾 RGB 帧,用于块间运动接续。Last RGB frames of the previous chunk for inter-chunk motion continuity."}),
"background_video": ("IMAGE", {"tooltip": "背景视频帧序列,用于替换/增强背景。Background video frame sequence for background replacement/enhancement."}),
"character_mask": ("MASK", {"tooltip": "角色遮罩,用于精确控制角色区域的保护。Character mask for precise control of character area protection."}),
"yaw_angles": ("FLOAT", {"default": 0.0, "min": -180.0, "max": 180.0, "tooltip": "偏航角序列(像素帧级别)。Yaw angle sequence (pixel frame level)."}),
"face_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "面部引导强度,0=关闭面部引导。Face guidance strength, 0=disable face guidance."}),
"pose_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 2.0, "step": 0.01, "tooltip": "姿态引导强度,0=关闭姿态引导,>1=增强姿态影响。Pose guidance strength, 0=disable pose guidance, >1=enhance pose influence."}),
"mid_frame": ("INT", {"default": -1, "min": -1, "max": 1000, "step": 1, "tooltip": "legacy 模式中间帧锚点位置,-1=不使用。Mid-frame anchor position in legacy mode, -1=not used."}),
"mid_strength": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.05, "tooltip": "legacy 模式中间帧锚点强度。Mid-frame anchor strength in legacy mode."}),
"neutral_mix_min": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "legacy 模式掩码=0时的中性灰混合比例。Neutral gray mix ratio when mask=0 in legacy mode."}),
"neutral_mix_max": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "legacy 模式掩码=1时的中性灰混合比例。Neutral gray mix ratio when mask=1 in legacy mode."}),
"prev_latent": ("LATENT", {"tooltip": "前一段输出的完整 latent(concat_latent),接入后忽略 continue_motion,直接用前一段潜变量替换中性灰帧编码后的 latent。Complete latent output from previous chunk, when connected ignores continue_motion and uses previous latent to replace neutral gray frame encoded latent."}),
}
}
RETURN_TYPES = ("CONDITIONING", "CONDITIONING", "LATENT", "INT", "INT", "INT", "LATENT", "FLOAT")
RETURN_NAMES = ("positive", "negative", "latent", "trim_latent", "trim_image", "video_frame_offset", "concat_latent", "latent_yaw_angles")
FUNCTION = "process"
CATEGORY = "WanLoop/整合节点"
def process(self, positive, negative, vae, width, height, length, batch_size,
continue_motion_max_frames, video_frame_offset, transition_width,
mode, tail_frame_count, tail_start_strength, tail_end_strength,
ref_mode,
clip_vision_output=None, reference_image=None,
face_video=None, pose_video=None, continue_motion=None,
background_video=None, character_mask=None,
face_strength=1.0, pose_strength=1.0,
mid_frame=-1, mid_strength=0.5,
neutral_mix_min=0.0, neutral_mix_max=1.0,
yaw_angles=None, prev_latent=None):
trim_to_pose_video = False
latent_length = ((length - 1) // 4) + 1
latent_width = width // 8
latent_height = height // 8
trim_latent = 0
ref_motion_latent_length = 0
_prev_latent_samples = None
_motion_latent_count = 0
# ----- 参考图像处理(根据模式走不同路径) -----
if reference_image is None:
reference_image = torch.zeros((1, height, width, 3))
if ref_mode == "兼容模式":
# 兼容模式:逐帧独立 VAE 编码(模拟 EverAnimate 单帧编码方式)
encoded_list = []
num_ref_frames = reference_image.shape[0]
for i in range(num_ref_frames):
single_img = reference_image[i:i+1]
single_img = comfy.utils.common_upscale(
single_img.movedim(-1, 1), width, height, "area", "center"
).movedim(1, -1)
single_latent = vae.encode(single_img[:, :, :, :3])
encoded_list.append(single_latent)
concat_latent_image = torch.cat(encoded_list, dim=2)
mask = torch.zeros((1, 4, concat_latent_image.shape[-3],
concat_latent_image.shape[-2],
concat_latent_image.shape[-1]),
device=concat_latent_image.device, dtype=concat_latent_image.dtype)
trim_latent += concat_latent_image.shape[2]
# 构造占位 image 用于后续兼容(不会被实际用于编码)
image = torch.ones((length, height, width, 3)) * 0.5
else:
# 原模式:内部做 1+4n 排列后再批量编码
# 第1张参考图 ×1 + 其余参考图(每张×4)
ref_batch_parts = [reference_image[0:1]]
if reference_image.shape[0] > 1:
for i in range(1, reference_image.shape[0]):
ref_batch_parts.append(reference_image[i:i+1].repeat(4, 1, 1, 1))
ref_batch = torch.cat(ref_batch_parts, dim=0)
# 限制到 length 帧
ref_batch = ref_batch[:length]
image = comfy.utils.common_upscale(ref_batch.movedim(-1, 1), width, height, "area", "center").movedim(1, -1)
concat_latent_image = vae.encode(image[:, :, :, :3])
mask = torch.zeros((1, 4, concat_latent_image.shape[-3], concat_latent_image.shape[-2], concat_latent_image.shape[-1]),
device=concat_latent_image.device, dtype=concat_latent_image.dtype)
trim_latent += concat_latent_image.shape[2]
# ----- continue_motion 处理(支持前一段 latent 直传) -----
if prev_latent is not None:
# 潜变量直传模式:忽略 continue_motion 像素帧,直接用中性灰占位
motion_latent_count = ((continue_motion_max_frames - 1) // 4) + 1
ref_motion_latent_length = motion_latent_count
image = torch.ones((length, height, width, 3)) * 0.5
_prev_latent_samples = prev_latent["samples"]
_motion_latent_count = motion_latent_count
logging.info("[PrevLatent] 启用潜变量直传,从上一段截取 %d 帧 latent(等效 %d 像素帧)",
motion_latent_count, continue_motion_max_frames)
elif continue_motion is None:
image = torch.ones((length, height, width, 3)) * 0.5
else:
continue_motion = continue_motion[-continue_motion_max_frames:]
video_frame_offset -= continue_motion.shape[0]
video_frame_offset = max(0, video_frame_offset)
continue_motion = comfy.utils.common_upscale(continue_motion[-length:].movedim(-1, 1), width, height, "area", "center").movedim(1, -1)
image = torch.ones((length, height, width, continue_motion.shape[-1]), device=continue_motion.device, dtype=continue_motion.dtype) * 0.5
image[:continue_motion.shape[0]] = continue_motion
ref_motion_latent_length += ((continue_motion.shape[0] - 1) // 4) + 1
# ----- clip_vision 处理 -----
if clip_vision_output is not None:
positive = node_helpers.conditioning_set_values(positive, {"clip_vision_output": clip_vision_output})
negative = node_helpers.conditioning_set_values(negative, {"clip_vision_output": clip_vision_output})
# ----- pose_video 处理 -----
if pose_video is not None:
if pose_video.shape[0] <= video_frame_offset:
pose_video = None
else:
pose_video = pose_video[video_frame_offset:]
if pose_video is not None:
ref_pixel_frames = reference_image.shape[0] if reference_image is not None else 1
ref_pixel_frames = min(ref_pixel_frames, length)
if pose_video.shape[0] < length:
pose_video = torch.cat([
pose_video,
pose_video[-1:].repeat(length - pose_video.shape[0], 1, 1, 1)
], dim=0)
else:
pose_video = pose_video[:length]
pose_video = comfy.utils.common_upscale(
pose_video.movedim(-1, 1), width, height, "area", "center"
).movedim(1, -1)
pose_video_latent = vae.encode(pose_video[:, :, :, :3])
# 前补 latent 帧以对齐 concat_latent_image 的 anchor 部分
delta = trim_latent - 1
if delta > 0:
zero_pad = torch.zeros_like(pose_video_latent[:, :, :1, :, :]).repeat(1, 1, delta, 1, 1)
pose_video_latent = torch.cat([zero_pad, pose_video_latent], dim=2)
logging.info("[PoseVideo] 已在开头补 %d 帧零 latent,总长度 = %d", delta, pose_video_latent.shape[2])
positive = node_helpers.conditioning_set_values(positive, {"pose_video_latent": pose_video_latent})
negative = node_helpers.conditioning_set_values(negative, {"pose_video_latent": pose_video_latent})
if trim_to_pose_video:
latent_length = pose_video_latent.shape[2]
length = latent_length * 4 - 3
image = image[:length]
# ----- face_video 处理 -----
if face_video is not None:
if face_video.shape[0] <= video_frame_offset:
face_video = None
else:
face_video = face_video[video_frame_offset:]
if face_video is not None:
if reference_image is not None:
# 前补像素帧以对齐 concat_latent_image 的 anchor 部分
prefix_frames_to_add = max(0, (trim_latent - 1) * 4)
if prefix_frames_to_add > 0:
neutral_pad = torch.full((prefix_frames_to_add, *face_video.shape[1:]), -1.0, device=face_video.device, dtype=face_video.dtype)
face_video = torch.cat([neutral_pad, face_video], dim=0)
logging.info("[FaceVideo] 已在开头补 %d 帧 -1.0,总帧数 = %d", prefix_frames_to_add, face_video.shape[0])
face_video = comfy.utils.common_upscale(face_video.movedim(-1, 1), 512, 512, "area", "center") * 2.0 - 1.0
face_video = face_video.movedim(0, 1).unsqueeze(0)
if face_strength != 1.0:
face_video = face_video * face_strength
positive = node_helpers.conditioning_set_values(positive, {"face_video_pixels": face_video})
negative = node_helpers.conditioning_set_values(negative, {"face_video_pixels": face_video * 0.0 - 1.0})
logging.info("[FaceVideo] 最终传给模型的总帧数: %d", face_video.shape[2])
# ----- 背景和遮罩处理 -----
ref_images_num = max(0, ref_motion_latent_length * 4 - 3)
# EverAnimate 兼容的保护帧数(character_mask/background_video 偏移量用)
continue_motion_latents = ref_motion_latent_length
if prev_latent is not None:
continue_motion_frames = continue_motion_max_frames
else:
continue_motion_frames = continue_motion.shape[0] if continue_motion is not None else 0
effective_frame_offset = max(0, int(video_frame_offset) - continue_motion_frames)
protected_frames = ref_motion_latent_length * 4
if background_video is not None:
if background_video.shape[0] > effective_frame_offset:
background_video = background_video[effective_frame_offset:]
background_video = comfy.utils.common_upscale(background_video[:length].movedim(-1, 1), width, height, "area", "center").movedim(1, -1)
if background_video.shape[0] > ref_images_num:
image[ref_images_num:background_video.shape[0]] = background_video[ref_images_num:]
# ----- 基于 continue_motion 内容生成掩码 -----
mask_refmotion = torch.ones((1, 1, latent_length * 4, latent_height, latent_width),
device=concat_latent_image.device, dtype=concat_latent_image.dtype)
if prev_latent is not None:
# 潜变量直传模式:无 continue_motion 像素帧,掩码简化处理
N = ref_motion_latent_length * 4
if mode == "vanilla":
mask_refmotion[:, :, :N, :, :] = 0.0
elif mode == "legacy" and tail_frame_count > 0:
# legacy 模式基于帧数计算,不依赖像素内容,可以正常工作
tail_len = min(tail_frame_count, N)
if tail_len > 0:
use_mid = (mid_frame >= 1 and mid_frame <= tail_len)
if use_mid:
mid_idx = mid_frame - 1
strengths_first = torch.linspace(tail_start_strength, mid_strength, mid_idx + 1, device=mask_refmotion.device)
strengths_second = torch.linspace(mid_strength, tail_end_strength, tail_len - mid_idx, device=mask_refmotion.device)
strengths = torch.cat([strengths_first, strengths_second[1:]])
logging.info("[PrevLatent][legacy模式] 使用中间帧锚点:第%d帧强度=%.2f,尾帧强度序列长度=%d", mid_frame, mid_strength, tail_len)
else:
strengths = torch.linspace(tail_start_strength, tail_end_strength, tail_len, device=mask_refmotion.device)
logging.info("[PrevLatent][legacy模式] 未启用中间帧锚点,使用线性插值")
float_mask = torch.zeros(N, device=mask_refmotion.device)
for i in range(tail_len):
frame_idx = N - tail_len + i
float_mask[frame_idx] = max(float_mask[frame_idx].item(), strengths[i].item())
expanded_float_mask = float_mask.view(1, 1, N, 1, 1)
mask_refmotion[:, :, :N, :, :] = expanded_float_mask
# fix 模式在潜变量直传时退化为 vanilla(无像素帧可做黑帧检测)
elif continue_motion is not None:
N = continue_motion.shape[0]
if mode == "vanilla":
# vanilla 模式:完全复刻官方行为,无条件 mask=0
ref_motion_latent_length = ((N - 1) // 4) + 1
mask_refmotion[:, :, :ref_motion_latent_length * 4, :, :] = 0.0
else:
# fix / legacy 模式:保留自定义的黑帧检测和尾帧处理
is_black = (continue_motion.abs().max(dim=-1)[0].max(dim=-1)[0].max(dim=-1)[0] < 1e-6)
binary_mask = is_black.float()
float_mask = binary_mask.clone().float()
if mode == "fix" and transition_width > 0:
trans = transition_width
black_regions = []
i = 0
while i < N:
if is_black[i]:
start = i
while i < N and is_black[i]:
i += 1
end = i - 1
black_regions.append((start, end))
else:
i += 1
for start, end in black_regions:
left_start = max(0, start - trans)
left_end = start - 1
for j in range(left_start, left_end + 1):
dist = start - j
value = max(0.0, 1.0 - (dist - 1) / trans)
float_mask[j] = max(float_mask[j], value)
right_start = end + 1
right_end = min(N - 1, end + trans)
for j in range(right_start, right_end + 1):
dist = j - end
value = max(0.0, 1.0 - (dist - 1) / trans)
float_mask[j] = max(float_mask[j], value)
# fix 模式:纯黑帧硬替换为中性灰
for i in range(N):
if binary_mask[i] == 1:
image[i] = 0.5
if mode == "legacy" and tail_frame_count > 0:
tail_len = min(tail_frame_count, N)
if tail_len > 0:
use_mid = (mid_frame >= 1 and mid_frame <= tail_len)
if use_mid:
mid_idx = mid_frame - 1
strengths_first = torch.linspace(tail_start_strength, mid_strength, mid_idx + 1, device=float_mask.device)
strengths_second = torch.linspace(mid_strength, tail_end_strength, tail_len - mid_idx, device=float_mask.device)
strengths = torch.cat([strengths_first, strengths_second[1:]])
logging.info("[legacy模式] 使用中间帧锚点:第%d帧强度=%.2f,尾帧强度序列长度=%d", mid_frame, mid_strength, tail_len)
else:
strengths = torch.linspace(tail_start_strength, tail_end_strength, tail_len, device=float_mask.device)
logging.info("[legacy模式] 未启用中间帧锚点,使用线性插值")
for i in range(tail_len):
frame_idx = N - tail_len + i
float_mask[frame_idx] = max(float_mask[frame_idx].item(), strengths[i].item())
if neutral_mix_min != 0.0 or neutral_mix_max != 1.0:
logging.info("[legacy模式] 中性灰混合比例范围:掩码0时=%.2f, 掩码1时=%.2f", neutral_mix_min, neutral_mix_max)
for i in range(tail_len):
frame_idx = N - tail_len + i
mask_strength = strengths[i].item()
mix_alpha = neutral_mix_min + (neutral_mix_max - neutral_mix_min) * mask_strength
if mix_alpha > 0:
original = image[frame_idx]
neutral = torch.full_like(original, 0.5)
image[frame_idx] = original * (1 - mix_alpha) + neutral * mix_alpha
float_mask = float_mask.to(device=concat_latent_image.device)
expanded_float_mask = float_mask.view(1, 1, N, 1, 1)
mask_refmotion[:, :, :N, :, :] = expanded_float_mask
# ----- character_mask 处理 -----
if character_mask is not None:
if character_mask.shape[0] > effective_frame_offset or character_mask.shape[0] == 1:
if character_mask.shape[0] == 1:
character_mask = character_mask.repeat((length,) + (1,) * (character_mask.ndim - 1))
else:
character_mask = character_mask[effective_frame_offset:]
if character_mask.ndim == 3:
character_mask = character_mask.unsqueeze(1)
character_mask = character_mask.movedim(0, 1)
if character_mask.ndim == 4:
character_mask = character_mask.unsqueeze(1)
character_mask = comfy.utils.common_upscale(character_mask[:, :, :length],
concat_latent_image.shape[-1],
concat_latent_image.shape[-2],
"nearest-exact", "center")
if character_mask.shape[2] > protected_frames:
mask_refmotion[:, :, protected_frames:character_mask.shape[2]] = character_mask[:, :, protected_frames:]
# ----- 拼接最终的 concat_latent_image(潜变量直传模式下替换前段 latent) -----
image_latent = vae.encode(image[:, :, :, :3])
if _prev_latent_samples is not None:
# 用前一段的潜变量替换最前面的 motion_latent_count 帧
trimmed = _prev_latent_samples[:, :, -_motion_latent_count:, :, :].to(device=image_latent.device, dtype=image_latent.dtype)
actual_count = min(_motion_latent_count, image_latent.shape[2])
image_latent[:, :, :actual_count, :, :] = trimmed[:, :, :actual_count, :, :]
logging.info("[PrevLatent] 已替换前 %d 帧 latent(传入 %d 帧,取末尾 %d 帧)",
actual_count, _prev_latent_samples.shape[2], _motion_latent_count)
concat_latent_image = torch.cat((concat_latent_image, image_latent), dim=2)
mask_refmotion = mask_refmotion.view(1, mask_refmotion.shape[2] // 4, 4, mask_refmotion.shape[3], mask_refmotion.shape[4]).transpose(1, 2)
mask = torch.cat((mask, mask_refmotion), dim=2)
positive = node_helpers.conditioning_set_values(positive, {"concat_latent_image": concat_latent_image, "concat_mask": mask})
negative = node_helpers.conditioning_set_values(negative, {"concat_latent_image": concat_latent_image, "concat_mask": mask})
latent = torch.zeros([batch_size, 16, latent_length + trim_latent, latent_height, latent_width],
device=comfy.model_management.intermediate_device())
out_latent = {"samples": latent}
trim_image = max(0, ref_motion_latent_length * 4 - 3)
# ----- pose_strength 强度控制(conditioning 层缩放) -----
if pose_strength != 1.0:
pos_list = []
for cond in positive:
c = cond[0]
cond_dict = cond[1].copy()
if "pose_video_latent" in cond_dict:
cond_dict["pose_video_latent"] = cond_dict["pose_video_latent"] * pose_strength
logging.info(f"[WanAnimateToVideoCustom] pose_strength={pose_strength}, "
f"pose_video_latent_shape={cond_dict['pose_video_latent'].shape}")
pos_list.append([c, cond_dict])
positive = pos_list
neg_list = []
for cond in negative:
c = cond[0]
cond_dict = cond[1].copy()
if "pose_video_latent" in cond_dict:
cond_dict["pose_video_latent"] = cond_dict["pose_video_latent"] * pose_strength
neg_list.append([c, cond_dict])
negative = neg_list
# ----- latent_yaw_angles 下采样 -----
if yaw_angles is not None:
latent_yaw_angles = self._downsample_yaw_to_latent(yaw_angles, latent_length + trim_latent)
else:
latent_yaw_angles = None
return (positive, negative, out_latent, trim_latent, trim_image, video_frame_offset + length, {"samples": concat_latent_image}, latent_yaw_angles)
# ==================== 辅助方法 ====================
def _downsample_yaw_to_latent(self, yaw_angles, total_latent_len):
"""将像素帧偏航角下采样为 latent 帧偏航角(每4帧取首值)"""
if yaw_angles is None:
return [0.0] * total_latent_len
# 展平为 list[float]
if isinstance(yaw_angles, (int, float)):
yaw_list = [float(yaw_angles)]
elif isinstance(yaw_angles, list):
yaw_list = [float(v) for v in yaw_angles]
elif isinstance(yaw_angles, torch.Tensor):
yaw_list = yaw_angles.flatten().tolist()
else:
yaw_list = []
if len(yaw_list) == 0:
return [0.0] * total_latent_len
# 每 4 个像素帧取一个 latent 帧的值(取第一个像素帧的角度)
latent_yaw = []
for i in range(total_latent_len):
pixel_idx = i * 4
if pixel_idx < len(yaw_list):
latent_yaw.append(float(yaw_list[pixel_idx]))
else:
latent_yaw.append(float(yaw_list[-1]))
return latent_yaw
# ==============================
# 节点注册(仅保留此节点)
# ==============================
NODE_CLASS_MAPPINGS = {
"WanAnimateToVideoCustom": WanAnimateToVideoCustom,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"WanAnimateToVideoCustom": "WanAnimate To Video (自定义)",
}
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]