From cb1fca3a7ad8d672125e44322d8045f8c46fe2ba Mon Sep 17 00:00:00 2001 From: Jonas Krauss Date: Mon, 1 Apr 2024 22:38:53 +0200 Subject: [PATCH 1/4] node to create wildcard label prompt from string list --- __init__.py | 2 ++ modules/impact/util_nodes.py | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/__init__.py b/__init__.py index 83717de3..62f9c08e 100644 --- a/__init__.py +++ b/__init__.py @@ -313,6 +313,7 @@ def wildcard_load(): "ImpactNeg": ImpactNeg, "ImpactConditionalStopIteration": ImpactConditionalStopIteration, "ImpactStringSelector": StringSelector, + "WildcardPromptFromStringList": WildcardPromptFromStringList, "RemoveNoiseMask": RemoveNoiseMask, @@ -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", diff --git a/modules/impact/util_nodes.py b/modules/impact/util_nodes.py index 1484d520..85ee2b17 100644 --- a/modules/impact/util_nodes.py +++ b/modules/impact/util_nodes.py @@ -5,6 +5,7 @@ import comfy import sys import nodes +import re class GeneralSwitch: @@ -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 + 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] == None: prefix_all[0] = "" + if postfix_all[0] == None: postfix_all[0] = "" + if restrict_to_tags[0] == None: restrict_to_tags[0] = "" + if exclude_tags[0] == 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: