@@ -10,74 +10,18 @@ The `ComfyUI-to-Python-Extension` is a powerful tool that translates ComfyUI wor
1010** To this:**
1111
1212```
13- import os
1413import random
15- import sys
16- from typing import Sequence, Mapping, Any, Union
1714import torch
15+ import sys
1816
19-
20- def add_comfyui_directory_to_sys_path() -> None:
21- """
22- Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI'.
23- Once found, the directory is added to sys.path.
24- """
25- start_path = os.getcwd() # Get the current working directory
26-
27- def search_directory(path: str) -> None:
28- # Check if the current directory contains 'ComfyUI'
29- if "ComfyUI" in os.listdir(path):
30- directory_path = os.path.join(path, "ComfyUI")
31- sys.path.append(directory_path)
32- print(f"ComfyUI found and added to sys.path: {directory_path}")
33-
34- # Get the parent directory
35- parent_directory = os.path.dirname(path)
36-
37- # If the parent directory is the same as the current directory, we've reached the root and stop the search
38- if parent_directory == path:
39- return
40-
41- # Recursively call the function with the parent directory
42- search_directory(parent_directory)
43-
44- # Start the search from the current working directory
45- search_directory(start_path)
46-
47-
48- def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
49- """Returns the value at the given index of a sequence or mapping.
50-
51- If the object is a sequence (like list or string), returns the value at the given index.
52- If the object is a mapping (like a dictionary), returns the value at the index-th key.
53-
54- Some return a dictionary, in these cases, we look for the "results" key
55-
56- Args:
57- obj (Union[Sequence, Mapping]): The object to retrieve the value from.
58- index (int): The index of the value to retrieve.
59-
60- Returns:
61- Any: The value at the given index.
62-
63- Raises:
64- IndexError: If the index is out of bounds for the object and the object is not a mapping.
65- """
66- try:
67- return obj[index]
68- except KeyError:
69- return obj["result"][index]
70-
71-
72- add_comfyui_directory_to_sys_path()
17+ sys.path.append("../")
7318from nodes import (
74- CLIPTextEncode,
75- KSamplerAdvanced,
76- CheckpointLoaderSimple,
7719 VAEDecode,
78- SaveImage ,
20+ KSamplerAdvanced ,
7921 EmptyLatentImage,
80- NODE_CLASS_MAPPINGS,
22+ SaveImage,
23+ CheckpointLoaderSimple,
24+ CLIPTextEncode,
8125)
8226
8327
@@ -95,26 +39,25 @@ def main():
9539
9640 cliptextencode = CLIPTextEncode()
9741 cliptextencode_6 = cliptextencode.encode(
98- text="Kylo Ren trapped inside of a Mark Rothko painting ",
99- clip=get_value_at_index( checkpointloadersimple_4, 1) ,
42+ text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it ",
43+ clip=checkpointloadersimple_4[1] ,
10044 )
10145
10246 cliptextencode_7 = cliptextencode.encode(
103- text="text, watermark", clip=get_value_at_index( checkpointloadersimple_4, 1)
47+ text="text, watermark", clip=checkpointloadersimple_4[1]
10448 )
10549
10650 checkpointloadersimple_12 = checkpointloadersimple.load_checkpoint(
10751 ckpt_name="sd_xl_refiner_1.0.safetensors"
10852 )
10953
11054 cliptextencode_15 = cliptextencode.encode(
111- text="Kylo Ren trapped inside of a Mark Rothko painting ",
112- clip=get_value_at_index( checkpointloadersimple_12, 1) ,
55+ text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it ",
56+ clip=checkpointloadersimple_12[1] ,
11357 )
11458
11559 cliptextencode_16 = cliptextencode.encode(
116- text="text, watermark",
117- clip=get_value_at_index(checkpointloadersimple_12, 1),
60+ text="text, watermark", clip=checkpointloadersimple_12[1]
11861 )
11962
12063 ksampleradvanced = KSamplerAdvanced()
@@ -132,10 +75,10 @@ def main():
13275 start_at_step=0,
13376 end_at_step=20,
13477 return_with_leftover_noise="enable",
135- model=get_value_at_index( checkpointloadersimple_4, 0) ,
136- positive=get_value_at_index( cliptextencode_6, 0) ,
137- negative=get_value_at_index( cliptextencode_7, 0) ,
138- latent_image=get_value_at_index( emptylatentimage_5, 0) ,
78+ model=checkpointloadersimple_4[0] ,
79+ positive=cliptextencode_6[0] ,
80+ negative=cliptextencode_7[0] ,
81+ latent_image=emptylatentimage_5[0] ,
13982 )
14083
14184 ksampleradvanced_11 = ksampleradvanced.sample(
@@ -148,19 +91,18 @@ def main():
14891 start_at_step=20,
14992 end_at_step=10000,
15093 return_with_leftover_noise="disable",
151- model=get_value_at_index( checkpointloadersimple_12, 0) ,
152- positive=get_value_at_index( cliptextencode_15, 0) ,
153- negative=get_value_at_index( cliptextencode_16, 0) ,
154- latent_image=get_value_at_index( ksampleradvanced_10, 0) ,
94+ model=checkpointloadersimple_12[0] ,
95+ positive=cliptextencode_15[0] ,
96+ negative=cliptextencode_16[0] ,
97+ latent_image=ksampleradvanced_10[0] ,
15598 )
15699
157100 vaedecode_17 = vaedecode.decode(
158- samples=get_value_at_index(ksampleradvanced_11, 0),
159- vae=get_value_at_index(checkpointloadersimple_12, 2),
101+ samples=ksampleradvanced_11[0], vae=checkpointloadersimple_12[2]
160102 )
161103
162104 saveimage_19 = saveimage.save_images(
163- filename_prefix="ComfyUI", images=get_value_at_index( vaedecode_17, 0)
105+ filename_prefix="ComfyUI", images=vaedecode_17[0]
164106 )
165107
166108
0 commit comments