Skip to content

Commit

Permalink
Simplify Loopy Dir, ensure queue prompts work as expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
AshMartian committed Feb 19, 2024
1 parent d46e29a commit 81e5830
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 60 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ Then restart ComfyUI

## Nodes

| Node | Description |
| :-------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| GIR Directory Picker![GIR Directory Picker](docs/DirectoryPicker.png) | **"Select Directory"** opens a GUI file browser using `tkinker`, the selected directory is persistent on disk (server restarts are no problem!), and available via output string. Changing the Selected Directory manually is supported too. |
| GIR Loopy Dir![GIR Loopy Dir](docs/DirectoryLoop.png) | Directory in, automatic loopy-ness out. Auto-increment and auto resetting, when the `loop_index` reaches the end of filtered files in directory, goes back to zero. Filter by extension or regex, manually change `loop_index` to override! Note: When queuing, the `loop_index` will increment in real time, not pre-generated like seed inputs. |
| GIR Image Nabber ![GIR Image Nabber](docs/ImageNabber.png) | A simple path to image node, for working with results of Loopy Dir without relying on an external node pack that has it's own directory filtering logic _(For videos, use [VHS Load Video (Path)](https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite/blob/main/videohelpersuite/load_video_nodes.py#L197)_) |
| GIR Happy Dance!\* ![Let's Dance!](docs/GIR_Dance.gif) | These nodes simplify and streamline the process of directory selection and iteration, making complex tasks feel like a breeze. With the ability to effortlessly pick directories and loop through files with smart reset capabilities, GIR is over the moon, knowing users can focus on creativity and productivity, leaving the tedious parts to the automation magic of ComfyUI DirGir. |
| Node | Description |
| :-------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| GIR Directory Picker![GIR Directory Picker](docs/DirectoryPicker.png) | **"Select Directory"** opens a GUI file browser using `tkinker`, the selected directory is persistent on disk (server restarts are no problem!), and available via output string. Changing the Selected Directory manually is supported too. |
| GIR Loopy Dir![GIR Loopy Dir](docs/DirectoryLoop.png) | Directory in, automatic loopy-ness out. Auto-increment and auto resetting, when the `loop_index` reaches the end of filtered files in directory, goes back to zero. Filter by extension or regex, manually change `loop_index` to override! Note: When queuing, the `loop_index` will increment in real time, queuing over the number of files available is no problem, the `loop_index` will tell you what index is next as the queue progresses. |
| GIR Image Nabber ![GIR Image Nabber](docs/ImageNabber.png) | A simple path to image node, for working with results of Loopy Dir without relying on an external node pack that has it's own directory filtering logic _(For videos, use [VHS Load Video (Path)](https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite/blob/main/videohelpersuite/load_video_nodes.py#L197)_) |
| GIR Happy Dance!\* ![Let's Dance!](docs/GIR_Dance.gif) | These nodes simplify and streamline the process of directory selection and iteration, making complex tasks feel like a breeze. With the ability to effortlessly pick directories and loop through files with smart reset capabilities, GIR is over the moon, knowing users can focus on creativity and productivity, leaving the tedious parts to the automation magic of ComfyUI DirGir. |

## Examples

Expand Down
30 changes: 10 additions & 20 deletions dir_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,30 @@ def iterate_directory(cls, directory, filter_type, filter_value, loop_index, pro

if len(cls.matched_files) == 0:
# No files found, reset index
cls.file_index = 0
loop_indexes[id] = 0
print("No files found in directory" + directory)
return (0, 0, "", "", [])

# If the loop index is not the same as the cls index + 1, set the cls index to the loop index
if loop_index != cls.file_index + 1 and loop_index < len(cls.matched_files):
cls.file_index = loop_index
loop_indexes[id] = cls.file_index
# Ensure loop_index is within bounds
loop_index = loop_indexes.get(id, 0)

if cls.file_index >= len(cls.matched_files):
cls.file_index = 0
if loop_index >= len(cls.matched_files):
# If the external loop index is beyond the available files, reset to 0
loop_indexes[id] = 0

# Serve the next file in the list
current_file = cls.matched_files[cls.file_index]
# Serve the file at the current loop index
current_file = cls.matched_files[loop_index]

# Prepare outputs
output = (len(cls.matched_files), cls.file_index,
current_file, os.path.join(directory, current_file), cls.matched_files)
output = (len(cls.matched_files), loop_index, current_file,
os.path.join(directory, current_file), cls.matched_files)

# Increment index or reset if at the end
cls.file_index = (cls.file_index + 1) % len(cls.matched_files)
loop_indexes[id] = cls.file_index
# Increment the external loop index or reset if at the end
loop_indexes[id] = (loop_index + 1) % len(cls.matched_files)

return output


@server.PromptServer.instance.routes.get("/gir-dir/loop-index")
async def get_last_index(request):
return web.json_response({'loop_index': loop_indexes.get(request.rel_url.query.get('id', '')) or 0})


@server.PromptServer.instance.routes.get("/gir-dir/loop-queue")
async def get_loop_queue(request):
loop_indexes[request.rel_url.query.get('id', '')] += 1
return web.json_response({'loop_queue': loop_indexes.get(request.rel_url.query.get('id', '')) or 0})
46 changes: 12 additions & 34 deletions web/js/gir.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,6 @@ function getLoopIndex(node_id) {
});
}

function queueLoopIndex(node_id) {
return new Promise((resolve, reject) => {
api.fetchApi('/gir-dir/queue-loop-index?id=' + node_id)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
resolve(data.loop_index);
})
.catch(error => {
console.error('Error triggering queue loop:', error);
reject(error);
});
});
}

app.registerExtension({
name: "DirGir.Picker",
category: "Dir GIR",
Expand Down Expand Up @@ -175,23 +156,20 @@ app.registerExtension({
const node = this;

getLoopIndex(node.id).then(loop_index => {
node.widgets.find(w => w.name === 'loop_index').value = loop_index;
});

let lastQueueRemaining = 0;
api.addEventListener('status', async (status) => {
if (status.detail.exec_info.queue_remaining === 0) {
getLoopIndex(node.id).then(loop_index => {
node.widgets.find(w => w.name === 'loop_index').value = loop_index;
});
} else if (status.detail.exec_info.queue_remaining !== lastQueueRemaining) {
// Trigger a queue loop index
queueLoopIndex(node.id).then(loop_index => {
node.widgets.find(w => w.name === 'loop_index').value = loop_index;
});
const loopIndexWidget = node.widgets.find(w => w.name === 'loop_index');
if (loopIndexWidget) {
loopIndexWidget.value = loop_index;
loopIndexWidget.afterQueued = () => {
loopIndexWidget.value += 1;
}
}
lastQueueRemaining = status.detail.exec_info.queue_remaining;
});

api.addEventListener('executing', () => {
node.widgets.find(w => w.name === 'loop_index').value += 1;
node.onResize?.(node.size);
node.widgets.find(w => w.name === 'loop_index').value -= 1;
})
api.addEventListener('executed', async (status) => {
getLoopIndex(node.id).then(loop_index => {
node.widgets.find(w => w.name === 'loop_index').value = loop_index;
Expand Down

0 comments on commit 81e5830

Please sign in to comment.