From 4887b0a6317dd316d9ae20f92d8cb859e1564e0d Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Sun, 17 Sep 2023 10:16:33 -0400 Subject: [PATCH] add before process callback --- modules/processing.py | 2 ++ modules/scripts.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/modules/processing.py b/modules/processing.py index fe0e78e6a..7f3a46b43 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -572,6 +572,8 @@ def print_profile(profile, msg: str): def process_images(p: StableDiffusionProcessing) -> Processed: if not hasattr(p.sd_model, 'sd_checkpoint_info'): return None + if p.scripts is not None: + p.scripts.before_process(p) stored_opts = {} for k, v in p.override_settings.copy().items(): orig = shared.opts.data.get(k, None) or shared.opts.data_labels[k].default diff --git a/modules/scripts.py b/modules/scripts.py index c5eb6afe8..697798975 100644 --- a/modules/scripts.py +++ b/modules/scripts.py @@ -67,6 +67,20 @@ class Script: """ pass # pylint: disable=unnecessary-pass + def setup(self, p, *args): + """For AlwaysVisible scripts, this function is called when the processing object is set up, before any processing starts. + args contains all values returned by components from ui(). + """ + pass + + def before_process(self, p, *args): + """ + This function is called very early during processing begins for AlwaysVisible scripts. + You can modify the processing object (p) here, inject hooks, etc. + args contains all values returned by components from ui() + """ + pass + def process(self, p, *args): """ This function is called before processing begins for AlwaysVisible scripts. @@ -437,6 +451,17 @@ class ScriptRunner: s.report() return processed + def before_process(self, p, **kwargs): + s = ScriptSummary('before-process') + for script in self.alwayson_scripts: + try: + script_args = p.script_args[script.args_from:script.args_to] + script.before_process(p, *script_args, **kwargs) + except Exception as e: + errors.display(e, f"Error running before process: {script.filename}") + s.record(script.title()) + s.report() + def process(self, p, **kwargs): s = ScriptSummary('process') for script in self.alwayson_scripts: