File size: 2,116 Bytes
9ab8b5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil
import importlib
import subprocess
from pathlib import Path

def get_modules():
    from sys import modules
    s = set()
    for m in modules.values():
        try: s.add(m)
        except Exception: ...
    return s

def reload_modules(m):
    s = get_modules()
    for module in s - m :
        try: importlib.reload(module)
        except Exception: ...

PRELOADED_MODULES = None

def install(module, PRELOADED_MODULES):
    if importlib.util.find_spec(module) is not None: return
    if PRELOADED_MODULES is None:
        PRELOADED_MODULES = get_modules()
    import sys
    try:
        print(f"\033[92m[smZNodes] \033[0;31m{module} is not installed. Attempting to install...\033[0m")
        subprocess.check_call([sys.executable, "-m", "pip", "install", module])
        reload_modules(PRELOADED_MODULES)
        print(f"\033[92m[smZNodes] {module} Installed!\033[0m")
    except Exception as e:
        print(f"\033[92m[smZNodes] \033[0;31mFailed to install {module}.\033[0m")
    return PRELOADED_MODULES

for pkg in ['compel', 'lark']:
    PRELOADED_MODULES = install(pkg, PRELOADED_MODULES)

# ============================
# web

cwd_path = Path(__file__).parent
comfy_path = cwd_path.parent.parent

def setup_web_extension():
    import nodes
    web_extension_path = os.path.join(comfy_path, "web", "extensions", "smZNodes")
    if os.path.exists(web_extension_path):
        shutil.rmtree(web_extension_path)
    if not hasattr(nodes, "EXTENSION_WEB_DIRS"):
        if not os.path.exists(web_extension_path):
            os.makedirs(web_extension_path)
        js_src_path = os.path.join(cwd_path, "web", "smZdynamicWidgets.js")
        shutil.copy(js_src_path, web_extension_path)

setup_web_extension()

# ============================

from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
WEB_DIRECTORY = "./web"
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]

from .smZNodes import add_custom_samplers, register_hooks

add_custom_samplers()
register_hooks()