Compare commits

...
42 Commits
Author SHA1 Message Date
drbaph b35b5d8a17 fix: whisper transcription compatibility with newer transformers (#274)
- Use getattr for max_length to handle removed WhisperConfig attribute
- Cast input_features to model dtype to fix float16 mismatch
2026-07-04 21:40:35 +02:00
carlostsai 6d5fd74333 fix: typo on vitmatte torch script name (#276) 2026-06-27 21:15:11 +02:00
Anderson Yan b705a177d3 fix: add retry to LoadImageFromURL 2026-03-19 08:39:38 +01:00
Mel Massadian 00fbad37c5 docs: remove deprecation
Updated caution and note sections regarding recent changes and versioning.
2026-01-10 10:32:45 +01:00
Benjamin Gregg 6cbe294c1b Fix Deepcopy Error
Fix Deepcopy Error in new comfy versions
2026-01-10 10:30:32 +01:00
Mel Massadian eabe43db79 fix: 🐛 add missing widgetTypes for COLOR 2025-09-07 11:54:27 +00:00
Austin Mroz 1c99a1c63c Set widgetType for COLOR widgets 2025-09-06 16:56:35 +02:00
Mel Massadian 426cdf5f9f fix: 🐛 temporary fix for COLOR 2025-09-06 12:38:41 +00:00
Mel Massadian 5fa3791559 📚 docs: add caution about project status 2025-09-06 11:03:42 +02:00
Mel Massadian 5c0e020c73 fix: 🐛 use gpu for uncrop if available
image tensors are often offloaded to cpu which makes
the gaussian blur dead slow
2025-07-18 15:26:58 +02:00
Mel Massadian d00722e9ea fix: 🐛 remove numpy from bbox crop/uncrop 2025-07-17 20:54:44 +02:00
Mel Massadian 0106c13250 fix: 🐛 typo in clock 2025-07-07 21:05:04 +02:00
Mel Massadian 55226058d4 feat: ✨ add a simple clock system
StartClock and EndClock
2025-07-05 18:18:10 +02:00
Mel Massadian 50e0f7b357 wip: 🚧 generic GetItem node
For now pretty bare bones
2025-07-04 12:02:29 +02:00
Mel Massadian 71f601094a feat: ✨ simple not boolean node
requested and contributed by vallestutz
2025-06-28 13:49:54 +02:00
Mel Massadian ea750b5e8b fix: 🐛 use core toast
I made this long before it was a thing in comfy.
It now wraps the builtin toat system unless specificaly requested.

(notify css broke in recent ComfyUI updates anyway)
2025-06-26 17:33:38 +02:00
Mel Massadian ff2e99f73e fix(web): 🐛 allow cancelling queue of animation builder
fixes #246
2025-06-26 17:23:42 +02:00
Mel Massadian efc6855073 chore: 🧹 apply biome on missed files 2025-06-26 15:42:31 +02:00
Mel Massadian 0853b7fb6a chore: 🧹 update biome 2025-06-26 15:42:31 +02:00
Mel Massadian 10aa493dd8 docs(web): 📚 add markdown notice for sidebar settings 2025-06-26 15:42:31 +02:00
Mel Massadian f038d76748 fix(web): 🐛 make main settings appear first 2025-06-26 15:42:31 +02:00
Mel Massadian 940a781f29 feat: ✨ implement ipaq ideas for the I/O sidebar 2025-06-26 15:42:31 +02:00
Jared J c7248344cc Clarify mtb.io-sidebar.img-size name and tooltip 2025-06-26 15:42:31 +02:00
Mel Massadian fab33a40a2 chore: 🧹 add debug after esm load 2025-06-26 12:59:03 +02:00
Mel Massadian 8f83e8d4d7 chore(web): 🧹 remove API stuff
this is being rewritten in typescript
2025-06-26 12:52:44 +02:00
Mel Massadian 6c59d5c32d chore: 🧹 support hot reloading 2025-06-26 12:52:44 +02:00
Mel Massadian e98f3f626f fix: 🐛 add rgb/rgba toggle to stack images
now defaulting to rgb (too many nodes don't properly support rgba)
2025-06-24 17:02:41 +02:00
Mel Massadian 7e89e96e9d feat(web): ✨ use comfy text area fontsize for editors 2025-06-08 19:58:05 +02:00
Mel Massadian 177b6eeef3 fix(web): 🐛 don't break note+ on undo
Issuing undo will both undo the last note edit and the last graph
edit...
I asked upstream about it:
https://github.com/Comfy-Org/ComfyUI_frontend/issues/4108

this also fixes height calculation
2025-06-08 16:52:06 +02:00
Mel Massadian 502a583409 fix(web): 🐛 properly init after ace load 2025-06-07 17:17:05 +02:00
Mel Massadian a7966355c1 fix(web): 🐛 use natural widget/properties de/serialization 2025-06-07 16:39:19 +02:00
Mel Massadian 321abea51a fix(web): 🐛 use the new settings api 2025-06-07 15:00:05 +02:00
Mel Massadian 63be3f26fd fix(web): 🐛 note+
- reworked the internal logic to be simpler and more robust
- removed the dedicated HTML editing mode
  markdown is a superset of HTML in this context
- fixed layout of the css editor
- introduces a quick edit mode: double-clicking the note's preview area
now opens an inline Ace editor
2025-06-07 14:34:39 +02:00
Mel Massadian c4f40e299f fix(web): 🐛 always bind the load event 2025-06-07 12:20:51 +02:00
Mel Massadian b541670a5b fix: 🐛 improve startup times 2025-06-05 16:37:27 +02:00
Mel Massadian 4574c6451c ci: 🤖 disable ci 2025-05-23 02:11:22 +02:00
Mel Massadian 7fb27804e1 chore!: 🧹 bump version 2025-05-23 01:34:56 +02:00
Mel Massadian 9a7e022df1 chore!: 🧹 bump version 2025-05-22 23:00:01 +02:00
Mel Massadian 2c483fd1d2 ci: 🤖 finally fix the registry issue
The upstream action was overwritting the checkout: https://github.com/Comfy-Org/publish-node-action/blob/d2366e7abb6ab16f3bb03e3520ae25c8cf749bc9/action.yml#L16
2025-05-22 22:58:10 +02:00
Mel Massadian 0967d439f5 chore!: 🧹 bump version
closes #230
2025-05-22 22:02:39 +02:00
Mel Massadian 319c02d658 fix: 🐛 ascii encoding only for whisper chunks
fixes #251
2025-05-22 21:52:44 +02:00
Mel Massadian 265cb953ec feat: ✨ rework extract points
Make use of both inputs if provided, more efficient point drawing
2025-05-18 20:56:41 +02:00
34 changed files with 1713 additions and 1185 deletions
+7
View File
@@ -0,0 +1,7 @@
**/GFPGAN/inputs/**
**/GFPGAN/tests/**
**/frame_interpolation/photos/*
moment.gif
node.zip
.DS_Store
+1 -3
View File
@@ -1,9 +1,6 @@
name: 📦 Publish to Comfy registry name: 📦 Publish to Comfy registry
on: on:
workflow_dispatch: workflow_dispatch:
push:
tags:
- '*'
permissions: permissions:
issues: write issues: write
@@ -21,4 +18,5 @@ jobs:
- name: 📦 Publish Custom Node - name: 📦 Publish Custom Node
uses: Comfy-Org/publish-node-action@v1 uses: Comfy-Org/publish-node-action@v1
with: with:
skip_checkout: 'true'
personal_access_token: ${{ secrets.COMFY_REGISTRY_TOKEN }} personal_access_token: ${{ secrets.COMFY_REGISTRY_TOKEN }}
+5
View File
@@ -1,11 +1,16 @@
__pycache__ __pycache__
*.py[cod] *.py[cod]
*.onnx *.onnx
wheels/ wheels/
node_modules/ node_modules/
compose.yaml compose.yaml
comfy_mtb.wsb comfy_mtb.wsb
Dockerfile Dockerfile
.DS_Store
node.zip
# I store the gh-pages worktrees (src & build) there # I store the gh-pages worktrees (src & build) there
.worktrees .worktrees
comfy.lock
+6
View File
@@ -1,4 +1,10 @@
# MTB Nodes # MTB Nodes
> [!NOTE]
> master/main is outdated for now to keep backward compatibility, the next version is being worked on in
> [`dev/0.6.0`](https://github.com/melMass/comfy_mtb/tree/dev/0.6.0)
[![embedded test](https://github.com/melMass/comfy_mtb/actions/workflows/test_embedded.yml/badge.svg)](https://github.com/melMass/comfy_mtb/actions/workflows/test_embedded.yml) [![embedded test](https://github.com/melMass/comfy_mtb/actions/workflows/test_embedded.yml/badge.svg)](https://github.com/melMass/comfy_mtb/actions/workflows/test_embedded.yml)
![home](https://repository-images.githubusercontent.com/649047066/a3eef9a7-20dd-4ef9-b839-884502d4e873) ![home](https://repository-images.githubusercontent.com/649047066/a3eef9a7-20dd-4ef9-b839-884502d4e873)
+29 -11
View File
@@ -3,11 +3,11 @@
# File: __init__.py # File: __init__.py
# Project: comfy_mtb # Project: comfy_mtb
# Author: Mel Massadian # Author: Mel Massadian
# Copyright (c) 2023 Mel Massadian # Copyright (c) 2023-2025 Mel Massadian
# #
### ###
__version__ = "0.3.0" __version__ = "0.5.4"
import os import os
@@ -34,6 +34,8 @@ from aiohttp import web
IN_COMFY = False IN_COMFY = False
PromptServer = None
try: try:
from server import PromptServer from server import PromptServer
@@ -75,7 +77,7 @@ def extract_nodes_from_source(filename: Path):
) )
break break
except SyntaxError: except SyntaxError:
log.error("Failed to parse") log.error(f"Failed to parse ast from: {filename}")
return nodes return nodes
@@ -240,10 +242,29 @@ if failed:
# - ENDPOINT # - ENDPOINT
if IN_COMFY and hasattr(PromptServer, "instance"): # TODO: move that away and simplify existing endpoints
def register_routes():
if not PromptServer:
log.error("No prompt server, are you inside comfy?")
if PromptServer.instance.app.frozen:
log.warning(
"The router is frozen and cannot be further edited."
"If you are hot reloading mtb this is expected."
)
return
img_cache = None img_cache = None
prompt_cache = None prompt_cache = None
import asyncio
import os
from io import BytesIO
from PIL import Image
with contextlib.suppress(ImportError): with contextlib.suppress(ImportError):
from cachetools import TTLCache from cachetools import TTLCache
@@ -360,13 +381,6 @@ if IN_COMFY and hasattr(PromptServer, "instance"):
# Return JSON for other requests # Return JSON for other requests
return web.json_response({"message": "Welcome to MTB!"}) return web.json_response({"message": "Welcome to MTB!"})
import asyncio
import os
from io import BytesIO
from aiohttp import web
from PIL import Image
def get_cached_image(file_path: str, preview_params=None, channel=None): def get_cached_image(file_path: str, preview_params=None, channel=None):
cache_key = (file_path, preview_params, channel) cache_key = (file_path, preview_params, channel)
if img_cache and (cache_key in img_cache): if img_cache and (cache_key in img_cache):
@@ -571,6 +585,10 @@ if IN_COMFY and hasattr(PromptServer, "instance"):
return await endpoint.do_action(request) return await endpoint.do_action(request)
if IN_COMFY and hasattr(PromptServer, "instance"):
register_routes()
# - WAS Dictionary # - WAS Dictionary
MANIFEST = { MANIFEST = {
"name": "MTB Nodes", # The title that will be displayed on Node Class menu,. and Node Class view "name": "MTB Nodes", # The title that will be displayed on Node Class menu,. and Node Class view
+13 -6
View File
@@ -1,19 +1,26 @@
{ {
"$schema": "https://biomejs.dev/schemas/1.6.1/schema.json", "$schema": "https://biomejs.dev/schemas/2.0.5/schema.json",
"organizeImports": { "assist": { "actions": { "source": { "organizeImports": "on" } } },
"enabled": true
},
"linter": { "linter": {
"enabled": true, "enabled": true,
"rules": { "rules": {
"recommended": true, "recommended": true,
"suspicious": { "suspicious": {
"noConsoleLog": "warn" "noConsole": { "level": "warn", "options": { "allow": ["log"] } }
}, },
"style": { "style": {
"noParameterAssign": "off", "noParameterAssign": "off",
"noShoutyConstants": "warn", "noShoutyConstants": "warn",
"useNamingConvention": "off" "useNamingConvention": "off",
"useAsConstAssertion": "error",
"useDefaultParameterLast": "error",
"useEnumInitializers": "error",
"useSelfClosingElements": "error",
"useSingleVarDeclarator": "error",
"noUnusedTemplateLiteral": "error",
"useNumberNamespace": "error",
"noInferrableTypes": "error",
"noUselessElse": "error"
} }
} }
}, },
+9 -6
View File
@@ -15,7 +15,6 @@ from .utils import (
backup_file, backup_file,
build_glob_patterns, build_glob_patterns,
glob_multiple, glob_multiple,
import_install,
reqs_map, reqs_map,
run_command, run_command,
styles_dir, styles_dir,
@@ -24,7 +23,6 @@ from .utils import (
endlog = mklog("mtb endpoint") endlog = mklog("mtb endpoint")
# - ACTIONS # - ACTIONS
import_install("requirements")
def ACTIONS_installDependency(dependency_names: list[str] | None = None): def ACTIONS_installDependency(dependency_names: list[str] | None = None):
@@ -112,11 +110,15 @@ def ACTIONS_getUserVideos(
def ACTIONS_getUserImages( def ACTIONS_getUserImages(
mode: Literal["input", "output"], mode: Literal["input", "output"],
target_width: int | str | None = None,
count=1000, count=1000,
offset=0, offset=0,
sort: str | None = None, sort: str | None = None,
include_subfolders: bool = False, include_subfolders: bool = False,
subfolder=None, subfolder: str | None = None,
# IIRC I copied this from Comfy base
# just keeping it until I properly checked implications
salt_urls=False,
): ):
# enabled = "MTB_EXPOSE" in os.environ # enabled = "MTB_EXPOSE" in os.environ
# if not enabled: # if not enabled:
@@ -124,11 +126,12 @@ def ACTIONS_getUserImages(
imgs = {} imgs = {}
count = count or 1000 count = count or 1000
target_width = int(target_width) if target_width else None
input_dir = Path(folder_paths.get_input_directory()) input_dir = Path(folder_paths.get_input_directory())
output_dir = Path(folder_paths.get_output_directory()) output_dir = Path(folder_paths.get_output_directory())
entry_dir = input_dir if mode == "input" else output_dir entry_dir: Path = input_dir if mode == "input" else output_dir
if subfolder: if subfolder:
entry_dir = entry_dir / subfolder entry_dir = entry_dir / subfolder
@@ -157,9 +160,9 @@ def ACTIONS_getUserImages(
imgs = { imgs = {
img.name: ( img.name: (
f"/mtb/view?filename={img.name}&width=512&type={mode}&subfolder={subfolder or ''}" f"/mtb/view?filename={img.name}{f'&width={target_width}' if target_width and target_width > 0 else ''}&type={mode}&subfolder={subfolder or ''}"
f"{img.parent.relative_to(entry_dir) if include_subfolders else ''}" f"{img.parent.relative_to(entry_dir) if include_subfolders else ''}"
f"&preview=&rand={secrets.randbelow(424242)}" f"&preview={f'&rand={secrets.randbelow(424242)}' if salt_urls else ''}"
) )
for i, img in enumerate(entries) for i, img in enumerate(entries)
if offset <= i < offset + count if offset <= i < offset + count
-1
View File
@@ -43,7 +43,6 @@ pip_map = {
"tb-nightly": "tensorboard", "tb-nightly": "tensorboard",
"protobuf": "google.protobuf", "protobuf": "google.protobuf",
"qrcode[pil]": "qrcode", "qrcode[pil]": "qrcode",
"requirements-parser": "requirements",
# Add more mappings as needed # Add more mappings as needed
} }
+16 -15
View File
@@ -1,20 +1,16 @@
from typing import Any, TypedDict from typing import TYPE_CHECKING, Any, TypedDict
import torch import torch
import torchaudio import torchaudio
from comfy.model_management import get_torch_device from comfy.model_management import get_torch_device
from huggingface_hub import snapshot_download from huggingface_hub import snapshot_download
from transformers import (
WhisperForConditionalGeneration,
WhisperProcessor,
)
# from transformers import ( if TYPE_CHECKING:
# AutoFeatureExtractor, from transformers import (
# WhisperForConditionalGeneration, WhisperForConditionalGeneration,
# WhisperModel, WhisperProcessor,
# WhisperProcessor, )
# )
from ..log import log from ..log import log
from ..utils import get_model_path from ..utils import get_model_path
@@ -101,8 +97,8 @@ class MtbAudio:
class WhisperPipeline(TypedDict): class WhisperPipeline(TypedDict):
"""Whisper model pipeline.""" """Whisper model pipeline."""
processor: WhisperProcessor processor: "WhisperProcessor"
model: WhisperForConditionalGeneration model: "WhisperForConditionalGeneration"
class MTB_LoadWhisper: class MTB_LoadWhisper:
@@ -148,6 +144,11 @@ class MTB_LoadWhisper:
def load(self, model_size="tiny", download_missing=False): def load(self, model_size="tiny", download_missing=False):
"""Load Whisper model and processor.""" """Load Whisper model and processor."""
from transformers import (
WhisperForConditionalGeneration,
WhisperProcessor,
)
whisper_dir = get_model_path("whisper") whisper_dir = get_model_path("whisper")
tag = f"whisper-{model_size}" tag = f"whisper-{model_size}"
model_dir = whisper_dir / tag model_dir = whisper_dir / tag
@@ -276,14 +277,14 @@ class MTB_AudioToText(MtbAudio):
f"Processing chunk {chunk_offset:.1f}s - {chunk_end / sample_rate:.1f}s" f"Processing chunk {chunk_offset:.1f}s - {chunk_end / sample_rate:.1f}s"
) )
max_length = model.config.max_length or 448 max_length = getattr(model.config, "max_length", None) or 448
attention_mask = torch.ones((1, max_length)) attention_mask = torch.ones((1, max_length))
input_features = processor( input_features = processor(
chunk_waveform, chunk_waveform,
sampling_rate=sample_rate, sampling_rate=sample_rate,
return_tensors="pt", return_tensors="pt",
).input_features.to(device) ).input_features.to(device=device, dtype=model.dtype)
with torch.no_grad(): with torch.no_grad():
predicted_ids = model.generate( predicted_ids = model.generate(
+4 -4
View File
@@ -335,9 +335,9 @@ class MTB_BatchShape:
"image_width": ("INT", {"default": 512}), "image_width": ("INT", {"default": 512}),
"image_height": ("INT", {"default": 512}), "image_height": ("INT", {"default": 512}),
"shape_size": ("INT", {"default": 100}), "shape_size": ("INT", {"default": 100}),
"color": ("COLOR", {"default": "#ffffff"}), "color": ("COLOR", {"default": "#ffffff","widgetType": "MTB_COLOR"}),
"bg_color": ("COLOR", {"default": "#000000"}), "bg_color": ("COLOR", {"default": "#000000","widgetType": "MTB_COLOR"}),
"shade_color": ("COLOR", {"default": "#000000"}), "shade_color": ("COLOR", {"default": "#000000","widgetType": "MTB_COLOR"}),
"thickness": ("INT", {"default": 5}), "thickness": ("INT", {"default": 5}),
"shadex": ("FLOAT", {"default": 0.0}), "shadex": ("FLOAT", {"default": 0.0}),
"shadey": ("FLOAT", {"default": 0.0}), "shadey": ("FLOAT", {"default": 0.0}),
@@ -842,7 +842,7 @@ class MTB_Batch2dTransform:
["edge", "constant", "reflect", "symmetric"], ["edge", "constant", "reflect", "symmetric"],
{"default": "edge"}, {"default": "edge"},
), ),
"constant_color": ("COLOR", {"default": "#000000"}), "constant_color": ("COLOR", {"default": "#000000","widgetType": "MTB_COLOR"}),
}, },
"optional": { "optional": {
"x": ("FLOATS",), "x": ("FLOATS",),
+190
View File
@@ -0,0 +1,190 @@
import time
import uuid
from collections import OrderedDict
from typing import Any, TypedDict
from comfy.comfy_types.node_typing import IO as CIO
from server import PromptServer
from ..log import log
class Clock(TypedDict):
name: str
start: float
end: float | None
active_timers: OrderedDict[str, Clock] = OrderedDict()
# TODO: lower this
MAX_CLOCKS = 50
class MTB_StartClock:
"""
Starts a profiling clock with a given name.
Outputs a unique ID that must be passed to EndClock.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"name": ("STRING", {"default": "Clock A"}),
"cache": (
"BOOLEAN",
{
"default": False,
"tooltip": "Cache the clock ID, this means the node will follow Comfy's default invalidation system. If False it will always invalidate / mark the node as 'dirty'",
},
),
},
"optional": {
"passthrough": (CIO.ANY,),
},
}
RETURN_TYPES = (
CIO.ANY,
"STRING",
)
RETURN_NAMES = (
"passthrough",
"clock_id",
)
FUNCTION = "start_timer"
CATEGORY = "mtb/utils"
def start_timer(
self, *, name: str, passthrough: Any | None = None, **kwargs
):
global active_timers
if len(active_timers) >= MAX_CLOCKS:
# get oldest clock
removed_key = None
for key, clock_data in active_timers.items():
if clock_data["end"] is not None:
removed_key = key
break
if removed_key:
removed_clock = active_timers.pop(removed_key)
log.info(
f"[Profiling] Evicted finished clock '{removed_clock['name']}' (ID: {removed_key}) due to limit ({MAX_CLOCKS})."
)
else:
removed_key, removed_clock = active_timers.popitem(last=False)
log.warning(
f"[Profiling] Evicted running clock '{removed_clock['name']}' (ID: {removed_key}) due to limit ({MAX_CLOCKS})."
)
clock_id = str(uuid.uuid4())
start_time = time.perf_counter()
active_timers[clock_id] = {
"start": start_time,
"name": name,
"end": None,
}
active_timers.move_to_end(clock_id)
log.debug(f"[Profiling] Clock '{name}' (ID: {clock_id}) started.")
return (
passthrough,
clock_id,
)
@classmethod
def IS_CHANGED(
cls, *, name: str, cache: bool = False, passthrough: Any | None = None
):
if not cache:
return float("Nan")
return {"name": name, "cache": cache, "passthrough": passthrough}
class MTB_EndClock:
"""
Stops a profiling clock identified by its ID and returns the elapsed time in milliseconds.
Errors if the clock ID is not found or already stopped.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"clock_id": (
"STRING",
{"forceInput": True},
),
},
"optional": {
"passthrough": (CIO.ANY,),
},
"hidden": {
"unique_id": "UNIQUE_ID",
},
}
RETURN_TYPES = (
CIO.ANY,
"STRING",
"FLOAT",
"INT",
)
RETURN_NAMES = (
"passthrough",
"name",
"seconds",
"milliseconds",
)
FUNCTION = "end_timer"
CATEGORY = "mtb/utils"
def end_timer(self, clock_id: str, passthrough, unique_id=None):
global active_timers
if clock_id not in active_timers:
raise ValueError(
f"Error: Clock with ID '{clock_id}' not found. "
"Ensure StartClock was executed for this ID and proper passthrough chaining."
)
clock = active_timers[clock_id]
if clock.get("end") is not None:
return (passthrough, clock["name"], clock["end"])
start_time = clock["start"]
end_time = time.perf_counter()
duration_seconds = end_time - start_time
duration_ms = int(duration_seconds * 1000)
clock["end"] = duration_ms
active_timers.move_to_end(clock_id)
log.debug(
f"[Profiling] Clock '{clock['name']}' (ID: {clock_id}) stopped. Elapsed: {duration_ms}ms"
)
if unique_id:
PromptServer.instance.send_progress_text(
f"Clock '{clock['name']}' took {duration_seconds:.4f} seconds",
unique_id,
)
return (passthrough, clock["name"], duration_seconds, duration_ms)
__nodes__ = [MTB_StartClock, MTB_EndClock]
+194 -181
View File
@@ -1,13 +1,22 @@
import numpy as np from typing import NamedTuple
import torch import torch
from PIL import Image, ImageDraw, ImageFilter import torchvision.transforms.functional as TF
from ..log import log from ..log import log
from ..utils import np2tensor, pil2tensor, tensor2np, tensor2pil
class BoundingBox(NamedTuple):
"""The bounding box tuple."""
x: int
y: int
width: int
height: int
class MTB_Bbox: class MTB_Bbox:
"""The bounding box (BBOX) custom type used by other nodes""" """A literal bounding box."""
@classmethod @classmethod
def INPUT_TYPES(cls): def INPUT_TYPES(cls):
@@ -37,12 +46,14 @@ class MTB_Bbox:
FUNCTION = "do_crop" FUNCTION = "do_crop"
CATEGORY = "mtb/crop" CATEGORY = "mtb/crop"
def do_crop(self, x: int, y: int, width: int, height: int): # bbox def do_crop(
return ((x, y, width, height),) self, x: int, y: int, width: int, height: int
) -> tuple[BoundingBox]: # bbox
return (BoundingBox(x, y, width, height),)
class MTB_SplitBbox: class MTB_SplitBbox:
"""Split the components of a bbox""" """Split the components of a bbox."""
@classmethod @classmethod
def INPUT_TYPES(cls): def INPUT_TYPES(cls):
@@ -55,8 +66,8 @@ class MTB_SplitBbox:
RETURN_TYPES = ("INT", "INT", "INT", "INT") RETURN_TYPES = ("INT", "INT", "INT", "INT")
RETURN_NAMES = ("x", "y", "width", "height") RETURN_NAMES = ("x", "y", "width", "height")
def split_bbox(self, bbox): def split_bbox(self, bbox: BoundingBox) -> BoundingBox:
return (bbox[0], bbox[1], bbox[2], bbox[3]) return bbox
class MTB_UpscaleBboxBy: class MTB_UpscaleBboxBy:
@@ -74,26 +85,23 @@ class MTB_UpscaleBboxBy:
FUNCTION = "upscale" FUNCTION = "upscale"
def upscale( def upscale(self, bbox: BoundingBox, scale: float) -> tuple[BoundingBox]:
self, bbox: tuple[int, int, int, int], scale: float
) -> tuple[tuple[int, int, int, int]]:
x, y, width, height = bbox x, y, width, height = bbox
center_x = x + width // 2 center_x = x + width / 2
center_y = y + height // 2 center_y = y + height / 2
new_width = int(width * scale) new_width = int(width * scale)
new_height = int(height * scale) new_height = int(height * scale)
new_x = center_x - new_width // 2 new_x = int(center_x - new_width / 2)
new_y = center_y - new_height // 2 new_y = int(center_y - new_height / 2)
scaled = (new_x, new_y, new_width, new_height) return (BoundingBox(new_x, new_y, new_width, new_height),)
return (scaled,)
class MTB_BboxFromMask: class MTB_BboxFromMask:
"""From a mask extract the bounding box""" """From a mask extract the bounding box."""
@classmethod @classmethod
def INPUT_TYPES(cls): def INPUT_TYPES(cls):
@@ -103,7 +111,7 @@ class MTB_BboxFromMask:
"invert": ("BOOLEAN", {"default": False}), "invert": ("BOOLEAN", {"default": False}),
}, },
"optional": { "optional": {
"image": ("IMAGE",), "image": ("IMAGE", {"tooltip": "Optional image"}),
}, },
} }
@@ -119,52 +127,44 @@ class MTB_BboxFromMask:
CATEGORY = "mtb/crop" CATEGORY = "mtb/crop"
def extract_bounding_box( def extract_bounding_box(
self, mask: torch.Tensor, invert: bool, image=None self,
): mask: torch.Tensor,
# if image != None: *,
# if mask.size(0) != image.size(0): invert: bool = False,
# if mask.size(0) != 1: image: torch.Tensor | None = None,
# log.error( ) -> tuple[BoundingBox, torch.Tensor | None]:
# f"Batch count mismatch for mask and image, it can either be 1 mask for X images, or X masks for X images (mask: {mask.shape} | image: {image.shape})" mask = 1 - mask if invert else mask
# ) non_zero_indices = torch.nonzero(mask)
# raise Exception( if non_zero_indices.numel() == 0:
# f"Batch count mismatch for mask and image, it can either be 1 mask for X images, or X masks for X images (mask: {mask.shape} | image: {image.shape})" log.warning(
# ) "BboxFromMask: Mask is empty. Returning a (0,0,0,0) bbox."
)
return (BoundingBox(0, 0, 0, 0), image)
# we invert it min_coords = torch.min(non_zero_indices, dim=0).values
_mask = tensor2pil(1.0 - mask)[0] if invert else tensor2pil(mask)[0] max_coords = torch.max(non_zero_indices, dim=0).values
alpha_channel = np.array(_mask)
non_zero_indices = np.nonzero(alpha_channel) min_y, min_x = min_coords[1].item(), min_coords[2].item()
max_y, max_x = max_coords[1].item(), max_coords[2].item()
min_x, max_x = np.min(non_zero_indices[1]), np.max(non_zero_indices[1]) width = max_x - min_x + 1
min_y, max_y = np.min(non_zero_indices[0]), np.max(non_zero_indices[0]) height = max_y - min_y + 1
# Create a bounding box tuple bounding_box = BoundingBox(
if image != None: int(min_x), int(min_y), int(width), int(height)
# Convert the image to a NumPy array
imgs = tensor2np(image)
out = []
for img in imgs:
# Crop the image from the bounding box
img = img[min_y:max_y, min_x:max_x, :]
log.debug(f"Cropped image to shape {img.shape}")
out.append(img)
image = np2tensor(out)
log.debug(f"Cropped images shape: {image.shape}")
bounding_box = (min_x, min_y, max_x - min_x, max_y - min_y)
return (
bounding_box,
image,
) )
cropped_image = None
if image is not None:
cropped_image = image[:, min_y : max_y + 1, min_x : max_x + 1, :]
return (bounding_box, cropped_image)
class MTB_Crop: class MTB_Crop:
"""Crops an image and an optional mask to a given bounding box """Crop an image and an optional mask to a given bounding box.
The bounding box can be given as a tuple of (x, y, width, height) or as a BBOX type
The BBOX input takes precedence over the tuple input The BBOX input takes precedence over the tuple input
""" """
@@ -204,35 +204,38 @@ class MTB_Crop:
def do_crop( def do_crop(
self, self,
image: torch.Tensor, image: torch.Tensor,
mask=None, *,
x=0, mask: torch.Tensor | None = None,
y=0, x: int = 0,
width=256, y: int = 0,
height=256, width: int = 256,
bbox=None, height: int = 256,
bbox: BoundingBox | None = None,
): ):
image = image.numpy()
if mask is not None:
mask = mask.numpy()
if bbox is not None: if bbox is not None:
x, y, width, height = bbox x, y, width, height = bbox
cropped_image = image[:, y : y + height, x : x + width, :] if width <= 0 or height <= 0:
cropped_mask = None log.error(
if mask is not None: "Crop dimensions must be positive. Check the BBOX or widget inputs."
cropped_mask = (
mask[:, y : y + height, x : x + width]
if mask is not None
else None
) )
crop_data = (x, y, width, height) return (
torch.zeros_like(image),
torch.zeros_like(mask) if mask is not None else None,
(x, y, width, height),
)
cropped_image = image[:, y : y + height, x : x + width, :]
cropped_mask = (
mask[:, y : y + height, x : x + width]
if mask is not None
else None
)
crop_data = BoundingBox(x, y, width, height)
return ( return (
torch.from_numpy(cropped_image), cropped_image,
torch.from_numpy(cropped_mask) cropped_mask if cropped_mask is not None else None,
if cropped_mask is not None
else None,
crop_data, crop_data,
) )
@@ -246,35 +249,33 @@ class MTB_Crop:
# return (x_left, y_top, x_right, y_bottom) # return (x_left, y_top, x_right, y_bottom)
def bbox_check(bbox, target_size=None): def bbox_check(bbox: BoundingBox, target_size: tuple[int, int] | None = None):
if not target_size: if not target_size:
return bbox return bbox
new_bbox = ( new_bbox = BoundingBox(
bbox[0], bbox.x,
bbox[1], bbox.y,
min(target_size[0] - bbox[0], bbox[2]), min(target_size[0] - bbox.x, bbox.width),
min(target_size[1] - bbox[1], bbox[3]), min(target_size[1] - bbox.y, bbox.height),
) )
if new_bbox != bbox: if new_bbox != bbox:
log.warn(f"BBox too big, constrained to {new_bbox}") log.warning(f"BBox too big, constrained to {new_bbox}")
return new_bbox return new_bbox
def bbox_to_region(bbox, target_size=None): def bbox_to_region(
bbox: BoundingBox, target_size: tuple[int, int] | None = None
):
bbox = bbox_check(bbox, target_size) bbox = bbox_check(bbox, target_size)
# to region # to region
return (bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]) return (bbox.x, bbox.y, bbox.x + bbox.width, bbox.y + bbox.height)
class MTB_Uncrop: class MTB_Uncrop:
"""Uncrops an image to a given bounding box """Uncrop an image to a given bounding box."""
The bounding box can be given as a tuple of (x, y, width, height) or as a BBOX type
The BBOX input takes precedence over the tuple input
"""
@classmethod @classmethod
def INPUT_TYPES(cls): def INPUT_TYPES(cls):
@@ -291,91 +292,113 @@ class MTB_Uncrop:
} }
RETURN_TYPES = ("IMAGE",) RETURN_TYPES = ("IMAGE",)
FUNCTION = "do_crop" FUNCTION = "do_uncrop"
CATEGORY = "mtb/crop" CATEGORY = "mtb/crop"
def do_crop(self, image, crop_image, bbox, border_blending): def do_uncrop(
def inset_border(image, border_width=20, border_color=(0)): self,
width, height = image.size image: torch.Tensor,
bordered_image = Image.new( crop_image: torch.Tensor,
image.mode, (width, height), border_color bbox: BoundingBox,
border_blending: float = 0.25,
):
if len(image) > 1 and len(image) != len(crop_image):
raise ValueError(
"Uncrop: Batch size of background 'image' must be 1 or match the 'crop_image' batch size."
) )
bordered_image.paste(image, (0, 0)) import comfy.utils
draw = ImageDraw.Draw(bordered_image)
draw.rectangle(
(0, 0, width - 1, height - 1),
outline=border_color,
width=border_width,
)
return bordered_image
single = image.size(0) == 1 pbar = comfy.utils.ProgressBar(4)
if image.size(0) != crop_image.size(0):
if not single:
raise ValueError(
"The Image batch count is greater than 1, but doesn't match the crop_image batch count. If using batches they should either match or only crop_image must be greater than 1"
)
images = tensor2pil(image) device = image.device
crop_imgs = tensor2pil(crop_image)
out_images = []
for i, crop in enumerate(crop_imgs):
if single:
img = images[0]
else:
img = images[i]
# uncrop the image based on the bounding box log.debug(f"Working on device: {device}")
bb_x, bb_y, bb_width, bb_height = bbox
paste_region = bbox_to_region( crop_image = crop_image.to(device)
(bb_x, bb_y, bb_width, bb_height), img.size
)
# log.debug(f"Paste region: {paste_region}")
# new_region = adjust_paste_region(img.size, paste_region)
# log.debug(f"Adjusted paste region: {new_region}")
# # Check if the adjusted paste region is different from the original
crop_img = crop.convert("RGB") if len(image) == 1 and len(crop_image) > 1:
image = image.repeat(len(crop_image), 1, 1, 1)
log.debug(f"Crop image size: {crop_img.size}") batch_size, bg_h, bg_w, _ = image.shape
log.debug(f"Image size: {img.size}") _, fg_h, fg_w, _ = crop_image.shape
x, y, width, height = bbox
if border_blending > 1.0: if (width, height) != (fg_w, fg_h):
border_blending = 1.0 log.warning(
elif border_blending < 0.0: f"Uncrop: crop_image size {(fg_w, fg_h)} "
border_blending = 0.0 "differs from bbox {(width, height)}. Resizing to fit bbox."
blend_ratio = (max(crop_img.size) / 2) * float(border_blending)
blend = img.convert("RGBA")
mask = Image.new("L", img.size, 0)
mask_block = Image.new("L", (bb_width, bb_height), 255)
mask_block = inset_border(mask_block, int(blend_ratio / 2), (0))
mask.paste(mask_block, paste_region)
log.debug(f"Blend size: {blend.size} | kind {blend.mode}")
log.debug(
f"Crop image size: {crop_img.size} | kind {crop_img.mode}"
)
log.debug(f"BBox: {paste_region}")
blend.paste(crop_img, paste_region)
mask = mask.filter(ImageFilter.BoxBlur(radius=blend_ratio / 4))
mask = mask.filter(
ImageFilter.GaussianBlur(radius=blend_ratio / 4)
) )
blend.putalpha(mask) resized_crop = crop_image.permute(0, 3, 1, 2)
img = Image.alpha_composite(img.convert("RGBA"), blend) resized_crop = torch.nn.functional.interpolate(
out_images.append(img.convert("RGB")) resized_crop,
size=(height, width),
mode="bicubic",
align_corners=False,
)
resized_crop = resized_crop.permute(0, 2, 3, 1)
return (pil2tensor(out_images),) pbar.update(1)
# paste coords
paste_x1 = max(x, 0)
paste_y1 = max(y, 0)
paste_x2 = min(x + width, bg_w)
paste_y2 = min(y + height, bg_h)
# region from crop (bound)
crop_x1 = max(0, -x)
crop_y1 = max(0, -y)
crop_x2 = crop_x1 + (paste_x2 - paste_x1)
crop_y2 = crop_y1 + (paste_y2 - paste_y1)
if paste_x1 >= paste_x2 or paste_y1 >= paste_y2:
log.warning(
"Uncrop: BBOX is entirely outside the image boundaries. Returning original image."
)
return (image,)
pbar.update(1)
source_slice = resized_crop[:, crop_y1:crop_y2, crop_x1:crop_x2, :]
final_image = image.clone()
final_image[:, paste_y1:paste_y2, paste_x1:paste_x2, :] = source_slice
pbar.update(1)
blend_radius = int(max(width, height) * border_blending * 0.5)
if blend_radius > 0:
_device = device
if torch.cuda.is_available():
_device = torch.device("cuda")
log.debug("Processing blending")
alpha_mask = torch.zeros((batch_size, bg_h, bg_w), device=_device)
alpha_mask[:, paste_y1:paste_y2, paste_x1:paste_x2] = 1.0
kernel_size = 2 * blend_radius + 1
log.debug("Gaussian blur...")
alpha_mask = TF.gaussian_blur(
alpha_mask.unsqueeze(1), kernel_size=[kernel_size, kernel_size]
).squeeze(1)
alpha_mask = alpha_mask.unsqueeze(-1)
log.debug("Applying blending")
final_image = final_image.to(_device) * alpha_mask + image.to(
_device
) * (1.0 - alpha_mask)
pbar.update(1)
return (final_image.to(device),)
class MTB_BBoxForceDimensions: class MTB_BBoxForceDimensions:
"""
Resize a BBOX to new dimensions while keeping its center.
Optionally constrains the BBOX to stay within image boundaries.
"""
@classmethod @classmethod
def INPUT_TYPES(cls): def INPUT_TYPES(cls):
return { return {
@@ -383,6 +406,7 @@ class MTB_BBoxForceDimensions:
"bbox": ("BBOX",), "bbox": ("BBOX",),
"width": ("INT", {"default": 512, "min": 1, "max": 8192}), "width": ("INT", {"default": 512, "min": 1, "max": 8192}),
"height": ("INT", {"default": 512, "min": 1, "max": 8192}), "height": ("INT", {"default": 512, "min": 1, "max": 8192}),
"constrain_to_image": ("BOOLEAN", {"default": True}),
}, },
"optional": { "optional": {
"image": ("IMAGE",), "image": ("IMAGE",),
@@ -395,10 +419,12 @@ class MTB_BBoxForceDimensions:
def force_dimensions( def force_dimensions(
self, self,
*,
bbox: tuple[int, int, int, int], bbox: tuple[int, int, int, int],
width: int, width: int,
height: int, height: int,
image: torch.Tensor = None, constrain_to_image: bool = True,
image: torch.Tensor | None = None,
) -> tuple[tuple[int, int, int, int]]: ) -> tuple[tuple[int, int, int, int]]:
x, y, curr_width, curr_height = bbox x, y, curr_width, curr_height = bbox
@@ -408,27 +434,14 @@ class MTB_BBoxForceDimensions:
new_x = center_x - width // 2 new_x = center_x - width // 2
new_y = center_y - height // 2 new_y = center_y - height // 2
if image is not None: if constrain_to_image and image is not None:
img_height, img_width = image.shape[1:3] img_height, img_width = image.shape[1:3]
x_overflow = max(0, new_x + width - img_width) + min(0, new_x) new_x = max(0, min(new_x, img_width - width))
y_overflow = max(0, new_y + height - img_height) + min(0, new_y) new_y = max(0, min(new_y, img_height - height))
if width > img_width or height > img_height: width = min(width, img_width)
x_exceed = width - img_width if width > img_width else 0 height = min(height, img_height)
y_exceed = height - img_height if height > img_height else 0
raise ValueError(
f"Target bbox dimensions ({width}x{height}) exceed image bounds ({img_width}x{img_height}) "
f"by {x_exceed}px horizontally and {y_exceed}px vertically"
)
if x_overflow > 0 or x_overflow < 0: return ((new_x, new_y, width, height),)
new_x -= x_overflow
if y_overflow > 0:
new_y -= y_overflow
elif y_overflow < 0:
new_y -= y_overflow # Add the negative overflow
return ((int(new_x), int(new_y), width, height),)
__nodes__ = [ __nodes__ = [
+7 -4
View File
@@ -4,12 +4,8 @@ import sys
from pathlib import Path from pathlib import Path
import comfy.model_management as model_management import comfy.model_management as model_management
import cv2
import insightface
import numpy as np import numpy as np
import onnxruntime
import torch import torch
from insightface.model_zoo.inswapper import INSwapper
from PIL import Image from PIL import Image
from ..errors import ModelNotFound from ..errors import ModelNotFound
@@ -43,6 +39,8 @@ class MTB_LoadFaceAnalysisModel:
DEPRECATED = True DEPRECATED = True
def load_model(self, faceswap_model: str): def load_model(self, faceswap_model: str):
import insightface
if faceswap_model == "antelopev2": if faceswap_model == "antelopev2":
download_antelopev2() download_antelopev2()
@@ -81,6 +79,9 @@ class MTB_LoadFaceSwapModel:
DEPRECATED = True DEPRECATED = True
def load_model(self, faceswap_model: str): def load_model(self, faceswap_model: str):
import onnxruntime
from insightface.model_zoo.inswapper import INSwapper
model_path = get_model_path("insightface", faceswap_model) model_path = get_model_path("insightface", faceswap_model)
if not model_path or not model_path.exists(): if not model_path or not model_path.exists():
raise ModelNotFound(f"{faceswap_model} ({model_path})") raise ModelNotFound(f"{faceswap_model} ({model_path})")
@@ -212,6 +213,8 @@ def swap_face(
face_swapper_model, face_swapper_model,
faces_index: set[int] | None = None, faces_index: set[int] | None = None,
) -> Image.Image: ) -> Image.Image:
import cv2
if faces_index is None: if faces_index is None:
faces_index = {0} faces_index = {0}
log.debug(f"Swapping faces: {faces_index}") log.debug(f"Swapping faces: {faces_index}")
+6 -6
View File
@@ -193,11 +193,11 @@ by default it fallsback to a default font.
), ),
"color": ( "color": (
"COLOR", "COLOR",
{"default": "black"}, {"default": "black", "widgetType": "MTB_COLOR"},
), ),
"background": ( "background": (
"COLOR", "COLOR",
{"default": "white"}, {"default": "white", "widgetType": "MTB_COLOR"},
), ),
"h_align": (("left", "center", "right"), {"default": "left"}), "h_align": (("left", "center", "right"), {"default": "left"}),
"v_align": (("top", "center", "bottom"), {"default": "top"}), "v_align": (("top", "center", "bottom"), {"default": "top"}),
@@ -343,9 +343,7 @@ by default it fallsback to a default font.
def render_text(text_to_render, alpha=None): def render_text(text_to_render, alpha=None):
if trim: if trim:
text_to_render = ( text_to_render = text_to_render.strip()
text_to_render.encode("ascii", "ignore").decode().strip()
)
if wrap: if wrap:
wrap_width = (((width / 100) * h_coverage) / font_size) * 2 wrap_width = (((width / 100) * h_coverage) / font_size) * 2
lines = textwrap.wrap(text_to_render, width=wrap_width) lines = textwrap.wrap(text_to_render, width=wrap_width)
@@ -418,7 +416,9 @@ by default it fallsback to a default font.
active_chunks.append((chunk["text"], alpha)) active_chunks.append((chunk["text"], alpha))
for chunk_text, alpha in active_chunks: for chunk_text, alpha in active_chunks:
chunk_img = render_text(chunk_text, alpha) chunk_img = render_text(
chunk_text.encode("ascii", "ignore").decode(), alpha
)
frame = Image.alpha_composite(frame, chunk_img) frame = Image.alpha_composite(frame, chunk_img)
frames.append(frame) frames.append(frame)
+51
View File
@@ -4,11 +4,13 @@ import re
import urllib.parse import urllib.parse
import urllib.request import urllib.request
from math import pi from math import pi
from typing import Any
import comfy.model_management as model_management import comfy.model_management as model_management
import comfy.utils import comfy.utils
import numpy as np import numpy as np
import torch import torch
from comfy.comfy_types.node_typing import IO as CIO
from PIL import Image from PIL import Image
from ..log import log from ..log import log
@@ -867,6 +869,53 @@ class MTB_TensorOps:
return (result,) return (result,)
class MTB_GetItem:
"""Generic index based getter for common types"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"container": (CIO.ANY,),
"index": ("INT", {"default": 0}),
}
}
RETURN_TYPES = (CIO.ANY,)
RETURN_NAMES = ("item",)
FUNCTION = "get_item"
CATEGORY = "mtb/utils"
def get_item(self, container: Any, index: int):
if "__getitem__" in dir(container):
log.debug(f"Container is {type(container)}")
res = container[index]
if type(res) is torch.Tensor:
res = res.unsqueeze(0)
return (res,)
class MTB_BooleanNot:
"""Inverts a boolean."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"bool_in": ("BOOLEAN", {"default": False}),
},
}
RETURN_TYPES = ("BOOLEAN",)
RETURN_NAMES = ("inverted_bool",)
FUNCTION = "invert"
CATEGORY = "mtb/utils"
def invert(self, bool_in: bool):
return (not bool_in,)
__nodes__ = [ __nodes__ = [
MTB_StringReplace, MTB_StringReplace,
MTB_FitNumber, MTB_FitNumber,
@@ -882,4 +931,6 @@ __nodes__ = [
MTB_FloatToFloats, MTB_FloatToFloats,
MTB_FloatsToInts, MTB_FloatsToInts,
MTB_TensorOps, MTB_TensorOps,
MTB_BooleanNot,
MTB_GetItem,
] ]
+138 -54
View File
@@ -3,11 +3,12 @@ import json
import math import math
import os import os
import comfy.model_management as model_management import comfy.utils
import folder_paths import folder_paths
import numpy as np import numpy as np
import torch import torch
import torch.nn.functional as F import torch.nn.functional as F
from comfy import model_management
from PIL import Image, ImageOps from PIL import Image, ImageOps
from PIL.PngImagePlugin import PngInfo from PIL.PngImagePlugin import PngInfo
from skimage.filters import gaussian from skimage.filters import gaussian
@@ -74,7 +75,10 @@ class MTB_ExtractCoordinatesFromImage:
def INPUT_TYPES(cls): def INPUT_TYPES(cls):
return { return {
"required": { "required": {
"threshold": ("FLOAT",), "threshold": (
"FLOAT",
{"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01},
),
"max_points": ("INT", {"default": 50, "min": 0}), "max_points": ("INT", {"default": 50, "min": 0}),
}, },
"optional": {"image": ("IMAGE",), "mask": ("MASK",)}, "optional": {"image": ("IMAGE",), "mask": ("MASK",)},
@@ -87,72 +91,124 @@ class MTB_ExtractCoordinatesFromImage:
image: torch.Tensor | None = None, image: torch.Tensor | None = None,
mask: torch.Tensor | None = None, mask: torch.Tensor | None = None,
) -> tuple[list[list[tuple[int, int]]], torch.Tensor]: ) -> tuple[list[list[tuple[int, int]]], torch.Tensor]:
if image is not None: if image is None and mask is None:
batch_count, height, width, channel_count = image.shape raise ValueError("Must provide either image or mask")
imgs = image
else:
if mask is None:
raise ValueError("Must provide either image or mask")
batch_count, height, width = mask.shape
channel_count = 1
imgs = mask
if channel_count not in [1, 2, 3, 4]: if image is not None:
raise ValueError(f"Incorrect channel count: {channel_count}") batch_count, height, width, _channel_count = image.shape
input_device = image.device
if mask is not None:
if mask.ndim == 2:
mask = mask.unsqueeze(0)
if mask.ndim != 3:
raise ValueError(
f"Mask has unexpected ndim: {mask.ndim}. Expected 2 or 3."
)
b_mask, h_mask, w_mask = mask.shape
if not (h_mask == height and w_mask == width):
raise ValueError(
f"Image dimensions ({height}x{width}) and mask dimensions ({h_mask}x{w_mask}) are spatially incompatible."
)
if b_mask == 1 and batch_count > 1:
mask = mask.expand(batch_count, height, width)
elif b_mask != batch_count:
raise ValueError(
f"Image batch size ({batch_count}) and mask batch size ({b_mask}) are incompatible and mask cannot be broadcast."
)
else:
if mask.ndim == 2:
mask = mask.unsqueeze(0)
if mask.ndim != 3:
raise ValueError(
f"Mask has unexpected ndim: {mask.ndim} when image is not provided. Expected 2 or 3."
)
batch_count, height, width = mask.shape
input_device = mask.device
all_points: list[list[tuple[int, int]]] = [] all_points: list[list[tuple[int, int]]] = []
debug_images = torch.zeros( debug_images = torch.zeros(
(batch_count, height, width, 3), (batch_count, height, width, 3),
dtype=torch.uint8, dtype=torch.uint8,
device=imgs.device, device=input_device,
) )
for i, img in enumerate(imgs): points_tensor = torch.tensor(
if channel_count == 1: [255, 255, 255], dtype=torch.uint8, device=input_device
alpha_channel = img if len(img.shape) == 2 else img[:, :, 0] )
elif channel_count == 2:
alpha_channel = img[:, :, 1] for i in range(batch_count):
elif channel_count == 4: value_threshold: torch.Tensor
alpha_channel = img[:, :, 3] if image is not None:
img_slice = image[i]
img_channels = img_slice.shape[2]
if img_channels == 1 or img_channels == 2:
value_threshold = img_slice[:, :, 0]
elif img_channels == 3 or img_channels == 4:
value_threshold = img_slice[:, :, :3].max(dim=2)[0]
else:
raise ValueError(
f"Unsupported image channel count: {img_channels} for image at batch index {i}"
)
else: else:
# get intensity mask_slice = mask[i]
alpha_channel = img[:, :, :3].max(dim=2)[0] value_threshold = mask_slice
points = (alpha_channel > threshold).nonzero(as_tuple=False) condition = value_threshold > threshold
if image is not None and mask is not None:
mask_slice = mask[i]
mask_active_condition = mask_slice > 0.0
condition = condition & mask_active_condition
if len(points) > max_points: points_yx = condition.nonzero(as_tuple=False)
indices = torch.randperm(points.size(0), device=img.device)[
:max_points
]
points = points[indices]
points = [(int(y.item()), int(x.item())) for x, y in points] if points_yx.size(0) > max_points:
all_points.append(points) # shuffle and pick max_points randomly
indices = torch.randperm(
points_yx.size(0), device=input_device
)[:max_points]
points_yx = points_yx[indices]
elif max_points == 0:
points_yx = torch.empty(
(0, 2), dtype=torch.long, device=input_device
)
for x, y in points: current_points = [
self._draw_circle(debug_images[i], (x, y), 5) (int(p[1].item()), int(p[0].item())) for p in points_yx
]
all_points.append(current_points)
for x_coord, y_coord in current_points:
self._draw_circle(
debug_images[i],
(x_coord, y_coord),
radius=5,
color_tensor=points_tensor,
)
return (all_points, debug_images) return (all_points, debug_images)
@staticmethod @staticmethod
def _draw_circle( def _draw_circle(
image: torch.Tensor, center: tuple[int, int], radius: int image: torch.Tensor,
center: tuple[int, int],
radius: int,
color_tensor: torch.Tensor,
): ):
"""Draw a 5px circle on the image.""" """Draw a 5px circle on the image."""
x0, y0 = center x0, y0 = center
for x in range(-radius, radius + 1): h, w, _ = image.shape
for y in range(-radius, radius + 1): min_x_bbox = max(0, x0 - radius)
in_radius = x**2 + y**2 <= radius**2 max_x_bbox = min(w - 1, x0 + radius)
in_bounds = ( min_y_bbox = max(0, y0 - radius)
0 <= x0 + x < image.shape[1] max_y_bbox = min(h - 1, y0 + radius)
and 0 <= y0 + y < image.shape[0]
) for py in range(min_y_bbox, max_y_bbox + 1):
if in_radius and in_bounds: for px in range(min_x_bbox, max_x_bbox + 1):
image[y0 + y, x0 + x] = torch.tensor( if (px - x0) ** 2 + (py - y0) ** 2 <= radius**2:
[255, 255, 255], image[py, px] = color_tensor
dtype=torch.uint8,
device=image.device,
)
class MTB_ColorCorrectGPU: class MTB_ColorCorrectGPU:
@@ -627,6 +683,7 @@ class MTB_ImageCompare:
import requests import requests
import time
class MTB_LoadImageFromUrl: class MTB_LoadImageFromUrl:
@@ -642,6 +699,14 @@ class MTB_LoadImageFromUrl:
"default": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Example.jpg/800px-Example.jpg" "default": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Example.jpg/800px-Example.jpg"
}, },
), ),
"retry_count": (
"INT",
{"default": 3, "min": 1, "max": 20, "step": 1},
),
"retry_interval": (
"FLOAT",
{"default": 1.0, "min": 0.0, "max": 60.0, "step": 0.1},
),
} }
} }
@@ -649,11 +714,27 @@ class MTB_LoadImageFromUrl:
FUNCTION = "load" FUNCTION = "load"
CATEGORY = "mtb/IO" CATEGORY = "mtb/IO"
def load(self, url): def load(self, url, retry_count, retry_interval):
# get the image from the url # get the image from the url with retry + exponential backoff
image = Image.open(requests.get(url, stream=True).raw) last_error = None
image = ImageOps.exif_transpose(image) for attempt in range(retry_count):
return (pil2tensor(image),) try:
response = requests.get(url, stream=True)
response.raise_for_status()
image = Image.open(response.raw)
image = ImageOps.exif_transpose(image)
return (pil2tensor(image),)
except Exception as e:
last_error = e
if attempt == retry_count - 1:
raise
wait_seconds = retry_interval * (2**attempt)
if wait_seconds > 0:
time.sleep(wait_seconds)
if last_error is not None:
raise last_error
raise RuntimeError("Failed to load image from URL without captured exception")
class MTB_Blur: class MTB_Blur:
@@ -823,8 +904,11 @@ class MTB_MaskToImage:
return { return {
"required": { "required": {
"mask": ("MASK",), "mask": ("MASK",),
"color": ("COLOR",), "color": ("COLOR", {"widgetType": "MTB_COLOR"}),
"background": ("COLOR", {"default": "#000000"}), "background": (
"COLOR",
{"default": "#000000", "widgetType": "MTB_COLOR"},
),
}, },
"optional": { "optional": {
"invert": ("BOOLEAN", {"default": False}), "invert": ("BOOLEAN", {"default": False}),
+9 -2
View File
@@ -21,7 +21,11 @@ class MTB_StackImages:
"match_method": ( "match_method": (
["error", "smallest", "largest"], ["error", "smallest", "largest"],
{"default": "error"}, {"default": "error"},
) ),
"output_rgb": (
"BOOLEAN",
{"default": True, "tooltip": "Output RGB instead of RGBA"},
),
}, },
} }
@@ -29,7 +33,7 @@ class MTB_StackImages:
FUNCTION = "stack" FUNCTION = "stack"
CATEGORY = "mtb/image utils" CATEGORY = "mtb/image utils"
def stack(self, vertical, match_method="error", **kwargs): def stack(self, vertical, match_method="error", output_rgb=True, **kwargs):
if not kwargs: if not kwargs:
raise ValueError("At least one tensor must be provided.") raise ValueError("At least one tensor must be provided.")
@@ -98,6 +102,9 @@ class MTB_StackImages:
stacked_tensor = torch.cat(normalized_tensors, dim=dim) stacked_tensor = torch.cat(normalized_tensors, dim=dim)
if output_rgb:
stacked_tensor = stacked_tensor[:, :, :, :3]
return (stacked_tensor,) return (stacked_tensor,)
def normalize_to_rgba(self, tensor): def normalize_to_rgba(self, tensor):
+17
View File
@@ -0,0 +1,17 @@
# from ..utils import hex_to_rgb
class MTB_ColorInput:
RETURN_TYPES = ("COLOR",)
FUNCTION = "color"
CATEGORY = "mtb/color"
@classmethod
def INPUT_TYPES(cls):
return {
"required": {"color": ("MTB_COLOR", {"default": "#ffffff"})},
}
def color(self, color):
return (color,)
__nodes__ = [MTB_ColorInput]
+1 -1
View File
@@ -34,7 +34,7 @@ class MTB_ImageRemoveBackgroundRembg:
), ),
"bgcolor": ( "bgcolor": (
"COLOR", "COLOR",
{"default": "#000000"}, {"default": "#000000","widgetType": "MTB_COLOR"},
), ),
}, },
} }
+1 -1
View File
@@ -145,7 +145,7 @@ class MTB_ModelPatchSeamless:
tilingX, tilingX,
tilingY, tilingY,
): ):
hacked_model = copy.deepcopy(model) hacked_model = model.clone()
self.apply_circular( self.apply_circular(
hacked_model.model, startStep, stopStep, tilingX, tilingY hacked_model.model, startStep, stopStep, tilingX, tilingY
) )
+4 -1
View File
@@ -43,7 +43,10 @@ class MTB_TransformImage:
["edge", "constant", "reflect", "symmetric"], ["edge", "constant", "reflect", "symmetric"],
{"default": "edge"}, {"default": "edge"},
), ),
"constant_color": ("COLOR", {"default": "#000000"}), "constant_color": (
"COLOR",
{"default": "#000000", "widgetType": "MTB_COLOR"},
),
}, },
"optional": { "optional": {
"filter_type": ( "filter_type": (
+1 -1
View File
@@ -27,7 +27,7 @@ class MTB_LoadVitMatteModel:
def execute(self, *, kind: str, autodownload: bool): def execute(self, *, kind: str, autodownload: bool):
dest = models_dir / "vitmatte" dest = models_dir / "vitmatte"
dest.mkdir(exist_ok=True) dest.mkdir(exist_ok=True)
name = "dist" if kind == "Distinctions-646" else "com" name = "dis" if kind == "Distinctions-646" else "com"
file = hf_hub_download( file = hf_hub_download(
repo_id="melmass/pytorch-scripts", repo_id="melmass/pytorch-scripts",
+2 -2
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "comfy-mtb" name = "comfy-mtb"
version = "0.3.0" version = "0.5.4"
description = "Animation oriented nodes pack for ComfyUI." description = "Animation oriented nodes pack for ComfyUI."
license = { text = "MIT" } license = { text = "MIT" }
readme = "README.md" readme = "README.md"
@@ -63,7 +63,7 @@ DisplayName = "comfy-mtb"
Icon = "https://avatars.githubusercontent.com/u/7041726?v=4" Icon = "https://avatars.githubusercontent.com/u/7041726?v=4"
[tool.bumpversion] [tool.bumpversion]
current_version = "0.3.0" current_version = "0.5.1"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)" parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{patch}"] serialize = ["{major}.{minor}.{patch}"]
search = "{current_version}" search = "{current_version}"
-18
View File
@@ -1,6 +1,5 @@
import contextlib import contextlib
import functools import functools
import importlib
import math import math
import operator import operator
import os import os
@@ -462,23 +461,6 @@ def _run_command(shell_cmd, ignored_lines_start):
print("Command executed successfully!") print("Command executed successfully!")
def import_install(package_name):
package_spec = reqs_map.get(package_name, package_name)
try:
importlib.import_module(package_name)
except Exception: # (ImportError, ModuleNotFoundError):
run_command(
[
Path(sys.executable).as_posix(),
"-m",
"pip",
"install",
package_spec,
]
)
importlib.import_module(package_name)
# endregion # endregion
+35 -75
View File
@@ -25,6 +25,19 @@ export function makeUUID() {
return uuid return uuid
} }
// - basic debounce decorator
export function debounce(func, delay) {
let timeout
let debounced = function (...args) {
clearTimeout(timeout)
timeout = setTimeout(() => func.apply(this, args), delay)
}
debounced.cancel = () => {
clearTimeout(timeout)
}
return debounced
}
//- local storage manager //- local storage manager
export class LocalStorageManager { export class LocalStorageManager {
constructor(namespace) { constructor(namespace) {
@@ -195,6 +208,7 @@ export function hideWidgetForGood(node, widget, suffix = '') {
widget.origComputeSize = widget.computeSize widget.origComputeSize = widget.computeSize
widget.origSerializeValue = widget.serializeValue widget.origSerializeValue = widget.serializeValue
widget.computeSize = () => [0, -4] // -4 is due to the gap litegraph adds between widgets automatically widget.computeSize = () => [0, -4] // -4 is due to the gap litegraph adds between widgets automatically
widget.hidden = true
widget.type = CONVERTED_TYPE + suffix widget.type = CONVERTED_TYPE + suffix
// widget.serializeValue = () => { // widget.serializeValue = () => {
// // Prevent serializing the widget if we have no input linked // // Prevent serializing the widget if we have no input linked
@@ -621,21 +635,21 @@ function getBrightness(rgbObj) {
export function calculateTotalChildrenHeight(parentElement) { export function calculateTotalChildrenHeight(parentElement) {
let totalHeight = 0 let totalHeight = 0
if (!parentElement || !parentElement.children) {
return 0
}
for (const child of parentElement.children) { for (const child of parentElement.children) {
const style = window.getComputedStyle(child) const style = window.getComputedStyle(child)
// Get height as an integer (without 'px') const height = Number.parseFloat(style.height)
const height = Number.parseInt(style.height, 10) const marginTop = Number.parseFloat(style.marginTop)
const marginBottom = Number.parseFloat(style.marginBottom)
// Get vertical margin as integers
const marginTop = Number.parseInt(style.marginTop, 10)
const marginBottom = Number.parseInt(style.marginBottom, 10)
// Sum up height and vertical margins
totalHeight += height + marginTop + marginBottom totalHeight += height + marginTop + marginBottom
} }
return totalHeight return Math.ceil(totalHeight)
} }
export const loadScript = ( export const loadScript = (
@@ -646,13 +660,15 @@ export const loadScript = (
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
// Check if the script already exists // Check if the script already exists
const existingScript = document.querySelector(`script[src="${FILE_URL}"]`) let scriptEle = document.querySelector(`script[src="${FILE_URL}"]`)
if (existingScript) { if (scriptEle) {
resolve({ status: true, message: 'Script already loaded' }) scriptEle.addEventListener('load', (_ev) => {
resolve({ status: true })
})
return return
} }
const scriptEle = document.createElement('script') scriptEle = document.createElement('script')
scriptEle.type = type scriptEle.type = type
scriptEle.async = async scriptEle.async = async
scriptEle.src = FILE_URL scriptEle.src = FILE_URL
@@ -671,6 +687,8 @@ export const loadScript = (
document.body.appendChild(scriptEle) document.body.appendChild(scriptEle)
} catch (error) { } catch (error) {
reject(error) reject(error)
} finally {
infoLogger(`Finally loaded script: ${FILE_URL}`)
} }
}) })
} }
@@ -784,12 +802,10 @@ function loadParser(shiki) {
export const ensureMarkdownParser = async (callback) => { export const ensureMarkdownParser = async (callback) => {
infoLogger('Ensuring md parser') infoLogger('Ensuring md parser')
let use_shiki = false const use_shiki = app.extensionManager.setting.get(
try { 'mtb.noteplus.use-shiki',
use_shiki = await api.getSetting('mtb.Use Shiki') false,
} catch (e) { )
console.warn('Option not available yet', e)
}
if (window.MTB?.mdParser) { if (window.MTB?.mdParser) {
infoLogger('Markdown parser found') infoLogger('Markdown parser found')
@@ -814,8 +830,7 @@ export const ensureMarkdownParser = async (callback) => {
callbackQueue.push(callback) callbackQueue.push(callback)
} }
await parserPromise await await parserPromise
await parserPromise
return window.MTB.mdParser return window.MTB.mdParser
} }
@@ -1154,58 +1169,3 @@ export const setServerInfo = async (opts) => {
} }
// #endregion // #endregion
// #region Authoring API / graph utilities
export const getAPIInputs = () => {
const inputs = {}
let counter = 1
for (const node of getNodes(true)) {
const widgets = node.widgets
if (node.properties.mtb_api && node.properties.useAPI) {
if (node.properties.mtb_api.inputs) {
for (const currentName in node.properties.mtb_api.inputs) {
const current = node.properties.mtb_api.inputs[currentName]
if (current.enabled) {
const inputName = current.name || currentName
const widget = widgets.find((w) => w.name === currentName)
if (!widget) continue
if (!(inputName in inputs)) {
inputs[inputName] = {
...current,
id: counter,
name: inputName,
type: current.type,
node_id: node.id,
widgets: [],
}
}
inputs[inputName].widgets.push(widget)
counter = counter + 1
}
}
}
}
}
return inputs
}
export const getNodes = (skip_unused) => {
const nodes = []
for (const outerNode of app.graph.computeExecutionOrder(false)) {
const skipNode =
(outerNode.mode === 2 || outerNode.mode === 4) && skip_unused
const innerNodes =
!skipNode && outerNode.getInnerNodes
? outerNode.getInnerNodes()
: [outerNode]
for (const node of innerNodes) {
if ((node.mode === 2 || node.mode === 4) && skip_unused) {
continue
}
nodes.push(node)
}
}
return nodes
}
// #endregion
+20 -25
View File
@@ -28,7 +28,7 @@ function createDebugSection(title) {
margin: '8px 0', margin: '8px 0',
padding: '8px', padding: '8px',
borderRadius: '4px', borderRadius: '4px',
backgroundColor: 'rgba(0,0,0,0.2)' backgroundColor: 'rgba(0,0,0,0.2)',
}) })
const header = mtb_ui.makeElement('h3', { const header = mtb_ui.makeElement('h3', {
@@ -37,7 +37,7 @@ function createDebugSection(title) {
borderBottom: '1px solid rgba(255,255,255,0.1)', borderBottom: '1px solid rgba(255,255,255,0.1)',
fontSize: '14px', fontSize: '14px',
fontWeight: 'bold', fontWeight: 'bold',
color: '#9f9' color: '#9f9',
}) })
header.textContent = title header.textContent = title
section.appendChild(header) section.appendChild(header)
@@ -47,21 +47,21 @@ function createDebugSection(title) {
function createDebugContent(content, type) { function createDebugContent(content, type) {
const wrapper = mtb_ui.makeElement('div', { const wrapper = mtb_ui.makeElement('div', {
margin: '4px 0' margin: '4px 0',
}) })
if (type === 'text') { if (type === 'text') {
const text = mtb_ui.makeElement('p', { const text = mtb_ui.makeElement('p', {
margin: '2px 0', margin: '2px 0',
fontFamily: 'monospace', fontFamily: 'monospace',
whiteSpace: 'pre-wrap' whiteSpace: 'pre-wrap',
}) })
text.innerHTML = content text.innerHTML = content
wrapper.appendChild(text) wrapper.appendChild(text)
} else if (type === 'image') { } else if (type === 'image') {
const img = mtb_ui.makeElement('img', { const img = mtb_ui.makeElement('img', {
width: '100%', width: '100%',
borderRadius: '2px' borderRadius: '2px',
}) })
img.src = content img.src = content
wrapper.appendChild(img) wrapper.appendChild(img)
@@ -148,18 +148,18 @@ app.registerExtension({
const uiData = data.ui || data const uiData = data.ui || data
if (uiData.items) { if (uiData.items) {
uiData.items.forEach(item => { uiData.items.forEach((item) => {
const inputName = item.input const inputName = item.input
if (!inputData[inputName]) { if (!inputData[inputName]) {
inputData[inputName] = { text: [], b64_images: [] } inputData[inputName] = { text: [], b64_images: [] }
} }
if (item.text) { if (item.text) {
inputData[inputName].text.push(...item.text) inputData[inputName].text.push(...item.text)
} }
if (item.b64_images) { if (item.b64_images) {
inputData[inputName].b64_images.push(...item.b64_images) inputData[inputName].b64_images.push(...item.b64_images)
} }
}) })
} }
let widgetI = 1 let widgetI = 1
@@ -171,23 +171,18 @@ app.registerExtension({
const section = createDebugSection(inputName) const section = createDebugSection(inputName)
if (content.text.length > 0) { if (content.text.length > 0) {
content.text.forEach(text => { content.text.forEach((text) => {
section.appendChild(createDebugContent(text, 'text')) section.appendChild(createDebugContent(text, 'text'))
}) })
} }
if (content.b64_images.length > 0) { if (content.b64_images.length > 0) {
content.b64_images.forEach(img => { content.b64_images.forEach((img) => {
section.appendChild(createDebugContent(img, 'image')) section.appendChild(createDebugContent(img, 'image'))
}) })
} }
this.addDOMWidget( this.addDOMWidget(`debug_section_${widgetI}`, 'CUSTOM', section, {})
`debug_section_${widgetI}`,
'CUSTOM',
section,
{}
)
widgetI++ widgetI++
} }
+296 -296
View File
@@ -13,40 +13,40 @@ import { api } from '../../scripts/api.js'
import { app } from '../../scripts/app.js' import { app } from '../../scripts/app.js'
import { LocalStorageManager } from './comfy_shared.js' import { LocalStorageManager } from './comfy_shared.js'
const styles = { const styles = {
lighbox: { lighbox: {
position: 'fixed', position: 'fixed',
top: 0, top: 0,
left: 0, left: 0,
width: '100vw', width: '100vw',
height: '100vh', height: '100vh',
background: 'rgba(0,0,0,0.5)', background: 'rgba(0,0,0,0.5)',
display: 'none', display: 'none',
justifyContent: 'center', justifyContent: 'center',
alignItems: 'center', alignItems: 'center',
zIndex: 999, zIndex: 999,
}, },
lightboxBtn: (extra) => ({ lightboxBtn: (extra) => ({
position: 'absolute', position: 'absolute',
top: '50%', top: '50%',
background: 'none', background: 'none',
border: 'none', border: 'none',
color: '#fff', color: '#fff',
zIndex: 1000, zIndex: 1000,
fontSize: '30px', fontSize: '30px',
cursor: 'pointer', cursor: 'pointer',
pointerEvents: 'auto', pointerEvents: 'auto',
...extra, ...extra,
}), }),
img_list: { img_list: {
minHeight: '30px', minHeight: '30px',
maxHeight: '300px', maxHeight: '300px',
width: '100vw', width: '100vw',
position: 'absolute', position: 'absolute',
bottom: 0, bottom: 0,
zIndex: 10, zIndex: 10,
background: '#333', background: '#333',
overflow: 'auto', overflow: 'auto',
}, },
} }
let currentImageIndex = 0 let currentImageIndex = 0
@@ -58,299 +58,299 @@ const storage = new LocalStorageManager('mtb')
let activated = storage.get('image_feed', false) let activated = storage.get('image_feed', false)
app.registerExtension({ app.registerExtension({
name: 'mtb.ImageFeed', name: 'mtb.ImageFeed',
setup: () => { setup: () => {
app.ui.settings.addSetting({ app.ui.settings.addSetting({
id: 'mtb.Main.image-feed-enabled', id: 'mtb.Main.image-feed-enabled',
category: ['mtb', 'Main', 'image-feed-enabled'], category: ['mtb', ' Main', 'image-feed-enabled'],
name: 'Enable Image Feed', name: 'Enable Image Feed',
type: 'boolean', type: 'boolean',
defaultValue: false, defaultValue: false,
attrs: { attrs: {
style: { style: {
fontFamily: 'monospace', fontFamily: 'monospace',
}, },
}, },
async onChange(value) { async onChange(value) {
storage.set('image_feed', value) storage.set('image_feed', value)
activated = value activated = value
}, },
}) })
}, },
init: async () => { init: async () => {
if (!activated) { if (!activated) {
return return
} }
const pythongossFeed = app.extensions.find( const pythongossFeed = app.extensions.find(
(e) => e.name === 'pysssss.ImageFeed', (e) => e.name === 'pysssss.ImageFeed',
) )
if (pythongossFeed) { if (pythongossFeed) {
console.warn( console.warn(
"[mtb] - Aborting the loading of mtb's imageFeed in favor of pysssss.ImageFeed", "[mtb] - Aborting the loading of mtb's imageFeed in favor of pysssss.ImageFeed",
) )
activated = false // just in case other methods are added later on activated = false // just in case other methods are added later on
return return
} }
// - HTML & CSS // - HTML & CSS
//- lightbox //- lightbox
const lightboxContainer = document.createElement('div') const lightboxContainer = document.createElement('div')
Object.assign(lightboxContainer.style, styles.lighbox) Object.assign(lightboxContainer.style, styles.lighbox)
const lightboxImage = document.createElement('img') const lightboxImage = document.createElement('img')
Object.assign(lightboxImage.style, { Object.assign(lightboxImage.style, {
maxHeight: '100%', maxHeight: '100%',
maxWidth: '100%', maxWidth: '100%',
borderRadius: '5px', borderRadius: '5px',
}) })
// previous and next buttons // previous and next buttons
const lightboxPrevBtn = document.createElement('button') const lightboxPrevBtn = document.createElement('button')
const lightboxNextBtn = document.createElement('button') const lightboxNextBtn = document.createElement('button')
lightboxPrevBtn.textContent = '❮' lightboxPrevBtn.textContent = '❮'
lightboxNextBtn.textContent = '❯' lightboxNextBtn.textContent = '❯'
Object.assign(lightboxPrevBtn.style, styles.lightboxBtn({ left: '0%' })) Object.assign(lightboxPrevBtn.style, styles.lightboxBtn({ left: '0%' }))
Object.assign(lightboxNextBtn.style, styles.lightboxBtn({ right: '0%' })) Object.assign(lightboxNextBtn.style, styles.lightboxBtn({ right: '0%' }))
// close button // close button
const lightboxCloseBtn = document.createElement('button') const lightboxCloseBtn = document.createElement('button')
Object.assign( Object.assign(
lightboxCloseBtn.style, lightboxCloseBtn.style,
styles.lightboxBtn({ right: '0', top: '0' }), styles.lightboxBtn({ right: '0', top: '0' }),
) )
lightboxCloseBtn.textContent = '❌' lightboxCloseBtn.textContent = '❌'
const lightboxButtons = document.createElement('div') const lightboxButtons = document.createElement('div')
Object.assign(lightboxButtons.style, { Object.assign(lightboxButtons.style, {
position: 'absolute', position: 'absolute',
top: '0%', top: '0%',
right: '0%', right: '0%',
// transform: "translate(50%, -50%)", // transform: "translate(50%, -50%)",
height: '100%', height: '100%',
width: '100%', width: '100%',
background: 'none', background: 'none',
border: 'none', border: 'none',
color: '#fff', color: '#fff',
fontSize: '30px', fontSize: '30px',
cursor: 'pointer', cursor: 'pointer',
pointerEvents: 'none', pointerEvents: 'none',
}) })
lightboxButtons.append(lightboxPrevBtn, lightboxNextBtn, lightboxCloseBtn) lightboxButtons.append(lightboxPrevBtn, lightboxNextBtn, lightboxCloseBtn)
lightboxContainer.append(lightboxButtons, lightboxImage) lightboxContainer.append(lightboxButtons, lightboxImage)
//- image list //- image list
const imageListContainer = document.createElement('div') const imageListContainer = document.createElement('div')
Object.assign(imageListContainer.style, styles.img_list) Object.assign(imageListContainer.style, styles.img_list)
const createImgListBtn = (text, style) => { const createImgListBtn = (text, style) => {
const btn = document.createElement('button') const btn = document.createElement('button')
btn.type = 'button' btn.type = 'button'
btn.textContent = text btn.textContent = text
Object.assign(btn.style, { Object.assign(btn.style, {
...style, ...style,
border: 'none', border: 'none',
color: '#fff', color: '#fff',
background: 'none', background: 'none',
height: '20px', height: '20px',
cursor: 'pointer', cursor: 'pointer',
position: 'absolute', position: 'absolute',
top: '5px', top: '5px',
fontSize: '12px', fontSize: '12px',
lineHeight: '12px', lineHeight: '12px',
}) })
imageListContainer.append(btn) imageListContainer.append(btn)
return btn return btn
} }
const showBtn = document.createElement('button') const showBtn = document.createElement('button')
const closeBtn = createImgListBtn('❌', { const closeBtn = createImgListBtn('❌', {
width: '20px', width: '20px',
textIndent: '-4px', textIndent: '-4px',
right: '5px', right: '5px',
}) })
const loadButton = createImgListBtn('Load Session History', { const loadButton = createImgListBtn('Load Session History', {
right: '90px', right: '90px',
}) })
const clearButton = createImgListBtn('Clear', { const clearButton = createImgListBtn('Clear', {
right: '30px', right: '30px',
}) })
//- tools popup button //- tools popup button
showBtn.classList.add('comfy-settings-btn') showBtn.classList.add('comfy-settings-btn')
Object.assign(showBtn.style, { Object.assign(showBtn.style, {
right: '16px', right: '16px',
cursor: 'pointer', cursor: 'pointer',
display: 'none', display: 'none',
}) })
//- append to DOM //- append to DOM
document.body.append(imageListContainer) document.body.append(imageListContainer)
showBtn.textContent = '🖼' showBtn.textContent = '🖼'
showBtn.onclick = () => { showBtn.onclick = () => {
imageListContainer.style.display = 'block' imageListContainer.style.display = 'block'
showBtn.style.display = 'none' showBtn.style.display = 'none'
} }
document.querySelector('.comfy-settings-btn').after(showBtn) document.querySelector('.comfy-settings-btn').after(showBtn)
document.querySelector('.comfy-settings-btn').after(lightboxContainer) document.querySelector('.comfy-settings-btn').after(lightboxContainer)
// for (const { output } of history) { // for (const { output } of history) {
// if (output?.images) { // if (output?.images) {
// for (const src of output.images) { // for (const src of output.images) {
// const img = document.createElement("img"); // const img = document.createElement("img");
// const but = document.createElement("button"); // const but = document.createElement("button");
//- callbacks //- callbacks
closeBtn.onclick = () => { closeBtn.onclick = () => {
imageListContainer.style.display = 'none' imageListContainer.style.display = 'none'
showBtn.style.display = 'unset' showBtn.style.display = 'unset'
} }
clearButton.onclick = () => { clearButton.onclick = () => {
imageListContainer.replaceChildren(closeBtn, clearButton, loadButton) imageListContainer.replaceChildren(closeBtn, clearButton, loadButton)
} }
lightboxNextBtn.onclick = () => { lightboxNextBtn.onclick = () => {
currentImageIndex = (currentImageIndex + 1) % imageUrls.length currentImageIndex = (currentImageIndex + 1) % imageUrls.length
const imageUrl = imageUrls[currentImageIndex] const imageUrl = imageUrls[currentImageIndex]
lightboxImage.src = imageUrl lightboxImage.src = imageUrl
} }
// Modify the lightboxPrevBtn onclick callback // Modify the lightboxPrevBtn onclick callback
lightboxPrevBtn.onclick = () => { lightboxPrevBtn.onclick = () => {
currentImageIndex = currentImageIndex =
(currentImageIndex - 1 + imageUrls.length) % imageUrls.length (currentImageIndex - 1 + imageUrls.length) % imageUrls.length
const imageUrl = imageUrls[currentImageIndex] const imageUrl = imageUrls[currentImageIndex]
lightboxImage.src = imageUrl lightboxImage.src = imageUrl
} }
lightboxCloseBtn.onclick = () => { lightboxCloseBtn.onclick = () => {
lightboxContainer.style.display = 'none' lightboxContainer.style.display = 'none'
} }
lightboxImage.onclick = lightboxNextBtn.onclick lightboxImage.onclick = lightboxNextBtn.onclick
/** /**
* This is the function that creates the image buttons for the image list * This is the function that creates the image buttons for the image list
* They are wrapped in a button so that they can be clicked and open * They are wrapped in a button so that they can be clicked and open
* the image in the lightbox. * the image in the lightbox.
* @param {*} src * @param {*} src
*/ */
const createImageBtn = (src) => { const createImageBtn = (src) => {
console.debug(`making image ${src.filename}`) console.debug(`making image ${src.filename}`)
const img = document.createElement('img') const img = document.createElement('img')
const but = document.createElement('button') const but = document.createElement('button')
Object.assign(but.style, { Object.assign(but.style, {
height: '120px', height: '120px',
width: '120px', width: '120px',
border: 'none', border: 'none',
padding: 0, padding: 0,
margin: 0, margin: 0,
}) })
Object.assign(img.style, { Object.assign(img.style, {
width: '100%', width: '100%',
height: '100%', height: '100%',
objectFit: 'cover', objectFit: 'cover',
}) })
img.src = `/view?filename=${encodeURIComponent(src.filename)}&type=${ img.src = `/view?filename=${encodeURIComponent(src.filename)}&type=${
src.type src.type
}&subfolder=${encodeURIComponent(src.subfolder)}` }&subfolder=${encodeURIComponent(src.subfolder)}`
imageUrls.push(img.src) imageUrls.push(img.src)
console.debug(img.src) console.debug(img.src)
img.onload = () => { img.onload = () => {
but.style.width = `${120 * (img.naturalWidth / img.naturalHeight)}px` but.style.width = `${120 * (img.naturalWidth / img.naturalHeight)}px`
} }
but.onclick = () => { but.onclick = () => {
lightboxContainer.style.display = 'flex' lightboxContainer.style.display = 'flex'
// add the same image to the lightbox // add the same image to the lightbox
lightboxImage.src = img.src lightboxImage.src = img.src
// lighboxContainer.replaceChildren(lightboxButtons, img); // lighboxContainer.replaceChildren(lightboxButtons, img);
} }
// add right click menu // add right click menu
but.addEventListener('contextmenu', (e) => { but.addEventListener('contextmenu', (e) => {
e.preventDefault() e.preventDefault()
if (image_menu) { if (image_menu) {
image_menu.remove() image_menu.remove()
} }
image_menu = document.createElement('div') image_menu = document.createElement('div')
Object.assign(image_menu.style, { Object.assign(image_menu.style, {
position: 'absolute', position: 'absolute',
top: `${e.clientY}px`, top: `${e.clientY}px`,
left: `${e.clientX}px`, left: `${e.clientX}px`,
background: '#333', background: '#333',
color: '#fff', color: '#fff',
padding: '5px', padding: '5px',
borderRadius: '5px', borderRadius: '5px',
zIndex: 999, zIndex: 999,
}) })
const load_img = document.createElement('button') const load_img = document.createElement('button')
load_img.textContent = 'Load' load_img.textContent = 'Load'
load_img.onclick = () => { load_img.onclick = () => {
app.handleFile(img.src) app.handleFile(img.src)
} }
image_menu.appendChild(load_img) image_menu.appendChild(load_img)
document.body.appendChild(image_menu) document.body.appendChild(image_menu)
}) })
but.append(img) but.append(img)
imageListContainer.prepend(but) imageListContainer.prepend(but)
} }
loadButton.onclick = async () => { loadButton.onclick = async () => {
const all_history = await api.getHistory() const all_history = await api.getHistory()
for (const history of all_history.History) { for (const history of all_history.History) {
if (history.outputs) { if (history.outputs) {
for (const key of Object.keys(history.outputs)) { for (const key of Object.keys(history.outputs)) {
console.debug(key) console.debug(key)
if (history.outputs[key].images) { if (history.outputs[key].images) {
for (const im of history.outputs[key].images) { for (const im of history.outputs[key].images) {
console.debug(im) console.debug(im)
createImageBtn(im) createImageBtn(im)
} }
} }
} }
// for (const src of outputs.outputs.images) { // for (const src of outputs.outputs.images) {
// console.debug(src) // console.debug(src)
// makeImage(`${src.subfolder}/${src.filename}`) // makeImage(`${src.subfolder}/${src.filename}`)
// } // }
} }
} }
} }
///////------- ///////-------
// const all_history = await api.getHistory() // const all_history = await api.getHistory()
// for (const history of all_history.History) { // for (const history of all_history.History) {
// if (history.outputs) { // if (history.outputs) {
// for (const key of Object.keys(history.outputs)) { // for (const key of Object.keys(history.outputs)) {
// for (const im of history.outputs[key].images) { // for (const im of history.outputs[key].images) {
// makeImage(im) // makeImage(im)
// } // }
// } // }
// // for (const src of outputs.outputs.images) { // // for (const src of outputs.outputs.images) {
// // console.debug(src) // // console.debug(src)
// // makeImage(`${src.subfolder}/${src.filename}`) // // makeImage(`${src.subfolder}/${src.filename}`)
// // } // // }
// } // }
// } // }
//- Hook into the API //- Hook into the API
api.addEventListener('executed', ({ detail }) => { api.addEventListener('executed', ({ detail }) => {
if (detail?.output?.images) { if (detail?.output?.images) {
for (const src of detail.output.images) { for (const src of detail.output.images) {
console.debug(`Adding ${src} to image feed`) console.debug(`Adding ${src} to image feed`)
createImageBtn(src) createImageBtn(src)
} }
} }
}) })
}, },
}) })
+143 -43
View File
@@ -3,6 +3,7 @@
import { app } from '../../scripts/app.js' import { app } from '../../scripts/app.js'
import { api } from '../../scripts/api.js' import { api } from '../../scripts/api.js'
import * as mtb_ui from './mtb_ui.js'
import * as shared from './comfy_shared.js' import * as shared from './comfy_shared.js'
import { import {
@@ -15,7 +16,13 @@ import {
} from './mtb_ui.js' } from './mtb_ui.js'
const offset = 0 const offset = 0
// These are "global" variables mostly meant to sync user settings.
let currentWidth = 200 let currentWidth = 200
let saltUrls =
app.extensionManager.setting.get('mtb.io-sidebar.salt_urls') || false
let targetWidth =
app.extensionManager.setting.get('mtb.io-sidebar.img-size') || 512
let currentMode = 'input' let currentMode = 'input'
let subfolder = '' let subfolder = ''
let currentSort = 'None' let currentSort = 'None'
@@ -46,15 +53,19 @@ const updateImage = (node, image) => {
* @param {ResultItem} resultItem * @param {ResultItem} resultItem
* @returns {string} - The request URL. * @returns {string} - The request URL.
*/ */
const resultItemToQuery = (resultItem) => const resultItemToQuery = (resultItem) => {
[ const res = [
`/mtb/view?filename=${resultItem.filename}`, `/mtb/view?filename=${resultItem.filename}`,
`width=512`,
`type=${resultItem.type}`, `type=${resultItem.type}`,
`subfolder=${resultItem.subfolder}`, `subfolder=${resultItem.subfolder}`,
`preview=`, 'preview=',
].join('&') ]
if (targetWidth > 0) {
res.splice(1, 0, `width=${targetWidth}`)
}
return res.join('&')
}
/** /**
* Retrieves the unique prompt ID from a history task item. * Retrieves the unique prompt ID from a history task item.
* @param {HistoryTaskItem} historyTaskItem * @param {HistoryTaskItem} historyTaskItem
@@ -80,7 +91,7 @@ const getNewOutputUrls = (mostRecentTask) => {
const imageOutputs = Object.values(nodeOutputs.images) const imageOutputs = Object.values(nodeOutputs.images)
imageOutputs.forEach( imageOutputs.forEach(
(resultItem) => (resultItem) =>
(urls[resultItem.filename] = resultItemToQuery(resultItem)) (urls[resultItem.filename] = resultItemToQuery(resultItem)),
) )
} }
// Can process `animated` and `audio` outputs here. // Can process `animated` and `audio` outputs here.
@@ -209,7 +220,7 @@ const getUrls = async (subfolder) => {
if (currentMode === 'video') { if (currentMode === 'video') {
const output = await shared.runAction( const output = await shared.runAction(
'getUserVideos', 'getUserVideos',
256, targetWidth,
count, count,
offset, offset,
currentSort, currentSort,
@@ -219,11 +230,13 @@ const getUrls = async (subfolder) => {
const output = await shared.runAction( const output = await shared.runAction(
'getUserImages', 'getUserImages',
currentMode, currentMode,
targetWidth,
count, count,
offset, offset,
currentSort, currentSort,
false, false,
subfolder, subfolder,
saltUrls,
) )
return output || {} return output || {}
} }
@@ -236,55 +249,110 @@ if (window?.__COMFYUI_FRONTEND_VERSION__) {
const sidebar_extension = { const sidebar_extension = {
name: 'mtb.io-sidebar', name: 'mtb.io-sidebar',
// init: async () => { settings: [
// try { {
// const res = await api.fetchApi('/mtb/server-info')
// const msg = await res.json()
// exposed = msg.exposed
// } catch (e) {
// console.error('Error:', e)
// }
// },
init: () => {
let handle
const version = window?.__COMFYUI_FRONTEND_VERSION__
console.log(`%c ${version}`, 'background: orange; color: white;')
ensureMTBStyles()
app.ui.settings.addSetting({
id: 'mtb.io-sidebar.count', id: 'mtb.io-sidebar.count',
category: ['mtb', 'Input & Output Sidebar', 'count'], category: ['mtb', 'Input & Output Sidebar', 'count'],
name: 'Number of images to fetch', name: 'Number of images to fetch',
type: 'number', type: 'number',
defaultValue: 1000, defaultValue: 1000,
tooltip: tooltip:
"This setting affects the input/output sidebar to determine how many images to fetch per pagination (pagination is not yet supported so for now it's the static total)", "This setting affects the input/output sidebar to determine how many images to fetch per pagination (pagination is not yet supported so for now it's the static total)",
attrs: { },
style: { {
// fontFamily: 'monospace', id: 'mtb.io-sidebar.salt_urls',
}, category: ['mtb', 'Input & Output Sidebar', 'salt_urls'],
name: 'Salt URLs',
type: 'boolean',
defaultValue: false,
onChange: (n, o) => {
saltUrls = n
}, },
}) tooltip:
'Adds a random query parameter to every urls to always invalidate caching.',
app.ui.settings.addSetting({ },
{
id: 'mtb.io-sidebar.img-size', id: 'mtb.io-sidebar.img-size',
category: ['mtb', 'Input & Output Sidebar', 'img-size'], category: ['mtb', 'Input & Output Sidebar', 'img-size'],
name: 'Resolution of the images', name: 'Resize width of shown images',
type: 'number',
defaultValue: 512, defaultValue: 512,
type: (name, setter, value, attrs) => {
targetWidth = value
const container = mtb_ui.makeElement('div', {
display: 'flex',
alignItems: 'center',
gap: '8px',
})
tooltip: "It's recommended to keep it at 512px", console.log({ name, setter, value, attrs })
attrs: {
style: { const baseId = name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase()
// fontFamily: 'monospace', const checkboxId = `${baseId}-checkbox`
}, const numberInputId = `${baseId}-number`
const isCheckedInitially = value !== -1
// TODO: better way to get defaultValue?
const defaultValue = 512
const initialNumberValue = isCheckedInitially ? value : defaultValue
console.log('recreate')
const checkbox = mtb_ui.makeElement(
// harder to match styles (.p-toggleswitch-input)
// since it uses a div synced to the input...
'input',
{},
container,
)
checkbox.type = 'checkbox'
checkbox.id = checkboxId
checkbox.checked = isCheckedInitially
const numberInput = mtb_ui.makeElement(
'input.p-inputtext',
{},
container,
)
numberInput.type = 'number'
numberInput.id = numberInputId
numberInput.value = initialNumberValue
numberInput.disabled = !isCheckedInitially
numberInput.min = 128
checkbox.addEventListener('change', () => {
let valToSet = -1
if (checkbox.checked) {
numberInput.disabled = false
valToSet = Number.parseInt(numberInput.value, 10)
if (Number.isNaN(valToSet) || valToSet < numberInput.min) {
valToSet = defaultValue
numberInput.value = valToSet
}
} else {
numberInput.disabled = true
}
setter(valToSet)
})
numberInput.addEventListener('input', () => {
if (checkbox.checked) {
const numValue = Number.parseInt(numberInput.value, 10)
if (!Number.isNaN(numValue) && numberInput.value !== '') {
setter(numValue)
}
}
})
return container
}, },
})
app.ui.settings.addSetting({ tooltip:
"If browsing large folders it's recommended to use this to avoid overflow/crash of the webpage. Image will get resized to this target width on the server before being sent to the client.",
},
{
id: 'mtb.io-sidebar.sort', id: 'mtb.io-sidebar.sort',
category: ['mtb', 'Input & Output Sidebar', 'sort'], category: ['mtb', 'Input & Output Sidebar', 'sort'],
name: 'Default sort mode', name: 'Default sort mode',
@@ -304,7 +372,39 @@ if (window?.__COMFYUI_FRONTEND_VERSION__) {
'Name', 'Name',
'Name-Reverse', 'Name-Reverse',
], ],
}) },
{
id: 'mtb.io-sidebar.notice',
category: ['mtb', 'Input & Output Sidebar', 'sort'],
name: ' ',
type: (name, setter, value, attrs) => {
const container = mtb_ui.makeElement('div')
const notice =
'## Important\nIf you make **any** edits here you need to toggle off and back on the sidebar for it to take effect.'
if (window.MTB?.mdParser) {
MTB.mdParser.parse(notice).then((e) => {
container.innerHTML = e
})
} else {
shared.ensureMarkdownParser((p) => {
p.parse(notice).then((e) => {
container.innerHTML = e
})
})
}
return container
},
},
],
init: () => {
let handle
const version = window?.__COMFYUI_FRONTEND_VERSION__
console.log(`%c ${version}`, 'background: orange; color: white;')
ensureMTBStyles()
app.extensionManager.registerSidebarTab({ app.extensionManager.registerSidebarTab({
id: 'mtb-inputs-outputs', id: 'mtb-inputs-outputs',
-28
View File
@@ -1,28 +0,0 @@
// NOTE: this will be the LT part of mtb API system
// I need to properly publish the source and fix a few things before
// import { app } from '../../scripts/app.js'
// // import { api } from '../../scripts/api.js'
//
// import * as shared from './comfy_shared.js'
// import { createOutliner } from './dist/mtb_inspector.js'
//
// if (window?.__COMFYUI_FRONTEND_VERSION__) {
// const version = window?.__COMFYUI_FRONTEND_VERSION__
// console.log(`%c ${version}`, 'background: orange; color: white;')
//
// const panel = app.extensionManager.registerSidebarTab({
// id: 'mtb-nodes',
// icon: 'pi pi-bolt',
// title: 'MTB',
// tooltip: 'MTB: API outliner',
// type: 'custom',
// // this is run everytime the tab's diplay is toggled on.
// render: (el) => {
// const outliner = createOutliner(el)
// const inputs = shared.getAPIInputs()
// console.log('INPUTS', inputs)
// outliner.$$set({ inputs })
// },
// })
// }
+4 -1
View File
@@ -203,7 +203,7 @@ export const wrapElement = (element, style = {}) => {
* @param {Object} [style] - CSS styles to apply to the element. * @param {Object} [style] - CSS styles to apply to the element.
* @returns {HTMLElement} - The created DOM element. * @returns {HTMLElement} - The created DOM element.
*/ */
export const makeElement = (kind, style) => { export const makeElement = (kind, style, parent) => {
let [real_kind, className] = kind.split('.') let [real_kind, className] = kind.split('.')
let id let id
@@ -224,6 +224,9 @@ export const makeElement = (kind, style) => {
if (id) { if (id) {
el.id = id el.id = id
} }
if (parent) {
parent.appendChild(el)
}
return el return el
} }
+49 -9
View File
@@ -21,7 +21,7 @@ import { infoLogger } from './comfy_shared.js'
import { NumberInputWidget } from './numberInput.js' import { NumberInputWidget } from './numberInput.js'
// NOTE: new widget types registered by MTB Widgets // NOTE: new widget types registered by MTB Widgets
const newTypes = [/*'BOOL'*/ 'COLOR', 'BBOX'] const newTypes = [/*'BOOL'*/ 'COLOR','MTB_COLOR', 'BBOX']
const deprecated_nodes = { const deprecated_nodes = {
// 'Animation Builder': // 'Animation Builder':
@@ -694,7 +694,7 @@ const mtb_widgets = {
app.ui.settings.addSetting({ app.ui.settings.addSetting({
id: 'mtb.Main.debug-enabled', id: 'mtb.Main.debug-enabled',
category: ['mtb', 'Main', 'debug-enabled'], category: ['mtb', ' Main', 'debug-enabled'],
name: 'Enable Debug (py and js)', name: 'Enable Debug (py and js)',
type: 'boolean', type: 'boolean',
defaultValue: false, defaultValue: false,
@@ -739,7 +739,6 @@ const mtb_widgets = {
// }, // },
COLOR: (node, inputName, inputData, _app) => { COLOR: (node, inputName, inputData, _app) => {
console.debug('Registering color')
return { return {
widget: node.addCustomWidget( widget: node.addCustomWidget(
MtbWidgets.COLOR(inputName, inputData[1]?.default || '#ff0000'), MtbWidgets.COLOR(inputName, inputData[1]?.default || '#ff0000'),
@@ -748,6 +747,16 @@ const mtb_widgets = {
minHeight: 30, minHeight: 30,
} }
}, },
MTB_COLOR: (node, inputName, inputData, _app) => {
return {
widget: node.addCustomWidget(
MtbWidgets.COLOR(inputName, inputData[1]?.default || '#ff0000'),
),
minWidth: 150,
minHeight: 30,
}
},
// BBOX: (node, inputName, inputData, app) => { // BBOX: (node, inputName, inputData, app) => {
// console.debug("Registering bbox") // console.debug("Registering bbox")
// return { // return {
@@ -1012,12 +1021,15 @@ const mtb_widgets = {
) )
loop_preview.value = 'Iteration: Idle' loop_preview.value = 'Iteration: Idle'
let cancelQueue = false
const onReset = () => { const onReset = () => {
raw_iteration.value = 0 raw_iteration.value = 0
raw_loop.value = 0 raw_loop.value = 0
value_preview.value = 'Idle' value_preview.value = 'Idle'
loop_preview.value = 'Iteration: Idle' loop_preview.value = 'Iteration: Idle'
cancelQueue = false
app.canvas.setDirty(true) app.canvas.setDirty(true)
} }
@@ -1026,15 +1038,43 @@ const mtb_widgets = {
this.addWidget('button', 'Reset', 'reset', onReset) this.addWidget('button', 'Reset', 'reset', onReset)
// run button // run button
this.addWidget('button', 'Queue', 'queue', () => { const chunkSize = 10
onReset() // this could maybe be a setting or checkbox this.addWidget('button', 'Queue', 'queue', async () => {
app.queuePrompt(0, total_frames.value * loop_count.value) onReset()
const totalPrompts = total_frames.value * loop_count.value
window.MTB?.notify?.( window.MTB?.notify?.(
`Started a queue of ${total_frames.value} frames (for ${ `Starting a queue of ${totalPrompts} frames in chunks of ${chunkSize}...`,
loop_count.value
} loop, so ${total_frames.value * loop_count.value})`,
5000, 5000,
) )
for (let i = 0; i < totalPrompts; i += chunkSize) {
console.log({ cancelQueue })
if (cancelQueue) {
window.MTB?.notify?.(
`Queueing cancelled after ${i} frames.`,
3000,
)
break
}
const currentChunkSize = Math.min(chunkSize, totalPrompts - i)
await app.queuePrompt(0, currentChunkSize)
}
if (!cancelQueue) {
window.MTB?.notify?.(
`Finished queuing ${totalPrompts} frames.`,
5000,
)
}
})
this.addWidget('button', 'Cancel', 'cancel', () => {
cancelQueue = true
window.MTB?.notify?.(
'Cancellation requested. Waiting for current chunk to finish...',
3000,
)
}) })
this.onRemoved = () => { this.onRemoved = () => {
+64 -52
View File
@@ -1,10 +1,13 @@
// web/note_plus.constants.js // web/note_plus.constants.js
export const DEFAULT_CSS = '' export const DEFAULT_CSS = `/** here you can write css**/
h1 {
color: whitesmoke;
}`
export const DEFAULT_HTML = `<p style='color:red;font-family:monospace'> export const DEFAULT_HTML = `<p style='color:red;font-family:monospace'>
Note+ Note+
</p>` </p>`
export const DEFAULT_MD = '## Note+' export const DEFAULT_MD = '# 📝 Note+'
export const DEFAULT_MODE = 'markdown' export const DEFAULT_MODE = 'markdown'
export const DEFAULT_THEME = 'one_dark' export const DEFAULT_THEME = 'one_dark'
@@ -55,58 +58,57 @@ We also support github callout:
` `
export const THEMES = [ export const THEMES = [
'ambiance', 'ambiance',
'chaos', 'chaos',
'chrome', 'chrome',
'cloud9_day', 'cloud9_day',
'cloud9_night', 'cloud9_night',
'cloud9_night_low_color', 'cloud9_night_low_color',
'cloud_editor', 'cloud_editor',
'cloud_editor_dark', 'cloud_editor_dark',
'clouds', 'clouds',
'clouds_midnight', 'clouds_midnight',
'cobalt', 'cobalt',
'crimson_editor', 'crimson_editor',
'dawn', 'dawn',
'dracula', 'dracula',
'dreamweaver', 'dreamweaver',
'eclipse', 'eclipse',
'github', 'github',
'github_dark', 'github_dark',
'gob', 'gob',
'gruvbox', 'gruvbox',
'gruvbox_dark_hard', 'gruvbox_dark_hard',
'gruvbox_light_hard', 'gruvbox_light_hard',
'idle_fingers', 'idle_fingers',
'iplastic', 'iplastic',
'katzenmilch', 'katzenmilch',
'kr_theme', 'kr_theme',
'kuroir', 'kuroir',
'merbivore', 'merbivore',
'merbivore_soft', 'merbivore_soft',
'mono_industrial', 'mono_industrial',
'monokai', 'monokai',
'nord_dark', 'nord_dark',
'one_dark', 'one_dark',
'pastel_on_dark', 'pastel_on_dark',
'solarized_dark', 'solarized_dark',
'solarized_light', 'solarized_light',
'sqlserver', 'sqlserver',
'terminal', 'terminal',
'textmate', 'textmate',
'tomorrow', 'tomorrow',
'tomorrow_night', 'tomorrow_night',
'tomorrow_night_blue', 'tomorrow_night_blue',
'tomorrow_night_bright', 'tomorrow_night_bright',
'tomorrow_night_eighties', 'tomorrow_night_eighties',
'twilight', 'twilight',
'vibrant_ink', 'vibrant_ink',
'vscode', 'vscode',
] ]
export const CSS_RESET = ` export const CSS_RESET = `
* { * {
font-family: monospace;
line-height: 1.25em; line-height: 1.25em;
} }
.shiki{ .shiki{
@@ -116,6 +118,8 @@ export const CSS_RESET = `
.markdown-callout-title { .markdown-callout-title {
.octicon{ .octicon{
fill:white; fill:white;
width:29px;
height:29px;
} }
/* background: var(--current-color); */ /* background: var(--current-color); */
color: var(--current-color); color: var(--current-color);
@@ -124,6 +128,8 @@ export const CSS_RESET = `
/* border-start-start-radius: var(--radius); */ /* border-start-start-radius: var(--radius); */
padding: 0.5em; padding: 0.5em;
padding-inline-start: 1em; padding-inline-start: 1em;
display: flex;
align-items: center;
} }
.markdown-callout-content { .markdown-callout-content {
padding: 1em; padding: 1em;
@@ -136,7 +142,12 @@ export const CSS_RESET = `
border-left: 3px solid var(--current-color); border-left: 3px solid var(--current-color);
margin-bottom: 1em; margin-bottom: 1em;
margin-top: 1em; margin-top: 1em;
} }
.markdown-callout p:nth-child(2) {
padding:1em;
}
.markdown-callout-tip { .markdown-callout-tip {
--text-color: whitesmoke; --text-color: whitesmoke;
@@ -164,8 +175,9 @@ export const CSS_RESET = `
flex-direction:column; flex-direction:column;
align-items: flex-start; align-items: flex-start;
width:95%; width:95%;
margin-left: 20px; /*margin-left: 20px;*/
margin-top:20px; /*margin-top:20px;*/
/*background-color: rgba(255,0,0,0.5)!important;*/ /*background-color: rgba(255,0,0,0.5)!important;*/
} }
+379 -336
View File
File diff suppressed because it is too large Load Diff
+12 -3
View File
@@ -41,7 +41,16 @@ const toastStyle = `
transition-duration: ${transition_time}ms; transition-duration: ${transition_time}ms;
` `
function notify(message, timeout = 3000) { function notify(message, timeout = 3000, old_mode = false) {
if (!old_mode) {
app.extensionManager.toast.add({
severity: 'info',
summary: 'MTB',
detail: message,
life: timeout,
})
return
}
log('Creating toast') log('Creating toast')
const container = document.getElementById('mtb-notify-container') const container = document.getElementById('mtb-notify-container')
const toast = document.createElement('div') const toast = document.createElement('div')
@@ -59,7 +68,7 @@ function notify(message, timeout = 3000) {
log('Transition out') log('Transition out')
const totalHeight = Array.from(container.children).reduce( const totalHeight = Array.from(container.children).reduce(
(acc, child) => acc + child.offsetHeight + 10, // Add spacing of 10px between toasts (acc, child) => acc + child.offsetHeight + 10, // Add spacing of 10px between toasts
0 0,
) )
container.style.height = `${totalHeight}px` container.style.height = `${totalHeight}px`
@@ -83,7 +92,7 @@ function notify(message, timeout = 3000) {
// Update container's height to fit new toast // Update container's height to fit new toast
const totalHeight = Array.from(container.children).reduce( const totalHeight = Array.from(container.children).reduce(
(acc, child) => acc + child.offsetHeight + 10, // Add spacing of 10px between toasts (acc, child) => acc + child.offsetHeight + 10, // Add spacing of 10px between toasts
0 0,
) )
container.style.height = `${totalHeight}px` container.style.height = `${totalHeight}px`