107 lines
4.5 KiB
Markdown
107 lines
4.5 KiB
Markdown
# ComfyUI-FilePathsFromFolder
|
|
|
|
A simple ComfyUI custom node pack for loading and selecting files from folders, with for-loop support for batch processing.
|
|
|
|
## Nodes
|
|
|
|
### LoadFilePathsFromFolder
|
|
Scans a folder for files matching a specific extension pattern.
|
|
|
|
**Parameters:**
|
|
- `folder_path`: Path to the folder to scan
|
|
- `extension`: File extension pattern (e.g., "*.png", "*.jpg", "*.txt")
|
|
|
|
**Outputs:**
|
|
- `filepaths`: Line-break separated list of full file paths
|
|
- `filenames`: Line-break separated list of filenames without extensions
|
|
- `count`: Number of files matched
|
|
|
|
### GetPathByIndex
|
|
Selects a specific file from the lists by index.
|
|
|
|
**Inputs:**
|
|
- `filenames`: Line-break separated filenames (from LoadFilePathsFromFolder)
|
|
- `filepaths`: Line-break separated filepaths (from LoadFilePathsFromFolder)
|
|
|
|
**Parameters:**
|
|
- `index`: Index of the file to select (0-based)
|
|
|
|
**Outputs:**
|
|
- `filename`: Selected filename
|
|
- `filepath`: Selected filepath
|
|
|
|
## Loop Nodes
|
|
|
|
### FilePathsForLoop
|
|
Initiates iteration through files in a folder. Uses ComfyUI's list-based batching to process each file individually.
|
|
|
|
**Parameters:**
|
|
- `folder_path`: Path to the folder to scan
|
|
- `extension`: File extension pattern (e.g., "*.png", "*.jpg", "*.txt")
|
|
|
|
**Outputs:**
|
|
- `filepath`: Full path of current file (list output - each item processed separately)
|
|
- `filename`: Filename without extension (list output)
|
|
- `current_index`: Current iteration index, 0-based (list output)
|
|
- `total_count`: Total number of files (list output)
|
|
|
|
**Important:** This node outputs lists with `OUTPUT_IS_LIST = True`. ComfyUI will automatically run all connected nodes once for each item in the list. Each connected node receives a **single value** from the list per execution, not the entire list.
|
|
|
|
### FilePathsLoopClose
|
|
Closes the file paths loop and collects iteration results.
|
|
|
|
**Inputs:**
|
|
- `current_index`: Current index from FilePathsForLoop (required, force input)
|
|
- `iteration_data`: Optional output from FilePathsEnsureExecution or any node in the loop
|
|
|
|
**Outputs:**
|
|
- `total_iterations`: Total number of iterations completed
|
|
|
|
**Usage:** This is the termination point of your loop. Connect the `current_index` from FilePathsForLoop (or through other nodes) to ensure the loop completes. If you need to ensure multiple nodes execute, connect them to FilePathsEnsureExecution first, then connect FilePathsEnsureExecution's output to this node's `iteration_data` input.
|
|
|
|
### FilePathsEnsureExecution
|
|
Ensures connected nodes execute during loop iteration by collecting their outputs.
|
|
|
|
**Inputs:**
|
|
- `input_1` through `input_5`: Optional inputs (supports any type with wildcard `*`)
|
|
|
|
**Outputs:**
|
|
- `passthrough`: The first non-None input value
|
|
|
|
**Usage:** Connect outputs from any nodes that need to execute within the loop to this node's inputs. Then connect this node's output downstream (e.g., to FilePathsLoopClose) to ensure all connected nodes complete execution. If you need more than 5 inputs, chain multiple FilePathsEnsureExecution nodes together - connect the first node's output to one of the second node's inputs.
|
|
|
|
## How ComfyUI List-Based Loops Work
|
|
|
|
When a node has `OUTPUT_IS_LIST = True`:
|
|
1. The node returns lists: `["file1.png", "file2.png", "file3.png"]`
|
|
2. ComfyUI automatically executes **all downstream nodes once per list item**
|
|
3. Each execution receives **one item from the list**, not the whole list
|
|
4. **Iteration 1**: downstream nodes receive `"file1.png"`
|
|
5. **Iteration 2**: downstream nodes receive `"file2.png"`
|
|
6. **Iteration 3**: downstream nodes receive `"file3.png"`
|
|
|
|
This is ComfyUI's native batching/looping mechanism.
|
|
|
|
## Example Workflows
|
|
|
|
### Simple Loop Processing
|
|
1. **FilePathsForLoop** - Set folder path and extension (e.g., "C:/images", "*.png")
|
|
2. Connect `filepath` to your image loader node (e.g., Load Image)
|
|
3. Connect your processing nodes (e.g., filters, effects)
|
|
4. Connect the final processing output to **FilePathsEnsureExecution** inputs
|
|
5. Connect **FilePathsForLoop**'s `current_index` to **FilePathsLoopClose**'s `current_index`
|
|
6. (Optional) Connect **FilePathsEnsureExecution**'s output to **FilePathsLoopClose**'s `passthrough_1`
|
|
|
|
This setup will automatically process each file in the folder sequentially.
|
|
|
|
### Why Three Nodes?
|
|
|
|
- **FilePathsForLoop**: Initiates the loop, provides file paths and metadata
|
|
- **FilePathsEnsureExecution**: Optional helper to chain dependencies and ensure execution order
|
|
- **FilePathsLoopClose**: Terminates the loop, provides completion signal, collects results
|
|
|
|
## Installation
|
|
|
|
Place this folder in your ComfyUI `custom_nodes` directory and restart ComfyUI.
|
|
|