File size: 22,998 Bytes
48d81fc |
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
import os
from PIL import ImageOps
import logging
import folder_paths
import torch
import nodes
from PIL import Image
import numpy as np
from impact import utils
# NOTE: this should not be `from . import core`.
# I don't know why but... 'from .' and 'from impact' refer to different core modules.
# This separates global variables of the core module and breaks the preview bridge.
from impact import core
# <--
import random
class PreviewBridge:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"images": ("IMAGE",),
"image": ("STRING", {"default": ""}),
},
"optional": {
"block": ("BOOLEAN", {"default": False, "label_on": "if_empty_mask", "label_off": "never", "tooltip": "is_empty_mask: If the mask is empty, the execution is stopped.\nnever: The execution is never stopped."}),
"restore_mask": (["never", "always", "if_same_size"], {"tooltip": "if_same_size: If the changed input image is the same size as the previous image, restore using the last saved mask\nalways: Whenever the input image changes, always restore using the last saved mask\nnever: Do not restore the mask.\n`restore_mask` has higher priority than `block`"}),
},
"hidden": {"unique_id": "UNIQUE_ID", "extra_pnginfo": "EXTRA_PNGINFO"},
}
RETURN_TYPES = ("IMAGE", "MASK", )
FUNCTION = "doit"
OUTPUT_NODE = True
CATEGORY = "ImpactPack/Util"
DESCRIPTION = "This is a feature that allows you to edit and send a Mask over a image.\nIf the block is set to 'is_empty_mask', the execution is stopped when the mask is empty."
def __init__(self):
super().__init__()
self.output_dir = folder_paths.get_temp_directory()
self.type = "temp"
self.prev_hash = None
@staticmethod
def load_image(pb_id):
is_fail = False
if pb_id not in core.preview_bridge_image_id_map:
is_fail = True
if not is_fail:
image_path, ui_item = core.preview_bridge_image_id_map[pb_id]
if not os.path.isfile(image_path):
is_fail = True
if not is_fail:
i = Image.open(image_path)
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
else:
image = utils.empty_pil_tensor()
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
ui_item = {
"filename": 'empty.png',
"subfolder": '',
"type": 'temp'
}
return image, mask.unsqueeze(0), ui_item
@staticmethod
def register_clipspace_image(clipspace_path, node_id):
"""Register a clipspace image file in the preview bridge system.
This handles the case where ComfyUI's mask editor creates clipspace files
that need to be integrated with the preview bridge system.
"""
# Remove [input] suffix if present
clean_path = clipspace_path.replace(" [input]", "").replace("[input]", "")
# Try to find the actual clipspace file
input_dir = folder_paths.get_input_directory()
potential_paths = [
clean_path,
os.path.join(input_dir, clean_path),
os.path.join(input_dir, "clipspace", os.path.basename(clean_path)),
os.path.abspath(clean_path),
]
actual_file = None
for path in potential_paths:
if os.path.isfile(path):
actual_file = path
break
if not actual_file:
return False
# Create ui_item for the clipspace file
ui_item = {
'filename': os.path.basename(actual_file),
'subfolder': 'clipspace',
'type': 'input'
}
# Register it using the preview bridge system
core.set_previewbridge_image(node_id, actual_file, ui_item)
# Also register under the original clipspace path for compatibility
core.preview_bridge_image_id_map[clipspace_path] = (actual_file, ui_item)
return True
def doit(self, images, image, unique_id, block=False, restore_mask="never", prompt=None, extra_pnginfo=None):
need_refresh = False
images_changed = False
# Check if images have changed (this determines if we start fresh)
if unique_id not in core.preview_bridge_cache:
need_refresh = True
images_changed = True
elif core.preview_bridge_cache[unique_id][0] is not images:
need_refresh = True
images_changed = True
# If images changed, clear the mask cache to ensure fresh start behavior
# This restores the original behavior where new images start with empty masks
# unless restore_mask is set to "always" or "if_same_size"
if images_changed and restore_mask not in ["always", "if_same_size"] and unique_id in core.preview_bridge_last_mask_cache:
del core.preview_bridge_last_mask_cache[unique_id]
# Handle clipspace files that aren't registered in the preview bridge system
# This only applies when images haven't changed (same image, new mask scenario)
if not need_refresh and image not in core.preview_bridge_image_id_map:
# Check if this is a clipspace file that needs to be registered
is_clipspace = image and ("clipspace" in image.lower() or "[input]" in image)
if is_clipspace:
if not PreviewBridge.register_clipspace_image(image, unique_id):
need_refresh = True
else:
need_refresh = True
if not need_refresh:
pixels, mask, path_item = PreviewBridge.load_image(image)
image = [path_item]
else:
# For new images (images_changed=True), we want to start fresh regardless of restore_mask
# For same image with refresh needed, respect the restore_mask setting
# Exception: when restore_mask is "always", restore even with new images
# Exception: when restore_mask is "if_same_size", allow restoration to check size compatibility
if restore_mask != "never" and (not images_changed or restore_mask in ["always", "if_same_size"]):
mask = core.preview_bridge_last_mask_cache.get(unique_id)
if mask is None:
mask = None
elif restore_mask == "if_same_size" and mask.shape[1:] != images.shape[1:3]:
# For if_same_size, clear mask if dimensions don't match
mask = None
# For "always", keep the mask regardless of size
else:
mask = None
if mask is None:
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
res = nodes.PreviewImage().save_images(images, filename_prefix="PreviewBridge/PB-", prompt=prompt, extra_pnginfo=extra_pnginfo)
else:
masked_images = utils.tensor_convert_rgba(images)
resized_mask = utils.resize_mask(mask, (images.shape[1], images.shape[2])).unsqueeze(3)
resized_mask = 1 - resized_mask
utils.tensor_putalpha(masked_images, resized_mask)
res = nodes.PreviewImage().save_images(masked_images, filename_prefix="PreviewBridge/PB-", prompt=prompt, extra_pnginfo=extra_pnginfo)
image2 = res['ui']['images']
pixels = images
path = os.path.join(folder_paths.get_temp_directory(), 'PreviewBridge', image2[0]['filename'])
core.set_previewbridge_image(unique_id, path, image2[0])
core.preview_bridge_image_id_map[image] = (path, image2[0])
core.preview_bridge_image_name_map[unique_id, path] = (image, image2[0])
core.preview_bridge_cache[unique_id] = (images, image2)
image = image2
is_empty_mask = torch.all(mask == 0)
if block and is_empty_mask and core.is_execution_model_version_supported():
from comfy_execution.graph import ExecutionBlocker
result = ExecutionBlocker(None), ExecutionBlocker(None)
elif block and is_empty_mask:
logging.warning("[Impact Pack] PreviewBridge: ComfyUI is outdated - blocking feature is disabled.")
result = pixels, mask
else:
result = pixels, mask
if not is_empty_mask:
core.preview_bridge_last_mask_cache[unique_id] = mask
return {
"ui": {"images": image},
"result": result,
}
def decode_latent(latent, preview_method, vae_opt=None):
if vae_opt is not None:
image = nodes.VAEDecode().decode(vae_opt, latent)[0]
return image
from comfy.cli_args import LatentPreviewMethod
import comfy.latent_formats as latent_formats
if preview_method.startswith("TAE"):
decoder_name = None
if preview_method == "TAESD15":
decoder_name = "taesd"
elif preview_method == 'TAESDXL':
decoder_name = "taesdxl"
elif preview_method == 'TAESD3':
decoder_name = "taesd3"
elif preview_method == 'TAEF1':
decoder_name = "taef1"
if decoder_name:
vae = nodes.VAELoader().load_vae(decoder_name)[0]
image = nodes.VAEDecode().decode(vae, latent)[0]
return image
if preview_method == "Latent2RGB-SD15":
latent_format = latent_formats.SD15()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-SDXL":
latent_format = latent_formats.SDXL()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-SD3":
latent_format = latent_formats.SD3()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-SD-X4":
latent_format = latent_formats.SD_X4()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-Playground-2.5":
latent_format = latent_formats.SDXL_Playground_2_5()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-SC-Prior":
latent_format = latent_formats.SC_Prior()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-SC-B":
latent_format = latent_formats.SC_B()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-FLUX.1":
latent_format = latent_formats.Flux()
method = LatentPreviewMethod.Latent2RGB
elif preview_method == "Latent2RGB-LTXV":
latent_format = latent_formats.LTXV()
method = LatentPreviewMethod.Latent2RGB
else:
logging.warning(f"[Impact Pack] PreviewBridgeLatent: '{preview_method}' is unsupported preview method.")
latent_format = latent_formats.SD15()
method = LatentPreviewMethod.Latent2RGB
previewer = core.get_previewer("cpu", latent_format=latent_format, force=True, method=method)
samples = latent_format.process_in(latent['samples'])
pil_image = previewer.decode_latent_to_preview(samples)
pixels_size = pil_image.size[0]*8, pil_image.size[1]*8
resized_image = pil_image.resize(pixels_size, resample=utils.LANCZOS)
return utils.to_tensor(resized_image).unsqueeze(0)
class PreviewBridgeLatent:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"latent": ("LATENT",),
"image": ("STRING", {"default": ""}),
"preview_method": (["Latent2RGB-FLUX.1",
"Latent2RGB-SDXL", "Latent2RGB-SD15", "Latent2RGB-SD3",
"Latent2RGB-SD-X4", "Latent2RGB-Playground-2.5",
"Latent2RGB-SC-Prior", "Latent2RGB-SC-B",
"Latent2RGB-LTXV",
"TAEF1", "TAESDXL", "TAESD15", "TAESD3"],),
},
"optional": {
"vae_opt": ("VAE", ),
"block": ("BOOLEAN", {"default": False, "label_on": "if_empty_mask", "label_off": "never", "tooltip": "is_empty_mask: If the mask is empty, the execution is stopped.\nnever: The execution is never stopped. Instead, it returns a white mask."}),
"restore_mask": (["never", "always", "if_same_size"], {"tooltip": "if_same_size: If the changed input latent is the same size as the previous latent, restore using the last saved mask\nalways: Whenever the input latent changes, always restore using the last saved mask\nnever: Do not restore the mask.\n`restore_mask` has higher priority than `block`\nIf the input latent already has a mask, do not restore mask."}),
},
"hidden": {"unique_id": "UNIQUE_ID", "prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
}
RETURN_TYPES = ("LATENT", "MASK", )
FUNCTION = "doit"
OUTPUT_NODE = True
CATEGORY = "ImpactPack/Util"
DESCRIPTION = "This is a feature that allows you to edit and send a Mask over a latent image.\nIf the block is set to 'is_empty_mask', the execution is stopped when the mask is empty."
def __init__(self):
super().__init__()
self.output_dir = folder_paths.get_temp_directory()
self.type = "temp"
self.prev_hash = None
self.prefix_append = "_temp_" + ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for x in range(5))
@staticmethod
def load_image(pb_id):
is_fail = False
if pb_id not in core.preview_bridge_image_id_map:
is_fail = True
if not is_fail:
image_path, ui_item = core.preview_bridge_image_id_map[pb_id]
if not os.path.isfile(image_path):
is_fail = True
if not is_fail:
i = Image.open(image_path)
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = None
else:
image = utils.empty_pil_tensor()
mask = None
ui_item = {
"filename": 'empty.png',
"subfolder": '',
"type": 'temp'
}
return image, mask, ui_item
def doit(self, latent, image, preview_method, vae_opt=None, block=False, unique_id=None, restore_mask='never', prompt=None, extra_pnginfo=None):
latent_channels = latent['samples'].shape[1]
if 'SD3' in preview_method or 'SC-Prior' in preview_method or 'FLUX.1' in preview_method or 'TAEF1' == preview_method:
preview_method_channels = 16
elif 'LTXV' in preview_method:
preview_method_channels = 128
else:
preview_method_channels = 4
if vae_opt is None and latent_channels != preview_method_channels:
logging.warning("[PreviewBridgeLatent] The version of latent is not compatible with preview_method.\nSD3, SD1/SD2, SDXL, SC-Prior, SC-B and FLUX.1 are not compatible with each other.")
raise Exception("The version of latent is not compatible with preview_method.<BR>SD3, SD1/SD2, SDXL, SC-Prior, SC-B and FLUX.1 are not compatible with each other.")
need_refresh = False
latent_changed = False
# Check if latent has changed
if unique_id not in core.preview_bridge_cache:
need_refresh = True
latent_changed = True
elif (core.preview_bridge_cache[unique_id][0] is not latent
or (vae_opt is None and core.preview_bridge_cache[unique_id][2] is not None)
or (vae_opt is None and core.preview_bridge_cache[unique_id][1] != preview_method)
or (vae_opt is not None and core.preview_bridge_cache[unique_id][2] is not vae_opt)):
need_refresh = True
latent_changed = True
# If latent changed, clear the mask cache to ensure fresh start behavior
# unless restore_mask is set to "always" or "if_same_size"
if latent_changed and restore_mask not in ["always", "if_same_size"] and unique_id in core.preview_bridge_last_mask_cache:
del core.preview_bridge_last_mask_cache[unique_id]
# Handle clipspace files that aren't registered in the preview bridge system
# This only applies when latent hasn't changed (same latent, new mask scenario)
if not need_refresh and image not in core.preview_bridge_image_id_map:
is_clipspace = image and ("clipspace" in image.lower() or "[input]" in image)
if is_clipspace:
if not PreviewBridge.register_clipspace_image(image, unique_id):
need_refresh = True
else:
need_refresh = True
if not need_refresh:
pixels, mask, path_item = PreviewBridge.load_image(image)
if mask is None:
mask = torch.ones(latent['samples'].shape[2:], dtype=torch.float32, device="cpu").unsqueeze(0)
if 'noise_mask' in latent:
res_latent = latent.copy()
del res_latent['noise_mask']
else:
res_latent = latent
is_empty_mask = True
else:
res_latent = latent.copy()
res_latent['noise_mask'] = mask
is_empty_mask = torch.all(mask == 1)
res_image = [path_item]
else:
decoded_image = decode_latent(latent, preview_method, vae_opt)
if 'noise_mask' in latent:
mask = latent['noise_mask'].squeeze(0) # 4D mask -> 3D mask
decoded_pil = utils.to_pil(decoded_image)
inverted_mask = 1 - mask # invert
resized_mask = utils.resize_mask(inverted_mask, (decoded_image.shape[1], decoded_image.shape[2]))
result_pil = utils.apply_mask_alpha_to_pil(decoded_pil, resized_mask)
full_output_folder, filename, counter, _, _ = folder_paths.get_save_image_path("PreviewBridge/PBL-"+self.prefix_append, folder_paths.get_temp_directory(), result_pil.size[0], result_pil.size[1])
file = f"{filename}_{counter}.png"
result_pil.save(os.path.join(full_output_folder, file), compress_level=4)
res_image = [{
'filename': file,
'subfolder': 'PreviewBridge',
'type': 'temp',
}]
is_empty_mask = False
else:
# For new latents (latent_changed=True), start fresh regardless of restore_mask
# For same latent with refresh needed, respect the restore_mask setting
# Exception: when restore_mask is "always", restore even with new latents
# Exception: when restore_mask is "if_same_size", allow restoration to check size compatibility
if restore_mask != "never" and (not latent_changed or restore_mask in ["always", "if_same_size"]):
mask = core.preview_bridge_last_mask_cache.get(unique_id)
if mask is None:
mask = None
elif restore_mask == "if_same_size" and mask.shape[1:] != decoded_image.shape[1:3]:
# For if_same_size, clear mask if dimensions don't match
mask = None
# For "always", keep the mask regardless of size
else:
mask = None
if mask is None:
mask = torch.ones(latent['samples'].shape[2:], dtype=torch.float32, device="cpu").unsqueeze(0)
res = nodes.PreviewImage().save_images(decoded_image, filename_prefix="PreviewBridge/PBL-", prompt=prompt, extra_pnginfo=extra_pnginfo)
else:
masked_images = utils.tensor_convert_rgba(decoded_image)
resized_mask = utils.resize_mask(mask, (decoded_image.shape[1], decoded_image.shape[2])).unsqueeze(3)
resized_mask = 1 - resized_mask
utils.tensor_putalpha(masked_images, resized_mask)
res = nodes.PreviewImage().save_images(masked_images, filename_prefix="PreviewBridge/PBL-", prompt=prompt, extra_pnginfo=extra_pnginfo)
res_image = res['ui']['images']
is_empty_mask = torch.all(mask == 1)
path = os.path.join(folder_paths.get_temp_directory(), 'PreviewBridge', res_image[0]['filename'])
core.set_previewbridge_image(unique_id, path, res_image[0])
core.preview_bridge_image_id_map[image] = (path, res_image[0])
core.preview_bridge_image_name_map[unique_id, path] = (image, res_image[0])
core.preview_bridge_cache[unique_id] = (latent, preview_method, vae_opt, res_image)
res_latent = latent
if block and is_empty_mask and core.is_execution_model_version_supported():
from comfy_execution.graph import ExecutionBlocker
result = ExecutionBlocker(None), ExecutionBlocker(None)
elif block and is_empty_mask:
logging.warning("[Impact Pack] PreviewBridgeLatent: ComfyUI is outdated - blocking feature is disabled.")
result = res_latent, mask
else:
result = res_latent, mask
if not is_empty_mask:
core.preview_bridge_last_mask_cache[unique_id] = mask
return {
"ui": {"images": res_image},
"result": result,
} |