1+ # last_frame_extractor_node.py
2+
3+ import torch
4+
5+ # This is the main class for your custom node
6+ class LastFrameExtractor :
7+ # A unique name for your node within the node selector
8+ @classmethod
9+ def INPUT_TYPES (s ):
10+ """
11+ Defines the input types for the node.
12+ 'IMAGE' is the type that comes from a VAE Decode or similar node,
13+ which is usually a batch of images (a tensor).
14+ """
15+ return {
16+ "required" : {
17+ # The 'IMAGE' type is a tensor of shape [B, H, W, C]
18+ # where B is the batch size (number of frames).
19+ "image_batch" : ("IMAGE" , {}),
20+ }
21+ }
22+
23+ # The category where your node will appear in the ComfyUI menu
24+ RETURN_TYPES = ("IMAGE" ,)
25+ FUNCTION = "extract_last_frame"
26+ CATEGORY = "utilities/frame"
27+
28+ def extract_last_frame (self , image_batch ):
29+ """
30+ The main function that performs the node's logic.
31+
32+ image_batch: A torch.Tensor of shape [B, H, W, C]
33+ """
34+
35+ # 1. Check if the batch is empty
36+ if image_batch .nelement () == 0 :
37+ # Return an empty tensor if the input is empty
38+ print ("Warning: Input image batch is empty." )
39+ # Create a 1x1x1x3 tensor of zeros to prevent connection errors
40+ return (torch .zeros ((1 , 1 , 1 , 3 )),)
41+
42+ # 2. Get the last frame from the batch
43+ # The batch is a tensor where the first dimension is the batch size (B).
44+ # In Python/PyTorch, -1 indexes the last element of the first dimension.
45+ last_frame = image_batch [- 1 ]
46+
47+ # 3. Reshape the single frame back into a batch of size 1
48+ # ComfyUI expects an output of shape [B, H, W, C], so we add
49+ # the batch dimension back (size 1).
50+ last_frame_batch = last_frame .unsqueeze (0 )
51+
52+ # Return the output as a tuple, which is the ComfyUI standard
53+ return (last_frame_batch ,)
54+
55+ # A list of classes to expose to ComfyUI
56+ NODE_CLASS_MAPPINGS = {
57+ "LastFrameExtractor" : LastFrameExtractor
58+ }
59+
60+ # A dictionary to set friendly names for the nodes in the UI
61+ NODE_DISPLAY_NAME_MAPPINGS = {
62+ "LastFrameExtractor" : "Last Frame Extractor"
63+ }
0 commit comments