-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_value_to_string.py
127 lines (99 loc) · 3.85 KB
/
node_value_to_string.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
"""
Node Value to String Nodes
Provides both multi-input and single-input nodes for converting node values to formatted strings.
"""
class NodeValue2StringBase:
"""Base class for node value to string conversion."""
TEMPLATE = "{name}:{value}"
CATEGORY = "string"
OUTPUT_NODE = True
def get_node_title(self, value):
"""
Attempt to extract the node's title or type from the input value.
Args:
value: Input value to extract node information from
Returns:
str: Node title or type
"""
try:
# Check if the value has metadata with node information
if hasattr(value, '_meta'):
# Try to get node title or type from metadata
node_title = value._meta.get('node_title')
node_type = value._meta.get('node_type')
if node_title:
return node_title
elif node_type:
return node_type
# Fallback to checking the value's class name
return value.__class__.__name__
except Exception:
# If all else fails, return a generic name
return "Unknown Node"
def get_node_value(self, value):
"""
Format different types of values consistently.
Args:
value: Input value to format
Returns:
str: Formatted value string
"""
# Handle different value types
if value is None:
return "None"
elif isinstance(value, float):
return f"{value:.4f}"
elif isinstance(value, list):
# For list types, return first element or list length
return str(value[0]) if value else "[]"
else:
return str(value)
class NodeValue2StringMulti(NodeValue2StringBase):
"""A node that formats multiple input values with their source node names."""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"inputcount": ("INT", {"default": 2, "min": 1, "max": 1000, "step": 1}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "format_node_values"
def format_node_values(self, inputcount, **kwargs):
"""
Format node values with their source node names.
Args:
inputcount (int): Number of inputs to process
**kwargs: Dynamic input values
Returns:
tuple: Formatted string result
"""
# Collect and format all connected values
formatted_strings = []
# Process inputs in order from value_1 to value_N
for i in range(1, inputcount + 1):
key = f"value_{i}"
if key in kwargs and kwargs[key] is not None:
value = kwargs[key]
# Get node title for the input
node_title = self.get_node_title(value)
# Format the value
formatted_value = self.get_node_value(value)
# Create formatted string
try:
formatted = self.TEMPLATE.format(name=node_title, value=formatted_value)
formatted_strings.append(formatted)
except Exception as e:
formatted_strings.append(f"Error formatting input {i}: {str(e)}")
# Join all formatted strings with newline
result = "\n".join(formatted_strings)
return (result,)
# Register the nodes
NODE_CLASS_MAPPINGS = {
"nodevalue2stringmulti": NodeValue2StringMulti
}
NODE_DISPLAY_NAME_MAPPINGS = {
"nodevalue2stringmulti": "Node Value to String Multi - klinter"
}
# Export the classes
__all__ = ['NodeValue2StringMulti']