@@ -10,18 +10,74 @@ The `ComfyUI-to-Python-Extension` is a powerful tool that translates ComfyUI wor
10
10
** To this:**
11
11
12
12
```
13
+ import os
13
14
import random
14
- import torch
15
15
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
16
55
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()
18
73
from nodes import (
19
- VAEDecode ,
74
+ CLIPTextEncode ,
20
75
KSamplerAdvanced,
21
- EmptyLatentImage,
22
- SaveImage,
23
76
CheckpointLoaderSimple,
24
- CLIPTextEncode,
77
+ VAEDecode,
78
+ SaveImage,
79
+ EmptyLatentImage,
80
+ NODE_CLASS_MAPPINGS,
25
81
)
26
82
27
83
@@ -39,25 +95,26 @@ def main():
39
95
40
96
cliptextencode = CLIPTextEncode()
41
97
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) ,
44
100
)
45
101
46
102
cliptextencode_7 = cliptextencode.encode(
47
- text="text, watermark", clip=checkpointloadersimple_4[1]
103
+ text="text, watermark", clip=get_value_at_index( checkpointloadersimple_4, 1)
48
104
)
49
105
50
106
checkpointloadersimple_12 = checkpointloadersimple.load_checkpoint(
51
107
ckpt_name="sd_xl_refiner_1.0.safetensors"
52
108
)
53
109
54
110
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) ,
57
113
)
58
114
59
115
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),
61
118
)
62
119
63
120
ksampleradvanced = KSamplerAdvanced()
@@ -75,10 +132,10 @@ def main():
75
132
start_at_step=0,
76
133
end_at_step=20,
77
134
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) ,
82
139
)
83
140
84
141
ksampleradvanced_11 = ksampleradvanced.sample(
@@ -91,18 +148,19 @@ def main():
91
148
start_at_step=20,
92
149
end_at_step=10000,
93
150
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) ,
98
155
)
99
156
100
157
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),
102
160
)
103
161
104
162
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)
106
164
)
107
165
108
166
@@ -168,4 +226,4 @@ if __name__ == "__main__":
168
226
169
227
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` .
170
228
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