-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAnySwitch.py
71 lines (56 loc) · 2.15 KB
/
AnySwitch.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
import torch
import numpy as np
from server import PromptServer
class SKB_AnySwitch:
name = "SKB_AnySwitch"
description = "A dynamic switch supporting all data types"
CATEGORY = "SKB"
EMPTY_IMAGE = torch.zeros((1, 8, 8, 3))
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"select": ("INT", {"default": 1, "min": 1, "max": 5, "step": 1}),
},
"optional": {
"input_1": ("*",),
"input_2": ("*",),
"input_3": ("*",),
"input_4": ("*",),
"input_5": ("*",),
}
}
RETURN_TYPES = ("IMAGE", "STRING", "INT")
RETURN_NAMES = ("image_output", "text_output", "int_output")
FUNCTION = "switch"
def switch(self, select, **kwargs):
connected_inputs = sum(1 for key in kwargs if key.startswith('input_') and kwargs[key] is not None)
empty_image = torch.zeros((1, 8, 8, 3))
if connected_inputs == 0:
PromptServer.instance.send_sync("SKB_AnySwitch.info", {"message": "No inputs connected"})
return (empty_image, "", 0)
select = min(select, connected_inputs)
selected_input = kwargs.get(f"input_{select}", None)
if selected_input is None:
PromptServer.instance.send_sync("SKB_AnySwitch.info", {"message": f"Input {select} is not connected"})
return (empty_image, "", 0)
if isinstance(selected_input, str):
return (empty_image, selected_input, 0)
elif isinstance(selected_input, int):
return (empty_image, "", selected_input)
elif isinstance(selected_input, (torch.Tensor, np.ndarray)) and len(selected_input.shape) >= 3:
return (selected_input, "", 0)
else:
return (empty_image, "", 0)
@classmethod
def IS_CHANGED(cls, select, **kwargs):
return float("NaN")
@classmethod
def VALIDATE_INPUTS(cls, input_types):
return True
NODE_CLASS_MAPPINGS = {
"SKB_AnySwitch": SKB_AnySwitch
}
NODE_DISPLAY_NAME_MAPPINGS = {
"SKB_AnySwitch": "Any Switch"
}