Skip to content

Improve logic for concatenate node + add wildcard node #8364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion comfy_extras/nodes_string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import random

from comfy.comfy_types.node_typing import IO

Expand All @@ -18,7 +19,10 @@ def INPUT_TYPES(s):
CATEGORY = "utils/string"

def execute(self, string_a, string_b, delimiter, **kwargs):
return delimiter.join((string_a, string_b)),
all_strings = [value for key, value in locals().items() if key.startswith("string_")]
non_empty = [s for s in all_strings if s]
result = delimiter.join(non_empty)
return result,

class StringSubstring():
@classmethod
Expand Down Expand Up @@ -331,6 +335,35 @@ def execute(self, string, regex_pattern, replace, case_insensitive=True, multili
result = re.sub(regex_pattern, replace, string, count=count, flags=flags)
return result,

class Wildcard():
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": (IO.STRING, {"multiline": True}),
"seed": (IO.INT, {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"use_newline_as_separator": (IO.BOOLEAN, {"default": True}),
"separator_if_not_newline": (IO.STRING, {"multiline": False, "default": " "})
}
}

RETURN_TYPES = (IO.STRING,)
FUNCTION = "execute"
CATEGORY = "utils/string"

def execute(self, text, seed, use_newline_as_separator, separator_if_not_newline, **kwargs):
if use_newline_as_separator:
wildcard = text.splitlines()
else:
wildcard = text.split(separator_if_not_newline)

if not wildcard:
return (""),

random.seed(seed)
i = random.randint(0, (len(wildcard)-1))
return (wildcard[i]),

NODE_CLASS_MAPPINGS = {
"StringConcatenate": StringConcatenate,
"StringSubstring": StringSubstring,
Expand All @@ -343,6 +376,7 @@ def execute(self, string, regex_pattern, replace, case_insensitive=True, multili
"RegexMatch": RegexMatch,
"RegexExtract": RegexExtract,
"RegexReplace": RegexReplace,
"Wildcard": Wildcard,
}

NODE_DISPLAY_NAME_MAPPINGS = {
Expand All @@ -357,4 +391,5 @@ def execute(self, string, regex_pattern, replace, case_insensitive=True, multili
"RegexMatch": "Regex Match",
"RegexExtract": "Regex Extract",
"RegexReplace": "Regex Replace",
"Wildcard": "Wildcard",
}