scripts.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. import os
  2. import re
  3. import sys
  4. import inspect
  5. from collections import namedtuple
  6. from dataclasses import dataclass
  7. import gradio as gr
  8. from modules import shared, paths, script_callbacks, extensions, script_loading, scripts_postprocessing, errors, timer
  9. AlwaysVisible = object()
  10. class PostprocessImageArgs:
  11. def __init__(self, image):
  12. self.image = image
  13. class PostprocessBatchListArgs:
  14. def __init__(self, images):
  15. self.images = images
  16. @dataclass
  17. class OnComponent:
  18. component: gr.blocks.Block
  19. class Script:
  20. name = None
  21. """script's internal name derived from title"""
  22. section = None
  23. """name of UI section that the script's controls will be placed into"""
  24. filename = None
  25. args_from = None
  26. args_to = None
  27. alwayson = False
  28. is_txt2img = False
  29. is_img2img = False
  30. tabname = None
  31. group = None
  32. """A gr.Group component that has all script's UI inside it."""
  33. create_group = True
  34. """If False, for alwayson scripts, a group component will not be created."""
  35. infotext_fields = None
  36. """if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
  37. parsing infotext to set the value for the component; see ui.py's txt2img_paste_fields for an example
  38. """
  39. paste_field_names = None
  40. """if set in ui(), this is a list of names of infotext fields; the fields will be sent through the
  41. various "Send to <X>" buttons when clicked
  42. """
  43. api_info = None
  44. """Generated value of type modules.api.models.ScriptInfo with information about the script for API"""
  45. on_before_component_elem_id = None
  46. """list of callbacks to be called before a component with an elem_id is created"""
  47. on_after_component_elem_id = None
  48. """list of callbacks to be called after a component with an elem_id is created"""
  49. setup_for_ui_only = False
  50. """If true, the script setup will only be run in Gradio UI, not in API"""
  51. def title(self):
  52. """this function should return the title of the script. This is what will be displayed in the dropdown menu."""
  53. raise NotImplementedError()
  54. def ui(self, is_img2img):
  55. """this function should create gradio UI elements. See https://gradio.app/docs/#components
  56. The return value should be an array of all components that are used in processing.
  57. Values of those returned components will be passed to run() and process() functions.
  58. """
  59. pass
  60. def show(self, is_img2img):
  61. """
  62. is_img2img is True if this function is called for the img2img interface, and Fasle otherwise
  63. This function should return:
  64. - False if the script should not be shown in UI at all
  65. - True if the script should be shown in UI if it's selected in the scripts dropdown
  66. - script.AlwaysVisible if the script should be shown in UI at all times
  67. """
  68. return True
  69. def run(self, p, *args):
  70. """
  71. This function is called if the script has been selected in the script dropdown.
  72. It must do all processing and return the Processed object with results, same as
  73. one returned by processing.process_images.
  74. Usually the processing is done by calling the processing.process_images function.
  75. args contains all values returned by components from ui()
  76. """
  77. pass
  78. def setup(self, p, *args):
  79. """For AlwaysVisible scripts, this function is called when the processing object is set up, before any processing starts.
  80. args contains all values returned by components from ui().
  81. """
  82. pass
  83. def before_process(self, p, *args):
  84. """
  85. This function is called very early during processing begins for AlwaysVisible scripts.
  86. You can modify the processing object (p) here, inject hooks, etc.
  87. args contains all values returned by components from ui()
  88. """
  89. pass
  90. def process(self, p, *args):
  91. """
  92. This function is called before processing begins for AlwaysVisible scripts.
  93. You can modify the processing object (p) here, inject hooks, etc.
  94. args contains all values returned by components from ui()
  95. """
  96. pass
  97. def before_process_batch(self, p, *args, **kwargs):
  98. """
  99. Called before extra networks are parsed from the prompt, so you can add
  100. new extra network keywords to the prompt with this callback.
  101. **kwargs will have those items:
  102. - batch_number - index of current batch, from 0 to number of batches-1
  103. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  104. - seeds - list of seeds for current batch
  105. - subseeds - list of subseeds for current batch
  106. """
  107. pass
  108. def after_extra_networks_activate(self, p, *args, **kwargs):
  109. """
  110. Called after extra networks activation, before conds calculation
  111. allow modification of the network after extra networks activation been applied
  112. won't be call if p.disable_extra_networks
  113. **kwargs will have those items:
  114. - batch_number - index of current batch, from 0 to number of batches-1
  115. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  116. - seeds - list of seeds for current batch
  117. - subseeds - list of subseeds for current batch
  118. - extra_network_data - list of ExtraNetworkParams for current stage
  119. """
  120. pass
  121. def process_batch(self, p, *args, **kwargs):
  122. """
  123. Same as process(), but called for every batch.
  124. **kwargs will have those items:
  125. - batch_number - index of current batch, from 0 to number of batches-1
  126. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  127. - seeds - list of seeds for current batch
  128. - subseeds - list of subseeds for current batch
  129. """
  130. pass
  131. def postprocess_batch(self, p, *args, **kwargs):
  132. """
  133. Same as process_batch(), but called for every batch after it has been generated.
  134. **kwargs will have same items as process_batch, and also:
  135. - batch_number - index of current batch, from 0 to number of batches-1
  136. - images - torch tensor with all generated images, with values ranging from 0 to 1;
  137. """
  138. pass
  139. def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, *args, **kwargs):
  140. """
  141. Same as postprocess_batch(), but receives batch images as a list of 3D tensors instead of a 4D tensor.
  142. This is useful when you want to update the entire batch instead of individual images.
  143. You can modify the postprocessing object (pp) to update the images in the batch, remove images, add images, etc.
  144. If the number of images is different from the batch size when returning,
  145. then the script has the responsibility to also update the following attributes in the processing object (p):
  146. - p.prompts
  147. - p.negative_prompts
  148. - p.seeds
  149. - p.subseeds
  150. **kwargs will have same items as process_batch, and also:
  151. - batch_number - index of current batch, from 0 to number of batches-1
  152. """
  153. pass
  154. def postprocess_image(self, p, pp: PostprocessImageArgs, *args):
  155. """
  156. Called for every image after it has been generated.
  157. """
  158. pass
  159. def postprocess(self, p, processed, *args):
  160. """
  161. This function is called after processing ends for AlwaysVisible scripts.
  162. args contains all values returned by components from ui()
  163. """
  164. pass
  165. def before_component(self, component, **kwargs):
  166. """
  167. Called before a component is created.
  168. Use elem_id/label fields of kwargs to figure out which component it is.
  169. This can be useful to inject your own components somewhere in the middle of vanilla UI.
  170. You can return created components in the ui() function to add them to the list of arguments for your processing functions
  171. """
  172. pass
  173. def after_component(self, component, **kwargs):
  174. """
  175. Called after a component is created. Same as above.
  176. """
  177. pass
  178. def on_before_component(self, callback, *, elem_id):
  179. """
  180. Calls callback before a component is created. The callback function is called with a single argument of type OnComponent.
  181. May be called in show() or ui() - but it may be too late in latter as some components may already be created.
  182. This function is an alternative to before_component in that it also cllows to run before a component is created, but
  183. it doesn't require to be called for every created component - just for the one you need.
  184. """
  185. if self.on_before_component_elem_id is None:
  186. self.on_before_component_elem_id = []
  187. self.on_before_component_elem_id.append((elem_id, callback))
  188. def on_after_component(self, callback, *, elem_id):
  189. """
  190. Calls callback after a component is created. The callback function is called with a single argument of type OnComponent.
  191. """
  192. if self.on_after_component_elem_id is None:
  193. self.on_after_component_elem_id = []
  194. self.on_after_component_elem_id.append((elem_id, callback))
  195. def describe(self):
  196. """unused"""
  197. return ""
  198. def elem_id(self, item_id):
  199. """helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
  200. need_tabname = self.show(True) == self.show(False)
  201. tabkind = 'img2img' if self.is_img2img else 'txt2img'
  202. tabname = f"{tabkind}_" if need_tabname else ""
  203. title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
  204. return f'script_{tabname}{title}_{item_id}'
  205. def before_hr(self, p, *args):
  206. """
  207. This function is called before hires fix start.
  208. """
  209. pass
  210. class ScriptBuiltinUI(Script):
  211. setup_for_ui_only = True
  212. def elem_id(self, item_id):
  213. """helper function to generate id for a HTML element, constructs final id out of tab and user-supplied item_id"""
  214. need_tabname = self.show(True) == self.show(False)
  215. tabname = ('img2img' if self.is_img2img else 'txt2img') + "_" if need_tabname else ""
  216. return f'{tabname}{item_id}'
  217. current_basedir = paths.script_path
  218. def basedir():
  219. """returns the base directory for the current script. For scripts in the main scripts directory,
  220. this is the main directory (where webui.py resides), and for scripts in extensions directory
  221. (ie extensions/aesthetic/script/aesthetic.py), this is extension's directory (extensions/aesthetic)
  222. """
  223. return current_basedir
  224. ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
  225. scripts_data = []
  226. postprocessing_scripts_data = []
  227. ScriptClassData = namedtuple("ScriptClassData", ["script_class", "path", "basedir", "module"])
  228. def list_scripts(scriptdirname, extension, *, include_extensions=True):
  229. scripts_list = []
  230. basedir = os.path.join(paths.script_path, scriptdirname)
  231. if os.path.exists(basedir):
  232. for filename in sorted(os.listdir(basedir)):
  233. scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(basedir, filename)))
  234. if include_extensions:
  235. for ext in extensions.active():
  236. scripts_list += ext.list_files(scriptdirname, extension)
  237. scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
  238. return scripts_list
  239. def list_files_with_name(filename):
  240. res = []
  241. dirs = [paths.script_path] + [ext.path for ext in extensions.active()]
  242. for dirpath in dirs:
  243. if not os.path.isdir(dirpath):
  244. continue
  245. path = os.path.join(dirpath, filename)
  246. if os.path.isfile(path):
  247. res.append(path)
  248. return res
  249. def load_scripts():
  250. global current_basedir
  251. scripts_data.clear()
  252. postprocessing_scripts_data.clear()
  253. script_callbacks.clear_callbacks()
  254. scripts_list = list_scripts("scripts", ".py") + list_scripts("modules/processing_scripts", ".py", include_extensions=False)
  255. syspath = sys.path
  256. def register_scripts_from_module(module):
  257. for script_class in module.__dict__.values():
  258. if not inspect.isclass(script_class):
  259. continue
  260. if issubclass(script_class, Script):
  261. scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
  262. elif issubclass(script_class, scripts_postprocessing.ScriptPostprocessing):
  263. postprocessing_scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
  264. def orderby(basedir):
  265. # 1st webui, 2nd extensions-builtin, 3rd extensions
  266. priority = {os.path.join(paths.script_path, "extensions-builtin"):1, paths.script_path:0}
  267. for key in priority:
  268. if basedir.startswith(key):
  269. return priority[key]
  270. return 9999
  271. for scriptfile in sorted(scripts_list, key=lambda x: [orderby(x.basedir), x]):
  272. try:
  273. if scriptfile.basedir != paths.script_path:
  274. sys.path = [scriptfile.basedir] + sys.path
  275. current_basedir = scriptfile.basedir
  276. script_module = script_loading.load_module(scriptfile.path)
  277. register_scripts_from_module(script_module)
  278. except Exception:
  279. errors.report(f"Error loading script: {scriptfile.filename}", exc_info=True)
  280. finally:
  281. sys.path = syspath
  282. current_basedir = paths.script_path
  283. timer.startup_timer.record(scriptfile.filename)
  284. global scripts_txt2img, scripts_img2img, scripts_postproc
  285. scripts_txt2img = ScriptRunner()
  286. scripts_img2img = ScriptRunner()
  287. scripts_postproc = scripts_postprocessing.ScriptPostprocessingRunner()
  288. def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
  289. try:
  290. return func(*args, **kwargs)
  291. except Exception:
  292. errors.report(f"Error calling: {filename}/{funcname}", exc_info=True)
  293. return default
  294. class ScriptRunner:
  295. def __init__(self):
  296. self.scripts = []
  297. self.selectable_scripts = []
  298. self.alwayson_scripts = []
  299. self.titles = []
  300. self.title_map = {}
  301. self.infotext_fields = []
  302. self.paste_field_names = []
  303. self.inputs = [None]
  304. self.on_before_component_elem_id = {}
  305. """dict of callbacks to be called before an element is created; key=elem_id, value=list of callbacks"""
  306. self.on_after_component_elem_id = {}
  307. """dict of callbacks to be called after an element is created; key=elem_id, value=list of callbacks"""
  308. def initialize_scripts(self, is_img2img):
  309. from modules import scripts_auto_postprocessing
  310. self.scripts.clear()
  311. self.alwayson_scripts.clear()
  312. self.selectable_scripts.clear()
  313. auto_processing_scripts = scripts_auto_postprocessing.create_auto_preprocessing_script_data()
  314. for script_data in auto_processing_scripts + scripts_data:
  315. script = script_data.script_class()
  316. script.filename = script_data.path
  317. script.is_txt2img = not is_img2img
  318. script.is_img2img = is_img2img
  319. script.tabname = "img2img" if is_img2img else "txt2img"
  320. visibility = script.show(script.is_img2img)
  321. if visibility == AlwaysVisible:
  322. self.scripts.append(script)
  323. self.alwayson_scripts.append(script)
  324. script.alwayson = True
  325. elif visibility:
  326. self.scripts.append(script)
  327. self.selectable_scripts.append(script)
  328. self.apply_on_before_component_callbacks()
  329. def apply_on_before_component_callbacks(self):
  330. for script in self.scripts:
  331. on_before = script.on_before_component_elem_id or []
  332. on_after = script.on_after_component_elem_id or []
  333. for elem_id, callback in on_before:
  334. if elem_id not in self.on_before_component_elem_id:
  335. self.on_before_component_elem_id[elem_id] = []
  336. self.on_before_component_elem_id[elem_id].append((callback, script))
  337. for elem_id, callback in on_after:
  338. if elem_id not in self.on_after_component_elem_id:
  339. self.on_after_component_elem_id[elem_id] = []
  340. self.on_after_component_elem_id[elem_id].append((callback, script))
  341. on_before.clear()
  342. on_after.clear()
  343. def create_script_ui(self, script):
  344. import modules.api.models as api_models
  345. script.args_from = len(self.inputs)
  346. script.args_to = len(self.inputs)
  347. controls = wrap_call(script.ui, script.filename, "ui", script.is_img2img)
  348. if controls is None:
  349. return
  350. script.name = wrap_call(script.title, script.filename, "title", default=script.filename).lower()
  351. api_args = []
  352. for control in controls:
  353. control.custom_script_source = os.path.basename(script.filename)
  354. arg_info = api_models.ScriptArg(label=control.label or "")
  355. for field in ("value", "minimum", "maximum", "step", "choices"):
  356. v = getattr(control, field, None)
  357. if v is not None:
  358. setattr(arg_info, field, v)
  359. api_args.append(arg_info)
  360. script.api_info = api_models.ScriptInfo(
  361. name=script.name,
  362. is_img2img=script.is_img2img,
  363. is_alwayson=script.alwayson,
  364. args=api_args,
  365. )
  366. if script.infotext_fields is not None:
  367. self.infotext_fields += script.infotext_fields
  368. if script.paste_field_names is not None:
  369. self.paste_field_names += script.paste_field_names
  370. self.inputs += controls
  371. script.args_to = len(self.inputs)
  372. def setup_ui_for_section(self, section, scriptlist=None):
  373. if scriptlist is None:
  374. scriptlist = self.alwayson_scripts
  375. for script in scriptlist:
  376. if script.alwayson and script.section != section:
  377. continue
  378. if script.create_group:
  379. with gr.Group(visible=script.alwayson) as group:
  380. self.create_script_ui(script)
  381. script.group = group
  382. else:
  383. self.create_script_ui(script)
  384. def prepare_ui(self):
  385. self.inputs = [None]
  386. def setup_ui(self):
  387. all_titles = [wrap_call(script.title, script.filename, "title") or script.filename for script in self.scripts]
  388. self.title_map = {title.lower(): script for title, script in zip(all_titles, self.scripts)}
  389. self.titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.selectable_scripts]
  390. self.setup_ui_for_section(None)
  391. dropdown = gr.Dropdown(label="Script", elem_id="script_list", choices=["None"] + self.titles, value="None", type="index")
  392. self.inputs[0] = dropdown
  393. self.setup_ui_for_section(None, self.selectable_scripts)
  394. def select_script(script_index):
  395. selected_script = self.selectable_scripts[script_index - 1] if script_index>0 else None
  396. return [gr.update(visible=selected_script == s) for s in self.selectable_scripts]
  397. def init_field(title):
  398. """called when an initial value is set from ui-config.json to show script's UI components"""
  399. if title == 'None':
  400. return
  401. script_index = self.titles.index(title)
  402. self.selectable_scripts[script_index].group.visible = True
  403. dropdown.init_field = init_field
  404. dropdown.change(
  405. fn=select_script,
  406. inputs=[dropdown],
  407. outputs=[script.group for script in self.selectable_scripts]
  408. )
  409. self.script_load_ctr = 0
  410. def onload_script_visibility(params):
  411. title = params.get('Script', None)
  412. if title:
  413. title_index = self.titles.index(title)
  414. visibility = title_index == self.script_load_ctr
  415. self.script_load_ctr = (self.script_load_ctr + 1) % len(self.titles)
  416. return gr.update(visible=visibility)
  417. else:
  418. return gr.update(visible=False)
  419. self.infotext_fields.append((dropdown, lambda x: gr.update(value=x.get('Script', 'None'))))
  420. self.infotext_fields.extend([(script.group, onload_script_visibility) for script in self.selectable_scripts])
  421. self.apply_on_before_component_callbacks()
  422. return self.inputs
  423. def run(self, p, *args):
  424. script_index = args[0]
  425. if script_index == 0:
  426. return None
  427. script = self.selectable_scripts[script_index-1]
  428. if script is None:
  429. return None
  430. script_args = args[script.args_from:script.args_to]
  431. processed = script.run(p, *script_args)
  432. shared.total_tqdm.clear()
  433. return processed
  434. def before_process(self, p):
  435. for script in self.alwayson_scripts:
  436. try:
  437. script_args = p.script_args[script.args_from:script.args_to]
  438. script.before_process(p, *script_args)
  439. except Exception:
  440. errors.report(f"Error running before_process: {script.filename}", exc_info=True)
  441. def process(self, p):
  442. for script in self.alwayson_scripts:
  443. try:
  444. script_args = p.script_args[script.args_from:script.args_to]
  445. script.process(p, *script_args)
  446. except Exception:
  447. errors.report(f"Error running process: {script.filename}", exc_info=True)
  448. def before_process_batch(self, p, **kwargs):
  449. for script in self.alwayson_scripts:
  450. try:
  451. script_args = p.script_args[script.args_from:script.args_to]
  452. script.before_process_batch(p, *script_args, **kwargs)
  453. except Exception:
  454. errors.report(f"Error running before_process_batch: {script.filename}", exc_info=True)
  455. def after_extra_networks_activate(self, p, **kwargs):
  456. for script in self.alwayson_scripts:
  457. try:
  458. script_args = p.script_args[script.args_from:script.args_to]
  459. script.after_extra_networks_activate(p, *script_args, **kwargs)
  460. except Exception:
  461. errors.report(f"Error running after_extra_networks_activate: {script.filename}", exc_info=True)
  462. def process_batch(self, p, **kwargs):
  463. for script in self.alwayson_scripts:
  464. try:
  465. script_args = p.script_args[script.args_from:script.args_to]
  466. script.process_batch(p, *script_args, **kwargs)
  467. except Exception:
  468. errors.report(f"Error running process_batch: {script.filename}", exc_info=True)
  469. def postprocess(self, p, processed):
  470. for script in self.alwayson_scripts:
  471. try:
  472. script_args = p.script_args[script.args_from:script.args_to]
  473. script.postprocess(p, processed, *script_args)
  474. except Exception:
  475. errors.report(f"Error running postprocess: {script.filename}", exc_info=True)
  476. def postprocess_batch(self, p, images, **kwargs):
  477. for script in self.alwayson_scripts:
  478. try:
  479. script_args = p.script_args[script.args_from:script.args_to]
  480. script.postprocess_batch(p, *script_args, images=images, **kwargs)
  481. except Exception:
  482. errors.report(f"Error running postprocess_batch: {script.filename}", exc_info=True)
  483. def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, **kwargs):
  484. for script in self.alwayson_scripts:
  485. try:
  486. script_args = p.script_args[script.args_from:script.args_to]
  487. script.postprocess_batch_list(p, pp, *script_args, **kwargs)
  488. except Exception:
  489. errors.report(f"Error running postprocess_batch_list: {script.filename}", exc_info=True)
  490. def postprocess_image(self, p, pp: PostprocessImageArgs):
  491. for script in self.alwayson_scripts:
  492. try:
  493. script_args = p.script_args[script.args_from:script.args_to]
  494. script.postprocess_image(p, pp, *script_args)
  495. except Exception:
  496. errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
  497. def before_component(self, component, **kwargs):
  498. for callback, script in self.on_before_component_elem_id.get(kwargs.get("elem_id"), []):
  499. try:
  500. callback(OnComponent(component=component))
  501. except Exception:
  502. errors.report(f"Error running on_before_component: {script.filename}", exc_info=True)
  503. for script in self.scripts:
  504. try:
  505. script.before_component(component, **kwargs)
  506. except Exception:
  507. errors.report(f"Error running before_component: {script.filename}", exc_info=True)
  508. def after_component(self, component, **kwargs):
  509. for callback, script in self.on_after_component_elem_id.get(component.elem_id, []):
  510. try:
  511. callback(OnComponent(component=component))
  512. except Exception:
  513. errors.report(f"Error running on_after_component: {script.filename}", exc_info=True)
  514. for script in self.scripts:
  515. try:
  516. script.after_component(component, **kwargs)
  517. except Exception:
  518. errors.report(f"Error running after_component: {script.filename}", exc_info=True)
  519. def script(self, title):
  520. return self.title_map.get(title.lower())
  521. def reload_sources(self, cache):
  522. for si, script in list(enumerate(self.scripts)):
  523. args_from = script.args_from
  524. args_to = script.args_to
  525. filename = script.filename
  526. module = cache.get(filename, None)
  527. if module is None:
  528. module = script_loading.load_module(script.filename)
  529. cache[filename] = module
  530. for script_class in module.__dict__.values():
  531. if type(script_class) == type and issubclass(script_class, Script):
  532. self.scripts[si] = script_class()
  533. self.scripts[si].filename = filename
  534. self.scripts[si].args_from = args_from
  535. self.scripts[si].args_to = args_to
  536. def before_hr(self, p):
  537. for script in self.alwayson_scripts:
  538. try:
  539. script_args = p.script_args[script.args_from:script.args_to]
  540. script.before_hr(p, *script_args)
  541. except Exception:
  542. errors.report(f"Error running before_hr: {script.filename}", exc_info=True)
  543. def setup_scrips(self, p, *, is_ui=True):
  544. for script in self.alwayson_scripts:
  545. if not is_ui and script.setup_for_ui_only:
  546. continue
  547. try:
  548. script_args = p.script_args[script.args_from:script.args_to]
  549. script.setup(p, *script_args)
  550. except Exception:
  551. errors.report(f"Error running setup: {script.filename}", exc_info=True)
  552. scripts_txt2img: ScriptRunner = None
  553. scripts_img2img: ScriptRunner = None
  554. scripts_postproc: scripts_postprocessing.ScriptPostprocessingRunner = None
  555. scripts_current: ScriptRunner = None
  556. def reload_script_body_only():
  557. cache = {}
  558. scripts_txt2img.reload_sources(cache)
  559. scripts_img2img.reload_sources(cache)
  560. reload_scripts = load_scripts # compatibility alias