File size: 2,112 Bytes
3dabe4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
from os import environ

import modules.scripts as scripts
from modules import script_callbacks

logger = logging.getLogger(__name__)
logger.setLevel(environ.get("SD_WEBUI_LOG_LEVEL", logging.INFO))


"""

Author: v0xie
GitHub URL: https://github.com/v0xie/sd-webui-incantations

"""


# XYZ Plot
# Based on @mcmonkey4eva's XYZ Plot implementation here: https://github.com/mcmonkeyprojects/sd-dynamic-thresholding/blob/master/scripts/dynamic_thresholding.py
def pag_apply_override(field, boolean: bool = False):
    def fun(p, x, xs):
        if boolean:
            x = True if x.lower() == "true" else False
        setattr(p, field, x)

    return fun


def pag_apply_field(field):
    def fun(p, x, xs):
        if not hasattr(p, "pag_active"):
            setattr(p, "pag_active", True)
        setattr(p, field, x)

    return fun


def get_xyz_axis_options() -> set:
    xyz_grid = [
        x for x in scripts.scripts_data if x.script_class.__module__ == "xyz_grid.py"
    ][0].module
    extra_axis_options = {
        xyz_grid.AxisOption(
            "[PAG] Active",
            str,
            pag_apply_override("pag_active", boolean=True),
            choices=xyz_grid.boolean_choice(reverse=True),
        ),
        xyz_grid.AxisOption("[PAG] PAG Scale", float, pag_apply_field("pag_scale")),
        # xyz_grid.AxisOption("[PAG] ctnms_alpha", float, pag_apply_field("pag_ctnms_alpha")),
    }
    return extra_axis_options


def make_axis_options(extra_axis_options: set):
    xyz_grid = [
        x for x in scripts.scripts_data if x.script_class.__module__ == "xyz_grid.py"
    ][0].module
    current_opts = [x.label for x in xyz_grid.axis_options]
    # TODO:
    for opt in extra_axis_options:
        if opt.label in current_opts:
            return
    xyz_grid.axis_options.extend(extra_axis_options)


def callback_before_ui():
    try:
        extra_axis_options = get_xyz_axis_options()
        make_axis_options(extra_axis_options)
    except:
        logger.exception("Incantation: Error while making axis options")


script_callbacks.on_before_ui(callback_before_ui)