File size: 16,706 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#---------------------------------------------------------------------------------------------------------------------#
# CR Animation Nodes by RockOfFire and Akatsuzi     https://github.com/Suzie1/CR-Animation-Nodes
# for ComfyUI                                       https://github.com/comfyanonymous/ComfyUI
#---------------------------------------------------------------------------------------------------------------------#

import comfy.sd
import torch
import os
import sys
import folder_paths
import random
from PIL import Image, ImageEnhance
import numpy as np
import io
from ..categories import icons

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy"))
#---------------------------------------------------------------------------------------------------------------------# 
# FUNCTIONS
#---------------------------------------------------------------------------------------------------------------------# 
def pil2tensor(image):
    return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
#---------------------------------------------------------------------------------------------------------------------# 
# NODES
#---------------------------------------------------------------------------------------------------------------------# 
class CR_CycleModels:

    @classmethod
    def INPUT_TYPES(s):
    
        modes = ["Off", "Sequential"]

        return {"required": {"mode": (modes,),
                             "model": ("MODEL",),
                             "clip": ("CLIP",),
                             "model_list": ("MODEL_LIST",),
                             "frame_interval": ("INT", {"default": 30, "min": 0, "max": 999, "step": 1,}),        
                             "loops": ("INT", {"default": 1, "min": 1, "max": 1000}),
                             "current_frame": ("INT", {"default": 0.0, "min": 0.0, "max": 9999.0, "step": 1.0,}),
                },
        }
    
    RETURN_TYPES = ("MODEL", "CLIP", "VAE", "STRING", )
    RETURN_NAMES = ("MODEL", "CLIP", "VAE", "show_help", )
    FUNCTION = "cycle_models"
    CATEGORY = icons.get("Comfyroll/Animation/Legacy")

    def cycle_models(self, mode, model, clip, model_list, frame_interval, loops, current_frame,):
        show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Cycler-Nodes#cr-cycle-models"

        # Initialize the list
        model_params = list()

        # Extend lora_params with the lora_list items
        if model_list:
            for _ in range(loops):
                model_params.extend(model_list)
            #print(f"[Debug] CR Cycle Models:{model_params}")
                
        if mode == "Off":
            return (model, clip, show_help, )               

        elif mode == "Sequential":
            if current_frame == 0:
                return (model, clip, show_help, ) 
            else:    
                # Calculate the index of the current model based on the current_frame and frame_interval
                current_model_index = (current_frame // frame_interval) % len(model_params)
                #print(f"[Debug] CR Cycle Models:{current_model_index}")
                
                # Get the parameters of the current model
                current_model_params = model_params[current_model_index]
                model_alias, ckpt_name = current_model_params
                print(f"[Info] CR Cycle Models: Current model is {ckpt_name}")
                
                # Load the current model
                ckpt_path = folder_paths.get_full_path("checkpoints", ckpt_name)
                out = comfy.sd.load_checkpoint_guess_config(ckpt_path, output_vae=True, output_clip=True, 
                embedding_directory=folder_paths.get_folder_paths("embeddings"))
                return (out, show_help, )
        #else:
        #    return (model, clip) 
 
#---------------------------------------------------------------------------------------------------------------------# 
class CR_CycleLoRAs:

    @classmethod
    def INPUT_TYPES(s):
        
        modes = ["Off", "Sequential"]
    
        return {"required": {"mode": (modes,),
                             "model": ("MODEL",),
                             "clip": ("CLIP",),
                             "lora_list": ("LORA_LIST",),
                             "frame_interval": ("INT", {"default": 30, "min": 0, "max": 999, "step": 1,}),
                             "loops": ("INT", {"default": 1, "min": 1, "max": 1000}),
                             "current_frame": ("INT", {"default": 0.0, "min": 0.0, "max": 9999.0, "step": 1.0,}),                             
                },
        }
    
    RETURN_TYPES = ("MODEL", "CLIP", "STRING", )
    RETURN_NAMES = ("MODEL", "CLIP", "show_help", )
    FUNCTION = "cycle"
    CATEGORY = icons.get("Comfyroll/Animation/Legacy")

    def cycle(self, mode, model, clip, lora_list, frame_interval, loops, current_frame):
        show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Cycler-Nodes#cr-cycle-loras"

        # Initialize the list
        lora_params = list()

        # Extend lora_params with lora_list items
        if lora_list:
            for _ in range(loops):
                lora_params.extend(lora_list)
            #print(f"[Debug] CR Cycle LoRAs:{lora_params}")            
        else:
            return (model, clip, show_help, )

        if mode == "Sequential":
            # Calculate the index of the current LoRA based on the current_frame and frame_interval
            current_lora_index = (current_frame // frame_interval) % len(lora_params)
            #print(f"[Debug] CR Cycle LoRAs:{current_lora_index}")
            
            # Get the parameters of the current LoRA
            current_lora_params = lora_params[current_lora_index]
            lora_alias, lora_name, model_strength, clip_strength = current_lora_params
            
            # Load the current LoRA
            lora_path = folder_paths.get_full_path("loras", lora_name)
            lora = comfy.utils.load_torch_file(lora_path, safe_load=True)
            print(f"[Info] CR_CycleLoRAs: Current LoRA is {lora_name}")

            # Apply the current LoRA to the model and clip
            model_lora, clip_lora = comfy.sd.load_lora_for_models(
            model, clip, lora, model_strength, clip_strength)
            return (model_lora, clip_lora, show_help, )
        else:
            return (model, clip, show_help, )

#---------------------------------------------------------------------------------------------------------------------#        
class CR_CycleText:

    @classmethod
    def INPUT_TYPES(s):
    
        modes = ["Sequential"]
    
        return {"required": {"mode": (modes,),
                             "text_list": ("TEXT_LIST",),
                             "frame_interval": ("INT", {"default": 30, "min": 0, "max": 999, "step": 1,}),         
                             "loops": ("INT", {"default": 1, "min": 1, "max": 1000}),
                             "current_frame": ("INT", {"default": 0.0, "min": 0.0, "max": 9999.0, "step": 1.0,}),
                },
        }
    
    RETURN_TYPES = ("STRING", "STRING", )
    RETURN_NAMES = ("STRING", "show_help", )
    FUNCTION = "cycle_text"
    CATEGORY = icons.get("Comfyroll/Animation/Legacy")

    def cycle_text(self, mode, text_list, frame_interval, loops, current_frame,):
        
        # Initialize the list
        text_params = list()

        # Extend text_params with text_list items
        if text_list:
            for _ in range(loops):
                text_params.extend(text_list)
            #print(f"[Debug] CR Cycle Text:{text_params}")

        if mode == "Sequential":
            # Calculate the index of the current text string based on the current_frame and frame_interval
            current_text_index = (current_frame // frame_interval) % len(text_params)
            #print(f"[Debug] CR Cycle Text:{current_text_index}")

            # Get the parameters of the current text            
            current_text_params = text_params[current_text_index]
            print(f"[Debug] CR Cycle Text:{current_text_params}")
            text_alias, current_text_item = current_text_params            
            #print(f"[Debug] CR Cycle Text:{current_text_item}")
            
            show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Cycler-Nodes#cr-cycle-text"

            return (current_text_item, show_help, )

#---------------------------------------------------------------------------------------------------------------------#        
class CR_CycleTextSimple:

    @classmethod
    def INPUT_TYPES(s):
    
        modes = ["Sequential"]
    
        return {"required": {"mode": (modes,),
                             "frame_interval": ("INT", {"default": 30, "min": 0, "max": 999, "step": 1,}),         
                             "loops": ("INT", {"default": 1, "min": 1, "max": 1000}),
                             "current_frame": ("INT", {"default": 0.0, "min": 0.0, "max": 9999.0, "step": 1.0,}),
                },
                "optional": {"text_1": ("STRING", {"multiline": False, "default": ""}),
                             "text_2": ("STRING", {"multiline": False, "default": ""}),
                             "text_3": ("STRING", {"multiline": False, "default": ""}),
                             "text_4": ("STRING", {"multiline": False, "default": ""}),               
                             "text_5": ("STRING", {"multiline": False, "default": ""}),
                             "text_list_simple": ("TEXT_LIST_SIMPLE",),
                },                                           
        }
    
    RETURN_TYPES = ("STRING", "STRING", )
    RETURN_NAMES = ("STRING", "show_help", )
    FUNCTION = "cycle_text"
    CATEGORY = icons.get("Comfyroll/Animation/Legacy")

    def cycle_text(self, mode, frame_interval, loops, current_frame,
        text_1, text_2, text_3, text_4, text_5,
        text_list_simple=None ):
        
        # Initialize the list
        text_params = list()
        
        text_list = list() 
        if text_1 != "":
            text_list.append(text_1)
        if text_2 != "":     
            text_list.append(text_2)
        if text_3 != "":             
            text_list.append(text_3)
        if text_4 != "":             
            text_list.append(text_4)
        if text_5 != "": 
            text_list.append(text_5)
        
        # Extend text_params with text items
        for _ in range(loops):
            if text_list_simple:
                text_params.extend(text_list_simple)
            text_params.extend(text_list)     
        #print(f"[Debug] CR Cycle Text:{len(text_params)}")
        #print(f"[Debug] CR Cycle Text:{text_params}")
        
        if mode == "Sequential":
            # Calculate the index of the current text string based on the current_frame and frame_interval
            current_text_index = (current_frame // frame_interval) % len(text_params)
            #print(f"[Debug] CR Cycle Text:{current_text_index}")

            # Get the parameters of the current text            
            current_text_item = text_params[current_text_index]          
            #print(f"[Debug] CR Cycle Text
            show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Cycler-Nodes#cr-cycle-text-simple"

            return (current_text_item, show_help, )

#---------------------------------------------------------------------------------------------------------------------#        
class CR_CycleImages:

    @classmethod
    def INPUT_TYPES(s):
    
        modes = ["Sequential"]
    
        return {"required": {"mode": (modes,),
                             "image_list": ("IMAGE_LIST",),
                             "frame_interval": ("INT", {"default": 30, "min": 0, "max": 999, "step": 1,}),         
                             "loops": ("INT", {"default": 1, "min": 1, "max": 1000}),
                             "current_frame": ("INT", {"default": 0.0, "min": 0.0, "max": 9999.0, "step": 1.0,}),
                },
        }
    
    RETURN_TYPES = ("IMAGE", "STRING", )
    RETURN_NAMES = ("IMAGE", "show_help", )
    FUNCTION = "cycle"
    CATEGORY = icons.get("Comfyroll/Animation/Legacy")

    def cycle(self, mode, image_list, frame_interval, loops, current_frame,):
    
        # Initialize the list
        image_params = list()

        # Extend image_params with image_list items
        if image_list:
            for _ in range(loops):
                image_params.extend(image_list)

        if mode == "Sequential":
            # Calculate the index of the current image string based on the current_frame and frame_interval
            current_image_index = (current_frame // frame_interval) % len(image_params)
            print(f"[Debug] CR Cycle Image:{current_image_index}")

            # Get the parameters of the current image            
            current_image_params = image_params[current_image_index]
            image_alias, current_image_item = current_image_params            
            
            show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Cycler-Nodes#cr-cycle-images"

            return (current_image_item, show_help, ) 
                
#---------------------------------------------------------------------------------------------------------------------#        
class CR_CycleImagesSimple:

    @classmethod
    def INPUT_TYPES(s):
    
        modes = ["Sequential"]
    
        return {"required": {"mode": (modes,),
                             "frame_interval": ("INT", {"default": 30, "min": 0, "max": 999, "step": 1,}),         
                             "loops": ("INT", {"default": 1, "min": 1, "max": 1000}),
                             "current_frame": ("INT", {"default": 0.0, "min": 0.0, "max": 9999.0, "step": 1.0,})
                },
                "optional": {"image_1": ("IMAGE",),
                             "image_2": ("IMAGE",),
                             "image_3": ("IMAGE",),
                             "image_4": ("IMAGE",),              
                             "image_5": ("IMAGE",),
                             "image_list_simple": ("IMAGE_LIST_SIMPLE",)
                }                                           
        }
    
    RETURN_TYPES = ("IMAGE", "STRING", )
    RETURN_NAMES = ("IMAGE", "show_help", )
    FUNCTION = "cycle_image"
    CATEGORY = icons.get("Comfyroll/Animation/Legacy")

    def cycle_image(self, mode, frame_interval, loops, current_frame,
        image_1=None, image_2=None, image_3=None, image_4=None, image_5=None,
        image_list_simple=None ):
        
        # Initialize the list
        image_params = list()
        
        image_list = list()
        if image_1 != None:        
            image_list.append(image_1),
        if image_2 != None: 
            image_list.append(image_2),
        if image_3 != None: 
            image_list.append(image_3),
        if image_4 != None: 
            image_list.append(image_4),
        if image_5 != None: 
            image_list.append(image_5),
        
        # Extend image_params with image items
        for _ in range(loops):
            if image_list_simple:
                image_params.extend(image_list_simple)
            image_params.extend(image_list)     

        if mode == "Sequential":
            # Calculate the index of the current image string based on the current_frame and frame_interval
            current_image_index = (current_frame // frame_interval) % len(image_params)
            print(f"[Debug] CR Cycle Text:{current_image_index}")

            # Get the parameters of the current image            
            current_image_item = image_params[current_image_index]          
            show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Cycler-Nodes#cr-cycle-images-simple"
            return (current_image_item, show_help, )
 
#---------------------------------------------------------------------------------------------------------------------#
# MAPPINGS
#---------------------------------------------------------------------------------------------------------------------#
# For reference only, actual mappings are in __init__.py
# 6 nodes
'''
NODE_CLASS_MAPPINGS = {  
    ### Cyclers
    "CR Cycle Models":CR_CycleModels,    
    "CR Cycle LoRAs":CR_CycleLoRAs,
    "CR Cycle Images":CR_CycleImages,
    "CR Cycle Images":CR_CycleImagesSimple,
    "CR Cycle Text":CR_CycleText,
    "CR Cycle Text Simple":CR_CycleTextSimple,   
}
'''