@@ -17,6 +17,9 @@ class Script:
17
17
args_to = None
18
18
alwayson = False
19
19
20
+ is_txt2img = False
21
+ is_img2img = False
22
+
20
23
"""A gr.Group component that has all script's UI inside it"""
21
24
group = None
22
25
@@ -93,6 +96,23 @@ def postprocess(self, p, processed, *args):
93
96
94
97
pass
95
98
99
+ def before_component (self , component , ** kwargs ):
100
+ """
101
+ Called before a component is created.
102
+ Use elem_id/label fields of kwargs to figure out which component it is.
103
+ This can be useful to inject your own components somewhere in the middle of vanilla UI.
104
+ You can return created components in the ui() function to add them to the list of arguments for your processing functions
105
+ """
106
+
107
+ pass
108
+
109
+ def after_component (self , component , ** kwargs ):
110
+ """
111
+ Called after a component is created. Same as above.
112
+ """
113
+
114
+ pass
115
+
96
116
def describe (self ):
97
117
"""unused"""
98
118
return ""
@@ -195,12 +215,18 @@ def __init__(self):
195
215
self .titles = []
196
216
self .infotext_fields = []
197
217
198
- def setup_ui (self , is_img2img ):
218
+ def initialize_scripts (self , is_img2img ):
219
+ self .scripts .clear ()
220
+ self .alwayson_scripts .clear ()
221
+ self .selectable_scripts .clear ()
222
+
199
223
for script_class , path , basedir in scripts_data :
200
224
script = script_class ()
201
225
script .filename = path
226
+ script .is_txt2img = not is_img2img
227
+ script .is_img2img = is_img2img
202
228
203
- visibility = script .show (is_img2img )
229
+ visibility = script .show (script . is_img2img )
204
230
205
231
if visibility == AlwaysVisible :
206
232
self .scripts .append (script )
@@ -211,6 +237,7 @@ def setup_ui(self, is_img2img):
211
237
self .scripts .append (script )
212
238
self .selectable_scripts .append (script )
213
239
240
+ def setup_ui (self ):
214
241
self .titles = [wrap_call (script .title , script .filename , "title" ) or f"{ script .filename } [error]" for script in self .selectable_scripts ]
215
242
216
243
inputs = [None ]
@@ -220,7 +247,7 @@ def create_script_ui(script, inputs, inputs_alwayson):
220
247
script .args_from = len (inputs )
221
248
script .args_to = len (inputs )
222
249
223
- controls = wrap_call (script .ui , script .filename , "ui" , is_img2img )
250
+ controls = wrap_call (script .ui , script .filename , "ui" , script . is_img2img )
224
251
225
252
if controls is None :
226
253
return
@@ -320,6 +347,22 @@ def postprocess(self, p, processed):
320
347
print (f"Error running postprocess: { script .filename } " , file = sys .stderr )
321
348
print (traceback .format_exc (), file = sys .stderr )
322
349
350
+ def before_component (self , component , ** kwargs ):
351
+ for script in self .scripts :
352
+ try :
353
+ script .before_component (component , ** kwargs )
354
+ except Exception :
355
+ print (f"Error running before_component: { script .filename } " , file = sys .stderr )
356
+ print (traceback .format_exc (), file = sys .stderr )
357
+
358
+ def after_component (self , component , ** kwargs ):
359
+ for script in self .scripts :
360
+ try :
361
+ script .after_component (component , ** kwargs )
362
+ except Exception :
363
+ print (f"Error running after_component: { script .filename } " , file = sys .stderr )
364
+ print (traceback .format_exc (), file = sys .stderr )
365
+
323
366
def reload_sources (self , cache ):
324
367
for si , script in list (enumerate (self .scripts )):
325
368
args_from = script .args_from
@@ -341,6 +384,7 @@ def reload_sources(self, cache):
341
384
342
385
scripts_txt2img = ScriptRunner ()
343
386
scripts_img2img = ScriptRunner ()
387
+ scripts_current : ScriptRunner = None
344
388
345
389
346
390
def reload_script_body_only ():
@@ -357,3 +401,22 @@ def reload_scripts():
357
401
scripts_txt2img = ScriptRunner ()
358
402
scripts_img2img = ScriptRunner ()
359
403
404
+
405
+ def IOComponent_init (self , * args , ** kwargs ):
406
+ if scripts_current is not None :
407
+ scripts_current .before_component (self , ** kwargs )
408
+
409
+ script_callbacks .before_component_callback (self , ** kwargs )
410
+
411
+ res = original_IOComponent_init (self , * args , ** kwargs )
412
+
413
+ script_callbacks .after_component_callback (self , ** kwargs )
414
+
415
+ if scripts_current is not None :
416
+ scripts_current .after_component (self , ** kwargs )
417
+
418
+ return res
419
+
420
+
421
+ original_IOComponent_init = gr .components .IOComponent .__init__
422
+ gr .components .IOComponent .__init__ = IOComponent_init
0 commit comments