diff --git a/README.md b/README.md index 9db80a2..ea2e375 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,15 @@ ## Purpose -This extension allows the marking of parts of the prompt and moves them to the negative prompt. This allows useful tricks when using a wildcard extension since you can add negative parts from choices made in the positive prompt. +This extension allows the tagging of parts of the prompt and moves them to the +negative prompt. This allows useful tricks when using a wildcard extension +since you can add negative content from choices made in the positive prompt. -The extension must be loaded after the wildcard extension. With the "Dynamic Prompts" extension this happens by default due to default folder names for both extensions. +Note: The extension must be loaded after the wildcard extension. + +With the "Dynamic Prompts" extension this happens by default due to default +folder names for both extensions. But if this is not the case, you can just +rename the extension folder so the ordering works out. ## Format @@ -27,3 +33,18 @@ Where position can be: * pN: at the position of the insertion point "" with N being 0-9 The default position is the start, and also if the insertion point is not found. + +## Example + +You have a wildcard for hair colors (\_\_haircolors\_\_) with one being +strawberry blonde, but you don't want strawberries. So in that option you add a +tag to add to the negative prompt, like so: + +```text +blonde +strawberry blonde +brunette +``` + +Then, if that option is chosen this extension will process it later and move +that part to the negative prompt. diff --git a/scripts/extension.py b/scripts/extension.py index d04012c..249c425 100644 --- a/scripts/extension.py +++ b/scripts/extension.py @@ -1,28 +1,26 @@ if __name__ == "__main__": raise SystemExit("This script must be run from a Stable Diffusion WebUI") -import logging -from modules import scripts, shared, script_callbacks -from modules.processing import StableDiffusionProcessing -from modules.shared import opts -from sendtonegative import SendToNegative import sys import os sys.path.insert(1, os.path.join(sys.path[0], "..")) -__all__ = ["SendToNegativeScript"] +from sendtonegative import SendToNegative +import logging +from modules import scripts, shared, script_callbacks +from modules.processing import StableDiffusionProcessing +from modules.shared import opts class SendToNegativeScript(scripts.Script): - def __init__(self): self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.INFO) if getattr(opts, "is_debug", False): self.logger.setLevel(logging.DEBUG) if not hasattr(self, "callbacks_added"): - script_callbacks.on_ui_settings(on_ui_settings) + script_callbacks.on_ui_settings(_on_ui_settings) self.callbacks_added = True def title(self): @@ -39,12 +37,12 @@ class SendToNegativeScript(scripts.Script): ) -def on_ui_settings(): +def _on_ui_settings(): section = ("send-to-negative", SendToNegative.NAME) shared.opts.add_option( key="stn_tagstart", info=shared.OptionInfo( - "", + SendToNegative.DEFAULT_tagEnd, label="Tag end", section=section, ), @@ -60,7 +58,7 @@ def on_ui_settings(): shared.opts.add_option( key="stn_tagparamstart", info=shared.OptionInfo( - "!", + SendToNegative.DEFAULT_tagParamStart, label="Tag parameter start", section=section, ), @@ -68,7 +66,7 @@ def on_ui_settings(): shared.opts.add_option( key="stn_tagparamend", info=shared.OptionInfo( - "!", + SendToNegative.DEFAULT_tagParamEnd, label="Tag parameter end", section=section, ), @@ -76,7 +74,7 @@ def on_ui_settings(): shared.opts.add_option( key="stn_separator", info=shared.OptionInfo( - ", ", + SendToNegative.DEFAULT_separator, label="Separator used when adding to the negative prompt", section=section, ), @@ -93,7 +91,7 @@ def on_ui_settings(): key="stn_cleanup", info=shared.OptionInfo( True, - label="Try to clean-up the prompt after processing", + label="Try to clean-up the prompt after processing. Removes extra spaces or separators (the configured separator).", section=section, ), ) diff --git a/sendtonegative.py b/sendtonegative.py index f7f512f..5e5c83b 100644 --- a/sendtonegative.py +++ b/sendtonegative.py @@ -3,10 +3,15 @@ import re class SendToNegative: - NAME = "Send to Negative" VERSION = "0.3" + DEFAULT_tagStart = "" + DEFAULT_tagParamStart = "!" + DEFAULT_tagParamEnd = "!" + DEFAULT_separator = ", " + def __init__( self, tagStart=None, @@ -32,9 +37,7 @@ class SendToNegative: pN - content is added where the insertion point N is in the negative prompt or at the start if it does not exist. N can be 0 to 9. - iN - marks the position of insertion point N. Used only in the negative prompt and does not accept content. N can be 0 to 9. - - The tags will be removed from the prompt or negative prompt without considering neighboring whitespace or separators. + iN - tags the position of insertion point N. Used only in the negative prompt and does not accept content. N can be 0 to 9. """ if logger is None: self.logger = logging.getLogger(__name__) @@ -43,18 +46,32 @@ class SendToNegative: self.logger = logger strStart = ( - tagStart if tagStart is not None else getattr(opts, "stn_tagstart", "") strParamStart = ( tagParamStart if tagParamStart is not None - else getattr(opts, "stn_tagparamstart", "!") + else getattr(opts, "stn_tagparamstart", self.DEFAULT_tagParamStart) + if opts is not None + else self.DEFAULT_tagParamStart ) strParamEnd = ( tagParamEnd if tagParamEnd is not None - else getattr(opts, "stn_tagparamend", "!") + else getattr(opts, "stn_tagparamend", self.DEFAULT_tagParamEnd) + if opts is not None + else self.DEFAULT_tagParamEnd ) escapeSequence = r"(?