osrplus

QuenWildcards

Oct 19th, 2025
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. # QuenWildcards — A1111-style __token__ resolver for ComfyUI
  2. # Usage: Put text files in a folder (one option per line), then write __token__ in your prompt.
  3. # The node replaces each __token__ with a random line from <folder>/<token>.txt
  4.  
  5. import os, re, random, hashlib
  6.  
  7. class QuenWildcardPrompt:
  8. @classmethod
  9. def INPUT_TYPES(cls):
  10. return {
  11. "required": {
  12. "text": ("STRING", {"multiline": True}),
  13. "folder": ("STRING", {"default": "A:/Comfy/wildcards/"}),
  14. "seed": ("INT", {"default": 0, "min": -1, "max": 2**31-1}),
  15. "deterministic": ("BOOLEAN", {"default": True}),
  16. "ext": ("STRING", {"default": ".txt"}),
  17. }
  18. }
  19.  
  20. RETURN_TYPES = ("STRING",)
  21. FUNCTION = "generate"
  22. CATEGORY = "Quen/Text"
  23.  
  24. TOKEN_RE = re.compile(r"__([A-Za-z0-9_\-]+)__")
  25.  
  26. def _pick(self, choices, token, seed, deterministic):
  27. if not choices:
  28. return f"__{token}__"
  29. if deterministic:
  30. h = hashlib.sha256(f"{token}:{seed}".encode("utf-8")).digest()
  31. rnd = random.Random(int.from_bytes(h[:8], "little"))
  32. return rnd.choice(choices)
  33. else:
  34. return random.choice(choices)
  35.  
  36. def generate(self, text, folder, seed, deterministic, ext):
  37. folder = os.path.expandvars(os.path.expanduser(folder))
  38. def replace(m):
  39. token = m.group(1)
  40. path = os.path.join(folder, f"{token}{ext}")
  41. try:
  42. with open(path, "r", encoding="utf-8") as f:
  43. lines = [ln.strip() for ln in f.readlines() if ln.strip()]
  44. except Exception:
  45. lines = []
  46. return self._pick(lines, token, seed, deterministic)
  47. return (self.TOKEN_RE.sub(replace, text),)
  48.  
  49. NODE_CLASS_MAPPINGS = {
  50. "QuenWildcardPrompt": QuenWildcardPrompt
  51. }
  52. NODE_DISPLAY_NAME_MAPPINGS = {
  53. "QuenWildcardPrompt": "Quen Wildcard Prompt"
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment