Skip to content
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

node to create wildcard label prompt from string list #536

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ def wildcard_load():
"ImpactNeg": ImpactNeg,
"ImpactConditionalStopIteration": ImpactConditionalStopIteration,
"ImpactStringSelector": StringSelector,
"WildcardPromptFromStringList": WildcardPromptFromStringList,

"RemoveNoiseMask": RemoveNoiseMask,

Expand Down Expand Up @@ -433,6 +434,7 @@ def wildcard_load():
"ImpactMakeImageList": "Make Image List",
"ImpactMakeImageBatch": "Make Image Batch",
"ImpactStringSelector": "String Selector",
"WildcardPromptFromStringList": "Wildcard Prompt from String List",
"ImpactIsNotEmptySEGS": "SEGS isn't Empty",
"SetDefaultImageForSEGS": "Set Default Image for SEGS",
"RemoveImageFromSEGS": "Remove Image from SEGS",
Expand Down
60 changes: 60 additions & 0 deletions modules/impact/util_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import comfy
import sys
import nodes
import re


class GeneralSwitch:
Expand Down Expand Up @@ -485,3 +486,62 @@ def doit(self, strings, multiline, select):
selected = lines[select % len(lines)]

return (selected, )


class WildcardPromptFromStringList:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"string_list": ("STRING", {"forceInput": True}),
"prefix_all": ("STRING", {"multiline": False}),
"postfix_all": ("STRING", {"multiline": False}),
"restrict_to_tags": ("STRING", {"multiline": False}),
"exclude_tags": ("STRING", {"multiline": False})
},
}

INPUT_IS_LIST = True
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior seems unclear because string_list is received as a list while the others are not being considered as lists.

It might be better to add a node such as StringListToString instead of directly receiving string_list in this node, and receive input as a concatenated string.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The review was pending due to my mistake.

Copy link
Contributor Author

@jkrauss82 jkrauss82 May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense.

I have added this node here: e41788a (used WAS node suite WAS_Text_List_to_Text as template).

A new workflow making use of the added node is attached.

prompt-per-tile-sample.json

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's awesome

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's awesome

thanks!

I have finally found the time to write up a short tutorial in the impact examples on how I imagine this node to be used, check it out here

RETURN_TYPES = ("STRING", "STRING",)
RETURN_NAMES = ("wildcard", "segs_labels",)
FUNCTION = "doit"

CATEGORY = "ImpactPack/Util"

def doit(self, string_list, prefix_all, postfix_all, restrict_to_tags, exclude_tags):
# need to access as list due to INPUT_IS_LIST
# some sanity checks and normalization for later processing
if prefix_all[0] is None: prefix_all[0] = ""
if postfix_all[0] is None: postfix_all[0] = ""
if restrict_to_tags[0] is None: restrict_to_tags[0] = ""
if exclude_tags[0] is None: exclude_tags[0] = ""
if not isinstance(restrict_to_tags[0], list):
restrict_to_tags[0] = restrict_to_tags[0].split(", ")
if not isinstance(exclude_tags[0], list):
exclude_tags[0] = exclude_tags[0].split(", ")

# build the wildcard prompt per list entry
output = ["[LAB]"]
labels = []
for x in string_list:
label = str(len(labels) + 1)
labels.append(label)
x = x.split(", ")
# restrict to tags
if restrict_to_tags[0][0] != "":
x = list(set(x) & set(restrict_to_tags[0]))
# remove tags
if exclude_tags[0][0] != "":
x = list(set(x) - set(exclude_tags[0]))
# next row: <LABEL> <PREFIX> <TAGS> <POSTFIX>
prompt_for_seg = f'[{label}] {prefix_all[0]} {", ".join(x)} {postfix_all[0]}'.strip()
output.append(prompt_for_seg)
output = "\n".join(output)

# clean string: fixup double spaces, commas etc.
output = re.sub(r' ,', ',', output)
output = re.sub(r' +', ' ', output)
output = re.sub(r',,+', ',', output)
output = re.sub(r'\n, ', '\n', output)

return (output, ", ".join(labels),)