-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathqueue_counter_reset.py
More file actions
98 lines (89 loc) · 4.06 KB
/
queue_counter_reset.py
File metadata and controls
98 lines (89 loc) · 4.06 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
# Define a global counter variable that persists across executions.
global_counter = 0
class DynamicQueueCounter:
# This flag informs ComfyUI that this node should always run.
ALWAYS_RUN = True
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
# Widget shows start as 0
"start": ("FLOAT", {"default": 0.0}),
# Widget shows stop as 8
"stop": ("FLOAT", {"default": 8.0}),
"step": ("FLOAT", {"default": 1.0}),
# Toggle to reset counter to start
"reset": ("BOOLEAN", {"default": False}),
}
}
RETURN_TYPES = ("FLOAT", "INT", "STRING")
RETURN_NAMES = ("FLOAT", "INT", "Info")
FUNCTION = "node"
CATEGORY = "custom.number"
@classmethod
def IS_CHANGED(cls, **kwargs):
# Always return NaN so that ComfyUI re-runs this node each prompt.
return float("NaN")
def node(self, start, stop, step, reset):
display_offset = 1.0
# Adjust widget values for internal calculation.
# (The displayed value is normally calculated as internal_value + display_offset.)
internal_start = float(start) - display_offset
internal_stop = float(stop) - display_offset
# Initialize counter if this is the first run
if not hasattr(self, "counter"):
self.counter = internal_start
self.last_reset = reset
print("DynamicQueueCounter: Initialized counter =", self.counter)
else:
# Check if user toggled the reset button
if reset != self.last_reset:
# Reset toggle state changed, reset counter to start
self.counter = internal_start
self.last_reset = reset
print(f"DynamicQueueCounter: Reset toggled - counter reset to {self.counter} (display: {internal_start + display_offset})")
if step == 0:
display_current = self.counter + display_offset
info = f"Output: {display_current}. Step is 0, counter remains unchanged."
return float(display_current), int(display_current), info
# Increment mode.
if internal_start < internal_stop:
next_value = self.counter + step
if next_value >= internal_stop:
# In reset branch, output the internal_stop directly (without adding offset)
# so that with start=0 and stop=8 the final displayed number is 7.
output = internal_stop
display_output = output # <-- FIX: remove extra display_offset addition.
display_reset = internal_start + display_offset
info = (
f"Output: {display_output}. Reached or exceeded stop; "
f"counter will reset to {display_reset} on next call."
)
self.counter = internal_start
else:
output = self.counter
self.counter = next_value
display_output = output + display_offset
info = (
f"Output: {display_output}. Next will be {self.counter + display_offset}."
)
# Decrement mode remains unchanged.
else:
next_value = self.counter - step
if next_value <= internal_stop:
output = internal_stop
display_output = output + display_offset
display_reset = internal_start + display_offset
info = (
f"Output: {display_output}. Reached or exceeded stop; "
f"counter will reset to {display_reset} on next call."
)
self.counter = internal_start
else:
output = self.counter
self.counter = next_value
display_output = output + display_offset
info = (
f"Output: {display_output}. Next will be {self.counter + display_offset}."
)
return float(display_output), int(display_output), info