Instructions to use modest-mouse/ltx-ugc-bundle with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LTX-2
How to use modest-mouse/ltx-ugc-bundle with LTX-2:
# Install the LTX-2 pipelines git clone https://github.com/Lightricks/LTX-2.git cd LTX-2 uv sync --frozen
# Download the weights from this repo, plus the Gemma text encoder hf download modest-mouse/ltx-ugc-bundle --local-dir models/ltx-ugc-bundle hf download google/gemma-3-12b-it-qat-q4_0-unquantized --local-dir models/gemma-3-12b
# Fast pipeline (distilled model, no distilled LoRA needed) uv run python -m ltx_pipelines.distilled \ --distilled-checkpoint-path models/ltx-ugc-bundle/<distilled-checkpoint>.safetensors \ --spatial-upsampler-path models/ltx-ugc-bundle/<spatial-upsampler>.safetensors \ --gemma-root models/gemma-3-12b \ --prompt "A beautiful sunset over the ocean" \ --output-path output.mp4 # For image-to-video, add: --image path/to/image.jpg 0 0.8# HQ pipeline (two-stage, higher quality) uv run python -m ltx_pipelines.ti2vid_two_stages_hq \ --checkpoint-path models/ltx-ugc-bundle/<checkpoint>.safetensors \ --distilled-lora models/ltx-ugc-bundle/<distilled-lora>.safetensors 0.8 \ --spatial-upsampler-path models/ltx-ugc-bundle/<spatial-upsampler>.safetensors \ --gemma-root models/gemma-3-12b \ --prompt "A beautiful sunset over the ocean" \ --output-path output.mp4 # For image-to-video, add: --image path/to/image.jpg 0 0.8 - Notebooks
- Google Colab
- Kaggle
Commit ·
0c222d5
0
Parent(s):
Duplicate from qwecja/ltx-ugc-bundle
Browse filesCo-authored-by: chen <qwecja@users.noreply.huggingface.co>
This view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +47 -0
- README.md +41 -0
- custom_nodes/ComfyMath/.gitignore +1 -0
- custom_nodes/ComfyMath/LICENSE +201 -0
- custom_nodes/ComfyMath/README.md +19 -0
- custom_nodes/ComfyMath/__init__.py +29 -0
- custom_nodes/ComfyMath/pyproject.toml +19 -0
- custom_nodes/ComfyMath/requirements.txt +1 -0
- custom_nodes/ComfyMath/src/comfymath/__init__.py +0 -0
- custom_nodes/ComfyMath/src/comfymath/bool.py +59 -0
- custom_nodes/ComfyMath/src/comfymath/control.py +3 -0
- custom_nodes/ComfyMath/src/comfymath/convert.py +273 -0
- custom_nodes/ComfyMath/src/comfymath/float.py +159 -0
- custom_nodes/ComfyMath/src/comfymath/graphics.py +153 -0
- custom_nodes/ComfyMath/src/comfymath/int.py +129 -0
- custom_nodes/ComfyMath/src/comfymath/number.py +94 -0
- custom_nodes/ComfyMath/src/comfymath/py.typed +0 -0
- custom_nodes/ComfyMath/src/comfymath/types.py +16 -0
- custom_nodes/ComfyMath/src/comfymath/vec.py +500 -0
- custom_nodes/ComfyUI-Custom-Scripts/.github/workflows/publish.yml +25 -0
- custom_nodes/ComfyUI-Custom-Scripts/.gitignore +5 -0
- custom_nodes/ComfyUI-Custom-Scripts/LICENSE +21 -0
- custom_nodes/ComfyUI-Custom-Scripts/README.md +160 -0
- custom_nodes/ComfyUI-Custom-Scripts/__init__.py +25 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/autocomplete.py +29 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/better_combos.py +172 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/constrain_image.py +71 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/constrain_image_for_video.py +72 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/math_expression.py +252 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/model_info.py +115 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/play_sound.py +42 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/repeater.py +46 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/reroute_primitive.py +59 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/show_text.py +49 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/string_function.py +49 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/system_notification.py +41 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/text_files.py +204 -0
- custom_nodes/ComfyUI-Custom-Scripts/py/workflows.py +61 -0
- custom_nodes/ComfyUI-Custom-Scripts/pyproject.toml +13 -0
- custom_nodes/ComfyUI-Custom-Scripts/pysssss.default.json +4 -0
- custom_nodes/ComfyUI-Custom-Scripts/pysssss.example.json +7 -0
- custom_nodes/ComfyUI-Custom-Scripts/pysssss.py +300 -0
- custom_nodes/ComfyUI-Custom-Scripts/user/text_file_dirs.json +5 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/canvas2svg.js +1192 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/favicon-active.ico +0 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/favicon.ico +0 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/no-image.png +0 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/notify.mp3 +0 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/autocompleter.js +602 -0
- custom_nodes/ComfyUI-Custom-Scripts/web/js/betterCombos.js +534 -0
.gitattributes
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
custom_nodes/ComfyUI-KJNodes/docs/images/2024-04-03_20_49_29-ComfyUI.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
custom_nodes/ComfyUI-KJNodes/fonts/FreeMono.ttf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
custom_nodes/ComfyUI-KJNodes/fonts/FreeMonoBoldOblique.otf filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
custom_nodes/ComfyUI-KJNodes/fonts/TTNorms-Black.otf filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
custom_nodes/ComfyUI-LTXVideo/gemma_configs/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
custom_nodes/rgthree-comfy/docs/rgthree_advanced.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
custom_nodes/rgthree-comfy/docs/rgthree_advanced_metadata.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
custom_nodes/rgthree-comfy/docs/rgthree_context.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
custom_nodes/rgthree-comfy/docs/rgthree_context_metadata.png filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
custom_nodes/ComfyUI-Easy-Use/py/modules/kolors/chatglm/tokenizer/vocab.txt filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
custom_nodes/ComfyUI-Easy-Use/resources/OpenSans-Medium.ttf filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
custom_nodes/ComfyUI-Easy-Use/resources/wenquan.ttf filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
tags:
|
| 4 |
+
- ltx
|
| 5 |
+
- ltx-2.3
|
| 6 |
+
- video-generation
|
| 7 |
+
- i2v
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Douravita UGC — LTX 2.3 I2V model bundle
|
| 11 |
+
|
| 12 |
+
Consolidated set of the 10 models needed to run the LTX 2.3 All-In-One workflow
|
| 13 |
+
(mode 03 Image-to-Video with native audio). Re-packaged from upstream
|
| 14 |
+
Hugging Face repos for a single-shot ~42 GB download during Docker builds.
|
| 15 |
+
|
| 16 |
+
## Layout
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
diffusion_models/ltx-2.3-22b-dev_transformer_only_fp8_scaled.safetensors (Kijai/LTX2.3_comfy)
|
| 20 |
+
vae/LTX23_video_vae_bf16.safetensors (Kijai/LTX2.3_comfy)
|
| 21 |
+
vae/LTX23_audio_vae_bf16.safetensors (Kijai/LTX2.3_comfy)
|
| 22 |
+
vae/taeltx2_3.safetensors (Kijai/LTX2.3_comfy)
|
| 23 |
+
text_encoders/gemma_3_12B_it_fp4_mixed.safetensors (Comfy-Org/ltx-2)
|
| 24 |
+
text_encoders/ltx-2.3_text_projection_bf16.safetensors (Kijai/LTX2.3_comfy)
|
| 25 |
+
latent_upscale_models/ltx-2.3-spatial-upscaler-x2-1.1.safetensors (Lightricks/LTX-2.3)
|
| 26 |
+
loras/ltx-2.3-22b-distilled-lora-dynamic_fro09_avg_rank_105_bf16.safetensors (Kijai/LTX2.3_comfy)
|
| 27 |
+
loras/ltx-2.3-22b-ic-lora-union-control-ref0.5.safetensors (Lightricks/...)
|
| 28 |
+
loras/ltx-2-19b-ic-lora-detailer.safetensors (Lightricks/...)
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Usage
|
| 32 |
+
|
| 33 |
+
```bash
|
| 34 |
+
hf download DouraVITA/ltx-ugc-bundle --local-dir ./models
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
All file paths are preserved — drop into `ComfyUI/models/` directly.
|
| 38 |
+
|
| 39 |
+
## Licenses
|
| 40 |
+
|
| 41 |
+
Each model carries its original upstream license. See source repos linked above.
|
custom_nodes/ComfyMath/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
**/__pycache__
|
custom_nodes/ComfyMath/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [yyyy] [name of copyright owner]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
custom_nodes/ComfyMath/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ComfyMath
|
| 2 |
+
|
| 3 |
+
Provides Math Nodes for [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
Provides nodes for:
|
| 8 |
+
* Boolean Logic
|
| 9 |
+
* Integer Arithmetic
|
| 10 |
+
* Floating Point Arithmetic and Functions
|
| 11 |
+
* Vec2, Vec3, and Vec4 Arithmetic and Functions
|
| 12 |
+
|
| 13 |
+
## Installation
|
| 14 |
+
|
| 15 |
+
From the `custom_nodes` directory in your ComfyUI installation, run:
|
| 16 |
+
|
| 17 |
+
```sh
|
| 18 |
+
git clone https://github.com/evanspearman/ComfyMath.git
|
| 19 |
+
```
|
custom_nodes/ComfyMath/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .src.comfymath.convert import NODE_CLASS_MAPPINGS as convert_NCM
|
| 2 |
+
from .src.comfymath.bool import NODE_CLASS_MAPPINGS as bool_NCM
|
| 3 |
+
from .src.comfymath.int import NODE_CLASS_MAPPINGS as int_NCM
|
| 4 |
+
from .src.comfymath.float import NODE_CLASS_MAPPINGS as float_NCM
|
| 5 |
+
from .src.comfymath.number import NODE_CLASS_MAPPINGS as number_NCM
|
| 6 |
+
from .src.comfymath.vec import NODE_CLASS_MAPPINGS as vec_NCM
|
| 7 |
+
from .src.comfymath.control import NODE_CLASS_MAPPINGS as control_NCM
|
| 8 |
+
from .src.comfymath.graphics import NODE_CLASS_MAPPINGS as graphics_NCM
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
NODE_CLASS_MAPPINGS = {
|
| 12 |
+
**convert_NCM,
|
| 13 |
+
**bool_NCM,
|
| 14 |
+
**int_NCM,
|
| 15 |
+
**float_NCM,
|
| 16 |
+
**number_NCM,
|
| 17 |
+
**vec_NCM,
|
| 18 |
+
**control_NCM,
|
| 19 |
+
**graphics_NCM,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def remove_cm_prefix(node_mapping: str) -> str:
|
| 24 |
+
if node_mapping.startswith("CM_"):
|
| 25 |
+
return node_mapping[3:]
|
| 26 |
+
return node_mapping
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
NODE_DISPLAY_NAME_MAPPINGS = {key: remove_cm_prefix(key) for key in NODE_CLASS_MAPPINGS}
|
custom_nodes/ComfyMath/pyproject.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[tool.poetry]
|
| 2 |
+
name = "comfymath"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Math nodes for ComfyUI"
|
| 5 |
+
authors = ["Evan Spearman <evan@spearman.mb.ca>"]
|
| 6 |
+
license = { text = "Apache License 2.0" }
|
| 7 |
+
readme = "README.md"
|
| 8 |
+
|
| 9 |
+
[tool.poetry.dependencies]
|
| 10 |
+
python = "^3.10"
|
| 11 |
+
numpy = "^1.25.1"
|
| 12 |
+
|
| 13 |
+
[tool.poetry.group.dev.dependencies]
|
| 14 |
+
mypy = "^1.4.1"
|
| 15 |
+
black = "^23.7.0"
|
| 16 |
+
|
| 17 |
+
[build-system]
|
| 18 |
+
requires = ["poetry-core"]
|
| 19 |
+
build-backend = "poetry.core.masonry.api"
|
custom_nodes/ComfyMath/requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
numpy
|
custom_nodes/ComfyMath/src/comfymath/__init__.py
ADDED
|
File without changes
|
custom_nodes/ComfyMath/src/comfymath/bool.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Callable, Mapping
|
| 2 |
+
|
| 3 |
+
DEFAULT_BOOL = ("BOOLEAN", {"default": False})
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
BOOL_UNARY_OPERATIONS: Mapping[str, Callable[[bool], bool]] = {
|
| 7 |
+
"Not": lambda a: not a,
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
BOOL_BINARY_OPERATIONS: Mapping[str, Callable[[bool, bool], bool]] = {
|
| 11 |
+
"Nor": lambda a, b: not (a or b),
|
| 12 |
+
"Xor": lambda a, b: a ^ b,
|
| 13 |
+
"Nand": lambda a, b: not (a and b),
|
| 14 |
+
"And": lambda a, b: a and b,
|
| 15 |
+
"Xnor": lambda a, b: not (a ^ b),
|
| 16 |
+
"Or": lambda a, b: a or b,
|
| 17 |
+
"Eq": lambda a, b: a == b,
|
| 18 |
+
"Neq": lambda a, b: a != b,
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class BoolUnaryOperation:
|
| 23 |
+
@classmethod
|
| 24 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 25 |
+
return {
|
| 26 |
+
"required": {"op": (list(BOOL_UNARY_OPERATIONS.keys()),), "a": DEFAULT_BOOL}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
RETURN_TYPES = ("BOOLEAN",)
|
| 30 |
+
FUNCTION = "op"
|
| 31 |
+
CATEGORY = "math/bool"
|
| 32 |
+
|
| 33 |
+
def op(self, op: str, a: bool) -> tuple[bool]:
|
| 34 |
+
return (BOOL_UNARY_OPERATIONS[op](a),)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class BoolBinaryOperation:
|
| 38 |
+
@classmethod
|
| 39 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 40 |
+
return {
|
| 41 |
+
"required": {
|
| 42 |
+
"op": (list(BOOL_BINARY_OPERATIONS.keys()),),
|
| 43 |
+
"a": DEFAULT_BOOL,
|
| 44 |
+
"b": DEFAULT_BOOL,
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
RETURN_TYPES = ("BOOLEAN",)
|
| 49 |
+
FUNCTION = "op"
|
| 50 |
+
CATEGORY = "math/bool"
|
| 51 |
+
|
| 52 |
+
def op(self, op: str, a: bool, b: bool) -> tuple[bool]:
|
| 53 |
+
return (BOOL_BINARY_OPERATIONS[op](a, b),)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
NODE_CLASS_MAPPINGS = {
|
| 57 |
+
"CM_BoolUnaryOperation": BoolUnaryOperation,
|
| 58 |
+
"CM_BoolBinaryOperation": BoolBinaryOperation,
|
| 59 |
+
}
|
custom_nodes/ComfyMath/src/comfymath/control.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Mapping
|
| 2 |
+
|
| 3 |
+
NODE_CLASS_MAPPINGS: Mapping[str, Any] = {}
|
custom_nodes/ComfyMath/src/comfymath/convert.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Mapping
|
| 2 |
+
|
| 3 |
+
from .vec import VEC2_ZERO, VEC3_ZERO, VEC4_ZERO
|
| 4 |
+
from .types import Number, Vec2, Vec3, Vec4
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BoolToInt:
|
| 8 |
+
@classmethod
|
| 9 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 10 |
+
return {"required": {"a": ("BOOLEAN", {"default": False})}}
|
| 11 |
+
|
| 12 |
+
RETURN_TYPES = ("INT",)
|
| 13 |
+
FUNCTION = "op"
|
| 14 |
+
CATEGORY = "math/conversion"
|
| 15 |
+
|
| 16 |
+
def op(self, a: bool) -> tuple[int]:
|
| 17 |
+
return (int(a),)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class IntToBool:
|
| 21 |
+
@classmethod
|
| 22 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 23 |
+
return {"required": {"a": ("INT", {"default": 0})}}
|
| 24 |
+
|
| 25 |
+
RETURN_TYPES = ("BOOLEAN",)
|
| 26 |
+
FUNCTION = "op"
|
| 27 |
+
CATEGORY = "math/conversion"
|
| 28 |
+
|
| 29 |
+
def op(self, a: int) -> tuple[bool]:
|
| 30 |
+
return (a != 0,)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class FloatToInt:
|
| 34 |
+
@classmethod
|
| 35 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 36 |
+
return {"required": {"a": ("FLOAT", {"default": 0.0, "round": False})}}
|
| 37 |
+
|
| 38 |
+
RETURN_TYPES = ("INT",)
|
| 39 |
+
FUNCTION = "op"
|
| 40 |
+
CATEGORY = "math/conversion"
|
| 41 |
+
|
| 42 |
+
def op(self, a: float) -> tuple[int]:
|
| 43 |
+
return (int(a),)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class IntToFloat:
|
| 47 |
+
@classmethod
|
| 48 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 49 |
+
return {"required": {"a": ("INT", {"default": 0})}}
|
| 50 |
+
|
| 51 |
+
RETURN_TYPES = ("FLOAT",)
|
| 52 |
+
FUNCTION = "op"
|
| 53 |
+
CATEGORY = "math/conversion"
|
| 54 |
+
|
| 55 |
+
def op(self, a: int) -> tuple[float]:
|
| 56 |
+
return (float(a),)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class IntToNumber:
|
| 60 |
+
@classmethod
|
| 61 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 62 |
+
return {"required": {"a": ("INT", {"default": 0})}}
|
| 63 |
+
|
| 64 |
+
RETURN_TYPES = ("NUMBER",)
|
| 65 |
+
FUNCTION = "op"
|
| 66 |
+
CATEGORY = "math/conversion"
|
| 67 |
+
|
| 68 |
+
def op(self, a: int) -> tuple[Number]:
|
| 69 |
+
return (a,)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class NumberToInt:
|
| 73 |
+
@classmethod
|
| 74 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 75 |
+
return {"required": {"a": ("NUMBER", {"default": 0.0})}}
|
| 76 |
+
|
| 77 |
+
RETURN_TYPES = ("INT",)
|
| 78 |
+
FUNCTION = "op"
|
| 79 |
+
CATEGORY = "math/conversion"
|
| 80 |
+
|
| 81 |
+
def op(self, a: Number) -> tuple[int]:
|
| 82 |
+
return (int(a),)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class FloatToNumber:
|
| 86 |
+
@classmethod
|
| 87 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 88 |
+
return {"required": {"a": ("FLOAT", {"default": 0.0, "round": False})}}
|
| 89 |
+
|
| 90 |
+
RETURN_TYPES = ("NUMBER",)
|
| 91 |
+
FUNCTION = "op"
|
| 92 |
+
CATEGORY = "math/conversion"
|
| 93 |
+
|
| 94 |
+
def op(self, a: float) -> tuple[Number]:
|
| 95 |
+
return (a,)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class NumberToFloat:
|
| 99 |
+
@classmethod
|
| 100 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 101 |
+
return {"required": {"a": ("NUMBER", {"default": 0.0})}}
|
| 102 |
+
|
| 103 |
+
RETURN_TYPES = ("FLOAT",)
|
| 104 |
+
FUNCTION = "op"
|
| 105 |
+
CATEGORY = "math/conversion"
|
| 106 |
+
|
| 107 |
+
def op(self, a: Number) -> tuple[float]:
|
| 108 |
+
return (float(a),)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class ComposeVec2:
|
| 112 |
+
@classmethod
|
| 113 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 114 |
+
return {
|
| 115 |
+
"required": {
|
| 116 |
+
"x": ("FLOAT", {"default": 0.0, "round": False}),
|
| 117 |
+
"y": ("FLOAT", {"default": 0.0, "round": False}),
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
RETURN_TYPES = ("VEC2",)
|
| 122 |
+
FUNCTION = "op"
|
| 123 |
+
CATEGORY = "math/conversion"
|
| 124 |
+
|
| 125 |
+
def op(self, x: float, y: float) -> tuple[Vec2]:
|
| 126 |
+
return ((x, y),)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
class FillVec2:
|
| 130 |
+
@classmethod
|
| 131 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 132 |
+
return {
|
| 133 |
+
"required": {
|
| 134 |
+
"a": ("FLOAT", {"default": 0.0, "round": False}),
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
RETURN_TYPES = ("VEC2",)
|
| 139 |
+
FUNCTION = "op"
|
| 140 |
+
CATEGORY = "math/conversion"
|
| 141 |
+
|
| 142 |
+
def op(self, a: float) -> tuple[Vec2]:
|
| 143 |
+
return ((a, a),)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
class BreakoutVec2:
|
| 147 |
+
@classmethod
|
| 148 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 149 |
+
return {"required": {"a": ("VEC2", {"default": VEC2_ZERO})}}
|
| 150 |
+
|
| 151 |
+
RETURN_TYPES = ("FLOAT", "FLOAT")
|
| 152 |
+
FUNCTION = "op"
|
| 153 |
+
CATEGORY = "math/conversion"
|
| 154 |
+
|
| 155 |
+
def op(self, a: Vec2) -> tuple[float, float]:
|
| 156 |
+
return (a[0], a[1])
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
class ComposeVec3:
|
| 160 |
+
@classmethod
|
| 161 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 162 |
+
return {
|
| 163 |
+
"required": {
|
| 164 |
+
"x": ("FLOAT", {"default": 0.0}),
|
| 165 |
+
"y": ("FLOAT", {"default": 0.0}),
|
| 166 |
+
"z": ("FLOAT", {"default": 0.0}),
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
RETURN_TYPES = ("VEC3",)
|
| 171 |
+
FUNCTION = "op"
|
| 172 |
+
CATEGORY = "math/conversion"
|
| 173 |
+
|
| 174 |
+
def op(self, x: float, y: float, z: float) -> tuple[Vec3]:
|
| 175 |
+
return ((x, y, z),)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
class FillVec3:
|
| 179 |
+
@classmethod
|
| 180 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 181 |
+
return {
|
| 182 |
+
"required": {
|
| 183 |
+
"a": ("FLOAT", {"default": 0.0}),
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
RETURN_TYPES = ("VEC3",)
|
| 188 |
+
FUNCTION = "op"
|
| 189 |
+
CATEGORY = "math/conversion"
|
| 190 |
+
|
| 191 |
+
def op(self, a: float) -> tuple[Vec3]:
|
| 192 |
+
return ((a, a, a),)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
class BreakoutVec3:
|
| 196 |
+
@classmethod
|
| 197 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 198 |
+
return {"required": {"a": ("VEC3", {"default": VEC3_ZERO})}}
|
| 199 |
+
|
| 200 |
+
RETURN_TYPES = ("FLOAT", "FLOAT", "FLOAT")
|
| 201 |
+
FUNCTION = "op"
|
| 202 |
+
CATEGORY = "math/conversion"
|
| 203 |
+
|
| 204 |
+
def op(self, a: Vec3) -> tuple[float, float, float]:
|
| 205 |
+
return (a[0], a[1], a[2])
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
class ComposeVec4:
|
| 209 |
+
@classmethod
|
| 210 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 211 |
+
return {
|
| 212 |
+
"required": {
|
| 213 |
+
"x": ("FLOAT", {"default": 0.0}),
|
| 214 |
+
"y": ("FLOAT", {"default": 0.0}),
|
| 215 |
+
"z": ("FLOAT", {"default": 0.0}),
|
| 216 |
+
"w": ("FLOAT", {"default": 0.0}),
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
RETURN_TYPES = ("VEC4",)
|
| 221 |
+
FUNCTION = "op"
|
| 222 |
+
CATEGORY = "math/conversion"
|
| 223 |
+
|
| 224 |
+
def op(self, x: float, y: float, z: float, w: float) -> tuple[Vec4]:
|
| 225 |
+
return ((x, y, z, w),)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
class FillVec4:
|
| 229 |
+
@classmethod
|
| 230 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 231 |
+
return {
|
| 232 |
+
"required": {
|
| 233 |
+
"a": ("FLOAT", {"default": 0.0}),
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
RETURN_TYPES = ("VEC4",)
|
| 238 |
+
FUNCTION = "op"
|
| 239 |
+
CATEGORY = "math/conversion"
|
| 240 |
+
|
| 241 |
+
def op(self, a: float) -> tuple[Vec4]:
|
| 242 |
+
return ((a, a, a, a),)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
class BreakoutVec4:
|
| 246 |
+
@classmethod
|
| 247 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 248 |
+
return {"required": {"a": ("VEC4", {"default": VEC4_ZERO})}}
|
| 249 |
+
|
| 250 |
+
RETURN_TYPES = ("FLOAT", "FLOAT", "FLOAT", "FLOAT")
|
| 251 |
+
FUNCTION = "op"
|
| 252 |
+
CATEGORY = "math/conversion"
|
| 253 |
+
|
| 254 |
+
def op(self, a: Vec4) -> tuple[float, float, float, float]:
|
| 255 |
+
return (a[0], a[1], a[2], a[3])
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
NODE_CLASS_MAPPINGS = {
|
| 259 |
+
"CM_BoolToInt": BoolToInt,
|
| 260 |
+
"CM_IntToBool": IntToBool,
|
| 261 |
+
"CM_FloatToInt": FloatToInt,
|
| 262 |
+
"CM_IntToFloat": IntToFloat,
|
| 263 |
+
"CM_IntToNumber": IntToNumber,
|
| 264 |
+
"CM_NumberToInt": NumberToInt,
|
| 265 |
+
"CM_FloatToNumber": FloatToNumber,
|
| 266 |
+
"CM_NumberToFloat": NumberToFloat,
|
| 267 |
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
from typing import Any, Callable, Mapping
|
| 4 |
+
|
| 5 |
+
DEFAULT_FLOAT = ("FLOAT", {"default": 0.0, "step": 0.001, "round": False})
|
| 6 |
+
|
| 7 |
+
FLOAT_UNARY_OPERATIONS: Mapping[str, Callable[[float], float]] = {
|
| 8 |
+
"Neg": lambda a: -a,
|
| 9 |
+
"Inc": lambda a: a + 1,
|
| 10 |
+
"Dec": lambda a: a - 1,
|
| 11 |
+
"Abs": lambda a: abs(a),
|
| 12 |
+
"Sqr": lambda a: a * a,
|
| 13 |
+
"Cube": lambda a: a * a * a,
|
| 14 |
+
"Sqrt": lambda a: math.sqrt(a),
|
| 15 |
+
"Exp": lambda a: math.exp(a),
|
| 16 |
+
"Ln": lambda a: math.log(a),
|
| 17 |
+
"Log10": lambda a: math.log10(a),
|
| 18 |
+
"Log2": lambda a: math.log2(a),
|
| 19 |
+
"Sin": lambda a: math.sin(a),
|
| 20 |
+
"Cos": lambda a: math.cos(a),
|
| 21 |
+
"Tan": lambda a: math.tan(a),
|
| 22 |
+
"Asin": lambda a: math.asin(a),
|
| 23 |
+
"Acos": lambda a: math.acos(a),
|
| 24 |
+
"Atan": lambda a: math.atan(a),
|
| 25 |
+
"Sinh": lambda a: math.sinh(a),
|
| 26 |
+
"Cosh": lambda a: math.cosh(a),
|
| 27 |
+
"Tanh": lambda a: math.tanh(a),
|
| 28 |
+
"Asinh": lambda a: math.asinh(a),
|
| 29 |
+
"Acosh": lambda a: math.acosh(a),
|
| 30 |
+
"Atanh": lambda a: math.atanh(a),
|
| 31 |
+
"Round": lambda a: round(a),
|
| 32 |
+
"Floor": lambda a: math.floor(a),
|
| 33 |
+
"Ceil": lambda a: math.ceil(a),
|
| 34 |
+
"Trunc": lambda a: math.trunc(a),
|
| 35 |
+
"Erf": lambda a: math.erf(a),
|
| 36 |
+
"Erfc": lambda a: math.erfc(a),
|
| 37 |
+
"Gamma": lambda a: math.gamma(a),
|
| 38 |
+
"Radians": lambda a: math.radians(a),
|
| 39 |
+
"Degrees": lambda a: math.degrees(a),
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
FLOAT_UNARY_CONDITIONS: Mapping[str, Callable[[float], bool]] = {
|
| 43 |
+
"IsZero": lambda a: a == 0.0,
|
| 44 |
+
"IsPositive": lambda a: a > 0.0,
|
| 45 |
+
"IsNegative": lambda a: a < 0.0,
|
| 46 |
+
"IsNonZero": lambda a: a != 0.0,
|
| 47 |
+
"IsPositiveInfinity": lambda a: math.isinf(a) and a > 0.0,
|
| 48 |
+
"IsNegativeInfinity": lambda a: math.isinf(a) and a < 0.0,
|
| 49 |
+
"IsNaN": lambda a: math.isnan(a),
|
| 50 |
+
"IsFinite": lambda a: math.isfinite(a),
|
| 51 |
+
"IsInfinite": lambda a: math.isinf(a),
|
| 52 |
+
"IsEven": lambda a: a % 2 == 0.0,
|
| 53 |
+
"IsOdd": lambda a: a % 2 != 0.0,
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
FLOAT_BINARY_OPERATIONS: Mapping[str, Callable[[float, float], float]] = {
|
| 57 |
+
"Add": lambda a, b: a + b,
|
| 58 |
+
"Sub": lambda a, b: a - b,
|
| 59 |
+
"Mul": lambda a, b: a * b,
|
| 60 |
+
"Div": lambda a, b: a / b,
|
| 61 |
+
"Mod": lambda a, b: a % b,
|
| 62 |
+
"Pow": lambda a, b: a**b,
|
| 63 |
+
"FloorDiv": lambda a, b: a // b,
|
| 64 |
+
"Max": lambda a, b: max(a, b),
|
| 65 |
+
"Min": lambda a, b: min(a, b),
|
| 66 |
+
"Log": lambda a, b: math.log(a, b),
|
| 67 |
+
"Atan2": lambda a, b: math.atan2(a, b),
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
FLOAT_BINARY_CONDITIONS: Mapping[str, Callable[[float, float], bool]] = {
|
| 71 |
+
"Eq": lambda a, b: a == b,
|
| 72 |
+
"Neq": lambda a, b: a != b,
|
| 73 |
+
"Gt": lambda a, b: a > b,
|
| 74 |
+
"Gte": lambda a, b: a >= b,
|
| 75 |
+
"Lt": lambda a, b: a < b,
|
| 76 |
+
"Lte": lambda a, b: a <= b,
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class FloatUnaryOperation:
|
| 81 |
+
@classmethod
|
| 82 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 83 |
+
return {
|
| 84 |
+
"required": {
|
| 85 |
+
"op": (list(FLOAT_UNARY_OPERATIONS.keys()),),
|
| 86 |
+
"a": DEFAULT_FLOAT,
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
RETURN_TYPES = ("FLOAT",)
|
| 91 |
+
FUNCTION = "op"
|
| 92 |
+
CATEGORY = "math/float"
|
| 93 |
+
|
| 94 |
+
def op(self, op: str, a: float) -> tuple[float]:
|
| 95 |
+
return (FLOAT_UNARY_OPERATIONS[op](a),)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class FloatUnaryCondition:
|
| 99 |
+
@classmethod
|
| 100 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 101 |
+
return {
|
| 102 |
+
"required": {
|
| 103 |
+
"op": (list(FLOAT_UNARY_CONDITIONS.keys()),),
|
| 104 |
+
"a": DEFAULT_FLOAT,
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
RETURN_TYPES = ("BOOLEAN",)
|
| 109 |
+
FUNCTION = "op"
|
| 110 |
+
CATEGORY = "math/float"
|
| 111 |
+
|
| 112 |
+
def op(self, op: str, a: float) -> tuple[bool]:
|
| 113 |
+
return (FLOAT_UNARY_CONDITIONS[op](a),)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class FloatBinaryOperation:
|
| 117 |
+
@classmethod
|
| 118 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 119 |
+
return {
|
| 120 |
+
"required": {
|
| 121 |
+
"op": (list(FLOAT_BINARY_OPERATIONS.keys()),),
|
| 122 |
+
"a": DEFAULT_FLOAT,
|
| 123 |
+
"b": DEFAULT_FLOAT,
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
RETURN_TYPES = ("FLOAT",)
|
| 128 |
+
FUNCTION = "op"
|
| 129 |
+
CATEGORY = "math/float"
|
| 130 |
+
|
| 131 |
+
def op(self, op: str, a: float, b: float) -> tuple[float]:
|
| 132 |
+
return (FLOAT_BINARY_OPERATIONS[op](a, b),)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class FloatBinaryCondition:
|
| 136 |
+
@classmethod
|
| 137 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 138 |
+
return {
|
| 139 |
+
"required": {
|
| 140 |
+
"op": (list(FLOAT_BINARY_CONDITIONS.keys()),),
|
| 141 |
+
"a": DEFAULT_FLOAT,
|
| 142 |
+
"b": DEFAULT_FLOAT,
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
RETURN_TYPES = ("BOOLEAN",)
|
| 147 |
+
FUNCTION = "op"
|
| 148 |
+
CATEGORY = "math/float"
|
| 149 |
+
|
| 150 |
+
def op(self, op: str, a: float, b: float) -> tuple[bool]:
|
| 151 |
+
return (FLOAT_BINARY_CONDITIONS[op](a, b),)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
NODE_CLASS_MAPPINGS = {
|
| 155 |
+
"CM_FloatUnaryOperation": FloatUnaryOperation,
|
| 156 |
+
"CM_FloatUnaryCondition": FloatUnaryCondition,
|
| 157 |
+
"CM_FloatBinaryOperation": FloatBinaryOperation,
|
| 158 |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|
+
from abc import ABC, abstractmethod
|
| 2 |
+
from typing import Any, Mapping, Sequence, Tuple
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
SDXL_SUPPORTED_RESOLUTIONS = [
|
| 6 |
+
(1024, 1024, 1.0),
|
| 7 |
+
(1152, 896, 1.2857142857142858),
|
| 8 |
+
(896, 1152, 0.7777777777777778),
|
| 9 |
+
(1216, 832, 1.4615384615384615),
|
| 10 |
+
(832, 1216, 0.6842105263157895),
|
| 11 |
+
(1344, 768, 1.75),
|
| 12 |
+
(768, 1344, 0.5714285714285714),
|
| 13 |
+
(1536, 640, 2.4),
|
| 14 |
+
(640, 1536, 0.4166666666666667),
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
SDXL_EXTENDED_RESOLUTIONS = [
|
| 18 |
+
(512, 2048, 0.25),
|
| 19 |
+
(512, 1984, 0.26),
|
| 20 |
+
(512, 1920, 0.27),
|
| 21 |
+
(512, 1856, 0.28),
|
| 22 |
+
(576, 1792, 0.32),
|
| 23 |
+
(576, 1728, 0.33),
|
| 24 |
+
(576, 1664, 0.35),
|
| 25 |
+
(640, 1600, 0.4),
|
| 26 |
+
(640, 1536, 0.42),
|
| 27 |
+
(704, 1472, 0.48),
|
| 28 |
+
(704, 1408, 0.5),
|
| 29 |
+
(704, 1344, 0.52),
|
| 30 |
+
(768, 1344, 0.57),
|
| 31 |
+
(768, 1280, 0.6),
|
| 32 |
+
(832, 1216, 0.68),
|
| 33 |
+
(832, 1152, 0.72),
|
| 34 |
+
(896, 1152, 0.78),
|
| 35 |
+
(896, 1088, 0.82),
|
| 36 |
+
(960, 1088, 0.88),
|
| 37 |
+
(960, 1024, 0.94),
|
| 38 |
+
(1024, 1024, 1.0),
|
| 39 |
+
(1024, 960, 1.8),
|
| 40 |
+
(1088, 960, 1.14),
|
| 41 |
+
(1088, 896, 1.22),
|
| 42 |
+
(1152, 896, 1.30),
|
| 43 |
+
(1152, 832, 1.39),
|
| 44 |
+
(1216, 832, 1.47),
|
| 45 |
+
(1280, 768, 1.68),
|
| 46 |
+
(1344, 768, 1.76),
|
| 47 |
+
(1408, 704, 2.0),
|
| 48 |
+
(1472, 704, 2.10),
|
| 49 |
+
(1536, 640, 2.4),
|
| 50 |
+
(1600, 640, 2.5),
|
| 51 |
+
(1664, 576, 2.90),
|
| 52 |
+
(1728, 576, 3.0),
|
| 53 |
+
(1792, 576, 3.12),
|
| 54 |
+
(1856, 512, 3.63),
|
| 55 |
+
(1920, 512, 3.76),
|
| 56 |
+
(1984, 512, 3.89),
|
| 57 |
+
(2048, 512, 4.0),
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class Resolution(ABC):
|
| 62 |
+
@classmethod
|
| 63 |
+
@abstractmethod
|
| 64 |
+
def resolutions(cls) -> Sequence[Tuple[int, int, float]]: ...
|
| 65 |
+
|
| 66 |
+
@classmethod
|
| 67 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 68 |
+
return {
|
| 69 |
+
"required": {
|
| 70 |
+
"resolution": ([f"{res[0]}x{res[1]}" for res in cls.resolutions()],)
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
RETURN_TYPES = ("INT", "INT")
|
| 75 |
+
RETURN_NAMES = ("width", "height")
|
| 76 |
+
FUNCTION = "op"
|
| 77 |
+
CATEGORY = "math/graphics"
|
| 78 |
+
|
| 79 |
+
def op(self, resolution: str) -> tuple[int, int]:
|
| 80 |
+
width, height = resolution.split("x")
|
| 81 |
+
return (int(width), int(height))
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class NearestResolution(ABC):
|
| 85 |
+
@classmethod
|
| 86 |
+
@abstractmethod
|
| 87 |
+
def resolutions(cls) -> Sequence[Tuple[int, int, float]]: ...
|
| 88 |
+
|
| 89 |
+
@classmethod
|
| 90 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 91 |
+
return {"required": {"image": ("IMAGE",)}}
|
| 92 |
+
|
| 93 |
+
RETURN_TYPES = ("INT", "INT")
|
| 94 |
+
RETURN_NAMES = ("width", "height")
|
| 95 |
+
FUNCTION = "op"
|
| 96 |
+
CATEGORY = "math/graphics"
|
| 97 |
+
|
| 98 |
+
def op(self, image) -> tuple[int, int]:
|
| 99 |
+
image_width = image.size()[2]
|
| 100 |
+
image_height = image.size()[1]
|
| 101 |
+
print(f"Input image resolution: {image_width}x{image_height}")
|
| 102 |
+
image_ratio = image_width / image_height
|
| 103 |
+
differences = [
|
| 104 |
+
(abs(image_ratio - resolution[2]), resolution)
|
| 105 |
+
for resolution in self.resolutions()
|
| 106 |
+
]
|
| 107 |
+
smallest = None
|
| 108 |
+
for difference in differences:
|
| 109 |
+
if smallest is None:
|
| 110 |
+
smallest = difference
|
| 111 |
+
else:
|
| 112 |
+
if difference[0] < smallest[0]:
|
| 113 |
+
smallest = difference
|
| 114 |
+
if smallest is not None:
|
| 115 |
+
width = smallest[1][0]
|
| 116 |
+
height = smallest[1][1]
|
| 117 |
+
else:
|
| 118 |
+
width = 1024
|
| 119 |
+
height = 1024
|
| 120 |
+
print(f"Selected resolution: {width}x{height}")
|
| 121 |
+
return (width, height)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class SDXLResolution(Resolution):
|
| 125 |
+
@classmethod
|
| 126 |
+
def resolutions(cls):
|
| 127 |
+
return SDXL_SUPPORTED_RESOLUTIONS
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
class SDXLExtendedResolution(Resolution):
|
| 131 |
+
@classmethod
|
| 132 |
+
def resolutions(cls):
|
| 133 |
+
return SDXL_EXTENDED_RESOLUTIONS
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class NearestSDXLResolution(NearestResolution):
|
| 137 |
+
@classmethod
|
| 138 |
+
def resolutions(cls):
|
| 139 |
+
return SDXL_SUPPORTED_RESOLUTIONS
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
class NearestSDXLExtendedResolution(NearestResolution):
|
| 143 |
+
@classmethod
|
| 144 |
+
def resolutions(cls):
|
| 145 |
+
return SDXL_EXTENDED_RESOLUTIONS
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
NODE_CLASS_MAPPINGS = {
|
| 149 |
+
"CM_SDXLResolution": SDXLResolution,
|
| 150 |
+
"CM_NearestSDXLResolution": NearestSDXLResolution,
|
| 151 |
+
"CM_SDXLExtendedResolution": SDXLExtendedResolution,
|
| 152 |
+
"CM_NearestSDXLExtendedResolution": NearestSDXLExtendedResolution,
|
| 153 |
+
}
|
custom_nodes/ComfyMath/src/comfymath/int.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
from typing import Any, Callable, Mapping
|
| 4 |
+
|
| 5 |
+
DEFAULT_INT = ("INT", {"default": 0})
|
| 6 |
+
|
| 7 |
+
INT_UNARY_OPERATIONS: Mapping[str, Callable[[int], int]] = {
|
| 8 |
+
"Abs": lambda a: abs(a),
|
| 9 |
+
"Neg": lambda a: -a,
|
| 10 |
+
"Inc": lambda a: a + 1,
|
| 11 |
+
"Dec": lambda a: a - 1,
|
| 12 |
+
"Sqr": lambda a: a * a,
|
| 13 |
+
"Cube": lambda a: a * a * a,
|
| 14 |
+
"Not": lambda a: ~a,
|
| 15 |
+
"Factorial": lambda a: math.factorial(a),
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
INT_UNARY_CONDITIONS: Mapping[str, Callable[[int], bool]] = {
|
| 19 |
+
"IsZero": lambda a: a == 0,
|
| 20 |
+
"IsNonZero": lambda a: a != 0,
|
| 21 |
+
"IsPositive": lambda a: a > 0,
|
| 22 |
+
"IsNegative": lambda a: a < 0,
|
| 23 |
+
"IsEven": lambda a: a % 2 == 0,
|
| 24 |
+
"IsOdd": lambda a: a % 2 == 1,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
INT_BINARY_OPERATIONS: Mapping[str, Callable[[int, int], int]] = {
|
| 28 |
+
"Add": lambda a, b: a + b,
|
| 29 |
+
"Sub": lambda a, b: a - b,
|
| 30 |
+
"Mul": lambda a, b: a * b,
|
| 31 |
+
"Div": lambda a, b: a // b,
|
| 32 |
+
"Mod": lambda a, b: a % b,
|
| 33 |
+
"Pow": lambda a, b: a**b,
|
| 34 |
+
"And": lambda a, b: a & b,
|
| 35 |
+
"Nand": lambda a, b: ~a & b,
|
| 36 |
+
"Or": lambda a, b: a | b,
|
| 37 |
+
"Nor": lambda a, b: ~a & b,
|
| 38 |
+
"Xor": lambda a, b: a ^ b,
|
| 39 |
+
"Xnor": lambda a, b: ~a ^ b,
|
| 40 |
+
"Shl": lambda a, b: a << b,
|
| 41 |
+
"Shr": lambda a, b: a >> b,
|
| 42 |
+
"Max": lambda a, b: max(a, b),
|
| 43 |
+
"Min": lambda a, b: min(a, b),
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
INT_BINARY_CONDITIONS: Mapping[str, Callable[[int, int], bool]] = {
|
| 47 |
+
"Eq": lambda a, b: a == b,
|
| 48 |
+
"Neq": lambda a, b: a != b,
|
| 49 |
+
"Gt": lambda a, b: a > b,
|
| 50 |
+
"Lt": lambda a, b: a < b,
|
| 51 |
+
"Geq": lambda a, b: a >= b,
|
| 52 |
+
"Leq": lambda a, b: a <= b,
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class IntUnaryOperation:
|
| 57 |
+
@classmethod
|
| 58 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 59 |
+
return {
|
| 60 |
+
"required": {"op": (list(INT_UNARY_OPERATIONS.keys()),), "a": DEFAULT_INT}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
RETURN_TYPES = ("INT",)
|
| 64 |
+
FUNCTION = "op"
|
| 65 |
+
CATEGORY = "math/int"
|
| 66 |
+
|
| 67 |
+
def op(self, op: str, a: int) -> tuple[int]:
|
| 68 |
+
return (INT_UNARY_OPERATIONS[op](a),)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class IntUnaryCondition:
|
| 72 |
+
@classmethod
|
| 73 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 74 |
+
return {
|
| 75 |
+
"required": {"op": (list(INT_UNARY_CONDITIONS.keys()),), "a": DEFAULT_INT}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
RETURN_TYPES = ("BOOL",)
|
| 79 |
+
FUNCTION = "op"
|
| 80 |
+
CATEGORY = "math/int"
|
| 81 |
+
|
| 82 |
+
def op(self, op: str, a: int) -> tuple[bool]:
|
| 83 |
+
return (INT_UNARY_CONDITIONS[op](a),)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class IntBinaryOperation:
|
| 87 |
+
@classmethod
|
| 88 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 89 |
+
return {
|
| 90 |
+
"required": {
|
| 91 |
+
"op": (list(INT_BINARY_OPERATIONS.keys()),),
|
| 92 |
+
"a": DEFAULT_INT,
|
| 93 |
+
"b": DEFAULT_INT,
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
RETURN_TYPES = ("INT",)
|
| 98 |
+
FUNCTION = "op"
|
| 99 |
+
CATEGORY = "math/int"
|
| 100 |
+
|
| 101 |
+
def op(self, op: str, a: int, b: int) -> tuple[int]:
|
| 102 |
+
return (INT_BINARY_OPERATIONS[op](a, b),)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
class IntBinaryCondition:
|
| 106 |
+
@classmethod
|
| 107 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 108 |
+
return {
|
| 109 |
+
"required": {
|
| 110 |
+
"op": (list(INT_BINARY_CONDITIONS.keys()),),
|
| 111 |
+
"a": DEFAULT_INT,
|
| 112 |
+
"b": DEFAULT_INT,
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
RETURN_TYPES = ("BOOL",)
|
| 117 |
+
FUNCTION = "op"
|
| 118 |
+
CATEGORY = "math/int"
|
| 119 |
+
|
| 120 |
+
def op(self, op: str, a: int, b: int) -> tuple[bool]:
|
| 121 |
+
return (INT_BINARY_CONDITIONS[op](a, b),)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
NODE_CLASS_MAPPINGS = {
|
| 125 |
+
"CM_IntUnaryOperation": IntUnaryOperation,
|
| 126 |
+
"CM_IntUnaryCondition": IntUnaryCondition,
|
| 127 |
+
"CM_IntBinaryOperation": IntBinaryOperation,
|
| 128 |
+
"CM_IntBinaryCondition": IntBinaryCondition,
|
| 129 |
+
}
|
custom_nodes/ComfyMath/src/comfymath/number.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Any, Callable, Mapping
|
| 3 |
+
|
| 4 |
+
from .float import (
|
| 5 |
+
FLOAT_UNARY_OPERATIONS,
|
| 6 |
+
FLOAT_UNARY_CONDITIONS,
|
| 7 |
+
FLOAT_BINARY_OPERATIONS,
|
| 8 |
+
FLOAT_BINARY_CONDITIONS,
|
| 9 |
+
)
|
| 10 |
+
from .types import Number
|
| 11 |
+
|
| 12 |
+
DEFAULT_NUMBER = ("NUMBER", {"default": 0.0})
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class NumberUnaryOperation:
|
| 16 |
+
@classmethod
|
| 17 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 18 |
+
return {
|
| 19 |
+
"required": {
|
| 20 |
+
"op": (list(FLOAT_UNARY_OPERATIONS.keys()),),
|
| 21 |
+
"a": DEFAULT_NUMBER,
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
RETURN_TYPES = ("NUMBER",)
|
| 26 |
+
FUNCTION = "op"
|
| 27 |
+
CATEGORY = "math/number"
|
| 28 |
+
|
| 29 |
+
def op(self, op: str, a: Number) -> tuple[float]:
|
| 30 |
+
return (FLOAT_UNARY_OPERATIONS[op](float(a)),)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class NumberUnaryCondition:
|
| 34 |
+
@classmethod
|
| 35 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 36 |
+
return {
|
| 37 |
+
"required": {
|
| 38 |
+
"op": (list(FLOAT_UNARY_CONDITIONS.keys()),),
|
| 39 |
+
"a": DEFAULT_NUMBER,
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
RETURN_TYPES = ("BOOL",)
|
| 44 |
+
FUNCTION = "op"
|
| 45 |
+
CATEGORY = "math/Number"
|
| 46 |
+
|
| 47 |
+
def op(self, op: str, a: Number) -> tuple[bool]:
|
| 48 |
+
return (FLOAT_UNARY_CONDITIONS[op](float(a)),)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class NumberBinaryOperation:
|
| 52 |
+
@classmethod
|
| 53 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 54 |
+
return {
|
| 55 |
+
"required": {
|
| 56 |
+
"op": (list(FLOAT_BINARY_OPERATIONS.keys()),),
|
| 57 |
+
"a": DEFAULT_NUMBER,
|
| 58 |
+
"b": DEFAULT_NUMBER,
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
RETURN_TYPES = ("NUMBER",)
|
| 63 |
+
FUNCTION = "op"
|
| 64 |
+
CATEGORY = "math/number"
|
| 65 |
+
|
| 66 |
+
def op(self, op: str, a: Number, b: Number) -> tuple[float]:
|
| 67 |
+
return (FLOAT_BINARY_OPERATIONS[op](float(a), float(b)),)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class NumberBinaryCondition:
|
| 71 |
+
@classmethod
|
| 72 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 73 |
+
return {
|
| 74 |
+
"required": {
|
| 75 |
+
"op": (list(FLOAT_BINARY_CONDITIONS.keys()),),
|
| 76 |
+
"a": DEFAULT_NUMBER,
|
| 77 |
+
"b": DEFAULT_NUMBER,
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
RETURN_TYPES = ("BOOL",)
|
| 82 |
+
FUNCTION = "op"
|
| 83 |
+
CATEGORY = "math/float"
|
| 84 |
+
|
| 85 |
+
def op(self, op: str, a: Number, b: Number) -> tuple[bool]:
|
| 86 |
+
return (FLOAT_BINARY_CONDITIONS[op](float(a), float(b)),)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
NODE_CLASS_MAPPINGS = {
|
| 90 |
+
"CM_NumberUnaryOperation": NumberUnaryOperation,
|
| 91 |
+
"CM_NumberUnaryCondition": NumberUnaryCondition,
|
| 92 |
+
"CM_NumberBinaryOperation": NumberBinaryOperation,
|
| 93 |
+
"CM_NumberBinaryCondition": NumberBinaryCondition,
|
| 94 |
+
}
|
custom_nodes/ComfyMath/src/comfymath/py.typed
ADDED
|
File without changes
|
custom_nodes/ComfyMath/src/comfymath/types.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
|
| 3 |
+
if sys.version_info[1] < 10:
|
| 4 |
+
from typing import Tuple, Union
|
| 5 |
+
|
| 6 |
+
Number = Union[int, float]
|
| 7 |
+
Vec2 = Tuple[float, float]
|
| 8 |
+
Vec3 = Tuple[float, float, float]
|
| 9 |
+
Vec4 = Tuple[float, float, float, float]
|
| 10 |
+
else:
|
| 11 |
+
from typing import TypeAlias
|
| 12 |
+
|
| 13 |
+
Number: TypeAlias = int | float
|
| 14 |
+
Vec2: TypeAlias = tuple[float, float]
|
| 15 |
+
Vec3: TypeAlias = tuple[float, float, float]
|
| 16 |
+
Vec4: TypeAlias = tuple[float, float, float, float]
|
custom_nodes/ComfyMath/src/comfymath/vec.py
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy
|
| 2 |
+
|
| 3 |
+
from typing import Any, Callable, Mapping
|
| 4 |
+
|
| 5 |
+
from .types import Vec2, Vec3, Vec4
|
| 6 |
+
|
| 7 |
+
VEC2_ZERO = (0.0, 0.0)
|
| 8 |
+
DEFAULT_VEC2 = ("VEC2", {"default": VEC2_ZERO})
|
| 9 |
+
|
| 10 |
+
VEC3_ZERO = (0.0, 0.0, 0.0)
|
| 11 |
+
DEFAULT_VEC3 = ("VEC3", {"default": VEC3_ZERO})
|
| 12 |
+
|
| 13 |
+
VEC4_ZERO = (0.0, 0.0, 0.0, 0.0)
|
| 14 |
+
DEFAULT_VEC4 = ("VEC4", {"default": VEC4_ZERO})
|
| 15 |
+
|
| 16 |
+
VEC_UNARY_OPERATIONS: Mapping[str, Callable[[numpy.ndarray], numpy.ndarray]] = {
|
| 17 |
+
"Neg": lambda a: -a,
|
| 18 |
+
"Normalize": lambda a: a / numpy.linalg.norm(a),
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
VEC_TO_SCALAR_UNARY_OPERATION: Mapping[str, Callable[[numpy.ndarray], float]] = {
|
| 22 |
+
"Norm": lambda a: numpy.linalg.norm(a).astype(float),
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
VEC_UNARY_CONDITIONS: Mapping[str, Callable[[numpy.ndarray], bool]] = {
|
| 26 |
+
"IsZero": lambda a: not numpy.any(a).astype(bool),
|
| 27 |
+
"IsNotZero": lambda a: numpy.any(a).astype(bool),
|
| 28 |
+
"IsNormalized": lambda a: numpy.allclose(a, a / numpy.linalg.norm(a)),
|
| 29 |
+
"IsNotNormalized": lambda a: not numpy.allclose(a, a / numpy.linalg.norm(a)),
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
VEC_BINARY_OPERATIONS: Mapping[
|
| 33 |
+
str, Callable[[numpy.ndarray, numpy.ndarray], numpy.ndarray]
|
| 34 |
+
] = {
|
| 35 |
+
"Add": lambda a, b: a + b,
|
| 36 |
+
"Sub": lambda a, b: a - b,
|
| 37 |
+
"Cross": lambda a, b: numpy.cross(a, b),
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
VEC_TO_SCALAR_BINARY_OPERATION: Mapping[
|
| 41 |
+
str, Callable[[numpy.ndarray, numpy.ndarray], float]
|
| 42 |
+
] = {
|
| 43 |
+
"Dot": lambda a, b: numpy.dot(a, b),
|
| 44 |
+
"Distance": lambda a, b: numpy.linalg.norm(a - b).astype(float),
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
VEC_BINARY_CONDITIONS: Mapping[str, Callable[[numpy.ndarray, numpy.ndarray], bool]] = {
|
| 48 |
+
"Eq": lambda a, b: numpy.allclose(a, b),
|
| 49 |
+
"Neq": lambda a, b: not numpy.allclose(a, b),
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
VEC_SCALAR_OPERATION: Mapping[str, Callable[[numpy.ndarray, float], numpy.ndarray]] = {
|
| 53 |
+
"Mul": lambda a, b: a * b,
|
| 54 |
+
"Div": lambda a, b: a / b,
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def _vec2_from_numpy(a: numpy.ndarray) -> Vec2:
|
| 59 |
+
return (
|
| 60 |
+
float(a[0]),
|
| 61 |
+
float(a[1]),
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def _vec3_from_numpy(a: numpy.ndarray) -> Vec3:
|
| 66 |
+
return (
|
| 67 |
+
float(a[0]),
|
| 68 |
+
float(a[1]),
|
| 69 |
+
float(a[2]),
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _vec4_from_numpy(a: numpy.ndarray) -> Vec4:
|
| 74 |
+
return (
|
| 75 |
+
float(a[0]),
|
| 76 |
+
float(a[1]),
|
| 77 |
+
float(a[2]),
|
| 78 |
+
float(a[3]),
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class Vec2UnaryOperation:
|
| 83 |
+
@classmethod
|
| 84 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 85 |
+
return {
|
| 86 |
+
"required": {
|
| 87 |
+
"op": (list(VEC_UNARY_OPERATIONS.keys()),),
|
| 88 |
+
"a": DEFAULT_VEC2,
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
RETURN_TYPES = ("VEC2",)
|
| 93 |
+
FUNCTION = "op"
|
| 94 |
+
CATEGORY = "math/vec2"
|
| 95 |
+
|
| 96 |
+
def op(self, op: str, a: Vec2) -> tuple[Vec2]:
|
| 97 |
+
return (_vec2_from_numpy(VEC_UNARY_OPERATIONS[op](numpy.array(a))),)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class Vec2ToScalarUnaryOperation:
|
| 101 |
+
@classmethod
|
| 102 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 103 |
+
return {
|
| 104 |
+
"required": {
|
| 105 |
+
"op": (list(VEC_TO_SCALAR_UNARY_OPERATION.keys()),),
|
| 106 |
+
"a": DEFAULT_VEC2,
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
RETURN_TYPES = ("FLOAT",)
|
| 111 |
+
FUNCTION = "op"
|
| 112 |
+
CATEGORY = "math/vec2"
|
| 113 |
+
|
| 114 |
+
def op(self, op: str, a: Vec2) -> tuple[float]:
|
| 115 |
+
return (VEC_TO_SCALAR_UNARY_OPERATION[op](numpy.array(a)),)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class Vec2UnaryCondition:
|
| 119 |
+
@classmethod
|
| 120 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 121 |
+
return {
|
| 122 |
+
"required": {
|
| 123 |
+
"op": (list(VEC_UNARY_CONDITIONS.keys()),),
|
| 124 |
+
"a": DEFAULT_VEC2,
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
RETURN_TYPES = ("BOOL",)
|
| 129 |
+
FUNCTION = "op"
|
| 130 |
+
CATEGORY = "math/vec2"
|
| 131 |
+
|
| 132 |
+
def op(self, op: str, a: Vec2) -> tuple[bool]:
|
| 133 |
+
return (VEC_UNARY_CONDITIONS[op](numpy.array(a)),)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class Vec2BinaryOperation:
|
| 137 |
+
@classmethod
|
| 138 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 139 |
+
return {
|
| 140 |
+
"required": {
|
| 141 |
+
"op": (list(VEC_BINARY_OPERATIONS.keys()),),
|
| 142 |
+
"a": DEFAULT_VEC2,
|
| 143 |
+
"b": DEFAULT_VEC2,
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
RETURN_TYPES = ("VEC2",)
|
| 148 |
+
FUNCTION = "op"
|
| 149 |
+
CATEGORY = "math/vec2"
|
| 150 |
+
|
| 151 |
+
def op(self, op: str, a: Vec2, b: Vec2) -> tuple[Vec2]:
|
| 152 |
+
return (
|
| 153 |
+
_vec2_from_numpy(VEC_BINARY_OPERATIONS[op](numpy.array(a), numpy.array(b))),
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
class Vec2ToScalarBinaryOperation:
|
| 158 |
+
@classmethod
|
| 159 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 160 |
+
return {
|
| 161 |
+
"required": {
|
| 162 |
+
"op": (list(VEC_TO_SCALAR_BINARY_OPERATION.keys()),),
|
| 163 |
+
"a": DEFAULT_VEC2,
|
| 164 |
+
"b": DEFAULT_VEC2,
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
RETURN_TYPES = ("FLOAT",)
|
| 169 |
+
FUNCTION = "op"
|
| 170 |
+
CATEGORY = "math/vec2"
|
| 171 |
+
|
| 172 |
+
def op(self, op: str, a: Vec2, b: Vec2) -> tuple[float]:
|
| 173 |
+
return (VEC_TO_SCALAR_BINARY_OPERATION[op](numpy.array(a), numpy.array(b)),)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class Vec2BinaryCondition:
|
| 177 |
+
@classmethod
|
| 178 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 179 |
+
return {
|
| 180 |
+
"required": {
|
| 181 |
+
"op": (list(VEC_BINARY_CONDITIONS.keys()),),
|
| 182 |
+
"a": DEFAULT_VEC2,
|
| 183 |
+
"b": DEFAULT_VEC2,
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
RETURN_TYPES = ("BOOL",)
|
| 188 |
+
FUNCTION = "op"
|
| 189 |
+
CATEGORY = "math/vec2"
|
| 190 |
+
|
| 191 |
+
def op(self, op: str, a: Vec2, b: Vec2) -> tuple[bool]:
|
| 192 |
+
return (VEC_BINARY_CONDITIONS[op](numpy.array(a), numpy.array(b)),)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
class Vec2ScalarOperation:
|
| 196 |
+
@classmethod
|
| 197 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 198 |
+
return {
|
| 199 |
+
"required": {
|
| 200 |
+
"op": (list(VEC_SCALAR_OPERATION.keys()),),
|
| 201 |
+
"a": DEFAULT_VEC2,
|
| 202 |
+
"b": ("FLOAT",),
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
RETURN_TYPES = ("VEC2",)
|
| 207 |
+
FUNCTION = "op"
|
| 208 |
+
CATEGORY = "math/vec2"
|
| 209 |
+
|
| 210 |
+
def op(self, op: str, a: Vec2, b: float) -> tuple[Vec2]:
|
| 211 |
+
return (_vec2_from_numpy(VEC_SCALAR_OPERATION[op](numpy.array(a), b)),)
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
class Vec3UnaryOperation:
|
| 215 |
+
@classmethod
|
| 216 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 217 |
+
return {
|
| 218 |
+
"required": {
|
| 219 |
+
"op": (list(VEC_UNARY_OPERATIONS.keys()),),
|
| 220 |
+
"a": DEFAULT_VEC3,
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
RETURN_TYPES = ("VEC3",)
|
| 225 |
+
FUNCTION = "op"
|
| 226 |
+
CATEGORY = "math/vec3"
|
| 227 |
+
|
| 228 |
+
def op(self, op: str, a: Vec3) -> tuple[Vec3]:
|
| 229 |
+
return (_vec3_from_numpy(VEC_UNARY_OPERATIONS[op](numpy.array(a))),)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
class Vec3ToScalarUnaryOperation:
|
| 233 |
+
@classmethod
|
| 234 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 235 |
+
return {
|
| 236 |
+
"required": {
|
| 237 |
+
"op": (list(VEC_TO_SCALAR_UNARY_OPERATION.keys()),),
|
| 238 |
+
"a": DEFAULT_VEC3,
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
RETURN_TYPES = ("FLOAT",)
|
| 243 |
+
FUNCTION = "op"
|
| 244 |
+
CATEGORY = "math/vec3"
|
| 245 |
+
|
| 246 |
+
def op(self, op: str, a: Vec3) -> tuple[float]:
|
| 247 |
+
return (VEC_TO_SCALAR_UNARY_OPERATION[op](numpy.array(a)),)
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
class Vec3UnaryCondition:
|
| 251 |
+
@classmethod
|
| 252 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 253 |
+
return {
|
| 254 |
+
"required": {
|
| 255 |
+
"op": (list(VEC_UNARY_CONDITIONS.keys()),),
|
| 256 |
+
"a": DEFAULT_VEC3,
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
RETURN_TYPES = ("BOOL",)
|
| 261 |
+
FUNCTION = "op"
|
| 262 |
+
CATEGORY = "math/vec3"
|
| 263 |
+
|
| 264 |
+
def op(self, op: str, a: Vec3) -> tuple[bool]:
|
| 265 |
+
return (VEC_UNARY_CONDITIONS[op](numpy.array(a)),)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
class Vec3BinaryOperation:
|
| 269 |
+
@classmethod
|
| 270 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 271 |
+
return {
|
| 272 |
+
"required": {
|
| 273 |
+
"op": (list(VEC_BINARY_OPERATIONS.keys()),),
|
| 274 |
+
"a": DEFAULT_VEC3,
|
| 275 |
+
"b": DEFAULT_VEC3,
|
| 276 |
+
}
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
RETURN_TYPES = ("VEC3",)
|
| 280 |
+
FUNCTION = "op"
|
| 281 |
+
CATEGORY = "math/vec3"
|
| 282 |
+
|
| 283 |
+
def op(self, op: str, a: Vec3, b: Vec3) -> tuple[Vec3]:
|
| 284 |
+
return (
|
| 285 |
+
_vec3_from_numpy(VEC_BINARY_OPERATIONS[op](numpy.array(a), numpy.array(b))),
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
class Vec3ToScalarBinaryOperation:
|
| 290 |
+
@classmethod
|
| 291 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 292 |
+
return {
|
| 293 |
+
"required": {
|
| 294 |
+
"op": (list(VEC_TO_SCALAR_BINARY_OPERATION.keys()),),
|
| 295 |
+
"a": DEFAULT_VEC3,
|
| 296 |
+
"b": DEFAULT_VEC3,
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
RETURN_TYPES = ("FLOAT",)
|
| 301 |
+
FUNCTION = "op"
|
| 302 |
+
CATEGORY = "math/vec3"
|
| 303 |
+
|
| 304 |
+
def op(self, op: str, a: Vec3, b: Vec3) -> tuple[float]:
|
| 305 |
+
return (VEC_TO_SCALAR_BINARY_OPERATION[op](numpy.array(a), numpy.array(b)),)
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
class Vec3BinaryCondition:
|
| 309 |
+
@classmethod
|
| 310 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 311 |
+
return {
|
| 312 |
+
"required": {
|
| 313 |
+
"op": (list(VEC_BINARY_CONDITIONS.keys()),),
|
| 314 |
+
"a": DEFAULT_VEC3,
|
| 315 |
+
"b": DEFAULT_VEC3,
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
RETURN_TYPES = ("BOOL",)
|
| 320 |
+
FUNCTION = "op"
|
| 321 |
+
CATEGORY = "math/vec3"
|
| 322 |
+
|
| 323 |
+
def op(self, op: str, a: Vec3, b: Vec3) -> tuple[bool]:
|
| 324 |
+
return (VEC_BINARY_CONDITIONS[op](numpy.array(a), numpy.array(b)),)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
class Vec3ScalarOperation:
|
| 328 |
+
@classmethod
|
| 329 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 330 |
+
return {
|
| 331 |
+
"required": {
|
| 332 |
+
"op": (list(VEC_SCALAR_OPERATION.keys()),),
|
| 333 |
+
"a": DEFAULT_VEC3,
|
| 334 |
+
"b": ("FLOAT",),
|
| 335 |
+
}
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
RETURN_TYPES = ("VEC3",)
|
| 339 |
+
FUNCTION = "op"
|
| 340 |
+
CATEGORY = "math/vec3"
|
| 341 |
+
|
| 342 |
+
def op(self, op: str, a: Vec3, b: float) -> tuple[Vec3]:
|
| 343 |
+
return (_vec3_from_numpy(VEC_SCALAR_OPERATION[op](numpy.array(a), b)),)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
class Vec4UnaryOperation:
|
| 347 |
+
@classmethod
|
| 348 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 349 |
+
return {
|
| 350 |
+
"required": {
|
| 351 |
+
"op": (list(VEC_UNARY_OPERATIONS.keys()),),
|
| 352 |
+
"a": DEFAULT_VEC4,
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
RETURN_TYPES = ("VEC4",)
|
| 357 |
+
FUNCTION = "op"
|
| 358 |
+
CATEGORY = "math/vec4"
|
| 359 |
+
|
| 360 |
+
def op(self, op: str, a: Vec4) -> tuple[Vec4]:
|
| 361 |
+
return (_vec4_from_numpy(VEC_UNARY_OPERATIONS[op](numpy.array(a))),)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
class Vec4ToScalarUnaryOperation:
|
| 365 |
+
@classmethod
|
| 366 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 367 |
+
return {
|
| 368 |
+
"required": {
|
| 369 |
+
"op": (list(VEC_TO_SCALAR_UNARY_OPERATION.keys()),),
|
| 370 |
+
"a": DEFAULT_VEC4,
|
| 371 |
+
}
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
RETURN_TYPES = ("FLOAT",)
|
| 375 |
+
FUNCTION = "op"
|
| 376 |
+
CATEGORY = "math/vec4"
|
| 377 |
+
|
| 378 |
+
def op(self, op: str, a: Vec4) -> tuple[float]:
|
| 379 |
+
return (VEC_TO_SCALAR_UNARY_OPERATION[op](numpy.array(a)),)
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
class Vec4UnaryCondition:
|
| 383 |
+
@classmethod
|
| 384 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 385 |
+
return {
|
| 386 |
+
"required": {
|
| 387 |
+
"op": (list(VEC_UNARY_CONDITIONS.keys()),),
|
| 388 |
+
"a": DEFAULT_VEC4,
|
| 389 |
+
}
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
RETURN_TYPES = ("BOOL",)
|
| 393 |
+
FUNCTION = "op"
|
| 394 |
+
CATEGORY = "math/vec4"
|
| 395 |
+
|
| 396 |
+
def op(self, op: str, a: Vec4) -> tuple[bool]:
|
| 397 |
+
return (VEC_UNARY_CONDITIONS[op](numpy.array(a)),)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
class Vec4BinaryOperation:
|
| 401 |
+
@classmethod
|
| 402 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 403 |
+
return {
|
| 404 |
+
"required": {
|
| 405 |
+
"op": (list(VEC_BINARY_OPERATIONS.keys()),),
|
| 406 |
+
"a": DEFAULT_VEC4,
|
| 407 |
+
"b": DEFAULT_VEC4,
|
| 408 |
+
}
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
RETURN_TYPES = ("VEC4",)
|
| 412 |
+
FUNCTION = "op"
|
| 413 |
+
CATEGORY = "math/vec4"
|
| 414 |
+
|
| 415 |
+
def op(self, op: str, a: Vec4, b: Vec4) -> tuple[Vec4]:
|
| 416 |
+
return (
|
| 417 |
+
_vec4_from_numpy(VEC_BINARY_OPERATIONS[op](numpy.array(a), numpy.array(b))),
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
class Vec4ToScalarBinaryOperation:
|
| 422 |
+
@classmethod
|
| 423 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 424 |
+
return {
|
| 425 |
+
"required": {
|
| 426 |
+
"op": (list(VEC_TO_SCALAR_BINARY_OPERATION.keys()),),
|
| 427 |
+
"a": DEFAULT_VEC4,
|
| 428 |
+
"b": DEFAULT_VEC4,
|
| 429 |
+
}
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
RETURN_TYPES = ("FLOAT",)
|
| 433 |
+
FUNCTION = "op"
|
| 434 |
+
CATEGORY = "math/vec4"
|
| 435 |
+
|
| 436 |
+
def op(self, op: str, a: Vec4, b: Vec4) -> tuple[float]:
|
| 437 |
+
return (VEC_TO_SCALAR_BINARY_OPERATION[op](numpy.array(a), numpy.array(b)),)
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
class Vec4BinaryCondition:
|
| 441 |
+
@classmethod
|
| 442 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 443 |
+
return {
|
| 444 |
+
"required": {
|
| 445 |
+
"op": (list(VEC_BINARY_CONDITIONS.keys()),),
|
| 446 |
+
"a": DEFAULT_VEC4,
|
| 447 |
+
"b": DEFAULT_VEC4,
|
| 448 |
+
}
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
RETURN_TYPES = ("BOOL",)
|
| 452 |
+
FUNCTION = "op"
|
| 453 |
+
CATEGORY = "math/vec4"
|
| 454 |
+
|
| 455 |
+
def op(self, op: str, a: Vec4, b: Vec4) -> tuple[bool]:
|
| 456 |
+
return (VEC_BINARY_CONDITIONS[op](numpy.array(a), numpy.array(b)),)
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
class Vec4ScalarOperation:
|
| 460 |
+
@classmethod
|
| 461 |
+
def INPUT_TYPES(cls) -> Mapping[str, Any]:
|
| 462 |
+
return {
|
| 463 |
+
"required": {
|
| 464 |
+
"op": (list(VEC_SCALAR_OPERATION.keys()),),
|
| 465 |
+
"a": DEFAULT_VEC4,
|
| 466 |
+
"b": ("FLOAT",),
|
| 467 |
+
}
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
RETURN_TYPES = ("VEC4",)
|
| 471 |
+
FUNCTION = "op"
|
| 472 |
+
CATEGORY = "math/vec4"
|
| 473 |
+
|
| 474 |
+
def op(self, op: str, a: Vec4, b: float) -> tuple[Vec4]:
|
| 475 |
+
return (_vec4_from_numpy(VEC_SCALAR_OPERATION[op](numpy.array(a), b)),)
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
NODE_CLASS_MAPPINGS = {
|
| 479 |
+
"CM_Vec2UnaryOperation": Vec2UnaryOperation,
|
| 480 |
+
"CM_Vec2UnaryCondition": Vec2UnaryCondition,
|
| 481 |
+
"CM_Vec2ToScalarUnaryOperation": Vec2ToScalarUnaryOperation,
|
| 482 |
+
"CM_Vec2BinaryOperation": Vec2BinaryOperation,
|
| 483 |
+
"CM_Vec2BinaryCondition": Vec2BinaryCondition,
|
| 484 |
+
"CM_Vec2ToScalarBinaryOperation": Vec2ToScalarBinaryOperation,
|
| 485 |
+
"CM_Vec2ScalarOperation": Vec2ScalarOperation,
|
| 486 |
+
"CM_Vec3UnaryOperation": Vec3UnaryOperation,
|
| 487 |
+
"CM_Vec3UnaryCondition": Vec3UnaryCondition,
|
| 488 |
+
"CM_Vec3ToScalarUnaryOperation": Vec3ToScalarUnaryOperation,
|
| 489 |
+
"CM_Vec3BinaryOperation": Vec3BinaryOperation,
|
| 490 |
+
"CM_Vec3BinaryCondition": Vec3BinaryCondition,
|
| 491 |
+
"CM_Vec3ToScalarBinaryOperation": Vec3ToScalarBinaryOperation,
|
| 492 |
+
"CM_Vec3ScalarOperation": Vec3ScalarOperation,
|
| 493 |
+
"CM_Vec4UnaryOperation": Vec4UnaryOperation,
|
| 494 |
+
"CM_Vec4UnaryCondition": Vec4UnaryCondition,
|
| 495 |
+
"CM_Vec4ToScalarUnaryOperation": Vec4ToScalarUnaryOperation,
|
| 496 |
+
"CM_Vec4BinaryOperation": Vec4BinaryOperation,
|
| 497 |
+
"CM_Vec4BinaryCondition": Vec4BinaryCondition,
|
| 498 |
+
"CM_Vec4ToScalarBinaryOperation": Vec4ToScalarBinaryOperation,
|
| 499 |
+
"CM_Vec4ScalarOperation": Vec4ScalarOperation,
|
| 500 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/.github/workflows/publish.yml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Publish to Comfy registry
|
| 2 |
+
on:
|
| 3 |
+
workflow_dispatch:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
paths:
|
| 8 |
+
- "pyproject.toml"
|
| 9 |
+
|
| 10 |
+
permissions:
|
| 11 |
+
issues: write
|
| 12 |
+
|
| 13 |
+
jobs:
|
| 14 |
+
publish-node:
|
| 15 |
+
name: Publish Custom Node to registry
|
| 16 |
+
runs-on: ubuntu-latest
|
| 17 |
+
if: ${{ github.repository_owner == 'pythongosssss' }}
|
| 18 |
+
steps:
|
| 19 |
+
- name: Check out code
|
| 20 |
+
uses: actions/checkout@v4
|
| 21 |
+
- name: Publish Custom Node
|
| 22 |
+
uses: Comfy-Org/publish-node-action@v1
|
| 23 |
+
with:
|
| 24 |
+
## Add your own personal access token to your Github Repository secrets and reference it here.
|
| 25 |
+
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
|
custom_nodes/ComfyUI-Custom-Scripts/.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
pysssss.json
|
| 3 |
+
user/autocomplete.txt
|
| 4 |
+
web/js/assets/favicon.user.ico
|
| 5 |
+
web/js/assets/favicon-active.user.ico
|
custom_nodes/ComfyUI-Custom-Scripts/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2023 pythongosssss
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
custom_nodes/ComfyUI-Custom-Scripts/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ComfyUI-Custom-Scripts
|
| 2 |
+
|
| 3 |
+
### ⚠️ While these extensions work for the most part, i'm very busy at the moment and so unable to keep on top of everything here, thanks for your patience!
|
| 4 |
+
|
| 5 |
+
# Installation
|
| 6 |
+
|
| 7 |
+
1. Clone the repository:
|
| 8 |
+
`git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts.git`
|
| 9 |
+
to your ComfyUI `custom_nodes` directory
|
| 10 |
+
|
| 11 |
+
The script will then automatically install all custom scripts and nodes.
|
| 12 |
+
It will attempt to use symlinks and junctions to prevent having to copy files and keep them up to date.
|
| 13 |
+
|
| 14 |
+
- For uninstallation:
|
| 15 |
+
- Delete the cloned repo in `custom_nodes`
|
| 16 |
+
- Ensure `web/extensions/pysssss/CustomScripts` has also been removed
|
| 17 |
+
|
| 18 |
+
# Update
|
| 19 |
+
1. Navigate to the cloned repo e.g. `custom_nodes/ComfyUI-Custom-Scripts`
|
| 20 |
+
2. `git pull`
|
| 21 |
+
|
| 22 |
+
# Features
|
| 23 |
+
|
| 24 |
+
## Autocomplete
|
| 25 |
+

|
| 26 |
+
Provides embedding and custom word autocomplete. You can view embedding details by clicking on the info icon on the list.
|
| 27 |
+
Define your list of custom words via the settings.
|
| 28 |
+

|
| 29 |
+
You can quickly default to danbooru tags using the Load button, or load/manage other custom word lists.
|
| 30 |
+

|
| 31 |
+
|
| 32 |
+
## Auto Arrange Graph
|
| 33 |
+

|
| 34 |
+
Adds a menu option to auto arrange the graph in order of execution, this makes very wide graphs!
|
| 35 |
+
|
| 36 |
+
## Always Snap to Grid
|
| 37 |
+

|
| 38 |
+
Adds a setting to make moving nodes always snap to grid.
|
| 39 |
+
|
| 40 |
+
## [Testing] "Better" Loader Lists
|
| 41 |
+

|
| 42 |
+
Adds custom Lora and Checkpoint loader nodes, these have the ability to show preview images, just place a png or jpg next to the file and it'll display in the list on hover (e.g. sdxl.safetensors and sdxl.png).
|
| 43 |
+
Optionally enable subfolders via the settings:
|
| 44 |
+

|
| 45 |
+
Adds an "examples" widget to load sample prompts, triggerwords, etc:
|
| 46 |
+

|
| 47 |
+
These should be stored in a folder matching the name of the model, e.g. if it is `loras/add_detail.safetensors` put your files in as `loras/add_detail/*.txt`
|
| 48 |
+
To quickly save a generated image as the preview to use for the model, you can right click on an image on a node, and select Save as Preview and choose the model to save the preview for:
|
| 49 |
+

|
| 50 |
+
|
| 51 |
+
## Checkpoint/LoRA/Embedding Info
|
| 52 |
+

|
| 53 |
+

|
| 54 |
+
Adds "View Info" menu option to view details about the selected LoRA or Checkpoint. To view embedding details, click the info button when using embedding autocomplete.
|
| 55 |
+
|
| 56 |
+
## Constrain Image
|
| 57 |
+
Adds a node for resizing an image to a max & min size optionally cropping if required.
|
| 58 |
+
|
| 59 |
+
## Custom Colors
|
| 60 |
+

|
| 61 |
+
Adds a custom color picker to nodes & groups
|
| 62 |
+
|
| 63 |
+
## Favicon Status
|
| 64 |
+

|
| 65 |
+

|
| 66 |
+
Adds a favicon and title to the window, favicon changes color while generating and the window title includes the number of prompts in the queue
|
| 67 |
+
|
| 68 |
+
## Image Feed
|
| 69 |
+

|
| 70 |
+
Adds a panel showing images that have been generated in the current session, you can control the direction that images are added and the position of the panel via the ComfyUI settings screen and the size of the panel and the images via the sliders at the top of the panel.
|
| 71 |
+

|
| 72 |
+
|
| 73 |
+
## KSampler (Advanced) denoise helper
|
| 74 |
+
Provides a simple method to set custom denoise on the advanced sampler
|
| 75 |
+

|
| 76 |
+

|
| 77 |
+
|
| 78 |
+
## Math Expression
|
| 79 |
+
Allows for evaluating complex expressions using values from the graph. You can input `INT`, `FLOAT`, `IMAGE` and `LATENT` values.
|
| 80 |
+

|
| 81 |
+
Other nodes values can be referenced via the `Node name for S&R` via the `Properties` menu item on a node, or the node title.
|
| 82 |
+
Supported operators: `+ - * /` (basic ops) `//` (floor division) `**` (power) `^` (xor) `%` (mod)
|
| 83 |
+
Supported functions `floor(num, dp?)` `floor(num)` `ceil(num)` `randomint(min,max)`
|
| 84 |
+
If using a `LATENT` or `IMAGE` you can get the dimensions using `a.width` or `a.height` where `a` is the input name.
|
| 85 |
+
|
| 86 |
+
## Node Finder
|
| 87 |
+

|
| 88 |
+
Adds a menu item for following/jumping to the executing node, and a menu to quickly go to a node of a specific type.
|
| 89 |
+
|
| 90 |
+
## Preset Text
|
| 91 |
+

|
| 92 |
+
Adds a node that lets you save and use text presets (e.g. for your 'normal' negatives)
|
| 93 |
+
|
| 94 |
+
## Quick Nodes
|
| 95 |
+

|
| 96 |
+
Adds various menu items to some nodes for quickly setting up common parts of graphs
|
| 97 |
+
|
| 98 |
+
## Play Sound
|
| 99 |
+

|
| 100 |
+
Plays a sound when the node is executed, either after each prompt or only when the queue is empty for queuing multiple prompts.
|
| 101 |
+
You can customize the sound by replacing the mp3 file `ComfyUI/custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/notify.mp3`
|
| 102 |
+
|
| 103 |
+
## System Notification
|
| 104 |
+

|
| 105 |
+

|
| 106 |
+
|
| 107 |
+
Sends a system notification via the browser when the node is executed, either after each prompt or only when the queue is empty for queuing multiple prompts.
|
| 108 |
+
|
| 109 |
+
## [WIP] Repeater
|
| 110 |
+

|
| 111 |
+
Node allows you to either create a list of N repeats of the input node, or create N outputs from the input node.
|
| 112 |
+
You can optionally decide if you want to reuse the input node, or create a new instance each time (e.g. a Checkpoint Loader would want to be re-used, but a random number would want to be unique)
|
| 113 |
+
TODO: Type safety on the wildcard outputs to require match with input
|
| 114 |
+
|
| 115 |
+
## Show Text
|
| 116 |
+

|
| 117 |
+
Takes input from a node that produces a string and displays it, useful for things like interrogator, prompt generators, etc.
|
| 118 |
+
|
| 119 |
+
## Show Image on Menu
|
| 120 |
+

|
| 121 |
+
Shows the current generating image on the menu at the bottom, you can disable this via the settings menu.
|
| 122 |
+
|
| 123 |
+
## String Function
|
| 124 |
+

|
| 125 |
+
Supports appending and replacing text
|
| 126 |
+
`tidy_tags` will add commas between parts when in `append` mode.
|
| 127 |
+
`replace` mode supports regex replace by using `/your regex here/` and you can reference capturing groups using `\number` e.g. `\1`
|
| 128 |
+
|
| 129 |
+
## Touch Support
|
| 130 |
+
Provides basic support for touch screen devices, its not perfect but better than nothing
|
| 131 |
+
|
| 132 |
+
## Widget Defaults
|
| 133 |
+

|
| 134 |
+
Allows you to specify default values for widgets when adding new nodes, the values are configured via the settings menu
|
| 135 |
+

|
| 136 |
+
|
| 137 |
+
## Workflows
|
| 138 |
+
Adds options to the menu for saving + loading workflows:
|
| 139 |
+

|
| 140 |
+
|
| 141 |
+
## Workflow Images
|
| 142 |
+

|
| 143 |
+
Adds menu options for importing/exporting the graph as SVG and PNG showing a view of the nodes
|
| 144 |
+
|
| 145 |
+
## (Testing) Reroute Primitive
|
| 146 |
+
 Provides a node that allows rerouting primitives.
|
| 147 |
+
The node can also be collapsed to a single point that you can drag around.
|
| 148 |
+

|
| 149 |
+
Warning: Don't use normal reroutes or primitives with these nodes, it isn't tested and this node replaces their functionality.
|
| 150 |
+
|
| 151 |
+
<br>
|
| 152 |
+
<br>
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
## WD14 Tagger
|
| 156 |
+
Moved to: https://github.com/pythongosssss/ComfyUI-WD14-Tagger
|
| 157 |
+
|
| 158 |
+
## ~~Lock Nodes & Groups~~
|
| 159 |
+
This is now a standard feature of ComfyUI
|
| 160 |
+
~~Adds a lock option to nodes & groups that prevents you from moving them until unlocked~~
|
custom_nodes/ComfyUI-Custom-Scripts/__init__.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib.util
|
| 2 |
+
import glob
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
from .pysssss import init, get_ext_dir
|
| 6 |
+
|
| 7 |
+
NODE_CLASS_MAPPINGS = {}
|
| 8 |
+
NODE_DISPLAY_NAME_MAPPINGS = {}
|
| 9 |
+
|
| 10 |
+
if init():
|
| 11 |
+
py = get_ext_dir("py")
|
| 12 |
+
files = glob.glob(os.path.join(py, "*.py"), recursive=False)
|
| 13 |
+
for file in files:
|
| 14 |
+
name = os.path.splitext(file)[0]
|
| 15 |
+
spec = importlib.util.spec_from_file_location(name, file)
|
| 16 |
+
module = importlib.util.module_from_spec(spec)
|
| 17 |
+
sys.modules[name] = module
|
| 18 |
+
spec.loader.exec_module(module)
|
| 19 |
+
if hasattr(module, "NODE_CLASS_MAPPINGS") and getattr(module, "NODE_CLASS_MAPPINGS") is not None:
|
| 20 |
+
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
|
| 21 |
+
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(module, "NODE_DISPLAY_NAME_MAPPINGS") is not None:
|
| 22 |
+
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS)
|
| 23 |
+
|
| 24 |
+
WEB_DIRECTORY = "./web"
|
| 25 |
+
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]
|
custom_nodes/ComfyUI-Custom-Scripts/py/autocomplete.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from server import PromptServer
|
| 2 |
+
from aiohttp import web
|
| 3 |
+
import os
|
| 4 |
+
import folder_paths
|
| 5 |
+
|
| 6 |
+
dir = os.path.abspath(os.path.join(__file__, "../../user"))
|
| 7 |
+
if not os.path.exists(dir):
|
| 8 |
+
os.mkdir(dir)
|
| 9 |
+
file = os.path.join(dir, "autocomplete.txt")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@PromptServer.instance.routes.get("/pysssss/autocomplete")
|
| 13 |
+
async def get_autocomplete(request):
|
| 14 |
+
if os.path.isfile(file):
|
| 15 |
+
return web.FileResponse(file)
|
| 16 |
+
return web.Response(status=404)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@PromptServer.instance.routes.post("/pysssss/autocomplete")
|
| 20 |
+
async def update_autocomplete(request):
|
| 21 |
+
with open(file, "w", encoding="utf-8") as f:
|
| 22 |
+
f.write(await request.text())
|
| 23 |
+
return web.Response(status=200)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@PromptServer.instance.routes.get("/pysssss/loras")
|
| 27 |
+
async def get_loras(request):
|
| 28 |
+
loras = folder_paths.get_filename_list("loras")
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import glob
|
| 2 |
+
import os
|
| 3 |
+
from nodes import LoraLoader, CheckpointLoaderSimple
|
| 4 |
+
import folder_paths
|
| 5 |
+
from server import PromptServer
|
| 6 |
+
from folder_paths import get_directory_by_type
|
| 7 |
+
from aiohttp import web
|
| 8 |
+
import shutil
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@PromptServer.instance.routes.get("/pysssss/view/{name}")
|
| 12 |
+
async def view(request):
|
| 13 |
+
name = request.match_info["name"]
|
| 14 |
+
pos = name.index("/")
|
| 15 |
+
type = name[0:pos]
|
| 16 |
+
name = name[pos+1:]
|
| 17 |
+
|
| 18 |
+
image_path = folder_paths.get_full_path(
|
| 19 |
+
type, name)
|
| 20 |
+
if not image_path:
|
| 21 |
+
return web.Response(status=404)
|
| 22 |
+
|
| 23 |
+
filename = os.path.basename(image_path)
|
| 24 |
+
return web.FileResponse(image_path, headers={"Content-Disposition": f"filename=\"{filename}\""})
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@PromptServer.instance.routes.post("/pysssss/save/{name}")
|
| 28 |
+
async def save_preview(request):
|
| 29 |
+
name = request.match_info["name"]
|
| 30 |
+
pos = name.index("/")
|
| 31 |
+
type = name[0:pos]
|
| 32 |
+
name = name[pos+1:]
|
| 33 |
+
|
| 34 |
+
body = await request.json()
|
| 35 |
+
|
| 36 |
+
dir = get_directory_by_type(body.get("type", "output"))
|
| 37 |
+
subfolder = body.get("subfolder", "")
|
| 38 |
+
full_output_folder = os.path.join(dir, os.path.normpath(subfolder))
|
| 39 |
+
|
| 40 |
+
filepath = os.path.join(full_output_folder, body.get("filename", ""))
|
| 41 |
+
|
| 42 |
+
if os.path.commonpath((dir, os.path.abspath(filepath))) != dir:
|
| 43 |
+
return web.Response(status=400)
|
| 44 |
+
|
| 45 |
+
image_path = folder_paths.get_full_path(type, name)
|
| 46 |
+
image_path = os.path.splitext(
|
| 47 |
+
image_path)[0] + os.path.splitext(filepath)[1]
|
| 48 |
+
|
| 49 |
+
shutil.copyfile(filepath, image_path)
|
| 50 |
+
|
| 51 |
+
return web.json_response({
|
| 52 |
+
"image": type + "/" + os.path.basename(image_path)
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@PromptServer.instance.routes.get("/pysssss/examples/{name}")
|
| 57 |
+
async def get_examples(request):
|
| 58 |
+
name = request.match_info["name"]
|
| 59 |
+
pos = name.index("/")
|
| 60 |
+
type = name[0:pos]
|
| 61 |
+
name = name[pos+1:]
|
| 62 |
+
|
| 63 |
+
file_path = folder_paths.get_full_path(
|
| 64 |
+
type, name)
|
| 65 |
+
if not file_path:
|
| 66 |
+
return web.Response(status=404)
|
| 67 |
+
|
| 68 |
+
file_path_no_ext = os.path.splitext(file_path)[0]
|
| 69 |
+
examples = []
|
| 70 |
+
|
| 71 |
+
if os.path.isdir(file_path_no_ext):
|
| 72 |
+
examples += sorted(map(lambda t: os.path.relpath(t, file_path_no_ext),
|
| 73 |
+
glob.glob(file_path_no_ext + "/*.txt")))
|
| 74 |
+
|
| 75 |
+
if os.path.isfile(file_path_no_ext + ".txt"):
|
| 76 |
+
examples += ["notes"]
|
| 77 |
+
|
| 78 |
+
return web.json_response(examples)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@PromptServer.instance.routes.post("/pysssss/examples/{name}")
|
| 82 |
+
async def save_example(request):
|
| 83 |
+
name = request.match_info["name"]
|
| 84 |
+
pos = name.index("/")
|
| 85 |
+
type = name[0:pos]
|
| 86 |
+
name = name[pos+1:]
|
| 87 |
+
body = await request.json()
|
| 88 |
+
example_name = body["name"]
|
| 89 |
+
example = body["example"]
|
| 90 |
+
|
| 91 |
+
file_path = folder_paths.get_full_path(
|
| 92 |
+
type, name)
|
| 93 |
+
if not file_path:
|
| 94 |
+
return web.Response(status=404)
|
| 95 |
+
|
| 96 |
+
if not example_name.endswith(".txt"):
|
| 97 |
+
example_name += ".txt"
|
| 98 |
+
|
| 99 |
+
file_path_no_ext = os.path.splitext(file_path)[0]
|
| 100 |
+
example_file = os.path.join(file_path_no_ext, example_name)
|
| 101 |
+
if not os.path.exists(file_path_no_ext):
|
| 102 |
+
os.mkdir(file_path_no_ext)
|
| 103 |
+
with open(example_file, 'w', encoding='utf8') as f:
|
| 104 |
+
f.write(example)
|
| 105 |
+
|
| 106 |
+
return web.Response(status=201)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@PromptServer.instance.routes.get("/pysssss/images/{type}")
|
| 110 |
+
async def get_images(request):
|
| 111 |
+
type = request.match_info["type"]
|
| 112 |
+
names = folder_paths.get_filename_list(type)
|
| 113 |
+
|
| 114 |
+
images = {}
|
| 115 |
+
for item_name in names:
|
| 116 |
+
file_name = os.path.splitext(item_name)[0]
|
| 117 |
+
file_path = folder_paths.get_full_path(type, item_name)
|
| 118 |
+
|
| 119 |
+
if file_path is None:
|
| 120 |
+
continue
|
| 121 |
+
|
| 122 |
+
file_path_no_ext = os.path.splitext(file_path)[0]
|
| 123 |
+
|
| 124 |
+
for ext in ["png", "jpg", "jpeg", "preview.png", "preview.jpeg"]:
|
| 125 |
+
if os.path.isfile(file_path_no_ext + "." + ext):
|
| 126 |
+
images[item_name] = f"{type}/{file_name}.{ext}"
|
| 127 |
+
break
|
| 128 |
+
|
| 129 |
+
return web.json_response(images)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
class LoraLoaderWithImages(LoraLoader):
|
| 133 |
+
RETURN_TYPES = (*LoraLoader.RETURN_TYPES, "STRING",)
|
| 134 |
+
RETURN_NAMES = (*getattr(LoraLoader, "RETURN_NAMES",
|
| 135 |
+
LoraLoader.RETURN_TYPES), "example")
|
| 136 |
+
|
| 137 |
+
@classmethod
|
| 138 |
+
def INPUT_TYPES(s):
|
| 139 |
+
types = super().INPUT_TYPES()
|
| 140 |
+
types["optional"] = {"prompt": ("STRING", {"hidden": True})}
|
| 141 |
+
return types
|
| 142 |
+
|
| 143 |
+
def load_lora(self, **kwargs):
|
| 144 |
+
prompt = kwargs.pop("prompt", "")
|
| 145 |
+
return (*super().load_lora(**kwargs), prompt)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
class CheckpointLoaderSimpleWithImages(CheckpointLoaderSimple):
|
| 149 |
+
RETURN_TYPES = (*CheckpointLoaderSimple.RETURN_TYPES, "STRING",)
|
| 150 |
+
RETURN_NAMES = (*getattr(CheckpointLoaderSimple, "RETURN_NAMES",
|
| 151 |
+
CheckpointLoaderSimple.RETURN_TYPES), "example")
|
| 152 |
+
|
| 153 |
+
@classmethod
|
| 154 |
+
def INPUT_TYPES(s):
|
| 155 |
+
types = super().INPUT_TYPES()
|
| 156 |
+
types["optional"] = {"prompt": ("STRING", {"hidden": True})}
|
| 157 |
+
return types
|
| 158 |
+
|
| 159 |
+
def load_checkpoint(self, **kwargs):
|
| 160 |
+
prompt = kwargs.pop("prompt", "")
|
| 161 |
+
return (*super().load_checkpoint(**kwargs), prompt)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
NODE_CLASS_MAPPINGS = {
|
| 165 |
+
"LoraLoader|pysssss": LoraLoaderWithImages,
|
| 166 |
+
"CheckpointLoader|pysssss": CheckpointLoaderSimpleWithImages,
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 170 |
+
"LoraLoader|pysssss": "Lora Loader 🐍",
|
| 171 |
+
"CheckpointLoader|pysssss": "Checkpoint Loader 🐍",
|
| 172 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/constrain_image.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
class ConstrainImage:
|
| 6 |
+
"""
|
| 7 |
+
A node that constrains an image to a maximum and minimum size while maintaining aspect ratio.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
@classmethod
|
| 11 |
+
def INPUT_TYPES(cls):
|
| 12 |
+
return {
|
| 13 |
+
"required": {
|
| 14 |
+
"images": ("IMAGE",),
|
| 15 |
+
"max_width": ("INT", {"default": 1024, "min": 0}),
|
| 16 |
+
"max_height": ("INT", {"default": 1024, "min": 0}),
|
| 17 |
+
"min_width": ("INT", {"default": 0, "min": 0}),
|
| 18 |
+
"min_height": ("INT", {"default": 0, "min": 0}),
|
| 19 |
+
"crop_if_required": (["yes", "no"], {"default": "no"}),
|
| 20 |
+
},
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
RETURN_TYPES = ("IMAGE",)
|
| 24 |
+
FUNCTION = "constrain_image"
|
| 25 |
+
CATEGORY = "image"
|
| 26 |
+
OUTPUT_IS_LIST = (True,)
|
| 27 |
+
|
| 28 |
+
def constrain_image(self, images, max_width, max_height, min_width, min_height, crop_if_required):
|
| 29 |
+
crop_if_required = crop_if_required == "yes"
|
| 30 |
+
results = []
|
| 31 |
+
for image in images:
|
| 32 |
+
i = 255. * image.cpu().numpy()
|
| 33 |
+
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)).convert("RGB")
|
| 34 |
+
|
| 35 |
+
current_width, current_height = img.size
|
| 36 |
+
aspect_ratio = current_width / current_height
|
| 37 |
+
|
| 38 |
+
constrained_width = min(max(current_width, min_width), max_width)
|
| 39 |
+
constrained_height = min(max(current_height, min_height), max_height)
|
| 40 |
+
|
| 41 |
+
if constrained_width / constrained_height > aspect_ratio:
|
| 42 |
+
constrained_width = max(int(constrained_height * aspect_ratio), min_width)
|
| 43 |
+
if crop_if_required:
|
| 44 |
+
constrained_height = int(current_height / (current_width / constrained_width))
|
| 45 |
+
else:
|
| 46 |
+
constrained_height = max(int(constrained_width / aspect_ratio), min_height)
|
| 47 |
+
if crop_if_required:
|
| 48 |
+
constrained_width = int(current_width / (current_height / constrained_height))
|
| 49 |
+
|
| 50 |
+
resized_image = img.resize((constrained_width, constrained_height), Image.LANCZOS)
|
| 51 |
+
|
| 52 |
+
if crop_if_required and (constrained_width > max_width or constrained_height > max_height):
|
| 53 |
+
left = max((constrained_width - max_width) // 2, 0)
|
| 54 |
+
top = max((constrained_height - max_height) // 2, 0)
|
| 55 |
+
right = min(constrained_width, max_width) + left
|
| 56 |
+
bottom = min(constrained_height, max_height) + top
|
| 57 |
+
resized_image = resized_image.crop((left, top, right, bottom))
|
| 58 |
+
|
| 59 |
+
resized_image = np.array(resized_image).astype(np.float32) / 255.0
|
| 60 |
+
resized_image = torch.from_numpy(resized_image)[None,]
|
| 61 |
+
results.append(resized_image)
|
| 62 |
+
|
| 63 |
+
return (results,)
|
| 64 |
+
|
| 65 |
+
NODE_CLASS_MAPPINGS = {
|
| 66 |
+
"ConstrainImage|pysssss": ConstrainImage,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 70 |
+
"ConstrainImage|pysssss": "Constrain Image 🐍",
|
| 71 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/constrain_image_for_video.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
class ConstrainImageforVideo:
|
| 6 |
+
"""
|
| 7 |
+
A node that constrains an image to a maximum and minimum size while maintaining aspect ratio.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
@classmethod
|
| 11 |
+
def INPUT_TYPES(cls):
|
| 12 |
+
return {
|
| 13 |
+
"required": {
|
| 14 |
+
"images": ("IMAGE",),
|
| 15 |
+
"max_width": ("INT", {"default": 1024, "min": 0}),
|
| 16 |
+
"max_height": ("INT", {"default": 1024, "min": 0}),
|
| 17 |
+
"min_width": ("INT", {"default": 0, "min": 0}),
|
| 18 |
+
"min_height": ("INT", {"default": 0, "min": 0}),
|
| 19 |
+
"crop_if_required": (["yes", "no"], {"default": "no"}),
|
| 20 |
+
},
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
RETURN_TYPES = ("IMAGE",)
|
| 24 |
+
RETURN_NAMES = ("IMAGE",)
|
| 25 |
+
FUNCTION = "constrain_image_for_video"
|
| 26 |
+
CATEGORY = "image"
|
| 27 |
+
|
| 28 |
+
def constrain_image_for_video(self, images, max_width, max_height, min_width, min_height, crop_if_required):
|
| 29 |
+
crop_if_required = crop_if_required == "yes"
|
| 30 |
+
results = []
|
| 31 |
+
for image in images:
|
| 32 |
+
i = 255. * image.cpu().numpy()
|
| 33 |
+
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)).convert("RGB")
|
| 34 |
+
|
| 35 |
+
current_width, current_height = img.size
|
| 36 |
+
aspect_ratio = current_width / current_height
|
| 37 |
+
|
| 38 |
+
constrained_width = max(min(current_width, min_width), max_width)
|
| 39 |
+
constrained_height = max(min(current_height, min_height), max_height)
|
| 40 |
+
|
| 41 |
+
if constrained_width / constrained_height > aspect_ratio:
|
| 42 |
+
constrained_width = max(int(constrained_height * aspect_ratio), min_width)
|
| 43 |
+
if crop_if_required:
|
| 44 |
+
constrained_height = int(current_height / (current_width / constrained_width))
|
| 45 |
+
else:
|
| 46 |
+
constrained_height = max(int(constrained_width / aspect_ratio), min_height)
|
| 47 |
+
if crop_if_required:
|
| 48 |
+
constrained_width = int(current_width / (current_height / constrained_height))
|
| 49 |
+
|
| 50 |
+
resized_image = img.resize((constrained_width, constrained_height), Image.LANCZOS)
|
| 51 |
+
|
| 52 |
+
if crop_if_required and (constrained_width > max_width or constrained_height > max_height):
|
| 53 |
+
left = max((constrained_width - max_width) // 2, 0)
|
| 54 |
+
top = max((constrained_height - max_height) // 2, 0)
|
| 55 |
+
right = min(constrained_width, max_width) + left
|
| 56 |
+
bottom = min(constrained_height, max_height) + top
|
| 57 |
+
resized_image = resized_image.crop((left, top, right, bottom))
|
| 58 |
+
|
| 59 |
+
resized_image = np.array(resized_image).astype(np.float32) / 255.0
|
| 60 |
+
resized_image = torch.from_numpy(resized_image)[None,]
|
| 61 |
+
results.append(resized_image)
|
| 62 |
+
all_images = torch.cat(results, dim=0)
|
| 63 |
+
|
| 64 |
+
return (all_images, all_images.size(0),)
|
| 65 |
+
|
| 66 |
+
NODE_CLASS_MAPPINGS = {
|
| 67 |
+
"ConstrainImageforVideo|pysssss": ConstrainImageforVideo,
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 71 |
+
"ConstrainImageforVideo|pysssss": "Constrain Image for Video 🐍",
|
| 72 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/math_expression.py
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ast
|
| 2 |
+
import math
|
| 3 |
+
import random
|
| 4 |
+
import operator as op
|
| 5 |
+
|
| 6 |
+
# Hack: string type that is always equal in not equal comparisons
|
| 7 |
+
class AnyType(str):
|
| 8 |
+
def __ne__(self, __value: object) -> bool:
|
| 9 |
+
return False
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Our any instance wants to be a wildcard string
|
| 13 |
+
any = AnyType("*")
|
| 14 |
+
|
| 15 |
+
operators = {
|
| 16 |
+
ast.Add: op.add,
|
| 17 |
+
ast.Sub: op.sub,
|
| 18 |
+
ast.Mult: op.mul,
|
| 19 |
+
ast.Div: op.truediv,
|
| 20 |
+
ast.FloorDiv: op.floordiv,
|
| 21 |
+
ast.Pow: op.pow,
|
| 22 |
+
ast.BitXor: op.xor,
|
| 23 |
+
ast.USub: op.neg,
|
| 24 |
+
ast.Mod: op.mod,
|
| 25 |
+
ast.BitAnd: op.and_,
|
| 26 |
+
ast.BitOr: op.or_,
|
| 27 |
+
ast.Invert: op.invert,
|
| 28 |
+
ast.And: lambda a, b: 1 if a and b else 0,
|
| 29 |
+
ast.Or: lambda a, b: 1 if a or b else 0,
|
| 30 |
+
ast.Not: lambda a: 0 if a else 1,
|
| 31 |
+
ast.RShift: op.rshift,
|
| 32 |
+
ast.LShift: op.lshift
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# TODO: restructure args to provide more info, generate hint based on args to save duplication
|
| 36 |
+
functions = {
|
| 37 |
+
"round": {
|
| 38 |
+
"args": (1, 2),
|
| 39 |
+
"call": lambda a, b = None: round(a, b),
|
| 40 |
+
"hint": "number, dp? = 0"
|
| 41 |
+
},
|
| 42 |
+
"ceil": {
|
| 43 |
+
"args": (1, 1),
|
| 44 |
+
"call": lambda a: math.ceil(a),
|
| 45 |
+
"hint": "number"
|
| 46 |
+
},
|
| 47 |
+
"floor": {
|
| 48 |
+
"args": (1, 1),
|
| 49 |
+
"call": lambda a: math.floor(a),
|
| 50 |
+
"hint": "number"
|
| 51 |
+
},
|
| 52 |
+
"min": {
|
| 53 |
+
"args": (2, None),
|
| 54 |
+
"call": lambda *args: min(*args),
|
| 55 |
+
"hint": "...numbers"
|
| 56 |
+
},
|
| 57 |
+
"max": {
|
| 58 |
+
"args": (2, None),
|
| 59 |
+
"call": lambda *args: max(*args),
|
| 60 |
+
"hint": "...numbers"
|
| 61 |
+
},
|
| 62 |
+
"randomint": {
|
| 63 |
+
"args": (2, 2),
|
| 64 |
+
"call": lambda a, b: random.randint(a, b),
|
| 65 |
+
"hint": "min, max"
|
| 66 |
+
},
|
| 67 |
+
"randomchoice": {
|
| 68 |
+
"args": (2, None),
|
| 69 |
+
"call": lambda *args: random.choice(args),
|
| 70 |
+
"hint": "...numbers"
|
| 71 |
+
},
|
| 72 |
+
"sqrt": {
|
| 73 |
+
"args": (1, 1),
|
| 74 |
+
"call": lambda a: math.sqrt(a),
|
| 75 |
+
"hint": "number"
|
| 76 |
+
},
|
| 77 |
+
"int": {
|
| 78 |
+
"args": (1, 1),
|
| 79 |
+
"call": lambda a = None: int(a),
|
| 80 |
+
"hint": "number"
|
| 81 |
+
},
|
| 82 |
+
"iif": {
|
| 83 |
+
"args": (3, 3),
|
| 84 |
+
"call": lambda a, b, c = None: b if a else c,
|
| 85 |
+
"hint": "value, truepart, falsepart"
|
| 86 |
+
},
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
autocompleteWords = list({
|
| 90 |
+
"text": x,
|
| 91 |
+
"value": f"{x}()",
|
| 92 |
+
"showValue": False,
|
| 93 |
+
"hint": f"{functions[x]['hint']}",
|
| 94 |
+
"caretOffset": -1
|
| 95 |
+
} for x in functions.keys())
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class MathExpression:
|
| 99 |
+
|
| 100 |
+
@classmethod
|
| 101 |
+
def INPUT_TYPES(cls):
|
| 102 |
+
return {
|
| 103 |
+
"required": {
|
| 104 |
+
"expression": ("STRING", {"multiline": True, "dynamicPrompts": False, "pysssss.autocomplete": {
|
| 105 |
+
"words": autocompleteWords,
|
| 106 |
+
"separator": ""
|
| 107 |
+
}}),
|
| 108 |
+
},
|
| 109 |
+
"optional": {
|
| 110 |
+
"a": (any, ),
|
| 111 |
+
"b": (any,),
|
| 112 |
+
"c": (any, ),
|
| 113 |
+
},
|
| 114 |
+
"hidden": {"extra_pnginfo": "EXTRA_PNGINFO",
|
| 115 |
+
"prompt": "PROMPT"},
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
RETURN_TYPES = ("INT", "FLOAT", )
|
| 119 |
+
FUNCTION = "evaluate"
|
| 120 |
+
CATEGORY = "utils"
|
| 121 |
+
OUTPUT_NODE = True
|
| 122 |
+
|
| 123 |
+
@classmethod
|
| 124 |
+
def IS_CHANGED(s, expression, **kwargs):
|
| 125 |
+
if "random" in expression:
|
| 126 |
+
return float("nan")
|
| 127 |
+
return expression
|
| 128 |
+
|
| 129 |
+
def get_widget_value(self, extra_pnginfo, prompt, node_name, widget_name):
|
| 130 |
+
workflow = extra_pnginfo["workflow"] if "workflow" in extra_pnginfo else { "nodes": [] }
|
| 131 |
+
node_id = None
|
| 132 |
+
for node in workflow["nodes"]:
|
| 133 |
+
name = node["type"]
|
| 134 |
+
if "properties" in node:
|
| 135 |
+
if "Node name for S&R" in node["properties"]:
|
| 136 |
+
name = node["properties"]["Node name for S&R"]
|
| 137 |
+
if name == node_name:
|
| 138 |
+
node_id = node["id"]
|
| 139 |
+
break
|
| 140 |
+
if "title" in node:
|
| 141 |
+
name = node["title"]
|
| 142 |
+
if name == node_name:
|
| 143 |
+
node_id = node["id"]
|
| 144 |
+
break
|
| 145 |
+
if node_id is not None:
|
| 146 |
+
values = prompt[str(node_id)]
|
| 147 |
+
if "inputs" in values:
|
| 148 |
+
if widget_name in values["inputs"]:
|
| 149 |
+
value = values["inputs"][widget_name]
|
| 150 |
+
if isinstance(value, list):
|
| 151 |
+
raise ValueError("Converted widgets are not supported via named reference, use the inputs instead.")
|
| 152 |
+
return value
|
| 153 |
+
raise NameError(f"Widget not found: {node_name}.{widget_name}")
|
| 154 |
+
raise NameError(f"Node not found: {node_name}.{widget_name}")
|
| 155 |
+
|
| 156 |
+
def get_size(self, target, property):
|
| 157 |
+
if isinstance(target, dict) and "samples" in target:
|
| 158 |
+
# Latent
|
| 159 |
+
if property == "width":
|
| 160 |
+
return target["samples"].shape[3] * 8
|
| 161 |
+
return target["samples"].shape[2] * 8
|
| 162 |
+
else:
|
| 163 |
+
# Image
|
| 164 |
+
if property == "width":
|
| 165 |
+
return target.shape[2]
|
| 166 |
+
return target.shape[1]
|
| 167 |
+
|
| 168 |
+
def evaluate(self, expression, prompt, extra_pnginfo={}, a=None, b=None, c=None):
|
| 169 |
+
expression = expression.replace('\n', ' ').replace('\r', '')
|
| 170 |
+
node = ast.parse(expression, mode='eval').body
|
| 171 |
+
|
| 172 |
+
lookup = {"a": a, "b": b, "c": c}
|
| 173 |
+
|
| 174 |
+
def eval_op(node, l, r):
|
| 175 |
+
l = eval_expr(l)
|
| 176 |
+
r = eval_expr(r)
|
| 177 |
+
l = l if isinstance(l, int) else float(l)
|
| 178 |
+
r = r if isinstance(r, int) else float(r)
|
| 179 |
+
return operators[type(node.op)](l, r)
|
| 180 |
+
|
| 181 |
+
def eval_expr(node):
|
| 182 |
+
if isinstance(node, ast.Constant) or isinstance(node, ast.Num):
|
| 183 |
+
return node.n
|
| 184 |
+
elif isinstance(node, ast.BinOp):
|
| 185 |
+
return eval_op(node, node.left, node.right)
|
| 186 |
+
elif isinstance(node, ast.BoolOp):
|
| 187 |
+
return eval_op(node, node.values[0], node.values[1])
|
| 188 |
+
elif isinstance(node, ast.UnaryOp):
|
| 189 |
+
return operators[type(node.op)](eval_expr(node.operand))
|
| 190 |
+
elif isinstance(node, ast.Attribute):
|
| 191 |
+
if node.value.id in lookup:
|
| 192 |
+
if node.attr == "width" or node.attr == "height":
|
| 193 |
+
return self.get_size(lookup[node.value.id], node.attr)
|
| 194 |
+
|
| 195 |
+
return self.get_widget_value(extra_pnginfo, prompt, node.value.id, node.attr)
|
| 196 |
+
elif isinstance(node, ast.Name):
|
| 197 |
+
if node.id in lookup:
|
| 198 |
+
val = lookup[node.id]
|
| 199 |
+
if isinstance(val, (int, float, complex)):
|
| 200 |
+
return val
|
| 201 |
+
else:
|
| 202 |
+
raise TypeError(
|
| 203 |
+
f"Compex types (LATENT/IMAGE) need to reference their width/height, e.g. {node.id}.width")
|
| 204 |
+
raise NameError(f"Name not found: {node.id}")
|
| 205 |
+
elif isinstance(node, ast.Call):
|
| 206 |
+
if node.func.id in functions:
|
| 207 |
+
fn = functions[node.func.id]
|
| 208 |
+
l = len(node.args)
|
| 209 |
+
if l < fn["args"][0] or (fn["args"][1] is not None and l > fn["args"][1]):
|
| 210 |
+
if fn["args"][1] is None:
|
| 211 |
+
toErr = " or more"
|
| 212 |
+
else:
|
| 213 |
+
toErr = f" to {fn['args'][1]}"
|
| 214 |
+
raise SyntaxError(
|
| 215 |
+
f"Invalid function call: {node.func.id} requires {fn['args'][0]}{toErr} arguments")
|
| 216 |
+
args = []
|
| 217 |
+
for arg in node.args:
|
| 218 |
+
args.append(eval_expr(arg))
|
| 219 |
+
return fn["call"](*args)
|
| 220 |
+
raise NameError(f"Invalid function call: {node.func.id}")
|
| 221 |
+
elif isinstance(node, ast.Compare):
|
| 222 |
+
l = eval_expr(node.left)
|
| 223 |
+
r = eval_expr(node.comparators[0])
|
| 224 |
+
if isinstance(node.ops[0], ast.Eq):
|
| 225 |
+
return 1 if l == r else 0
|
| 226 |
+
if isinstance(node.ops[0], ast.NotEq):
|
| 227 |
+
return 1 if l != r else 0
|
| 228 |
+
if isinstance(node.ops[0], ast.Gt):
|
| 229 |
+
return 1 if l > r else 0
|
| 230 |
+
if isinstance(node.ops[0], ast.GtE):
|
| 231 |
+
return 1 if l >= r else 0
|
| 232 |
+
if isinstance(node.ops[0], ast.Lt):
|
| 233 |
+
return 1 if l < r else 0
|
| 234 |
+
if isinstance(node.ops[0], ast.LtE):
|
| 235 |
+
return 1 if l <= r else 0
|
| 236 |
+
raise NotImplementedError(
|
| 237 |
+
"Operator " + node.ops[0].__class__.__name__ + " not supported.")
|
| 238 |
+
else:
|
| 239 |
+
raise TypeError(node)
|
| 240 |
+
|
| 241 |
+
r = eval_expr(node)
|
| 242 |
+
return {"ui": {"value": [r]}, "result": (int(r), float(r),)}
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
NODE_CLASS_MAPPINGS = {
|
| 246 |
+
"MathExpression|pysssss": MathExpression,
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 250 |
+
"MathExpression|pysssss": "Math Expression 🐍",
|
| 251 |
+
}
|
| 252 |
+
|
custom_nodes/ComfyUI-Custom-Scripts/py/model_info.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import json
|
| 3 |
+
from aiohttp import web
|
| 4 |
+
from server import PromptServer
|
| 5 |
+
import folder_paths
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_metadata(filepath):
|
| 10 |
+
with open(filepath, "rb") as file:
|
| 11 |
+
# https://github.com/huggingface/safetensors#format
|
| 12 |
+
# 8 bytes: N, an unsigned little-endian 64-bit integer, containing the size of the header
|
| 13 |
+
header_size = int.from_bytes(file.read(8), "little", signed=False)
|
| 14 |
+
|
| 15 |
+
if header_size <= 0:
|
| 16 |
+
raise BufferError("Invalid header size")
|
| 17 |
+
|
| 18 |
+
header = file.read(header_size)
|
| 19 |
+
if header_size <= 0:
|
| 20 |
+
raise BufferError("Invalid header")
|
| 21 |
+
|
| 22 |
+
header_json = json.loads(header)
|
| 23 |
+
return header_json["__metadata__"] if "__metadata__" in header_json else None
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@PromptServer.instance.routes.post("/pysssss/metadata/notes/{name}")
|
| 27 |
+
async def save_notes(request):
|
| 28 |
+
name = request.match_info["name"]
|
| 29 |
+
pos = name.index("/")
|
| 30 |
+
type = name[0:pos]
|
| 31 |
+
name = name[pos+1:]
|
| 32 |
+
|
| 33 |
+
file_path = None
|
| 34 |
+
if type == "embeddings" or type == "loras":
|
| 35 |
+
name = name.lower()
|
| 36 |
+
files = folder_paths.get_filename_list(type)
|
| 37 |
+
for f in files:
|
| 38 |
+
lower_f = f.lower()
|
| 39 |
+
if lower_f == name:
|
| 40 |
+
file_path = folder_paths.get_full_path(type, f)
|
| 41 |
+
else:
|
| 42 |
+
n = os.path.splitext(f)[0].lower()
|
| 43 |
+
if n == name:
|
| 44 |
+
file_path = folder_paths.get_full_path(type, f)
|
| 45 |
+
|
| 46 |
+
if file_path is not None:
|
| 47 |
+
break
|
| 48 |
+
else:
|
| 49 |
+
file_path = folder_paths.get_full_path(
|
| 50 |
+
type, name)
|
| 51 |
+
if not file_path:
|
| 52 |
+
return web.Response(status=404)
|
| 53 |
+
|
| 54 |
+
file_no_ext = os.path.splitext(file_path)[0]
|
| 55 |
+
info_file = file_no_ext + ".txt"
|
| 56 |
+
with open(info_file, "w") as f:
|
| 57 |
+
f.write(await request.text())
|
| 58 |
+
|
| 59 |
+
return web.Response(status=200)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@PromptServer.instance.routes.get("/pysssss/metadata/{name}")
|
| 63 |
+
async def load_metadata(request):
|
| 64 |
+
name = request.match_info["name"]
|
| 65 |
+
pos = name.index("/")
|
| 66 |
+
type = name[0:pos]
|
| 67 |
+
name = name[pos+1:]
|
| 68 |
+
|
| 69 |
+
file_path = None
|
| 70 |
+
if type == "embeddings" or type == "loras":
|
| 71 |
+
name = name.lower()
|
| 72 |
+
files = folder_paths.get_filename_list(type)
|
| 73 |
+
for f in files:
|
| 74 |
+
lower_f = f.lower()
|
| 75 |
+
if lower_f == name:
|
| 76 |
+
file_path = folder_paths.get_full_path(type, f)
|
| 77 |
+
else:
|
| 78 |
+
n = os.path.splitext(f)[0].lower()
|
| 79 |
+
if n == name:
|
| 80 |
+
file_path = folder_paths.get_full_path(type, f)
|
| 81 |
+
|
| 82 |
+
if file_path is not None:
|
| 83 |
+
break
|
| 84 |
+
else:
|
| 85 |
+
file_path = folder_paths.get_full_path(
|
| 86 |
+
type, name)
|
| 87 |
+
if not file_path:
|
| 88 |
+
return web.Response(status=404)
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
meta = get_metadata(file_path)
|
| 92 |
+
except:
|
| 93 |
+
meta = None
|
| 94 |
+
|
| 95 |
+
if meta is None:
|
| 96 |
+
meta = {}
|
| 97 |
+
|
| 98 |
+
file_no_ext = os.path.splitext(file_path)[0]
|
| 99 |
+
|
| 100 |
+
info_file = file_no_ext + ".txt"
|
| 101 |
+
if os.path.isfile(info_file):
|
| 102 |
+
with open(info_file, "r") as f:
|
| 103 |
+
meta["pysssss.notes"] = f.read()
|
| 104 |
+
|
| 105 |
+
hash_file = file_no_ext + ".sha256"
|
| 106 |
+
if os.path.isfile(hash_file):
|
| 107 |
+
with open(hash_file, "rt") as f:
|
| 108 |
+
meta["pysssss.sha256"] = f.read()
|
| 109 |
+
else:
|
| 110 |
+
with open(file_path, "rb") as f:
|
| 111 |
+
meta["pysssss.sha256"] = hashlib.sha256(f.read()).hexdigest()
|
| 112 |
+
with open(hash_file, "wt") as f:
|
| 113 |
+
f.write(meta["pysssss.sha256"])
|
| 114 |
+
|
| 115 |
+
return web.json_response(meta)
|
custom_nodes/ComfyUI-Custom-Scripts/py/play_sound.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hack: string type that is always equal in not equal comparisons
|
| 2 |
+
class AnyType(str):
|
| 3 |
+
def __ne__(self, __value: object) -> bool:
|
| 4 |
+
return False
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Our any instance wants to be a wildcard string
|
| 8 |
+
any = AnyType("*")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PlaySound:
|
| 12 |
+
@classmethod
|
| 13 |
+
def INPUT_TYPES(s):
|
| 14 |
+
return {"required": {
|
| 15 |
+
"any": (any, {}),
|
| 16 |
+
"mode": (["always", "on empty queue"], {}),
|
| 17 |
+
"volume": ("FLOAT", {"min": 0, "max": 1, "step": 0.1, "default": 0.5}),
|
| 18 |
+
"file": ("STRING", { "default": "notify.mp3" })
|
| 19 |
+
}}
|
| 20 |
+
|
| 21 |
+
FUNCTION = "nop"
|
| 22 |
+
INPUT_IS_LIST = True
|
| 23 |
+
OUTPUT_IS_LIST = (True,)
|
| 24 |
+
OUTPUT_NODE = True
|
| 25 |
+
RETURN_TYPES = (any,)
|
| 26 |
+
|
| 27 |
+
CATEGORY = "utils"
|
| 28 |
+
|
| 29 |
+
def IS_CHANGED(self, **kwargs):
|
| 30 |
+
return float("NaN")
|
| 31 |
+
|
| 32 |
+
def nop(self, any, mode, volume, file):
|
| 33 |
+
return {"ui": {"a": []}, "result": (any,)}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
NODE_CLASS_MAPPINGS = {
|
| 37 |
+
"PlaySound|pysssss": PlaySound,
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 41 |
+
"PlaySound|pysssss": "PlaySound 🐍",
|
| 42 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/repeater.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hack: string type that is always equal in not equal comparisons
|
| 2 |
+
class AnyType(str):
|
| 3 |
+
def __ne__(self, __value: object) -> bool:
|
| 4 |
+
return False
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Our any instance wants to be a wildcard string
|
| 8 |
+
any = AnyType("*")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Repeater:
|
| 12 |
+
@classmethod
|
| 13 |
+
def INPUT_TYPES(s):
|
| 14 |
+
return {"required": {
|
| 15 |
+
"source": (any, {}),
|
| 16 |
+
"repeats": ("INT", {"min": 0, "max": 5000, "default": 2}),
|
| 17 |
+
"output": (["single", "multi"], {}),
|
| 18 |
+
"node_mode": (["reuse", "create"], {}),
|
| 19 |
+
}}
|
| 20 |
+
|
| 21 |
+
RETURN_TYPES = (any,)
|
| 22 |
+
FUNCTION = "repeat"
|
| 23 |
+
OUTPUT_NODE = False
|
| 24 |
+
OUTPUT_IS_LIST = (True,)
|
| 25 |
+
|
| 26 |
+
CATEGORY = "utils"
|
| 27 |
+
|
| 28 |
+
def repeat(self, repeats, output, node_mode, **kwargs):
|
| 29 |
+
if output == "multi":
|
| 30 |
+
# Multi outputs are split to indiviual nodes on the frontend when serializing
|
| 31 |
+
return ([kwargs["source"]],)
|
| 32 |
+
elif node_mode == "reuse":
|
| 33 |
+
# When reusing we have a single input node, repeat that N times
|
| 34 |
+
return ([kwargs["source"]] * repeats,)
|
| 35 |
+
else:
|
| 36 |
+
# When creating new nodes, they'll be added dynamically when the graph is serialized
|
| 37 |
+
return ((list(kwargs.values())),)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
NODE_CLASS_MAPPINGS = {
|
| 41 |
+
"Repeater|pysssss": Repeater,
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 45 |
+
"Repeater|pysssss": "Repeater 🐍",
|
| 46 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/reroute_primitive.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hack: string type that is always equal in not equal comparisons
|
| 2 |
+
class AnyType(str):
|
| 3 |
+
def __ne__(self, __value: object) -> bool:
|
| 4 |
+
return False
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Our any instance wants to be a wildcard string
|
| 8 |
+
any = AnyType("*")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class ReroutePrimitive:
|
| 12 |
+
@classmethod
|
| 13 |
+
def INPUT_TYPES(cls):
|
| 14 |
+
return {
|
| 15 |
+
"required": {"value": (any, )},
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
@classmethod
|
| 19 |
+
def VALIDATE_INPUTS(s, **kwargs):
|
| 20 |
+
return True
|
| 21 |
+
|
| 22 |
+
RETURN_TYPES = (any,)
|
| 23 |
+
FUNCTION = "route"
|
| 24 |
+
CATEGORY = "__hidden__"
|
| 25 |
+
|
| 26 |
+
def route(self, value):
|
| 27 |
+
return (value,)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class MultiPrimitive:
|
| 31 |
+
@classmethod
|
| 32 |
+
def INPUT_TYPES(cls):
|
| 33 |
+
return {
|
| 34 |
+
"required": {},
|
| 35 |
+
"optional": {"value": (any, )},
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
@classmethod
|
| 39 |
+
def VALIDATE_INPUTS(s, **kwargs):
|
| 40 |
+
return True
|
| 41 |
+
|
| 42 |
+
RETURN_TYPES = (any,)
|
| 43 |
+
FUNCTION = "listify"
|
| 44 |
+
CATEGORY = "utils"
|
| 45 |
+
OUTPUT_IS_LIST = (True,)
|
| 46 |
+
|
| 47 |
+
def listify(self, **kwargs):
|
| 48 |
+
return (list(kwargs.values()),)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
NODE_CLASS_MAPPINGS = {
|
| 52 |
+
"ReroutePrimitive|pysssss": ReroutePrimitive,
|
| 53 |
+
# "MultiPrimitive|pysssss": MultiPrimitive,
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 57 |
+
"ReroutePrimitive|pysssss": "Reroute Primitive 🐍",
|
| 58 |
+
# "MultiPrimitive|pysssss": "Multi Primitive 🐍",
|
| 59 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/show_text.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class ShowText:
|
| 2 |
+
@classmethod
|
| 3 |
+
def INPUT_TYPES(s):
|
| 4 |
+
return {
|
| 5 |
+
"required": {
|
| 6 |
+
"text": ("STRING", {"forceInput": True}),
|
| 7 |
+
},
|
| 8 |
+
"hidden": {
|
| 9 |
+
"unique_id": "UNIQUE_ID",
|
| 10 |
+
"extra_pnginfo": "EXTRA_PNGINFO",
|
| 11 |
+
},
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
INPUT_IS_LIST = True
|
| 15 |
+
RETURN_TYPES = ("STRING",)
|
| 16 |
+
FUNCTION = "notify"
|
| 17 |
+
OUTPUT_NODE = True
|
| 18 |
+
OUTPUT_IS_LIST = (True,)
|
| 19 |
+
|
| 20 |
+
CATEGORY = "utils"
|
| 21 |
+
|
| 22 |
+
def notify(self, text, unique_id=None, extra_pnginfo=None):
|
| 23 |
+
if unique_id is not None and extra_pnginfo is not None:
|
| 24 |
+
if not isinstance(extra_pnginfo, list):
|
| 25 |
+
print("Error: extra_pnginfo is not a list")
|
| 26 |
+
elif (
|
| 27 |
+
not isinstance(extra_pnginfo[0], dict)
|
| 28 |
+
or "workflow" not in extra_pnginfo[0]
|
| 29 |
+
):
|
| 30 |
+
print("Error: extra_pnginfo[0] is not a dict or missing 'workflow' key")
|
| 31 |
+
else:
|
| 32 |
+
workflow = extra_pnginfo[0]["workflow"]
|
| 33 |
+
node = next(
|
| 34 |
+
(x for x in workflow["nodes"] if str(x["id"]) == str(unique_id[0])),
|
| 35 |
+
None,
|
| 36 |
+
)
|
| 37 |
+
if node:
|
| 38 |
+
node["widgets_values"] = [text]
|
| 39 |
+
|
| 40 |
+
return {"ui": {"text": text}, "result": (text,)}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
NODE_CLASS_MAPPINGS = {
|
| 44 |
+
"ShowText|pysssss": ShowText,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 48 |
+
"ShowText|pysssss": "Show Text 🐍",
|
| 49 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/string_function.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
class StringFunction:
|
| 4 |
+
@classmethod
|
| 5 |
+
def INPUT_TYPES(s):
|
| 6 |
+
return {
|
| 7 |
+
"required": {
|
| 8 |
+
"action": (["append", "replace"], {}),
|
| 9 |
+
"tidy_tags": (["yes", "no"], {}),
|
| 10 |
+
},
|
| 11 |
+
"optional": {
|
| 12 |
+
"text_a": ("STRING", {"multiline": True, "dynamicPrompts": False}),
|
| 13 |
+
"text_b": ("STRING", {"multiline": True, "dynamicPrompts": False}),
|
| 14 |
+
"text_c": ("STRING", {"multiline": True, "dynamicPrompts": False})
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
RETURN_TYPES = ("STRING",)
|
| 19 |
+
FUNCTION = "exec"
|
| 20 |
+
CATEGORY = "utils"
|
| 21 |
+
OUTPUT_NODE = True
|
| 22 |
+
|
| 23 |
+
def exec(self, action, tidy_tags, text_a="", text_b="", text_c=""):
|
| 24 |
+
tidy_tags = tidy_tags == "yes"
|
| 25 |
+
out = ""
|
| 26 |
+
if action == "append":
|
| 27 |
+
out = (", " if tidy_tags else "").join(filter(None, [text_a, text_b, text_c]))
|
| 28 |
+
else:
|
| 29 |
+
if text_c is None:
|
| 30 |
+
text_c = ""
|
| 31 |
+
if text_b.startswith("/") and text_b.endswith("/"):
|
| 32 |
+
regex = text_b[1:-1]
|
| 33 |
+
out = re.sub(regex, text_c, text_a)
|
| 34 |
+
else:
|
| 35 |
+
out = text_a.replace(text_b, text_c)
|
| 36 |
+
if tidy_tags:
|
| 37 |
+
out = re.sub(r"\s{2,}", " ", out)
|
| 38 |
+
out = out.replace(" ,", ",")
|
| 39 |
+
out = re.sub(r",{2,}", ",", out)
|
| 40 |
+
out = out.strip()
|
| 41 |
+
return {"ui": {"text": (out,)}, "result": (out,)}
|
| 42 |
+
|
| 43 |
+
NODE_CLASS_MAPPINGS = {
|
| 44 |
+
"StringFunction|pysssss": StringFunction,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 48 |
+
"StringFunction|pysssss": "String Function 🐍",
|
| 49 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/system_notification.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hack: string type that is always equal in not equal comparisons
|
| 2 |
+
class AnyType(str):
|
| 3 |
+
def __ne__(self, __value: object) -> bool:
|
| 4 |
+
return False
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Our any instance wants to be a wildcard string
|
| 8 |
+
any = AnyType("*")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SystemNotification:
|
| 12 |
+
@classmethod
|
| 13 |
+
def INPUT_TYPES(s):
|
| 14 |
+
return {"required": {
|
| 15 |
+
"message": ("STRING", {"default": "Your notification has triggered."}),
|
| 16 |
+
"any": (any, {}),
|
| 17 |
+
"mode": (["always", "on empty queue"], {}),
|
| 18 |
+
}}
|
| 19 |
+
|
| 20 |
+
FUNCTION = "nop"
|
| 21 |
+
INPUT_IS_LIST = True
|
| 22 |
+
OUTPUT_IS_LIST = (True,)
|
| 23 |
+
OUTPUT_NODE = True
|
| 24 |
+
RETURN_TYPES = (any,)
|
| 25 |
+
|
| 26 |
+
CATEGORY = "utils"
|
| 27 |
+
|
| 28 |
+
def IS_CHANGED(self, **kwargs):
|
| 29 |
+
return float("NaN")
|
| 30 |
+
|
| 31 |
+
def nop(self, any, message, mode):
|
| 32 |
+
return {"ui": {"message": message, "mode": mode}, "result": (any,)}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
NODE_CLASS_MAPPINGS = {
|
| 36 |
+
"SystemNotification|pysssss": SystemNotification,
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 40 |
+
"SystemNotification|pysssss": "SystemNotification 🐍",
|
| 41 |
+
}
|
custom_nodes/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import folder_paths
|
| 3 |
+
import json
|
| 4 |
+
from server import PromptServer
|
| 5 |
+
import glob
|
| 6 |
+
from aiohttp import web
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_allowed_dirs():
|
| 10 |
+
dir = os.path.abspath(os.path.join(__file__, "../../user"))
|
| 11 |
+
file = os.path.join(dir, "text_file_dirs.json")
|
| 12 |
+
with open(file, "r") as f:
|
| 13 |
+
return json.loads(f.read())
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_valid_dirs():
|
| 17 |
+
return get_allowed_dirs().keys()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_dir_from_name(name):
|
| 21 |
+
dirs = get_allowed_dirs()
|
| 22 |
+
if name not in dirs:
|
| 23 |
+
raise KeyError(name + " dir not found")
|
| 24 |
+
|
| 25 |
+
path = dirs[name]
|
| 26 |
+
path = path.replace("$input", folder_paths.get_input_directory())
|
| 27 |
+
path = path.replace("$output", folder_paths.get_output_directory())
|
| 28 |
+
path = path.replace("$temp", folder_paths.get_temp_directory())
|
| 29 |
+
return path
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def is_child_dir(parent_path, child_path):
|
| 33 |
+
parent_path = os.path.abspath(parent_path)
|
| 34 |
+
child_path = os.path.abspath(child_path)
|
| 35 |
+
return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def get_real_path(dir):
|
| 39 |
+
dir = dir.replace("/**/", "/")
|
| 40 |
+
dir = os.path.abspath(dir)
|
| 41 |
+
dir = os.path.split(dir)[0]
|
| 42 |
+
return dir
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@PromptServer.instance.routes.get("/pysssss/text-file/{name}")
|
| 46 |
+
async def get_files(request):
|
| 47 |
+
name = request.match_info["name"]
|
| 48 |
+
dir = get_dir_from_name(name)
|
| 49 |
+
recursive = "/**/" in dir
|
| 50 |
+
# Ugh cant use root_path on glob... lazy hack..
|
| 51 |
+
pre = get_real_path(dir)
|
| 52 |
+
|
| 53 |
+
files = list(map(lambda t: os.path.relpath(t, pre),
|
| 54 |
+
glob.glob(dir, recursive=recursive)))
|
| 55 |
+
|
| 56 |
+
if len(files) == 0:
|
| 57 |
+
files = ["[none]"]
|
| 58 |
+
return web.json_response(files)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def get_file(root_dir, file):
|
| 62 |
+
if file == "[none]" or not file or not file.strip():
|
| 63 |
+
raise ValueError("No file")
|
| 64 |
+
|
| 65 |
+
root_dir = get_dir_from_name(root_dir)
|
| 66 |
+
root_dir = get_real_path(root_dir)
|
| 67 |
+
if not os.path.exists(root_dir):
|
| 68 |
+
os.mkdir(root_dir)
|
| 69 |
+
full_path = os.path.join(root_dir, file)
|
| 70 |
+
|
| 71 |
+
file_dir = os.path.dirname(full_path)
|
| 72 |
+
if file_dir and not os.path.exists(file_dir):
|
| 73 |
+
os.makedirs(file_dir, exist_ok=True)
|
| 74 |
+
|
| 75 |
+
if not is_child_dir(root_dir, full_path):
|
| 76 |
+
raise ReferenceError()
|
| 77 |
+
|
| 78 |
+
return full_path
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class TextFileNode:
|
| 82 |
+
RETURN_TYPES = ("STRING",)
|
| 83 |
+
CATEGORY = "utils"
|
| 84 |
+
|
| 85 |
+
@classmethod
|
| 86 |
+
def VALIDATE_INPUTS(self, root_dir, file, **kwargs):
|
| 87 |
+
if file == "[none]" or not file or not file.strip():
|
| 88 |
+
return True
|
| 89 |
+
get_file(root_dir, file)
|
| 90 |
+
return True
|
| 91 |
+
|
| 92 |
+
def load_text(self, **kwargs):
|
| 93 |
+
self.file = get_file(kwargs["root_dir"], kwargs["file"])
|
| 94 |
+
with open(self.file, "r") as f:
|
| 95 |
+
return (f.read(), )
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class LoadText(TextFileNode):
|
| 99 |
+
@classmethod
|
| 100 |
+
def IS_CHANGED(self, **kwargs):
|
| 101 |
+
return os.path.getmtime(self.file)
|
| 102 |
+
|
| 103 |
+
@classmethod
|
| 104 |
+
def INPUT_TYPES(s):
|
| 105 |
+
return {
|
| 106 |
+
"required": {
|
| 107 |
+
"root_dir": (list(get_valid_dirs()), {}),
|
| 108 |
+
"file": (["[none]"], {
|
| 109 |
+
"pysssss.binding": [{
|
| 110 |
+
"source": "root_dir",
|
| 111 |
+
"callback": [{
|
| 112 |
+
"type": "set",
|
| 113 |
+
"target": "$this.disabled",
|
| 114 |
+
"value": True
|
| 115 |
+
}, {
|
| 116 |
+
"type": "fetch",
|
| 117 |
+
"url": "/pysssss/text-file/{$source.value}",
|
| 118 |
+
"then": [{
|
| 119 |
+
"type": "set",
|
| 120 |
+
"target": "$this.options.values",
|
| 121 |
+
"value": "$result"
|
| 122 |
+
}, {
|
| 123 |
+
"type": "validate-combo"
|
| 124 |
+
}, {
|
| 125 |
+
"type": "set",
|
| 126 |
+
"target": "$this.disabled",
|
| 127 |
+
"value": False
|
| 128 |
+
}]
|
| 129 |
+
}],
|
| 130 |
+
}]
|
| 131 |
+
})
|
| 132 |
+
},
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
FUNCTION = "load_text"
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class SaveText(TextFileNode):
|
| 139 |
+
OUTPUT_NODE = True
|
| 140 |
+
|
| 141 |
+
@classmethod
|
| 142 |
+
def IS_CHANGED(self, **kwargs):
|
| 143 |
+
return float("nan")
|
| 144 |
+
|
| 145 |
+
@classmethod
|
| 146 |
+
def INPUT_TYPES(s):
|
| 147 |
+
return {
|
| 148 |
+
"required": {
|
| 149 |
+
"root_dir": (list(get_valid_dirs()), {}),
|
| 150 |
+
"file": ("STRING", {"default": "file.txt"}),
|
| 151 |
+
"append": (["append", "overwrite", "new only"], {}),
|
| 152 |
+
"insert": ("BOOLEAN", {
|
| 153 |
+
"default": True, "label_on": "new line", "label_off": "none",
|
| 154 |
+
"pysssss.binding": [{
|
| 155 |
+
"source": "append",
|
| 156 |
+
"callback": [{
|
| 157 |
+
"type": "if",
|
| 158 |
+
"condition": [{
|
| 159 |
+
"left": "$source.value",
|
| 160 |
+
"op": "eq",
|
| 161 |
+
"right": '"append"'
|
| 162 |
+
}],
|
| 163 |
+
"true": [{
|
| 164 |
+
"type": "set",
|
| 165 |
+
"target": "$this.disabled",
|
| 166 |
+
"value": False
|
| 167 |
+
}],
|
| 168 |
+
"false": [{
|
| 169 |
+
"type": "set",
|
| 170 |
+
"target": "$this.disabled",
|
| 171 |
+
"value": True
|
| 172 |
+
}],
|
| 173 |
+
}]
|
| 174 |
+
}]
|
| 175 |
+
}),
|
| 176 |
+
"text": ("STRING", {"forceInput": True, "multiline": True})
|
| 177 |
+
},
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
FUNCTION = "write_text"
|
| 181 |
+
|
| 182 |
+
def write_text(self, **kwargs):
|
| 183 |
+
self.file = get_file(kwargs["root_dir"], kwargs["file"])
|
| 184 |
+
if kwargs["append"] == "new only" and os.path.exists(self.file):
|
| 185 |
+
raise FileExistsError(
|
| 186 |
+
self.file + " already exists and 'new only' is selected.")
|
| 187 |
+
with open(self.file, "a+" if kwargs["append"] == "append" else "w") as f:
|
| 188 |
+
is_append = f.tell() != 0
|
| 189 |
+
if is_append and kwargs["insert"]:
|
| 190 |
+
f.write("\n")
|
| 191 |
+
f.write(kwargs["text"])
|
| 192 |
+
|
| 193 |
+
return super().load_text(**kwargs)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
NODE_CLASS_MAPPINGS = {
|
| 197 |
+
"LoadText|pysssss": LoadText,
|
| 198 |
+
"SaveText|pysssss": SaveText,
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 202 |
+
"LoadText|pysssss": "Load Text 🐍",
|
| 203 |
+
"SaveText|pysssss": "Save Text 🐍",
|
| 204 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/py/workflows.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from server import PromptServer
|
| 2 |
+
from aiohttp import web
|
| 3 |
+
import os
|
| 4 |
+
import inspect
|
| 5 |
+
import json
|
| 6 |
+
import importlib
|
| 7 |
+
import sys
|
| 8 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
| 9 |
+
import pysssss
|
| 10 |
+
|
| 11 |
+
root_directory = os.path.dirname(inspect.getfile(PromptServer))
|
| 12 |
+
workflows_directory = os.path.join(root_directory, "pysssss-workflows")
|
| 13 |
+
workflows_directory = pysssss.get_config_value(
|
| 14 |
+
"workflows.directory", workflows_directory)
|
| 15 |
+
if not os.path.isabs(workflows_directory):
|
| 16 |
+
workflows_directory = os.path.abspath(os.path.join(root_directory, workflows_directory))
|
| 17 |
+
|
| 18 |
+
NODE_CLASS_MAPPINGS = {}
|
| 19 |
+
NODE_DISPLAY_NAME_MAPPINGS = {}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@PromptServer.instance.routes.get("/pysssss/workflows")
|
| 23 |
+
async def get_workflows(request):
|
| 24 |
+
files = []
|
| 25 |
+
for dirpath, directories, file in os.walk(workflows_directory):
|
| 26 |
+
for file in file:
|
| 27 |
+
if (file.endswith(".json")):
|
| 28 |
+
files.append(os.path.relpath(os.path.join(
|
| 29 |
+
dirpath, file), workflows_directory))
|
| 30 |
+
return web.json_response(list(map(lambda f: os.path.splitext(f)[0].replace("\\", "/"), files)))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@PromptServer.instance.routes.get("/pysssss/workflows/{name:.+}")
|
| 34 |
+
async def get_workflow(request):
|
| 35 |
+
file = os.path.abspath(os.path.join(
|
| 36 |
+
workflows_directory, request.match_info["name"] + ".json"))
|
| 37 |
+
if os.path.commonpath([file, workflows_directory]) != workflows_directory:
|
| 38 |
+
return web.Response(status=403)
|
| 39 |
+
|
| 40 |
+
return web.FileResponse(file)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@PromptServer.instance.routes.post("/pysssss/workflows")
|
| 44 |
+
async def save_workflow(request):
|
| 45 |
+
json_data = await request.json()
|
| 46 |
+
file = os.path.abspath(os.path.join(
|
| 47 |
+
workflows_directory, json_data["name"] + ".json"))
|
| 48 |
+
if os.path.commonpath([file, workflows_directory]) != workflows_directory:
|
| 49 |
+
return web.Response(status=403)
|
| 50 |
+
|
| 51 |
+
if os.path.exists(file) and ("overwrite" not in json_data or json_data["overwrite"] == False):
|
| 52 |
+
return web.Response(status=409)
|
| 53 |
+
|
| 54 |
+
sub_path = os.path.dirname(file)
|
| 55 |
+
if not os.path.exists(sub_path):
|
| 56 |
+
os.makedirs(sub_path)
|
| 57 |
+
|
| 58 |
+
with open(file, "w") as f:
|
| 59 |
+
f.write(json.dumps(json_data["workflow"]))
|
| 60 |
+
|
| 61 |
+
return web.Response(status=201)
|
custom_nodes/ComfyUI-Custom-Scripts/pyproject.toml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "comfyui-custom-scripts"
|
| 3 |
+
description = "Enhancements & experiments for ComfyUI, mostly focusing on UI features"
|
| 4 |
+
version = "1.2.5"
|
| 5 |
+
license = { file = "LICENSE" }
|
| 6 |
+
|
| 7 |
+
[project.urls]
|
| 8 |
+
Repository = "https://github.com/pythongosssss/ComfyUI-Custom-Scripts"
|
| 9 |
+
|
| 10 |
+
[tool.comfy]
|
| 11 |
+
PublisherId = "pythongosssss"
|
| 12 |
+
DisplayName = "ComfyUI-Custom-Scripts"
|
| 13 |
+
Icon = ""
|
custom_nodes/ComfyUI-Custom-Scripts/pysssss.default.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "CustomScripts",
|
| 3 |
+
"logging": false
|
| 4 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/pysssss.example.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "CustomScripts",
|
| 3 |
+
"logging": false,
|
| 4 |
+
"workflows": {
|
| 5 |
+
"directory": "C:\\ComfyUI-Workflows"
|
| 6 |
+
}
|
| 7 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/pysssss.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import shutil
|
| 5 |
+
import inspect
|
| 6 |
+
import aiohttp
|
| 7 |
+
from server import PromptServer
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
|
| 10 |
+
config = None
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def is_logging_enabled():
|
| 14 |
+
config = get_extension_config()
|
| 15 |
+
if "logging" not in config:
|
| 16 |
+
return False
|
| 17 |
+
return config["logging"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def log(message, type=None, always=False, name=None):
|
| 21 |
+
if not always and not is_logging_enabled():
|
| 22 |
+
return
|
| 23 |
+
|
| 24 |
+
if type is not None:
|
| 25 |
+
message = f"[{type}] {message}"
|
| 26 |
+
|
| 27 |
+
if name is None:
|
| 28 |
+
name = get_extension_config()["name"]
|
| 29 |
+
|
| 30 |
+
print(f"(pysssss:{name}) {message}")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_ext_dir(subpath=None, mkdir=False):
|
| 34 |
+
dir = os.path.dirname(__file__)
|
| 35 |
+
if subpath is not None:
|
| 36 |
+
dir = os.path.join(dir, subpath)
|
| 37 |
+
|
| 38 |
+
dir = os.path.abspath(dir)
|
| 39 |
+
|
| 40 |
+
if mkdir and not os.path.exists(dir):
|
| 41 |
+
os.makedirs(dir)
|
| 42 |
+
return dir
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_comfy_dir(subpath=None, mkdir=False):
|
| 46 |
+
dir = os.path.dirname(inspect.getfile(PromptServer))
|
| 47 |
+
if subpath is not None:
|
| 48 |
+
dir = os.path.join(dir, subpath)
|
| 49 |
+
|
| 50 |
+
dir = os.path.abspath(dir)
|
| 51 |
+
|
| 52 |
+
if mkdir and not os.path.exists(dir):
|
| 53 |
+
os.makedirs(dir)
|
| 54 |
+
return dir
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_web_ext_dir():
|
| 58 |
+
config = get_extension_config()
|
| 59 |
+
name = config["name"]
|
| 60 |
+
dir = get_comfy_dir("web/extensions/pysssss")
|
| 61 |
+
if not os.path.exists(dir):
|
| 62 |
+
os.makedirs(dir)
|
| 63 |
+
dir = os.path.join(dir, name)
|
| 64 |
+
return dir
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_extension_config(reload=False):
|
| 68 |
+
global config
|
| 69 |
+
if reload == False and config is not None:
|
| 70 |
+
return config
|
| 71 |
+
|
| 72 |
+
config_path = get_ext_dir("pysssss.json")
|
| 73 |
+
default_config_path = get_ext_dir("pysssss.default.json")
|
| 74 |
+
if not os.path.exists(config_path):
|
| 75 |
+
if os.path.exists(default_config_path):
|
| 76 |
+
shutil.copy(default_config_path, config_path)
|
| 77 |
+
if not os.path.exists(config_path):
|
| 78 |
+
log(f"Failed to create config at {config_path}", type="ERROR", always=True, name="???")
|
| 79 |
+
print(f"Extension path: {get_ext_dir()}")
|
| 80 |
+
return {"name": "Unknown", "version": -1}
|
| 81 |
+
|
| 82 |
+
else:
|
| 83 |
+
log("Missing pysssss.default.json, this extension may not work correctly. Please reinstall the extension.",
|
| 84 |
+
type="ERROR", always=True, name="???")
|
| 85 |
+
print(f"Extension path: {get_ext_dir()}")
|
| 86 |
+
return {"name": "Unknown", "version": -1}
|
| 87 |
+
|
| 88 |
+
with open(config_path, "r") as f:
|
| 89 |
+
config = json.loads(f.read())
|
| 90 |
+
return config
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def link_js(src, dst):
|
| 94 |
+
src = os.path.abspath(src)
|
| 95 |
+
dst = os.path.abspath(dst)
|
| 96 |
+
if os.name == "nt":
|
| 97 |
+
try:
|
| 98 |
+
import _winapi
|
| 99 |
+
_winapi.CreateJunction(src, dst)
|
| 100 |
+
return True
|
| 101 |
+
except:
|
| 102 |
+
pass
|
| 103 |
+
try:
|
| 104 |
+
os.symlink(src, dst)
|
| 105 |
+
return True
|
| 106 |
+
except:
|
| 107 |
+
import logging
|
| 108 |
+
logging.exception('')
|
| 109 |
+
return False
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def is_junction(path):
|
| 113 |
+
if os.name != "nt":
|
| 114 |
+
return False
|
| 115 |
+
try:
|
| 116 |
+
return bool(os.readlink(path))
|
| 117 |
+
except OSError:
|
| 118 |
+
return False
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def install_js():
|
| 122 |
+
src_dir = get_ext_dir("web/js")
|
| 123 |
+
if not os.path.exists(src_dir):
|
| 124 |
+
log("No JS")
|
| 125 |
+
return
|
| 126 |
+
|
| 127 |
+
should_install = should_install_js()
|
| 128 |
+
if should_install:
|
| 129 |
+
log("it looks like you're running an old version of ComfyUI that requires manual setup of web files, it is recommended you update your installation.", "warning", True)
|
| 130 |
+
dst_dir = get_web_ext_dir()
|
| 131 |
+
linked = os.path.islink(dst_dir) or is_junction(dst_dir)
|
| 132 |
+
if linked or os.path.exists(dst_dir):
|
| 133 |
+
if linked:
|
| 134 |
+
if should_install:
|
| 135 |
+
log("JS already linked")
|
| 136 |
+
else:
|
| 137 |
+
os.unlink(dst_dir)
|
| 138 |
+
log("JS unlinked, PromptServer will serve extension")
|
| 139 |
+
elif not should_install:
|
| 140 |
+
shutil.rmtree(dst_dir)
|
| 141 |
+
log("JS deleted, PromptServer will serve extension")
|
| 142 |
+
return
|
| 143 |
+
|
| 144 |
+
if not should_install:
|
| 145 |
+
log("JS skipped, PromptServer will serve extension")
|
| 146 |
+
return
|
| 147 |
+
|
| 148 |
+
if link_js(src_dir, dst_dir):
|
| 149 |
+
log("JS linked")
|
| 150 |
+
return
|
| 151 |
+
|
| 152 |
+
log("Copying JS files")
|
| 153 |
+
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=True)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def should_install_js():
|
| 157 |
+
return not hasattr(PromptServer.instance, "supports") or "custom_nodes_from_web" not in PromptServer.instance.supports
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def init(check_imports=None):
|
| 161 |
+
log("Init")
|
| 162 |
+
|
| 163 |
+
if check_imports is not None:
|
| 164 |
+
import importlib.util
|
| 165 |
+
for imp in check_imports:
|
| 166 |
+
spec = importlib.util.find_spec(imp)
|
| 167 |
+
if spec is None:
|
| 168 |
+
log(f"{imp} is required, please check requirements are installed.",
|
| 169 |
+
type="ERROR", always=True)
|
| 170 |
+
return False
|
| 171 |
+
|
| 172 |
+
install_js()
|
| 173 |
+
return True
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def get_async_loop():
|
| 177 |
+
loop = None
|
| 178 |
+
try:
|
| 179 |
+
loop = asyncio.get_event_loop()
|
| 180 |
+
except:
|
| 181 |
+
loop = asyncio.new_event_loop()
|
| 182 |
+
asyncio.set_event_loop(loop)
|
| 183 |
+
return loop
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def get_http_session():
|
| 187 |
+
loop = get_async_loop()
|
| 188 |
+
return aiohttp.ClientSession(loop=loop)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
async def download(url, stream, update_callback=None, session=None):
|
| 192 |
+
close_session = False
|
| 193 |
+
if session is None:
|
| 194 |
+
close_session = True
|
| 195 |
+
session = get_http_session()
|
| 196 |
+
try:
|
| 197 |
+
async with session.get(url) as response:
|
| 198 |
+
size = int(response.headers.get('content-length', 0)) or None
|
| 199 |
+
|
| 200 |
+
with tqdm(
|
| 201 |
+
unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1], total=size,
|
| 202 |
+
) as progressbar:
|
| 203 |
+
perc = 0
|
| 204 |
+
async for chunk in response.content.iter_chunked(2048):
|
| 205 |
+
stream.write(chunk)
|
| 206 |
+
progressbar.update(len(chunk))
|
| 207 |
+
if update_callback is not None and progressbar.total is not None and progressbar.total != 0:
|
| 208 |
+
last = perc
|
| 209 |
+
perc = round(progressbar.n / progressbar.total, 2)
|
| 210 |
+
if perc != last:
|
| 211 |
+
last = perc
|
| 212 |
+
await update_callback(perc)
|
| 213 |
+
finally:
|
| 214 |
+
if close_session and session is not None:
|
| 215 |
+
await session.close()
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
async def download_to_file(url, destination, update_callback=None, is_ext_subpath=True, session=None):
|
| 219 |
+
if is_ext_subpath:
|
| 220 |
+
destination = get_ext_dir(destination)
|
| 221 |
+
with open(destination, mode='wb') as f:
|
| 222 |
+
download(url, f, update_callback, session)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def wait_for_async(async_fn, loop=None):
|
| 226 |
+
res = []
|
| 227 |
+
|
| 228 |
+
async def run_async():
|
| 229 |
+
r = await async_fn()
|
| 230 |
+
res.append(r)
|
| 231 |
+
|
| 232 |
+
if loop is None:
|
| 233 |
+
try:
|
| 234 |
+
loop = asyncio.get_event_loop()
|
| 235 |
+
except:
|
| 236 |
+
loop = asyncio.new_event_loop()
|
| 237 |
+
asyncio.set_event_loop(loop)
|
| 238 |
+
|
| 239 |
+
loop.run_until_complete(run_async())
|
| 240 |
+
|
| 241 |
+
return res[0]
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
def update_node_status(client_id, node, text, progress=None):
|
| 245 |
+
if client_id is None:
|
| 246 |
+
client_id = PromptServer.instance.client_id
|
| 247 |
+
|
| 248 |
+
if client_id is None:
|
| 249 |
+
return
|
| 250 |
+
|
| 251 |
+
PromptServer.instance.send_sync("pysssss/update_status", {
|
| 252 |
+
"node": node,
|
| 253 |
+
"progress": progress,
|
| 254 |
+
"text": text
|
| 255 |
+
}, client_id)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
async def update_node_status_async(client_id, node, text, progress=None):
|
| 259 |
+
if client_id is None:
|
| 260 |
+
client_id = PromptServer.instance.client_id
|
| 261 |
+
|
| 262 |
+
if client_id is None:
|
| 263 |
+
return
|
| 264 |
+
|
| 265 |
+
await PromptServer.instance.send("pysssss/update_status", {
|
| 266 |
+
"node": node,
|
| 267 |
+
"progress": progress,
|
| 268 |
+
"text": text
|
| 269 |
+
}, client_id)
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def get_config_value(key, default=None, throw=False):
|
| 273 |
+
split = key.split(".")
|
| 274 |
+
obj = get_extension_config()
|
| 275 |
+
for s in split:
|
| 276 |
+
if s in obj:
|
| 277 |
+
obj = obj[s]
|
| 278 |
+
else:
|
| 279 |
+
if throw:
|
| 280 |
+
raise KeyError("Configuration key missing: " + key)
|
| 281 |
+
else:
|
| 282 |
+
return default
|
| 283 |
+
return obj
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
def is_inside_dir(root_dir, check_path):
|
| 287 |
+
root_dir = os.path.abspath(root_dir)
|
| 288 |
+
if not os.path.isabs(check_path):
|
| 289 |
+
check_path = os.path.abspath(os.path.join(root_dir, check_path))
|
| 290 |
+
return os.path.commonpath([check_path, root_dir]) == root_dir
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def get_child_dir(root_dir, child_path, throw_if_outside=True):
|
| 294 |
+
child_path = os.path.abspath(os.path.join(root_dir, child_path))
|
| 295 |
+
if is_inside_dir(root_dir, child_path):
|
| 296 |
+
return child_path
|
| 297 |
+
if throw_if_outside:
|
| 298 |
+
raise NotADirectoryError(
|
| 299 |
+
"Saving outside the target folder is not allowed.")
|
| 300 |
+
return None
|
custom_nodes/ComfyUI-Custom-Scripts/user/text_file_dirs.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"input": "$input/**/*.txt",
|
| 3 |
+
"output": "$output/**/*.txt",
|
| 4 |
+
"temp": "$temp/**/*.txt"
|
| 5 |
+
}
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/canvas2svg.js
ADDED
|
@@ -0,0 +1,1192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*!!
|
| 2 |
+
* Canvas 2 Svg v1.0.19
|
| 3 |
+
* A low level canvas to SVG converter. Uses a mock canvas context to build an SVG document.
|
| 4 |
+
*
|
| 5 |
+
* Licensed under the MIT license:
|
| 6 |
+
* http://www.opensource.org/licenses/mit-license.php
|
| 7 |
+
*
|
| 8 |
+
* Author:
|
| 9 |
+
* Kerry Liu
|
| 10 |
+
*
|
| 11 |
+
* Copyright (c) 2014 Gliffy Inc.
|
| 12 |
+
*/
|
| 13 |
+
|
| 14 |
+
;(function() {
|
| 15 |
+
"use strict";
|
| 16 |
+
|
| 17 |
+
var STYLES, ctx, CanvasGradient, CanvasPattern, namedEntities;
|
| 18 |
+
|
| 19 |
+
//helper function to format a string
|
| 20 |
+
function format(str, args) {
|
| 21 |
+
var keys = Object.keys(args), i;
|
| 22 |
+
for (i=0; i<keys.length; i++) {
|
| 23 |
+
str = str.replace(new RegExp("\\{" + keys[i] + "\\}", "gi"), args[keys[i]]);
|
| 24 |
+
}
|
| 25 |
+
return str;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
//helper function that generates a random string
|
| 29 |
+
function randomString(holder) {
|
| 30 |
+
var chars, randomstring, i;
|
| 31 |
+
if (!holder) {
|
| 32 |
+
throw new Error("cannot create a random attribute name for an undefined object");
|
| 33 |
+
}
|
| 34 |
+
chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
|
| 35 |
+
randomstring = "";
|
| 36 |
+
do {
|
| 37 |
+
randomstring = "";
|
| 38 |
+
for (i = 0; i < 12; i++) {
|
| 39 |
+
randomstring += chars[Math.floor(Math.random() * chars.length)];
|
| 40 |
+
}
|
| 41 |
+
} while (holder[randomstring]);
|
| 42 |
+
return randomstring;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
//helper function to map named to numbered entities
|
| 46 |
+
function createNamedToNumberedLookup(items, radix) {
|
| 47 |
+
var i, entity, lookup = {}, base10, base16;
|
| 48 |
+
items = items.split(',');
|
| 49 |
+
radix = radix || 10;
|
| 50 |
+
// Map from named to numbered entities.
|
| 51 |
+
for (i = 0; i < items.length; i += 2) {
|
| 52 |
+
entity = '&' + items[i + 1] + ';';
|
| 53 |
+
base10 = parseInt(items[i], radix);
|
| 54 |
+
lookup[entity] = '&#'+base10+';';
|
| 55 |
+
}
|
| 56 |
+
//FF and IE need to create a regex from hex values ie == \xa0
|
| 57 |
+
lookup["\\xa0"] = ' ';
|
| 58 |
+
return lookup;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
//helper function to map canvas-textAlign to svg-textAnchor
|
| 62 |
+
function getTextAnchor(textAlign) {
|
| 63 |
+
//TODO: support rtl languages
|
| 64 |
+
var mapping = {"left":"start", "right":"end", "center":"middle", "start":"start", "end":"end"};
|
| 65 |
+
return mapping[textAlign] || mapping.start;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
//helper function to map canvas-textBaseline to svg-dominantBaseline
|
| 69 |
+
function getDominantBaseline(textBaseline) {
|
| 70 |
+
//INFO: not supported in all browsers
|
| 71 |
+
var mapping = {"alphabetic": "alphabetic", "hanging": "hanging", "top":"text-before-edge", "bottom":"text-after-edge", "middle":"central"};
|
| 72 |
+
return mapping[textBaseline] || mapping.alphabetic;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// Unpack entities lookup where the numbers are in radix 32 to reduce the size
|
| 76 |
+
// entity mapping courtesy of tinymce
|
| 77 |
+
namedEntities = createNamedToNumberedLookup(
|
| 78 |
+
'50,nbsp,51,iexcl,52,cent,53,pound,54,curren,55,yen,56,brvbar,57,sect,58,uml,59,copy,' +
|
| 79 |
+
'5a,ordf,5b,laquo,5c,not,5d,shy,5e,reg,5f,macr,5g,deg,5h,plusmn,5i,sup2,5j,sup3,5k,acute,' +
|
| 80 |
+
'5l,micro,5m,para,5n,middot,5o,cedil,5p,sup1,5q,ordm,5r,raquo,5s,frac14,5t,frac12,5u,frac34,' +
|
| 81 |
+
'5v,iquest,60,Agrave,61,Aacute,62,Acirc,63,Atilde,64,Auml,65,Aring,66,AElig,67,Ccedil,' +
|
| 82 |
+
'68,Egrave,69,Eacute,6a,Ecirc,6b,Euml,6c,Igrave,6d,Iacute,6e,Icirc,6f,Iuml,6g,ETH,6h,Ntilde,' +
|
| 83 |
+
'6i,Ograve,6j,Oacute,6k,Ocirc,6l,Otilde,6m,Ouml,6n,times,6o,Oslash,6p,Ugrave,6q,Uacute,' +
|
| 84 |
+
'6r,Ucirc,6s,Uuml,6t,Yacute,6u,THORN,6v,szlig,70,agrave,71,aacute,72,acirc,73,atilde,74,auml,' +
|
| 85 |
+
'75,aring,76,aelig,77,ccedil,78,egrave,79,eacute,7a,ecirc,7b,euml,7c,igrave,7d,iacute,7e,icirc,' +
|
| 86 |
+
'7f,iuml,7g,eth,7h,ntilde,7i,ograve,7j,oacute,7k,ocirc,7l,otilde,7m,ouml,7n,divide,7o,oslash,' +
|
| 87 |
+
'7p,ugrave,7q,uacute,7r,ucirc,7s,uuml,7t,yacute,7u,thorn,7v,yuml,ci,fnof,sh,Alpha,si,Beta,' +
|
| 88 |
+
'sj,Gamma,sk,Delta,sl,Epsilon,sm,Zeta,sn,Eta,so,Theta,sp,Iota,sq,Kappa,sr,Lambda,ss,Mu,' +
|
| 89 |
+
'st,Nu,su,Xi,sv,Omicron,t0,Pi,t1,Rho,t3,Sigma,t4,Tau,t5,Upsilon,t6,Phi,t7,Chi,t8,Psi,' +
|
| 90 |
+
't9,Omega,th,alpha,ti,beta,tj,gamma,tk,delta,tl,epsilon,tm,zeta,tn,eta,to,theta,tp,iota,' +
|
| 91 |
+
'tq,kappa,tr,lambda,ts,mu,tt,nu,tu,xi,tv,omicron,u0,pi,u1,rho,u2,sigmaf,u3,sigma,u4,tau,' +
|
| 92 |
+
'u5,upsilon,u6,phi,u7,chi,u8,psi,u9,omega,uh,thetasym,ui,upsih,um,piv,812,bull,816,hellip,' +
|
| 93 |
+
'81i,prime,81j,Prime,81u,oline,824,frasl,88o,weierp,88h,image,88s,real,892,trade,89l,alefsym,' +
|
| 94 |
+
'8cg,larr,8ch,uarr,8ci,rarr,8cj,darr,8ck,harr,8dl,crarr,8eg,lArr,8eh,uArr,8ei,rArr,8ej,dArr,' +
|
| 95 |
+
'8ek,hArr,8g0,forall,8g2,part,8g3,exist,8g5,empty,8g7,nabla,8g8,isin,8g9,notin,8gb,ni,8gf,prod,' +
|
| 96 |
+
'8gh,sum,8gi,minus,8gn,lowast,8gq,radic,8gt,prop,8gu,infin,8h0,ang,8h7,and,8h8,or,8h9,cap,8ha,cup,' +
|
| 97 |
+
'8hb,int,8hk,there4,8hs,sim,8i5,cong,8i8,asymp,8j0,ne,8j1,equiv,8j4,le,8j5,ge,8k2,sub,8k3,sup,8k4,' +
|
| 98 |
+
'nsub,8k6,sube,8k7,supe,8kl,oplus,8kn,otimes,8l5,perp,8m5,sdot,8o8,lceil,8o9,rceil,8oa,lfloor,8ob,' +
|
| 99 |
+
'rfloor,8p9,lang,8pa,rang,9ea,loz,9j0,spades,9j3,clubs,9j5,hearts,9j6,diams,ai,OElig,aj,oelig,b0,' +
|
| 100 |
+
'Scaron,b1,scaron,bo,Yuml,m6,circ,ms,tilde,802,ensp,803,emsp,809,thinsp,80c,zwnj,80d,zwj,80e,lrm,' +
|
| 101 |
+
'80f,rlm,80j,ndash,80k,mdash,80o,lsquo,80p,rsquo,80q,sbquo,80s,ldquo,80t,rdquo,80u,bdquo,810,dagger,' +
|
| 102 |
+
'811,Dagger,81g,permil,81p,lsaquo,81q,rsaquo,85c,euro', 32);
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
//Some basic mappings for attributes and default values.
|
| 106 |
+
STYLES = {
|
| 107 |
+
"strokeStyle":{
|
| 108 |
+
svgAttr : "stroke", //corresponding svg attribute
|
| 109 |
+
canvas : "#000000", //canvas default
|
| 110 |
+
svg : "none", //svg default
|
| 111 |
+
apply : "stroke" //apply on stroke() or fill()
|
| 112 |
+
},
|
| 113 |
+
"fillStyle":{
|
| 114 |
+
svgAttr : "fill",
|
| 115 |
+
canvas : "#000000",
|
| 116 |
+
svg : null, //svg default is black, but we need to special case this to handle canvas stroke without fill
|
| 117 |
+
apply : "fill"
|
| 118 |
+
},
|
| 119 |
+
"lineCap":{
|
| 120 |
+
svgAttr : "stroke-linecap",
|
| 121 |
+
canvas : "butt",
|
| 122 |
+
svg : "butt",
|
| 123 |
+
apply : "stroke"
|
| 124 |
+
},
|
| 125 |
+
"lineJoin":{
|
| 126 |
+
svgAttr : "stroke-linejoin",
|
| 127 |
+
canvas : "miter",
|
| 128 |
+
svg : "miter",
|
| 129 |
+
apply : "stroke"
|
| 130 |
+
},
|
| 131 |
+
"miterLimit":{
|
| 132 |
+
svgAttr : "stroke-miterlimit",
|
| 133 |
+
canvas : 10,
|
| 134 |
+
svg : 4,
|
| 135 |
+
apply : "stroke"
|
| 136 |
+
},
|
| 137 |
+
"lineWidth":{
|
| 138 |
+
svgAttr : "stroke-width",
|
| 139 |
+
canvas : 1,
|
| 140 |
+
svg : 1,
|
| 141 |
+
apply : "stroke"
|
| 142 |
+
},
|
| 143 |
+
"globalAlpha": {
|
| 144 |
+
svgAttr : "opacity",
|
| 145 |
+
canvas : 1,
|
| 146 |
+
svg : 1,
|
| 147 |
+
apply : "fill stroke"
|
| 148 |
+
},
|
| 149 |
+
"font":{
|
| 150 |
+
//font converts to multiple svg attributes, there is custom logic for this
|
| 151 |
+
canvas : "10px sans-serif"
|
| 152 |
+
},
|
| 153 |
+
"shadowColor":{
|
| 154 |
+
canvas : "#000000"
|
| 155 |
+
},
|
| 156 |
+
"shadowOffsetX":{
|
| 157 |
+
canvas : 0
|
| 158 |
+
},
|
| 159 |
+
"shadowOffsetY":{
|
| 160 |
+
canvas : 0
|
| 161 |
+
},
|
| 162 |
+
"shadowBlur":{
|
| 163 |
+
canvas : 0
|
| 164 |
+
},
|
| 165 |
+
"textAlign":{
|
| 166 |
+
canvas : "start"
|
| 167 |
+
},
|
| 168 |
+
"textBaseline":{
|
| 169 |
+
canvas : "alphabetic"
|
| 170 |
+
},
|
| 171 |
+
"lineDash" : {
|
| 172 |
+
svgAttr : "stroke-dasharray",
|
| 173 |
+
canvas : [],
|
| 174 |
+
svg : null,
|
| 175 |
+
apply : "stroke"
|
| 176 |
+
}
|
| 177 |
+
};
|
| 178 |
+
|
| 179 |
+
/**
|
| 180 |
+
*
|
| 181 |
+
* @param gradientNode - reference to the gradient
|
| 182 |
+
* @constructor
|
| 183 |
+
*/
|
| 184 |
+
CanvasGradient = function(gradientNode, ctx) {
|
| 185 |
+
this.__root = gradientNode;
|
| 186 |
+
this.__ctx = ctx;
|
| 187 |
+
};
|
| 188 |
+
|
| 189 |
+
/**
|
| 190 |
+
* Adds a color stop to the gradient root
|
| 191 |
+
*/
|
| 192 |
+
CanvasGradient.prototype.addColorStop = function(offset, color) {
|
| 193 |
+
var stop = this.__ctx.__createElement("stop"), regex, matches;
|
| 194 |
+
stop.setAttribute("offset", offset);
|
| 195 |
+
if(color.indexOf("rgba") !== -1) {
|
| 196 |
+
//separate alpha value, since webkit can't handle it
|
| 197 |
+
regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?\.?\d*)\s*\)/gi;
|
| 198 |
+
matches = regex.exec(color);
|
| 199 |
+
stop.setAttribute("stop-color", format("rgb({r},{g},{b})", {r:matches[1], g:matches[2], b:matches[3]}));
|
| 200 |
+
stop.setAttribute("stop-opacity", matches[4]);
|
| 201 |
+
} else {
|
| 202 |
+
stop.setAttribute("stop-color", color);
|
| 203 |
+
}
|
| 204 |
+
this.__root.appendChild(stop);
|
| 205 |
+
};
|
| 206 |
+
|
| 207 |
+
CanvasPattern = function(pattern, ctx) {
|
| 208 |
+
this.__root = pattern;
|
| 209 |
+
this.__ctx = ctx;
|
| 210 |
+
};
|
| 211 |
+
|
| 212 |
+
/**
|
| 213 |
+
* The mock canvas context
|
| 214 |
+
* @param o - options include:
|
| 215 |
+
* width - width of your canvas (defaults to 500)
|
| 216 |
+
* height - height of your canvas (defaults to 500)
|
| 217 |
+
* enableMirroring - enables canvas mirroring (get image data) (defaults to false)
|
| 218 |
+
* document - the document object (defaults to the current document)
|
| 219 |
+
*/
|
| 220 |
+
ctx = function(o) {
|
| 221 |
+
|
| 222 |
+
var defaultOptions = { width:500, height:500, enableMirroring : false}, options;
|
| 223 |
+
|
| 224 |
+
//keep support for this way of calling C2S: new C2S(width,height)
|
| 225 |
+
if(arguments.length > 1) {
|
| 226 |
+
options = defaultOptions;
|
| 227 |
+
options.width = arguments[0];
|
| 228 |
+
options.height = arguments[1];
|
| 229 |
+
} else if( !o ) {
|
| 230 |
+
options = defaultOptions;
|
| 231 |
+
} else {
|
| 232 |
+
options = o;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
if(!(this instanceof ctx)) {
|
| 236 |
+
//did someone call this without new?
|
| 237 |
+
return new ctx(options);
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
//setup options
|
| 241 |
+
this.width = options.width || defaultOptions.width;
|
| 242 |
+
this.height = options.height || defaultOptions.height;
|
| 243 |
+
this.enableMirroring = options.enableMirroring !== undefined ? options.enableMirroring : defaultOptions.enableMirroring;
|
| 244 |
+
|
| 245 |
+
this.canvas = this; ///point back to this instance!
|
| 246 |
+
this.__document = options.document || document;
|
| 247 |
+
this.__canvas = this.__document.createElement("canvas");
|
| 248 |
+
this.__ctx = this.__canvas.getContext("2d");
|
| 249 |
+
|
| 250 |
+
this.__setDefaultStyles();
|
| 251 |
+
this.__stack = [this.__getStyleState()];
|
| 252 |
+
this.__groupStack = [];
|
| 253 |
+
|
| 254 |
+
//the root svg element
|
| 255 |
+
this.__root = this.__document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
| 256 |
+
this.__root.setAttribute("version", 1.1);
|
| 257 |
+
this.__root.setAttribute("xmlns", "http://www.w3.org/2000/svg");
|
| 258 |
+
this.__root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
|
| 259 |
+
this.__root.setAttribute("width", this.width);
|
| 260 |
+
this.__root.setAttribute("height", this.height);
|
| 261 |
+
|
| 262 |
+
//make sure we don't generate the same ids in defs
|
| 263 |
+
this.__ids = {};
|
| 264 |
+
|
| 265 |
+
//defs tag
|
| 266 |
+
this.__defs = this.__document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
| 267 |
+
this.__root.appendChild(this.__defs);
|
| 268 |
+
|
| 269 |
+
//also add a group child. the svg element can't use the transform attribute
|
| 270 |
+
this.__currentElement = this.__document.createElementNS("http://www.w3.org/2000/svg", "g");
|
| 271 |
+
this.__root.appendChild(this.__currentElement);
|
| 272 |
+
};
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
/**
|
| 276 |
+
* Creates the specified svg element
|
| 277 |
+
* @private
|
| 278 |
+
*/
|
| 279 |
+
ctx.prototype.__createElement = function (elementName, properties, resetFill) {
|
| 280 |
+
if (typeof properties === "undefined") {
|
| 281 |
+
properties = {};
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
var element = this.__document.createElementNS("http://www.w3.org/2000/svg", elementName),
|
| 285 |
+
keys = Object.keys(properties), i, key;
|
| 286 |
+
if(resetFill) {
|
| 287 |
+
//if fill or stroke is not specified, the svg element should not display. By default SVG's fill is black.
|
| 288 |
+
element.setAttribute("fill", "none");
|
| 289 |
+
element.setAttribute("stroke", "none");
|
| 290 |
+
}
|
| 291 |
+
for(i=0; i<keys.length; i++) {
|
| 292 |
+
key = keys[i];
|
| 293 |
+
element.setAttribute(key, properties[key]);
|
| 294 |
+
}
|
| 295 |
+
return element;
|
| 296 |
+
};
|
| 297 |
+
|
| 298 |
+
/**
|
| 299 |
+
* Applies default canvas styles to the context
|
| 300 |
+
* @private
|
| 301 |
+
*/
|
| 302 |
+
ctx.prototype.__setDefaultStyles = function() {
|
| 303 |
+
//default 2d canvas context properties see:http://www.w3.org/TR/2dcontext/
|
| 304 |
+
var keys = Object.keys(STYLES), i, key;
|
| 305 |
+
for(i=0; i<keys.length; i++) {
|
| 306 |
+
key = keys[i];
|
| 307 |
+
this[key] = STYLES[key].canvas;
|
| 308 |
+
}
|
| 309 |
+
};
|
| 310 |
+
|
| 311 |
+
/**
|
| 312 |
+
* Applies styles on restore
|
| 313 |
+
* @param styleState
|
| 314 |
+
* @private
|
| 315 |
+
*/
|
| 316 |
+
ctx.prototype.__applyStyleState = function(styleState) {
|
| 317 |
+
var keys = Object.keys(styleState), i, key;
|
| 318 |
+
for(i=0; i<keys.length; i++) {
|
| 319 |
+
key = keys[i];
|
| 320 |
+
this[key] = styleState[key];
|
| 321 |
+
}
|
| 322 |
+
};
|
| 323 |
+
|
| 324 |
+
/**
|
| 325 |
+
* Gets the current style state
|
| 326 |
+
* @return {Object}
|
| 327 |
+
* @private
|
| 328 |
+
*/
|
| 329 |
+
ctx.prototype.__getStyleState = function() {
|
| 330 |
+
var i, styleState = {}, keys = Object.keys(STYLES), key;
|
| 331 |
+
for(i=0; i<keys.length; i++) {
|
| 332 |
+
key = keys[i];
|
| 333 |
+
styleState[key] = this[key];
|
| 334 |
+
}
|
| 335 |
+
return styleState;
|
| 336 |
+
};
|
| 337 |
+
|
| 338 |
+
/**
|
| 339 |
+
* Apples the current styles to the current SVG element. On "ctx.fill" or "ctx.stroke"
|
| 340 |
+
* @param type
|
| 341 |
+
* @private
|
| 342 |
+
*/
|
| 343 |
+
ctx.prototype.__applyStyleToCurrentElement = function(type) {
|
| 344 |
+
var keys = Object.keys(STYLES), i, style, value, id, regex, matches;
|
| 345 |
+
for(i=0; i<keys.length; i++) {
|
| 346 |
+
style = STYLES[keys[i]];
|
| 347 |
+
value = this[keys[i]];
|
| 348 |
+
if(style.apply) {
|
| 349 |
+
//is this a gradient or pattern?
|
| 350 |
+
if(style.apply.indexOf("fill")!==-1 && value instanceof CanvasPattern) {
|
| 351 |
+
//pattern
|
| 352 |
+
if(value.__ctx) {
|
| 353 |
+
//copy over defs
|
| 354 |
+
while(value.__ctx.__defs.childNodes.length) {
|
| 355 |
+
id = value.__ctx.__defs.childNodes[0].getAttribute("id");
|
| 356 |
+
this.__ids[id] = id;
|
| 357 |
+
this.__defs.appendChild(value.__ctx.__defs.childNodes[0]);
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
this.__currentElement.setAttribute("fill", format("url(#{id})", {id:value.__root.getAttribute("id")}));
|
| 361 |
+
}
|
| 362 |
+
else if(style.apply.indexOf("fill")!==-1 && value instanceof CanvasGradient) {
|
| 363 |
+
//gradient
|
| 364 |
+
this.__currentElement.setAttribute("fill", format("url(#{id})", {id:value.__root.getAttribute("id")}));
|
| 365 |
+
} else if(style.apply.indexOf(type)!==-1 && style.svg !== value) {
|
| 366 |
+
if((style.svgAttr === "stroke" || style.svgAttr === "fill") && value.indexOf("rgba") !== -1) {
|
| 367 |
+
//separate alpha value, since illustrator can't handle it
|
| 368 |
+
regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?\.?\d*)\s*\)/gi;
|
| 369 |
+
matches = regex.exec(value);
|
| 370 |
+
this.__currentElement.setAttribute(style.svgAttr, format("rgb({r},{g},{b})", {r:matches[1], g:matches[2], b:matches[3]}));
|
| 371 |
+
//should take globalAlpha here
|
| 372 |
+
var opacity = matches[4];
|
| 373 |
+
var globalAlpha = this.globalAlpha;
|
| 374 |
+
if (globalAlpha != null) {
|
| 375 |
+
opacity *= globalAlpha;
|
| 376 |
+
}
|
| 377 |
+
this.__currentElement.setAttribute(style.svgAttr+"-opacity", opacity);
|
| 378 |
+
} else {
|
| 379 |
+
var attr = style.svgAttr;
|
| 380 |
+
if (keys[i] === 'globalAlpha') {
|
| 381 |
+
attr = type+'-'+style.svgAttr;
|
| 382 |
+
if (this.__currentElement.getAttribute(attr)) {
|
| 383 |
+
//fill-opacity or stroke-opacity has already been set by stroke or fill.
|
| 384 |
+
continue;
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
//otherwise only update attribute if right type, and not svg default
|
| 388 |
+
this.__currentElement.setAttribute(attr, value);
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
}
|
| 392 |
+
}
|
| 393 |
+
}
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
};
|
| 397 |
+
|
| 398 |
+
/**
|
| 399 |
+
* Will return the closest group or svg node. May return the current element.
|
| 400 |
+
* @private
|
| 401 |
+
*/
|
| 402 |
+
ctx.prototype.__closestGroupOrSvg = function(node) {
|
| 403 |
+
node = node || this.__currentElement;
|
| 404 |
+
if(node.nodeName === "g" || node.nodeName === "svg") {
|
| 405 |
+
return node;
|
| 406 |
+
} else {
|
| 407 |
+
return this.__closestGroupOrSvg(node.parentNode);
|
| 408 |
+
}
|
| 409 |
+
};
|
| 410 |
+
|
| 411 |
+
/**
|
| 412 |
+
* Returns the serialized value of the svg so far
|
| 413 |
+
* @param fixNamedEntities - Standalone SVG doesn't support named entities, which document.createTextNode encodes.
|
| 414 |
+
* If true, we attempt to find all named entities and encode it as a numeric entity.
|
| 415 |
+
* @return serialized svg
|
| 416 |
+
*/
|
| 417 |
+
ctx.prototype.getSerializedSvg = function(fixNamedEntities) {
|
| 418 |
+
var serialized = new XMLSerializer().serializeToString(this.__root),
|
| 419 |
+
keys, i, key, value, regexp, xmlns;
|
| 420 |
+
|
| 421 |
+
//IE search for a duplicate xmnls because they didn't implement setAttributeNS correctly
|
| 422 |
+
xmlns = /xmlns="http:\/\/www\.w3\.org\/2000\/svg".+xmlns="http:\/\/www\.w3\.org\/2000\/svg/gi;
|
| 423 |
+
if(xmlns.test(serialized)) {
|
| 424 |
+
serialized = serialized.replace('xmlns="http://www.w3.org/2000/svg','xmlns:xlink="http://www.w3.org/1999/xlink');
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
if(fixNamedEntities) {
|
| 428 |
+
keys = Object.keys(namedEntities);
|
| 429 |
+
//loop over each named entity and replace with the proper equivalent.
|
| 430 |
+
for(i=0; i<keys.length; i++) {
|
| 431 |
+
key = keys[i];
|
| 432 |
+
value = namedEntities[key];
|
| 433 |
+
regexp = new RegExp(key, "gi");
|
| 434 |
+
if(regexp.test(serialized)) {
|
| 435 |
+
serialized = serialized.replace(regexp, value);
|
| 436 |
+
}
|
| 437 |
+
}
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
return serialized;
|
| 441 |
+
};
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
/**
|
| 445 |
+
* Returns the root svg
|
| 446 |
+
* @return
|
| 447 |
+
*/
|
| 448 |
+
ctx.prototype.getSvg = function() {
|
| 449 |
+
return this.__root;
|
| 450 |
+
};
|
| 451 |
+
/**
|
| 452 |
+
* Will generate a group tag.
|
| 453 |
+
*/
|
| 454 |
+
ctx.prototype.save = function() {
|
| 455 |
+
var group = this.__createElement("g"), parent = this.__closestGroupOrSvg();
|
| 456 |
+
this.__groupStack.push(parent);
|
| 457 |
+
parent.appendChild(group);
|
| 458 |
+
this.__currentElement = group;
|
| 459 |
+
this.__stack.push(this.__getStyleState());
|
| 460 |
+
};
|
| 461 |
+
/**
|
| 462 |
+
* Sets current element to parent, or just root if already root
|
| 463 |
+
*/
|
| 464 |
+
ctx.prototype.restore = function(){
|
| 465 |
+
this.__currentElement = this.__groupStack.pop();
|
| 466 |
+
//Clearing canvas will make the poped group invalid, currentElement is set to the root group node.
|
| 467 |
+
if (!this.__currentElement) {
|
| 468 |
+
this.__currentElement = this.__root.childNodes[1];
|
| 469 |
+
}
|
| 470 |
+
var state = this.__stack.pop();
|
| 471 |
+
this.__applyStyleState(state);
|
| 472 |
+
|
| 473 |
+
};
|
| 474 |
+
|
| 475 |
+
/**
|
| 476 |
+
* Helper method to add transform
|
| 477 |
+
* @private
|
| 478 |
+
*/
|
| 479 |
+
ctx.prototype.__addTransform = function(t) {
|
| 480 |
+
|
| 481 |
+
//if the current element has siblings, add another group
|
| 482 |
+
var parent = this.__closestGroupOrSvg();
|
| 483 |
+
if(parent.childNodes.length > 0) {
|
| 484 |
+
var group = this.__createElement("g");
|
| 485 |
+
parent.appendChild(group);
|
| 486 |
+
this.__currentElement = group;
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
var transform = this.__currentElement.getAttribute("transform");
|
| 490 |
+
if(transform) {
|
| 491 |
+
transform += " ";
|
| 492 |
+
} else {
|
| 493 |
+
transform = "";
|
| 494 |
+
}
|
| 495 |
+
transform += t;
|
| 496 |
+
this.__currentElement.setAttribute("transform", transform);
|
| 497 |
+
};
|
| 498 |
+
|
| 499 |
+
/**
|
| 500 |
+
* scales the current element
|
| 501 |
+
*/
|
| 502 |
+
ctx.prototype.scale = function(x, y) {
|
| 503 |
+
if(y === undefined) {
|
| 504 |
+
y = x;
|
| 505 |
+
}
|
| 506 |
+
this.__addTransform(format("scale({x},{y})", {x:x, y:y}));
|
| 507 |
+
};
|
| 508 |
+
|
| 509 |
+
/**
|
| 510 |
+
* rotates the current element
|
| 511 |
+
*/
|
| 512 |
+
ctx.prototype.rotate = function(angle){
|
| 513 |
+
var degrees = (angle * 180 / Math.PI);
|
| 514 |
+
this.__addTransform(format("rotate({angle},{cx},{cy})", {angle:degrees, cx:0, cy:0}));
|
| 515 |
+
};
|
| 516 |
+
|
| 517 |
+
/**
|
| 518 |
+
* translates the current element
|
| 519 |
+
*/
|
| 520 |
+
ctx.prototype.translate = function(x, y){
|
| 521 |
+
this.__addTransform(format("translate({x},{y})", {x:x,y:y}));
|
| 522 |
+
};
|
| 523 |
+
|
| 524 |
+
/**
|
| 525 |
+
* applies a transform to the current element
|
| 526 |
+
*/
|
| 527 |
+
ctx.prototype.transform = function(a, b, c, d, e, f){
|
| 528 |
+
this.__addTransform(format("matrix({a},{b},{c},{d},{e},{f})", {a:a, b:b, c:c, d:d, e:e, f:f}));
|
| 529 |
+
};
|
| 530 |
+
|
| 531 |
+
/**
|
| 532 |
+
* Create a new Path Element
|
| 533 |
+
*/
|
| 534 |
+
ctx.prototype.beginPath = function(){
|
| 535 |
+
var path, parent;
|
| 536 |
+
|
| 537 |
+
// Note that there is only one current default path, it is not part of the drawing state.
|
| 538 |
+
// See also: https://html.spec.whatwg.org/multipage/scripting.html#current-default-path
|
| 539 |
+
this.__currentDefaultPath = "";
|
| 540 |
+
this.__currentPosition = {};
|
| 541 |
+
|
| 542 |
+
path = this.__createElement("path", {}, true);
|
| 543 |
+
parent = this.__closestGroupOrSvg();
|
| 544 |
+
parent.appendChild(path);
|
| 545 |
+
this.__currentElement = path;
|
| 546 |
+
};
|
| 547 |
+
|
| 548 |
+
/**
|
| 549 |
+
* Helper function to apply currentDefaultPath to current path element
|
| 550 |
+
* @private
|
| 551 |
+
*/
|
| 552 |
+
ctx.prototype.__applyCurrentDefaultPath = function() {
|
| 553 |
+
if(this.__currentElement.nodeName === "path") {
|
| 554 |
+
var d = this.__currentDefaultPath;
|
| 555 |
+
this.__currentElement.setAttribute("d", d);
|
| 556 |
+
} else {
|
| 557 |
+
throw new Error("Attempted to apply path command to node " + this.__currentElement.nodeName);
|
| 558 |
+
}
|
| 559 |
+
};
|
| 560 |
+
|
| 561 |
+
/**
|
| 562 |
+
* Helper function to add path command
|
| 563 |
+
* @private
|
| 564 |
+
*/
|
| 565 |
+
ctx.prototype.__addPathCommand = function(command){
|
| 566 |
+
this.__currentDefaultPath += " ";
|
| 567 |
+
this.__currentDefaultPath += command;
|
| 568 |
+
};
|
| 569 |
+
|
| 570 |
+
/**
|
| 571 |
+
* Adds the move command to the current path element,
|
| 572 |
+
* if the currentPathElement is not empty create a new path element
|
| 573 |
+
*/
|
| 574 |
+
ctx.prototype.moveTo = function(x,y){
|
| 575 |
+
if(this.__currentElement.nodeName !== "path") {
|
| 576 |
+
this.beginPath();
|
| 577 |
+
}
|
| 578 |
+
|
| 579 |
+
// creates a new subpath with the given point
|
| 580 |
+
this.__currentPosition = {x: x, y: y};
|
| 581 |
+
this.__addPathCommand(format("M {x} {y}", {x:x, y:y}));
|
| 582 |
+
};
|
| 583 |
+
|
| 584 |
+
/**
|
| 585 |
+
* Closes the current path
|
| 586 |
+
*/
|
| 587 |
+
ctx.prototype.closePath = function(){
|
| 588 |
+
this.__addPathCommand("Z");
|
| 589 |
+
};
|
| 590 |
+
|
| 591 |
+
/**
|
| 592 |
+
* Adds a line to command
|
| 593 |
+
*/
|
| 594 |
+
ctx.prototype.lineTo = function(x, y){
|
| 595 |
+
this.__currentPosition = {x: x, y: y};
|
| 596 |
+
if (this.__currentDefaultPath.indexOf('M') > -1) {
|
| 597 |
+
this.__addPathCommand(format("L {x} {y}", {x:x, y:y}));
|
| 598 |
+
} else {
|
| 599 |
+
this.__addPathCommand(format("M {x} {y}", {x:x, y:y}));
|
| 600 |
+
}
|
| 601 |
+
};
|
| 602 |
+
|
| 603 |
+
/**
|
| 604 |
+
* Add a bezier command
|
| 605 |
+
*/
|
| 606 |
+
ctx.prototype.bezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
|
| 607 |
+
this.__currentPosition = {x: x, y: y};
|
| 608 |
+
this.__addPathCommand(format("C {cp1x} {cp1y} {cp2x} {cp2y} {x} {y}",
|
| 609 |
+
{cp1x:cp1x, cp1y:cp1y, cp2x:cp2x, cp2y:cp2y, x:x, y:y}));
|
| 610 |
+
};
|
| 611 |
+
|
| 612 |
+
/**
|
| 613 |
+
* Adds a quadratic curve to command
|
| 614 |
+
*/
|
| 615 |
+
ctx.prototype.quadraticCurveTo = function(cpx, cpy, x, y){
|
| 616 |
+
this.__currentPosition = {x: x, y: y};
|
| 617 |
+
this.__addPathCommand(format("Q {cpx} {cpy} {x} {y}", {cpx:cpx, cpy:cpy, x:x, y:y}));
|
| 618 |
+
};
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
/**
|
| 622 |
+
* Return a new normalized vector of given vector
|
| 623 |
+
*/
|
| 624 |
+
var normalize = function(vector) {
|
| 625 |
+
var len = Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1]);
|
| 626 |
+
return [vector[0] / len, vector[1] / len];
|
| 627 |
+
};
|
| 628 |
+
|
| 629 |
+
/**
|
| 630 |
+
* Adds the arcTo to the current path
|
| 631 |
+
*
|
| 632 |
+
* @see http://www.w3.org/TR/2015/WD-2dcontext-20150514/#dom-context-2d-arcto
|
| 633 |
+
*/
|
| 634 |
+
ctx.prototype.arcTo = function(x1, y1, x2, y2, radius) {
|
| 635 |
+
// Let the point (x0, y0) be the last point in the subpath.
|
| 636 |
+
var x0 = this.__currentPosition && this.__currentPosition.x;
|
| 637 |
+
var y0 = this.__currentPosition && this.__currentPosition.y;
|
| 638 |
+
|
| 639 |
+
// First ensure there is a subpath for (x1, y1).
|
| 640 |
+
if (typeof x0 == "undefined" || typeof y0 == "undefined") {
|
| 641 |
+
return;
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
// Negative values for radius must cause the implementation to throw an IndexSizeError exception.
|
| 645 |
+
if (radius < 0) {
|
| 646 |
+
throw new Error("IndexSizeError: The radius provided (" + radius + ") is negative.");
|
| 647 |
+
}
|
| 648 |
+
|
| 649 |
+
// If the point (x0, y0) is equal to the point (x1, y1),
|
| 650 |
+
// or if the point (x1, y1) is equal to the point (x2, y2),
|
| 651 |
+
// or if the radius radius is zero,
|
| 652 |
+
// then the method must add the point (x1, y1) to the subpath,
|
| 653 |
+
// and connect that point to the previous point (x0, y0) by a straight line.
|
| 654 |
+
if (((x0 === x1) && (y0 === y1))
|
| 655 |
+
|| ((x1 === x2) && (y1 === y2))
|
| 656 |
+
|| (radius === 0)) {
|
| 657 |
+
this.lineTo(x1, y1);
|
| 658 |
+
return;
|
| 659 |
+
}
|
| 660 |
+
|
| 661 |
+
// Otherwise, if the points (x0, y0), (x1, y1), and (x2, y2) all lie on a single straight line,
|
| 662 |
+
// then the method must add the point (x1, y1) to the subpath,
|
| 663 |
+
// and connect that point to the previous point (x0, y0) by a straight line.
|
| 664 |
+
var unit_vec_p1_p0 = normalize([x0 - x1, y0 - y1]);
|
| 665 |
+
var unit_vec_p1_p2 = normalize([x2 - x1, y2 - y1]);
|
| 666 |
+
if (unit_vec_p1_p0[0] * unit_vec_p1_p2[1] === unit_vec_p1_p0[1] * unit_vec_p1_p2[0]) {
|
| 667 |
+
this.lineTo(x1, y1);
|
| 668 |
+
return;
|
| 669 |
+
}
|
| 670 |
+
|
| 671 |
+
// Otherwise, let The Arc be the shortest arc given by circumference of the circle that has radius radius,
|
| 672 |
+
// and that has one point tangent to the half-infinite line that crosses the point (x0, y0) and ends at the point (x1, y1),
|
| 673 |
+
// and that has a different point tangent to the half-infinite line that ends at the point (x1, y1), and crosses the point (x2, y2).
|
| 674 |
+
// The points at which this circle touches these two lines are called the start and end tangent points respectively.
|
| 675 |
+
|
| 676 |
+
// note that both vectors are unit vectors, so the length is 1
|
| 677 |
+
var cos = (unit_vec_p1_p0[0] * unit_vec_p1_p2[0] + unit_vec_p1_p0[1] * unit_vec_p1_p2[1]);
|
| 678 |
+
var theta = Math.acos(Math.abs(cos));
|
| 679 |
+
|
| 680 |
+
// Calculate origin
|
| 681 |
+
var unit_vec_p1_origin = normalize([
|
| 682 |
+
unit_vec_p1_p0[0] + unit_vec_p1_p2[0],
|
| 683 |
+
unit_vec_p1_p0[1] + unit_vec_p1_p2[1]
|
| 684 |
+
]);
|
| 685 |
+
var len_p1_origin = radius / Math.sin(theta / 2);
|
| 686 |
+
var x = x1 + len_p1_origin * unit_vec_p1_origin[0];
|
| 687 |
+
var y = y1 + len_p1_origin * unit_vec_p1_origin[1];
|
| 688 |
+
|
| 689 |
+
// Calculate start angle and end angle
|
| 690 |
+
// rotate 90deg clockwise (note that y axis points to its down)
|
| 691 |
+
var unit_vec_origin_start_tangent = [
|
| 692 |
+
-unit_vec_p1_p0[1],
|
| 693 |
+
unit_vec_p1_p0[0]
|
| 694 |
+
];
|
| 695 |
+
// rotate 90deg counter clockwise (note that y axis points to its down)
|
| 696 |
+
var unit_vec_origin_end_tangent = [
|
| 697 |
+
unit_vec_p1_p2[1],
|
| 698 |
+
-unit_vec_p1_p2[0]
|
| 699 |
+
];
|
| 700 |
+
var getAngle = function(vector) {
|
| 701 |
+
// get angle (clockwise) between vector and (1, 0)
|
| 702 |
+
var x = vector[0];
|
| 703 |
+
var y = vector[1];
|
| 704 |
+
if (y >= 0) { // note that y axis points to its down
|
| 705 |
+
return Math.acos(x);
|
| 706 |
+
} else {
|
| 707 |
+
return -Math.acos(x);
|
| 708 |
+
}
|
| 709 |
+
};
|
| 710 |
+
var startAngle = getAngle(unit_vec_origin_start_tangent);
|
| 711 |
+
var endAngle = getAngle(unit_vec_origin_end_tangent);
|
| 712 |
+
|
| 713 |
+
// Connect the point (x0, y0) to the start tangent point by a straight line
|
| 714 |
+
this.lineTo(x + unit_vec_origin_start_tangent[0] * radius,
|
| 715 |
+
y + unit_vec_origin_start_tangent[1] * radius);
|
| 716 |
+
|
| 717 |
+
// Connect the start tangent point to the end tangent point by arc
|
| 718 |
+
// and adding the end tangent point to the subpath.
|
| 719 |
+
this.arc(x, y, radius, startAngle, endAngle);
|
| 720 |
+
};
|
| 721 |
+
|
| 722 |
+
/**
|
| 723 |
+
* Sets the stroke property on the current element
|
| 724 |
+
*/
|
| 725 |
+
ctx.prototype.stroke = function(){
|
| 726 |
+
if(this.__currentElement.nodeName === "path") {
|
| 727 |
+
this.__currentElement.setAttribute("paint-order", "fill stroke markers");
|
| 728 |
+
}
|
| 729 |
+
this.__applyCurrentDefaultPath();
|
| 730 |
+
this.__applyStyleToCurrentElement("stroke");
|
| 731 |
+
};
|
| 732 |
+
|
| 733 |
+
/**
|
| 734 |
+
* Sets fill properties on the current element
|
| 735 |
+
*/
|
| 736 |
+
ctx.prototype.fill = function(){
|
| 737 |
+
if(this.__currentElement.nodeName === "path") {
|
| 738 |
+
this.__currentElement.setAttribute("paint-order", "stroke fill markers");
|
| 739 |
+
}
|
| 740 |
+
this.__applyCurrentDefaultPath();
|
| 741 |
+
this.__applyStyleToCurrentElement("fill");
|
| 742 |
+
};
|
| 743 |
+
|
| 744 |
+
/**
|
| 745 |
+
* Adds a rectangle to the path.
|
| 746 |
+
*/
|
| 747 |
+
ctx.prototype.rect = function(x, y, width, height){
|
| 748 |
+
if(this.__currentElement.nodeName !== "path") {
|
| 749 |
+
this.beginPath();
|
| 750 |
+
}
|
| 751 |
+
this.moveTo(x, y);
|
| 752 |
+
this.lineTo(x+width, y);
|
| 753 |
+
this.lineTo(x+width, y+height);
|
| 754 |
+
this.lineTo(x, y+height);
|
| 755 |
+
this.lineTo(x, y);
|
| 756 |
+
this.closePath();
|
| 757 |
+
};
|
| 758 |
+
|
| 759 |
+
|
| 760 |
+
/**
|
| 761 |
+
* adds a rectangle element
|
| 762 |
+
*/
|
| 763 |
+
ctx.prototype.fillRect = function(x, y, width, height){
|
| 764 |
+
var rect, parent;
|
| 765 |
+
rect = this.__createElement("rect", {
|
| 766 |
+
x : x,
|
| 767 |
+
y : y,
|
| 768 |
+
width : width,
|
| 769 |
+
height : height
|
| 770 |
+
}, true);
|
| 771 |
+
parent = this.__closestGroupOrSvg();
|
| 772 |
+
parent.appendChild(rect);
|
| 773 |
+
this.__currentElement = rect;
|
| 774 |
+
this.__applyStyleToCurrentElement("fill");
|
| 775 |
+
};
|
| 776 |
+
|
| 777 |
+
/**
|
| 778 |
+
* Draws a rectangle with no fill
|
| 779 |
+
* @param x
|
| 780 |
+
* @param y
|
| 781 |
+
* @param width
|
| 782 |
+
* @param height
|
| 783 |
+
*/
|
| 784 |
+
ctx.prototype.strokeRect = function(x, y, width, height){
|
| 785 |
+
var rect, parent;
|
| 786 |
+
rect = this.__createElement("rect", {
|
| 787 |
+
x : x,
|
| 788 |
+
y : y,
|
| 789 |
+
width : width,
|
| 790 |
+
height : height
|
| 791 |
+
}, true);
|
| 792 |
+
parent = this.__closestGroupOrSvg();
|
| 793 |
+
parent.appendChild(rect);
|
| 794 |
+
this.__currentElement = rect;
|
| 795 |
+
this.__applyStyleToCurrentElement("stroke");
|
| 796 |
+
};
|
| 797 |
+
|
| 798 |
+
|
| 799 |
+
/**
|
| 800 |
+
* Clear entire canvas:
|
| 801 |
+
* 1. save current transforms
|
| 802 |
+
* 2. remove all the childNodes of the root g element
|
| 803 |
+
*/
|
| 804 |
+
ctx.prototype.__clearCanvas = function() {
|
| 805 |
+
var current = this.__closestGroupOrSvg(),
|
| 806 |
+
transform = current.getAttribute("transform");
|
| 807 |
+
var rootGroup = this.__root.childNodes[1];
|
| 808 |
+
var childNodes = rootGroup.childNodes;
|
| 809 |
+
for (var i = childNodes.length - 1; i >= 0; i--) {
|
| 810 |
+
if (childNodes[i]) {
|
| 811 |
+
rootGroup.removeChild(childNodes[i]);
|
| 812 |
+
}
|
| 813 |
+
}
|
| 814 |
+
this.__currentElement = rootGroup;
|
| 815 |
+
//reset __groupStack as all the child group nodes are all removed.
|
| 816 |
+
this.__groupStack = [];
|
| 817 |
+
if (transform) {
|
| 818 |
+
this.__addTransform(transform);
|
| 819 |
+
}
|
| 820 |
+
};
|
| 821 |
+
|
| 822 |
+
/**
|
| 823 |
+
* "Clears" a canvas by just drawing a white rectangle in the current group.
|
| 824 |
+
*/
|
| 825 |
+
ctx.prototype.clearRect = function(x, y, width, height) {
|
| 826 |
+
//clear entire canvas
|
| 827 |
+
if (x === 0 && y === 0 && width === this.width && height === this.height) {
|
| 828 |
+
this.__clearCanvas();
|
| 829 |
+
return;
|
| 830 |
+
}
|
| 831 |
+
var rect, parent = this.__closestGroupOrSvg();
|
| 832 |
+
rect = this.__createElement("rect", {
|
| 833 |
+
x : x,
|
| 834 |
+
y : y,
|
| 835 |
+
width : width,
|
| 836 |
+
height : height,
|
| 837 |
+
fill : "#FFFFFF"
|
| 838 |
+
}, true);
|
| 839 |
+
parent.appendChild(rect);
|
| 840 |
+
};
|
| 841 |
+
|
| 842 |
+
/**
|
| 843 |
+
* Adds a linear gradient to a defs tag.
|
| 844 |
+
* Returns a canvas gradient object that has a reference to it's parent def
|
| 845 |
+
*/
|
| 846 |
+
ctx.prototype.createLinearGradient = function(x1, y1, x2, y2){
|
| 847 |
+
var grad = this.__createElement("linearGradient", {
|
| 848 |
+
id : randomString(this.__ids),
|
| 849 |
+
x1 : x1+"px",
|
| 850 |
+
x2 : x2+"px",
|
| 851 |
+
y1 : y1+"px",
|
| 852 |
+
y2 : y2+"px",
|
| 853 |
+
"gradientUnits" : "userSpaceOnUse"
|
| 854 |
+
}, false);
|
| 855 |
+
this.__defs.appendChild(grad);
|
| 856 |
+
return new CanvasGradient(grad, this);
|
| 857 |
+
};
|
| 858 |
+
|
| 859 |
+
/**
|
| 860 |
+
* Adds a radial gradient to a defs tag.
|
| 861 |
+
* Returns a canvas gradient object that has a reference to it's parent def
|
| 862 |
+
*/
|
| 863 |
+
ctx.prototype.createRadialGradient = function(x0, y0, r0, x1, y1, r1){
|
| 864 |
+
var grad = this.__createElement("radialGradient", {
|
| 865 |
+
id : randomString(this.__ids),
|
| 866 |
+
cx : x1+"px",
|
| 867 |
+
cy : y1+"px",
|
| 868 |
+
r : r1+"px",
|
| 869 |
+
fx : x0+"px",
|
| 870 |
+
fy : y0+"px",
|
| 871 |
+
"gradientUnits" : "userSpaceOnUse"
|
| 872 |
+
}, false);
|
| 873 |
+
this.__defs.appendChild(grad);
|
| 874 |
+
return new CanvasGradient(grad, this);
|
| 875 |
+
|
| 876 |
+
};
|
| 877 |
+
|
| 878 |
+
/**
|
| 879 |
+
* Parses the font string and returns svg mapping
|
| 880 |
+
* @private
|
| 881 |
+
*/
|
| 882 |
+
ctx.prototype.__parseFont = function() {
|
| 883 |
+
var regex = /^\s*(?=(?:(?:[-a-z]+\s*){0,2}(italic|oblique))?)(?=(?:(?:[-a-z]+\s*){0,2}(small-caps))?)(?=(?:(?:[-a-z]+\s*){0,2}(bold(?:er)?|lighter|[1-9]00))?)(?:(?:normal|\1|\2|\3)\s*){0,3}((?:xx?-)?(?:small|large)|medium|smaller|larger|[.\d]+(?:\%|in|[cem]m|ex|p[ctx]))(?:\s*\/\s*(normal|[.\d]+(?:\%|in|[cem]m|ex|p[ctx])))?\s*([-,\'\"\sa-z]+?)\s*$/i;
|
| 884 |
+
var fontPart = regex.exec( this.font );
|
| 885 |
+
var data = {
|
| 886 |
+
style : fontPart[1] || 'normal',
|
| 887 |
+
size : fontPart[4] || '10px',
|
| 888 |
+
family : fontPart[6] || 'sans-serif',
|
| 889 |
+
weight: fontPart[3] || 'normal',
|
| 890 |
+
decoration : fontPart[2] || 'normal',
|
| 891 |
+
href : null
|
| 892 |
+
};
|
| 893 |
+
|
| 894 |
+
//canvas doesn't support underline natively, but we can pass this attribute
|
| 895 |
+
if(this.__fontUnderline === "underline") {
|
| 896 |
+
data.decoration = "underline";
|
| 897 |
+
}
|
| 898 |
+
|
| 899 |
+
//canvas also doesn't support linking, but we can pass this as well
|
| 900 |
+
if(this.__fontHref) {
|
| 901 |
+
data.href = this.__fontHref;
|
| 902 |
+
}
|
| 903 |
+
|
| 904 |
+
return data;
|
| 905 |
+
};
|
| 906 |
+
|
| 907 |
+
/**
|
| 908 |
+
* Helper to link text fragments
|
| 909 |
+
* @param font
|
| 910 |
+
* @param element
|
| 911 |
+
* @return {*}
|
| 912 |
+
* @private
|
| 913 |
+
*/
|
| 914 |
+
ctx.prototype.__wrapTextLink = function(font, element) {
|
| 915 |
+
if(font.href) {
|
| 916 |
+
var a = this.__createElement("a");
|
| 917 |
+
a.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", font.href);
|
| 918 |
+
a.appendChild(element);
|
| 919 |
+
return a;
|
| 920 |
+
}
|
| 921 |
+
return element;
|
| 922 |
+
};
|
| 923 |
+
|
| 924 |
+
/**
|
| 925 |
+
* Fills or strokes text
|
| 926 |
+
* @param text
|
| 927 |
+
* @param x
|
| 928 |
+
* @param y
|
| 929 |
+
* @param action - stroke or fill
|
| 930 |
+
* @private
|
| 931 |
+
*/
|
| 932 |
+
ctx.prototype.__applyText = function(text, x, y, action) {
|
| 933 |
+
var font = this.__parseFont(),
|
| 934 |
+
parent = this.__closestGroupOrSvg(),
|
| 935 |
+
textElement = this.__createElement("text", {
|
| 936 |
+
"font-family" : font.family,
|
| 937 |
+
"font-size" : font.size,
|
| 938 |
+
"font-style" : font.style,
|
| 939 |
+
"font-weight" : font.weight,
|
| 940 |
+
"text-decoration" : font.decoration,
|
| 941 |
+
"x" : x,
|
| 942 |
+
"y" : y,
|
| 943 |
+
"text-anchor": getTextAnchor(this.textAlign),
|
| 944 |
+
"dominant-baseline": getDominantBaseline(this.textBaseline)
|
| 945 |
+
}, true);
|
| 946 |
+
|
| 947 |
+
textElement.appendChild(this.__document.createTextNode(text));
|
| 948 |
+
this.__currentElement = textElement;
|
| 949 |
+
this.__applyStyleToCurrentElement(action);
|
| 950 |
+
parent.appendChild(this.__wrapTextLink(font,textElement));
|
| 951 |
+
};
|
| 952 |
+
|
| 953 |
+
/**
|
| 954 |
+
* Creates a text element
|
| 955 |
+
* @param text
|
| 956 |
+
* @param x
|
| 957 |
+
* @param y
|
| 958 |
+
*/
|
| 959 |
+
ctx.prototype.fillText = function(text, x, y){
|
| 960 |
+
this.__applyText(text, x, y, "fill");
|
| 961 |
+
};
|
| 962 |
+
|
| 963 |
+
/**
|
| 964 |
+
* Strokes text
|
| 965 |
+
* @param text
|
| 966 |
+
* @param x
|
| 967 |
+
* @param y
|
| 968 |
+
*/
|
| 969 |
+
ctx.prototype.strokeText = function(text, x, y){
|
| 970 |
+
this.__applyText(text, x, y, "stroke");
|
| 971 |
+
};
|
| 972 |
+
|
| 973 |
+
/**
|
| 974 |
+
* No need to implement this for svg.
|
| 975 |
+
* @param text
|
| 976 |
+
* @return {TextMetrics}
|
| 977 |
+
*/
|
| 978 |
+
ctx.prototype.measureText = function(text){
|
| 979 |
+
this.__ctx.font = this.font;
|
| 980 |
+
return this.__ctx.measureText(text);
|
| 981 |
+
};
|
| 982 |
+
|
| 983 |
+
/**
|
| 984 |
+
* Arc command!
|
| 985 |
+
*/
|
| 986 |
+
ctx.prototype.arc = function(x, y, radius, startAngle, endAngle, counterClockwise) {
|
| 987 |
+
// in canvas no circle is drawn if no angle is provided.
|
| 988 |
+
if (startAngle === endAngle) {
|
| 989 |
+
return;
|
| 990 |
+
}
|
| 991 |
+
startAngle = startAngle % (2*Math.PI);
|
| 992 |
+
endAngle = endAngle % (2*Math.PI);
|
| 993 |
+
if(startAngle === endAngle) {
|
| 994 |
+
//circle time! subtract some of the angle so svg is happy (svg elliptical arc can't draw a full circle)
|
| 995 |
+
endAngle = ((endAngle + (2*Math.PI)) - 0.001 * (counterClockwise ? -1 : 1)) % (2*Math.PI);
|
| 996 |
+
}
|
| 997 |
+
var endX = x+radius*Math.cos(endAngle),
|
| 998 |
+
endY = y+radius*Math.sin(endAngle),
|
| 999 |
+
startX = x+radius*Math.cos(startAngle),
|
| 1000 |
+
startY = y+radius*Math.sin(startAngle),
|
| 1001 |
+
sweepFlag = counterClockwise ? 0 : 1,
|
| 1002 |
+
largeArcFlag = 0,
|
| 1003 |
+
diff = endAngle - startAngle;
|
| 1004 |
+
|
| 1005 |
+
// https://github.com/gliffy/canvas2svg/issues/4
|
| 1006 |
+
if(diff < 0) {
|
| 1007 |
+
diff += 2*Math.PI;
|
| 1008 |
+
}
|
| 1009 |
+
|
| 1010 |
+
if(counterClockwise) {
|
| 1011 |
+
largeArcFlag = diff > Math.PI ? 0 : 1;
|
| 1012 |
+
} else {
|
| 1013 |
+
largeArcFlag = diff > Math.PI ? 1 : 0;
|
| 1014 |
+
}
|
| 1015 |
+
|
| 1016 |
+
this.lineTo(startX, startY);
|
| 1017 |
+
this.__addPathCommand(format("A {rx} {ry} {xAxisRotation} {largeArcFlag} {sweepFlag} {endX} {endY}",
|
| 1018 |
+
{rx:radius, ry:radius, xAxisRotation:0, largeArcFlag:largeArcFlag, sweepFlag:sweepFlag, endX:endX, endY:endY}));
|
| 1019 |
+
|
| 1020 |
+
this.__currentPosition = {x: endX, y: endY};
|
| 1021 |
+
};
|
| 1022 |
+
|
| 1023 |
+
/**
|
| 1024 |
+
* Generates a ClipPath from the clip command.
|
| 1025 |
+
*/
|
| 1026 |
+
ctx.prototype.clip = function(){
|
| 1027 |
+
var group = this.__closestGroupOrSvg(),
|
| 1028 |
+
clipPath = this.__createElement("clipPath"),
|
| 1029 |
+
id = randomString(this.__ids),
|
| 1030 |
+
newGroup = this.__createElement("g");
|
| 1031 |
+
|
| 1032 |
+
this.__applyCurrentDefaultPath();
|
| 1033 |
+
group.removeChild(this.__currentElement);
|
| 1034 |
+
clipPath.setAttribute("id", id);
|
| 1035 |
+
clipPath.appendChild(this.__currentElement);
|
| 1036 |
+
|
| 1037 |
+
this.__defs.appendChild(clipPath);
|
| 1038 |
+
|
| 1039 |
+
//set the clip path to this group
|
| 1040 |
+
group.setAttribute("clip-path", format("url(#{id})", {id:id}));
|
| 1041 |
+
|
| 1042 |
+
//clip paths can be scaled and transformed, we need to add another wrapper group to avoid later transformations
|
| 1043 |
+
// to this path
|
| 1044 |
+
group.appendChild(newGroup);
|
| 1045 |
+
|
| 1046 |
+
this.__currentElement = newGroup;
|
| 1047 |
+
|
| 1048 |
+
};
|
| 1049 |
+
|
| 1050 |
+
/**
|
| 1051 |
+
* Draws a canvas, image or mock context to this canvas.
|
| 1052 |
+
* Note that all svg dom manipulation uses node.childNodes rather than node.children for IE support.
|
| 1053 |
+
* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage
|
| 1054 |
+
*/
|
| 1055 |
+
ctx.prototype.drawImage = function(){
|
| 1056 |
+
//convert arguments to a real array
|
| 1057 |
+
var args = Array.prototype.slice.call(arguments),
|
| 1058 |
+
image=args[0],
|
| 1059 |
+
dx, dy, dw, dh, sx=0, sy=0, sw, sh, parent, svg, defs, group,
|
| 1060 |
+
currentElement, svgImage, canvas, context, id;
|
| 1061 |
+
|
| 1062 |
+
if(args.length === 3) {
|
| 1063 |
+
dx = args[1];
|
| 1064 |
+
dy = args[2];
|
| 1065 |
+
sw = image.width;
|
| 1066 |
+
sh = image.height;
|
| 1067 |
+
dw = sw;
|
| 1068 |
+
dh = sh;
|
| 1069 |
+
} else if(args.length === 5) {
|
| 1070 |
+
dx = args[1];
|
| 1071 |
+
dy = args[2];
|
| 1072 |
+
dw = args[3];
|
| 1073 |
+
dh = args[4];
|
| 1074 |
+
sw = image.width;
|
| 1075 |
+
sh = image.height;
|
| 1076 |
+
} else if(args.length === 9) {
|
| 1077 |
+
sx = args[1];
|
| 1078 |
+
sy = args[2];
|
| 1079 |
+
sw = args[3];
|
| 1080 |
+
sh = args[4];
|
| 1081 |
+
dx = args[5];
|
| 1082 |
+
dy = args[6];
|
| 1083 |
+
dw = args[7];
|
| 1084 |
+
dh = args[8];
|
| 1085 |
+
} else {
|
| 1086 |
+
throw new Error("Inavlid number of arguments passed to drawImage: " + arguments.length);
|
| 1087 |
+
}
|
| 1088 |
+
|
| 1089 |
+
parent = this.__closestGroupOrSvg();
|
| 1090 |
+
currentElement = this.__currentElement;
|
| 1091 |
+
var translateDirective = "translate(" + dx + ", " + dy + ")";
|
| 1092 |
+
if(image instanceof ctx) {
|
| 1093 |
+
//canvas2svg mock canvas context. In the future we may want to clone nodes instead.
|
| 1094 |
+
//also I'm currently ignoring dw, dh, sw, sh, sx, sy for a mock context.
|
| 1095 |
+
svg = image.getSvg().cloneNode(true);
|
| 1096 |
+
if (svg.childNodes && svg.childNodes.length > 1) {
|
| 1097 |
+
defs = svg.childNodes[0];
|
| 1098 |
+
while(defs.childNodes.length) {
|
| 1099 |
+
id = defs.childNodes[0].getAttribute("id");
|
| 1100 |
+
this.__ids[id] = id;
|
| 1101 |
+
this.__defs.appendChild(defs.childNodes[0]);
|
| 1102 |
+
}
|
| 1103 |
+
group = svg.childNodes[1];
|
| 1104 |
+
if (group) {
|
| 1105 |
+
//save original transform
|
| 1106 |
+
var originTransform = group.getAttribute("transform");
|
| 1107 |
+
var transformDirective;
|
| 1108 |
+
if (originTransform) {
|
| 1109 |
+
transformDirective = originTransform+" "+translateDirective;
|
| 1110 |
+
} else {
|
| 1111 |
+
transformDirective = translateDirective;
|
| 1112 |
+
}
|
| 1113 |
+
group.setAttribute("transform", transformDirective);
|
| 1114 |
+
parent.appendChild(group);
|
| 1115 |
+
}
|
| 1116 |
+
}
|
| 1117 |
+
} else if(image.nodeName === "CANVAS" || image.nodeName === "IMG") {
|
| 1118 |
+
//canvas or image
|
| 1119 |
+
svgImage = this.__createElement("image");
|
| 1120 |
+
svgImage.setAttribute("width", dw);
|
| 1121 |
+
svgImage.setAttribute("height", dh);
|
| 1122 |
+
svgImage.setAttribute("preserveAspectRatio", "none");
|
| 1123 |
+
|
| 1124 |
+
if(sx || sy || sw !== image.width || sh !== image.height) {
|
| 1125 |
+
//crop the image using a temporary canvas
|
| 1126 |
+
canvas = this.__document.createElement("canvas");
|
| 1127 |
+
canvas.width = dw;
|
| 1128 |
+
canvas.height = dh;
|
| 1129 |
+
context = canvas.getContext("2d");
|
| 1130 |
+
context.drawImage(image, sx, sy, sw, sh, 0, 0, dw, dh);
|
| 1131 |
+
image = canvas;
|
| 1132 |
+
}
|
| 1133 |
+
svgImage.setAttribute("transform", translateDirective);
|
| 1134 |
+
svgImage.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href",
|
| 1135 |
+
image.nodeName === "CANVAS" ? image.toDataURL() : image.getAttribute("src"));
|
| 1136 |
+
parent.appendChild(svgImage);
|
| 1137 |
+
}
|
| 1138 |
+
};
|
| 1139 |
+
|
| 1140 |
+
/**
|
| 1141 |
+
* Generates a pattern tag
|
| 1142 |
+
*/
|
| 1143 |
+
ctx.prototype.createPattern = function(image, repetition){
|
| 1144 |
+
var pattern = this.__document.createElementNS("http://www.w3.org/2000/svg", "pattern"), id = randomString(this.__ids),
|
| 1145 |
+
img;
|
| 1146 |
+
pattern.setAttribute("id", id);
|
| 1147 |
+
pattern.setAttribute("width", image.width);
|
| 1148 |
+
pattern.setAttribute("height", image.height);
|
| 1149 |
+
if(image.nodeName === "CANVAS" || image.nodeName === "IMG") {
|
| 1150 |
+
img = this.__document.createElementNS("http://www.w3.org/2000/svg", "image");
|
| 1151 |
+
img.setAttribute("width", image.width);
|
| 1152 |
+
img.setAttribute("height", image.height);
|
| 1153 |
+
img.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href",
|
| 1154 |
+
image.nodeName === "CANVAS" ? image.toDataURL() : image.getAttribute("src"));
|
| 1155 |
+
pattern.appendChild(img);
|
| 1156 |
+
this.__defs.appendChild(pattern);
|
| 1157 |
+
} else if(image instanceof ctx) {
|
| 1158 |
+
pattern.appendChild(image.__root.childNodes[1]);
|
| 1159 |
+
this.__defs.appendChild(pattern);
|
| 1160 |
+
}
|
| 1161 |
+
return new CanvasPattern(pattern, this);
|
| 1162 |
+
};
|
| 1163 |
+
|
| 1164 |
+
ctx.prototype.setLineDash = function(dashArray) {
|
| 1165 |
+
if (dashArray && dashArray.length > 0) {
|
| 1166 |
+
this.lineDash = dashArray.join(",");
|
| 1167 |
+
} else {
|
| 1168 |
+
this.lineDash = null;
|
| 1169 |
+
}
|
| 1170 |
+
};
|
| 1171 |
+
|
| 1172 |
+
/**
|
| 1173 |
+
* Not yet implemented
|
| 1174 |
+
*/
|
| 1175 |
+
ctx.prototype.drawFocusRing = function(){};
|
| 1176 |
+
ctx.prototype.createImageData = function(){};
|
| 1177 |
+
ctx.prototype.getImageData = function(){};
|
| 1178 |
+
ctx.prototype.putImageData = function(){};
|
| 1179 |
+
ctx.prototype.globalCompositeOperation = function(){};
|
| 1180 |
+
ctx.prototype.setTransform = function(){};
|
| 1181 |
+
|
| 1182 |
+
//add options for alternative namespace
|
| 1183 |
+
if (typeof window === "object") {
|
| 1184 |
+
window.C2S = ctx;
|
| 1185 |
+
}
|
| 1186 |
+
|
| 1187 |
+
// CommonJS/Browserify
|
| 1188 |
+
if (typeof module === "object" && typeof module.exports === "object") {
|
| 1189 |
+
module.exports = ctx;
|
| 1190 |
+
}
|
| 1191 |
+
|
| 1192 |
+
}());
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/favicon-active.ico
ADDED
|
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/favicon.ico
ADDED
|
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/no-image.png
ADDED
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/notify.mp3
ADDED
|
Binary file (74.4 kB). View file
|
|
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/autocompleter.js
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { app } from "../../../scripts/app.js";
|
| 2 |
+
import { ComfyWidgets } from "../../../scripts/widgets.js";
|
| 3 |
+
import { api } from "../../../scripts/api.js";
|
| 4 |
+
import { $el, ComfyDialog } from "../../../scripts/ui.js";
|
| 5 |
+
import { TextAreaAutoComplete } from "./common/autocomplete.js";
|
| 6 |
+
import { ModelInfoDialog } from "./common/modelInfoDialog.js";
|
| 7 |
+
import { LoraInfoDialog } from "./modelInfo.js";
|
| 8 |
+
|
| 9 |
+
function parseCSV(csvText) {
|
| 10 |
+
const rows = [];
|
| 11 |
+
const delimiter = ",";
|
| 12 |
+
const quote = '"';
|
| 13 |
+
let currentField = "";
|
| 14 |
+
let inQuotedField = false;
|
| 15 |
+
|
| 16 |
+
function pushField() {
|
| 17 |
+
rows[rows.length - 1].push(currentField);
|
| 18 |
+
currentField = "";
|
| 19 |
+
inQuotedField = false;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
rows.push([]); // Initialize the first row
|
| 23 |
+
|
| 24 |
+
for (let i = 0; i < csvText.length; i++) {
|
| 25 |
+
const char = csvText[i];
|
| 26 |
+
const nextChar = csvText[i + 1];
|
| 27 |
+
|
| 28 |
+
// Special handling for backslash escaped quotes
|
| 29 |
+
if (char === "\\" && nextChar === quote) {
|
| 30 |
+
currentField += quote;
|
| 31 |
+
i++;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if (!inQuotedField) {
|
| 35 |
+
if (char === quote) {
|
| 36 |
+
inQuotedField = true;
|
| 37 |
+
} else if (char === delimiter) {
|
| 38 |
+
pushField();
|
| 39 |
+
} else if (char === "\r" || char === "\n" || i === csvText.length - 1) {
|
| 40 |
+
pushField();
|
| 41 |
+
if (nextChar === "\n") {
|
| 42 |
+
i++; // Handle Windows line endings (\r\n)
|
| 43 |
+
}
|
| 44 |
+
rows.push([]); // Start a new row
|
| 45 |
+
} else {
|
| 46 |
+
currentField += char;
|
| 47 |
+
}
|
| 48 |
+
} else {
|
| 49 |
+
if (char === quote && nextChar === quote) {
|
| 50 |
+
currentField += quote;
|
| 51 |
+
i++; // Skip the next quote
|
| 52 |
+
} else if (char === quote) {
|
| 53 |
+
inQuotedField = false;
|
| 54 |
+
} else if (char === "\r" || char === "\n" || i === csvText.length - 1) {
|
| 55 |
+
// Dont allow new lines in quoted text, assume its wrong
|
| 56 |
+
const parsed = parseCSV(currentField);
|
| 57 |
+
rows.pop();
|
| 58 |
+
rows.push(...parsed);
|
| 59 |
+
inQuotedField = false;
|
| 60 |
+
currentField = "";
|
| 61 |
+
rows.push([]);
|
| 62 |
+
} else {
|
| 63 |
+
currentField += char;
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
if (currentField || csvText[csvText.length - 1] === ",") {
|
| 69 |
+
pushField();
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// Remove the last row if it's empty
|
| 73 |
+
if (rows[rows.length - 1].length === 0) {
|
| 74 |
+
rows.pop();
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
return rows;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
async function getCustomWords() {
|
| 81 |
+
const resp = await api.fetchApi("/pysssss/autocomplete", { cache: "no-store" });
|
| 82 |
+
if (resp.status === 200) {
|
| 83 |
+
return await resp.text();
|
| 84 |
+
}
|
| 85 |
+
return undefined;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
async function addCustomWords(text) {
|
| 89 |
+
if (!text) {
|
| 90 |
+
text = await getCustomWords();
|
| 91 |
+
}
|
| 92 |
+
if (text) {
|
| 93 |
+
TextAreaAutoComplete.updateWords(
|
| 94 |
+
"pysssss.customwords",
|
| 95 |
+
parseCSV(text).reduce((p, n) => {
|
| 96 |
+
let text;
|
| 97 |
+
let priority;
|
| 98 |
+
let value;
|
| 99 |
+
let num;
|
| 100 |
+
switch (n.length) {
|
| 101 |
+
case 0:
|
| 102 |
+
return;
|
| 103 |
+
case 1:
|
| 104 |
+
// Single word
|
| 105 |
+
text = n[0];
|
| 106 |
+
break;
|
| 107 |
+
case 2:
|
| 108 |
+
// Word,[priority|alias]
|
| 109 |
+
num = +n[1];
|
| 110 |
+
if (isNaN(num)) {
|
| 111 |
+
text = n[0] + "🔄️" + n[1];
|
| 112 |
+
value = n[0];
|
| 113 |
+
} else {
|
| 114 |
+
text = n[0];
|
| 115 |
+
priority = num;
|
| 116 |
+
}
|
| 117 |
+
break;
|
| 118 |
+
case 4:
|
| 119 |
+
// a1111 csv format?
|
| 120 |
+
value = n[0];
|
| 121 |
+
priority = +n[2];
|
| 122 |
+
const aliases = n[3]?.trim();
|
| 123 |
+
if (aliases && aliases !== "null") { // Weird null in an example csv, maybe they are JSON.parsing the last column?
|
| 124 |
+
const split = aliases.split(",");
|
| 125 |
+
for (const text of split) {
|
| 126 |
+
p[text] = { text, priority, value };
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
text = value;
|
| 130 |
+
break;
|
| 131 |
+
default:
|
| 132 |
+
// Word,alias,priority
|
| 133 |
+
text = n[1];
|
| 134 |
+
value = n[0];
|
| 135 |
+
priority = +n[2];
|
| 136 |
+
break;
|
| 137 |
+
}
|
| 138 |
+
p[text] = { text, priority, value };
|
| 139 |
+
return p;
|
| 140 |
+
}, {})
|
| 141 |
+
);
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
function toggleLoras() {
|
| 146 |
+
[TextAreaAutoComplete.globalWords, TextAreaAutoComplete.globalWordsExclLoras] = [
|
| 147 |
+
TextAreaAutoComplete.globalWordsExclLoras,
|
| 148 |
+
TextAreaAutoComplete.globalWords,
|
| 149 |
+
];
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
class EmbeddingInfoDialog extends ModelInfoDialog {
|
| 153 |
+
async addInfo() {
|
| 154 |
+
super.addInfo();
|
| 155 |
+
const info = await this.addCivitaiInfo();
|
| 156 |
+
if (info) {
|
| 157 |
+
$el("div", {
|
| 158 |
+
parent: this.content,
|
| 159 |
+
innerHTML: info.description,
|
| 160 |
+
style: {
|
| 161 |
+
maxHeight: "250px",
|
| 162 |
+
overflow: "auto",
|
| 163 |
+
},
|
| 164 |
+
});
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
class CustomWordsDialog extends ComfyDialog {
|
| 170 |
+
async show() {
|
| 171 |
+
const text = await getCustomWords();
|
| 172 |
+
this.words = $el("textarea", {
|
| 173 |
+
textContent: text,
|
| 174 |
+
style: {
|
| 175 |
+
width: "70vw",
|
| 176 |
+
height: "70vh",
|
| 177 |
+
},
|
| 178 |
+
});
|
| 179 |
+
|
| 180 |
+
const input = $el("input", {
|
| 181 |
+
style: {
|
| 182 |
+
flex: "auto",
|
| 183 |
+
},
|
| 184 |
+
value:
|
| 185 |
+
"https://gist.githubusercontent.com/pythongosssss/1d3efa6050356a08cea975183088159a/raw/a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt",
|
| 186 |
+
});
|
| 187 |
+
|
| 188 |
+
super.show(
|
| 189 |
+
$el(
|
| 190 |
+
"div",
|
| 191 |
+
{
|
| 192 |
+
style: {
|
| 193 |
+
display: "flex",
|
| 194 |
+
flexDirection: "column",
|
| 195 |
+
overflow: "hidden",
|
| 196 |
+
maxHeight: "100%",
|
| 197 |
+
},
|
| 198 |
+
},
|
| 199 |
+
[
|
| 200 |
+
$el("h2", {
|
| 201 |
+
textContent: "Custom Autocomplete Words",
|
| 202 |
+
style: {
|
| 203 |
+
color: "#fff",
|
| 204 |
+
marginTop: 0,
|
| 205 |
+
textAlign: "center",
|
| 206 |
+
fontFamily: "sans-serif",
|
| 207 |
+
},
|
| 208 |
+
}),
|
| 209 |
+
$el(
|
| 210 |
+
"div",
|
| 211 |
+
{
|
| 212 |
+
style: {
|
| 213 |
+
color: "#fff",
|
| 214 |
+
fontFamily: "sans-serif",
|
| 215 |
+
display: "flex",
|
| 216 |
+
alignItems: "center",
|
| 217 |
+
gap: "5px",
|
| 218 |
+
},
|
| 219 |
+
},
|
| 220 |
+
[
|
| 221 |
+
$el("label", { textContent: "Load Custom List: " }),
|
| 222 |
+
input,
|
| 223 |
+
$el("button", {
|
| 224 |
+
textContent: "Load",
|
| 225 |
+
onclick: async () => {
|
| 226 |
+
try {
|
| 227 |
+
const res = await fetch(input.value);
|
| 228 |
+
if (res.status !== 200) {
|
| 229 |
+
throw new Error("Error loading: " + res.status + " " + res.statusText);
|
| 230 |
+
}
|
| 231 |
+
this.words.value = await res.text();
|
| 232 |
+
} catch (error) {
|
| 233 |
+
alert("Error loading custom list, try manually copy + pasting the list");
|
| 234 |
+
}
|
| 235 |
+
},
|
| 236 |
+
}),
|
| 237 |
+
]
|
| 238 |
+
),
|
| 239 |
+
this.words,
|
| 240 |
+
]
|
| 241 |
+
)
|
| 242 |
+
);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
createButtons() {
|
| 246 |
+
const btns = super.createButtons();
|
| 247 |
+
const save = $el("button", {
|
| 248 |
+
type: "button",
|
| 249 |
+
textContent: "Save",
|
| 250 |
+
onclick: async (e) => {
|
| 251 |
+
try {
|
| 252 |
+
const res = await api.fetchApi("/pysssss/autocomplete", { method: "POST", body: this.words.value });
|
| 253 |
+
if (res.status !== 200) {
|
| 254 |
+
throw new Error("Error saving: " + res.status + " " + res.statusText);
|
| 255 |
+
}
|
| 256 |
+
save.textContent = "Saved!";
|
| 257 |
+
addCustomWords(this.words.value);
|
| 258 |
+
setTimeout(() => {
|
| 259 |
+
save.textContent = "Save";
|
| 260 |
+
}, 500);
|
| 261 |
+
} catch (error) {
|
| 262 |
+
alert("Error saving word list!");
|
| 263 |
+
console.error(error);
|
| 264 |
+
}
|
| 265 |
+
},
|
| 266 |
+
});
|
| 267 |
+
|
| 268 |
+
btns.unshift(save);
|
| 269 |
+
return btns;
|
| 270 |
+
}
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
const id = "pysssss.AutoCompleter";
|
| 274 |
+
|
| 275 |
+
app.registerExtension({
|
| 276 |
+
name: id,
|
| 277 |
+
init() {
|
| 278 |
+
const STRING = ComfyWidgets.STRING;
|
| 279 |
+
const SKIP_WIDGETS = new Set(["ttN xyPlot.x_values", "ttN xyPlot.y_values"]);
|
| 280 |
+
ComfyWidgets.STRING = function (node, inputName, inputData) {
|
| 281 |
+
const r = STRING.apply(this, arguments);
|
| 282 |
+
|
| 283 |
+
if (inputData[1]?.multiline) {
|
| 284 |
+
// Disabled on this input
|
| 285 |
+
const config = inputData[1]?.["pysssss.autocomplete"];
|
| 286 |
+
if (config === false) return r;
|
| 287 |
+
|
| 288 |
+
// In list of widgets to skip
|
| 289 |
+
const id = `${node.comfyClass}.${inputName}`;
|
| 290 |
+
if (SKIP_WIDGETS.has(id)) return r;
|
| 291 |
+
|
| 292 |
+
let words;
|
| 293 |
+
let separator;
|
| 294 |
+
if (typeof config === "object") {
|
| 295 |
+
separator = config.separator;
|
| 296 |
+
words = {};
|
| 297 |
+
if (config.words) {
|
| 298 |
+
// Custom wordlist, this will have been registered on setup
|
| 299 |
+
Object.assign(words, TextAreaAutoComplete.groups[node.comfyClass + "." + inputName] ?? {});
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
for (const item of config.groups ?? []) {
|
| 303 |
+
if (item === "*") {
|
| 304 |
+
// This widget wants all global words included
|
| 305 |
+
Object.assign(words, TextAreaAutoComplete.globalWords);
|
| 306 |
+
} else {
|
| 307 |
+
// This widget wants a specific group included
|
| 308 |
+
Object.assign(words, TextAreaAutoComplete.groups[item] ?? {});
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
new TextAreaAutoComplete(r.widget.inputEl ?? r.widget.element, words, separator);
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
return r;
|
| 317 |
+
};
|
| 318 |
+
|
| 319 |
+
TextAreaAutoComplete.globalSeparator = localStorage.getItem(id + ".AutoSeparate") ?? ", ";
|
| 320 |
+
const enabledSetting = app.ui.settings.addSetting({
|
| 321 |
+
id,
|
| 322 |
+
name: "🐍 Text Autocomplete",
|
| 323 |
+
defaultValue: true,
|
| 324 |
+
type: (name, setter, value) => {
|
| 325 |
+
return $el("tr", [
|
| 326 |
+
$el("td", [
|
| 327 |
+
$el("label", {
|
| 328 |
+
for: id.replaceAll(".", "-"),
|
| 329 |
+
textContent: name,
|
| 330 |
+
}),
|
| 331 |
+
]),
|
| 332 |
+
$el("td", [
|
| 333 |
+
$el(
|
| 334 |
+
"label",
|
| 335 |
+
{
|
| 336 |
+
textContent: "Enabled ",
|
| 337 |
+
style: {
|
| 338 |
+
display: "block",
|
| 339 |
+
},
|
| 340 |
+
},
|
| 341 |
+
[
|
| 342 |
+
$el("input", {
|
| 343 |
+
id: id.replaceAll(".", "-"),
|
| 344 |
+
type: "checkbox",
|
| 345 |
+
checked: value,
|
| 346 |
+
onchange: (event) => {
|
| 347 |
+
const checked = !!event.target.checked;
|
| 348 |
+
TextAreaAutoComplete.enabled = checked;
|
| 349 |
+
setter(checked);
|
| 350 |
+
},
|
| 351 |
+
}),
|
| 352 |
+
]
|
| 353 |
+
),
|
| 354 |
+
$el(
|
| 355 |
+
"label.comfy-tooltip-indicator",
|
| 356 |
+
{
|
| 357 |
+
title: "This requires other ComfyUI nodes/extensions that support using LoRAs in the prompt.",
|
| 358 |
+
textContent: "Loras enabled ",
|
| 359 |
+
style: {
|
| 360 |
+
display: "block",
|
| 361 |
+
},
|
| 362 |
+
},
|
| 363 |
+
[
|
| 364 |
+
$el("input", {
|
| 365 |
+
type: "checkbox",
|
| 366 |
+
checked: !!TextAreaAutoComplete.lorasEnabled,
|
| 367 |
+
onchange: (event) => {
|
| 368 |
+
const checked = !!event.target.checked;
|
| 369 |
+
TextAreaAutoComplete.lorasEnabled = checked;
|
| 370 |
+
toggleLoras();
|
| 371 |
+
localStorage.setItem(id + ".ShowLoras", TextAreaAutoComplete.lorasEnabled);
|
| 372 |
+
},
|
| 373 |
+
}),
|
| 374 |
+
]
|
| 375 |
+
),
|
| 376 |
+
$el(
|
| 377 |
+
"label",
|
| 378 |
+
{
|
| 379 |
+
textContent: "Auto-insert comma ",
|
| 380 |
+
style: {
|
| 381 |
+
display: "block",
|
| 382 |
+
},
|
| 383 |
+
},
|
| 384 |
+
[
|
| 385 |
+
$el("input", {
|
| 386 |
+
type: "checkbox",
|
| 387 |
+
checked: !!TextAreaAutoComplete.globalSeparator,
|
| 388 |
+
onchange: (event) => {
|
| 389 |
+
const checked = !!event.target.checked;
|
| 390 |
+
TextAreaAutoComplete.globalSeparator = checked ? ", " : "";
|
| 391 |
+
localStorage.setItem(id + ".AutoSeparate", TextAreaAutoComplete.globalSeparator);
|
| 392 |
+
},
|
| 393 |
+
}),
|
| 394 |
+
]
|
| 395 |
+
),
|
| 396 |
+
$el(
|
| 397 |
+
"label",
|
| 398 |
+
{
|
| 399 |
+
textContent: "Replace _ with space ",
|
| 400 |
+
style: {
|
| 401 |
+
display: "block",
|
| 402 |
+
},
|
| 403 |
+
},
|
| 404 |
+
[
|
| 405 |
+
$el("input", {
|
| 406 |
+
type: "checkbox",
|
| 407 |
+
checked: !!TextAreaAutoComplete.replacer,
|
| 408 |
+
onchange: (event) => {
|
| 409 |
+
const checked = !!event.target.checked;
|
| 410 |
+
TextAreaAutoComplete.replacer = checked ? (v) => v.replaceAll("_", " ") : undefined;
|
| 411 |
+
localStorage.setItem(id + ".ReplaceUnderscore", checked);
|
| 412 |
+
},
|
| 413 |
+
}),
|
| 414 |
+
]
|
| 415 |
+
),
|
| 416 |
+
$el(
|
| 417 |
+
"label",
|
| 418 |
+
{
|
| 419 |
+
textContent: "Insert suggestion on: ",
|
| 420 |
+
style: {
|
| 421 |
+
display: "block",
|
| 422 |
+
},
|
| 423 |
+
},
|
| 424 |
+
[
|
| 425 |
+
$el(
|
| 426 |
+
"label",
|
| 427 |
+
{
|
| 428 |
+
textContent: "Tab",
|
| 429 |
+
style: {
|
| 430 |
+
display: "block",
|
| 431 |
+
marginLeft: "20px",
|
| 432 |
+
},
|
| 433 |
+
},
|
| 434 |
+
[
|
| 435 |
+
$el("input", {
|
| 436 |
+
type: "checkbox",
|
| 437 |
+
checked: !!TextAreaAutoComplete.insertOnTab,
|
| 438 |
+
onchange: (event) => {
|
| 439 |
+
const checked = !!event.target.checked;
|
| 440 |
+
TextAreaAutoComplete.insertOnTab = checked;
|
| 441 |
+
localStorage.setItem(id + ".InsertOnTab", checked);
|
| 442 |
+
},
|
| 443 |
+
}),
|
| 444 |
+
]
|
| 445 |
+
),
|
| 446 |
+
$el(
|
| 447 |
+
"label",
|
| 448 |
+
{
|
| 449 |
+
textContent: "Enter",
|
| 450 |
+
style: {
|
| 451 |
+
display: "block",
|
| 452 |
+
marginLeft: "20px",
|
| 453 |
+
},
|
| 454 |
+
},
|
| 455 |
+
[
|
| 456 |
+
$el("input", {
|
| 457 |
+
type: "checkbox",
|
| 458 |
+
checked: !!TextAreaAutoComplete.insertOnEnter,
|
| 459 |
+
onchange: (event) => {
|
| 460 |
+
const checked = !!event.target.checked;
|
| 461 |
+
TextAreaAutoComplete.insertOnEnter = checked;
|
| 462 |
+
localStorage.setItem(id + ".InsertOnEnter", checked);
|
| 463 |
+
},
|
| 464 |
+
}),
|
| 465 |
+
]
|
| 466 |
+
),
|
| 467 |
+
]
|
| 468 |
+
),
|
| 469 |
+
$el(
|
| 470 |
+
"label",
|
| 471 |
+
{
|
| 472 |
+
textContent: "Max suggestions: ",
|
| 473 |
+
style: {
|
| 474 |
+
display: "block",
|
| 475 |
+
},
|
| 476 |
+
},
|
| 477 |
+
[
|
| 478 |
+
$el("input", {
|
| 479 |
+
type: "number",
|
| 480 |
+
value: +TextAreaAutoComplete.suggestionCount,
|
| 481 |
+
style: {
|
| 482 |
+
width: "80px"
|
| 483 |
+
},
|
| 484 |
+
onchange: (event) => {
|
| 485 |
+
const value = +event.target.value;
|
| 486 |
+
TextAreaAutoComplete.suggestionCount = value;;
|
| 487 |
+
localStorage.setItem(id + ".SuggestionCount", TextAreaAutoComplete.suggestionCount);
|
| 488 |
+
},
|
| 489 |
+
}),
|
| 490 |
+
]
|
| 491 |
+
),
|
| 492 |
+
$el("button", {
|
| 493 |
+
textContent: "Manage Custom Words",
|
| 494 |
+
onclick: () => {
|
| 495 |
+
try {
|
| 496 |
+
// Try closing old settings window
|
| 497 |
+
if (typeof app.ui.settings.element?.close === "function") {
|
| 498 |
+
app.ui.settings.element.close();
|
| 499 |
+
}
|
| 500 |
+
} catch (error) {
|
| 501 |
+
}
|
| 502 |
+
try {
|
| 503 |
+
// Try closing new vue dialog
|
| 504 |
+
document.querySelector(".p-dialog-close-button").click();
|
| 505 |
+
} catch (error) {
|
| 506 |
+
// Fallback to just hiding the element
|
| 507 |
+
app.ui.settings.element.style.display = "none";
|
| 508 |
+
}
|
| 509 |
+
|
| 510 |
+
new CustomWordsDialog().show();
|
| 511 |
+
},
|
| 512 |
+
style: {
|
| 513 |
+
fontSize: "14px",
|
| 514 |
+
display: "block",
|
| 515 |
+
marginTop: "5px",
|
| 516 |
+
},
|
| 517 |
+
}),
|
| 518 |
+
]),
|
| 519 |
+
]);
|
| 520 |
+
},
|
| 521 |
+
});
|
| 522 |
+
|
| 523 |
+
TextAreaAutoComplete.enabled = enabledSetting.value;
|
| 524 |
+
TextAreaAutoComplete.replacer = localStorage.getItem(id + ".ReplaceUnderscore") === "true" ? (v) => v.replaceAll("_", " ") : undefined;
|
| 525 |
+
TextAreaAutoComplete.insertOnTab = localStorage.getItem(id + ".InsertOnTab") !== "false";
|
| 526 |
+
TextAreaAutoComplete.insertOnEnter = localStorage.getItem(id + ".InsertOnEnter") !== "false";
|
| 527 |
+
TextAreaAutoComplete.lorasEnabled = localStorage.getItem(id + ".ShowLoras") === "true";
|
| 528 |
+
TextAreaAutoComplete.suggestionCount = +localStorage.getItem(id + ".SuggestionCount") || 20;
|
| 529 |
+
},
|
| 530 |
+
setup() {
|
| 531 |
+
async function addEmbeddings() {
|
| 532 |
+
const embeddings = await api.getEmbeddings();
|
| 533 |
+
const words = {};
|
| 534 |
+
words["embedding:"] = { text: "embedding:" };
|
| 535 |
+
|
| 536 |
+
for (const emb of embeddings) {
|
| 537 |
+
const v = `embedding:${emb}`;
|
| 538 |
+
words[v] = {
|
| 539 |
+
text: v,
|
| 540 |
+
info: () => new EmbeddingInfoDialog(emb).show("embeddings", emb),
|
| 541 |
+
use_replacer: false,
|
| 542 |
+
};
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
TextAreaAutoComplete.updateWords("pysssss.embeddings", words);
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
async function addLoras() {
|
| 549 |
+
let loras;
|
| 550 |
+
try {
|
| 551 |
+
loras = LiteGraph.registered_node_types["LoraLoader"]?.nodeData.input.required.lora_name[0];
|
| 552 |
+
} catch (error) {}
|
| 553 |
+
|
| 554 |
+
if (!loras?.length) {
|
| 555 |
+
loras = await api.fetchApi("/pysssss/loras", { cache: "no-store" }).then((res) => res.json());
|
| 556 |
+
}
|
| 557 |
+
|
| 558 |
+
const words = {};
|
| 559 |
+
words["lora:"] = { text: "lora:" };
|
| 560 |
+
|
| 561 |
+
for (const lora of loras) {
|
| 562 |
+
const v = `<lora:${lora}:1.0>`;
|
| 563 |
+
words[v] = {
|
| 564 |
+
text: v,
|
| 565 |
+
info: () => new LoraInfoDialog(lora).show("loras", lora),
|
| 566 |
+
use_replacer: false,
|
| 567 |
+
};
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
TextAreaAutoComplete.updateWords("pysssss.loras", words);
|
| 571 |
+
}
|
| 572 |
+
|
| 573 |
+
// store global words with/without loras
|
| 574 |
+
Promise.all([addEmbeddings(), addCustomWords()])
|
| 575 |
+
.then(() => {
|
| 576 |
+
TextAreaAutoComplete.globalWordsExclLoras = Object.assign({}, TextAreaAutoComplete.globalWords);
|
| 577 |
+
})
|
| 578 |
+
.then(addLoras)
|
| 579 |
+
.then(() => {
|
| 580 |
+
if (!TextAreaAutoComplete.lorasEnabled) {
|
| 581 |
+
toggleLoras(); // off by default
|
| 582 |
+
}
|
| 583 |
+
});
|
| 584 |
+
},
|
| 585 |
+
beforeRegisterNodeDef(_, def) {
|
| 586 |
+
// Process each input to see if there is a custom word list for
|
| 587 |
+
// { input: { required: { something: ["STRING", { "pysssss.autocomplete": ["groupid", ["custom", "words"] ] }] } } }
|
| 588 |
+
const inputs = { ...def.input?.required, ...def.input?.optional };
|
| 589 |
+
for (const input in inputs) {
|
| 590 |
+
const config = inputs[input][1]?.["pysssss.autocomplete"];
|
| 591 |
+
if (!config) continue;
|
| 592 |
+
if (typeof config === "object" && config.words) {
|
| 593 |
+
const words = {};
|
| 594 |
+
for (const text of config.words || []) {
|
| 595 |
+
const obj = typeof text === "string" ? { text } : text;
|
| 596 |
+
words[obj.text] = obj;
|
| 597 |
+
}
|
| 598 |
+
TextAreaAutoComplete.updateWords(def.name + "." + input, words, false);
|
| 599 |
+
}
|
| 600 |
+
}
|
| 601 |
+
},
|
| 602 |
+
});
|
custom_nodes/ComfyUI-Custom-Scripts/web/js/betterCombos.js
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { app } from "../../../scripts/app.js";
|
| 2 |
+
import { ComfyWidgets } from "../../../scripts/widgets.js";
|
| 3 |
+
import { $el } from "../../../scripts/ui.js";
|
| 4 |
+
import { api } from "../../../scripts/api.js";
|
| 5 |
+
|
| 6 |
+
const CHECKPOINT_LOADER = "CheckpointLoader|pysssss";
|
| 7 |
+
const LORA_LOADER = "LoraLoader|pysssss";
|
| 8 |
+
const IMAGE_WIDTH = 384;
|
| 9 |
+
const IMAGE_HEIGHT = 384;
|
| 10 |
+
|
| 11 |
+
function getType(node) {
|
| 12 |
+
if (node.comfyClass === CHECKPOINT_LOADER) {
|
| 13 |
+
return "checkpoints";
|
| 14 |
+
}
|
| 15 |
+
return "loras";
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
function getWidgetName(type) {
|
| 19 |
+
return type === "checkpoints" ? "ckpt_name" : "lora_name";
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
function encodeRFC3986URIComponent(str) {
|
| 23 |
+
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
const calculateImagePosition = (el, bodyRect) => {
|
| 27 |
+
let { top, left, right } = el.getBoundingClientRect();
|
| 28 |
+
const { width: bodyWidth, height: bodyHeight } = bodyRect;
|
| 29 |
+
|
| 30 |
+
const isSpaceRight = right + IMAGE_WIDTH <= bodyWidth;
|
| 31 |
+
if (isSpaceRight) {
|
| 32 |
+
left = right;
|
| 33 |
+
} else {
|
| 34 |
+
left -= IMAGE_WIDTH;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
top = top - IMAGE_HEIGHT / 2;
|
| 38 |
+
if (top + IMAGE_HEIGHT > bodyHeight) {
|
| 39 |
+
top = bodyHeight - IMAGE_HEIGHT;
|
| 40 |
+
}
|
| 41 |
+
if (top < 0) {
|
| 42 |
+
top = 0;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
return { left: Math.round(left), top: Math.round(top), isLeft: !isSpaceRight };
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
function showImage(relativeToEl, imageEl) {
|
| 49 |
+
const bodyRect = document.body.getBoundingClientRect();
|
| 50 |
+
if (!bodyRect) return;
|
| 51 |
+
|
| 52 |
+
const { left, top, isLeft } = calculateImagePosition(relativeToEl, bodyRect);
|
| 53 |
+
|
| 54 |
+
imageEl.style.left = `${left}px`;
|
| 55 |
+
imageEl.style.top = `${top}px`;
|
| 56 |
+
|
| 57 |
+
if (isLeft) {
|
| 58 |
+
imageEl.classList.add("left");
|
| 59 |
+
} else {
|
| 60 |
+
imageEl.classList.remove("left");
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
document.body.appendChild(imageEl);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
let imagesByType = {};
|
| 67 |
+
const loadImageList = async (type) => {
|
| 68 |
+
imagesByType[type] = await (await api.fetchApi(`/pysssss/images/${type}`)).json();
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
app.registerExtension({
|
| 72 |
+
name: "pysssss.Combo++",
|
| 73 |
+
init() {
|
| 74 |
+
const displayOptions = { "List (normal)": 0, "Tree (subfolders)": 1, "Thumbnails (grid)": 2 };
|
| 75 |
+
const displaySetting = app.ui.settings.addSetting({
|
| 76 |
+
id: "pysssss.Combo++.Submenu",
|
| 77 |
+
name: "🐍 Lora & Checkpoint loader display mode",
|
| 78 |
+
defaultValue: 1,
|
| 79 |
+
type: "combo",
|
| 80 |
+
options: (value) => {
|
| 81 |
+
value = +value;
|
| 82 |
+
|
| 83 |
+
return Object.entries(displayOptions).map(([k, v]) => ({
|
| 84 |
+
value: v,
|
| 85 |
+
text: k,
|
| 86 |
+
selected: k === value,
|
| 87 |
+
}));
|
| 88 |
+
},
|
| 89 |
+
});
|
| 90 |
+
|
| 91 |
+
$el("style", {
|
| 92 |
+
textContent: `
|
| 93 |
+
.pysssss-combo-image {
|
| 94 |
+
position: absolute;
|
| 95 |
+
left: 0;
|
| 96 |
+
top: 0;
|
| 97 |
+
width: ${IMAGE_WIDTH}px;
|
| 98 |
+
height: ${IMAGE_HEIGHT}px;
|
| 99 |
+
object-fit: contain;
|
| 100 |
+
object-position: top left;
|
| 101 |
+
z-index: 9999;
|
| 102 |
+
}
|
| 103 |
+
.pysssss-combo-image.left {
|
| 104 |
+
object-position: top right;
|
| 105 |
+
}
|
| 106 |
+
.pysssss-combo-folder { opacity: 0.7 }
|
| 107 |
+
.pysssss-combo-folder-arrow { display: inline-block; width: 15px; }
|
| 108 |
+
.pysssss-combo-folder:hover { background-color: rgba(255, 255, 255, 0.1); }
|
| 109 |
+
.pysssss-combo-prefix { display: none }
|
| 110 |
+
|
| 111 |
+
/* Special handling for when the filter input is populated to revert to normal */
|
| 112 |
+
.litecontextmenu:has(input:not(:placeholder-shown)) .pysssss-combo-folder-contents {
|
| 113 |
+
display: block !important;
|
| 114 |
+
}
|
| 115 |
+
.litecontextmenu:has(input:not(:placeholder-shown)) .pysssss-combo-folder {
|
| 116 |
+
display: none;
|
| 117 |
+
}
|
| 118 |
+
.litecontextmenu:has(input:not(:placeholder-shown)) .pysssss-combo-prefix {
|
| 119 |
+
display: inline;
|
| 120 |
+
}
|
| 121 |
+
.litecontextmenu:has(input:not(:placeholder-shown)) .litemenu-entry {
|
| 122 |
+
padding-left: 2px !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/* Grid mode */
|
| 126 |
+
.pysssss-combo-grid {
|
| 127 |
+
display: grid;
|
| 128 |
+
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
| 129 |
+
gap: 10px;
|
| 130 |
+
overflow-x: hidden;
|
| 131 |
+
max-width: 60vw;
|
| 132 |
+
}
|
| 133 |
+
.pysssss-combo-grid .comfy-context-menu-filter {
|
| 134 |
+
grid-column: 1 / -1;
|
| 135 |
+
position: sticky;
|
| 136 |
+
top: 0;
|
| 137 |
+
}
|
| 138 |
+
.pysssss-combo-grid .litemenu-entry {
|
| 139 |
+
word-break: break-word;
|
| 140 |
+
display: flex;
|
| 141 |
+
flex-direction: column;
|
| 142 |
+
justify-content: space-between;
|
| 143 |
+
align-items: center;
|
| 144 |
+
}
|
| 145 |
+
.pysssss-combo-grid .litemenu-entry:before {
|
| 146 |
+
content: "";
|
| 147 |
+
display: block;
|
| 148 |
+
width: 100%;
|
| 149 |
+
height: 250px;
|
| 150 |
+
background-size: contain;
|
| 151 |
+
background-position: center;
|
| 152 |
+
background-repeat: no-repeat;
|
| 153 |
+
/* No-image image attribution: Picture icons created by Pixel perfect - Flaticon */
|
| 154 |
+
background-image: var(--background-image, url(extensions/ComfyUI-Custom-Scripts/js/assets/no-image.png));
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
`,
|
| 158 |
+
parent: document.body,
|
| 159 |
+
});
|
| 160 |
+
const p1 = loadImageList("checkpoints");
|
| 161 |
+
const p2 = loadImageList("loras");
|
| 162 |
+
|
| 163 |
+
const refreshComboInNodes = app.refreshComboInNodes;
|
| 164 |
+
app.refreshComboInNodes = async function () {
|
| 165 |
+
const r = await Promise.all([
|
| 166 |
+
refreshComboInNodes.apply(this, arguments),
|
| 167 |
+
loadImageList("checkpoints").catch(() => {}),
|
| 168 |
+
loadImageList("loras").catch(() => {}),
|
| 169 |
+
]);
|
| 170 |
+
return r[0];
|
| 171 |
+
};
|
| 172 |
+
|
| 173 |
+
const imageHost = $el("img.pysssss-combo-image");
|
| 174 |
+
|
| 175 |
+
const positionMenu = (menu, fillWidth) => {
|
| 176 |
+
// compute best position
|
| 177 |
+
let left = app.canvas.last_mouse[0] - 10;
|
| 178 |
+
let top = app.canvas.last_mouse[1] - 10;
|
| 179 |
+
|
| 180 |
+
const body_rect = document.body.getBoundingClientRect();
|
| 181 |
+
const root_rect = menu.getBoundingClientRect();
|
| 182 |
+
|
| 183 |
+
if (body_rect.width && left > body_rect.width - root_rect.width - 10) left = body_rect.width - root_rect.width - 10;
|
| 184 |
+
if (body_rect.height && top > body_rect.height - root_rect.height - 10) top = body_rect.height - root_rect.height - 10;
|
| 185 |
+
|
| 186 |
+
menu.style.left = `${left}px`;
|
| 187 |
+
menu.style.top = `${top}px`;
|
| 188 |
+
if (fillWidth) {
|
| 189 |
+
menu.style.right = "10px";
|
| 190 |
+
}
|
| 191 |
+
};
|
| 192 |
+
|
| 193 |
+
const updateMenu = async (menu, type) => {
|
| 194 |
+
try {
|
| 195 |
+
await p1;
|
| 196 |
+
await p2;
|
| 197 |
+
} catch (error) {
|
| 198 |
+
console.error(error);
|
| 199 |
+
console.error("Error loading pysssss.betterCombos data");
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// Clamp max height so it doesn't overflow the screen
|
| 203 |
+
const position = menu.getBoundingClientRect();
|
| 204 |
+
const maxHeight = window.innerHeight - position.top - 20;
|
| 205 |
+
menu.style.maxHeight = `${maxHeight}px`;
|
| 206 |
+
|
| 207 |
+
const images = imagesByType[type];
|
| 208 |
+
const items = menu.querySelectorAll(".litemenu-entry");
|
| 209 |
+
|
| 210 |
+
// Add image handler to items
|
| 211 |
+
const addImageHandler = (item) => {
|
| 212 |
+
const text = item.getAttribute("data-value").trim();
|
| 213 |
+
if (images[text]) {
|
| 214 |
+
const textNode = document.createTextNode("*");
|
| 215 |
+
item.appendChild(textNode);
|
| 216 |
+
|
| 217 |
+
item.addEventListener(
|
| 218 |
+
"mouseover",
|
| 219 |
+
() => {
|
| 220 |
+
imageHost.src = `/pysssss/view/${encodeRFC3986URIComponent(images[text])}?${+new Date()}`;
|
| 221 |
+
document.body.appendChild(imageHost);
|
| 222 |
+
showImage(item, imageHost);
|
| 223 |
+
},
|
| 224 |
+
{ passive: true }
|
| 225 |
+
);
|
| 226 |
+
item.addEventListener(
|
| 227 |
+
"mouseout",
|
| 228 |
+
() => {
|
| 229 |
+
imageHost.remove();
|
| 230 |
+
},
|
| 231 |
+
{ passive: true }
|
| 232 |
+
);
|
| 233 |
+
item.addEventListener(
|
| 234 |
+
"click",
|
| 235 |
+
() => {
|
| 236 |
+
imageHost.remove();
|
| 237 |
+
},
|
| 238 |
+
{ passive: true }
|
| 239 |
+
);
|
| 240 |
+
}
|
| 241 |
+
};
|
| 242 |
+
|
| 243 |
+
const createTree = () => {
|
| 244 |
+
// Create a map to store folder structures
|
| 245 |
+
const folderMap = new Map();
|
| 246 |
+
const rootItems = [];
|
| 247 |
+
const splitBy = (navigator.platform || navigator.userAgent).includes("Win") ? /\/|\\/ : /\//;
|
| 248 |
+
const itemsSymbol = Symbol("items");
|
| 249 |
+
|
| 250 |
+
// First pass - organize items into folder structure
|
| 251 |
+
for (const item of items) {
|
| 252 |
+
const path = item.getAttribute("data-value").split(splitBy);
|
| 253 |
+
|
| 254 |
+
// Remove path from visible text
|
| 255 |
+
item.textContent = path[path.length - 1];
|
| 256 |
+
if (path.length > 1) {
|
| 257 |
+
// Add the prefix path back in so it can be filtered on
|
| 258 |
+
const prefix = $el("span.pysssss-combo-prefix", {
|
| 259 |
+
textContent: path.slice(0, -1).join("/") + "/",
|
| 260 |
+
});
|
| 261 |
+
item.prepend(prefix);
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
addImageHandler(item);
|
| 265 |
+
|
| 266 |
+
if (path.length === 1) {
|
| 267 |
+
rootItems.push(item);
|
| 268 |
+
continue;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
// Temporarily remove the item from current position
|
| 272 |
+
item.remove();
|
| 273 |
+
|
| 274 |
+
// Create folder hierarchy
|
| 275 |
+
let currentLevel = folderMap;
|
| 276 |
+
for (let i = 0; i < path.length - 1; i++) {
|
| 277 |
+
const folder = path[i];
|
| 278 |
+
if (!currentLevel.has(folder)) {
|
| 279 |
+
currentLevel.set(folder, new Map());
|
| 280 |
+
}
|
| 281 |
+
currentLevel = currentLevel.get(folder);
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// Store the actual item in the deepest folder
|
| 285 |
+
if (!currentLevel.has(itemsSymbol)) {
|
| 286 |
+
currentLevel.set(itemsSymbol, []);
|
| 287 |
+
}
|
| 288 |
+
currentLevel.get(itemsSymbol).push(item);
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
const createFolderElement = (name) => {
|
| 292 |
+
const folder = $el("div.litemenu-entry.pysssss-combo-folder", {
|
| 293 |
+
innerHTML: `<span class="pysssss-combo-folder-arrow">▶</span> ${name}`,
|
| 294 |
+
style: { paddingLeft: "5px" },
|
| 295 |
+
});
|
| 296 |
+
return folder;
|
| 297 |
+
};
|
| 298 |
+
|
| 299 |
+
const insertFolderStructure = (parentElement, map, level = 0) => {
|
| 300 |
+
for (const [folderName, content] of map.entries()) {
|
| 301 |
+
if (folderName === itemsSymbol) continue;
|
| 302 |
+
|
| 303 |
+
const folderElement = createFolderElement(folderName);
|
| 304 |
+
folderElement.style.paddingLeft = `${level * 10 + 5}px`;
|
| 305 |
+
parentElement.appendChild(folderElement);
|
| 306 |
+
|
| 307 |
+
const childContainer = $el("div.pysssss-combo-folder-contents", {
|
| 308 |
+
style: { display: "none" },
|
| 309 |
+
});
|
| 310 |
+
|
| 311 |
+
// Add items in this folder
|
| 312 |
+
const items = content.get(itemsSymbol) || [];
|
| 313 |
+
for (const item of items) {
|
| 314 |
+
item.style.paddingLeft = `${(level + 1) * 10 + 14}px`;
|
| 315 |
+
childContainer.appendChild(item);
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
// Recursively add subfolders
|
| 319 |
+
insertFolderStructure(childContainer, content, level + 1);
|
| 320 |
+
parentElement.appendChild(childContainer);
|
| 321 |
+
|
| 322 |
+
// Add click handler for folder
|
| 323 |
+
folderElement.addEventListener("click", (e) => {
|
| 324 |
+
e.stopPropagation();
|
| 325 |
+
const arrow = folderElement.querySelector(".pysssss-combo-folder-arrow");
|
| 326 |
+
const contents = folderElement.nextElementSibling;
|
| 327 |
+
if (contents.style.display === "none") {
|
| 328 |
+
contents.style.display = "block";
|
| 329 |
+
arrow.textContent = "▼";
|
| 330 |
+
} else {
|
| 331 |
+
contents.style.display = "none";
|
| 332 |
+
arrow.textContent = "▶";
|
| 333 |
+
}
|
| 334 |
+
});
|
| 335 |
+
}
|
| 336 |
+
};
|
| 337 |
+
|
| 338 |
+
insertFolderStructure(items[0]?.parentElement || menu, folderMap);
|
| 339 |
+
positionMenu(menu);
|
| 340 |
+
};
|
| 341 |
+
|
| 342 |
+
const addImageData = (item) => {
|
| 343 |
+
const text = item.getAttribute("data-value").trim();
|
| 344 |
+
if (images[text]) {
|
| 345 |
+
item.style.setProperty("--background-image", `url(/pysssss/view/${encodeRFC3986URIComponent(images[text])})`);
|
| 346 |
+
}
|
| 347 |
+
};
|
| 348 |
+
|
| 349 |
+
if (displaySetting.value === 1 || displaySetting.value === true) {
|
| 350 |
+
createTree();
|
| 351 |
+
} else if (displaySetting.value === 2) {
|
| 352 |
+
menu.classList.add("pysssss-combo-grid");
|
| 353 |
+
|
| 354 |
+
for (const item of items) {
|
| 355 |
+
addImageData(item);
|
| 356 |
+
}
|
| 357 |
+
positionMenu(menu, true);
|
| 358 |
+
} else {
|
| 359 |
+
for (const item of items) {
|
| 360 |
+
addImageHandler(item);
|
| 361 |
+
}
|
| 362 |
+
}
|
| 363 |
+
};
|
| 364 |
+
|
| 365 |
+
const mutationObserver = new MutationObserver((mutations) => {
|
| 366 |
+
const node = app.canvas.current_node;
|
| 367 |
+
|
| 368 |
+
if (!node || (node.comfyClass !== LORA_LOADER && node.comfyClass !== CHECKPOINT_LOADER)) {
|
| 369 |
+
return;
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
for (const mutation of mutations) {
|
| 373 |
+
for (const removed of mutation.removedNodes) {
|
| 374 |
+
if (removed.classList?.contains("litecontextmenu")) {
|
| 375 |
+
imageHost.remove();
|
| 376 |
+
}
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
for (const added of mutation.addedNodes) {
|
| 380 |
+
if (added.classList?.contains("litecontextmenu")) {
|
| 381 |
+
const overWidget = app.canvas.getWidgetAtCursor();
|
| 382 |
+
const type = getType(node);
|
| 383 |
+
if (overWidget?.name === getWidgetName(type)) {
|
| 384 |
+
requestAnimationFrame(() => {
|
| 385 |
+
// Bad hack to prevent showing on right click menu by checking for the filter input
|
| 386 |
+
if (!added.querySelector(".comfy-context-menu-filter")) return;
|
| 387 |
+
updateMenu(added, type);
|
| 388 |
+
});
|
| 389 |
+
}
|
| 390 |
+
return;
|
| 391 |
+
}
|
| 392 |
+
}
|
| 393 |
+
}
|
| 394 |
+
});
|
| 395 |
+
mutationObserver.observe(document.body, { childList: true, subtree: false });
|
| 396 |
+
},
|
| 397 |
+
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
| 398 |
+
const isCkpt = nodeData.name === CHECKPOINT_LOADER;
|
| 399 |
+
const isLora = nodeData.name === LORA_LOADER;
|
| 400 |
+
if (isCkpt || isLora) {
|
| 401 |
+
const onAdded = nodeType.prototype.onAdded;
|
| 402 |
+
nodeType.prototype.onAdded = function () {
|
| 403 |
+
onAdded?.apply(this, arguments);
|
| 404 |
+
const { widget: exampleList } = ComfyWidgets["COMBO"](this, "example", [[""], {}], app);
|
| 405 |
+
this.widgets.find((w) => w.name === "prompt").computeSize = () => [0, -4];
|
| 406 |
+
let exampleWidget;
|
| 407 |
+
|
| 408 |
+
const get = async (route, suffix) => {
|
| 409 |
+
const url = encodeRFC3986URIComponent(`${getType(nodeType)}${suffix || ""}`);
|
| 410 |
+
return await api.fetchApi(`/pysssss/${route}/${url}`);
|
| 411 |
+
};
|
| 412 |
+
|
| 413 |
+
const getExample = async () => {
|
| 414 |
+
if (exampleList.value === "[none]") {
|
| 415 |
+
if (exampleWidget) {
|
| 416 |
+
exampleWidget.inputEl.remove();
|
| 417 |
+
exampleWidget = null;
|
| 418 |
+
this.widgets.length -= 1;
|
| 419 |
+
}
|
| 420 |
+
return;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
const v = this.widgets[0].value;
|
| 424 |
+
const pos = v.lastIndexOf(".");
|
| 425 |
+
const name = v.substr(0, pos);
|
| 426 |
+
let exampleName = exampleList.value;
|
| 427 |
+
let viewPath = `/${name}`;
|
| 428 |
+
if (exampleName === "notes") {
|
| 429 |
+
viewPath += ".txt";
|
| 430 |
+
} else {
|
| 431 |
+
viewPath += `/${exampleName}`;
|
| 432 |
+
}
|
| 433 |
+
const example = await (await get("view", viewPath)).text();
|
| 434 |
+
if (!exampleWidget) {
|
| 435 |
+
exampleWidget = ComfyWidgets["STRING"](this, "prompt", ["STRING", { multiline: true }], app).widget;
|
| 436 |
+
exampleWidget.inputEl.readOnly = true;
|
| 437 |
+
exampleWidget.inputEl.style.opacity = 0.6;
|
| 438 |
+
}
|
| 439 |
+
exampleWidget.value = example;
|
| 440 |
+
};
|
| 441 |
+
|
| 442 |
+
const exampleCb = exampleList.callback;
|
| 443 |
+
exampleList.callback = function () {
|
| 444 |
+
getExample();
|
| 445 |
+
return exampleCb?.apply(this, arguments) ?? exampleList.value;
|
| 446 |
+
};
|
| 447 |
+
|
| 448 |
+
const listExamples = async () => {
|
| 449 |
+
exampleList.disabled = true;
|
| 450 |
+
exampleList.options.values = ["[none]"];
|
| 451 |
+
exampleList.value = "[none]";
|
| 452 |
+
let examples = [];
|
| 453 |
+
if (this.widgets[0].value) {
|
| 454 |
+
try {
|
| 455 |
+
examples = await (await get("examples", `/${this.widgets[0].value}`)).json();
|
| 456 |
+
} catch (error) {}
|
| 457 |
+
}
|
| 458 |
+
exampleList.options.values = ["[none]", ...examples];
|
| 459 |
+
exampleList.value = exampleList.options.values[+!!examples.length];
|
| 460 |
+
exampleList.callback();
|
| 461 |
+
exampleList.disabled = !examples.length;
|
| 462 |
+
app.graph.setDirtyCanvas(true, true);
|
| 463 |
+
};
|
| 464 |
+
|
| 465 |
+
// Expose function to update examples
|
| 466 |
+
nodeType.prototype["pysssss.updateExamples"] = listExamples;
|
| 467 |
+
|
| 468 |
+
const modelWidget = this.widgets[0];
|
| 469 |
+
const modelCb = modelWidget.callback;
|
| 470 |
+
let prev = undefined;
|
| 471 |
+
modelWidget.callback = function () {
|
| 472 |
+
let ret = modelCb?.apply(this, arguments) ?? modelWidget.value;
|
| 473 |
+
if (typeof ret === "object" && "content" in ret) {
|
| 474 |
+
ret = ret.content;
|
| 475 |
+
modelWidget.value = ret;
|
| 476 |
+
}
|
| 477 |
+
let v = ret;
|
| 478 |
+
if (prev !== v) {
|
| 479 |
+
listExamples();
|
| 480 |
+
prev = v;
|
| 481 |
+
}
|
| 482 |
+
return ret;
|
| 483 |
+
};
|
| 484 |
+
setTimeout(() => {
|
| 485 |
+
modelWidget.callback();
|
| 486 |
+
}, 30);
|
| 487 |
+
};
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
const getExtraMenuOptions = nodeType.prototype.getExtraMenuOptions;
|
| 491 |
+
nodeType.prototype.getExtraMenuOptions = function (_, options) {
|
| 492 |
+
if (this.imgs) {
|
| 493 |
+
// If this node has images then we add an open in new tab item
|
| 494 |
+
let img;
|
| 495 |
+
if (this.imageIndex != null) {
|
| 496 |
+
// An image is selected so select that
|
| 497 |
+
img = this.imgs[this.imageIndex];
|
| 498 |
+
} else if (this.overIndex != null) {
|
| 499 |
+
// No image is selected but one is hovered
|
| 500 |
+
img = this.imgs[this.overIndex];
|
| 501 |
+
}
|
| 502 |
+
if (img) {
|
| 503 |
+
const nodes = app.graph._nodes.filter((n) => n.comfyClass === LORA_LOADER || n.comfyClass === CHECKPOINT_LOADER);
|
| 504 |
+
if (nodes.length) {
|
| 505 |
+
options.unshift({
|
| 506 |
+
content: "Save as Preview",
|
| 507 |
+
submenu: {
|
| 508 |
+
options: nodes.map((n) => ({
|
| 509 |
+
content: n.widgets[0].value,
|
| 510 |
+
callback: async () => {
|
| 511 |
+
const url = new URL(img.src);
|
| 512 |
+
await api.fetchApi("/pysssss/save/" + encodeRFC3986URIComponent(`${getType(n)}/${n.widgets[0].value}`), {
|
| 513 |
+
method: "POST",
|
| 514 |
+
body: JSON.stringify({
|
| 515 |
+
filename: url.searchParams.get("filename"),
|
| 516 |
+
subfolder: url.searchParams.get("subfolder"),
|
| 517 |
+
type: url.searchParams.get("type"),
|
| 518 |
+
}),
|
| 519 |
+
headers: {
|
| 520 |
+
"content-type": "application/json",
|
| 521 |
+
},
|
| 522 |
+
});
|
| 523 |
+
loadImageList(getType(n));
|
| 524 |
+
},
|
| 525 |
+
})),
|
| 526 |
+
},
|
| 527 |
+
});
|
| 528 |
+
}
|
| 529 |
+
}
|
| 530 |
+
}
|
| 531 |
+
return getExtraMenuOptions?.apply(this, arguments);
|
| 532 |
+
};
|
| 533 |
+
},
|
| 534 |
+
});
|