import os import random import sys from typing import Sequence, Mapping, Any, Union import torch def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any: """Returns the value at the given index of a sequence or mapping. If the object is a sequence (like list or string), returns the value at the given index. If the object is a mapping (like a dictionary), returns the value at the index-th key. Some return a dictionary, in these cases, we look for the "results" key Args: obj (Union[Sequence, Mapping]): The object to retrieve the value from. index (int): The index of the value to retrieve. Returns: Any: The value at the given index. Raises: IndexError: If the index is out of bounds for the object and the object is not a mapping. """ try: return obj[index] except KeyError: return obj["result"][index] def find_path(name: str, path: str = None) -> str: """ Recursively looks at parent folders starting from the given path until it finds the given name. Returns the path as a Path object if found, or None otherwise. """ # If no path is given, use the current working directory if path is None: path = os.getcwd() # Check if the current directory contains the name if name in os.listdir(path): path_name = os.path.join(path, name) print(f"{name} found: {path_name}") return path_name # Get the parent directory parent_directory = os.path.dirname(path) # If the parent directory is the same as the current directory, we've reached the root and stop the search if parent_directory == path: return None # Recursively call the function with the parent directory return find_path(name, parent_directory) def add_comfyui_directory_to_sys_path() -> None: """ Add 'ComfyUI' to the sys.path """ comfyui_path = find_path("ComfyUI") if comfyui_path is not None and os.path.isdir(comfyui_path): sys.path.append(comfyui_path) print(f"'{comfyui_path}' added to sys.path") def add_extra_model_paths() -> None: """ Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path. """ from main import load_extra_path_config extra_model_paths = find_path("extra_model_paths.yaml") if extra_model_paths is not None: load_extra_path_config(extra_model_paths) else: print("Could not find the extra_model_paths config file.") add_comfyui_directory_to_sys_path() add_extra_model_paths() def import_custom_nodes() -> None: """Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS This function sets up a new asyncio event loop, initializes the PromptServer, creates a PromptQueue, and initializes the custom nodes. """ import asyncio import execution from nodes import init_custom_nodes import server # Creating a new event loop and setting it as the default loop loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # Creating an instance of PromptServer with the loop server_instance = server.PromptServer(loop) execution.PromptQueue(server_instance) # Initializing custom nodes init_custom_nodes() from nodes import NODE_CLASS_MAPPINGS, SaveImage def main(): import_custom_nodes() with torch.inference_mode(): lora_stacker = NODE_CLASS_MAPPINGS["LoRA Stacker"]() lora_stacker_2 = lora_stacker.lora_stacker(input_mode="simple", lora_count=2) eff_loader_sdxl = NODE_CLASS_MAPPINGS["Eff. Loader SDXL"]() ksampler_sdxl_eff = NODE_CLASS_MAPPINGS["KSampler SDXL (Eff.)"]() saveimage = SaveImage() for q in range(10): eff_loader_sdxl_1 = eff_loader_sdxl.efficientloaderSDXL( base_ckpt_name="copaxTimelessxlSDXL1_v5.safetensors", base_clip_skip=-1, refiner_ckpt_name="sdXL_v10RefinerVAEFix.safetensors", refiner_clip_skip=-1, positive_ascore=6, negative_ascore=2, vae_name="Baked VAE", positive="photorealistic scene of planet of the apes , (an ape wearing armor:1.5) carrying in tether a frightened beautiful human, (young Nordic woman:2) in (tattered linen dress:1.5), she has perfect face, cleavage, torn clothes, delicate face, crying:1.8, chained, hands tied up, symmetrical face, 20 y.o., freckles, realistic, wide angle, wide shot, 4k, film grain, depth, masterpiece", negative="embedding:negativeXL (worst quality:1.5), (low quality:1.5), (normal quality:1.5), lowres, bad anatomy, bad hands, multiple eyebrow, (cropped), extra limb, missing limbs, deformed hands, long neck, long body, (bad hands), signature, username, artist name, conjoined fingers, deformed fingers, ugly eyes, imperfect eyes, skewed eyes, unnatural face, unnatural body, error, painting by bad-artist, asian, long hands, helmet:1.5, cgi, 3d, illustration, blue skin, unnatural skin, close up shot, out of frame ", empty_latent_width=1024, empty_latent_height=1024, batch_size=1, lora_stack=get_value_at_index(lora_stacker_2, 0), ) ksampler_sdxl_eff_3 = ksampler_sdxl_eff.sample_sdxl( sampler_state="Sample", noise_seed=random.randint(1, 2**64), steps=25, cfg=7, sampler_name="dpmpp_2s_ancestral", scheduler="karras", start_at_step=0, refine_at_step=20, preview_method="none", vae_decode="true", sdxl_tuple=get_value_at_index(eff_loader_sdxl_1, 0), latent_image=get_value_at_index(eff_loader_sdxl_1, 1), optional_vae=get_value_at_index(eff_loader_sdxl_1, 2), ) saveimage_5 = saveimage.save_images( filename_prefix="ComfyUI", images=get_value_at_index(ksampler_sdxl_eff_3, 3), ) images=get_value_at_index(ksampler_sdxl_eff_3, 3) if __name__ == "__main__": main()