Added Prompt Load and Save nodes
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import folder_paths
|
||||
|
||||
class MangoPromptLoad:
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
output_dir = folder_paths.get_output_directory()
|
||||
choices = []
|
||||
if output_dir and os.path.exists(output_dir):
|
||||
# Recursively search for .txt files in output_dir and subdirectories
|
||||
for root, dirs, files in os.walk(output_dir):
|
||||
for file in files:
|
||||
if file.lower().endswith(".txt"):
|
||||
rel_path = os.path.relpath(os.path.join(root, file), output_dir)
|
||||
choices.append(rel_path)
|
||||
choices = sorted(choices)
|
||||
# Provide a fallback if no files found
|
||||
if not choices:
|
||||
choices = [""]
|
||||
return {
|
||||
"required": {
|
||||
"filename": (choices, {"tooltip": "Select a .txt file from the output folder and its subfolders."})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING",)
|
||||
FUNCTION = "load_prompt"
|
||||
CATEGORY = "Mango Node Pack/Metadata"
|
||||
DESCRIPTION = (
|
||||
"Loads prompt text from a .txt file found in the default ComfyUI output folder "
|
||||
"and all its subfolders. Perfect to serve up a prompt for PromptMango!"
|
||||
)
|
||||
|
||||
def load_prompt(self, filename):
|
||||
output_dir = folder_paths.get_output_directory()
|
||||
if not output_dir:
|
||||
print("Error: Output directory not found! Check your ComfyUI setup.")
|
||||
return ("",)
|
||||
|
||||
full_path = os.path.join(output_dir, filename)
|
||||
if not os.path.exists(full_path):
|
||||
print(f"Error: File {full_path} not found.")
|
||||
return ("",)
|
||||
|
||||
try:
|
||||
with open(full_path, "r", encoding="utf-8") as f:
|
||||
text = f.read()
|
||||
return (text,)
|
||||
except Exception as e:
|
||||
print("Error loading file:", e)
|
||||
return ("",)
|
||||
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime
|
||||
import folder_paths
|
||||
|
||||
class PromptSave:
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"prompt": ("STRING", {"multiline": True, "tooltip": "The prompt text to save."}),
|
||||
"filename_prefix": ("STRING", {"default": "Prompt", "tooltip": "Base name for the saved prompt file."}),
|
||||
"subdirectory_name": ("STRING", {"default": "Prompts", "tooltip": "Subfolder (inside the output folder) to save the prompt."}),
|
||||
}
|
||||
}
|
||||
OUTPUT_NODE = True
|
||||
RETURN_TYPES = ()
|
||||
FUNCTION = "save_prompt"
|
||||
CATEGORY = "Mango Node Pack/Metadata"
|
||||
DESCRIPTION = "Saves prompt text to a .txt file with a simple incrementing suffix."
|
||||
|
||||
def save_prompt(self, prompt, filename_prefix="Prompt", subdirectory_name="Prompts"):
|
||||
try:
|
||||
# Get the output directory from folder_paths.
|
||||
output_dir = folder_paths.get_output_directory()
|
||||
if not output_dir:
|
||||
print("Error: Output directory not found!")
|
||||
return ("",)
|
||||
|
||||
# Determine the target folder.
|
||||
full_output_folder = os.path.join(output_dir, subdirectory_name) if subdirectory_name else output_dir
|
||||
os.makedirs(full_output_folder, exist_ok=True)
|
||||
|
||||
# Find the next available file number.
|
||||
pattern = re.compile(rf"^{re.escape(filename_prefix)}_(\d+)\.txt$")
|
||||
max_num = 0
|
||||
for f in os.listdir(full_output_folder):
|
||||
m = pattern.match(f)
|
||||
if m:
|
||||
try:
|
||||
num = int(m.group(1))
|
||||
if num > max_num:
|
||||
max_num = num
|
||||
except Exception as e:
|
||||
print("Error parsing file number in file", f, ":", e)
|
||||
next_num = max_num + 1
|
||||
|
||||
file_name = f"{filename_prefix}_{next_num}.txt"
|
||||
full_path = os.path.join(full_output_folder, file_name)
|
||||
print("Saving prompt to:", full_path)
|
||||
|
||||
# Save the prompt using UTF-8 encoding.
|
||||
with open(full_path, "w", encoding="utf-8") as f:
|
||||
f.write(prompt)
|
||||
|
||||
return (prompt,)
|
||||
except Exception as e:
|
||||
print("Error saving prompt:", e)
|
||||
return (prompt,)
|
||||
Reference in New Issue
Block a user