Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
167 lines (139 loc) · 4.38 KB
/
Copy path__init__.py
File metadata and controls
167 lines (139 loc) · 4.38 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
SimpleVariables - Set/Get Variable nodes for ComfyUI
"""
import time
class AnyType(str):
"""Matches any type for ComfyUI type checking"""
def __eq__(self, _):
return True
def __ne__(self, _):
return False
ANY = AnyType("*")
# Global storage
_variables = {}
class SimpleSet:
"""Set a variable by name - stores value and passes through"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"name": ("STRING", {"default": "var"}),
},
"optional": {
"value": (ANY,),
}
}
RETURN_TYPES = (ANY,)
RETURN_NAMES = ("value",)
FUNCTION = "set_var"
CATEGORY = "SimpleVariables"
OUTPUT_NODE = True
@classmethod
def IS_CHANGED(cls, name, value=None):
return time.time()
def set_var(self, name, value=None):
_variables[name] = value
# Generate preview
if value is None:
preview = f"{name} = None"
elif hasattr(value, 'shape'):
preview = f"{name} = {type(value).__name__} {list(value.shape)}"
elif isinstance(value, (int, float, bool)):
preview = f"{name} = {value}"
elif isinstance(value, str):
preview = f'{name} = "{value[:30]}..."' if len(value) > 30 else f'{name} = "{value}"'
else:
preview = f"{name} = {type(value).__name__}"
return {"ui": {"text": [preview]}, "result": (value,)}
class SimpleGet:
"""Get a variable by name"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"name": ("STRING", {"default": "var"}),
},
"optional": {
"trigger": (ANY,), # Connect to SimpleSet output for execution order
}
}
RETURN_TYPES = (ANY,)
RETURN_NAMES = ("value",)
FUNCTION = "get_var"
CATEGORY = "SimpleVariables"
OUTPUT_NODE = True
@classmethod
def IS_CHANGED(cls, **kwargs):
return time.time()
def get_var(self, name, trigger=None):
value = _variables.get(name)
# Generate preview
if name not in _variables:
preview = f"{name} = NOT SET"
elif value is None:
preview = f"{name} = None"
elif hasattr(value, 'shape'):
preview = f"{name} = {type(value).__name__} {list(value.shape)}"
elif isinstance(value, (int, float, bool)):
preview = f"{name} = {value}"
elif isinstance(value, str):
preview = f'{name} = "{value[:30]}..."' if len(value) > 30 else f'{name} = "{value}"'
else:
preview = f"{name} = {type(value).__name__}"
return {"ui": {"text": [preview]}, "result": (value,)}
class SimpleList:
"""List all stored variables"""
@classmethod
def INPUT_TYPES(cls):
return {"required": {}}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("list",)
FUNCTION = "list_vars"
CATEGORY = "SimpleVariables"
OUTPUT_NODE = True
@classmethod
def IS_CHANGED(cls):
return time.time()
def list_vars(self):
if not _variables:
text = "(no variables)"
else:
lines = [f"{k} = {type(v).__name__}" for k, v in _variables.items()]
text = "\n".join(lines)
return {"ui": {"text": [text]}, "result": (text,)}
class SimpleClear:
"""Clear all stored variables"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"clear": ("BOOLEAN", {"default": False}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("status",)
FUNCTION = "clear_vars"
CATEGORY = "SimpleVariables"
OUTPUT_NODE = True
def clear_vars(self, clear):
if clear:
count = len(_variables)
_variables.clear()
status = f"Cleared {count} variable(s)"
else:
status = "Set clear=True to clear"
return {"ui": {"text": [status]}, "result": (status,)}
NODE_CLASS_MAPPINGS = {
"SimpleSet": SimpleSet,
"SimpleGet": SimpleGet,
"SimpleList": SimpleList,
"SimpleClear": SimpleClear,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"SimpleSet": "SimpleSet",
"SimpleGet": "SimpleGet",
"SimpleList": "SimpleList",
"SimpleClear": "SimpleClear",
}
WEB_DIRECTORY = "./web"
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]