Archived
43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
import os
|
|
import sys
|
|
comfyui_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
if comfyui_root not in sys.path:
|
|
sys.path.insert(0, comfyui_root)
|
|
import comfy.utils
|
|
from comfy_execution.utils import get_executing_context
|
|
|
|
def add_progress_hook(callback, server_instance=None):
|
|
"""Add a progress callback that chains with ComfyUI's built-in hook.
|
|
|
|
Args:
|
|
callback: Function(value, total, preview_image, prompt_id, node_id) to call on progress updates
|
|
server_instance: Optional server instance for fallback prompt_id/node_id
|
|
"""
|
|
_original_hook = None
|
|
_original_set_hook = comfy.utils.set_progress_bar_global_hook
|
|
|
|
def hook(value, total, preview_image, prompt_id=None, node_id=None):
|
|
# Populate prompt_id and node_id from executing context if not provided
|
|
executing_context = get_executing_context()
|
|
if prompt_id is None and executing_context is not None:
|
|
prompt_id = executing_context.prompt_id
|
|
if node_id is None and executing_context is not None:
|
|
node_id = executing_context.node_id
|
|
# Fallback to server instance if available
|
|
if server_instance is not None:
|
|
if prompt_id is None:
|
|
prompt_id = getattr(server_instance, 'last_prompt_id', None)
|
|
if node_id is None:
|
|
node_id = getattr(server_instance, 'last_node_id', None)
|
|
|
|
callback(value, total, preview_image, prompt_id, node_id)
|
|
if _original_hook is not None:
|
|
_original_hook(value, total, preview_image, prompt_id=prompt_id, node_id=node_id)
|
|
|
|
def wrapped_set_hook(function):
|
|
nonlocal _original_hook
|
|
_original_hook = function
|
|
_original_set_hook(hook)
|
|
|
|
comfy.utils.set_progress_bar_global_hook = wrapped_set_hook
|
|
comfy.utils.set_progress_bar_global_hook(hook) |