Skip to content

Commit c9a02f6

Browse files
authored
Update README.md
1 parent 203a675 commit c9a02f6

File tree

1 file changed

+81
-23
lines changed

1 file changed

+81
-23
lines changed

README.md

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,74 @@ The `ComfyUI-to-Python-Extension` is a powerful tool that translates ComfyUI wor
1010
**To this:**
1111

1212
```
13+
import os
1314
import random
14-
import torch
1515
import sys
16+
from typing import Sequence, Mapping, Any, Union
17+
import torch
18+
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
1655
17-
sys.path.append("../")
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()
1873
from nodes import (
19-
VAEDecode,
74+
CLIPTextEncode,
2075
KSamplerAdvanced,
21-
EmptyLatentImage,
22-
SaveImage,
2376
CheckpointLoaderSimple,
24-
CLIPTextEncode,
77+
VAEDecode,
78+
SaveImage,
79+
EmptyLatentImage,
80+
NODE_CLASS_MAPPINGS,
2581
)
2682
2783
@@ -39,25 +95,26 @@ def main():
3995
4096
cliptextencode = CLIPTextEncode()
4197
cliptextencode_6 = cliptextencode.encode(
42-
text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it",
43-
clip=checkpointloadersimple_4[1],
98+
text="Kylo Ren trapped inside of a Mark Rothko painting",
99+
clip=get_value_at_index(checkpointloadersimple_4, 1),
44100
)
45101
46102
cliptextencode_7 = cliptextencode.encode(
47-
text="text, watermark", clip=checkpointloadersimple_4[1]
103+
text="text, watermark", clip=get_value_at_index(checkpointloadersimple_4, 1)
48104
)
49105
50106
checkpointloadersimple_12 = checkpointloadersimple.load_checkpoint(
51107
ckpt_name="sd_xl_refiner_1.0.safetensors"
52108
)
53109
54110
cliptextencode_15 = cliptextencode.encode(
55-
text="evening sunset scenery blue sky nature, glass bottle with a galaxy in it",
56-
clip=checkpointloadersimple_12[1],
111+
text="Kylo Ren trapped inside of a Mark Rothko painting",
112+
clip=get_value_at_index(checkpointloadersimple_12, 1),
57113
)
58114
59115
cliptextencode_16 = cliptextencode.encode(
60-
text="text, watermark", clip=checkpointloadersimple_12[1]
116+
text="text, watermark",
117+
clip=get_value_at_index(checkpointloadersimple_12, 1),
61118
)
62119
63120
ksampleradvanced = KSamplerAdvanced()
@@ -75,10 +132,10 @@ def main():
75132
start_at_step=0,
76133
end_at_step=20,
77134
return_with_leftover_noise="enable",
78-
model=checkpointloadersimple_4[0],
79-
positive=cliptextencode_6[0],
80-
negative=cliptextencode_7[0],
81-
latent_image=emptylatentimage_5[0],
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),
82139
)
83140
84141
ksampleradvanced_11 = ksampleradvanced.sample(
@@ -91,18 +148,19 @@ def main():
91148
start_at_step=20,
92149
end_at_step=10000,
93150
return_with_leftover_noise="disable",
94-
model=checkpointloadersimple_12[0],
95-
positive=cliptextencode_15[0],
96-
negative=cliptextencode_16[0],
97-
latent_image=ksampleradvanced_10[0],
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),
98155
)
99156
100157
vaedecode_17 = vaedecode.decode(
101-
samples=ksampleradvanced_11[0], vae=checkpointloadersimple_12[2]
158+
samples=get_value_at_index(ksampleradvanced_11, 0),
159+
vae=get_value_at_index(checkpointloadersimple_12, 2),
102160
)
103161
104162
saveimage_19 = saveimage.save_images(
105-
filename_prefix="ComfyUI", images=vaedecode_17[0]
163+
filename_prefix="ComfyUI", images=get_value_at_index(vaedecode_17, 0)
106164
)
107165
108166
@@ -168,4 +226,4 @@ if __name__ == "__main__":
168226

169227
9. After running `comfyui_to_python.py`, a new .py file will be created in the current working directory that contains the same name as the `input` variable. If you made no changes, look for `workflow_api.py`.
170228

171-
10. Now you can execute the newly created .py file to generate images without launching a server.
229+
10. Now you can execute the newly created .py file to generate images without launching a server.

0 commit comments

Comments
 (0)