88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
# switch_path_lazy.py
|
||
|
||
from server import PromptServer
|
||
from comfy_execution.graph_utils import ExecutionBlocker # Important !
|
||
|
||
class AnyType(str):
|
||
"""Wildcard type to accept any input/output type"""
|
||
def __ne__(self, __value: object) -> bool:
|
||
return False
|
||
def __eq__(self, __value: object) -> bool:
|
||
return True
|
||
def __str__(self):
|
||
return "*"
|
||
|
||
ANY_TYPE = AnyType("*")
|
||
|
||
|
||
class SwitchPathLazy:
|
||
|
||
@classmethod
|
||
def INPUT_TYPES(cls):
|
||
return {
|
||
"required": {
|
||
"state": ("BOOLEAN", {"default": False}),
|
||
"on_true": (ANY_TYPE, {"lazy": True}),
|
||
"on_false": (ANY_TYPE, {"lazy": True}),
|
||
},
|
||
"hidden": {"unique_id": "UNIQUE_ID"},
|
||
}
|
||
|
||
RETURN_TYPES = (ANY_TYPE,)
|
||
RETURN_NAMES = ("output",)
|
||
FUNCTION = "execute"
|
||
CATEGORY = "flow control"
|
||
|
||
def check_lazy_status(self, state, on_true=None, on_false=None, **kwargs):
|
||
"""Tell ComfyUI which lazy input is required"""
|
||
if state:
|
||
return ["on_true"] # Only require on_true if state=True
|
||
else:
|
||
return ["on_false"] # Only require on_false if state=False
|
||
|
||
def execute(self, state, on_true=None, on_false=None, unique_id=None, **kwargs):
|
||
# Determine active and inactive values
|
||
if state:
|
||
active = on_true
|
||
inactive = on_false
|
||
active_path = "on_true"
|
||
inactive_path = "on_false"
|
||
else:
|
||
active = on_false
|
||
inactive = on_true
|
||
active_path = "on_false"
|
||
inactive_path = "on_true"
|
||
|
||
# --- Live display under the node ---
|
||
if unique_id:
|
||
try:
|
||
html_text = (
|
||
f"<b style='color:white;'>State:</b> <b style='color:#a0ffa0;'>{str(state).upper()}</b><br>"
|
||
f"<b style='color:white;'>Active Path:</b> <b style='color:#a0ffa0;'>{active_path}</b><br>"
|
||
f"<b style='color:white;'>Inactive Path:</b> <b style='color:#ff8080;'>{inactive_path}</b><br>"
|
||
f"<b style='color:white;'>Lazy Skip:</b> <span style='color:#a0ffa0;'>inactive branch blocked</span>"
|
||
)
|
||
PromptServer.instance.send_progress_text(html_text, unique_id)
|
||
except Exception:
|
||
pass
|
||
|
||
# If the inactive branch was connected, return ExecutionBlocker to block upstream execution
|
||
# (but if it was None, just pass None – no need to block)
|
||
if inactive is not None:
|
||
return (ExecutionBlocker(None),) # This skips execution on the inactive branch
|
||
|
||
# Otherwise, return the active value
|
||
return (active,)
|
||
|
||
|
||
# Node registration
|
||
NODE_CLASS_MAPPINGS = {
|
||
"SwitchPathLazy": SwitchPathLazy
|
||
}
|
||
|
||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||
"SwitchPathLazy": "Switch Path (Lazy + Info)"
|
||
}
|
||
|
||
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
|