diff --git a/SimpleSampler.py b/SimpleSampler.py index 65e46c7..f9647d3 100644 --- a/SimpleSampler.py +++ b/SimpleSampler.py @@ -17,7 +17,9 @@ import hashlib import copy import traceback + from PIL import Image +from nodes import common_ksampler from PIL.PngImagePlugin import PngInfo import numpy as np @@ -25,10 +27,12 @@ import numpy as np #print(f"SimpleSampler __file__ {os.path.splitext(os.path.basename(__file__))[0]}") import os -if __name__ == os.path.splitext(os.path.basename(__file__))[0] : +if __name__ == os.path.splitext(os.path.basename(__file__))[0] or __name__ =='__main__': from ConsoleColor import print, console + from wildcards import wildcards else: from .ConsoleColor import print, console + from .wildcards import wildcards #print(__file__) #print(os.path.basename(__file__)) @@ -46,6 +50,46 @@ except: err_msg = traceback.format_exc() print(err_msg) + +def encode(clip, text): + if wildcardsOn: + text=wildcards.run(text) + return [[clip.encode(text), {}]] + +def generate(width, height, batch_size=1): + latent = torch.zeros([batch_size, 4, height // 8, width // 8]) + return {"samples":latent} + # RETURN_TYPES = ("LATENT",) + +def decode(vae, samples): + return vae.decode(samples["samples"]) + # RETURN_TYPES = ("IMAGE",) + +def sample( + model, seed, steps, cfg, sampler_name, scheduler, + clip, + vae, + positive, negative, + #latent_image, + width, height, denoise=1.0, batch_size=1 + ): + + samples=common_ksampler( + model, seed, steps, cfg, sampler_name, scheduler, + #positive, + encode(clip, positive), + #negative, + encode(clip, negative), + #latent_image, + generate( width, height, batch_size=1), + denoise=denoise)[0] + + return (decode(vae,samples),) + +def load_vae(vae_name): + vae_path = folder_paths.get_full_path("vae", vae_name) + vae = comfy.sd.VAE(ckpt_path=vae_path) + return vae #---------------------------- class SimpleSampler: @classmethod @@ -54,39 +98,88 @@ class SimpleSampler: { "model": ("MODEL",), #"positive": ("CONDITIONING", ), - "positive": ("STRING", {"multiline": True}), "clip": ("CLIP", ), + "clip": ("CLIP", ), + "vae": ("VAE", ), + "positive": ("STRING", {"multiline": True}), #"negative": ("CONDITIONING", ), - "negative": ("STRING", {"multiline": True}), "clip": ("CLIP", ), + "negative": ("STRING", {"multiline": True}), "width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}), "height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}), "batch_size": ("INT", {"default": 1, "min": 1, "max": 64}), "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), - "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "cfg": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 100.0}), "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), - "latent_image": ("LATENT", ), + #"latent_image": ("LATENT", ), "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), }} - - RETURN_TYPES = ("LATENT",) - FUNCTION = "sample" + RETURN_TYPES = ("IMAGE",) + #RETURN_TYPES = ("LATENT",) + FUNCTION = "simple" CATEGORY = "sampling" - def encode(self, clip, text): - if wildcardsOn: - text=wildcards.run(text) - return ([[clip.encode(text), {}]], ) - - def generate(self, width, height, batch_size=1): - latent = torch.zeros([batch_size, 4, height // 8, width // 8]) - return ({"samples":latent}, ) - - def sample(self, - model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, + def simple(self, + model, seed, steps, cfg, sampler_name, scheduler, + clip, + vae, + positive, negative, width, height, denoise=1.0, batch_size=1 ): - return common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, self.encode(clip, positive), elf.encode(clip, negative), self.generate( width, height, batch_size=1), denoise=denoise) - + return sample( + model, seed, steps, cfg, sampler_name, scheduler, + clip, + vae, + positive, negative, + width, height, denoise, batch_size + ) + +#---------------------------- +class SimpleSamplerVAE: + @classmethod + def INPUT_TYPES(s): + return {"required": + { + "model": ("MODEL",), + #"positive": ("CONDITIONING", ), + "clip": ("CLIP", ), + #"vae": ("VAE", ), + "vae_name": (folder_paths.get_filename_list("vae"), ), + "positive": ("STRING", {"multiline": True}), + #"negative": ("CONDITIONING", ), + "negative": ("STRING", {"multiline": True}), + "width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}), + "height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}), + "batch_size": ("INT", {"default": 1, "min": 1, "max": 64}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), + #"latent_image": ("LATENT", ), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + }} + RETURN_TYPES = ("IMAGE",) + #RETURN_TYPES = ("LATENT",) + FUNCTION = "simple" + + CATEGORY = "sampling" + + def simple(self, + model, seed, steps, cfg, sampler_name, scheduler, + clip, + vae_name, + positive, negative, + width, height, denoise=1.0, batch_size=1 + ): + + return sample( + model, seed, steps, cfg, sampler_name, scheduler, + clip, + load_vae(vae_name), + positive, negative, + width, height, denoise, batch_size + ) + diff --git a/__init__.py b/__init__.py index 195f730..f292f5e 100644 --- a/__init__.py +++ b/__init__.py @@ -68,7 +68,7 @@ if not md.startswith("custom_nodes."): add("LoraLoaderTextRandom") add("Random_Sampler") add("VAELoaderDecode") - add("SimpleSampler") + add("SimpleSampler",["SimpleSampler","SimpleSamplerVAE"]) add("SaveImageSimple") #add("test")