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

rework set_named_arg #14773

Merged
merged 2 commits into from
Jan 27, 2024
Merged
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
28 changes: 20 additions & 8 deletions modules/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,22 +939,34 @@ def setup_scrips(self, p, *, is_ui=True):
except Exception:
errors.report(f"Error running setup: {script.filename}", exc_info=True)

def set_named_arg(self, args, script_type, arg_elem_id, value):
script = next((x for x in self.scripts if type(x).__name__ == script_type), None)
def set_named_arg(self, args, script_name, arg_elem_id, value, fuzzy=False):
"""Locate an arg of a specific script in script_args and set its value
Args:
args: all script args of process p, p.script_args
script_name: the name target script name to
arg_elem_id: the elem_id of the target arg
value: the value to set
fuzzy: if True, arg_elem_id can be a substring of the control.elem_id else exact match
Returns:
Updated script args
when script_name in not found or arg_elem_id is not found in script controls, raise RuntimeError
"""
script = next((x for x in self.scripts if x.name == script_name), None)
if script is None:
return
raise RuntimeError(f"script {script_name} not found")

for i, control in enumerate(script.controls):
if arg_elem_id in control.elem_id:
if arg_elem_id in control.elem_id if fuzzy else arg_elem_id == control.elem_id:
index = script.args_from + i

if isinstance(args, list):
if isinstance(args, tuple):
return args[:index] + (value,) + args[index + 1:]
elif isinstance(args, list):
args[index] = value
return args
elif isinstance(args, tuple):
return args[:index] + (value,) + args[index+1:]
else:
return None
raise RuntimeError(f"args is not a list or tuple, but {type(args)}")
raise RuntimeError(f"arg_elem_id {arg_elem_id} not found in script {script_name}")


scripts_txt2img: ScriptRunner = None
Expand Down
Loading