Repository navigation
Expand file tree
/
Copy pathnodes.py
More file actions
269 lines (223 loc) · 8.65 KB
/
Copy pathnodes.py
File metadata and controls
269 lines (223 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import os
import glob
class FilePathsForLoop:
"""
Iterates through files in a folder one at a time.
Returns each file path individually for processing in a loop.
Connect to any node that processes files, then to FilePathsLoopClose.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"folder_path": ("STRING", {"default": ""}),
"extension": ("STRING", {"default": "*.png"}),
}
}
RETURN_TYPES = ("STRING", "STRING", "INT", "INT")
RETURN_NAMES = ("current_filepath", "current_filename", "current_index", "total_file_count")
FUNCTION = "iterate_files"
CATEGORY = "FilePathsFromFolder/Loop"
OUTPUT_NODE = False
OUTPUT_IS_LIST = (True, True, True, True)
def iterate_files(self, folder_path, extension):
# Build search pattern
search_pattern = os.path.join(folder_path, extension)
# Find all matching files
matched_files = glob.glob(search_pattern)
matched_files.sort()
total_count = len(matched_files)
# If no files, return empty lists
if not matched_files:
return ([], [], [], [])
# Build output lists
filepaths = []
filenames = []
indices = []
counts = []
for idx, filepath in enumerate(matched_files):
basename = os.path.basename(filepath)
filename_without_ext = os.path.splitext(basename)[0]
filepaths.append(filepath)
filenames.append(filename_without_ext)
indices.append(idx)
counts.append(total_count)
# Return as lists - OUTPUT_IS_LIST tells ComfyUI this is a batch
return (filepaths, filenames, indices, counts)
class FilePathsLoopClose:
"""
Closes the file paths loop.
Connect FilePathsEnsureExecution output here to ensure all nodes in the loop execute.
This ensures all iterations complete before proceeding.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"current_index": ("INT", {"default": 0, "forceInput": True}),
},
"optional": {
"passthrough": ("*",),
}
}
INPUT_IS_LIST = True
RETURN_TYPES = ("INT",)
RETURN_NAMES = ("processed_count",)
FUNCTION = "close_loop"
CATEGORY = "FilePathsFromFolder/Loop"
OUTPUT_NODE = True
def close_loop(self, current_index, passthrough=None):
# Count total iterations
total = len(current_index) if isinstance(current_index, list) else 1
return (total,)
class FilePathsEnsureExecution:
"""
Ensures connected nodes execute by passing through their outputs.
Use this to chain multiple nodes that need to execute during each loop iteration.
If you need more than 5 inputs, you can chain multiple of these nodes together.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"optional": {
"passthrough_1": ("*",),
"passthrough_2": ("*",),
"passthrough_3": ("*",),
"passthrough_4": ("*",),
"passthrough_5": ("*",),
}
}
RETURN_TYPES = ("*",)
RETURN_NAMES = ("passthrough",)
FUNCTION = "passthrough"
CATEGORY = "FilePathsFromFolder/Loop"
OUTPUT_NODE = False
def passthrough(self, **kwargs):
# Return the first non-None input as passthrough
for key in sorted(kwargs.keys()):
if kwargs[key] is not None:
return (kwargs[key],)
return (None,)
class LoadFilePathsFromFolder:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"folder_path": ("STRING", {"default": ""}),
"extension": ("STRING", {"default": "*.png"}),
}
}
RETURN_TYPES = ("STRING", "STRING", "INT")
RETURN_NAMES = ("filepaths", "filenames", "count")
FUNCTION = "load_files"
CATEGORY = "FilePathsFromFolder"
def load_files(self, folder_path, extension):
# Build search pattern
search_pattern = os.path.join(folder_path, extension)
# Find all matching files
matched_files = glob.glob(search_pattern)
matched_files.sort()
# Extract file paths and names
filepaths = []
filenames = []
for filepath in matched_files:
filepaths.append(filepath)
# Get filename without extension
basename = os.path.basename(filepath)
filename_without_ext = os.path.splitext(basename)[0]
filenames.append(filename_without_ext)
# Join with line breaks
filepaths_str = "\n".join(filepaths)
filenames_str = "\n".join(filenames)
count = len(matched_files)
return (filepaths_str, filenames_str, count)
class GetPathByIndex:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"filepaths": ("STRING", {"default": "", "multiline": True}),
"filenames": ("STRING", {"default": "", "multiline": True}),
"index": ("INT", {"default": 0, "min": 0}),
}
}
RETURN_TYPES = ("STRING", "STRING")
RETURN_NAMES = ("filepath", "filename")
FUNCTION = "get_by_index"
CATEGORY = "FilePathsFromFolder"
def get_by_index(self, filepaths, filenames, index):
# Split by line breaks
filepath_list = filepaths.split("\n") if filepaths else []
filename_list = filenames.split("\n") if filenames else []
# Get item at index (default to empty if out of range)
filepath = filepath_list[index] if index < len(filepath_list) else ""
filename = filename_list[index] if index < len(filename_list) else ""
return (filepath, filename)
class FilePathsLoopSyncTrigger:
"""
Batch synchronization trigger: splits batch into trigger + batch passthrough.
- 'trigger' output: single value, connects to nodes that run ONCE (cleanup/checkpoint/etc.)
- 'anything' output: original batch (N items), connects to FilePathsLoopSyncResume
Use with FilePathsLoopSyncResume to ensure one-time operation completes before batch continues.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"anything": ("*", {"forceInput": True}),
}
}
INPUT_IS_LIST = True
RETURN_TYPES = ("*", "*")
RETURN_NAMES = ("anything", "trigger")
OUTPUT_IS_LIST = (True, False)
FUNCTION = "trigger_once"
CATEGORY = "FilePathsFromFolder/Loop"
OUTPUT_NODE = False
def trigger_once(self, anything):
# Split: single trigger for one-time ops + original batch to continue
trigger_value = anything[0] if isinstance(anything, list) and len(anything) > 0 else anything
return (anything, trigger_value)
class FilePathsLoopSyncResume:
"""
Batch synchronization resume: waits for single operation, then continues batch.
Just a wait point - doesn't modify data.
Connect batch from Trigger's 'anything' output here.
Connect completion signal from your one-time operation to 'wait_for'.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"anything": ("*", {"forceInput": True}),
"wait_for": ("*",),
}
}
INPUT_IS_LIST = True
RETURN_TYPES = ("*",)
RETURN_NAMES = ("anything",)
OUTPUT_IS_LIST = (True,)
FUNCTION = "resume_batch"
CATEGORY = "FilePathsFromFolder/Loop"
OUTPUT_NODE = False
def resume_batch(self, anything, wait_for=None):
# Simple passthrough after waiting for completion
return (anything,)
NODE_CLASS_MAPPINGS = {
"LoadFilePathsFromFolder": LoadFilePathsFromFolder,
"GetPathByIndex": GetPathByIndex,
"FilePathsForLoop": FilePathsForLoop,
"FilePathsLoopClose": FilePathsLoopClose,
"FilePathsEnsureExecution": FilePathsEnsureExecution,
"FilePathsLoopSyncTrigger": FilePathsLoopSyncTrigger,
"FilePathsLoopSyncResume": FilePathsLoopSyncResume,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadFilePathsFromFolder": "Load File Paths From Folder",
"GetPathByIndex": "Get File Path By Index",
"FilePathsForLoop": "File Paths For Loop Start",
"FilePathsLoopClose": "File Paths For Loop End",
"FilePathsEnsureExecution": "File Paths For Loop Ensure Execution",
"FilePathsLoopSyncTrigger": "File Paths For Loop Sync Trigger",
"FilePathsLoopSyncResume": "File Paths For Loop Sync Resume",
}