-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgh_nodes.py
305 lines (252 loc) · 8.92 KB
/
gh_nodes.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import torch
from pathlib import Path
from typing import Tuple
import hashlib
from PIL import Image, ImageOps
import base64
import socket
from io import BytesIO
import numpy as np
import folder_paths
class AnyType(str):
"""A special class that is always equal in not equal comparisons. Credit to pythongosssss"""
def __ne__(self, __value: object) -> bool:
return False
any_type = AnyType("*")
class GHNode:
@classmethod
def INPUT_TYPES(cls):
raise NotImplementedError("INPUT_TYPES method must be implemented in subclasses.")
RETURN_TYPES = (any_type,)
FUNCTION = "run"
OUTPUT_NODE = True
CATEGORY = "Grasshopper"
def run(self, *args, **kwargs):
raise NotImplementedError("run method must be implemented in subclasses.")
class LoadImageGH:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input_val": ("STRING", {"multiline": False}),
"invert_mask": ("BOOLEAN", {"default": False}),
"nickname": ("STRING", {"multiline": False}),
}
}
#RETURN_TYPES = ("IMAGE","MASK",)
RETURN_TYPES = (any_type,any_type,)
RETURN_NAMES = ("image","mask",)
FUNCTION = "load_image"
OUTPUT_NODE = True
CATEGORY = "Grasshopper"
def load_image(self, input_val, invert_mask, nickname):
image_path = LoadImageGH._resolve_path(input_val)
i = Image.open(image_path).convert("RGBA") # Open image with alpha channel
i = ImageOps.exif_transpose(i)
input_val = i.convert("RGB") # Convert to RGB for standardization
input_val = np.array(input_val).astype(np.float32) / 255.0
input_val = torch.from_numpy(input_val)[None,]
if 'A' in i.getbands(): # Check if alpha channel exists
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
if invert_mask:
mask = 1 - mask
else:
mask = torch.zeros(input_val.shape[1:], dtype=torch.float32) # Create a zero mask if no alpha channel
return (input_val, mask.unsqueeze(0))
@staticmethod
def _resolve_path(input_val):
image_path = Path(folder_paths.get_annotated_filepath(input_val))
return image_path
@classmethod
def IS_CHANGED(cls, input_val):
image_path = LoadImageGH._resolve_path(input_val)
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(cls, input_val):
# If image is an output of another node, it will be None during validation
if input_val is None:
return True
image_path = GHLoadImage._resolve_path(input_val)
if not image_path.exists():
return "Invalid image path: {}".format(image_path)
return True
class GHFloat(GHNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val": ("FLOAT", {"default": 0.00}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_NAMES = ("float",)
def run(self, input_val, nickname):
return (input_val,)
class GHInteger(GHNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val": ("INT", {"default": 0}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_NAMES = ("integer",)
def run(self, input_val, nickname):
return (input_val,)
class GHBool(GHNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val": ("BOOLEAN", {"default": False}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_NAMES = ("boolean",)
def run(self, input_val, nickname):
return (input_val,)
class GHFile(GHNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val": ("STRING", {"multiline": False}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_NAMES = ("file",)
def run(self, input_val, nickname):
return (input_val,)
class GHString(GHNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val": ("STRING", {"multiline": False}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_NAMES = ("string",)
def run(self, input_val, nickname):
return (input_val,)
class GHPrompt(GHNode):
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val": ("STRING", {"multiline": True}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_NAMES = ("prompt",)
def run(self, input_val, nickname):
return (input_val,)
class GHSampler:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_val_seed": ("INT", {"default": -1}),
"input_val_steps": ("INT", {"default": 10}),
"input_val_cfg": ("FLOAT", {"default": 6}),
"input_val_sampler": ("STRING", {"default": "dpmpp_2m_sde","multiline": False}),
"input_val_scheduler": ("STRING", {"default": "karras","multiline": False}),
"input_val_denoise": ("FLOAT", {"default": 1}),
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_TYPES = (any_type,any_type,any_type,any_type,any_type,any_type,)
RETURN_NAMES = ("seed","steps","cfg","sampler","scheduler","denoise",)
FUNCTION = "run"
OUTPUT_NODE = True
CATEGORY = "Grasshopper"
def run(self, input_val_seed,input_val_steps,input_val_cfg,input_val_sampler,input_val_scheduler,input_val_denoise, nickname):
return (input_val_seed,input_val_steps,input_val_cfg,input_val_sampler,input_val_scheduler,input_val_denoise,)
"""
class GHReceivedImage:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"nickname": ("STRING", {"multiline": False}),
}
}
RETURN_TYPES = ("IMAGE", )
RETURN_NAMES = ("received image",)
FUNCTION = "load_image"
OUTPUT_NODE = True
CATEGORY = "Grasshopper"
@classmethod
def IS_CHANGED(s, *args, **kwargs):
return torch.rand(1).item()
def __init__(self):
super().__init__() # Call superclass constructor
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(('127.0.0.1', 8888))
self.server.listen(1)
print(f"Waiting for connection...")
def receive_image(self):
try:
self.server.settimeout(30) # Set timeout to 60 seconds
conn, addr = self.server.accept()
print(f"Connected to {addr}")
data = b""
while True:
chunk = conn.recv(1024)
if not chunk:
break
data += chunk
if data:
img = Image.open(BytesIO(data))
img = img.convert("RGB")
img = np.array(img).astype(np.float32) / 255.0
img = torch.from_numpy(img)[None,]
return img
except socket.timeout:
print("Timeout: No response from the server after 1 minute.")
return None
except Exception as e:
print("Error occurred during image receiving:", e)
return None
finally:
if 'conn' in locals():
conn.close() # Close the connection if it was established
def load_image(self, nickname):
try:
num = torch.randint(0, 1000, (1,)).item()
received_image = self.receive_image()
if received_image is None:
return None # Return None if no image was received
outdata = [received_image, num]
return (outdata[0], )
except Exception as e:
print("Error occurred during image receiving:", e)
return None
"""
NODE_CLASS_MAPPINGS = {
"GHSampler": GHSampler,
"GHPrompt": GHPrompt,
"GHString": GHString,
"GHInteger": GHInteger,
"GHFloat": GHFloat,
"GHBool": GHBool,
"GHFile": GHFile,
"LoadImageGH": LoadImageGH
#"GHReceivedImage": GHReceivedImage
}
NODE_DISPLAY_NAME_MAPPINGS = {
"GHSampler": "GHSampler",
"GHPrompt": "GHPrompt",
"GHString": "GHString",
"GHInteger": "GHInteger",
"GHFloat": "GHFloat",
"GHBool": "GHBool",
"GHFile": "GHFile",
"LoadImageGH": "LoadImageGH"
#"GHReceivedImage": "GHReceivedImage"
}