From 81e5830e09dd087d9e55a95cd2bbfb2bf1e6e3c4 Mon Sep 17 00:00:00 2001 From: Ash Martian Date: Sun, 18 Feb 2024 20:58:24 -0800 Subject: [PATCH] Simplify Loopy Dir, ensure queue prompts work as expected. --- README.md | 12 ++++++------ dir_loop.py | 30 ++++++++++-------------------- web/js/gir.js | 46 ++++++++++++---------------------------------- 3 files changed, 28 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 5ce46e3..378d286 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dir_loop.py b/dir_loop.py index 584c82a..c677b4e 100644 --- a/dir_loop.py +++ b/dir_loop.py @@ -58,30 +58,26 @@ 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 @@ -89,9 +85,3 @@ def iterate_directory(cls, directory, filter_type, filter_value, loop_index, pro @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}) diff --git a/web/js/gir.js b/web/js/gir.js index 7ec6a24..edb59d2 100644 --- a/web/js/gir.js +++ b/web/js/gir.js @@ -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", @@ -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;