Compare commits
90
Commits
v0.1.0
...
dev/psd-nodes
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
625b818d8c | ||
|
|
cbbc2d0705 | ||
|
|
a71c273baf | ||
|
|
49c64c74eb | ||
|
|
2ecd4700d7 | ||
|
|
ea5d73d48c | ||
|
|
30d6cfe812 | ||
|
|
610afe031f | ||
|
|
a4d99d966b | ||
|
|
4fc84d615d | ||
|
|
8523392df7 | ||
|
|
dbdb872b74 | ||
|
|
40560f8154 | ||
|
|
e7f72f9825 | ||
|
|
11444662b9 | ||
|
|
2eccba4e33 | ||
|
|
5ec5511433 | ||
|
|
630b492347 | ||
|
|
4f30829e06 | ||
|
|
414beb99a1 | ||
|
|
3f14b1676d | ||
|
|
9c2e8ac57c | ||
|
|
4dd5321852 | ||
|
|
91f60d4c46 | ||
|
|
fb644847ca | ||
|
|
84ac8ac852 | ||
|
|
63b3aece2b | ||
|
|
a54d7d5346 | ||
|
|
13d255a730 | ||
|
|
2bc7ae88bf | ||
|
|
0fb2d4da90 | ||
|
|
cfb3b237cf | ||
|
|
3d5075fea2 | ||
|
|
098d74a3cd | ||
|
|
e74314b04e | ||
|
|
d4f791d7a1 | ||
|
|
2ff04672da | ||
|
|
b854a302ce | ||
|
|
512de6023e | ||
|
|
c5bbe83008 | ||
|
|
7b3afca817 | ||
|
|
bbfcb62c39 | ||
|
|
a22fd01d66 | ||
|
|
8e5b7765cc | ||
|
|
36d8e6bdb0 | ||
|
|
3dadc119f4 | ||
|
|
ffa1a87b91 | ||
|
|
346ff649d5 | ||
|
|
247fbfbc21 | ||
|
|
9b24eddd9c | ||
|
|
505314294f | ||
|
|
f5cd56ce86 | ||
|
|
cbcacbe3c9 | ||
|
|
7c020bab28 | ||
|
|
9e751a242f | ||
|
|
0e311cf2c6 | ||
|
|
889f08c08b | ||
|
|
5d661b2509 | ||
|
|
be162a2047 | ||
|
|
6d74670556 | ||
|
|
61cbf4c624 | ||
|
|
4ea26ed8de | ||
|
|
cd39fea580 | ||
|
|
c237737420 | ||
|
|
232cf8966c | ||
|
|
96a0618c59 | ||
|
|
d143e83dba | ||
|
|
3dfe98c795 | ||
|
|
c0cc5572d8 | ||
|
|
8695cd3f1b | ||
|
|
cf865529ab | ||
|
|
3b9190a69b | ||
|
|
9a4eda3ef5 | ||
|
|
a2ecc11ebd | ||
|
|
7e9c97ecb4 | ||
|
|
3de160af25 | ||
|
|
3801a443bc | ||
|
|
bbdac97e49 | ||
|
|
50d51c70d0 | ||
|
|
55c9736a9b | ||
|
|
21729b2784 | ||
|
|
8d3cc39b72 | ||
|
|
abf1e82adb | ||
|
|
10d05031b1 | ||
|
|
7142b284ad | ||
|
|
11128ff85a | ||
|
|
a393793cfa | ||
|
|
f77ddbd6a3 | ||
|
|
22b9b94679 | ||
|
|
64b2c72cf4 |
@@ -27,15 +27,15 @@ jobs:
|
||||
steps:
|
||||
- name: ♻️ Checking out the repository
|
||||
uses: actions/checkout@v3
|
||||
- name: "🐍 Setting up Python"
|
||||
- name: '🐍 Setting up Python'
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: "3.10.9"
|
||||
python-version: '3.10.9'
|
||||
|
||||
- name: 📦 Building and Bundling wheels
|
||||
shell: bash
|
||||
run: |
|
||||
python -m pip wheel --no-cache-dir --no-deps -r requirements-wheels.txt -w ./wheels 2>&1 | tee build.log
|
||||
python -m pip wheel --no-cache-dir -r reqs.txt -w ./wheels 2>&1 | tee build.log
|
||||
|
||||
# find source wheels
|
||||
packages=$(cat build.log | awk -F 'Building wheels for collected packages: ' '{print $2}')
|
||||
@@ -43,6 +43,13 @@ jobs:
|
||||
|
||||
IFS=', ' read -r -a package_array <<< "$packages"
|
||||
|
||||
# Save reversed package_array to wheel_order.txt
|
||||
reversed_array=()
|
||||
for ((idx=${#package_array[@]}-1; idx>=0; idx--)); do
|
||||
reversed_array+=("${package_array[idx]}")
|
||||
done
|
||||
printf '%s\n' "${reversed_array[@]}" > ./wheels/wheel_order.txt
|
||||
|
||||
printf "Autodetect this source package: \e[32m%s\e[0m\n" "${package_array[@]}"
|
||||
|
||||
# Iterate through the wheel files and remove those that are not source built
|
||||
@@ -69,4 +76,4 @@ jobs:
|
||||
uses: actions/cache/save@v3
|
||||
with:
|
||||
path: ${{ env.archive_name }}.zip
|
||||
key: ${{ env.archive_name }}
|
||||
key: ${{ env.archive_name }}-${{ hashFiles('reqs.txt') }}
|
||||
|
||||
@@ -6,7 +6,7 @@ on:
|
||||
name:
|
||||
description: Release tag / name ?
|
||||
required: true
|
||||
default: "latest"
|
||||
default: 'latest'
|
||||
type: string
|
||||
environment:
|
||||
description: Environment to run tests against
|
||||
@@ -27,8 +27,36 @@ jobs:
|
||||
- name: ♻️ Checking out the repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: "recursive"
|
||||
submodules: 'recursive'
|
||||
path: ${{ env.repo_name }}
|
||||
|
||||
# - name: 📝 Prepare file with paths to remove
|
||||
# run: |
|
||||
# find ${{ env.repo_name }} -type f -size +10M > .release_ignore
|
||||
# find ${{ env.repo_name }} -type d -empty >> .release_ignore
|
||||
# shell: bash
|
||||
|
||||
- name: 🗑️ Remove files and directories listed in .release_ignore
|
||||
shell: bash
|
||||
run: |
|
||||
release_ignore="${{ env.repo_name }}/.release_ignore"
|
||||
if [ -f "$release_ignore" ]; then
|
||||
while IFS= read -r entry || [ -n "$entry" ]; do
|
||||
target="${{ env.repo_name }}/$entry"
|
||||
if [ -e "$target" ]; then
|
||||
if [ -f "$target" ]; then
|
||||
rm "$target"
|
||||
elif [ -d "$target" ]; then
|
||||
rm -r "$target"
|
||||
fi
|
||||
else
|
||||
echo "Warning: $entry does not exist in the repository. Skipping removal."
|
||||
fi
|
||||
done < "$release_ignore"
|
||||
else
|
||||
echo "No .release_ignore file found. Skipping removal of files and directories."
|
||||
fi
|
||||
|
||||
- name: 📦 Building custom comfy nodes
|
||||
shell: bash
|
||||
run: |
|
||||
@@ -70,10 +98,18 @@ jobs:
|
||||
id: cache
|
||||
with:
|
||||
path: ${{ env.archive_name }}.zip
|
||||
key: ${{ env.archive_name }}
|
||||
key: ${{ env.archive_name }}-${{ hashFiles('reqs.txt') }}
|
||||
- name: 📦 Unzip wheels
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p wheels
|
||||
unzip -j ${{ env.archive_name }}.zip "**/*.whl" -d wheels
|
||||
unzip -j ${{ env.archive_name }}.zip "**/*.txt" -d wheels
|
||||
if: success()
|
||||
- name: ✅ Add wheels to release
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
tag_name: ${{ inputs.name }}
|
||||
files: |
|
||||
${{ env.archive_name }}.zip
|
||||
wheels/*.whl
|
||||
wheels/wheel_order.txt
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
name: 🧪 Test Comfy Portable
|
||||
|
||||
on: workflow_dispatch
|
||||
jobs:
|
||||
install-comfy:
|
||||
runs-on: windows-latest
|
||||
env:
|
||||
repo_name: ${{ github.event.repository.name }}
|
||||
steps:
|
||||
- name: ⚡️ Restore Cache if Available
|
||||
id: cache-comfy
|
||||
uses: actions/cache/restore@v3
|
||||
with:
|
||||
path: ComfyUI_windows_portable
|
||||
key: ${{ runner.os }}-comfy-env
|
||||
|
||||
- name: 🚡 Download and Extract Comfy
|
||||
id: download-extract-comfy
|
||||
if: steps.cache-comfy.outputs.cache-hit != 'true'
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir comfy_temp
|
||||
curl -L -o comfy_temp/comfyui.7z https://github.com/comfyanonymous/ComfyUI/releases/download/latest/ComfyUI_windows_portable_nvidia_cu118_or_cpu.7z
|
||||
|
||||
7z x comfy_temp/comfyui.7z -o./comfy_temp
|
||||
|
||||
|
||||
# mv comfy_temp/ComfyUI_windows_portable/python_embeded .
|
||||
# mv comfy_temp/ComfyUI_windows_portable/ComfyUI .
|
||||
# mv comfy_temp/ComfyUI_windows_portable/update .
|
||||
ls
|
||||
mv comfy_temp/ComfyUI_windows_portable .
|
||||
|
||||
- name: 💾 Store cache
|
||||
uses: actions/cache/save@v3
|
||||
if: steps.cache-comfy.outputs.cache-hit != 'true'
|
||||
with:
|
||||
path: ComfyUI_windows_portable
|
||||
key: ${{ runner.os }}-comfy-env
|
||||
- name: ⏬ Install other extensions
|
||||
shell: bash
|
||||
run: |
|
||||
export COMFY_PYTHON="${GITHUB_WORKSPACE}/ComfyUI_windows_portable/python_embeded/python.exe"
|
||||
cd "${GITHUB_WORKSPACE}/ComfyUI_windows_portable/ComfyUI/custom_nodes"
|
||||
|
||||
git clone https://github.com/Fannovel16/comfy_controlnet_preprocessors
|
||||
cd comfy_controlnet_preprocessors
|
||||
$COMFY_PYTHON -m pip install -r requirements.txt
|
||||
|
||||
- name: ♻️ Checking out comfy_mtb to custom_nodes
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: 'recursive'
|
||||
path: ComfyUI_windows_portable/ComfyUI/custom_nodes/${{ env.repo_name }}
|
||||
|
||||
- name: 📦 Install mtb nodes
|
||||
shell: bash
|
||||
run: |
|
||||
# run install
|
||||
export COMFY_PYTHON="${GITHUB_WORKSPACE}/ComfyUI_windows_portable/python_embeded/python.exe"
|
||||
cd "${GITHUB_WORKSPACE}/ComfyUI_windows_portable/ComfyUI/custom_nodes"
|
||||
$COMFY_PYTHON ${{ env.repo_name }}/install.py -w
|
||||
|
||||
- name: ⏬ Import mtb_nodes
|
||||
shell: bash
|
||||
run: |
|
||||
export COMFY_PYTHON="${GITHUB_WORKSPACE}/ComfyUI_windows_portable/python_embeded/python.exe"
|
||||
cd "${GITHUB_WORKSPACE}/ComfyUI_windows_portable/ComfyUI"
|
||||
$COMFY_PYTHON -s main.py --quick-test-for-ci --cpu
|
||||
|
||||
$COMFY_PYTHON -m pip freeze
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 2,
|
||||
"useTabs": false
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
extern/frame_interpolation/moment.gif
|
||||
extern/frame_interpolation/photos
|
||||
extern/GFPGAN/inputs
|
||||
.git
|
||||
+2
-2
@@ -49,7 +49,7 @@ python scripts/download_models.py
|
||||
1. 确保您处于用于 ComfyUI 的 Python 环境中。
|
||||
2. 运行以下命令安装所需的依赖项:
|
||||
```bash
|
||||
pip install -r comfy_mtb/requirements.txt
|
||||
pip install -r comfy_mtb/reqs.txt
|
||||
```
|
||||
|
||||
</details>
|
||||
@@ -77,7 +77,7 @@ python scripts/download_models.py
|
||||
!python custom_nodes/comfy_mtb/scripts/download_models.py -y
|
||||
|
||||
# install the dependencies
|
||||
!pip install -r custom_nodes/comfy_mtb/requirements.txt -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html
|
||||
!pip install -r custom_nodes/comfy_mtb/reqs.txt -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html
|
||||
```
|
||||
|
||||
如果运行后 colab 抱怨需要重新启动运行时,请重新启动,然后不要重新运行之前的单元格,只运行运行本地隧道的单元格。(可能需要先添加一个包含 `%cd ComfyUI` 的单元格)
|
||||
|
||||
+2
-2
@@ -52,7 +52,7 @@ python scripts/download_models.py
|
||||
1. ComfyUIで使用しているPython環境であることを確認してください。
|
||||
2. 以下のコマンドを実行して、必要な依存関係をインストールします:
|
||||
```bash
|
||||
pip install -r comfy_mtb/requirements.txt
|
||||
pip install -r comfy_mtb/reqs.txt
|
||||
```
|
||||
|
||||
</details>
|
||||
@@ -78,7 +78,7 @@ ComfyUI with localtunnel (Recommended Way)**ヘッダーのすぐ後(コード
|
||||
!python custom_nodes/comfy_mtb/scripts/download_models.py -y
|
||||
|
||||
# install the dependencies
|
||||
!pip install -r custom_nodes/comfy_mtb/requirements.txt -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html
|
||||
!pip install -r custom_nodes/comfy_mtb/reqs.txt -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html
|
||||
```
|
||||
これを実行した後、colabがランタイムを再起動する必要があると文句を言ったら、それを実行し、それ以前のセルは再実行せず、localtunnelを実行するセルだけを再実行してください。(最初に`%cd ComfyUI`のセルを追加する必要があるかもしれません...)
|
||||
|
||||
|
||||
+2
-2
@@ -48,7 +48,7 @@ On first run the script [tries to symlink](https://github.com/melMass/comfy_mtb/
|
||||
1. Make sure you are in the Python environment you use for ComfyUI.
|
||||
2. Install the required dependencies by running the following command:
|
||||
```bash
|
||||
pip install -r comfy_mtb/requirements.txt
|
||||
pip install -r comfy_mtb/reqs.txt
|
||||
```
|
||||
|
||||
</details>
|
||||
@@ -76,7 +76,7 @@ Add a new code cell just after the **Run ComfyUI with localtunnel (Recommended W
|
||||
!python custom_nodes/comfy_mtb/scripts/download_models.py -y
|
||||
|
||||
# install the dependencies
|
||||
!pip install -r custom_nodes/comfy_mtb/requirements.txt -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html
|
||||
!pip install -r custom_nodes/comfy_mtb/reqs.txt -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html
|
||||
```
|
||||
If after running this, colab complains about needing to restart runtime, do it, and then do not rerun earlier cells, just the one to run the localtunnel. (you might have to add a cell with `%cd ComfyUI` first...)
|
||||
|
||||
|
||||
+14
-17
@@ -1,13 +1,13 @@
|
||||
## MTB Nodes
|
||||
# MTB Nodes
|
||||
|
||||
<a href="https://www.buymeacoffee.com/melmass" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 32px !important;width: 140px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
||||
|
||||
[** 安装指南**](./INSTALL-CN.md) | [** 示例**](https://github.com/melMass/comfy_mtb/wiki/Examples)
|
||||
|
||||
欢迎使用 MTB Nodes 项目!这个代码库是开放的,您可以自由地探索和利用。它的主要目的是构建用于 [MLOPs](https://github.com/Bismuth-Consultancy-BV/MLOPs) 中的概念验证(POCs)。该项目中的许多节点都是受到现有社区贡献或内置功能的启发而创建的。
|
||||
|
||||
在继续之前,请注意与此项目中使用的某些库相关的许可证。例如,`deepbump` 库采用 [GPLv3](https://github.com/HugoTini/DeepBump/blob/master/LICENSE) 许可证。
|
||||
|
||||
- [MTB Nodes](#mtb-nodes)
|
||||
- [安装](#安装)
|
||||
- [节点列表](#节点列表)
|
||||
- [bbox](#bbox)
|
||||
- [colors](#colors)
|
||||
@@ -20,27 +20,24 @@
|
||||
- [Comfy 资源](#comfy-资源)
|
||||
|
||||
|
||||
## 安装
|
||||
|
||||
- 移至 [INSTALL-CN.md](./INSTALL-CN.md)
|
||||
|
||||
|
||||
## 节点列表
|
||||
# 节点列表
|
||||
|
||||
### bbox
|
||||
## bbox
|
||||
- `Bounding Box`: BBox 构造函数(自定义类型)
|
||||
- `BBox From Mask`: 从遮罩中提取边界框
|
||||
- `Crop`: 根据边界框裁剪图像
|
||||
- `Uncrop`: 根据边界框还原图像
|
||||
|
||||
### colors
|
||||
## colors
|
||||
- `Colored Image`: 给定尺寸的纯色图像
|
||||
- `RGB to HSV`: -
|
||||
- `HSV to RGB`: -
|
||||
- `Color Correct`: 基本颜色校正工具
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/7c20ac83-31ff-40ea-a1a0-06c2acefb2ef" width=345/>
|
||||
|
||||
### 人脸检测/交换
|
||||
## 人脸检测/交换
|
||||
- `Face Swap`: 使用 deepinsight/insightface 模型进行人脸交换(该节点在早期版本中称为 `Roop`,功能相同,`Roop` 只是使用这些模型的应用程序)
|
||||
> **注意**
|
||||
> 人脸索引允许您选择要替换的人脸,如下所示:
|
||||
@@ -48,13 +45,13 @@
|
||||
- `Load Face Swap Model`: 加载 insightface 模型用于人脸交换
|
||||
- `Restore Face`: 使用 [GFPGan](https://github.com/TencentARC/GFPGAN) 还原人脸,与 `Face Swap` 配合使用效果很好,并支持 `bg_upscaler` 的 Comfy 原生放大器
|
||||
|
||||
### 图像插值(动画)
|
||||
## 图像插值(动画)
|
||||
- `Load Film Model`: 加载 [FILM](https://github.com/google-research/frame-interpolation) 模型
|
||||
- `Film Interpolation`: 使用 [FILM](https://github.com/google-research/frame-interpolation) 处理输入帧
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/3afd1647-6634-4b92-a34b-51432e6a9834" width=400/>
|
||||
- `Export to Prores (experimental)`: 将输入帧导出为 ProRes 4444 mov 文件。这使用 ffmpeg stdin 发送原始的 NumPy 数组,与 `Film Interpolation` 一起使用,目前很简单,但可以进一步扩展。
|
||||
|
||||
### 图像操作
|
||||
## 图像操作
|
||||
- `Blur`: 使用高斯滤波器对图像进行模糊处理。
|
||||
- `Deglaze Image`: 从 [FN16](https://github.com/Fannovel16/FN16-ComfyUI-nodes/blob/main/DeglazeImage.py) 中提取
|
||||
- `Denoise`: 对输入图像进行降噪处理
|
||||
@@ -66,11 +63,11 @@
|
||||
- `Mask To Image`: 将遮罩(Alpha)转换为带有颜色和背景的 RGB 图像
|
||||
- `Save Image Grid`: 将输入批次中的所有图像保存为图像网格。
|
||||
|
||||
### 潜在变量工具
|
||||
## 潜在变量工具
|
||||
- `Latent Lerp`: 两个潜在变量之间的线性插值(混合)
|
||||
|
||||
|
||||
### 其他工具
|
||||
## 其他工具
|
||||
- `Concat Images`: 接受两个图像流,并将它们合并为其他 Comfy 管道支持的图像批次。
|
||||
- `Image Resize Factor`: **已弃用**,因为我后来发现了内
|
||||
|
||||
@@ -84,11 +81,11 @@
|
||||
- `Int to Number`: 用于 WASSuite 数字节点的补充
|
||||
- `Smart Step`: 使用百分比来控制 `KAdvancedSampler` 的步骤(开始/停止)
|
||||
|
||||
### 纹理
|
||||
## 纹理
|
||||
|
||||
- `DeepBump`: 从单张图片生成法线图和高度图
|
||||
|
||||
## Comfy 资源
|
||||
# Comfy 资源
|
||||
|
||||
**指南**:
|
||||
- [官方示例(英文)](https://comfyanonymous.github.io/ComfyUI_examples/)
|
||||
@@ -99,4 +96,4 @@
|
||||
**扩展和自定义节点**:
|
||||
- @WASasquatch 的[Comfy 列表插件(英文)](https://github.com/WASasquatch/comfyui-plugins)
|
||||
|
||||
- [CivitAI 上的 ComfyUI 标签(英文)](https://civitai.com/tag/comfyui)
|
||||
- [CivitAI 上的 ComfyUI 标签(英文)](https://civitai.com/tag/comfyui)
|
||||
|
||||
+14
-19
@@ -1,13 +1,13 @@
|
||||
## MTB Nodes
|
||||
# MTB Nodes
|
||||
|
||||
<a href="https://www.buymeacoffee.com/melmass" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 32px !important;width: 140px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
||||
|
||||
[**インストールガイド**](./INSTALL-JP.md) | [**サンプル**](https://github.com/melMass/comfy_mtb/wiki/Examples)
|
||||
|
||||
MTB Nodesプロジェクトへようこそ!このコードベースは、自由に探索し、利用することができます。主な目的は、[MLOPs](https://github.com/Bismuth-Consultancy-BV/MLOPs)の実装のための概念実証(POC)を構築することです。このプロジェクトの多くのノードは、既存のコミュニティの貢献や組み込みの機能に触発されています。
|
||||
|
||||
続行する前に、このプロジェクトで使用されている特定のライブラリに関連するライセンスに注意してください。たとえば、「deepbump」ライブラリは、[GPLv3](https://github.com/HugoTini/DeepBump/blob/master/LICENSE)の下でライセンスされています。
|
||||
|
||||
- [MTB Nodes](#mtb-nodes)
|
||||
- [インストール](#インストール)
|
||||
- [ノードリスト](#ノードリスト)
|
||||
- [bbox](#bbox)
|
||||
- [colors](#colors)
|
||||
@@ -20,27 +20,22 @@ MTB Nodesプロジェクトへようこそ!このコードベースは、自
|
||||
- [Comfyリソース](#comfyリソース)
|
||||
|
||||
|
||||
## インストール
|
||||
# ノードリスト
|
||||
|
||||
- [INSTALL-JP.md](./INSTALL-JP.md)に移動しました。
|
||||
|
||||
|
||||
## ノードリスト
|
||||
|
||||
### bbox
|
||||
## bbox
|
||||
- `Bounding Box`: BBoxコンストラクタ(カスタムタイプ)
|
||||
- `BBox From Mask`: マスクからバウンディングボックスを抽出
|
||||
- `Crop`: BBoxから画像を切り抜く
|
||||
- `Uncrop`: BBoxから画像を元に戻す
|
||||
|
||||
### colors
|
||||
## colors
|
||||
- `Colored Image`: 指定されたサイズの一定の色の画像
|
||||
- `RGB to HSV`: -
|
||||
- `HSV to RGB`: -
|
||||
- `Color Correct`: 基本的なカラーコレクションツール
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/7c20ac83-31ff-40ea-a1a0-06c2acefb2ef" width=345/>
|
||||
|
||||
### 顔検出 / スワッピング
|
||||
## 顔検出 / スワッピング
|
||||
- `Face Swap`: deepinsight/insightfaceモデルを使用した顔の入れ替え(このノードは初期バージョンでは「Roop」と呼ばれていましたが、同じ機能を提供します。Roopは単にこれらのモデルを使用するアプリです)
|
||||
> **注意**
|
||||
> 顔のインデックスを使用して置き換える顔を選択できます。以下を参照してください:
|
||||
@@ -48,13 +43,13 @@ MTB Nodesプロジェクトへようこそ!このコードベースは、自
|
||||
- `Load Face Swap Model`: 顔の交換のためのinsightfaceモデルを読み込む
|
||||
- `Restore Face`: [GFPGan](https://github.com/TencentARC/GFPGAN)を使用して顔を復元し、`Face Swap`と組み合わせて使用すると非常に効果的であり、`bg_upscaler`のComfyネイティブアップスケーラーもサポートしています。
|
||||
|
||||
### 画像補間(アニメーション)
|
||||
## 画像補間(アニメーション)
|
||||
- `Load Film Model`: [FILM](https://github.com/google-research/frame-interpolation)モデルを読み込む
|
||||
- `Film Interpolation`: [FILM](https://github.com/google-research/frame-interpolation)を使用して入力フレームを処理する
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/3afd1647-6634-4b92-a34b-51432e6a9834" width=400/>
|
||||
- `Export to Prores (experimental)`: 入力フレームをProRes 4444 movファイルにエクスポートします。これは現在は単純なものですが、`Film Interpolation`と組み合わせて使用するためのffmpegのstdinを使用して生のNumPy配列を送信するもので、拡張することもできます。
|
||||
|
||||
### 画像操作
|
||||
## 画像操作
|
||||
- `Blur`: ガウスフィルタを使用して画像をぼかす
|
||||
- `Deglaze Image`: [FN16](https://github.com/Fannovel16/FN16-ComfyUI-nodes/blob/main/DeglazeImage.py)から取得
|
||||
- `Denoise`: 入力画像のノイズを除去する
|
||||
@@ -68,10 +63,10 @@ MTB Nodesプロジェクトへようこそ!このコードベースは、自
|
||||
- `Mask To Image`: マスク(アルファ)をカラーと背景を持つRGBイメージに変換します。
|
||||
- `Save Image Grid`: 入力バッチのすべての画像を画像グリッドとして保存します。
|
||||
|
||||
### 潜在的なユーティリティ
|
||||
## 潜在的なユーティリティ
|
||||
- `Latent Lerp`: 2つの潜在的なベクトルの間の線形補間(ブレンド)
|
||||
|
||||
### その他のユーティリティ
|
||||
## その他のユーティリティ
|
||||
- `Concat Images`: 2つの画像ストリームを取り、他のComfyパイプラインでサポートされている画像のバッチとしてマージします。
|
||||
- `Image Resize Factor`: **非推奨**。組み込みの画像リサイズ機能を発見したため、削除される予定です。
|
||||
- `Text To Image`: フォントを使用してテキストを画像に変換するためのユーティリティ
|
||||
@@ -83,11 +78,11 @@ MTB Nodesプロジェクトへようこそ!このコードベースは、自
|
||||
- `Int to Number`: WASSuiteの数値ノードの補完
|
||||
- `Smart Step`: `KAdvancedSampler`のステップ(開始/停止)を制御するための非常に基本的なツールで、パーセンテージを使用します。
|
||||
|
||||
### テクスチャ
|
||||
## テクスチャ
|
||||
|
||||
- `DeepBump`: 1枚の画像から法線マップと高さマップを生成します。
|
||||
|
||||
## Comfyリソース
|
||||
# Comfyリソース
|
||||
|
||||
**ガイド**:
|
||||
- [公式の例(英語)](https://comfyanonymous.github.io/ComfyUI_examples/)
|
||||
@@ -98,4 +93,4 @@ MTB Nodesプロジェクトへようこそ!このコードベースは、自
|
||||
**拡張機能とカスタムノード**:
|
||||
- @WASasquatchによる[Comfyリスト用のプラグイン(英語)](https://github.com/WASasquatch/comfyui-plugins)
|
||||
|
||||
- [CivitAIのComfyUIタグ(英語)](https://civitai.com/tag/comfyui)
|
||||
- [CivitAIのComfyUIタグ(英語)](https://civitai.com/tag/comfyui)
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
## MTB Nodes
|
||||
# MTB Nodes
|
||||
|
||||
[](https://github.com/melMass/comfy_mtb/actions/workflows/test_embedded.yml)
|
||||
|
||||
<!-- omit in toc -->
|
||||
|
||||
**Translated Readme (using DeepTranslate, PRs are welcome)**:
|
||||

|
||||
[日本語による説明](./README-JP.md)
|
||||

|
||||
[中文说明](./README-CN.md)
|
||||
|
||||
<a href="https://www.buymeacoffee.com/melmass" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 32px !important;width: 140px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
||||
|
||||
[**Install Guide**](./INSTALL.md) | [**Examples**](https://github.com/melMass/comfy_mtb/wiki/Examples)
|
||||
|
||||
Welcome to the MTB Nodes project! This codebase is open for you to explore and utilize as you wish. Its primary purpose is to build proof-of-concepts (POCs) for implementation in [MLOPs](https://github.com/Bismuth-Consultancy-BV/MLOPs). Many nodes in this project are inspired by existing community contributions or built-in functionalities.
|
||||
|
||||
Before proceeding, please be aware of the licenses associated with certain libraries used in this project. For example, the `deepbump` library is licensed under [GPLv3](https://github.com/HugoTini/DeepBump/blob/master/LICENSE).
|
||||
|
||||
- [MTB Nodes](#mtb-nodes)
|
||||
- [Installation](#installation)
|
||||
- [Node List](#node-list)
|
||||
- [bbox](#bbox)
|
||||
- [colors](#colors)
|
||||
@@ -20,27 +30,22 @@ Before proceeding, please be aware of the licenses associated with certain libra
|
||||
- [Comfy Resources](#comfy-resources)
|
||||
|
||||
|
||||
## Installation
|
||||
# Node List
|
||||
|
||||
- Moved to [INSTALL.md](./INSTALL.md)
|
||||
|
||||
|
||||
## Node List
|
||||
|
||||
### bbox
|
||||
## bbox
|
||||
- `Bounding Box`: BBox constructor (custom type),
|
||||
- `BBox From Mask`: From a mask extract the bounding box
|
||||
- `Crop`: Crop image from BBox
|
||||
- `Uncrop`: Uncrop image from BBox
|
||||
|
||||
### colors
|
||||
## colors
|
||||
- `Colored Image`: Constant color image of given size
|
||||
- `RGB to HSV`: -,
|
||||
- `HSV to RGB`: -,
|
||||
- `Color Correct`: Basic color correction tools
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/7c20ac83-31ff-40ea-a1a0-06c2acefb2ef" width=345/>
|
||||
|
||||
### face detection / swapping
|
||||
## face detection / swapping
|
||||
- `Face Swap`: Face swap using deepinsight/insightface models (this node used to be called `Roop` in early versions, it does the same, roop is *just* an app that uses those model)
|
||||
> **Note**
|
||||
> The face index allow you to choose which face to replace as you can see here:
|
||||
@@ -48,13 +53,13 @@ Before proceeding, please be aware of the licenses associated with certain libra
|
||||
- `Load Face Swap Model`: Load an insightface model for face swapping
|
||||
- `Restore Face`: Using [GFPGan](https://github.com/TencentARC/GFPGAN) to restore faces, works great in conjunction with `Face Swap` and supports Comfy native upscalers for the `bg_upscaler`
|
||||
|
||||
### image interpolation (animation)
|
||||
## image interpolation (animation)
|
||||
- `Load Film Model`: Loads a [FILM](https://github.com/google-research/frame-interpolation) model
|
||||
- `Film Interpolation`: Process input frames using [FILM](https://github.com/google-research/frame-interpolation)
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/3afd1647-6634-4b92-a34b-51432e6a9834" width=400/>
|
||||
- `Export to Prores (experimental)`: Exports the input frames to a ProRes 4444 mov file. This is using ffmpeg stdin to send raw numpy arrays, used with `Film Interpolation` and very simple for now but could be expanded upon.
|
||||
|
||||
### image ops
|
||||
## image ops
|
||||
- `Blur`: Blur an image using a Gaussian filter.
|
||||
- `Deglaze Image`: taken from [FN16](https://github.com/Fannovel16/FN16-ComfyUI-nodes/blob/main/DeglazeImage.py),
|
||||
- `Denoise`: Denoise the input image,
|
||||
@@ -66,11 +71,11 @@ Before proceeding, please be aware of the licenses associated with certain libra
|
||||
- `Mask To Image`: Converts a mask (alpha) to an RGB image with a color and background
|
||||
- `Save Image Grid`: Save all the images in the input batch as a grid of images.
|
||||
|
||||
### latent utils
|
||||
## latent utils
|
||||
- `Latent Lerp`: Linear interpolation (blend) between two latent
|
||||
|
||||
|
||||
### misc utils
|
||||
## misc utils
|
||||
- `Concat Images`: Takes two image stream and merge them as a batch of images supported by other Comfy pipelines.
|
||||
- `Image Resize Factor`: **Deprecated**, I since discovered the builtin image resize.
|
||||
- `Text To Image`: Utils to convert text to image using a font
|
||||
@@ -82,11 +87,11 @@ Before proceeding, please be aware of the licenses associated with certain libra
|
||||
- `Int to Number`: Supplement for WASSuite number nodes
|
||||
- `Smart Step`: A very basic tool to control the steps (start/stop) of the `KAdvancedSampler` using percentage
|
||||
|
||||
### textures
|
||||
## textures
|
||||
|
||||
- `DeepBump`: Normal & height maps generation from single pictures
|
||||
|
||||
## Comfy Resources
|
||||
# Comfy Resources
|
||||
|
||||
**Guides**:
|
||||
- [Official Examples (eng)](https://comfyanonymous.github.io/ComfyUI_examples/)
|
||||
|
||||
+146
-78
@@ -1,10 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
###
|
||||
# File: __init__.py
|
||||
# Project: comfy_mtb
|
||||
# Author: Mel Massadian
|
||||
# Copyright (c) 2023 Mel Massadian
|
||||
#
|
||||
###
|
||||
import os
|
||||
|
||||
# todo: don't override this if the user has that setup already
|
||||
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
|
||||
os.environ["TF_GPU_ALLOCATOR"] = "cuda_malloc_async"
|
||||
|
||||
import traceback
|
||||
from .log import log, blue_text, cyan_text, get_summary, get_label
|
||||
from .utils import here
|
||||
from .utils import comfy_dir
|
||||
import importlib
|
||||
import os
|
||||
import ast
|
||||
@@ -14,7 +26,7 @@ NODE_CLASS_MAPPINGS = {}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {}
|
||||
NODE_CLASS_MAPPINGS_DEBUG = {}
|
||||
|
||||
__version__ = "0.1.0"
|
||||
__version__ = "0.1.4"
|
||||
|
||||
|
||||
def extract_nodes_from_source(filename):
|
||||
@@ -33,19 +45,15 @@ def extract_nodes_from_source(filename):
|
||||
if isinstance(target, ast.Name) and target.id == "__nodes__":
|
||||
value = ast.get_source_segment(source_code, node.value)
|
||||
node_value = ast.parse(value).body[0].value
|
||||
if isinstance(node_value, ast.List) or isinstance(
|
||||
node_value, ast.Tuple
|
||||
):
|
||||
for element in node_value.elts:
|
||||
if isinstance(element, ast.Name):
|
||||
print(element.id)
|
||||
nodes.append(element.id)
|
||||
|
||||
if isinstance(node_value, (ast.List, ast.Tuple)):
|
||||
nodes.extend(
|
||||
element.id
|
||||
for element in node_value.elts
|
||||
if isinstance(element, ast.Name)
|
||||
)
|
||||
break
|
||||
except SyntaxError:
|
||||
log.error("Failed to parse")
|
||||
pass # File couldn't be parsed
|
||||
|
||||
return nodes
|
||||
|
||||
|
||||
@@ -89,7 +97,7 @@ def load_nodes():
|
||||
|
||||
|
||||
# - REGISTER WEB EXTENSIONS
|
||||
web_extensions_root = utils.comfy_dir / "web" / "extensions"
|
||||
web_extensions_root = comfy_dir / "web" / "extensions"
|
||||
web_mtb = web_extensions_root / "mtb"
|
||||
|
||||
if web_mtb.exists():
|
||||
@@ -102,8 +110,16 @@ if web_mtb.exists():
|
||||
|
||||
elif web_extensions_root.exists():
|
||||
web_tgt = here / "web"
|
||||
src = web_tgt.as_posix()
|
||||
dst = web_mtb.as_posix()
|
||||
try:
|
||||
os.symlink(web_tgt.as_posix(), web_mtb.as_posix())
|
||||
if os.name == "nt":
|
||||
import _winapi
|
||||
|
||||
_winapi.CreateJunction(src, dst)
|
||||
else:
|
||||
os.symlink(web_tgt.as_posix(), web_mtb.as_posix())
|
||||
|
||||
except OSError:
|
||||
log.warn(f"Failed to create symlink to {web_mtb}, trying to copy it")
|
||||
try:
|
||||
@@ -111,15 +127,17 @@ elif web_extensions_root.exists():
|
||||
|
||||
shutil.copytree(web_tgt, web_mtb)
|
||||
log.info(f"Successfully copied {web_tgt} to {web_mtb}")
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
log.warn(
|
||||
f"Failed to symlink and copy {web_tgt} to {web_mtb}. Please copy the folder manually."
|
||||
)
|
||||
log.warn(e)
|
||||
|
||||
except Exception: # OSError
|
||||
except Exception as e:
|
||||
log.warn(
|
||||
f"Failed to create symlink to {web_mtb}. Please copy the folder manually."
|
||||
)
|
||||
log.warn(e)
|
||||
else:
|
||||
log.warn(
|
||||
f"Comfy root probably not found automatically, please copy the folder {web_mtb} manually in the web/extensions folder of ComfyUI"
|
||||
@@ -158,88 +176,138 @@ log.info(
|
||||
|
||||
# - ENDPOINT
|
||||
from server import PromptServer
|
||||
from .log import mklog, log
|
||||
from .log import log
|
||||
from aiohttp import web
|
||||
from importlib import reload
|
||||
import logging
|
||||
from .endpoint import endlog
|
||||
|
||||
endlog = mklog("endpoint")
|
||||
if hasattr(PromptServer, "instance"):
|
||||
restore_deps = ["basicsr"]
|
||||
swap_deps = ["insightface", "onnxruntime"]
|
||||
|
||||
node_dependency_mapping = {
|
||||
"FaceSwap": swap_deps,
|
||||
"LoadFaceSwapModel": swap_deps,
|
||||
"LoadFaceAnalysisModel": restore_deps,
|
||||
}
|
||||
|
||||
@PromptServer.instance.routes.get("/mtb/status")
|
||||
async def get_full_library(request):
|
||||
files = []
|
||||
endlog.debug("Getting status")
|
||||
return web.json_response(
|
||||
{
|
||||
"registered": NODE_CLASS_MAPPINGS_DEBUG,
|
||||
"failed": failed,
|
||||
}
|
||||
)
|
||||
@PromptServer.instance.routes.get("/mtb/status")
|
||||
async def get_full_library(request):
|
||||
from . import endpoint
|
||||
|
||||
reload(endpoint)
|
||||
|
||||
@PromptServer.instance.routes.post("/mtb/debug")
|
||||
async def set_debug(request):
|
||||
json_data = await request.json()
|
||||
enabled = json_data.get("enabled")
|
||||
if enabled:
|
||||
os.environ["MTB_DEBUG"] = "true"
|
||||
log.setLevel(logging.DEBUG)
|
||||
log.debug("Debug mode set")
|
||||
endlog.debug("Getting node registration status")
|
||||
# Check if the request prefers HTML content
|
||||
if "text/html" in request.headers.get("Accept", ""):
|
||||
# # Return an HTML page
|
||||
html_response = endpoint.render_table(
|
||||
NODE_CLASS_MAPPINGS_DEBUG, title="Registered"
|
||||
)
|
||||
html_response += endpoint.render_table(
|
||||
{
|
||||
k: {"dependencies": node_dependency_mapping.get(k)}
|
||||
if node_dependency_mapping.get(k)
|
||||
else "-"
|
||||
for k in failed
|
||||
},
|
||||
title="Failed to load",
|
||||
)
|
||||
|
||||
else:
|
||||
if "MTB_DEBUG" in os.environ:
|
||||
return web.Response(
|
||||
text=endpoint.render_base_template("MTB", html_response),
|
||||
content_type="text/html",
|
||||
)
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
"registered": NODE_CLASS_MAPPINGS_DEBUG,
|
||||
"failed": failed,
|
||||
}
|
||||
)
|
||||
|
||||
@PromptServer.instance.routes.post("/mtb/debug")
|
||||
async def set_debug(request):
|
||||
json_data = await request.json()
|
||||
enabled = json_data.get("enabled")
|
||||
if enabled:
|
||||
os.environ["MTB_DEBUG"] = "true"
|
||||
log.setLevel(logging.DEBUG)
|
||||
log.debug("Debug mode set from API (/mtb/debug POST route)")
|
||||
|
||||
elif "MTB_DEBUG" in os.environ:
|
||||
# del os.environ["MTB_DEBUG"]
|
||||
os.environ.pop("MTB_DEBUG")
|
||||
log.setLevel(logging.INFO)
|
||||
|
||||
return web.json_response({"message": f"Debug mode {'set' if enabled else 'unset'}"})
|
||||
|
||||
|
||||
@PromptServer.instance.routes.get("/mtb")
|
||||
async def get_home(request):
|
||||
from . import endpoint
|
||||
|
||||
reload(endpoint)
|
||||
# Check if the request prefers HTML content
|
||||
if "text/html" in request.headers.get("Accept", ""):
|
||||
# # Return an HTML page
|
||||
html_response = f"""
|
||||
<div class="flex-container menu">
|
||||
<a href="/mtb/debug">debug</a>
|
||||
<a href="/mtb/status">status</a>
|
||||
</div>
|
||||
"""
|
||||
return web.Response(
|
||||
text=endpoint.render_base_template("MTB", html_response),
|
||||
content_type="text/html",
|
||||
return web.json_response(
|
||||
{"message": f"Debug mode {'set' if enabled else 'unset'}"}
|
||||
)
|
||||
|
||||
# Return JSON for other requests
|
||||
return web.json_response({"message": "Welcome to MTB!"})
|
||||
@PromptServer.instance.routes.get("/mtb")
|
||||
async def get_home(request):
|
||||
from . import endpoint
|
||||
|
||||
reload(endpoint)
|
||||
# Check if the request prefers HTML content
|
||||
if "text/html" in request.headers.get("Accept", ""):
|
||||
# # Return an HTML page
|
||||
html_response = """
|
||||
<div class="flex-container menu">
|
||||
<a href="/mtb/debug">debug</a>
|
||||
<a href="/mtb/status">status</a>
|
||||
</div>
|
||||
"""
|
||||
return web.Response(
|
||||
text=endpoint.render_base_template("MTB", html_response),
|
||||
content_type="text/html",
|
||||
)
|
||||
|
||||
@PromptServer.instance.routes.get("/mtb/debug")
|
||||
async def get_debug(request):
|
||||
from . import endpoint
|
||||
# Return JSON for other requests
|
||||
return web.json_response({"message": "Welcome to MTB!"})
|
||||
|
||||
reload(endpoint)
|
||||
enabled = False
|
||||
if "MTB_DEBUG" in os.environ:
|
||||
enabled = True
|
||||
# Check if the request prefers HTML content
|
||||
if "text/html" in request.headers.get("Accept", ""):
|
||||
# # Return an HTML page
|
||||
html_response = f"""
|
||||
<h1>MTB Debug Status: {'Enabled' if enabled else 'Disabled'}</h1>
|
||||
"""
|
||||
return web.Response(
|
||||
text=endpoint.render_base_template("Debug", html_response),
|
||||
content_type="text/html",
|
||||
)
|
||||
@PromptServer.instance.routes.get("/mtb/debug")
|
||||
async def get_debug(request):
|
||||
from . import endpoint
|
||||
|
||||
# Return JSON for other requests
|
||||
return web.json_response({"enabled": enabled})
|
||||
reload(endpoint)
|
||||
enabled = "MTB_DEBUG" in os.environ
|
||||
# Check if the request prefers HTML content
|
||||
if "text/html" in request.headers.get("Accept", ""):
|
||||
# # Return an HTML page
|
||||
html_response = f"""
|
||||
<h1>MTB Debug Status: {'Enabled' if enabled else 'Disabled'}</h1>
|
||||
"""
|
||||
return web.Response(
|
||||
text=endpoint.render_base_template("Debug", html_response),
|
||||
content_type="text/html",
|
||||
)
|
||||
|
||||
# Return JSON for other requests
|
||||
return web.json_response({"enabled": enabled})
|
||||
|
||||
@PromptServer.instance.routes.get("/mtb/actions")
|
||||
async def no_route(request):
|
||||
from . import endpoint
|
||||
|
||||
if "text/html" in request.headers.get("Accept", ""):
|
||||
html_response = """
|
||||
<h1>Actions has no get for now...</h1>
|
||||
"""
|
||||
return web.Response(
|
||||
text=endpoint.render_base_template("Actions", html_response),
|
||||
content_type="text/html",
|
||||
)
|
||||
return web.json_response({"message": "actions has no get for now"})
|
||||
|
||||
@PromptServer.instance.routes.post("/mtb/actions")
|
||||
async def do_action(request):
|
||||
from . import endpoint
|
||||
|
||||
reload(endpoint)
|
||||
|
||||
return await endpoint.do_action(request)
|
||||
|
||||
|
||||
# - WAS Dictionary
|
||||
|
||||
+148
-2
@@ -1,4 +1,124 @@
|
||||
from .utils import here
|
||||
from .utils import here, run_command, comfy_mode
|
||||
from aiohttp import web
|
||||
from .log import mklog
|
||||
import sys
|
||||
|
||||
endlog = mklog("mtb endpoint")
|
||||
|
||||
# - ACTIONS
|
||||
import requirements
|
||||
|
||||
|
||||
|
||||
def ACTIONS_installDependency(dependency_names=None):
|
||||
if dependency_names is None:
|
||||
return {"error": "No dependency name provided"}
|
||||
endlog.debug(f"Received Install Dependency request for {dependency_names}")
|
||||
reqs = []
|
||||
if comfy_mode == "embeded":
|
||||
reqs = list(requirements.parse((here / "reqs_portable.txt").read_text()))
|
||||
else:
|
||||
reqs = list(requirements.parse((here / "reqs.txt").read_text()))
|
||||
print([x.specs for x in reqs])
|
||||
print(
|
||||
"\n".join([f"{x.line} {''.join(x.specs[0] if x.specs else '')}" for x in reqs])
|
||||
)
|
||||
for dependency_name in dependency_names:
|
||||
for req in reqs:
|
||||
if req.name == dependency_name:
|
||||
endlog.debug(f"Dependency {dependency_name} installed")
|
||||
break
|
||||
return {"success": True}
|
||||
|
||||
|
||||
def ACTIONS_getStyles(style_name=None):
|
||||
from .nodes.conditions import StylesLoader
|
||||
|
||||
styles = StylesLoader.options
|
||||
match_list = ["name"]
|
||||
if styles:
|
||||
filtered_styles = {
|
||||
key: value
|
||||
for key, value in styles.items()
|
||||
if not key.startswith("__") and key not in match_list
|
||||
}
|
||||
if style_name:
|
||||
return filtered_styles.get(style_name, {"error": "Style not found"})
|
||||
return filtered_styles
|
||||
return {"error": "No styles found"}
|
||||
|
||||
|
||||
async def do_action(request) -> web.Response:
|
||||
endlog.debug("Init action request")
|
||||
request_data = await request.json()
|
||||
name = request_data.get("name")
|
||||
args = request_data.get("args")
|
||||
|
||||
endlog.debug(f"Received action request: {name} {args}")
|
||||
|
||||
method_name = f"ACTIONS_{name}"
|
||||
method = globals().get(method_name)
|
||||
|
||||
if callable(method):
|
||||
result = method(args) if args else method()
|
||||
endlog.debug(f"Action result: {result}")
|
||||
return web.json_response({"result": result})
|
||||
|
||||
available_methods = [
|
||||
attr[len("ACTIONS_") :] for attr in globals() if attr.startswith("ACTIONS_")
|
||||
]
|
||||
|
||||
return web.json_response(
|
||||
{"error": "Invalid method name.", "available_methods": available_methods}
|
||||
)
|
||||
|
||||
|
||||
# - HTML UTILS
|
||||
|
||||
|
||||
def dependencies_button(name, dependencies):
|
||||
deps = ",".join([f"'{x}'" for x in dependencies])
|
||||
return f"""
|
||||
<button class="dependency-button" onclick="window.mtb_action('installDependency',[{deps}])">Install {name} deps</button>
|
||||
"""
|
||||
|
||||
|
||||
def render_table(table_dict, sort=True, title=None):
|
||||
table_dict = sorted(
|
||||
table_dict.items(), key=lambda item: item[0]
|
||||
) # Sort the dictionary by keys
|
||||
|
||||
table_rows = ""
|
||||
for name, item in table_dict:
|
||||
if isinstance(item, dict):
|
||||
if "dependencies" in item:
|
||||
table_rows += f"<tr><td>{name}</td><td>"
|
||||
table_rows += f"{dependencies_button(name,item['dependencies'])}"
|
||||
|
||||
table_rows += "</td></tr>"
|
||||
else:
|
||||
table_rows += f"<tr><td>{name}</td><td>{render_table(item)}</td></tr>"
|
||||
# elif isinstance(item, str):
|
||||
# table_rows += f"<tr><td>{name}</td><td>{item}</td></tr>"
|
||||
else:
|
||||
table_rows += f"<tr><td>{name}</td><td>{item}</td></tr>"
|
||||
|
||||
return f"""
|
||||
<div class="table-container">
|
||||
{"" if title is None else f"<h1>{title}</h1>"}
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{table_rows}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
"""
|
||||
|
||||
|
||||
def render_base_template(title, content):
|
||||
@@ -18,10 +138,35 @@ def render_base_template(title, content):
|
||||
{css_content}
|
||||
</style>
|
||||
</head>
|
||||
<script type="module">
|
||||
import {{ api }} from '/scripts/api.js'
|
||||
const mtb_action = async (action, args) =>{{
|
||||
console.log(`Sending ${{action}} with args: ${{args}}`)
|
||||
}}
|
||||
window.mtb_action = async (action, args) =>{{
|
||||
console.log(`Sending ${{action}} with args: ${{args}} to the API`)
|
||||
const res = await api.fetchApi('/actions', {{
|
||||
method: 'POST',
|
||||
body: JSON.stringify({{
|
||||
name: action,
|
||||
args,
|
||||
}}),
|
||||
}})
|
||||
|
||||
const output = await res.json()
|
||||
console.debug(`Received ${{action}} response:`, output)
|
||||
if (output?.result?.error){{
|
||||
alert(`An error occured: {{output?.result?.error}}`)
|
||||
}}
|
||||
return output?.result
|
||||
}}
|
||||
</script>
|
||||
<body>
|
||||
<header>
|
||||
<a href="/">Back to Comfy</a>
|
||||
<div class="mtb_logo">
|
||||
<img src="https://repository-images.githubusercontent.com/649047066/a3eef9a7-20dd-4ef9-b839-884502d4e873" alt="Comfy MTB Logo" height="70" width="128">
|
||||
<span class="title">Comfy MTB</span>
|
||||
<span class="title">Comfy MTB</span></div>
|
||||
<a style="width:128px;text-align:center" href="https://www.github.com/melmass/comfy_mtb">
|
||||
{github_icon_svg}
|
||||
</a>
|
||||
@@ -35,5 +180,6 @@ def render_base_template(title, content):
|
||||
<!-- Shared footer content here -->
|
||||
</footer>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
"""
|
||||
|
||||
+485
-449
File diff suppressed because it is too large
Load Diff
+425
-447
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+82
-1
@@ -6,6 +6,87 @@ html {
|
||||
color: whitesmoke;
|
||||
}
|
||||
|
||||
a {
|
||||
color: whitesmoke;
|
||||
|
||||
}
|
||||
|
||||
.table-container {
|
||||
width: 70%;
|
||||
height: 100%;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
table {
|
||||
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
th,
|
||||
td {
|
||||
padding: 10px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: rgb(45, 45, 45);
|
||||
/* Light gray background for header row */
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
tr:nth-child(even) {
|
||||
background-color: rgb(45, 45, 45);
|
||||
/* Alternate row background color */
|
||||
}
|
||||
|
||||
tr:hover {
|
||||
background-color: #797979;
|
||||
/* Highlight color on hover */
|
||||
}
|
||||
|
||||
td:nth-child(2) {
|
||||
/* Applies to the second column (Description) */
|
||||
width: 80%;
|
||||
/* Adjust the width as needed */
|
||||
word-wrap: break-word;
|
||||
/* Allow long words to be broken and wrapped to the next line */
|
||||
}
|
||||
|
||||
.mtb_logo {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
/* Styling for WebKit-based browsers (Chrome, Edge) */
|
||||
.table-container::-webkit-scrollbar {
|
||||
width: 10px;
|
||||
/* Set the width of the scrollbar */
|
||||
}
|
||||
|
||||
.table-container::-webkit-scrollbar-thumb {
|
||||
background-color: #797979;
|
||||
/* Color of the scrollbar thumb */
|
||||
}
|
||||
|
||||
/* Styling for Firefox */
|
||||
.table-container {
|
||||
scrollbar-width: thin;
|
||||
/* Set the width of the scrollbar */
|
||||
}
|
||||
|
||||
.table-container::-webkit-scrollbar-thumb {
|
||||
background-color: #797979;
|
||||
/* Color of the scrollbar thumb */
|
||||
}
|
||||
|
||||
/* Optionally, you can also style the scrollbar track (background) */
|
||||
.table-container::-webkit-scrollbar-track {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
@@ -18,7 +99,7 @@ body {
|
||||
.title {
|
||||
font-size: 2.5em;
|
||||
font-weight: 700;
|
||||
margin: 1em;
|
||||
|
||||
}
|
||||
|
||||
header {
|
||||
|
||||
+330
-170
@@ -1,7 +1,6 @@
|
||||
import requests
|
||||
import os
|
||||
import ast
|
||||
import re
|
||||
import argparse
|
||||
import sys
|
||||
import subprocess
|
||||
@@ -9,10 +8,12 @@ from importlib import import_module
|
||||
import platform
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import zipfile
|
||||
import shutil
|
||||
import stat
|
||||
|
||||
import threading
|
||||
import signal
|
||||
from contextlib import suppress
|
||||
from queue import Queue, Empty
|
||||
from contextlib import contextmanager
|
||||
|
||||
here = Path(__file__).parent
|
||||
executable = sys.executable
|
||||
@@ -27,9 +28,19 @@ elif ".venv" in executable:
|
||||
mode = "venv"
|
||||
|
||||
|
||||
if mode == None:
|
||||
if mode is None:
|
||||
mode = "unknown"
|
||||
|
||||
# - Constants
|
||||
repo_url = "https://github.com/melmass/comfy_mtb.git"
|
||||
repo_owner = "melmass"
|
||||
repo_name = "comfy_mtb"
|
||||
short_platform = {
|
||||
"windows": "win_amd64",
|
||||
"linux": "linux_x86_64",
|
||||
}
|
||||
current_platform = platform.system().lower()
|
||||
|
||||
# region ansi
|
||||
# ANSI escape sequences for text styling
|
||||
ANSI_FORMATS = {
|
||||
@@ -102,22 +113,131 @@ def print_formatted(text, *formats, color=None, background=None, **kwargs):
|
||||
formatted_text = apply_format(text, *formats)
|
||||
formatted_text = apply_color(formatted_text, color, background)
|
||||
file = kwargs.get("file", sys.stdout)
|
||||
header = "[mtb install] "
|
||||
|
||||
# Handle console encoding for Unicode characters (utf-8)
|
||||
encoded_header = header.encode(sys.stdout.encoding, errors="replace").decode(
|
||||
sys.stdout.encoding
|
||||
)
|
||||
encoded_text = formatted_text.encode(sys.stdout.encoding, errors="replace").decode(
|
||||
sys.stdout.encoding
|
||||
)
|
||||
|
||||
print(
|
||||
apply_color(apply_format("[mtb install] ", "bold"), color="yellow"),
|
||||
formatted_text,
|
||||
" " * len(encoded_header)
|
||||
if kwargs.get("no_header")
|
||||
else apply_color(apply_format(encoded_header, "bold"), color="yellow"),
|
||||
encoded_text,
|
||||
file=file,
|
||||
)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region utils
|
||||
def enqueue_output(out, queue):
|
||||
for char in iter(lambda: out.read(1), b""):
|
||||
queue.put(char)
|
||||
out.close()
|
||||
|
||||
|
||||
def run_command(cmd, ignored_lines_start=None):
|
||||
if ignored_lines_start is None:
|
||||
ignored_lines_start = []
|
||||
|
||||
if isinstance(cmd, str):
|
||||
shell_cmd = cmd
|
||||
elif isinstance(cmd, list):
|
||||
shell_cmd = ""
|
||||
for arg in cmd:
|
||||
if isinstance(arg, Path):
|
||||
arg = arg.as_posix()
|
||||
shell_cmd += f"{arg} "
|
||||
else:
|
||||
raise ValueError(
|
||||
"Invalid 'cmd' argument. It must be a string or a list of arguments."
|
||||
)
|
||||
|
||||
process = subprocess.Popen(
|
||||
shell_cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
universal_newlines=True,
|
||||
shell=True,
|
||||
)
|
||||
|
||||
# Create separate threads to read standard output and standard error streams
|
||||
stdout_queue = Queue()
|
||||
stderr_queue = Queue()
|
||||
stdout_thread = threading.Thread(
|
||||
target=enqueue_output, args=(process.stdout, stdout_queue)
|
||||
)
|
||||
stderr_thread = threading.Thread(
|
||||
target=enqueue_output, args=(process.stderr, stderr_queue)
|
||||
)
|
||||
stdout_thread.daemon = True
|
||||
stderr_thread.daemon = True
|
||||
stdout_thread.start()
|
||||
stderr_thread.start()
|
||||
|
||||
interrupted = False
|
||||
|
||||
def signal_handler(signum, frame):
|
||||
nonlocal interrupted
|
||||
interrupted = True
|
||||
print("Command execution interrupted.")
|
||||
|
||||
# Register the signal handler for keyboard interrupts (SIGINT)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
stdout_buffer = ""
|
||||
stderr_buffer = ""
|
||||
|
||||
# Process output from both streams until the process completes or interrupted
|
||||
while not interrupted and (
|
||||
process.poll() is None or not stdout_queue.empty() or not stderr_queue.empty()
|
||||
):
|
||||
with suppress(Empty):
|
||||
stdout_char = stdout_queue.get_nowait()
|
||||
stdout_buffer += stdout_char
|
||||
if stdout_char == "\n":
|
||||
if not any(
|
||||
stdout_buffer.startswith(ign) for ign in ignored_lines_start
|
||||
):
|
||||
print(stdout_buffer.strip())
|
||||
stdout_buffer = ""
|
||||
with suppress(Empty):
|
||||
stderr_char = stderr_queue.get_nowait()
|
||||
stderr_buffer += stderr_char
|
||||
if stderr_char == "\n":
|
||||
print(stderr_buffer.strip())
|
||||
stderr_buffer = ""
|
||||
|
||||
# Print any remaining content in buffers
|
||||
if stdout_buffer and not any(
|
||||
stdout_buffer.startswith(ign) for ign in ignored_lines_start
|
||||
):
|
||||
print(stdout_buffer.strip())
|
||||
if stderr_buffer:
|
||||
print(stderr_buffer.strip())
|
||||
|
||||
return_code = process.returncode
|
||||
|
||||
if return_code == 0 and not interrupted:
|
||||
print("Command executed successfully!")
|
||||
else:
|
||||
if not interrupted:
|
||||
print(f"Command failed with return code: {return_code}")
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
try:
|
||||
import requirements
|
||||
except ImportError:
|
||||
print_formatted("Installing requirements-parser...", "italic", color="yellow")
|
||||
subprocess.check_call(
|
||||
[sys.executable, "-m", "pip", "install", "requirements-parser"]
|
||||
)
|
||||
run_command([sys.executable, "-m", "pip", "install", "requirements-parser"])
|
||||
import requirements
|
||||
|
||||
print_formatted("Done.", "italic", color="green")
|
||||
@@ -126,10 +246,8 @@ try:
|
||||
from tqdm import tqdm
|
||||
except ImportError:
|
||||
print_formatted("Installing tqdm...", "italic", color="yellow")
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "tqdm"])
|
||||
run_command([sys.executable, "-m", "pip", "install", "--upgrade", "tqdm"])
|
||||
from tqdm import tqdm
|
||||
import importlib
|
||||
|
||||
|
||||
pip_map = {
|
||||
"onnxruntime-gpu": "onnxruntime",
|
||||
@@ -141,16 +259,41 @@ pip_map = {
|
||||
|
||||
|
||||
def is_pipe():
|
||||
try:
|
||||
mode = os.fstat(0).st_mode
|
||||
return (
|
||||
stat.S_ISFIFO(mode)
|
||||
or stat.S_ISREG(mode)
|
||||
or stat.S_ISBLK(mode)
|
||||
or stat.S_ISSOCK(mode)
|
||||
)
|
||||
except OSError:
|
||||
if not sys.stdin.isatty():
|
||||
return False
|
||||
if sys.platform == "win32":
|
||||
try:
|
||||
import msvcrt
|
||||
|
||||
return msvcrt.get_osfhandle(0) != -1
|
||||
except ImportError:
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
mode = os.fstat(0).st_mode
|
||||
return (
|
||||
stat.S_ISFIFO(mode)
|
||||
or stat.S_ISREG(mode)
|
||||
or stat.S_ISBLK(mode)
|
||||
or stat.S_ISSOCK(mode)
|
||||
)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
@contextmanager
|
||||
def suppress_std():
|
||||
with open(os.devnull, "w") as devnull:
|
||||
old_stdout = sys.stdout
|
||||
old_stderr = sys.stderr
|
||||
sys.stdout = devnull
|
||||
sys.stderr = devnull
|
||||
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
sys.stdout = old_stdout
|
||||
sys.stderr = old_stderr
|
||||
|
||||
|
||||
# Get the version from __init__.py
|
||||
@@ -211,87 +354,94 @@ def try_import(requirement):
|
||||
installed = False
|
||||
|
||||
pip_name = dependency
|
||||
if specs := requirement.specs:
|
||||
pip_name += "".join(specs[0])
|
||||
|
||||
pip_spec = "".join(specs[0]) if (specs := requirement.specs) else ""
|
||||
try:
|
||||
import_module(import_name)
|
||||
with suppress_std():
|
||||
import_module(import_name)
|
||||
print_formatted(
|
||||
f"Package {pip_name} already installed (import name: '{import_name}').",
|
||||
f"\t✅ Package {pip_name} already installed (import name: '{import_name}').",
|
||||
"bold",
|
||||
color="green",
|
||||
no_header=True,
|
||||
)
|
||||
installed = True
|
||||
except ImportError:
|
||||
pass
|
||||
print_formatted(
|
||||
f"\t⛔ Package {pip_name} is missing (import name: '{import_name}').",
|
||||
"bold",
|
||||
color="red",
|
||||
no_header=True,
|
||||
)
|
||||
|
||||
return (installed, pip_name, import_name)
|
||||
return (installed, pip_name, pip_spec, import_name)
|
||||
|
||||
|
||||
def import_or_install(requirement, dry=False):
|
||||
installed, pip_name, import_name = try_import(requirement)
|
||||
installed, pip_name, pip_spec, import_name = try_import(requirement)
|
||||
|
||||
pip_install_name = pip_name + pip_spec
|
||||
|
||||
if not installed:
|
||||
print_formatted(f"Installing package {pip_name}...", "italic", color="yellow")
|
||||
if dry:
|
||||
print_formatted(
|
||||
f"Dry-run: Package {pip_name} would be installed (import name: '{import_name}').",
|
||||
f"Dry-run: Package {pip_install_name} would be installed (import name: '{import_name}').",
|
||||
color="yellow",
|
||||
)
|
||||
else:
|
||||
try:
|
||||
subprocess.check_call(
|
||||
[sys.executable, "-m", "pip", "install", pip_name]
|
||||
)
|
||||
run_command([sys.executable, "-m", "pip", "install", pip_install_name])
|
||||
print_formatted(
|
||||
f"Package {pip_name} installed successfully using pip package name (import name: '{import_name}')",
|
||||
f"Package {pip_install_name} installed successfully using pip package name (import name: '{import_name}')",
|
||||
"bold",
|
||||
color="green",
|
||||
)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print_formatted(
|
||||
f"Failed to install package {pip_name} using pip package name (import name: '{import_name}'). Error: {str(e)}",
|
||||
f"Failed to install package {pip_install_name} using pip package name (import name: '{import_name}'). Error: {str(e)}",
|
||||
"bold",
|
||||
color="red",
|
||||
)
|
||||
|
||||
|
||||
def get_github_assets(tag=None):
|
||||
if tag:
|
||||
tag_url = (
|
||||
f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/tags/{tag}"
|
||||
)
|
||||
else:
|
||||
tag_url = (
|
||||
f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
|
||||
)
|
||||
response = requests.get(tag_url)
|
||||
if response.status_code == 404:
|
||||
# print_formatted(
|
||||
# f"Tag version '{apply_color(version,'cyan')}' not found for {owner}/{repo} repository."
|
||||
# )
|
||||
print_formatted("Error retrieving the release assets.", color="red")
|
||||
sys.exit()
|
||||
|
||||
tag_data = response.json()
|
||||
tag_name = tag_data["name"]
|
||||
|
||||
return tag_data, tag_name
|
||||
|
||||
|
||||
# Install dependencies from requirements.txt
|
||||
def install_dependencies(dry=False):
|
||||
parsed_requirements = get_requirements(here / "requirements.txt")
|
||||
parsed_requirements = get_requirements(here / "reqs.txt")
|
||||
if not parsed_requirements:
|
||||
return
|
||||
print_formatted(
|
||||
"Installing dependencies from requirements.txt...", "italic", color="yellow"
|
||||
"Installing dependencies from reqs.txt...", "italic", color="yellow"
|
||||
)
|
||||
|
||||
for requirement in parsed_requirements:
|
||||
import_or_install(requirement, dry=dry)
|
||||
|
||||
if mode == "venv":
|
||||
parsed_requirements = get_requirements(here / "requirements-wheels.txt")
|
||||
if not parsed_requirements:
|
||||
return
|
||||
for requirement in parsed_requirements:
|
||||
import_or_install(requirement, dry=dry)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
full = False
|
||||
if is_pipe():
|
||||
print_formatted("Pipe detected, full install...", color="green")
|
||||
# we clone our repo
|
||||
url = "https://github.com/melmass/comfy_mtb.git"
|
||||
clone_dir = here / "custom_nodes" / "comfy_mtb"
|
||||
if not clone_dir.exists():
|
||||
clone_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
print_formatted(f"Cloning {url} to {clone_dir}", "italic", color="yellow")
|
||||
subprocess.check_call(["git", "clone", "--recursive", url, clone_dir])
|
||||
|
||||
# os.chdir(clone_dir)
|
||||
here = clone_dir
|
||||
full = True
|
||||
|
||||
if len(sys.argv) == 1:
|
||||
print_formatted(
|
||||
"No arguments provided, doing a full install/update...",
|
||||
@@ -302,7 +452,13 @@ if __name__ == "__main__":
|
||||
full = True
|
||||
|
||||
# Parse command-line arguments
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(description="Comfy_mtb install script")
|
||||
parser.add_argument(
|
||||
"--path",
|
||||
"-p",
|
||||
type=str,
|
||||
help="Path to clone the repository to (i.e the absolute path to ComfyUI/custom_nodes)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--wheels", "-w", action="store_true", help="Install wheel dependencies"
|
||||
)
|
||||
@@ -315,73 +471,88 @@ if __name__ == "__main__":
|
||||
help="Print what will happen without doing it (still making requests to the GH Api)",
|
||||
)
|
||||
|
||||
# - keep
|
||||
# parser.add_argument(
|
||||
# "--version",
|
||||
# default=get_local_version(),
|
||||
# help="Version to check against the GitHub API",
|
||||
# )
|
||||
print_formatted("mtb install", "bold", color="yellow")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
wheels_directory = here / "wheels"
|
||||
# wheels_directory = here / "wheels"
|
||||
print_formatted(f"Detected environment: {apply_color(mode,'cyan')}")
|
||||
|
||||
if args.path:
|
||||
clone_dir = Path(args.path)
|
||||
if not clone_dir.exists():
|
||||
print_formatted(
|
||||
"The path provided does not exist on disk... It must be pointing to ComfyUI's custom_nodes directory"
|
||||
)
|
||||
sys.exit()
|
||||
|
||||
else:
|
||||
repo_dir = clone_dir / repo_name
|
||||
if not repo_dir.exists():
|
||||
print_formatted(f"Cloning to {repo_dir}...", "italic", color="yellow")
|
||||
run_command(["git", "clone", "--recursive", repo_url, repo_dir])
|
||||
else:
|
||||
print_formatted(
|
||||
f"Directory {repo_dir} already exists, we will update it..."
|
||||
)
|
||||
run_command(["git", "pull", "-C", repo_dir])
|
||||
# os.chdir(clone_dir)
|
||||
here = clone_dir
|
||||
full = True
|
||||
|
||||
# Install dependencies from requirements.txt
|
||||
# if args.requirements or mode == "venv":
|
||||
install_dependencies(dry=args.dry)
|
||||
|
||||
if (not args.wheels and mode not in ["colab", "embeded"]) and not full:
|
||||
print_formatted(
|
||||
"Skipping wheel installation. Use --wheels to install wheel dependencies. (only needed for Comfy embed)",
|
||||
"italic",
|
||||
color="yellow",
|
||||
)
|
||||
sys.exit()
|
||||
# if (not args.wheels and mode not in ["colab", "embeded"]) and not full:
|
||||
# print_formatted(
|
||||
# "Skipping wheel installation. Use --wheels to install wheel dependencies. (only needed for Comfy embed)",
|
||||
# "italic",
|
||||
# color="yellow",
|
||||
# )
|
||||
|
||||
if mode in ["colab", "embeded"]:
|
||||
print_formatted(
|
||||
f"Downloading and installing release wheels since we are in a Comfy {apply_color(mode,'cyan')} environment",
|
||||
)
|
||||
if full:
|
||||
print_formatted(
|
||||
f"Downloading and installing release wheels since no arguments where provided"
|
||||
)
|
||||
# install_dependencies(dry=args.dry)
|
||||
# sys.exit()
|
||||
|
||||
# - Check the env before proceeding.
|
||||
missing_wheels = False
|
||||
parsed_requirements = get_requirements(here / "requirements-wheels.txt")
|
||||
if parsed_requirements:
|
||||
# if mode in ["colab", "embeded"]:
|
||||
# print_formatted(
|
||||
# f"Downloading and installing release wheels since we are in a Comfy {apply_color(mode,'cyan')} environment",
|
||||
# "italic",
|
||||
# color="yellow",
|
||||
# )
|
||||
# if full:
|
||||
# print_formatted(
|
||||
# f"Downloading and installing release wheels since no arguments where provided",
|
||||
# "italic",
|
||||
# color="yellow",
|
||||
# )
|
||||
|
||||
print_formatted("Checking environment...", "italic", color="yellow")
|
||||
missing_deps = []
|
||||
if parsed_requirements := get_requirements(here / "reqs.txt"):
|
||||
for requirement in parsed_requirements:
|
||||
installed, pip_name, import_name = try_import(requirement)
|
||||
installed, pip_name, pip_spec, import_name = try_import(requirement)
|
||||
if not installed:
|
||||
missing_wheels = True
|
||||
break
|
||||
missing_deps.append(pip_name.split("-")[0])
|
||||
|
||||
if not missing_wheels:
|
||||
if not missing_deps:
|
||||
print_formatted(
|
||||
f"All required wheels are already installed.", "italic", color="green"
|
||||
"All requirements are already installed. Enjoy 🚀",
|
||||
"italic",
|
||||
color="green",
|
||||
)
|
||||
sys.exit()
|
||||
|
||||
# Fetch the JSON data from the GitHub API URL
|
||||
owner = "melmass"
|
||||
repo = "comfy_mtb"
|
||||
# # - Get the tag version from the GitHub API
|
||||
# tag_data, tag_name = get_github_assets(tag=None)
|
||||
|
||||
# # - keep
|
||||
# version = args.version
|
||||
current_platform = platform.system().lower()
|
||||
|
||||
# Get the tag version from the GitHub API
|
||||
tag_url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
|
||||
response = requests.get(tag_url)
|
||||
if response.status_code == 404:
|
||||
# print_formatted(
|
||||
# f"Tag version '{apply_color(version,'cyan')}' not found for {owner}/{repo} repository."
|
||||
# )
|
||||
print_formatted("Error retrieving the release assets.", color="red")
|
||||
sys.exit()
|
||||
|
||||
tag_data = response.json()
|
||||
tag_name = tag_data["name"]
|
||||
|
||||
# # Compare the local and tag versions
|
||||
# if version and tag_name:
|
||||
# if re.match(r"v?(\d+(\.\d+)+)", version) and re.match(
|
||||
@@ -398,75 +569,64 @@ if __name__ == "__main__":
|
||||
# )
|
||||
# sys.exit()
|
||||
|
||||
# Download the assets for the given version
|
||||
matching_assets = [
|
||||
asset for asset in tag_data["assets"] if current_platform in asset["name"]
|
||||
]
|
||||
if not matching_assets:
|
||||
print_formatted(
|
||||
f"Unsupported operating system: {current_platform}", color="yellow"
|
||||
)
|
||||
# matching_assets = [
|
||||
# asset
|
||||
# for asset in tag_data["assets"]
|
||||
# if asset["name"].endswith(".whl")
|
||||
# and (
|
||||
# "any" in asset["name"] or short_platform[current_platform] in asset["name"]
|
||||
# )
|
||||
# ]
|
||||
# if not matching_assets:
|
||||
# print_formatted(
|
||||
# f"Unsupported operating system: {current_platform}", color="yellow"
|
||||
# )
|
||||
# wheel_order_asset = next(
|
||||
# (asset for asset in tag_data["assets"] if asset["name"] == "wheel_order.txt"),
|
||||
# None,
|
||||
# )
|
||||
# if wheel_order_asset is not None:
|
||||
# print_formatted(
|
||||
# "⚙️ Sorting the release wheels using wheels order", "italic", color="yellow"
|
||||
# )
|
||||
# response = requests.get(wheel_order_asset["browser_download_url"])
|
||||
# if response.status_code == 200:
|
||||
# wheel_order = [line.strip() for line in response.text.splitlines()]
|
||||
|
||||
wheels_directory.mkdir(exist_ok=True)
|
||||
# def get_order_index(val):
|
||||
# try:
|
||||
# return wheel_order.index(val)
|
||||
# except ValueError:
|
||||
# return len(wheel_order)
|
||||
|
||||
for asset in matching_assets:
|
||||
asset_name = asset["name"]
|
||||
asset_download_url = asset["browser_download_url"]
|
||||
print_formatted(f"Downloading asset: {asset_name}", color="yellow")
|
||||
asset_dest = wheels_directory / asset_name
|
||||
download_file(asset_download_url, asset_dest)
|
||||
# matching_assets = sorted(
|
||||
# matching_assets,
|
||||
# key=lambda x: get_order_index(x["name"].split("-")[0]),
|
||||
# )
|
||||
# else:
|
||||
# print("Failed to fetch wheel_order.txt. Status code:", response.status_code)
|
||||
|
||||
# - Unzip to wheels dir
|
||||
whl_files = []
|
||||
with zipfile.ZipFile(asset_dest, "r") as zip_ref:
|
||||
for item in tqdm(zip_ref.namelist(), desc="Extracting", unit="file"):
|
||||
if item.endswith(".whl"):
|
||||
item_basename = os.path.basename(item)
|
||||
target_path = wheels_directory / item_basename
|
||||
with zip_ref.open(item) as source, open(
|
||||
target_path, "wb"
|
||||
) as target:
|
||||
whl_files.append(target_path)
|
||||
shutil.copyfileobj(source, target)
|
||||
# missing_deps_urls = []
|
||||
# for whl_file in matching_assets:
|
||||
# # check if installed
|
||||
# missing_deps_urls.append(whl_file["browser_download_url"])
|
||||
|
||||
print_formatted(
|
||||
f"Wheels extracted for {current_platform} to the '{wheels_directory}' directory.",
|
||||
"bold",
|
||||
color="green",
|
||||
)
|
||||
install_cmd = [sys.executable, "-m", "pip", "install"]
|
||||
|
||||
if whl_files:
|
||||
for whl_file in tqdm(whl_files, desc="Installing", unit="package"):
|
||||
whl_path = wheels_directory / whl_file
|
||||
|
||||
# check if installed
|
||||
try:
|
||||
whl_dep = whl_path.name.split("-")[0]
|
||||
import_name = pip_map.get(whl_dep, whl_dep)
|
||||
import_module(import_name)
|
||||
tqdm.write(
|
||||
f"Package {import_name} already installed, skipping wheel installation.",
|
||||
)
|
||||
continue
|
||||
except ImportError:
|
||||
if args.dry:
|
||||
tqdm.write(
|
||||
f"Dry-run: Package {whl_path.name} would be installed.",
|
||||
)
|
||||
continue
|
||||
|
||||
tqdm.write("Installing wheel: " + whl_path.name)
|
||||
|
||||
subprocess.check_call(
|
||||
[
|
||||
sys.executable,
|
||||
"-m",
|
||||
"pip",
|
||||
"install",
|
||||
whl_path.resolve().as_posix(),
|
||||
]
|
||||
)
|
||||
|
||||
print_formatted("Wheels installation completed.", color="green")
|
||||
# - Install all deps
|
||||
if not args.dry:
|
||||
if platform.system() == "Windows":
|
||||
wheel_cmd = install_cmd + ["-r", (here / "reqs_windows.txt")]
|
||||
else:
|
||||
print_formatted("No .whl files found. Nothing to install.", color="yellow")
|
||||
wheel_cmd = install_cmd + ["-r", (here / "reqs.txt")]
|
||||
|
||||
run_command(wheel_cmd)
|
||||
print_formatted(
|
||||
"✅ Successfully installed all dependencies.", "italic", color="green"
|
||||
)
|
||||
else:
|
||||
print_formatted(
|
||||
f"Would have run the following command:\n\t{apply_color(' '.join(install_cmd),'cyan')}",
|
||||
"italic",
|
||||
color="yellow",
|
||||
)
|
||||
|
||||
@@ -3,7 +3,6 @@ import re
|
||||
import os
|
||||
|
||||
base_log_level = logging.DEBUG if os.environ.get("MTB_DEBUG") else logging.INFO
|
||||
print(f"Log level: {base_log_level}")
|
||||
|
||||
|
||||
# Custom object that discards the output
|
||||
|
||||
+9
-6
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"Animation Builder (mtb)": "Convenient way to manage basic animation maths at the core of many of my workflows",
|
||||
"Any To String (mtb)": "Tries to take any input and convert it to a string",
|
||||
"Bbox (mtb)": "The bounding box (BBOX) custom type used by other nodes",
|
||||
"Bbox From Mask (mtb)": "From a mask extract the bounding box",
|
||||
"Blur (mtb)": "Blur an image using a Gaussian filter.",
|
||||
@@ -9,12 +10,12 @@
|
||||
"Crop (mtb)": "Crops an image and an optional mask to a given bounding box\n\n The bounding box can be given as a tuple of (x, y, width, height) or as a BBOX type\n The BBOX input takes precedence over the tuple input\n ",
|
||||
"Debug (mtb)": "Experimental node to debug any Comfy values, support for more types and widgets is planned",
|
||||
"Deep Bump (mtb)": "Normal & height maps generation from single pictures",
|
||||
"Export To Prores (mtb)": "Export to ProRes 4444 (Experimental)",
|
||||
"Export With Ffmpeg (mtb)": "Export with FFmpeg (Experimental)",
|
||||
"Face Swap (mtb)": "Face swap using deepinsight/insightface models",
|
||||
"Film Interpolation (mtb)": "Google Research FILM frame interpolation for large motion",
|
||||
"Fit Number (mtb)": "Fit the input float using a source and target range",
|
||||
"Float To Number (mtb)": "Node addon for the WAS Suite. Converts a \"comfy\" FLOAT to a NUMBER.",
|
||||
"Get Batch From History (mtb)": "Very experimental node to load images from the history of the server.\n\n Queue items without output are ignore in the count.",
|
||||
"Get Batch From History (mtb)": "Very experimental node to load images from the history of the server.\n\n Queue items without output are ignored in the count.",
|
||||
"Image Compare (mtb)": "Compare two images and return a difference image",
|
||||
"Image Premultiply (mtb)": "Premultiply image with mask",
|
||||
"Image Remove Background Rembg (mtb)": "Removes the background from the input using Rembg.",
|
||||
@@ -22,8 +23,7 @@
|
||||
"Int To Bool (mtb)": "Basic int to bool conversion",
|
||||
"Int To Number (mtb)": "Node addon for the WAS Suite. Converts a \"comfy\" INT to a NUMBER.",
|
||||
"Latent Lerp (mtb)": "Linear interpolation (blend) between two latent vectors",
|
||||
"Latent Noise (mtb)": "Inject noise into latent space",
|
||||
"Latent Transform (mtb)": "Dumb attempt at reproducing some deforum like motion",
|
||||
"Load Face Analysis Model (mtb)": "Loads a face analysis model",
|
||||
"Load Face Enhance Model (mtb)": "Loads a GFPGan or RestoreFormer model for face enhancement.",
|
||||
"Load Face Swap Model (mtb)": "Loads a faceswap model",
|
||||
"Load Film Model (mtb)": "Loads a FILM model",
|
||||
@@ -35,9 +35,12 @@
|
||||
"Save Gif (mtb)": "Save the images from the batch as a GIF",
|
||||
"Save Image Grid (mtb)": "Save all the images in the input batch as a grid of images.",
|
||||
"Save Image Sequence (mtb)": "Save an image sequence to a folder. The current frame is used to determine which image to save.\n\n This is merely a wrapper around the `save_images` function with formatting for the output folder and filename.\n ",
|
||||
"Save Tensors (mtb)": "Save torch tensors (image, mask or latent) to disk, useful to debug things outside comfy",
|
||||
"Smart Step (mtb)": "Utils to control the steps start/stop of the KAdvancedSampler in percentage",
|
||||
"String Replace (mtb)": "Basic string replacement",
|
||||
"Styles Loader (mtb)": "Load csv files and populate a dropdown from the rows (\u00e0 la A111)",
|
||||
"Text To Image (mtb)": "Utils to convert text to image using a font\n\n\n The tool looks for any .ttf file in the Comfy folder hierarchy.\n ",
|
||||
"Uncrop (mtb)": "Uncrops an image to a given bounding box\n\n The bounding box can be given as a tuple of (x, y, width, height) or as a BBOX type\n The BBOX input takes precedence over the tuple input"
|
||||
}
|
||||
"Transform Image (mtb)": "Save torch tensors (image, mask or latent) to disk, useful to debug things outside comfy\n\n\n it return a tensor representing the transformed images with the same shape as the input tensor\n ",
|
||||
"Uncrop (mtb)": "Uncrops an image to a given bounding box\n\n The bounding box can be given as a tuple of (x, y, width, height) or as a BBOX type\n The BBOX input takes precedence over the tuple input",
|
||||
"Unsplash Image (mtb)": "Unsplash Image given a keyword and a size"
|
||||
}
|
||||
|
||||
+1
-4
@@ -4,9 +4,6 @@ from ..log import log
|
||||
class AnimationBuilder:
|
||||
"""Convenient way to manage basic animation maths at the core of many of my workflows"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -20,7 +17,7 @@ class AnimationBuilder:
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("INT", "FLOAT", "INT", "BOOL")
|
||||
RETURN_TYPES = ("INT", "FLOAT", "INT", "BOOLEAN")
|
||||
RETURN_NAMES = ("frame", "0-1 (scaled)", "count", "loop_ended")
|
||||
CATEGORY = "mtb/animation"
|
||||
FUNCTION = "build_animation"
|
||||
|
||||
+102
-122
@@ -1,4 +1,3 @@
|
||||
from ..utils import pil2tensor
|
||||
from ..utils import here
|
||||
from ..log import log
|
||||
import folder_paths
|
||||
@@ -7,12 +6,91 @@ import shutil
|
||||
import csv
|
||||
|
||||
|
||||
class InterpolateClipSequential:
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"base_text": ("STRING", {"multiline": True}),
|
||||
"text_to_replace": ("STRING", {"default": ""}),
|
||||
"clip": ("CLIP",),
|
||||
"interpolation_strength": (
|
||||
"FLOAT",
|
||||
{"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("CONDITIONING",)
|
||||
FUNCTION = "interpolate_encodings_sequential"
|
||||
|
||||
CATEGORY = "mtb/conditioning"
|
||||
|
||||
def interpolate_encodings_sequential(
|
||||
self, base_text, text_to_replace, clip, interpolation_strength, **replacements
|
||||
):
|
||||
log.debug(f"Received interpolation_strength: {interpolation_strength}")
|
||||
|
||||
# - Ensure interpolation strength is within [0, 1]
|
||||
interpolation_strength = max(0.0, min(1.0, interpolation_strength))
|
||||
|
||||
# - Check if replacements were provided
|
||||
if not replacements:
|
||||
raise ValueError("At least one replacement should be provided.")
|
||||
|
||||
num_replacements = len(replacements)
|
||||
log.debug(f"Number of replacements: {num_replacements}")
|
||||
|
||||
segment_length = 1.0 / num_replacements
|
||||
log.debug(f"Calculated segment_length: {segment_length}")
|
||||
|
||||
# - Find the segment that the interpolation_strength falls into
|
||||
segment_index = min(
|
||||
int(interpolation_strength // segment_length), num_replacements - 1
|
||||
)
|
||||
log.debug(f"Segment index: {segment_index}")
|
||||
|
||||
# - Calculate the local strength within the segment
|
||||
local_strength = (
|
||||
interpolation_strength - (segment_index * segment_length)
|
||||
) / segment_length
|
||||
log.debug(f"Local strength: {local_strength}")
|
||||
|
||||
# - If it's the first segment, interpolate between base_text and the first replacement
|
||||
if segment_index == 0:
|
||||
replacement_text = list(replacements.values())[0]
|
||||
log.debug("Using the base text a the base blend")
|
||||
# - Start with the base_text condition
|
||||
tokens = clip.tokenize(base_text)
|
||||
cond_from, pooled_from = clip.encode_from_tokens(tokens, return_pooled=True)
|
||||
else:
|
||||
base_replace = list(replacements.values())[segment_index - 1]
|
||||
log.debug(f"Using {base_replace} a the base blend")
|
||||
|
||||
# - Start with the base_text condition replaced by the closest replacement
|
||||
tokens = clip.tokenize(base_text.replace(text_to_replace, base_replace))
|
||||
cond_from, pooled_from = clip.encode_from_tokens(tokens, return_pooled=True)
|
||||
|
||||
replacement_text = list(replacements.values())[segment_index]
|
||||
|
||||
interpolated_text = base_text.replace(text_to_replace, replacement_text)
|
||||
tokens = clip.tokenize(interpolated_text)
|
||||
cond_to, pooled_to = clip.encode_from_tokens(tokens, return_pooled=True)
|
||||
|
||||
# - Linearly interpolate between the two conditions
|
||||
interpolated_condition = (
|
||||
1.0 - local_strength
|
||||
) * cond_from + local_strength * cond_to
|
||||
interpolated_pooled = (
|
||||
1.0 - local_strength
|
||||
) * pooled_from + local_strength * pooled_to
|
||||
|
||||
return ([[interpolated_condition, {"pooled_output": interpolated_pooled}]],)
|
||||
|
||||
|
||||
class SmartStep:
|
||||
"""Utils to control the steps start/stop of the KAdvancedSampler in percentage"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -62,30 +140,28 @@ class StylesLoader:
|
||||
|
||||
options = {}
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
input_dir = Path(folder_paths.base_path) / "styles"
|
||||
if not input_dir.exists():
|
||||
install_default_styles()
|
||||
if not cls.options:
|
||||
input_dir = Path(folder_paths.base_path) / "styles"
|
||||
if not input_dir.exists():
|
||||
install_default_styles()
|
||||
|
||||
if not (files := [f for f in input_dir.iterdir() if f.suffix == ".csv"]):
|
||||
log.warn(
|
||||
"No styles found in the styles folder, place at least one csv file in the styles folder at the root of ComfyUI (for instance ComfyUI/styles/mystyle.csv)"
|
||||
)
|
||||
|
||||
for file in files:
|
||||
with open(file, "r", encoding="utf8") as f:
|
||||
parsed = csv.reader(f)
|
||||
for row in parsed:
|
||||
log.debug(f"Adding style {row[0]}")
|
||||
cls.options[row[0]] = (row[1], row[2])
|
||||
|
||||
else:
|
||||
log.debug(f"Using cached styles (count: {len(cls.options)})")
|
||||
|
||||
if not (files := [f for f in input_dir.iterdir() if f.suffix == ".csv"]):
|
||||
log.error(
|
||||
"No styles found in the styles folder, place at least one csv file in the styles folder"
|
||||
)
|
||||
return {
|
||||
"required": {
|
||||
"style_name": (["error"],),
|
||||
}
|
||||
}
|
||||
for file in files:
|
||||
with open(file, "r", encoding="utf8") as f:
|
||||
parsed = csv.reader(f)
|
||||
for row in parsed:
|
||||
log.debug(f"Adding style {row[0]}")
|
||||
cls.options[row[0]] = (row[1], row[2])
|
||||
return {
|
||||
"required": {
|
||||
"style_name": (list(cls.options.keys()),),
|
||||
@@ -102,100 +178,4 @@ class StylesLoader:
|
||||
return (self.options[style_name][0], self.options[style_name][1])
|
||||
|
||||
|
||||
class TextToImage:
|
||||
"""Utils to convert text to image using a font
|
||||
|
||||
|
||||
The tool looks for any .ttf file in the Comfy folder hierarchy.
|
||||
"""
|
||||
|
||||
fonts = {}
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
fonts = list(Path(folder_paths.base_path).glob("**/*.ttf"))
|
||||
if not fonts:
|
||||
log.error(
|
||||
"No fonts found in the fonts folder, place at least one ttf file in the fonts folder"
|
||||
)
|
||||
return {
|
||||
"required": {
|
||||
"font": (["error"],),
|
||||
}
|
||||
}
|
||||
for font in fonts:
|
||||
log.debug(f"Adding font {font}")
|
||||
cls.fonts[font.stem] = font.as_posix()
|
||||
|
||||
return {
|
||||
"required": {
|
||||
"text": (
|
||||
"STRING",
|
||||
{"default": "Hello world!"},
|
||||
),
|
||||
"font": ((sorted(cls.fonts.keys())),),
|
||||
"wrap": (
|
||||
"INT",
|
||||
{"default": 120, "min": 0, "max": 8096, "step": 1},
|
||||
),
|
||||
"font_size": (
|
||||
"INT",
|
||||
{"default": 12, "min": 1, "max": 100, "step": 1},
|
||||
),
|
||||
"width": (
|
||||
"INT",
|
||||
{"default": 512, "min": 1, "max": 1000, "step": 1},
|
||||
),
|
||||
"height": (
|
||||
"INT",
|
||||
{"default": 512, "min": 1, "max": 8096, "step": 1},
|
||||
),
|
||||
# "position": (["INT"], {"default": 0, "min": 0, "max": 100, "step": 1}),
|
||||
"color": (
|
||||
"COLOR",
|
||||
{"default": "black"},
|
||||
),
|
||||
"background": (
|
||||
"COLOR",
|
||||
{"default": "white"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
RETURN_NAMES = ("image",)
|
||||
FUNCTION = "text_to_image"
|
||||
CATEGORY = "mtb/generate"
|
||||
|
||||
def text_to_image(
|
||||
self, text, font, wrap, font_size, width, height, color, background
|
||||
):
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import textwrap
|
||||
|
||||
font = self.fonts[font]
|
||||
font = ImageFont.truetype(font, font_size)
|
||||
if wrap == 0:
|
||||
wrap = width / font_size
|
||||
lines = textwrap.wrap(text, width=wrap)
|
||||
log.debug(f"Lines: {lines}")
|
||||
line_height = font.getsize("hg")[1]
|
||||
img_height = height # line_height * len(lines)
|
||||
img_width = width # max(font.getsize(line)[0] for line in lines)
|
||||
|
||||
img = Image.new("RGBA", (img_width, img_height), background)
|
||||
draw = ImageDraw.Draw(img)
|
||||
y_text = 0
|
||||
for line in lines:
|
||||
width, height = font.getsize(line)
|
||||
draw.text((0, y_text), line, color, font=font)
|
||||
y_text += height
|
||||
|
||||
# img.save(os.path.join(folder_paths.base_path, f'{str(uuid.uuid4())}.png'))
|
||||
return (pil2tensor(img),)
|
||||
|
||||
|
||||
__nodes__ = [SmartStep, TextToImage, StylesLoader]
|
||||
__nodes__ = [SmartStep, StylesLoader, InterpolateClipSequential]
|
||||
|
||||
@@ -9,9 +9,6 @@ from ..log import log
|
||||
class Bbox:
|
||||
"""The bounding box (BBOX) custom type used by other nodes"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -42,9 +39,6 @@ class Bbox:
|
||||
class BboxFromMask:
|
||||
"""From a mask extract the bounding box"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -116,9 +110,6 @@ class Crop:
|
||||
The BBOX input takes precedence over the tuple input
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -205,9 +196,6 @@ class Uncrop:
|
||||
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"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
|
||||
+67
-3
@@ -2,6 +2,9 @@ from ..utils import tensor2pil
|
||||
from ..log import log
|
||||
import io, base64
|
||||
import torch
|
||||
import folder_paths
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Debug:
|
||||
@@ -35,9 +38,9 @@ class Debug:
|
||||
b64_imgs = []
|
||||
for im in image:
|
||||
buffered = io.BytesIO()
|
||||
im.save(buffered, format="JPEG")
|
||||
im.save(buffered, format="PNG")
|
||||
b64_imgs.append(
|
||||
"data:image/jpeg;base64,"
|
||||
"data:image/png;base64,"
|
||||
+ base64.b64encode(buffered.getvalue()).decode("utf-8")
|
||||
)
|
||||
|
||||
@@ -54,4 +57,65 @@ class Debug:
|
||||
return output
|
||||
|
||||
|
||||
__nodes__ = [Debug]
|
||||
class SaveTensors:
|
||||
"""Save torch tensors (image, mask or latent) to disk, useful to debug things outside comfy"""
|
||||
|
||||
def __init__(self):
|
||||
self.output_dir = folder_paths.get_output_directory()
|
||||
self.type = "mtb/debug"
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"filename_prefix": ("STRING", {"default": "ComfyPickle"}),
|
||||
},
|
||||
"optional": {
|
||||
"image": ("IMAGE",),
|
||||
"mask": ("MASK",),
|
||||
"latent": ("LATENT",),
|
||||
},
|
||||
}
|
||||
|
||||
FUNCTION = "save"
|
||||
OUTPUT_NODE = True
|
||||
RETURN_TYPES = ()
|
||||
CATEGORY = "mtb/debug"
|
||||
|
||||
def save(
|
||||
self,
|
||||
filename_prefix,
|
||||
image: Optional[torch.Tensor] = None,
|
||||
mask: Optional[torch.Tensor] = None,
|
||||
latent: Optional[torch.Tensor] = None,
|
||||
):
|
||||
(
|
||||
full_output_folder,
|
||||
filename,
|
||||
counter,
|
||||
subfolder,
|
||||
filename_prefix,
|
||||
) = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
|
||||
full_output_folder = Path(full_output_folder)
|
||||
if image is not None:
|
||||
image_file = f"{filename}_image_{counter:05}.pt"
|
||||
torch.save(image, full_output_folder / image_file)
|
||||
# np.save(full_output_folder/ image_file, image.cpu().numpy())
|
||||
|
||||
if mask is not None:
|
||||
mask_file = f"{filename}_mask_{counter:05}.pt"
|
||||
torch.save(mask, full_output_folder / mask_file)
|
||||
# np.save(full_output_folder/ mask_file, mask.cpu().numpy())
|
||||
|
||||
if latent is not None:
|
||||
# for latent we must use pickle
|
||||
latent_file = f"{filename}_latent_{counter:05}.pt"
|
||||
torch.save(latent, full_output_folder / latent_file)
|
||||
# pickle.dump(latent, open(full_output_folder/ latent_file, "wb"))
|
||||
|
||||
# np.save(full_output_folder/ latent_file, latent[""].cpu().numpy())
|
||||
|
||||
return f"{filename_prefix}_{counter:05}"
|
||||
|
||||
|
||||
__nodes__ = [Debug, SaveTensors]
|
||||
|
||||
+3
-8
@@ -241,9 +241,6 @@ def normals_to_height(normals_img, seamless, progress_callback):
|
||||
class DeepBump:
|
||||
"""Normal & height maps generation from single pictures"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -264,7 +261,7 @@ class DeepBump:
|
||||
"LARGEST",
|
||||
],
|
||||
),
|
||||
"normals_to_height_seamless": (["TRUE", "FALSE"],),
|
||||
"normals_to_height_seamless": ("BOOLEAN", {"default": False}),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -279,7 +276,7 @@ class DeepBump:
|
||||
mode="Color to Normals",
|
||||
color_to_normals_overlap="SMALL",
|
||||
normals_to_curvature_blur_radius="SMALL",
|
||||
normals_to_height_seamless="TRUE",
|
||||
normals_to_height_seamless=True,
|
||||
):
|
||||
image = utils_inference.tensor2pil(image)
|
||||
|
||||
@@ -295,9 +292,7 @@ class DeepBump:
|
||||
in_img, normals_to_curvature_blur_radius, None
|
||||
)
|
||||
if mode == "Normals to Height":
|
||||
out_img = normals_to_height(
|
||||
in_img, normals_to_height_seamless == "TRUE", None
|
||||
)
|
||||
out_img = normals_to_height(in_img, normals_to_height_seamless, None)
|
||||
|
||||
out_img = (np.transpose(out_img, (1, 2, 0)) * 255).astype(np.uint8)
|
||||
|
||||
|
||||
+41
-16
@@ -4,9 +4,12 @@ import numpy as np
|
||||
import os
|
||||
from pathlib import Path
|
||||
import folder_paths
|
||||
from ..utils import pil2tensor, np2tensor, tensor2np
|
||||
|
||||
from basicsr.utils import imwrite
|
||||
|
||||
|
||||
from PIL import Image
|
||||
from ..utils import pil2tensor, tensor2pil, np2tensor, tensor2np
|
||||
import torch
|
||||
from ..log import NullWriter, log
|
||||
from comfy import model_management
|
||||
@@ -23,15 +26,39 @@ class LoadFaceEnhanceModel:
|
||||
|
||||
@classmethod
|
||||
def get_models_root(cls):
|
||||
return Path(folder_paths.models_dir) / "upscale_models"
|
||||
fr = Path(folder_paths.models_dir) / "face_restore"
|
||||
if fr.exists():
|
||||
return (fr, None)
|
||||
|
||||
um = Path(folder_paths.models_dir) / "upscale_models"
|
||||
return (fr, um) if um.exists() else (None, None)
|
||||
|
||||
@classmethod
|
||||
def get_models(cls):
|
||||
models_path = cls.get_models_root()
|
||||
fr_models_path, um_models_path = cls.get_models_root()
|
||||
|
||||
if fr_models_path is None and um_models_path is None:
|
||||
log.warning("Face restoration models not found.")
|
||||
return []
|
||||
if not fr_models_path.exists():
|
||||
log.warning(
|
||||
f"No Face Restore checkpoints found at {fr_models_path} (if you've used mtb before these checkpoints were saved in upscale_models before)"
|
||||
)
|
||||
log.warning(
|
||||
"For now we fallback to upscale_models but this will be removed in a future version"
|
||||
)
|
||||
if um_models_path.exists():
|
||||
return [
|
||||
x
|
||||
for x in um_models_path.iterdir()
|
||||
if x.name.endswith(".pth")
|
||||
and ("GFPGAN" in x.name or "RestoreFormer" in x.name)
|
||||
]
|
||||
return []
|
||||
|
||||
return [
|
||||
x
|
||||
for x in models_path.iterdir()
|
||||
for x in fr_models_path.iterdir()
|
||||
if x.name.endswith(".pth")
|
||||
and ("GFPGAN" in x.name or "RestoreFormer" in x.name)
|
||||
]
|
||||
@@ -57,7 +84,7 @@ class LoadFaceEnhanceModel:
|
||||
def load_model(self, model_name, upscale=2, bg_upsampler=None):
|
||||
basic = "RestoreFormer" not in model_name
|
||||
|
||||
root = self.get_models_root()
|
||||
fr_root, um_root = self.get_models_root()
|
||||
|
||||
if bg_upsampler is not None:
|
||||
log.warning(
|
||||
@@ -68,7 +95,9 @@ class LoadFaceEnhanceModel:
|
||||
|
||||
sys.stdout = NullWriter()
|
||||
model = GFPGANer(
|
||||
model_path=(root / model_name).as_posix(),
|
||||
model_path=(
|
||||
(fr_root if fr_root.exists() else um_root) / model_name
|
||||
).as_posix(),
|
||||
upscale=upscale,
|
||||
arch="clean" if basic else "RestoreFormer", # or original for v1.0 only
|
||||
channel_multiplier=2, # 1 for v1.0 only
|
||||
@@ -136,12 +165,12 @@ class RestoreFace:
|
||||
"image": ("IMAGE",),
|
||||
"model": ("FACEENHANCE_MODEL",),
|
||||
# Input are aligned faces
|
||||
"aligned": (["true", "false"], {"default": "false"}),
|
||||
"aligned": ("BOOLEAN", {"default": False}),
|
||||
# Only restore the center face
|
||||
"only_center_face": (["true", "false"], {"default": "false"}),
|
||||
"only_center_face": ("BOOLEAN", {"default": False}),
|
||||
# Adjustable weights
|
||||
"weight": ("FLOAT", {"default": 0.5}),
|
||||
"save_tmp_steps": (["true", "false"], {"default": "true"}),
|
||||
"save_tmp_steps": ("BOOLEAN", {"default": True}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,15 +212,11 @@ class RestoreFace:
|
||||
self,
|
||||
image: torch.Tensor,
|
||||
model: GFPGANer,
|
||||
aligned="false",
|
||||
only_center_face="false",
|
||||
aligned=False,
|
||||
only_center_face=False,
|
||||
weight=0.5,
|
||||
save_tmp_steps="true",
|
||||
save_tmp_steps=True,
|
||||
) -> Tuple[torch.Tensor]:
|
||||
save_tmp_steps = save_tmp_steps == "true"
|
||||
aligned = aligned == "true"
|
||||
only_center_face = only_center_face == "true"
|
||||
|
||||
out = [
|
||||
self.do_restore(
|
||||
image[i], model, aligned, only_center_face, weight, save_tmp_steps
|
||||
|
||||
+63
-19
@@ -2,17 +2,16 @@
|
||||
import onnxruntime
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
from typing import List, Set, Tuple, Union, Optional
|
||||
from typing import List, Set, Union, Optional
|
||||
import cv2
|
||||
import folder_paths
|
||||
import glob
|
||||
import insightface
|
||||
import numpy as np
|
||||
import os
|
||||
import tempfile
|
||||
import torch
|
||||
from insightface.model_zoo.inswapper import INSwapper
|
||||
from ..utils import pil2tensor, tensor2pil
|
||||
from ..utils import pil2tensor, tensor2pil, download_antelopev2
|
||||
from ..log import mklog, NullWriter
|
||||
import sys
|
||||
import comfy.model_management as model_management
|
||||
@@ -23,6 +22,46 @@ import comfy.model_management as model_management
|
||||
log = mklog(__name__)
|
||||
|
||||
|
||||
class LoadFaceAnalysisModel:
|
||||
"""Loads a face analysis model"""
|
||||
|
||||
models = []
|
||||
|
||||
@staticmethod
|
||||
def get_models() -> List[str]:
|
||||
models_path = os.path.join(folder_paths.models_dir, "insightface/*")
|
||||
models = glob.glob(models_path)
|
||||
models = [
|
||||
Path(x).name for x in models if x.endswith(".onnx") or x.endswith(".pth")
|
||||
]
|
||||
return models
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"faceswap_model": (
|
||||
["antelopev2", "buffalo_l", "buffalo_m", "buffalo_sc"],
|
||||
{"default": "buffalo_l"},
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("FACE_ANALYSIS_MODEL",)
|
||||
FUNCTION = "load_model"
|
||||
CATEGORY = "mtb/facetools"
|
||||
|
||||
def load_model(self, faceswap_model: str):
|
||||
if faceswap_model == "antelopev2":
|
||||
download_antelopev2()
|
||||
|
||||
face_analyser = insightface.app.FaceAnalysis(
|
||||
name=faceswap_model,
|
||||
root=os.path.join(folder_paths.models_dir, "insightface"),
|
||||
)
|
||||
return (face_analyser,)
|
||||
|
||||
|
||||
class LoadFaceSwapModel:
|
||||
"""Loads a faceswap model"""
|
||||
|
||||
@@ -81,9 +120,10 @@ class FaceSwap:
|
||||
"image": ("IMAGE",),
|
||||
"reference": ("IMAGE",),
|
||||
"faces_index": ("STRING", {"default": "0"}),
|
||||
"faceanalysis_model": ("FACE_ANALYSIS_MODEL", {"default": "None"}),
|
||||
"faceswap_model": ("FACESWAP_MODEL", {"default": "None"}),
|
||||
},
|
||||
"optional": {"debug": (["true", "false"], {"default": "false"})},
|
||||
"optional": {},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
@@ -95,8 +135,8 @@ class FaceSwap:
|
||||
image: torch.Tensor,
|
||||
reference: torch.Tensor,
|
||||
faces_index: str,
|
||||
faceanalysis_model,
|
||||
faceswap_model,
|
||||
debug="false",
|
||||
):
|
||||
def do_swap(img):
|
||||
model_management.throw_exception_if_processing_interrupted()
|
||||
@@ -106,7 +146,7 @@ class FaceSwap:
|
||||
int(x) for x in faces_index.strip(",").split(",") if x.isnumeric()
|
||||
}
|
||||
sys.stdout = NullWriter()
|
||||
swapped = swap_face(ref, img, faceswap_model, face_ids)
|
||||
swapped = swap_face(faceanalysis_model, ref, img, faceswap_model, face_ids)
|
||||
sys.stdout = sys.__stdout__
|
||||
return pil2tensor(swapped)
|
||||
|
||||
@@ -120,8 +160,8 @@ class FaceSwap:
|
||||
image = do_swap(image)
|
||||
|
||||
else:
|
||||
image = [do_swap(image[i]) for i in range(batch_count)]
|
||||
image = torch.cat(image, dim=0)
|
||||
image_batch = [do_swap(image[i]) for i in range(batch_count)]
|
||||
image = torch.cat(image_batch, dim=0)
|
||||
|
||||
return (image,)
|
||||
|
||||
@@ -130,17 +170,18 @@ class FaceSwap:
|
||||
|
||||
|
||||
# region face swap utils
|
||||
def get_face_single(img_data: np.ndarray, face_index=0, det_size=(640, 640)):
|
||||
face_analyser = insightface.app.FaceAnalysis(
|
||||
name="buffalo_l", root=os.path.join(folder_paths.models_dir, "insightface")
|
||||
)
|
||||
def get_face_single(
|
||||
face_analyser, img_data: np.ndarray, face_index=0, det_size=(640, 640)
|
||||
):
|
||||
face_analyser.prepare(ctx_id=0, det_size=det_size)
|
||||
face = face_analyser.get(img_data)
|
||||
|
||||
if len(face) == 0 and det_size[0] > 320 and det_size[1] > 320:
|
||||
log.debug("No face ed, trying again with smaller image")
|
||||
det_size_half = (det_size[0] // 2, det_size[1] // 2)
|
||||
return get_face_single(img_data, face_index=face_index, det_size=det_size_half)
|
||||
return get_face_single(
|
||||
face_analyser, img_data, face_index=face_index, det_size=det_size_half
|
||||
)
|
||||
|
||||
try:
|
||||
return sorted(face, key=lambda x: x.bbox[0])[face_index]
|
||||
@@ -149,6 +190,7 @@ def get_face_single(img_data: np.ndarray, face_index=0, det_size=(640, 640)):
|
||||
|
||||
|
||||
def swap_face(
|
||||
face_analyser,
|
||||
source_img: Union[Image.Image, List[Image.Image]],
|
||||
target_img: Union[Image.Image, List[Image.Image]],
|
||||
face_swapper_model,
|
||||
@@ -160,14 +202,16 @@ def swap_face(
|
||||
result_image = target_img
|
||||
|
||||
if face_swapper_model is not None:
|
||||
source_img = cv2.cvtColor(np.array(source_img), cv2.COLOR_RGB2BGR)
|
||||
target_img = cv2.cvtColor(np.array(target_img), cv2.COLOR_RGB2BGR)
|
||||
source_face = get_face_single(source_img, face_index=0)
|
||||
cv_source_img = cv2.cvtColor(np.array(source_img), cv2.COLOR_RGB2BGR)
|
||||
cv_target_img = cv2.cvtColor(np.array(target_img), cv2.COLOR_RGB2BGR)
|
||||
source_face = get_face_single(face_analyser, cv_source_img, face_index=0)
|
||||
if source_face is not None:
|
||||
result = target_img
|
||||
result = cv_target_img
|
||||
|
||||
for face_num in faces_index:
|
||||
target_face = get_face_single(target_img, face_index=face_num)
|
||||
target_face = get_face_single(
|
||||
face_analyser, cv_target_img, face_index=face_num
|
||||
)
|
||||
if target_face is not None:
|
||||
sys.stdout = NullWriter()
|
||||
result = face_swapper_model.get(result, target_face, source_face)
|
||||
@@ -186,4 +230,4 @@ def swap_face(
|
||||
# endregion face swap utils
|
||||
|
||||
|
||||
__nodes__ = [FaceSwap, LoadFaceSwapModel]
|
||||
__nodes__ = [FaceSwap, LoadFaceSwapModel, LoadFaceAnalysisModel]
|
||||
|
||||
-116
@@ -1,116 +0,0 @@
|
||||
import qrcode
|
||||
from ..utils import pil2tensor
|
||||
from PIL import Image
|
||||
|
||||
# class MtbExamples:
|
||||
# """MTB Example Images"""
|
||||
|
||||
# def __init__(self):
|
||||
# pass
|
||||
|
||||
# @classmethod
|
||||
# @lru_cache(maxsize=1)
|
||||
# def get_root(cls):
|
||||
# return here / "examples" / "samples"
|
||||
|
||||
# @classmethod
|
||||
# def INPUT_TYPES(cls):
|
||||
# input_dir = cls.get_root()
|
||||
# files = [f.name for f in input_dir.iterdir() if f.is_file()]
|
||||
# return {
|
||||
# "required": {"image": (sorted(files),)},
|
||||
# }
|
||||
|
||||
# RETURN_TYPES = ("IMAGE", "MASK")
|
||||
# FUNCTION = "do_mtb_examples"
|
||||
# CATEGORY = "fun"
|
||||
|
||||
# def do_mtb_examples(self, image, index):
|
||||
# image_path = (self.get_root() / image).as_posix()
|
||||
|
||||
# i = Image.open(image_path)
|
||||
# i = ImageOps.exif_transpose(i)
|
||||
# image = i.convert("RGB")
|
||||
# image = np.array(image).astype(np.float32) / 255.0
|
||||
# image = torch.from_numpy(image)[None,]
|
||||
# if "A" in i.getbands():
|
||||
# mask = np.array(i.getchannel("A")).astype(np.float32) / 255.0
|
||||
# mask = 1.0 - torch.from_numpy(mask)
|
||||
# else:
|
||||
# mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
|
||||
# return (image, mask)
|
||||
|
||||
# @classmethod
|
||||
# def IS_CHANGED(cls, image):
|
||||
# image_path = (cls.get_root() / image).as_posix()
|
||||
|
||||
# m = hashlib.sha256()
|
||||
# with open(image_path, "rb") as f:
|
||||
# m.update(f.read())
|
||||
# return m.digest().hex()
|
||||
|
||||
|
||||
class QrCode:
|
||||
"""Basic QR Code generator"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"url": ("STRING", {"default": "https://www.github.com"}),
|
||||
"width": (
|
||||
"INT",
|
||||
{"default": 256, "max": 8096, "min": 0, "step": 1},
|
||||
),
|
||||
"height": (
|
||||
"INT",
|
||||
{"default": 256, "max": 8096, "min": 0, "step": 1},
|
||||
),
|
||||
"error_correct": (("L", "M", "Q", "H"), {"default": "L"}),
|
||||
"box_size": ("INT", {"default": 10, "max": 8096, "min": 0, "step": 1}),
|
||||
"border": ("INT", {"default": 4, "max": 8096, "min": 0, "step": 1}),
|
||||
"invert": (("True", "False"), {"default": "False"}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "do_qr"
|
||||
CATEGORY = "mtb/generate"
|
||||
|
||||
def do_qr(self, url, width, height, error_correct, box_size, border, invert):
|
||||
if error_correct == "L" or error_correct not in ["M", "Q", "H"]:
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_L
|
||||
elif error_correct == "M":
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_M
|
||||
elif error_correct == "Q":
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_Q
|
||||
else:
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_H
|
||||
|
||||
qr = qrcode.QRCode(
|
||||
version=1,
|
||||
error_correction=error_correct,
|
||||
box_size=box_size,
|
||||
border=border,
|
||||
)
|
||||
qr.add_data(url)
|
||||
qr.make(fit=True)
|
||||
|
||||
back_color = (255, 255, 255) if invert == "True" else (0, 0, 0)
|
||||
fill_color = (0, 0, 0) if invert == "True" else (255, 255, 255)
|
||||
|
||||
code = img = qr.make_image(back_color=back_color, fill_color=fill_color)
|
||||
|
||||
# that we now resize without filtering
|
||||
code = code.resize((width, height), Image.NEAREST)
|
||||
|
||||
return (pil2tensor(code),)
|
||||
|
||||
|
||||
__nodes__ = [
|
||||
QrCode,
|
||||
# MtbExamples,
|
||||
]
|
||||
@@ -0,0 +1,286 @@
|
||||
import qrcode
|
||||
from ..utils import pil2tensor
|
||||
from ..utils import comfy_dir
|
||||
from typing import cast
|
||||
from PIL import Image
|
||||
from ..log import log
|
||||
|
||||
# class MtbExamples:
|
||||
# """MTB Example Images"""
|
||||
|
||||
# def __init__(self):
|
||||
# pass
|
||||
|
||||
# @classmethod
|
||||
# @lru_cache(maxsize=1)
|
||||
# def get_root(cls):
|
||||
# return here / "examples" / "samples"
|
||||
|
||||
# @classmethod
|
||||
# def INPUT_TYPES(cls):
|
||||
# input_dir = cls.get_root()
|
||||
# files = [f.name for f in input_dir.iterdir() if f.is_file()]
|
||||
# return {
|
||||
# "required": {"image": (sorted(files),)},
|
||||
# }
|
||||
|
||||
# RETURN_TYPES = ("IMAGE", "MASK")
|
||||
# FUNCTION = "do_mtb_examples"
|
||||
# CATEGORY = "fun"
|
||||
|
||||
# def do_mtb_examples(self, image, index):
|
||||
# image_path = (self.get_root() / image).as_posix()
|
||||
|
||||
# i = Image.open(image_path)
|
||||
# i = ImageOps.exif_transpose(i)
|
||||
# image = i.convert("RGB")
|
||||
# image = np.array(image).astype(np.float32) / 255.0
|
||||
# image = torch.from_numpy(image)[None,]
|
||||
# if "A" in i.getbands():
|
||||
# mask = np.array(i.getchannel("A")).astype(np.float32) / 255.0
|
||||
# mask = 1.0 - torch.from_numpy(mask)
|
||||
# else:
|
||||
# mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
|
||||
# return (image, mask)
|
||||
|
||||
# @classmethod
|
||||
# def IS_CHANGED(cls, image):
|
||||
# image_path = (cls.get_root() / image).as_posix()
|
||||
|
||||
# m = hashlib.sha256()
|
||||
# with open(image_path, "rb") as f:
|
||||
# m.update(f.read())
|
||||
# return m.digest().hex()
|
||||
|
||||
|
||||
class UnsplashImage:
|
||||
"""Unsplash Image given a keyword and a size"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"width": ("INT", {"default": 512, "max": 8096, "min": 0, "step": 1}),
|
||||
"height": ("INT", {"default": 512, "max": 8096, "min": 0, "step": 1}),
|
||||
"random_seed": ("INT", {"default": 0, "max": 1e5, "min": 0, "step": 1}),
|
||||
},
|
||||
"optional": {
|
||||
"keyword": ("STRING", {"default": "nature"}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "do_unsplash_image"
|
||||
CATEGORY = "mtb/generate"
|
||||
|
||||
def do_unsplash_image(self, width, height, random_seed, keyword=None):
|
||||
import requests
|
||||
import io
|
||||
|
||||
base_url = "https://source.unsplash.com/random/"
|
||||
|
||||
if width and height:
|
||||
base_url += f"/{width}x{height}"
|
||||
|
||||
if keyword:
|
||||
keyword = keyword.replace(" ", "%20")
|
||||
base_url += f"?{keyword}&{random_seed}"
|
||||
else:
|
||||
base_url += f"?&{random_seed}"
|
||||
try:
|
||||
log.debug(f"Getting unsplash image from {base_url}")
|
||||
response = requests.get(base_url)
|
||||
response.raise_for_status()
|
||||
|
||||
image = Image.open(io.BytesIO(response.content))
|
||||
return (
|
||||
pil2tensor(
|
||||
image,
|
||||
),
|
||||
)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("Error retrieving image:", e)
|
||||
return (None,)
|
||||
|
||||
|
||||
class QrCode:
|
||||
"""Basic QR Code generator"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"url": ("STRING", {"default": "https://www.github.com"}),
|
||||
"width": (
|
||||
"INT",
|
||||
{"default": 256, "max": 8096, "min": 0, "step": 1},
|
||||
),
|
||||
"height": (
|
||||
"INT",
|
||||
{"default": 256, "max": 8096, "min": 0, "step": 1},
|
||||
),
|
||||
"error_correct": (("L", "M", "Q", "H"), {"default": "L"}),
|
||||
"box_size": ("INT", {"default": 10, "max": 8096, "min": 0, "step": 1}),
|
||||
"border": ("INT", {"default": 4, "max": 8096, "min": 0, "step": 1}),
|
||||
"invert": (("BOOLEAN",), {"default": False}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "do_qr"
|
||||
CATEGORY = "mtb/generate"
|
||||
|
||||
def do_qr(self, url, width, height, error_correct, box_size, border, invert):
|
||||
log.warning(
|
||||
"This node will soon be deprecated, there are much better alternatives like https://github.com/coreyryanhanson/comfy-qr"
|
||||
)
|
||||
if error_correct == "L" or error_correct not in ["M", "Q", "H"]:
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_L
|
||||
elif error_correct == "M":
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_M
|
||||
elif error_correct == "Q":
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_Q
|
||||
else:
|
||||
error_correct = qrcode.constants.ERROR_CORRECT_H
|
||||
|
||||
qr = qrcode.QRCode(
|
||||
version=1,
|
||||
error_correction=error_correct,
|
||||
box_size=box_size,
|
||||
border=border,
|
||||
)
|
||||
qr.add_data(url)
|
||||
qr.make(fit=True)
|
||||
|
||||
back_color = (255, 255, 255) if invert else (0, 0, 0)
|
||||
fill_color = (0, 0, 0) if invert else (255, 255, 255)
|
||||
|
||||
code = img = qr.make_image(back_color=back_color, fill_color=fill_color)
|
||||
|
||||
# that we now resize without filtering
|
||||
code = code.resize((width, height), Image.NEAREST)
|
||||
|
||||
return (pil2tensor(code),)
|
||||
|
||||
|
||||
def bbox_dim(bbox):
|
||||
left, upper, right, lower = bbox
|
||||
width = right - left
|
||||
height = lower - upper
|
||||
return width, height
|
||||
|
||||
|
||||
class TextToImage:
|
||||
"""Utils to convert text to image using a font
|
||||
|
||||
|
||||
The tool looks for any .ttf file in the Comfy folder hierarchy.
|
||||
"""
|
||||
|
||||
fonts = {}
|
||||
|
||||
def __init__(self):
|
||||
# - This is executed when the graph is executed, we could conditionaly reload fonts there
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def CACHE_FONTS(cls):
|
||||
font_extensions = ["*.ttf", "*.otf", "*.woff", "*.woff2", "*.eot"]
|
||||
fonts = []
|
||||
|
||||
for extension in font_extensions:
|
||||
fonts.extend(comfy_dir.glob(f"**/{extension}"))
|
||||
|
||||
if not fonts:
|
||||
log.warn(
|
||||
"> No fonts found in the comfy folder, place at least one font file somewhere in ComfyUI's hierarchy"
|
||||
)
|
||||
else:
|
||||
log.debug(f"> Found {len(fonts)} fonts")
|
||||
|
||||
for font in fonts:
|
||||
log.debug(f"Adding font {font}")
|
||||
cls.fonts[font.stem] = font.as_posix()
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
if not cls.fonts:
|
||||
cls.CACHE_FONTS()
|
||||
else:
|
||||
log.debug(f"Using cached fonts (count: {len(cls.fonts)})")
|
||||
return {
|
||||
"required": {
|
||||
"text": (
|
||||
"STRING",
|
||||
{"default": "Hello world!"},
|
||||
),
|
||||
"font": ((sorted(cls.fonts.keys())),),
|
||||
"wrap": (
|
||||
"INT",
|
||||
{"default": 120, "min": 0, "max": 8096, "step": 1},
|
||||
),
|
||||
"font_size": (
|
||||
"INT",
|
||||
{"default": 12, "min": 1, "max": 2500, "step": 1},
|
||||
),
|
||||
"width": (
|
||||
"INT",
|
||||
{"default": 512, "min": 1, "max": 8096, "step": 1},
|
||||
),
|
||||
"height": (
|
||||
"INT",
|
||||
{"default": 512, "min": 1, "max": 8096, "step": 1},
|
||||
),
|
||||
# "position": (["INT"], {"default": 0, "min": 0, "max": 100, "step": 1}),
|
||||
"color": (
|
||||
"COLOR",
|
||||
{"default": "black"},
|
||||
),
|
||||
"background": (
|
||||
"COLOR",
|
||||
{"default": "white"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
RETURN_NAMES = ("image",)
|
||||
FUNCTION = "text_to_image"
|
||||
CATEGORY = "mtb/generate"
|
||||
|
||||
def text_to_image(
|
||||
self, text, font, wrap, font_size, width, height, color, background
|
||||
):
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import textwrap
|
||||
|
||||
font = self.fonts[font]
|
||||
font = cast(ImageFont.FreeTypeFont, ImageFont.truetype(font, font_size))
|
||||
if wrap == 0:
|
||||
wrap = width / font_size
|
||||
lines = textwrap.wrap(text, width=wrap)
|
||||
log.debug(f"Lines: {lines}")
|
||||
line_height = bbox_dim(font.getbbox("hg"))[1]
|
||||
img_height = height # line_height * len(lines)
|
||||
img_width = width # max(font.getsize(line)[0] for line in lines)
|
||||
|
||||
img = Image.new("RGBA", (img_width, img_height), background)
|
||||
draw = ImageDraw.Draw(img)
|
||||
y_text = 0
|
||||
# - bbox is [left, upper, right, lower]
|
||||
for line in lines:
|
||||
width, height = bbox_dim(font.getbbox(line))
|
||||
draw.text((0, y_text), line, color, font=font)
|
||||
y_text += height
|
||||
|
||||
# img.save(os.path.join(folder_paths.base_path, f'{str(uuid.uuid4())}.png'))
|
||||
return (pil2tensor(img),)
|
||||
|
||||
|
||||
__nodes__ = [
|
||||
QrCode,
|
||||
UnsplashImage,
|
||||
TextToImage
|
||||
# MtbExamples,
|
||||
]
|
||||
+165
-5
@@ -1,4 +1,133 @@
|
||||
from ..log import log
|
||||
from PIL import Image
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import torch
|
||||
import json
|
||||
from comfy.cli_args import args
|
||||
from ..utils import pil2tensor, apply_easing
|
||||
import io
|
||||
import numpy as np
|
||||
|
||||
|
||||
def get_image(filename, subfolder, folder_type):
|
||||
log.debug(f"Getting image {filename} from {subfolder} of {folder_type}")
|
||||
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
|
||||
url_values = urllib.parse.urlencode(data)
|
||||
with urllib.request.urlopen(
|
||||
f"http://{args.listen}:{args.port}/view?{url_values}"
|
||||
) as response:
|
||||
return io.BytesIO(response.read())
|
||||
|
||||
|
||||
class GetBatchFromHistory:
|
||||
"""Very experimental node to load images from the history of the server.
|
||||
|
||||
Queue items without output are ignored in the count."""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"enable": ("BOOLEAN", {"default": True}),
|
||||
"count": ("INT", {"default": 1, "min": 0}),
|
||||
"offset": ("INT", {"default": 0, "min": -1e9, "max": 1e9}),
|
||||
"internal_count": ("INT", {"default": 0}),
|
||||
},
|
||||
"optional": {
|
||||
"passthrough_image": ("IMAGE",),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
RETURN_NAMES = ("images",)
|
||||
CATEGORY = "mtb/animation"
|
||||
FUNCTION = "load_from_history"
|
||||
|
||||
def load_from_history(
|
||||
self,
|
||||
enable=True,
|
||||
count=0,
|
||||
offset=0,
|
||||
internal_count=0, # hacky way to invalidate the node
|
||||
passthrough_image=None,
|
||||
):
|
||||
if not enable or count == 0:
|
||||
if passthrough_image is not None:
|
||||
log.debug("Using passthrough image")
|
||||
return (passthrough_image,)
|
||||
log.debug("Load from history is disabled for this iteration")
|
||||
return (torch.zeros(0),)
|
||||
frames = []
|
||||
|
||||
with urllib.request.urlopen(
|
||||
f"http://{args.listen}:{args.port}/history"
|
||||
) as response:
|
||||
return self.load_batch_frames(response, offset, count, frames)
|
||||
|
||||
def load_batch_frames(self, response, offset, count, frames):
|
||||
history = json.loads(response.read())
|
||||
|
||||
output_images = []
|
||||
|
||||
for run in history.values():
|
||||
for node_output in run["outputs"].values():
|
||||
if "images" in node_output:
|
||||
for image in node_output["images"]:
|
||||
image_data = get_image(
|
||||
image["filename"], image["subfolder"], image["type"]
|
||||
)
|
||||
output_images.append(image_data)
|
||||
|
||||
if not output_images:
|
||||
return (torch.zeros(0),)
|
||||
|
||||
# Directly get desired range of images
|
||||
start_index = max(len(output_images) - offset - count, 0)
|
||||
end_index = len(output_images) - offset
|
||||
selected_images = output_images[start_index:end_index]
|
||||
|
||||
frames = [Image.open(image) for image in selected_images]
|
||||
|
||||
if not frames:
|
||||
return (torch.zeros(0),)
|
||||
elif len(frames) != count:
|
||||
log.warning(f"Expected {count} images, got {len(frames)} instead")
|
||||
|
||||
output = pil2tensor(frames)
|
||||
|
||||
return (output,)
|
||||
|
||||
|
||||
class AnyToString:
|
||||
"""Tries to take any input and convert it to a string"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {"input": ("*")},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING",)
|
||||
FUNCTION = "do_str"
|
||||
CATEGORY = "mtb/converters"
|
||||
|
||||
def do_str(self, input):
|
||||
if isinstance(input, str):
|
||||
return (input,)
|
||||
elif isinstance(input, torch.Tensor):
|
||||
return (f"Tensor of shape {input.shape} and dtype {input.dtype}",)
|
||||
elif isinstance(input, Image.Image):
|
||||
return (f"PIL Image of size {input.size} and mode {input.mode}",)
|
||||
elif isinstance(input, np.ndarray):
|
||||
return (f"Numpy array of shape {input.shape} and dtype {input.dtype}",)
|
||||
|
||||
elif isinstance(input, dict):
|
||||
return (f"Dictionary of {len(input)} items, with keys {input.keys()}",)
|
||||
|
||||
else:
|
||||
log.debug(f"Falling back to string conversion of {input}")
|
||||
return (str(input),)
|
||||
|
||||
|
||||
class StringReplace:
|
||||
@@ -38,11 +167,38 @@ class FitNumber:
|
||||
return {
|
||||
"required": {
|
||||
"value": ("FLOAT", {"default": 0, "forceInput": True}),
|
||||
"clamp": ("BOOL", {"default": False}),
|
||||
"clamp": ("BOOLEAN", {"default": False}),
|
||||
"source_min": ("FLOAT", {"default": 0.0}),
|
||||
"source_max": ("FLOAT", {"default": 1.0}),
|
||||
"target_min": ("FLOAT", {"default": 0.0}),
|
||||
"target_max": ("FLOAT", {"default": 1.0}),
|
||||
"easing": (
|
||||
[
|
||||
"Linear",
|
||||
"Sine In",
|
||||
"Sine Out",
|
||||
"Sine In/Out",
|
||||
"Quart In",
|
||||
"Quart Out",
|
||||
"Quart In/Out",
|
||||
"Cubic In",
|
||||
"Cubic Out",
|
||||
"Cubic In/Out",
|
||||
"Circ In",
|
||||
"Circ Out",
|
||||
"Circ In/Out",
|
||||
"Back In",
|
||||
"Back Out",
|
||||
"Back In/Out",
|
||||
"Elastic In",
|
||||
"Elastic Out",
|
||||
"Elastic In/Out",
|
||||
"Bounce In",
|
||||
"Bounce Out",
|
||||
"Bounce In/Out",
|
||||
],
|
||||
{"default": "Linear"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,10 +214,14 @@ class FitNumber:
|
||||
source_max: float,
|
||||
target_min: float,
|
||||
target_max: float,
|
||||
easing: str,
|
||||
):
|
||||
res = target_min + (target_max - target_min) * (value - source_min) / (
|
||||
source_max - source_min
|
||||
)
|
||||
normalized_value = (value - source_min) / (source_max - source_min)
|
||||
|
||||
eased_value = apply_easing(normalized_value, easing)
|
||||
|
||||
# - Convert the eased value to the target range
|
||||
res = target_min + (target_max - target_min) * eased_value
|
||||
|
||||
if clamp:
|
||||
if target_min > target_max:
|
||||
@@ -72,4 +232,4 @@ class FitNumber:
|
||||
return (res,)
|
||||
|
||||
|
||||
__nodes__ = [StringReplace, FitNumber]
|
||||
__nodes__ = [StringReplace, FitNumber, GetBatchFromHistory, AnyToString]
|
||||
|
||||
@@ -6,101 +6,11 @@ import folder_paths
|
||||
from ..log import log
|
||||
import torch
|
||||
from frame_interpolation.eval import util, interpolator
|
||||
from ..utils import tensor2np
|
||||
import numpy as np
|
||||
import comfy
|
||||
from PIL import Image
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import json
|
||||
import comfy.utils
|
||||
import tensorflow as tf
|
||||
import comfy.model_management as model_management
|
||||
import io
|
||||
|
||||
from comfy.cli_args import args
|
||||
from ..utils import pil2tensor
|
||||
|
||||
|
||||
def get_image(filename, subfolder, folder_type):
|
||||
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
|
||||
url_values = urllib.parse.urlencode(data)
|
||||
with urllib.request.urlopen(
|
||||
"http://{}:{}/view?{}".format(args.listen, args.port, url_values)
|
||||
) as response:
|
||||
return io.BytesIO(response.read())
|
||||
|
||||
|
||||
class GetBatchFromHistory:
|
||||
"""Very experimental node to load images from the history of the server.
|
||||
|
||||
Queue items without output are ignore in the count."""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"enable": ("BOOL", {"default": True}),
|
||||
"count": ("INT", {"default": 1, "min": 0}),
|
||||
"offset": ("INT", {"default": 0, "min": -1e9, "max": 1e9}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
RETURN_NAMES = "images"
|
||||
CATEGORY = "mtb/animation"
|
||||
FUNCTION = "load_from_history"
|
||||
|
||||
def load_from_history(
|
||||
self,
|
||||
enable=True,
|
||||
count=0,
|
||||
offset=0,
|
||||
):
|
||||
if not enable or count == 0:
|
||||
log.debug("Load from history is disabled for this iteration")
|
||||
return (torch.zeros(0),)
|
||||
frames = []
|
||||
|
||||
with urllib.request.urlopen(
|
||||
"http://{}:{}/history".format(args.listen, args.port)
|
||||
) as response:
|
||||
history = json.loads(response.read())
|
||||
|
||||
output_images = []
|
||||
for k, run in history.items():
|
||||
for o in run["outputs"]:
|
||||
for node_id in run["outputs"]:
|
||||
node_output = run["outputs"][node_id]
|
||||
if "images" in node_output:
|
||||
images_output = []
|
||||
for image in node_output["images"]:
|
||||
image_data = get_image(
|
||||
image["filename"], image["subfolder"], image["type"]
|
||||
)
|
||||
images_output.append(image_data)
|
||||
output_images.extend(images_output)
|
||||
if len(output_images) == 0:
|
||||
return (torch.zeros(0),)
|
||||
for i, image in enumerate(list(reversed(output_images))):
|
||||
if i < offset:
|
||||
continue
|
||||
if i >= offset + count:
|
||||
break
|
||||
# Decode image as tensor
|
||||
img = Image.open(image)
|
||||
log.debug(f"Image from history {i} of shape {img.size}")
|
||||
frames.append(img)
|
||||
|
||||
# Display the shape of the tensor
|
||||
# print("Tensor shape:", image_tensor.shape)
|
||||
|
||||
# return (output_images,)
|
||||
|
||||
output = pil2tensor(
|
||||
list(reversed(frames)),
|
||||
)
|
||||
|
||||
return (output,)
|
||||
|
||||
|
||||
class LoadFilmModel:
|
||||
@@ -145,9 +55,6 @@ class LoadFilmModel:
|
||||
class FilmInterpolation:
|
||||
"""Google Research FILM frame interpolation for large motion"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -210,9 +117,6 @@ class FilmInterpolation:
|
||||
class ConcatImages:
|
||||
"""Add images to batch"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "concat_images"
|
||||
CATEGORY = "mtb/image"
|
||||
@@ -247,9 +151,4 @@ class ConcatImages:
|
||||
return (self.concatenate_tensors(imageA, imageB),)
|
||||
|
||||
|
||||
__nodes__ = [
|
||||
LoadFilmModel,
|
||||
FilmInterpolation,
|
||||
ConcatImages,
|
||||
GetBatchFromHistory,
|
||||
]
|
||||
__nodes__ = [LoadFilmModel, FilmInterpolation, ConcatImages]
|
||||
|
||||
+67
-120
@@ -1,34 +1,27 @@
|
||||
import torch
|
||||
from skimage.filters import gaussian
|
||||
from skimage.restoration import denoise_tv_chambolle
|
||||
from skimage.util import compare_images
|
||||
from skimage.color import rgb2hsv, hsv2rgb
|
||||
import numpy as np
|
||||
import torchvision.transforms.functional as F
|
||||
from PIL import Image, ImageChops
|
||||
from ..utils import tensor2pil, pil2tensor, np2tensor, tensor2np
|
||||
import cv2
|
||||
import torch.nn.functional as F
|
||||
from PIL import Image
|
||||
from ..utils import tensor2pil, pil2tensor, tensor2np
|
||||
import torch
|
||||
from ..log import log
|
||||
import folder_paths
|
||||
from PIL.PngImagePlugin import PngInfo
|
||||
import json
|
||||
import os
|
||||
import comfy.model_management as model_management
|
||||
import math
|
||||
|
||||
|
||||
try:
|
||||
from cv2.ximgproc import guidedFilter
|
||||
except ImportError:
|
||||
log.warning("cv2.ximgproc.guidedFilter not found, use opencv-contrib-python")
|
||||
# try:
|
||||
# from cv2.ximgproc import guidedFilter
|
||||
# except ImportError:
|
||||
# log.warning("cv2.ximgproc.guidedFilter not found, use opencv-contrib-python")
|
||||
|
||||
|
||||
class ColorCorrect:
|
||||
"""Various color correction methods"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -187,9 +180,6 @@ class ColorCorrect:
|
||||
class ImageCompare:
|
||||
"""Compare two images and return a difference image"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -252,9 +242,6 @@ class LoadImageFromUrl:
|
||||
class Blur:
|
||||
"""Blur an image using a Gaussian filter."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -312,9 +299,6 @@ class Blur:
|
||||
class MaskToImage:
|
||||
"""Converts a mask (alpha) to an RGB image with a color and background"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -383,16 +367,13 @@ class ColoredImage:
|
||||
class ImagePremultiply:
|
||||
"""Premultiply image with mask"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"image": ("IMAGE",),
|
||||
"mask": ("MASK",),
|
||||
"invert": (["True", "False"], {"default": "False"}),
|
||||
"invert": ("BOOLEAN", {"default": False}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,8 +382,6 @@ class ImagePremultiply:
|
||||
FUNCTION = "premultiply"
|
||||
|
||||
def premultiply(self, image, mask, invert):
|
||||
invert = invert == "True"
|
||||
|
||||
images = tensor2pil(image)
|
||||
if invert:
|
||||
masks = tensor2pil(mask) # .convert("L")
|
||||
@@ -433,9 +412,6 @@ class ImagePremultiply:
|
||||
class ImageResizeFactor:
|
||||
"""Extracted mostly from WAS Node Suite, with a few edits (most notably multiple image support) and less features."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -445,10 +421,18 @@ class ImageResizeFactor:
|
||||
"FLOAT",
|
||||
{"default": 2, "min": 0.01, "max": 16.0, "step": 0.01},
|
||||
),
|
||||
"supersample": (["true", "false"], {"default": "true"}),
|
||||
"supersample": ("BOOLEAN", {"default": True}),
|
||||
"resampling": (
|
||||
["lanczos", "nearest", "bilinear", "bicubic"],
|
||||
{"default": "lanczos"},
|
||||
[
|
||||
"nearest",
|
||||
"linear",
|
||||
"bilinear",
|
||||
"bicubic",
|
||||
"trilinear",
|
||||
"area",
|
||||
"nearest-exact",
|
||||
],
|
||||
{"default": "nearest"},
|
||||
),
|
||||
},
|
||||
"optional": {
|
||||
@@ -460,97 +444,61 @@ class ImageResizeFactor:
|
||||
RETURN_TYPES = ("IMAGE", "MASK")
|
||||
FUNCTION = "resize"
|
||||
|
||||
def resize_image(
|
||||
self,
|
||||
image: torch.Tensor,
|
||||
factor: float = 0.5,
|
||||
supersample=False,
|
||||
resample="lanczos",
|
||||
mask=None,
|
||||
) -> torch.Tensor:
|
||||
model_management.throw_exception_if_processing_interrupted()
|
||||
batch_count = 1
|
||||
img = tensor2pil(image)
|
||||
|
||||
if isinstance(img, list):
|
||||
log.debug("Multiple images detected (list)")
|
||||
out = []
|
||||
for im in img:
|
||||
im = self.resize_image(
|
||||
pil2tensor(im), factor, supersample, resample, mask
|
||||
)
|
||||
out.append(im)
|
||||
return torch.cat(out, dim=0)
|
||||
elif isinstance(img, torch.Tensor):
|
||||
if len(image.shape) > 3:
|
||||
batch_count = image.size(0)
|
||||
|
||||
if batch_count > 1:
|
||||
log.debug("Multiple images detected (batch count)")
|
||||
out = [
|
||||
self.resize_image(image[i], factor, supersample, resample, mask)
|
||||
for i in range(batch_count)
|
||||
]
|
||||
return torch.cat(out, dim=0)
|
||||
|
||||
log.debug("Resizing image")
|
||||
# Get the current width and height of the image
|
||||
current_width, current_height = img.size
|
||||
|
||||
log.debug(f"Current width: {current_width}, Current height: {current_height}")
|
||||
|
||||
# Calculate the new width and height based on the given mode and parameters
|
||||
new_width, new_height = int(factor * current_width), int(
|
||||
factor * current_height
|
||||
)
|
||||
|
||||
log.debug(f"New width: {new_width}, New height: {new_height}")
|
||||
|
||||
# Define a dictionary of resampling filters
|
||||
resample_filters = {"nearest": 0, "bilinear": 2, "bicubic": 3, "lanczos": 1}
|
||||
|
||||
# Apply supersample
|
||||
if supersample == "true":
|
||||
super_size = (new_width * 8, new_height * 8)
|
||||
log.debug(f"Applying supersample: {super_size}")
|
||||
img = img.resize(
|
||||
super_size, resample=Image.Resampling(resample_filters[resample])
|
||||
)
|
||||
|
||||
# Resize the image using the given resampling filter
|
||||
resized_image = img.resize(
|
||||
(new_width, new_height),
|
||||
resample=Image.Resampling(resample_filters[resample]),
|
||||
)
|
||||
|
||||
return pil2tensor(resized_image)
|
||||
|
||||
def resize(
|
||||
self,
|
||||
image: torch.Tensor,
|
||||
factor: float,
|
||||
supersample: str,
|
||||
supersample: bool,
|
||||
resampling: str,
|
||||
mask=None,
|
||||
):
|
||||
log.debug(f"Resizing image with factor {factor} and resampling {resampling}")
|
||||
supersample = supersample == "true"
|
||||
batch_count = image.size(0)
|
||||
log.debug(f"Batch count: {batch_count}")
|
||||
if batch_count == 1:
|
||||
log.debug("Batch count is 1, returning single image")
|
||||
return (self.resize_image(image, factor, supersample, resampling),)
|
||||
# Check if the tensor has the correct dimension
|
||||
if len(image.shape) not in [3, 4]: # HxWxC or BxHxWxC
|
||||
raise ValueError("Expected image tensor of shape (H, W, C) or (B, H, W, C)")
|
||||
|
||||
# Transpose to CxHxW or BxCxHxW for PyTorch
|
||||
if len(image.shape) == 3:
|
||||
image = image.permute(2, 0, 1).unsqueeze(0) # CxHxW
|
||||
else:
|
||||
log.debug("Batch count is greater than 1, returning multiple images")
|
||||
images = [
|
||||
self.resize_image(image[i], factor, supersample, resampling)
|
||||
for i in range(batch_count)
|
||||
]
|
||||
images = torch.cat(images, dim=0)
|
||||
return (images,)
|
||||
image = image.permute(0, 3, 1, 2) # BxCxHxW
|
||||
|
||||
# Compute new dimensions
|
||||
B, C, H, W = image.shape
|
||||
new_H, new_W = int(H * factor), int(W * factor)
|
||||
|
||||
import math
|
||||
align_corner_filters = ("linear", "bilinear", "bicubic", "trilinear")
|
||||
# Resize the image
|
||||
resized_image = F.interpolate(
|
||||
image,
|
||||
size=(new_H, new_W),
|
||||
mode=resampling,
|
||||
align_corners=resampling in align_corner_filters,
|
||||
)
|
||||
|
||||
# Optionally supersample
|
||||
if supersample:
|
||||
resized_image = F.interpolate(
|
||||
resized_image,
|
||||
scale_factor=2,
|
||||
mode=resampling,
|
||||
align_corners=resampling in align_corner_filters,
|
||||
)
|
||||
|
||||
# Transpose back to the original format: BxHxWxC or HxWxC
|
||||
if len(image.shape) == 4:
|
||||
resized_image = resized_image.permute(0, 2, 3, 1)
|
||||
else:
|
||||
resized_image = resized_image.squeeze(0).permute(1, 2, 0)
|
||||
|
||||
# Apply mask if provided
|
||||
if mask is not None:
|
||||
if len(mask.shape) != len(resized_image.shape):
|
||||
raise ValueError(
|
||||
"Mask tensor should have the same dimensions as the image tensor"
|
||||
)
|
||||
resized_image = resized_image * mask
|
||||
|
||||
return (resized_image,)
|
||||
|
||||
|
||||
class SaveImageGrid:
|
||||
@@ -566,7 +514,7 @@ class SaveImageGrid:
|
||||
"required": {
|
||||
"images": ("IMAGE",),
|
||||
"filename_prefix": ("STRING", {"default": "ComfyUI"}),
|
||||
"save_intermediate": (["true", "false"], {"default": "false"}),
|
||||
"save_intermediate": ("BOOLEAN", {"default": False}),
|
||||
},
|
||||
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
||||
}
|
||||
@@ -607,11 +555,10 @@ class SaveImageGrid:
|
||||
self,
|
||||
images,
|
||||
filename_prefix="Grid",
|
||||
save_intermediate="false",
|
||||
save_intermediate=False,
|
||||
prompt=None,
|
||||
extra_pnginfo=None,
|
||||
):
|
||||
save_intermediate = save_intermediate == "true"
|
||||
(
|
||||
full_output_folder,
|
||||
filename,
|
||||
|
||||
+83
-59
@@ -1,4 +1,4 @@
|
||||
from ..utils import tensor2np
|
||||
from ..utils import tensor2np, PIL_FILTER_MAP
|
||||
import uuid
|
||||
import folder_paths
|
||||
from ..log import log
|
||||
@@ -7,13 +7,12 @@ import subprocess
|
||||
import torch
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class ExportToProres:
|
||||
"""Export to ProRes 4444 (Experimental)"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
class ExportWithFfmpeg:
|
||||
"""Export with FFmpeg (Experimental)"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
@@ -23,6 +22,11 @@ class ExportToProres:
|
||||
# "frames": ("FRAMES",),
|
||||
"fps": ("FLOAT", {"default": 24, "min": 1}),
|
||||
"prefix": ("STRING", {"default": "export"}),
|
||||
"format": (["mov", "mp4", "mkv", "avi"], {"default": "mov"}),
|
||||
"codec": (
|
||||
["prores_ks", "libx264", "libx265"],
|
||||
{"default": "prores_ks"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,13 +40,17 @@ class ExportToProres:
|
||||
images: torch.Tensor,
|
||||
fps: float,
|
||||
prefix: str,
|
||||
format: str,
|
||||
codec: str,
|
||||
):
|
||||
if images.size(0) == 0:
|
||||
return ("",)
|
||||
output_dir = Path(folder_paths.get_output_directory())
|
||||
id = f"{prefix}_{uuid.uuid4()}.mov"
|
||||
pix_fmt = "rgb48le" if codec == "prores_ks" else "yuv420p"
|
||||
file_ext = format
|
||||
file_id = f"{prefix}_{uuid.uuid4()}.{file_ext}"
|
||||
|
||||
log.debug(f"Exporting to {output_dir / id}")
|
||||
log.debug(f"Exporting to {output_dir / file_id}")
|
||||
|
||||
frames = tensor2np(images)
|
||||
log.debug(f"Frames type {type(frames[0])}")
|
||||
@@ -52,7 +60,7 @@ class ExportToProres:
|
||||
|
||||
height, width, _ = frames[0].shape
|
||||
|
||||
out_path = (output_dir / id).as_posix()
|
||||
out_path = (output_dir / file_id).as_posix()
|
||||
|
||||
# Prepare the FFmpeg command
|
||||
command = [
|
||||
@@ -65,17 +73,13 @@ class ExportToProres:
|
||||
"-s",
|
||||
f"{width}x{height}",
|
||||
"-pix_fmt",
|
||||
"rgb48le",
|
||||
pix_fmt,
|
||||
"-r",
|
||||
str(fps),
|
||||
"-i",
|
||||
"-",
|
||||
"-c:v",
|
||||
"prores_ks",
|
||||
"-profile:v",
|
||||
"4",
|
||||
"-pix_fmt",
|
||||
"yuva444p10le",
|
||||
codec,
|
||||
"-r",
|
||||
str(fps),
|
||||
"-y",
|
||||
@@ -94,6 +98,37 @@ class ExportToProres:
|
||||
return (out_path,)
|
||||
|
||||
|
||||
def prepare_animated_batch(
|
||||
batch: torch.Tensor,
|
||||
pingpong=False,
|
||||
resize_by=1.0,
|
||||
resample_filter: Optional[Image.Resampling] = None,
|
||||
image_type=np.uint8,
|
||||
) -> List[Image.Image]:
|
||||
images = tensor2np(batch)
|
||||
images = [frame.astype(image_type) for frame in images]
|
||||
|
||||
height, width, _ = batch[0].shape
|
||||
|
||||
if pingpong:
|
||||
reversed_frames = images[::-1]
|
||||
images.extend(reversed_frames)
|
||||
pil_images = [Image.fromarray(frame) for frame in images]
|
||||
|
||||
# Resize frames if necessary
|
||||
if abs(resize_by - 1.0) > 1e-6:
|
||||
new_width = int(width * resize_by)
|
||||
new_height = int(height * resize_by)
|
||||
pil_images_resized = [
|
||||
frame.resize((new_width, new_height), resample=resample_filter)
|
||||
for frame in pil_images
|
||||
]
|
||||
pil_images = pil_images_resized
|
||||
|
||||
return pil_images
|
||||
|
||||
|
||||
# todo: deprecate for apng
|
||||
class SaveGif:
|
||||
"""Save the images from the batch as a GIF"""
|
||||
|
||||
@@ -104,8 +139,12 @@ class SaveGif:
|
||||
"image": ("IMAGE",),
|
||||
"fps": ("INT", {"default": 12, "min": 1, "max": 120}),
|
||||
"resize_by": ("FLOAT", {"default": 1.0, "min": 0.1}),
|
||||
"pingpong": ("BOOL", {"default": False}),
|
||||
}
|
||||
"optimize": ("BOOLEAN", {"default": False}),
|
||||
"pingpong": ("BOOLEAN", {"default": False}),
|
||||
},
|
||||
"optional": {
|
||||
"resample_filter": (list(PIL_FILTER_MAP.keys()),),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ()
|
||||
@@ -113,59 +152,44 @@ class SaveGif:
|
||||
CATEGORY = "mtb/IO"
|
||||
FUNCTION = "save_gif"
|
||||
|
||||
def save_gif(self, image, fps=12, resize_by=1.0, pingpong=False):
|
||||
def save_gif(
|
||||
self,
|
||||
image,
|
||||
fps=12,
|
||||
resize_by=1.0,
|
||||
optimize=False,
|
||||
pingpong=False,
|
||||
resample_filter=None,
|
||||
):
|
||||
if image.size(0) == 0:
|
||||
return ("",)
|
||||
|
||||
images = tensor2np(image)
|
||||
images = [frame.astype(np.uint8) for frame in images]
|
||||
if pingpong:
|
||||
reversed_frames = images[::-1]
|
||||
images.extend(reversed_frames)
|
||||
if resample_filter is not None:
|
||||
resample_filter = PIL_FILTER_MAP.get(resample_filter)
|
||||
|
||||
height, width, _ = image[0].shape
|
||||
pil_images = prepare_animated_batch(
|
||||
image,
|
||||
pingpong,
|
||||
resize_by,
|
||||
resample_filter,
|
||||
)
|
||||
|
||||
ruuid = uuid.uuid4()
|
||||
|
||||
ruuid = ruuid.hex[:10]
|
||||
|
||||
out_path = f"{folder_paths.output_directory}/{ruuid}.gif"
|
||||
|
||||
log.debug(f"Saving a gif file {width}x{height} as {ruuid}.gif")
|
||||
|
||||
# Prepare the FFmpeg command
|
||||
command = [
|
||||
"ffmpeg",
|
||||
"-y",
|
||||
"-f",
|
||||
"rawvideo",
|
||||
"-vcodec",
|
||||
"rawvideo",
|
||||
"-s",
|
||||
f"{width}x{height}",
|
||||
"-pix_fmt",
|
||||
"rgb24", # GIF only supports rgb24
|
||||
"-r",
|
||||
str(fps),
|
||||
"-i",
|
||||
"-",
|
||||
"-vf",
|
||||
f"fps={fps},scale={width * resize_by}:-1", # Set frame rate and resize if necessary
|
||||
"-y",
|
||||
# Create the GIF from PIL images
|
||||
pil_images[0].save(
|
||||
out_path,
|
||||
]
|
||||
save_all=True,
|
||||
append_images=pil_images[1:],
|
||||
optimize=optimize,
|
||||
duration=int(1000 / fps),
|
||||
loop=0,
|
||||
)
|
||||
|
||||
process = subprocess.Popen(command, stdin=subprocess.PIPE)
|
||||
|
||||
for frame in images:
|
||||
model_management.throw_exception_if_processing_interrupted()
|
||||
process.stdin.write(frame.tobytes())
|
||||
|
||||
process.stdin.close()
|
||||
process.wait()
|
||||
results = []
|
||||
results.append({"filename": f"{ruuid}.gif", "subfolder": "", "type": "output"})
|
||||
results = [{"filename": f"{ruuid}.gif", "subfolder": "", "type": "output"}]
|
||||
return {"ui": {"gif": results}}
|
||||
|
||||
|
||||
__nodes__ = [SaveGif, ExportToProres]
|
||||
__nodes__ = [SaveGif, ExportWithFfmpeg]
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import torch
|
||||
|
||||
|
||||
class LatentLerp:
|
||||
"""Linear interpolation (blend) between two latent vectors"""
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
@@ -28,6 +27,7 @@ class LatentLerp:
|
||||
|
||||
return (a,)
|
||||
|
||||
|
||||
__nodes__ = [
|
||||
LatentLerp,
|
||||
]
|
||||
]
|
||||
|
||||
+7
-10
@@ -7,17 +7,14 @@ import comfy.utils
|
||||
class ImageRemoveBackgroundRembg:
|
||||
"""Removes the background from the input using Rembg."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"image": ("IMAGE",),
|
||||
"alpha_matting": (
|
||||
["True", "False"],
|
||||
{"default": "False"},
|
||||
"BOOLEAN",
|
||||
{"default": False},
|
||||
),
|
||||
"alpha_matting_foreground_threshold": (
|
||||
"INT",
|
||||
@@ -32,12 +29,12 @@ class ImageRemoveBackgroundRembg:
|
||||
{"default": 10, "min": 0, "max": 255},
|
||||
),
|
||||
"post_process_mask": (
|
||||
["True", "False"],
|
||||
{"default": "False"},
|
||||
"BOOLEAN",
|
||||
{"default": False},
|
||||
),
|
||||
"bgcolor": (
|
||||
"COLOR",
|
||||
{"default": "black"},
|
||||
{"default": "#000000"},
|
||||
),
|
||||
},
|
||||
}
|
||||
@@ -76,13 +73,13 @@ class ImageRemoveBackgroundRembg:
|
||||
for img in images:
|
||||
img_rm = remove(
|
||||
data=img,
|
||||
alpha_matting=alpha_matting == "True",
|
||||
alpha_matting=alpha_matting,
|
||||
alpha_matting_foreground_threshold=alpha_matting_foreground_threshold,
|
||||
alpha_matting_background_threshold=alpha_matting_background_threshold,
|
||||
alpha_matting_erode_size=alpha_matting_erode_size,
|
||||
session=None,
|
||||
only_mask=False,
|
||||
post_process_mask=post_process_mask == "True",
|
||||
post_process_mask=post_process_mask,
|
||||
bgcolor=None,
|
||||
)
|
||||
|
||||
|
||||
+3
-11
@@ -14,7 +14,7 @@ class IntToBool:
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("BOOL",)
|
||||
RETURN_TYPES = ("BOOLEAN",)
|
||||
FUNCTION = "int_to_bool"
|
||||
CATEGORY = "mtb/number"
|
||||
|
||||
@@ -25,9 +25,6 @@ class IntToBool:
|
||||
class IntToNumber:
|
||||
"""Node addon for the WAS Suite. Converts a "comfy" INT to a NUMBER."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -56,9 +53,6 @@ class IntToNumber:
|
||||
class FloatToNumber:
|
||||
"""Node addon for the WAS Suite. Converts a "comfy" FLOAT to a NUMBER."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
@@ -83,13 +77,11 @@ class FloatToNumber:
|
||||
def float_to_number(self, float):
|
||||
return (float,)
|
||||
|
||||
|
||||
|
||||
return (int,)
|
||||
|
||||
|
||||
__nodes__ = [
|
||||
FloatToNumber,
|
||||
IntToBool,
|
||||
IntToNumber,
|
||||
|
||||
]
|
||||
]
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
from pytoshop.user import nested_layers
|
||||
|
||||
# from pytoshop.image_data import ImageData
|
||||
from .. import utils
|
||||
from ..log import log
|
||||
from uuid import uuid4
|
||||
from pathlib import Path
|
||||
import folder_paths
|
||||
from importlib import reload
|
||||
|
||||
|
||||
class PsdSave:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"input_1": ("PSDLAYER",),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ()
|
||||
FUNCTION = "psd_save"
|
||||
CATEGORY = "psd"
|
||||
OUTPUT_NODE = True
|
||||
|
||||
def psd_save(self, **kwargs):
|
||||
groups = {
|
||||
"main": [],
|
||||
}
|
||||
out_layers = []
|
||||
for input, item in kwargs.items():
|
||||
for group, layer in item.items():
|
||||
if group not in groups:
|
||||
groups[group] = []
|
||||
groups[group].append(layer)
|
||||
|
||||
for group, layers in groups.items():
|
||||
current_group = nested_layers.Group(
|
||||
group, visible=True, opacity=255, layers=layers, closed=False
|
||||
)
|
||||
out_layers.append(current_group)
|
||||
|
||||
out_layers = nested_layers.nested_layers_to_psd(out_layers, color_mode=3)
|
||||
output_name = f"{uuid4()}.psd"
|
||||
output_path = Path(folder_paths.output_directory) / output_name
|
||||
|
||||
log.info(f"Saving PSD to {output_name}")
|
||||
|
||||
with open(output_path, "wb") as f:
|
||||
out_layers.write(f)
|
||||
|
||||
return ()
|
||||
|
||||
|
||||
class PsdLayer:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"layer_name": ("STRING", {"default": "layer"}),
|
||||
"image": ("IMAGE",),
|
||||
},
|
||||
"optional": {"mask": ("MASK",)},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("PSDLAYER",)
|
||||
FUNCTION = "psd_layer"
|
||||
CATEGORY = "psd"
|
||||
|
||||
def psd_layer(self, layer_name, image, mask=None):
|
||||
reload(utils)
|
||||
group = "main"
|
||||
if "/" in layer_name:
|
||||
sepname = layer_name.split("/")
|
||||
# layer_name = sepname.pop() # todo: support nesting?
|
||||
group = sepname[0]
|
||||
layer_name = sepname[1]
|
||||
psd = utils.tensor2pytolayer(image, layer_name, mask=mask)
|
||||
# log.warning("Mask is currently ignored for PSD Layers...")
|
||||
return ({group: psd},)
|
||||
|
||||
|
||||
__nodes__ = [PsdLayer, PsdSave]
|
||||
@@ -0,0 +1,110 @@
|
||||
import torch
|
||||
import torchvision.transforms.functional as TF
|
||||
from ..utils import log, hex_to_rgb, tensor2pil, pil2tensor
|
||||
from math import sqrt, ceil
|
||||
from typing import cast
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class TransformImage:
|
||||
"""Save torch tensors (image, mask or latent) to disk, useful to debug things outside comfy
|
||||
|
||||
|
||||
it return a tensor representing the transformed images with the same shape as the input tensor
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"image": ("IMAGE",),
|
||||
"x": ("FLOAT", {"default": 0, "step": 1, "min": -4096, "max": 4096}),
|
||||
"y": ("FLOAT", {"default": 0, "step": 1, "min": -4096, "max": 4096}),
|
||||
"zoom": ("FLOAT", {"default": 1.0, "min": 0.001, "step": 0.01}),
|
||||
"angle": ("FLOAT", {"default": 0, "step": 1, "min": -360, "max": 360}),
|
||||
"shear": (
|
||||
"FLOAT",
|
||||
{"default": 0, "step": 1, "min": -4096, "max": 4096},
|
||||
),
|
||||
"border_handling": (
|
||||
["edge", "constant", "reflect", "symmetric"],
|
||||
{"default": "edge"},
|
||||
),
|
||||
"constant_color": ("COLOR", {"default": "#000000"}),
|
||||
},
|
||||
}
|
||||
|
||||
FUNCTION = "transform"
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
CATEGORY = "mtb/transform"
|
||||
|
||||
def transform(
|
||||
self,
|
||||
image: torch.Tensor,
|
||||
x: float,
|
||||
y: float,
|
||||
zoom: float,
|
||||
angle: float,
|
||||
shear: float,
|
||||
border_handling="edge",
|
||||
constant_color=None,
|
||||
):
|
||||
x = int(x)
|
||||
y = int(y)
|
||||
angle = int(angle)
|
||||
|
||||
log.debug(f"Zoom: {zoom} | x: {x}, y: {y}, angle: {angle}, shear: {shear}")
|
||||
|
||||
if image.size(0) == 0:
|
||||
return (torch.zeros(0),)
|
||||
transformed_images = []
|
||||
frames_count, frame_height, frame_width, frame_channel_count = image.size()
|
||||
|
||||
new_height, new_width = int(frame_height * zoom), int(frame_width * zoom)
|
||||
|
||||
log.debug(f"New height: {new_height}, New width: {new_width}")
|
||||
|
||||
# - Calculate diagonal of the original image
|
||||
diagonal = sqrt(frame_width**2 + frame_height**2)
|
||||
max_padding = ceil(diagonal * zoom - min(frame_width, frame_height))
|
||||
# Calculate padding for zoom
|
||||
pw = int(frame_width - new_width)
|
||||
ph = int(frame_height - new_height)
|
||||
|
||||
pw += abs(max_padding)
|
||||
ph += abs(max_padding)
|
||||
|
||||
padding = [max(0, pw + x), max(0, ph + y), max(0, pw - x), max(0, ph - y)]
|
||||
|
||||
constant_color = hex_to_rgb(constant_color)
|
||||
log.debug(f"Fill Tuple: {constant_color}")
|
||||
|
||||
for img in tensor2pil(image):
|
||||
img = TF.pad(
|
||||
img, # transformed_frame,
|
||||
padding=padding,
|
||||
padding_mode=border_handling,
|
||||
fill=constant_color or 0,
|
||||
)
|
||||
|
||||
img = cast(
|
||||
Image.Image,
|
||||
TF.affine(img, angle=angle, scale=zoom, translate=[x, y], shear=shear),
|
||||
)
|
||||
|
||||
left = abs(padding[0])
|
||||
upper = abs(padding[1])
|
||||
right = img.width - abs(padding[2])
|
||||
bottom = img.height - abs(padding[3])
|
||||
|
||||
# log.debug("crop is [:,top:bottom, left:right] for tensors")
|
||||
log.debug("crop is [left, top, right, bottom] for PIL")
|
||||
log.debug(f"crop is {left}, {upper}, {right}, {bottom}")
|
||||
img = img.crop((left, upper, right, bottom))
|
||||
|
||||
transformed_images.append(img)
|
||||
|
||||
return (pil2tensor(transformed_images),)
|
||||
|
||||
|
||||
__nodes__ = [TransformImage]
|
||||
+1
-1
@@ -13,6 +13,6 @@
|
||||
"reportMissingImports": true,
|
||||
"reportMissingTypeStubs": false,
|
||||
"pythonVersion": "3.10",
|
||||
"pythonPlatform": "Windows",
|
||||
"pythonPlatform": "All",
|
||||
"reportOptionalMemberAccess": "none"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
onnxruntime-gpu==1.15.1
|
||||
qrcode[pil]
|
||||
rembg==2.0.50
|
||||
tensorflow
|
||||
facexlib==0.3.0
|
||||
insightface==0.7.3
|
||||
basicsr==1.4.2
|
||||
pytoshop
|
||||
@@ -0,0 +1,19 @@
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/pycocotools-2.0.6-cp310-cp310-win_amd64.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/future-0.18.3-py3-none-any.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/filterpy-1.4.5-py3-none-any.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/easydict-1.10-py3-none-any.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/gdown-4.7.1-py3-none-any.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/basicsr-1.4.2-py3-none-any.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/mmcv-2.0.0-py2.py3-none-any.whl
|
||||
https://github.com/melMass/comfy_mtb/releases/download/v0.1.3/insightface-0.7.3-cp310-cp310-win_amd64.whl
|
||||
|
||||
onnxruntime-gpu==1.15.1
|
||||
qrcode[pil]
|
||||
rembg==2.0.50
|
||||
# on windows non WSL 2.10 is the last version with GPU support
|
||||
tensorflow==2.10.1;
|
||||
tb-nightly==2.12.0a20230126; platform_system == "Windows"
|
||||
facexlib==0.3.0
|
||||
# the old tf version on windows comes with a breaking protobuf version
|
||||
protobuf==3.19.6
|
||||
pytoshop
|
||||
@@ -1,3 +0,0 @@
|
||||
insightface==0.7.3
|
||||
mmcv==2.0.0
|
||||
basicsr==1.4.2
|
||||
@@ -1,14 +0,0 @@
|
||||
onnxruntime-gpu==1.15.1
|
||||
imageio===2.28.1
|
||||
qrcode[pil]
|
||||
numpy==1.23.5
|
||||
rembg==2.0.37
|
||||
# on windows non WSL 2.10 is the last version with GPU support
|
||||
tensorflow<2.11.0; platform_system == "Windows"
|
||||
tb-nightly==2.12.0a20230126; platform_system == "Windows"
|
||||
tensorflow; platform_system != "Windows"
|
||||
# the old tf version on windows comes with a breaking protobuf version
|
||||
protobuf==3.19.6
|
||||
gdown @ git+https://github.com/melMass/gdown@main
|
||||
mmdet==3.0.0
|
||||
facexlib==0.3.0
|
||||
@@ -2,6 +2,8 @@ import os
|
||||
import requests
|
||||
from rich.console import Console
|
||||
from tqdm import tqdm
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
try:
|
||||
import folder_paths
|
||||
@@ -30,13 +32,13 @@ models_to_download = {
|
||||
"size": 332,
|
||||
"download_url": [
|
||||
"https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth",
|
||||
"https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
||||
# TODO: provide a way to selectively download models from "packs"
|
||||
# https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/GFPGANv1.pth
|
||||
# https://github.com/TencentARC/GFPGAN/releases/download/v0.2.0/GFPGANCleanv1-NoCE-C2.pth
|
||||
# https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth
|
||||
# https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth
|
||||
],
|
||||
"destination": "upscale_models",
|
||||
"destination": "face_restore",
|
||||
},
|
||||
"FILM: Frame Interpolation for Large Motion": {
|
||||
"size": 402,
|
||||
@@ -51,7 +53,6 @@ console = Console()
|
||||
|
||||
from urllib.parse import urlparse
|
||||
from pathlib import Path
|
||||
import gdown
|
||||
|
||||
|
||||
def download_model(download_url, destination):
|
||||
@@ -63,6 +64,21 @@ def download_model(download_url, destination):
|
||||
filename = os.path.basename(urlparse(download_url).path)
|
||||
response = None
|
||||
if "drive.google.com" in download_url:
|
||||
try:
|
||||
import gdown
|
||||
except ImportError:
|
||||
print("Installing gdown")
|
||||
subprocess.check_call(
|
||||
[
|
||||
sys.executable,
|
||||
"-m",
|
||||
"pip",
|
||||
"install",
|
||||
"git+https://github.com/melMass/gdown@main",
|
||||
]
|
||||
)
|
||||
import gdown
|
||||
|
||||
if "/folders/" in download_url:
|
||||
# download folder
|
||||
try:
|
||||
|
||||
@@ -4,8 +4,43 @@ import torch
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
from typing import Union, List
|
||||
from typing import List, Optional
|
||||
from pytoshop.user import nested_layers
|
||||
from pytoshop import enums
|
||||
|
||||
# from pytoshop.layers import LayerMask, LayerRecord
|
||||
from .log import log
|
||||
from typing import List
|
||||
import signal
|
||||
from contextlib import suppress
|
||||
from queue import Queue, Empty
|
||||
import subprocess
|
||||
import threading
|
||||
import os
|
||||
import math
|
||||
|
||||
try:
|
||||
from .log import log
|
||||
except ImportError:
|
||||
try:
|
||||
from log import log
|
||||
|
||||
log.warn("Imported log without relative path")
|
||||
except ImportError:
|
||||
import logging
|
||||
|
||||
log = logging.getLogger("comfy mtb utils")
|
||||
log.warn("[comfy mtb] You probably called the file outside a module.")
|
||||
|
||||
|
||||
# region MISC Utilities
|
||||
def hex_to_rgb(hex_color):
|
||||
try:
|
||||
hex_color = hex_color.lstrip("#")
|
||||
return tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
|
||||
except ValueError:
|
||||
log.error(f"Invalid hex color: {hex_color}")
|
||||
return (0, 0, 0)
|
||||
|
||||
|
||||
def add_path(path, prepend=False):
|
||||
@@ -24,33 +59,150 @@ def add_path(path, prepend=False):
|
||||
sys.path.append(path)
|
||||
|
||||
|
||||
# Get the absolute path of the parent directory of the current script
|
||||
def enqueue_output(out, queue):
|
||||
for line in iter(out.readline, b""):
|
||||
queue.put(line)
|
||||
out.close()
|
||||
|
||||
|
||||
def run_command(cmd):
|
||||
if isinstance(cmd, str):
|
||||
shell_cmd = cmd
|
||||
elif isinstance(cmd, list):
|
||||
shell_cmd = ""
|
||||
for arg in cmd:
|
||||
if isinstance(arg, Path):
|
||||
arg = arg.as_posix()
|
||||
shell_cmd += f"{arg} "
|
||||
else:
|
||||
raise ValueError(
|
||||
"Invalid 'cmd' argument. It must be a string or a list of arguments."
|
||||
)
|
||||
|
||||
process = subprocess.Popen(
|
||||
shell_cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
universal_newlines=True,
|
||||
shell=True,
|
||||
)
|
||||
|
||||
# Create separate threads to read standard output and standard error streams
|
||||
stdout_queue = Queue()
|
||||
stderr_queue = Queue()
|
||||
stdout_thread = threading.Thread(
|
||||
target=enqueue_output, args=(process.stdout, stdout_queue)
|
||||
)
|
||||
stderr_thread = threading.Thread(
|
||||
target=enqueue_output, args=(process.stderr, stderr_queue)
|
||||
)
|
||||
stdout_thread.daemon = True
|
||||
stderr_thread.daemon = True
|
||||
stdout_thread.start()
|
||||
stderr_thread.start()
|
||||
|
||||
interrupted = False
|
||||
|
||||
def signal_handler(signum, frame):
|
||||
nonlocal interrupted
|
||||
interrupted = True
|
||||
print("Command execution interrupted.")
|
||||
|
||||
# Register the signal handler for keyboard interrupts (SIGINT)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
# Process output from both streams until the process completes or interrupted
|
||||
while not interrupted and (
|
||||
process.poll() is None or not stdout_queue.empty() or not stderr_queue.empty()
|
||||
):
|
||||
with suppress(Empty):
|
||||
stdout_line = stdout_queue.get_nowait()
|
||||
if stdout_line.strip() != "":
|
||||
print(stdout_line.strip())
|
||||
with suppress(Empty):
|
||||
stderr_line = stderr_queue.get_nowait()
|
||||
if stderr_line.strip() != "":
|
||||
print(stderr_line.strip())
|
||||
return_code = process.returncode
|
||||
|
||||
if return_code == 0 and not interrupted:
|
||||
print("Command executed successfully!")
|
||||
else:
|
||||
if not interrupted:
|
||||
print(f"Command failed with return code: {return_code}")
|
||||
|
||||
|
||||
# todo use the requirements library
|
||||
reqs_map = {
|
||||
"onnxruntime": "onnxruntime-gpu==1.15.1",
|
||||
"basicsr": "basicsr==1.4.2",
|
||||
"rembg": "rembg==2.0.50",
|
||||
"qrcode": "qrcode[pil]",
|
||||
}
|
||||
|
||||
|
||||
def import_install(package_name):
|
||||
from pip._internal import main as pip_main
|
||||
|
||||
try:
|
||||
__import__(package_name)
|
||||
except ImportError:
|
||||
package_spec = reqs_map.get(package_name)
|
||||
if package_spec is None:
|
||||
print(f"Installing {package_name}")
|
||||
package_spec = package_name
|
||||
|
||||
pip_main(["install", package_spec])
|
||||
__import__(package_name)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region GLOBAL VARIABLES
|
||||
# - detect mode
|
||||
comfy_mode = None
|
||||
if os.environ.get("COLAB_GPU"):
|
||||
comfy_mode = "colab"
|
||||
elif "python_embeded" in sys.executable:
|
||||
comfy_mode = "embeded"
|
||||
elif ".venv" in sys.executable:
|
||||
comfy_mode = "venv"
|
||||
|
||||
# - Get the absolute path of the parent directory of the current script
|
||||
here = Path(__file__).parent.resolve()
|
||||
|
||||
# Construct the absolute path to the ComfyUI directory
|
||||
# - Construct the absolute path to the ComfyUI directory
|
||||
comfy_dir = here.parent.parent
|
||||
|
||||
# Construct the path to the font file
|
||||
# - Construct the path to the font file
|
||||
font_path = here / "font.ttf"
|
||||
|
||||
# Add extern folder to path
|
||||
# - Add extern folder to path
|
||||
extern_root = here / "extern"
|
||||
add_path(extern_root)
|
||||
for pth in extern_root.iterdir():
|
||||
if pth.is_dir():
|
||||
add_path(pth)
|
||||
|
||||
|
||||
# Add the ComfyUI directory and custom nodes path to the sys.path list
|
||||
# - Add the ComfyUI directory and custom nodes path to the sys.path list
|
||||
add_path(comfy_dir)
|
||||
add_path((comfy_dir / "custom_nodes"))
|
||||
|
||||
PIL_FILTER_MAP = {
|
||||
"nearest": Image.Resampling.NEAREST,
|
||||
"box": Image.Resampling.BOX,
|
||||
"bilinear": Image.Resampling.BILINEAR,
|
||||
"hamming": Image.Resampling.HAMMING,
|
||||
"bicubic": Image.Resampling.BICUBIC,
|
||||
"lanczos": Image.Resampling.LANCZOS,
|
||||
}
|
||||
# endregion
|
||||
|
||||
|
||||
# region TENSOR UTILITIES
|
||||
def tensor2pil(image: torch.Tensor) -> List[Image.Image]:
|
||||
batch_count = 1
|
||||
if len(image.shape) > 3:
|
||||
batch_count = image.size(0)
|
||||
|
||||
batch_count = image.size(0) if len(image.shape) > 3 else 1
|
||||
if batch_count > 1:
|
||||
out = []
|
||||
for i in range(batch_count):
|
||||
@@ -79,9 +231,7 @@ def np2tensor(img_np: np.ndarray | List[np.ndarray]) -> torch.Tensor:
|
||||
|
||||
|
||||
def tensor2np(tensor: torch.Tensor) -> List[np.ndarray]:
|
||||
batch_count = 1
|
||||
if len(tensor.shape) > 3:
|
||||
batch_count = tensor.size(0)
|
||||
batch_count = tensor.size(0) if len(tensor.shape) > 3 else 1
|
||||
if batch_count > 1:
|
||||
out = []
|
||||
for i in range(batch_count):
|
||||
@@ -89,3 +239,288 @@ def tensor2np(tensor: torch.Tensor) -> List[np.ndarray]:
|
||||
return out
|
||||
|
||||
return [np.clip(255.0 * tensor.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)]
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region MODEL Utilities
|
||||
def download_antelopev2():
|
||||
antelopev2_url = "https://drive.google.com/uc?id=18wEUfMNohBJ4K3Ly5wpTejPfDzp-8fI8"
|
||||
|
||||
try:
|
||||
import gdown
|
||||
|
||||
import folder_paths
|
||||
|
||||
log.debug("Loading antelopev2 model")
|
||||
|
||||
dest = Path(folder_paths.models_dir) / "insightface"
|
||||
archive = dest / "antelopev2.zip"
|
||||
final_path = dest / "models" / "antelopev2"
|
||||
if not final_path.exists():
|
||||
log.info(f"antelopev2 not found, downloading to {dest}")
|
||||
gdown.download(
|
||||
antelopev2_url,
|
||||
archive.as_posix(),
|
||||
resume=True,
|
||||
)
|
||||
|
||||
log.info(f"Unzipping antelopev2 to {final_path}")
|
||||
|
||||
if archive.exists():
|
||||
# we unzip it
|
||||
import zipfile
|
||||
|
||||
with zipfile.ZipFile(archive.as_posix(), "r") as zip_ref:
|
||||
zip_ref.extractall(final_path.parent.as_posix())
|
||||
|
||||
except Exception as e:
|
||||
log.error(
|
||||
f"Could not load or download antelopev2 model, download it manually from {antelopev2_url}"
|
||||
)
|
||||
raise e
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region UV Utilities
|
||||
|
||||
|
||||
def create_uv_map_tensor(width=512, height=512):
|
||||
u = torch.linspace(0.0, 1.0, steps=width)
|
||||
v = torch.linspace(0.0, 1.0, steps=height)
|
||||
|
||||
U, V = torch.meshgrid(u, v)
|
||||
|
||||
uv_map = torch.zeros(height, width, 3, dtype=torch.float32)
|
||||
uv_map[:, :, 0] = U.t()
|
||||
uv_map[:, :, 1] = V.t()
|
||||
|
||||
return uv_map.unsqueeze(0)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region ANIMATION Utilities
|
||||
def apply_easing(value, easing_type):
|
||||
if value < 0 or value > 1:
|
||||
raise ValueError("The value should be between 0 and 1.")
|
||||
|
||||
if easing_type == "Linear":
|
||||
return value
|
||||
|
||||
# Back easing functions
|
||||
def easeInBack(t):
|
||||
s = 1.70158
|
||||
return t * t * ((s + 1) * t - s)
|
||||
|
||||
def easeOutBack(t):
|
||||
s = 1.70158
|
||||
return ((t - 1) * t * ((s + 1) * t + s)) + 1
|
||||
|
||||
def easeInOutBack(t):
|
||||
s = 1.70158 * 1.525
|
||||
if t < 0.5:
|
||||
return (t * t * (t * (s + 1) - s)) * 2
|
||||
return ((t - 2) * t * ((s + 1) * t + s) + 2) * 2
|
||||
|
||||
# Elastic easing functions
|
||||
def easeInElastic(t):
|
||||
if t == 0:
|
||||
return 0
|
||||
if t == 1:
|
||||
return 1
|
||||
p = 0.3
|
||||
s = p / 4
|
||||
return -(math.pow(2, 10 * (t - 1)) * math.sin((t - 1 - s) * (2 * math.pi) / p))
|
||||
|
||||
def easeOutElastic(t):
|
||||
if t == 0:
|
||||
return 0
|
||||
if t == 1:
|
||||
return 1
|
||||
p = 0.3
|
||||
s = p / 4
|
||||
return math.pow(2, -10 * t) * math.sin((t - s) * (2 * math.pi) / p) + 1
|
||||
|
||||
def easeInOutElastic(t):
|
||||
if t == 0:
|
||||
return 0
|
||||
if t == 1:
|
||||
return 1
|
||||
p = 0.3 * 1.5
|
||||
s = p / 4
|
||||
t = t * 2
|
||||
if t < 1:
|
||||
return -0.5 * (
|
||||
math.pow(2, 10 * (t - 1)) * math.sin((t - 1 - s) * (2 * math.pi) / p)
|
||||
)
|
||||
return (
|
||||
0.5 * math.pow(2, -10 * (t - 1)) * math.sin((t - 1 - s) * (2 * math.pi) / p)
|
||||
+ 1
|
||||
)
|
||||
|
||||
# Bounce easing functions
|
||||
def easeInBounce(t):
|
||||
return 1 - easeOutBounce(1 - t)
|
||||
|
||||
def easeOutBounce(t):
|
||||
if t < (1 / 2.75):
|
||||
return 7.5625 * t * t
|
||||
elif t < (2 / 2.75):
|
||||
t -= 1.5 / 2.75
|
||||
return 7.5625 * t * t + 0.75
|
||||
elif t < (2.5 / 2.75):
|
||||
t -= 2.25 / 2.75
|
||||
return 7.5625 * t * t + 0.9375
|
||||
else:
|
||||
t -= 2.625 / 2.75
|
||||
return 7.5625 * t * t + 0.984375
|
||||
|
||||
def easeInOutBounce(t):
|
||||
if t < 0.5:
|
||||
return easeInBounce(t * 2) * 0.5
|
||||
return easeOutBounce(t * 2 - 1) * 0.5 + 0.5
|
||||
|
||||
# Quart easing functions
|
||||
def easeInQuart(t):
|
||||
return t * t * t * t
|
||||
|
||||
def easeOutQuart(t):
|
||||
t -= 1
|
||||
return -(t**2 * t * t - 1)
|
||||
|
||||
def easeInOutQuart(t):
|
||||
t *= 2
|
||||
if t < 1:
|
||||
return 0.5 * t * t * t * t
|
||||
t -= 2
|
||||
return -0.5 * (t**2 * t * t - 2)
|
||||
|
||||
# Cubic easing functions
|
||||
def easeInCubic(t):
|
||||
return t * t * t
|
||||
|
||||
def easeOutCubic(t):
|
||||
t -= 1
|
||||
return t**2 * t + 1
|
||||
|
||||
def easeInOutCubic(t):
|
||||
t *= 2
|
||||
if t < 1:
|
||||
return 0.5 * t * t * t
|
||||
t -= 2
|
||||
return 0.5 * (t**2 * t + 2)
|
||||
|
||||
# Circ easing functions
|
||||
def easeInCirc(t):
|
||||
return -(math.sqrt(1 - t * t) - 1)
|
||||
|
||||
def easeOutCirc(t):
|
||||
t -= 1
|
||||
return math.sqrt(1 - t**2)
|
||||
|
||||
def easeInOutCirc(t):
|
||||
t *= 2
|
||||
if t < 1:
|
||||
return -0.5 * (math.sqrt(1 - t**2) - 1)
|
||||
t -= 2
|
||||
return 0.5 * (math.sqrt(1 - t**2) + 1)
|
||||
|
||||
# Sine easing functions
|
||||
def easeInSine(t):
|
||||
return -math.cos(t * (math.pi / 2)) + 1
|
||||
|
||||
def easeOutSine(t):
|
||||
return math.sin(t * (math.pi / 2))
|
||||
|
||||
def easeInOutSine(t):
|
||||
return -0.5 * (math.cos(math.pi * t) - 1)
|
||||
|
||||
easing_functions = {
|
||||
"Sine In": easeInSine,
|
||||
"Sine Out": easeOutSine,
|
||||
"Sine In/Out": easeInOutSine,
|
||||
"Quart In": easeInQuart,
|
||||
"Quart Out": easeOutQuart,
|
||||
"Quart In/Out": easeInOutQuart,
|
||||
"Cubic In": easeInCubic,
|
||||
"Cubic Out": easeOutCubic,
|
||||
"Cubic In/Out": easeInOutCubic,
|
||||
"Circ In": easeInCirc,
|
||||
"Circ Out": easeOutCirc,
|
||||
"Circ In/Out": easeInOutCirc,
|
||||
"Back In": easeInBack,
|
||||
"Back Out": easeOutBack,
|
||||
"Back In/Out": easeInOutBack,
|
||||
"Elastic In": easeInElastic,
|
||||
"Elastic Out": easeOutElastic,
|
||||
"Elastic In/Out": easeInOutElastic,
|
||||
"Bounce In": easeInBounce,
|
||||
"Bounce Out": easeOutBounce,
|
||||
"Bounce In/Out": easeInOutBounce,
|
||||
}
|
||||
|
||||
function_ease = easing_functions.get(easing_type)
|
||||
if function_ease:
|
||||
return function_ease(value)
|
||||
|
||||
log.error(f"Unknown easing type: {easing_type}")
|
||||
log.error(f"Available easing types: {list(easing_functions.keys())}")
|
||||
raise ValueError(f"Unknown easing type: {easing_type}")
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
def tensor2pytolayer(
|
||||
tensor: torch.Tensor,
|
||||
name: str,
|
||||
visible: bool = True,
|
||||
opacity: int = 255,
|
||||
group_id: int = 0,
|
||||
blend_mode=enums.BlendMode.normal,
|
||||
x: int = 0,
|
||||
y: int = 0,
|
||||
# channels: int = 3,
|
||||
metadata: dict = {},
|
||||
layer_color=0,
|
||||
color_mode=None,
|
||||
mask: Optional[
|
||||
torch.Tensor
|
||||
] = None, # Add the mask parameter with default value as None
|
||||
) -> nested_layers.Image:
|
||||
batch_count = tensor.size(0) if len(tensor.shape) > 3 else 1
|
||||
if batch_count > 1:
|
||||
raise ValueError(
|
||||
f"Only one image is supported (batch size is currently {batch_count})"
|
||||
)
|
||||
out_channels = tensor2pil(tensor)[0]
|
||||
arr = np.array(out_channels)
|
||||
|
||||
# If a mask is provided, convert it to numpy array
|
||||
if mask is not None:
|
||||
mask_arr = np.array(tensor2pil(mask)[0])
|
||||
else:
|
||||
mask_arr = np.full_like(arr, 255, dtype=np.uint8)
|
||||
|
||||
channels = [arr[:, :, 0], arr[:, :, 1], arr[:, :, 2], mask_arr[:, :, 0]]
|
||||
|
||||
image = nested_layers.Image(
|
||||
name=name,
|
||||
visible=visible,
|
||||
opacity=opacity,
|
||||
group_id=group_id,
|
||||
blend_mode=blend_mode,
|
||||
top=y,
|
||||
left=x,
|
||||
channels=channels,
|
||||
metadata=metadata,
|
||||
layer_color=layer_color,
|
||||
color_mode=color_mode,
|
||||
)
|
||||
|
||||
return image
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
## Core
|
||||
These 3 scripts cannot be used independently and must all be present to work, they are mostly enhancing the frontend of python nodes
|
||||
- `comfy_shared`: library of methods used in `mtb_widgets` and `debug`
|
||||
|
||||
**mtb_widgets** define ui callbacks, and various widgets like the `COLOR` type:
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/5dbcb714-e1e2-4be7-b0e2-68a6c38c83de" width=400/>
|
||||
|
||||
or the `BOOL` type:
|
||||
<img src="https://github.com/melMass/comfy_mtb/assets/7041726/7601366d-601c-4f4d-b735-1a4b076770b0" width=400/>
|
||||
|
||||
There is also `Debug` which is a node that should be able to display any data input, it handle a few cases and fallback to the string representation of the
|
||||
data otherwise:
|
||||

|
||||
|
||||
|
||||
|
||||
## Standalone
|
||||
These scripts can be taken and placed independently of `comfy_mtb` or any other files, mimicking what pythongosss did for their
|
||||
|
||||
- **imageFeed**: a fork of @pythongosssss ' s [image feed](https://github.com/pythongosssss/ComfyUI-Custom-Scripts/tree/main/js), it adds support for: a lightbox to see images bigger, a way to load the current session history (in case of a web page reload), and different icons, most of the work come from the original script.
|
||||
|
||||
|
||||
> **NOTE**
|
||||
>
|
||||
> The original imagefeed got updated since and offer more options, ideally I would clean my lightbox thing and PR it to pythongoss later but in the meantime the script will detect if you already use the original one and not load this fork
|
||||
|
||||
|
||||
- 
|
||||
|
||||
|
||||
- **notify**: a basic toast notification system that I use in some places accross mtb, it can be used by simply calling `window.MTB.notify("Hello world!")`
|
||||

|
||||
+241
-205
@@ -1,282 +1,318 @@
|
||||
import { app } from "/scripts/app.js";
|
||||
/**
|
||||
* File: comfy_shared.js
|
||||
* Project: comfy_mtb
|
||||
* Author: Mel Massadian
|
||||
*
|
||||
* Copyright (c) 2023 Mel Massadian
|
||||
*
|
||||
*/
|
||||
|
||||
import { app } from '/scripts/app.js'
|
||||
|
||||
export const log = (...args) => {
|
||||
if (window.MTB_DEBUG) {
|
||||
console.debug(...args);
|
||||
}
|
||||
|
||||
if (window.MTB?.DEBUG) {
|
||||
console.debug(...args)
|
||||
}
|
||||
}
|
||||
|
||||
//- WIDGET UTILS
|
||||
export const CONVERTED_TYPE = "converted-widget";
|
||||
export const CONVERTED_TYPE = 'converted-widget'
|
||||
|
||||
export function offsetDOMWidget(widget, ctx, node, widgetWidth, widgetY, height) {
|
||||
const margin = 10;
|
||||
const elRect = ctx.canvas.getBoundingClientRect();
|
||||
const transform = new DOMMatrix()
|
||||
.scaleSelf(elRect.width / ctx.canvas.width, elRect.height / ctx.canvas.height)
|
||||
.multiplySelf(ctx.getTransform())
|
||||
.translateSelf(margin, margin + widgetY);
|
||||
export function offsetDOMWidget(
|
||||
widget,
|
||||
ctx,
|
||||
node,
|
||||
widgetWidth,
|
||||
widgetY,
|
||||
height
|
||||
) {
|
||||
const margin = 10
|
||||
const elRect = ctx.canvas.getBoundingClientRect()
|
||||
const transform = new DOMMatrix()
|
||||
.scaleSelf(
|
||||
elRect.width / ctx.canvas.width,
|
||||
elRect.height / ctx.canvas.height
|
||||
)
|
||||
.multiplySelf(ctx.getTransform())
|
||||
.translateSelf(margin, margin + widgetY)
|
||||
|
||||
const scale = new DOMMatrix().scaleSelf(transform.a, transform.d)
|
||||
Object.assign(widget.inputEl.style, {
|
||||
transformOrigin: "0 0",
|
||||
transform: scale,
|
||||
left: `${transform.a + transform.e}px`,
|
||||
top: `${transform.d + transform.f}px`,
|
||||
width: `${widgetWidth - (margin * 2)}px`,
|
||||
// height: `${(widget.parent?.inputHeight || 32) - (margin * 2)}px`,
|
||||
height: `${(height || widget.parent?.inputHeight || 32) - (margin * 2)}px`,
|
||||
const scale = new DOMMatrix().scaleSelf(transform.a, transform.d)
|
||||
Object.assign(widget.inputEl.style, {
|
||||
transformOrigin: '0 0',
|
||||
transform: scale,
|
||||
left: `${transform.a + transform.e}px`,
|
||||
top: `${transform.d + transform.f}px`,
|
||||
width: `${widgetWidth - margin * 2}px`,
|
||||
// height: `${(widget.parent?.inputHeight || 32) - (margin * 2)}px`,
|
||||
height: `${(height || widget.parent?.inputHeight || 32) - margin * 2}px`,
|
||||
|
||||
position: "absolute",
|
||||
background: (!node.color) ? '' : node.color,
|
||||
color: (!node.color) ? '' : 'white',
|
||||
zIndex: app.graph._nodes.indexOf(node),
|
||||
})
|
||||
position: 'absolute',
|
||||
background: !node.color ? '' : node.color,
|
||||
color: !node.color ? '' : 'white',
|
||||
zIndex: 5, //app.graph._nodes.indexOf(node),
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the type and link type from a widget config object.
|
||||
* @param {*} config
|
||||
* @returns
|
||||
* @param {*} config
|
||||
* @returns
|
||||
*/
|
||||
export function getWidgetType(config) {
|
||||
// Special handling for COMBO so we restrict links based on the entries
|
||||
let type = config[0];
|
||||
let linkType = type;
|
||||
if (type instanceof Array) {
|
||||
type = "COMBO";
|
||||
linkType = linkType.join(",");
|
||||
}
|
||||
return { type, linkType };
|
||||
// Special handling for COMBO so we restrict links based on the entries
|
||||
let type = config?.[0]
|
||||
let linkType = type
|
||||
if (type instanceof Array) {
|
||||
type = 'COMBO'
|
||||
linkType = linkType.join(',')
|
||||
}
|
||||
return { type, linkType }
|
||||
}
|
||||
|
||||
export const dynamic_connection = (node, index, connected, connectionPrefix = "input_", connectionType = "PSDLAYER") => {
|
||||
|
||||
// remove all non connected inputs
|
||||
if (!connected && node.inputs.length > 1) {
|
||||
log(`Removing input ${index} (${node.inputs[index].name})`)
|
||||
if (node.widgets) {
|
||||
const w = node.widgets.find((w) => w.name === node.inputs[index].name);
|
||||
if (w) {
|
||||
w.onRemove?.();
|
||||
node.widgets.length = node.widgets.length - 1
|
||||
}
|
||||
}
|
||||
node.removeInput(index)
|
||||
|
||||
// make inputs sequential again
|
||||
for (let i = 0; i < node.inputs.length; i++) {
|
||||
node.inputs[i].label = `${connectionPrefix}${i + 1}`
|
||||
}
|
||||
export const dynamic_connection = (
|
||||
node,
|
||||
index,
|
||||
connected,
|
||||
connectionPrefix = 'input_',
|
||||
connectionType = 'PSDLAYER'
|
||||
) => {
|
||||
// remove all non connected inputs
|
||||
if (!connected && node.inputs.length > 1) {
|
||||
log(`Removing input ${index} (${node.inputs[index].name})`)
|
||||
if (node.widgets) {
|
||||
const w = node.widgets.find((w) => w.name === node.inputs[index].name)
|
||||
if (w) {
|
||||
w.onRemoved?.()
|
||||
node.widgets.length = node.widgets.length - 1
|
||||
}
|
||||
}
|
||||
node.removeInput(index)
|
||||
|
||||
// add an extra input
|
||||
if (node.inputs[node.inputs.length - 1].link != undefined) {
|
||||
log(`Adding input ${node.inputs.length + 1} (${connectionPrefix}${node.inputs.length + 1})`)
|
||||
|
||||
node.addInput(`${connectionPrefix}${node.inputs.length + 1}`, connectionType)
|
||||
// make inputs sequential again
|
||||
for (let i = 0; i < node.inputs.length; i++) {
|
||||
node.inputs[i].label = `${connectionPrefix}${i + 1}`
|
||||
}
|
||||
}
|
||||
|
||||
// add an extra input
|
||||
if (node.inputs[node.inputs.length - 1].link != undefined) {
|
||||
log(
|
||||
`Adding input ${node.inputs.length + 1} (${connectionPrefix}${
|
||||
node.inputs.length + 1
|
||||
})`
|
||||
)
|
||||
|
||||
node.addInput(
|
||||
`${connectionPrefix}${node.inputs.length + 1}`,
|
||||
connectionType
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends a callback to the extra menu options of a given node type.
|
||||
* @param {*} nodeType
|
||||
* @param {*} cb
|
||||
* @param {*} nodeType
|
||||
* @param {*} cb
|
||||
*/
|
||||
export function addMenuHandler(nodeType, cb) {
|
||||
const getOpts = nodeType.prototype.getExtraMenuOptions;
|
||||
nodeType.prototype.getExtraMenuOptions = function () {
|
||||
const r = getOpts.apply(this, arguments);
|
||||
cb.apply(this, arguments);
|
||||
return r;
|
||||
};
|
||||
const getOpts = nodeType.prototype.getExtraMenuOptions
|
||||
nodeType.prototype.getExtraMenuOptions = function () {
|
||||
const r = getOpts.apply(this, arguments)
|
||||
cb.apply(this, arguments)
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
export function hideWidget(node, widget, suffix = "") {
|
||||
widget.origType = widget.type;
|
||||
widget.hidden = true
|
||||
widget.origComputeSize = widget.computeSize;
|
||||
widget.origSerializeValue = widget.serializeValue;
|
||||
widget.computeSize = () => [0, -4]; // -4 is due to the gap litegraph adds between widgets automatically
|
||||
widget.type = CONVERTED_TYPE + suffix;
|
||||
widget.serializeValue = () => {
|
||||
// Prevent serializing the widget if we have no input linked
|
||||
const { link } = node.inputs.find((i) => i.widget?.name === widget.name);
|
||||
if (link == null) {
|
||||
return undefined;
|
||||
}
|
||||
return widget.origSerializeValue ? widget.origSerializeValue() : widget.value;
|
||||
};
|
||||
|
||||
// Hide any linked widgets, e.g. seed+seedControl
|
||||
if (widget.linkedWidgets) {
|
||||
for (const w of widget.linkedWidgets) {
|
||||
hideWidget(node, w, ":" + widget.name);
|
||||
}
|
||||
export function hideWidget(node, widget, suffix = '') {
|
||||
widget.origType = widget.type
|
||||
widget.hidden = true
|
||||
widget.origComputeSize = widget.computeSize
|
||||
widget.origSerializeValue = widget.serializeValue
|
||||
widget.computeSize = () => [0, -4] // -4 is due to the gap litegraph adds between widgets automatically
|
||||
widget.type = CONVERTED_TYPE + suffix
|
||||
widget.serializeValue = () => {
|
||||
// Prevent serializing the widget if we have no input linked
|
||||
const { link } = node.inputs.find((i) => i.widget?.name === widget.name)
|
||||
if (link == null) {
|
||||
return undefined
|
||||
}
|
||||
return widget.origSerializeValue
|
||||
? widget.origSerializeValue()
|
||||
: widget.value
|
||||
}
|
||||
|
||||
// Hide any linked widgets, e.g. seed+seedControl
|
||||
if (widget.linkedWidgets) {
|
||||
for (const w of widget.linkedWidgets) {
|
||||
hideWidget(node, w, ':' + widget.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function showWidget(widget) {
|
||||
widget.type = widget.origType;
|
||||
widget.computeSize = widget.origComputeSize;
|
||||
widget.serializeValue = widget.origSerializeValue;
|
||||
widget.type = widget.origType
|
||||
widget.computeSize = widget.origComputeSize
|
||||
widget.serializeValue = widget.origSerializeValue
|
||||
|
||||
delete widget.origType;
|
||||
delete widget.origComputeSize;
|
||||
delete widget.origSerializeValue;
|
||||
delete widget.origType
|
||||
delete widget.origComputeSize
|
||||
delete widget.origSerializeValue
|
||||
|
||||
// Hide any linked widgets, e.g. seed+seedControl
|
||||
if (widget.linkedWidgets) {
|
||||
for (const w of widget.linkedWidgets) {
|
||||
showWidget(w);
|
||||
}
|
||||
// Hide any linked widgets, e.g. seed+seedControl
|
||||
if (widget.linkedWidgets) {
|
||||
for (const w of widget.linkedWidgets) {
|
||||
showWidget(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function convertToWidget(node, widget) {
|
||||
showWidget(widget);
|
||||
const sz = node.size;
|
||||
node.removeInput(node.inputs.findIndex((i) => i.widget?.name === widget.name));
|
||||
showWidget(widget)
|
||||
const sz = node.size
|
||||
node.removeInput(node.inputs.findIndex((i) => i.widget?.name === widget.name))
|
||||
|
||||
for (const widget of node.widgets) {
|
||||
widget.last_y -= LiteGraph.NODE_SLOT_HEIGHT;
|
||||
}
|
||||
for (const widget of node.widgets) {
|
||||
widget.last_y -= LiteGraph.NODE_SLOT_HEIGHT
|
||||
}
|
||||
|
||||
// Restore original size but grow if needed
|
||||
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])]);
|
||||
// Restore original size but grow if needed
|
||||
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])])
|
||||
}
|
||||
|
||||
|
||||
export function convertToInput(node, widget, config) {
|
||||
hideWidget(node, widget);
|
||||
hideWidget(node, widget)
|
||||
|
||||
const { linkType } = getWidgetType(config);
|
||||
const { linkType } = getWidgetType(config)
|
||||
|
||||
// Add input and store widget config for creating on primitive node
|
||||
const sz = node.size;
|
||||
node.addInput(widget.name, linkType, {
|
||||
widget: { name: widget.name, config },
|
||||
});
|
||||
// Add input and store widget config for creating on primitive node
|
||||
const sz = node.size
|
||||
node.addInput(widget.name, linkType, {
|
||||
widget: { name: widget.name, config },
|
||||
})
|
||||
|
||||
for (const widget of node.widgets) {
|
||||
widget.last_y += LiteGraph.NODE_SLOT_HEIGHT;
|
||||
}
|
||||
for (const widget of node.widgets) {
|
||||
widget.last_y += LiteGraph.NODE_SLOT_HEIGHT
|
||||
}
|
||||
|
||||
// Restore original size but grow if needed
|
||||
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])]);
|
||||
// Restore original size but grow if needed
|
||||
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])])
|
||||
}
|
||||
|
||||
export function hideWidgetForGood(node, widget, suffix = "") {
|
||||
widget.origType = widget.type;
|
||||
widget.origComputeSize = widget.computeSize;
|
||||
widget.origSerializeValue = widget.serializeValue;
|
||||
widget.computeSize = () => [0, -4]; // -4 is due to the gap litegraph adds between widgets automatically
|
||||
widget.type = CONVERTED_TYPE + suffix;
|
||||
// widget.serializeValue = () => {
|
||||
// // Prevent serializing the widget if we have no input linked
|
||||
// const w = node.inputs?.find((i) => i.widget?.name === widget.name);
|
||||
// if (w?.link == null) {
|
||||
// return undefined;
|
||||
// }
|
||||
// return widget.origSerializeValue ? widget.origSerializeValue() : widget.value;
|
||||
// };
|
||||
export function hideWidgetForGood(node, widget, suffix = '') {
|
||||
widget.origType = widget.type
|
||||
widget.origComputeSize = widget.computeSize
|
||||
widget.origSerializeValue = widget.serializeValue
|
||||
widget.computeSize = () => [0, -4] // -4 is due to the gap litegraph adds between widgets automatically
|
||||
widget.type = CONVERTED_TYPE + suffix
|
||||
// widget.serializeValue = () => {
|
||||
// // Prevent serializing the widget if we have no input linked
|
||||
// const w = node.inputs?.find((i) => i.widget?.name === widget.name);
|
||||
// if (w?.link == null) {
|
||||
// return undefined;
|
||||
// }
|
||||
// return widget.origSerializeValue ? widget.origSerializeValue() : widget.value;
|
||||
// };
|
||||
|
||||
// Hide any linked widgets, e.g. seed+seedControl
|
||||
if (widget.linkedWidgets) {
|
||||
for (const w of widget.linkedWidgets) {
|
||||
hideWidgetForGood(node, w, ":" + widget.name);
|
||||
}
|
||||
// Hide any linked widgets, e.g. seed+seedControl
|
||||
if (widget.linkedWidgets) {
|
||||
for (const w of widget.linkedWidgets) {
|
||||
hideWidgetForGood(node, w, ':' + widget.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function fixWidgets(node) {
|
||||
if (node.inputs) {
|
||||
for (const input of node.inputs) {
|
||||
log(input)
|
||||
if (input.widget || node.widgets) {
|
||||
// if (newTypes.includes(input.type)) {
|
||||
const matching_widget = node.widgets.find((w) => w.name === input.name);
|
||||
if (matching_widget) {
|
||||
if (node.inputs) {
|
||||
for (const input of node.inputs) {
|
||||
log(input)
|
||||
if (input.widget || node.widgets) {
|
||||
// if (newTypes.includes(input.type)) {
|
||||
const matching_widget = node.widgets.find((w) => w.name === input.name)
|
||||
if (matching_widget) {
|
||||
// if (matching_widget.hidden) {
|
||||
// log(`Already hidden skipping ${matching_widget.name}`)
|
||||
// continue
|
||||
// }
|
||||
const w = node.widgets.find((w) => w.name === matching_widget.name)
|
||||
if (w && w.type != CONVERTED_TYPE) {
|
||||
log(w)
|
||||
log(`hidding ${w.name}(${w.type}) from ${node.type}`)
|
||||
log(node)
|
||||
hideWidget(node, w)
|
||||
} else {
|
||||
log(`converting to widget ${w}`)
|
||||
|
||||
|
||||
// if (matching_widget.hidden) {
|
||||
// log(`Already hidden skipping ${matching_widget.name}`)
|
||||
// continue
|
||||
// }
|
||||
const w = node.widgets.find((w) => w.name === matching_widget.name);
|
||||
if (w && w.type != CONVERTED_TYPE) {
|
||||
log(w)
|
||||
log(`hidding ${w.name}(${w.type}) from ${node.type}`)
|
||||
log(node)
|
||||
hideWidget(node, w);
|
||||
} else {
|
||||
log(`converting to widget ${w}`)
|
||||
|
||||
convertToWidget(node, input)
|
||||
}
|
||||
}
|
||||
}
|
||||
convertToWidget(node, input)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
export function inner_value_change(widget, value, event = undefined) {
|
||||
if (widget.type == "number" || widget.type == "BBOX") {
|
||||
value = Number(value);
|
||||
} else if (widget.type == "BOOL") {
|
||||
value = Boolean(value)
|
||||
}
|
||||
widget.value = value;
|
||||
if (widget.options && widget.options.property && node.properties[widget.options.property] !== undefined) {
|
||||
node.setProperty(widget.options.property, value);
|
||||
}
|
||||
if (widget.callback) {
|
||||
widget.callback(widget.value, app.canvas, node, pos, event);
|
||||
}
|
||||
if (widget.type == 'number' || widget.type == 'BBOX') {
|
||||
value = Number(value)
|
||||
} else if (widget.type == 'BOOL') {
|
||||
value = Boolean(value)
|
||||
}
|
||||
widget.value = value
|
||||
if (
|
||||
widget.options &&
|
||||
widget.options.property &&
|
||||
node.properties[widget.options.property] !== undefined
|
||||
) {
|
||||
node.setProperty(widget.options.property, value)
|
||||
}
|
||||
if (widget.callback) {
|
||||
widget.callback(widget.value, app.canvas, node, pos, event)
|
||||
}
|
||||
}
|
||||
|
||||
//- COLOR UTILS
|
||||
export function isColorBright(rgb, threshold = 240) {
|
||||
const brightess = getBrightness(rgb)
|
||||
return brightess > threshold
|
||||
const brightess = getBrightness(rgb)
|
||||
return brightess > threshold
|
||||
}
|
||||
|
||||
function getBrightness(rgbObj) {
|
||||
return Math.round(((parseInt(rgbObj[0]) * 299) + (parseInt(rgbObj[1]) * 587) + (parseInt(rgbObj[2]) * 114)) / 1000)
|
||||
return Math.round(
|
||||
(parseInt(rgbObj[0]) * 299 +
|
||||
parseInt(rgbObj[1]) * 587 +
|
||||
parseInt(rgbObj[2]) * 114) /
|
||||
1000
|
||||
)
|
||||
}
|
||||
|
||||
//- HTML / CSS UTILS
|
||||
export function defineClass(className, classStyles) {
|
||||
const styleSheets = document.styleSheets;
|
||||
const styleSheets = document.styleSheets
|
||||
|
||||
// Helper function to check if the class exists in a style sheet
|
||||
function classExistsInStyleSheet(styleSheet) {
|
||||
const rules = styleSheet.rules || styleSheet.cssRules;
|
||||
for (const rule of rules) {
|
||||
if (rule.selectorText === `.${className}`) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
// Helper function to check if the class exists in a style sheet
|
||||
function classExistsInStyleSheet(styleSheet) {
|
||||
const rules = styleSheet.rules || styleSheet.cssRules
|
||||
for (const rule of rules) {
|
||||
if (rule.selectorText === `.${className}`) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the class is already defined in any of the style sheets
|
||||
let classExists = false;
|
||||
for (const styleSheet of styleSheets) {
|
||||
if (classExistsInStyleSheet(styleSheet)) {
|
||||
classExists = true;
|
||||
break;
|
||||
}
|
||||
// Check if the class is already defined in any of the style sheets
|
||||
let classExists = false
|
||||
for (const styleSheet of styleSheets) {
|
||||
if (classExistsInStyleSheet(styleSheet)) {
|
||||
classExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If the class doesn't exist, add the new class definition to the first style sheet
|
||||
if (!classExists) {
|
||||
if (styleSheets[0].insertRule) {
|
||||
styleSheets[0].insertRule(`.${className} { ${classStyles} }`, 0);
|
||||
} else if (styleSheets[0].addRule) {
|
||||
styleSheets[0].addRule(`.${className}`, classStyles, 0);
|
||||
}
|
||||
// If the class doesn't exist, add the new class definition to the first style sheet
|
||||
if (!classExists) {
|
||||
if (styleSheets[0].insertRule) {
|
||||
styleSheets[0].insertRule(`.${className} { ${classStyles} }`, 0)
|
||||
} else if (styleSheets[0].addRule) {
|
||||
styleSheets[0].addRule(`.${className}`, classStyles, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+89
-70
@@ -1,80 +1,99 @@
|
||||
import { app } from "/scripts/app.js";
|
||||
/**
|
||||
* File: debug.js
|
||||
* Project: comfy_mtb
|
||||
* Author: Mel Massadian
|
||||
*
|
||||
* Copyright (c) 2023 Mel Massadian
|
||||
*
|
||||
*/
|
||||
|
||||
import { app } from '/scripts/app.js'
|
||||
import * as shared from '/extensions/mtb/comfy_shared.js'
|
||||
import { log } from '/extensions/mtb/comfy_shared.js'
|
||||
import { MtbWidgets } from '/extensions/mtb/mtb_widgets.js'
|
||||
|
||||
// TODO: respect inputs order...
|
||||
|
||||
|
||||
|
||||
app.registerExtension({
|
||||
name: "mtb.Debug",
|
||||
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
||||
if (nodeData.name === "Debug (mtb)") {
|
||||
const onConnectionsChange = nodeType.prototype.onConnectionsChange;
|
||||
nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) {
|
||||
const r = onConnectionsChange ? onConnectionsChange.apply(this, arguments) : undefined;
|
||||
// TODO: remove all widgets on disconnect once computed
|
||||
shared.dynamic_connection(this, index, connected, "anything_", "*")
|
||||
name: 'mtb.Debug',
|
||||
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
||||
if (nodeData.name === 'Debug (mtb)') {
|
||||
const onConnectionsChange = nodeType.prototype.onConnectionsChange
|
||||
nodeType.prototype.onConnectionsChange = function (
|
||||
type,
|
||||
index,
|
||||
connected,
|
||||
link_info
|
||||
) {
|
||||
const r = onConnectionsChange
|
||||
? onConnectionsChange.apply(this, arguments)
|
||||
: undefined
|
||||
// TODO: remove all widgets on disconnect once computed
|
||||
shared.dynamic_connection(this, index, connected, 'anything_', '*')
|
||||
|
||||
//- infer type
|
||||
if (link_info) {
|
||||
const fromNode = this.graph._nodes.find((otherNode) => otherNode.id == link_info.origin_id);
|
||||
const type = fromNode.outputs[link_info.origin_slot].type;
|
||||
this.inputs[index].type = type;
|
||||
// this.inputs[index].label = type.toLowerCase()
|
||||
}
|
||||
//- restore dynamic input
|
||||
if (!connected) {
|
||||
this.inputs[index].type = "*";
|
||||
this.inputs[index].label = `anything_${index + 1}`
|
||||
}
|
||||
}
|
||||
|
||||
const onExecuted = nodeType.prototype.onExecuted;
|
||||
nodeType.prototype.onExecuted = function (message) {
|
||||
log(message)
|
||||
onExecuted?.apply(this, arguments);
|
||||
log(message)
|
||||
if (this.widgets) {
|
||||
// const pos = this.widgets.findIndex((w) => w.name === "anything_1");
|
||||
// if (pos !== -1) {
|
||||
for (let i = 0; i < this.widgets.length; i++) {
|
||||
this.widgets[i].onRemove?.();
|
||||
}
|
||||
this.widgets.length = 0;
|
||||
|
||||
}
|
||||
let widgetI = 1
|
||||
if (message.text) {
|
||||
for (const txt of message.text) {
|
||||
const w = this.addCustomWidget(MtbWidgets.DEBUG_STRING(txt, widgetI))
|
||||
w.parent = this;
|
||||
widgetI++;
|
||||
}
|
||||
}
|
||||
if (message.b64_images) {
|
||||
for (const img of message.b64_images) {
|
||||
const w = this.addCustomWidget(MtbWidgets.DEBUG_IMG(img, widgetI))
|
||||
w.parent = this;
|
||||
widgetI++;
|
||||
}
|
||||
// this.onResize?.(this.size);
|
||||
// this.resize?.(this.size)
|
||||
this.setSize(this.computeSize())
|
||||
};
|
||||
|
||||
this.onRemoved = function () {
|
||||
// When removing this node we need to remove the input from the DOM
|
||||
for (let y in this.widgets) {
|
||||
if (this.widgets[y].canvas) {
|
||||
this.widgets[y].canvas.remove();
|
||||
}
|
||||
this.widgets[y].onRemove?.();
|
||||
}
|
||||
}
|
||||
}
|
||||
//- infer type
|
||||
if (link_info) {
|
||||
const fromNode = this.graph._nodes.find(
|
||||
(otherNode) => otherNode.id == link_info.origin_id
|
||||
)
|
||||
const type = fromNode.outputs[link_info.origin_slot].type
|
||||
this.inputs[index].type = type
|
||||
// this.inputs[index].label = type.toLowerCase()
|
||||
}
|
||||
//- restore dynamic input
|
||||
if (!connected) {
|
||||
this.inputs[index].type = '*'
|
||||
this.inputs[index].label = `anything_${index + 1}`
|
||||
}
|
||||
}
|
||||
|
||||
const onExecuted = nodeType.prototype.onExecuted
|
||||
nodeType.prototype.onExecuted = function (message) {
|
||||
onExecuted?.apply(this, arguments)
|
||||
|
||||
const prefix = 'anything_'
|
||||
|
||||
if (this.widgets) {
|
||||
// const pos = this.widgets.findIndex((w) => w.name === "anything_1");
|
||||
// if (pos !== -1) {
|
||||
for (let i = 0; i < this.widgets.length; i++) {
|
||||
this.widgets[i].onRemoved?.()
|
||||
}
|
||||
this.widgets.length = 0
|
||||
}
|
||||
let widgetI = 1
|
||||
if (message.text) {
|
||||
for (const txt of message.text) {
|
||||
const w = this.addCustomWidget(
|
||||
MtbWidgets.DEBUG_STRING(`${prefix}_${widgetI}`, txt)
|
||||
)
|
||||
w.parent = this
|
||||
widgetI++
|
||||
}
|
||||
}
|
||||
if (message.b64_images) {
|
||||
for (const img of message.b64_images) {
|
||||
const w = this.addCustomWidget(
|
||||
MtbWidgets.DEBUG_IMG(`${prefix}_${widgetI}`, img)
|
||||
)
|
||||
w.parent = this
|
||||
widgetI++
|
||||
}
|
||||
// this.onResize?.(this.size);
|
||||
// this.resize?.(this.size)
|
||||
this.setSize(this.computeSize())
|
||||
}
|
||||
|
||||
this.onRemoved = function () {
|
||||
// When removing this node we need to remove the input from the DOM
|
||||
for (let y in this.widgets) {
|
||||
if (this.widgets[y].canvas) {
|
||||
this.widgets[y].canvas.remove()
|
||||
}
|
||||
this.widgets[y].onRemoved?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
})
|
||||
|
||||
+289
-267
@@ -1,311 +1,333 @@
|
||||
import { api } from "/scripts/api.js";
|
||||
import { app } from "/scripts/app.js";
|
||||
/**
|
||||
* File: imageFeed.js
|
||||
* Project: comfy_mtb
|
||||
* Author: Mel Massadian
|
||||
*
|
||||
* Copyright (c) 2023 Mel Massadian
|
||||
*
|
||||
*/
|
||||
|
||||
// forked from pysssss's imageFeed.js
|
||||
|
||||
const styles = {
|
||||
lighbox: {
|
||||
position: "fixed",
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: "100vw",
|
||||
height: "100vh",
|
||||
background: "rgba(0,0,0,0.5)",
|
||||
display: "none",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
zIndex: 999,
|
||||
},
|
||||
lightboxBtn: (extra) => ({
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
background: "none",
|
||||
border: "none",
|
||||
color: "#fff",
|
||||
zIndex: 9999999,
|
||||
fontSize: "30px",
|
||||
cursor: "pointer",
|
||||
pointerEvents: "bounding-box",
|
||||
...extra,
|
||||
})
|
||||
,
|
||||
img_list: {
|
||||
import { api } from '/scripts/api.js'
|
||||
import { app } from '/scripts/app.js'
|
||||
|
||||
minHeight: "30px",
|
||||
maxHeight: "300px",
|
||||
width: "100vw",
|
||||
position: "absolute",
|
||||
bottom: 0,
|
||||
zIndex: 9999999,
|
||||
background: "#333",
|
||||
overflow: "auto",
|
||||
}
|
||||
const styles = {
|
||||
lighbox: {
|
||||
position: 'fixed',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: '100vw',
|
||||
height: '100vh',
|
||||
background: 'rgba(0,0,0,0.5)',
|
||||
display: 'none',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
zIndex: 999,
|
||||
},
|
||||
lightboxBtn: (extra) => ({
|
||||
position: 'absolute',
|
||||
top: '50%',
|
||||
background: 'none',
|
||||
border: 'none',
|
||||
color: '#fff',
|
||||
zIndex: 1000,
|
||||
fontSize: '30px',
|
||||
cursor: 'pointer',
|
||||
pointerEvents: 'auto',
|
||||
...extra,
|
||||
}),
|
||||
img_list: {
|
||||
minHeight: '30px',
|
||||
maxHeight: '300px',
|
||||
width: '100vw',
|
||||
position: 'absolute',
|
||||
bottom: 0,
|
||||
zIndex: 10,
|
||||
background: '#333',
|
||||
overflow: 'auto',
|
||||
},
|
||||
}
|
||||
|
||||
let currentImageIndex = 0;
|
||||
const imageUrls = [];
|
||||
let currentImageIndex = 0
|
||||
const imageUrls = []
|
||||
|
||||
let image_menu = null
|
||||
let activated = true
|
||||
|
||||
app.registerExtension({
|
||||
name: "mtb.ImageFeed",
|
||||
setup: async () => {
|
||||
// - HTML & CSS
|
||||
//- lightbox
|
||||
const lightboxContainer = document.createElement("div");
|
||||
Object.assign(lightboxContainer.style, styles.lighbox);
|
||||
name: 'mtb.ImageFeed',
|
||||
init: async () => {
|
||||
const pythongossFeed = app.extensions.find(
|
||||
(e) => e.name == 'pysssss.ImageFeed'
|
||||
)
|
||||
if (pythongossFeed) {
|
||||
console.warn(
|
||||
"[mtb] - Aborting the loading of mtb's imageFeed in favor of pysssss.ImageFeed"
|
||||
)
|
||||
activated = false // just in case other methods are added later on
|
||||
return
|
||||
}
|
||||
// - HTML & CSS
|
||||
//- lightbox
|
||||
const lightboxContainer = document.createElement('div')
|
||||
Object.assign(lightboxContainer.style, styles.lighbox)
|
||||
|
||||
const lightboxImage = document.createElement("img");
|
||||
Object.assign(lightboxImage.style, {
|
||||
maxHeight: "100%",
|
||||
maxWidth: "100%",
|
||||
borderRadius: "5px",
|
||||
});
|
||||
const lightboxImage = document.createElement('img')
|
||||
Object.assign(lightboxImage.style, {
|
||||
maxHeight: '100%',
|
||||
maxWidth: '100%',
|
||||
borderRadius: '5px',
|
||||
})
|
||||
|
||||
// previous and next buttons
|
||||
const lightboxPrevBtn = document.createElement("button");
|
||||
const lightboxNextBtn = document.createElement("button");
|
||||
// previous and next buttons
|
||||
const lightboxPrevBtn = document.createElement('button')
|
||||
const lightboxNextBtn = document.createElement('button')
|
||||
|
||||
lightboxPrevBtn.textContent = "❮";
|
||||
lightboxNextBtn.textContent = "❯";
|
||||
lightboxPrevBtn.textContent = '❮'
|
||||
lightboxNextBtn.textContent = '❯'
|
||||
|
||||
Object.assign(lightboxPrevBtn.style, styles.lightboxBtn({ left: "0%" }));
|
||||
Object.assign(lightboxNextBtn.style, styles.lightboxBtn({ right: "0%" }));
|
||||
Object.assign(lightboxPrevBtn.style, styles.lightboxBtn({ left: '0%' }))
|
||||
Object.assign(lightboxNextBtn.style, styles.lightboxBtn({ right: '0%' }))
|
||||
|
||||
// close button
|
||||
const lightboxCloseBtn = document.createElement("button");
|
||||
Object.assign(lightboxCloseBtn.style, styles.lightboxBtn({ right: "0", top: "0" }));
|
||||
lightboxCloseBtn.textContent = "❌";
|
||||
// close button
|
||||
const lightboxCloseBtn = document.createElement('button')
|
||||
Object.assign(
|
||||
lightboxCloseBtn.style,
|
||||
styles.lightboxBtn({ right: '0', top: '0' })
|
||||
)
|
||||
lightboxCloseBtn.textContent = '❌'
|
||||
|
||||
const lightboxButtons = document.createElement("div");
|
||||
Object.assign(lightboxButtons.style, {
|
||||
position: "absolute",
|
||||
top: "0%",
|
||||
right: "0%",
|
||||
// transform: "translate(50%, -50%)",
|
||||
height: "100%",
|
||||
width: "100%",
|
||||
background: "none",
|
||||
border: "none",
|
||||
color: "#fff",
|
||||
fontSize: "30px",
|
||||
cursor: "pointer",
|
||||
pointerEvents: "none",
|
||||
});
|
||||
const lightboxButtons = document.createElement('div')
|
||||
Object.assign(lightboxButtons.style, {
|
||||
position: 'absolute',
|
||||
top: '0%',
|
||||
right: '0%',
|
||||
// transform: "translate(50%, -50%)",
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
background: 'none',
|
||||
border: 'none',
|
||||
color: '#fff',
|
||||
fontSize: '30px',
|
||||
cursor: 'pointer',
|
||||
pointerEvents: 'none',
|
||||
})
|
||||
|
||||
lightboxButtons.append(lightboxPrevBtn, lightboxNextBtn, lightboxCloseBtn);
|
||||
lightboxContainer.append(lightboxButtons, lightboxImage);
|
||||
lightboxButtons.append(lightboxPrevBtn, lightboxNextBtn, lightboxCloseBtn)
|
||||
lightboxContainer.append(lightboxButtons, lightboxImage)
|
||||
|
||||
//- image list
|
||||
const imageListContainer = document.createElement("div");
|
||||
Object.assign(imageListContainer.style, styles.img_list);
|
||||
//- image list
|
||||
const imageListContainer = document.createElement('div')
|
||||
Object.assign(imageListContainer.style, styles.img_list)
|
||||
|
||||
const createImgListBtn = (text, style) => {
|
||||
const btn = document.createElement('button')
|
||||
btn.type = 'button'
|
||||
btn.textContent = text
|
||||
Object.assign(btn.style, {
|
||||
...style,
|
||||
border: 'none',
|
||||
color: '#fff',
|
||||
background: 'none',
|
||||
height: '20px',
|
||||
cursor: 'pointer',
|
||||
position: 'absolute',
|
||||
top: '5px',
|
||||
fontSize: '12px',
|
||||
lineHeight: '12px',
|
||||
})
|
||||
imageListContainer.append(btn)
|
||||
return btn
|
||||
}
|
||||
const showBtn = document.createElement('button')
|
||||
const closeBtn = createImgListBtn('❌', {
|
||||
width: '20px',
|
||||
textIndent: '-4px',
|
||||
right: '5px',
|
||||
})
|
||||
const loadButton = createImgListBtn('Load Session History', {
|
||||
right: '90px',
|
||||
})
|
||||
const clearButton = createImgListBtn('Clear', {
|
||||
right: '30px',
|
||||
})
|
||||
|
||||
const createImgListBtn = (text, style) => {
|
||||
const btn = document.createElement("button");
|
||||
btn.type = "button";
|
||||
btn.textContent = text;
|
||||
Object.assign(btn.style, {
|
||||
...style,
|
||||
border: "none",
|
||||
color: "#fff",
|
||||
background: "none",
|
||||
height: "20px",
|
||||
cursor: "pointer",
|
||||
position: "absolute",
|
||||
top: "5px",
|
||||
fontSize: "12px",
|
||||
lineHeight: "12px",
|
||||
});
|
||||
imageListContainer.append(btn);
|
||||
return btn;
|
||||
}
|
||||
const showBtn = document.createElement("button");
|
||||
const closeBtn = createImgListBtn("❌", {
|
||||
width: "20px",
|
||||
textIndent: "-4px",
|
||||
right: "5px",
|
||||
});
|
||||
const loadButton = createImgListBtn("Load Session History", {
|
||||
right: "90px",
|
||||
});
|
||||
const clearButton = createImgListBtn("Clear", {
|
||||
right: "30px",
|
||||
});
|
||||
//- tools popup button
|
||||
showBtn.classList.add('comfy-settings-btn')
|
||||
Object.assign(showBtn.style, {
|
||||
right: '16px',
|
||||
cursor: 'pointer',
|
||||
display: 'none',
|
||||
})
|
||||
|
||||
//- append to DOM
|
||||
document.body.append(imageListContainer)
|
||||
|
||||
//- tools popup button
|
||||
showBtn.classList.add("comfy-settings-btn");
|
||||
Object.assign(showBtn.style, {
|
||||
right: "16px",
|
||||
cursor: "pointer",
|
||||
display: "none",
|
||||
});
|
||||
showBtn.textContent = '🖼️'
|
||||
showBtn.onclick = () => {
|
||||
imageListContainer.style.display = 'block'
|
||||
showBtn.style.display = 'none'
|
||||
}
|
||||
document.querySelector('.comfy-settings-btn').after(showBtn)
|
||||
document.querySelector('.comfy-settings-btn').after(lightboxContainer)
|
||||
|
||||
//- append to DOM
|
||||
document.body.append(imageListContainer);
|
||||
// for (const { output } of history) {
|
||||
// if (output?.images) {
|
||||
// for (const src of output.images) {
|
||||
// const img = document.createElement("img");
|
||||
// const but = document.createElement("button");
|
||||
|
||||
//- callbacks
|
||||
closeBtn.onclick = () => {
|
||||
imageListContainer.style.display = 'none'
|
||||
showBtn.style.display = 'unset'
|
||||
}
|
||||
|
||||
showBtn.textContent = "🖼️";
|
||||
showBtn.onclick = () => {
|
||||
imageListContainer.style.display = "block";
|
||||
showBtn.style.display = "none";
|
||||
};
|
||||
document.querySelector(".comfy-settings-btn").after(showBtn);
|
||||
document.querySelector(".comfy-settings-btn").after(lightboxContainer);
|
||||
clearButton.onclick = () => {
|
||||
imageListContainer.replaceChildren(closeBtn, clearButton, loadButton)
|
||||
}
|
||||
|
||||
lightboxNextBtn.onclick = () => {
|
||||
currentImageIndex = (currentImageIndex + 1) % imageUrls.length
|
||||
const imageUrl = imageUrls[currentImageIndex]
|
||||
lightboxImage.src = imageUrl
|
||||
}
|
||||
|
||||
// Modify the lightboxPrevBtn onclick callback
|
||||
lightboxPrevBtn.onclick = () => {
|
||||
currentImageIndex =
|
||||
(currentImageIndex - 1 + imageUrls.length) % imageUrls.length
|
||||
const imageUrl = imageUrls[currentImageIndex]
|
||||
lightboxImage.src = imageUrl
|
||||
}
|
||||
|
||||
// for (const { output } of history) {
|
||||
// if (output?.images) {
|
||||
// for (const src of output.images) {
|
||||
// const img = document.createElement("img");
|
||||
// const but = document.createElement("button");
|
||||
lightboxCloseBtn.onclick = () => {
|
||||
lightboxContainer.style.display = 'none'
|
||||
}
|
||||
lightboxImage.onclick = lightboxNextBtn.onclick
|
||||
/**
|
||||
* 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
|
||||
* the image in the lightbox.
|
||||
* @param {*} src
|
||||
*/
|
||||
const createImageBtn = (src) => {
|
||||
console.debug(`making image ${src.filename}`)
|
||||
const img = document.createElement('img')
|
||||
const but = document.createElement('button')
|
||||
|
||||
//- callbacks
|
||||
closeBtn.onclick = () => {
|
||||
imageListContainer.style.display = "none";
|
||||
showBtn.style.display = "unset";
|
||||
};
|
||||
Object.assign(but.style, {
|
||||
height: '120px',
|
||||
width: '120px',
|
||||
border: 'none',
|
||||
padding: 0,
|
||||
margin: 0,
|
||||
})
|
||||
Object.assign(img.style, {
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
objectFit: 'cover',
|
||||
})
|
||||
|
||||
clearButton.onclick = () => {
|
||||
imageListContainer.replaceChildren(closeBtn, clearButton, loadButton);
|
||||
}
|
||||
img.src = `/view?filename=${encodeURIComponent(src.filename)}&type=${
|
||||
src.type
|
||||
}&subfolder=${encodeURIComponent(src.subfolder)}`
|
||||
|
||||
lightboxNextBtn.onclick = () => {
|
||||
currentImageIndex = (currentImageIndex + 1) % imageUrls.length;
|
||||
const imageUrl = imageUrls[currentImageIndex];
|
||||
lightboxImage.src = imageUrl;
|
||||
};
|
||||
imageUrls.push(img.src)
|
||||
|
||||
// Modify the lightboxPrevBtn onclick callback
|
||||
lightboxPrevBtn.onclick = () => {
|
||||
currentImageIndex = (currentImageIndex - 1 + imageUrls.length) % imageUrls.length;
|
||||
const imageUrl = imageUrls[currentImageIndex];
|
||||
lightboxImage.src = imageUrl;
|
||||
};
|
||||
console.debug(img.src)
|
||||
|
||||
img.onload = () => {
|
||||
but.style.width = `${120 * (img.naturalWidth / img.naturalHeight)}px`
|
||||
}
|
||||
|
||||
lightboxCloseBtn.onclick = () => {
|
||||
lightboxContainer.style.display = "none";
|
||||
};
|
||||
lightboxImage.onclick = lightboxNextBtn.onclick;
|
||||
/**
|
||||
* 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
|
||||
* the image in the lightbox.
|
||||
* @param {*} src
|
||||
*/
|
||||
const createImageBtn = (src) => {
|
||||
console.debug(`making image ${src.filename}`);
|
||||
const img = document.createElement("img");
|
||||
const but = document.createElement("button");
|
||||
but.onclick = () => {
|
||||
lightboxContainer.style.display = 'flex'
|
||||
// add the same image to the lightbox
|
||||
lightboxImage.src = img.src
|
||||
// lighboxContainer.replaceChildren(lightboxButtons, img);
|
||||
}
|
||||
|
||||
Object.assign(but.style, {
|
||||
height: "120px",
|
||||
width: "120px",
|
||||
});
|
||||
Object.assign(img.style, {
|
||||
width: "100%",
|
||||
height: "100%",
|
||||
objectFit: "scale-down",
|
||||
});
|
||||
// add right click menu
|
||||
but.addEventListener('contextmenu', (e) => {
|
||||
e.preventDefault()
|
||||
|
||||
img.src = `/view?filename=${encodeURIComponent(src.filename)}&type=${src.type}&subfolder=${encodeURIComponent(
|
||||
src.subfolder
|
||||
)}`;
|
||||
if (image_menu) {
|
||||
image_menu.remove()
|
||||
}
|
||||
|
||||
imageUrls.push(img.src);
|
||||
image_menu = document.createElement('div')
|
||||
Object.assign(image_menu.style, {
|
||||
position: 'absolute',
|
||||
top: `${e.clientY}px`,
|
||||
left: `${e.clientX}px`,
|
||||
background: '#333',
|
||||
color: '#fff',
|
||||
padding: '5px',
|
||||
borderRadius: '5px',
|
||||
zIndex: 999,
|
||||
})
|
||||
const load_img = document.createElement('button')
|
||||
load_img.textContent = 'Load'
|
||||
load_img.onclick = () => {
|
||||
app.handleFile(img.src)
|
||||
}
|
||||
|
||||
console.debug(img.src)
|
||||
image_menu.appendChild(load_img)
|
||||
document.body.appendChild(image_menu)
|
||||
})
|
||||
|
||||
but.onclick = () => {
|
||||
lightboxContainer.style.display = "flex";
|
||||
// add the same image to the lightbox
|
||||
lightboxImage.src = img.src;
|
||||
// lighboxContainer.replaceChildren(lightboxButtons, img);
|
||||
but.append(img)
|
||||
imageListContainer.prepend(but)
|
||||
}
|
||||
|
||||
};
|
||||
loadButton.onclick = async () => {
|
||||
const all_history = await api.getHistory()
|
||||
for (const history of all_history.History) {
|
||||
if (history.outputs) {
|
||||
for (const key of Object.keys(history.outputs)) {
|
||||
console.debug(key)
|
||||
if (history.outputs[key].images) {
|
||||
for (const im of history.outputs[key].images) {
|
||||
console.debug(im)
|
||||
createImageBtn(im)
|
||||
}
|
||||
}
|
||||
}
|
||||
// for (const src of outputs.outputs.images) {
|
||||
// console.debug(src)
|
||||
// makeImage(`${src.subfolder}/${src.filename}`)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add right click menu
|
||||
but.addEventListener("contextmenu", (e) => {
|
||||
e.preventDefault();
|
||||
///////-------
|
||||
|
||||
if (image_menu) {
|
||||
image_menu.remove();
|
||||
}
|
||||
// const all_history = await api.getHistory()
|
||||
// for (const history of all_history.History) {
|
||||
// if (history.outputs) {
|
||||
// for (const key of Object.keys(history.outputs)) {
|
||||
// for (const im of history.outputs[key].images) {
|
||||
// makeImage(im)
|
||||
// }
|
||||
// }
|
||||
// // for (const src of outputs.outputs.images) {
|
||||
// // console.debug(src)
|
||||
// // makeImage(`${src.subfolder}/${src.filename}`)
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
|
||||
image_menu = document.createElement("div");
|
||||
Object.assign(image_menu.style, {
|
||||
position: "absolute",
|
||||
top: `${e.clientY}px`,
|
||||
left: `${e.clientX}px`,
|
||||
background: "#333",
|
||||
color: "#fff",
|
||||
padding: "5px",
|
||||
borderRadius: "5px",
|
||||
zIndex: 999,
|
||||
});
|
||||
const load_img = document.createElement("button");
|
||||
load_img.textContent = "Load";
|
||||
load_img.onclick = () => {
|
||||
app.handleFile(img.src)
|
||||
|
||||
}
|
||||
|
||||
image_menu.appendChild(load_img)
|
||||
document.body.appendChild(image_menu)
|
||||
})
|
||||
|
||||
but.append(img)
|
||||
imageListContainer.prepend(but)
|
||||
};
|
||||
|
||||
|
||||
loadButton.onclick = async () => {
|
||||
const all_history = await api.getHistory();
|
||||
for (const history of all_history.History) {
|
||||
if (history.outputs) {
|
||||
for (const key of Object.keys(history.outputs)) {
|
||||
console.debug(key)
|
||||
for (const im of history.outputs[key].images) {
|
||||
console.debug(im)
|
||||
createImageBtn(im)
|
||||
}
|
||||
}
|
||||
// for (const src of outputs.outputs.images) {
|
||||
// console.debug(src)
|
||||
// makeImage(`${src.subfolder}/${src.filename}`)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////-------
|
||||
|
||||
// const all_history = await api.getHistory()
|
||||
// for (const history of all_history.History) {
|
||||
// if (history.outputs) {
|
||||
// for (const key of Object.keys(history.outputs)) {
|
||||
// for (const im of history.outputs[key].images) {
|
||||
// makeImage(im)
|
||||
// }
|
||||
// }
|
||||
// // for (const src of outputs.outputs.images) {
|
||||
// // console.debug(src)
|
||||
// // makeImage(`${src.subfolder}/${src.filename}`)
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
|
||||
//- Hook into the API
|
||||
api.addEventListener("executed", ({ detail }) => {
|
||||
if (detail?.output?.images) {
|
||||
for (const src of detail.output.images) {
|
||||
console.debug(`Adding ${src} to image feed`)
|
||||
createImageBtn(src)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
//- Hook into the API
|
||||
api.addEventListener('executed', ({ detail }) => {
|
||||
if (detail?.output?.images) {
|
||||
for (const src of detail.output.images) {
|
||||
console.debug(`Adding ${src} to image feed`)
|
||||
createImageBtn(src)
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
+898
-1058
File diff suppressed because it is too large
Load Diff
+115
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* File: notify.js
|
||||
* Project: comfy_mtb
|
||||
* Author: Mel Massadian
|
||||
*
|
||||
* Copyright (c) 2023 Mel Massadian
|
||||
*
|
||||
*/
|
||||
|
||||
import { app } from '/scripts/app.js'
|
||||
|
||||
const log = (...args) => {
|
||||
if (window.MTB?.TRACE) {
|
||||
console.debug(...args)
|
||||
}
|
||||
}
|
||||
|
||||
let transition_time = 300
|
||||
|
||||
const containerStyle = `
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
left: 20px;
|
||||
font-family: monospace;
|
||||
z-index: 99999;
|
||||
height: 0;
|
||||
overflow: hidden;
|
||||
transition: height ${transition_time}ms ease-in-out;
|
||||
|
||||
`
|
||||
|
||||
const toastStyle = `
|
||||
background-color: #333;
|
||||
color: #fff;
|
||||
padding: 10px;
|
||||
border-radius: 5px;
|
||||
opacity: 0;
|
||||
overflow:hidden;
|
||||
height:20px;
|
||||
transition-property: opacity, height, padding;
|
||||
transition-duration: ${transition_time}ms;
|
||||
`
|
||||
|
||||
function notify(message, timeout = 3000) {
|
||||
log('Creating toast')
|
||||
const container = document.getElementById('mtb-notify-container')
|
||||
const toast = document.createElement('div')
|
||||
toast.style.cssText = toastStyle
|
||||
toast.innerText = message
|
||||
container.appendChild(toast)
|
||||
|
||||
toast.addEventListener('transitionend', (e) => {
|
||||
// Only on out
|
||||
if (
|
||||
e.target === toast &&
|
||||
e.propertyName === 'height' &&
|
||||
e.elapsedTime > transition_time / 1000 - Number.EPSILON
|
||||
) {
|
||||
log('Transition out')
|
||||
const totalHeight = Array.from(container.children).reduce(
|
||||
(acc, child) => acc + child.offsetHeight + 10, // Add spacing of 10px between toasts
|
||||
0
|
||||
)
|
||||
container.style.height = `${totalHeight}px`
|
||||
|
||||
// If there are no toasts left, set the container's height to 0
|
||||
if (container.children.length === 0) {
|
||||
container.style.height = '0'
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
container.removeChild(toast)
|
||||
log('Removed toast from DOM')
|
||||
}, transition_time)
|
||||
} else {
|
||||
log('Transition')
|
||||
}
|
||||
})
|
||||
|
||||
// Fading in the toast
|
||||
toast.style.opacity = '1'
|
||||
|
||||
// Update container's height to fit new toast
|
||||
const totalHeight = Array.from(container.children).reduce(
|
||||
(acc, child) => acc + child.offsetHeight + 10, // Add spacing of 10px between toasts
|
||||
0
|
||||
)
|
||||
container.style.height = `${totalHeight}px`
|
||||
|
||||
// remove the toast after the specified timeout
|
||||
setTimeout(() => {
|
||||
// trigger the transitions
|
||||
toast.style.opacity = '0'
|
||||
toast.style.height = '0'
|
||||
toast.style.paddingTop = '0'
|
||||
toast.style.paddingBottom = '0'
|
||||
}, timeout - transition_time)
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: 'mtb.Notify',
|
||||
setup() {
|
||||
if (!window.MTB) {
|
||||
window.MTB = {}
|
||||
}
|
||||
|
||||
const container = document.createElement('div')
|
||||
container.id = 'mtb-notify-container'
|
||||
container.style.cssText = containerStyle
|
||||
|
||||
document.body.appendChild(container)
|
||||
window.MTB.notify = notify
|
||||
// window.MTB.notify('Hello world!')
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user