daquanzhou commited on
Commit
613c9ab
1 Parent(s): 90ed29e

merge github repos and lfs track ckpt/path/safetensors/pt

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +4 -0
  2. .gitignore +3 -0
  3. custom_nodes/ComfyUI-AnimateDiff-Evolved/LICENSE +201 -0
  4. custom_nodes/ComfyUI-AnimateDiff-Evolved/README.md +495 -0
  5. custom_nodes/ComfyUI-AnimateDiff-Evolved/__init__.py +10 -0
  6. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/ad_settings.py +143 -0
  7. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/context.py +389 -0
  8. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/freeinit.py +162 -0
  9. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/logger.py +36 -0
  10. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/model_injection.py +581 -0
  11. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/motion_lora.py +25 -0
  12. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/motion_module_ad.py +971 -0
  13. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes.py +149 -0
  14. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_ad_settings.py +107 -0
  15. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_context.py +347 -0
  16. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_deprecated.py +277 -0
  17. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_extras.py +78 -0
  18. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_gen1.py +340 -0
  19. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_gen2.py +212 -0
  20. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_lora.py +90 -0
  21. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_multival.py +136 -0
  22. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_sample.py +255 -0
  23. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_sigma_schedule.py +141 -0
  24. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/sample_settings.py +555 -0
  25. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/sampling.py +528 -0
  26. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/utils_model.py +417 -0
  27. custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/utils_motion.py +230 -0
  28. custom_nodes/ComfyUI-AnimateDiff-Evolved/models/.gitkeep +0 -0
  29. custom_nodes/ComfyUI-AnimateDiff-Evolved/models/mm_sd_v15_v2.ckpt +3 -0
  30. custom_nodes/ComfyUI-AnimateDiff-Evolved/motion_lora/.gitkeep +0 -0
  31. custom_nodes/ComfyUI-AnimateDiff-Evolved/motion_lora/v2_lora_ZoomIn.ckpt +3 -0
  32. custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/av1-webm.json +10 -0
  33. custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/h264-mp4.json +9 -0
  34. custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/h265-mp4.json +11 -0
  35. custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/webm.json +9 -0
  36. custom_nodes/ComfyUI-AnimateDiff-Evolved/web/js/gif_preview.js +142 -0
  37. custom_nodes/ComfyUI-Impact-Pack/LICENSE.txt +674 -0
  38. custom_nodes/ComfyUI-Impact-Pack/README.md +454 -0
  39. custom_nodes/ComfyUI-Impact-Pack/__init__.py +502 -0
  40. custom_nodes/ComfyUI-Impact-Pack/custom_wildcards/put_wildcards_here +0 -0
  41. custom_nodes/ComfyUI-Impact-Pack/disable.py +38 -0
  42. custom_nodes/ComfyUI-Impact-Pack/impact-pack.ini +8 -0
  43. custom_nodes/ComfyUI-Impact-Pack/impact_subpack/LICENSE +661 -0
  44. custom_nodes/ComfyUI-Impact-Pack/impact_subpack/README.md +18 -0
  45. custom_nodes/ComfyUI-Impact-Pack/impact_subpack/impact/subcore.py +213 -0
  46. custom_nodes/ComfyUI-Impact-Pack/impact_subpack/impact/subpack_nodes.py +45 -0
  47. custom_nodes/ComfyUI-Impact-Pack/impact_subpack/install.py +32 -0
  48. custom_nodes/ComfyUI-Impact-Pack/impact_subpack/requirements.txt +1 -0
  49. custom_nodes/ComfyUI-Impact-Pack/install.py +285 -0
  50. custom_nodes/ComfyUI-Impact-Pack/js/comboBoolMigration.js +35 -0
.gitattributes ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
2
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
3
+ *.pt filter=lfs diff=lfs merge=lfs -text
4
+ *.pth filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ __pycache__
2
+ temp/
3
+ *.log
custom_nodes/ComfyUI-AnimateDiff-Evolved/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 2023 Jedrzej Kosinski
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/ComfyUI-AnimateDiff-Evolved/README.md ADDED
@@ -0,0 +1,495 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AnimateDiff for ComfyUI
2
+
3
+ Improved [AnimateDiff](https://github.com/guoyww/AnimateDiff/) integration for ComfyUI, as well as advanced sampling options dubbed Evolved Sampling usable outside of AnimateDiff. Please read the AnimateDiff repo README and Wiki for more information about how it works at its core.
4
+
5
+ AnimateDiff workflows will often make use of these helpful node packs:
6
+ - [ComfyUI_FizzNodes](https://github.com/FizzleDorf/ComfyUI_FizzNodes) for prompt-travel functionality with the BatchPromptSchedule node. Maintained by FizzleDorf.
7
+ - [ComfyUI-Advanced-ControlNet](https://github.com/Kosinkadink/ComfyUI-Advanced-ControlNet) for making ControlNets work with Context Options and controlling which latents should be affected by the ControlNet inputs. Includes SparseCtrl support. Maintained by me.
8
+ - [ComfyUI-VideoHelperSuite](https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite) for loading videos, combining images into videos, and doing various image/latent operations like appending, splitting, duplicating, selecting, or counting. Actively maintained by AustinMroz and I.
9
+ - [comfyui_controlnet_aux](https://github.com/Fannovel16/comfyui_controlnet_aux) for ControlNet preprocessors not present in vanilla ComfyUI. Maintained by Fannovel16.
10
+ - [ComfyUI_IPAdapter_plus](https://github.com/cubiq/ComfyUI_IPAdapter_plus) for IPAdapter support. Maintained by cubiq (matt3o).
11
+
12
+ # Installation
13
+
14
+ ## If using ComfyUI Manager:
15
+
16
+ 1. Look for ```AnimateDiff Evolved```, and be sure the author is ```Kosinkadink```. Install it.
17
+ ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/2c7f29e1-d024-49e1-9eb0-d38070142584)
18
+
19
+
20
+ ## If installing manually:
21
+ 1. Clone this repo into `custom_nodes` folder.
22
+
23
+ # Model Setup:
24
+ 1. Download motion modules. You will need at least 1. Different modules produce different results.
25
+ - Original models ```mm_sd_v14```, ```mm_sd_v15```, ```mm_sd_v15_v2```, ```v3_sd15_mm```: [HuggingFace](https://huggingface.co/guoyww/animatediff/tree/cd71ae134a27ec6008b968d6419952b0c0494cf2) | [Google Drive](https://drive.google.com/drive/folders/1EqLC65eR1-W-sGD0Im7fkED6c8GkiNFI) | [CivitAI](https://civitai.com/models/108836)
26
+ - Stabilized finetunes of mm_sd_v14, ```mm-Stabilized_mid``` and ```mm-Stabilized_high```, by **manshoety**: [HuggingFace](https://huggingface.co/manshoety/AD_Stabilized_Motion/tree/main)
27
+ - Finetunes of mm_sd_v15_v2, ```mm-p_0.5.pth``` and ```mm-p_0.75.pth```, by **manshoety**: [HuggingFace](https://huggingface.co/manshoety/beta_testing_models/tree/main)
28
+ - Higher resolution finetune,```temporaldiff-v1-animatediff``` by **CiaraRowles**: [HuggingFace](https://huggingface.co/CiaraRowles/TemporalDiff/tree/main)
29
+ - FP16/safetensor versions of vanilla motion models, hosted by **continue-revolution** (takes up less storage space, but uses up the same amount of VRAM as ComfyUI loads models in fp16 by default): [HuffingFace](https://huggingface.co/conrevo/AnimateDiff-A1111/tree/main)
30
+ 2. Place models in one of these locations (you can rename models if you wish):
31
+ - ```ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved/models```
32
+ - ```ComfyUI/models/animatediff_models```
33
+ 3. Optionally, you can use Motion LoRAs to influence movement of v2-based motion models like mm_sd_v15_v2.
34
+ - [Google Drive](https://drive.google.com/drive/folders/1EqLC65eR1-W-sGD0Im7fkED6c8GkiNFI?usp=sharing) | [HuggingFace](https://huggingface.co/guoyww/animatediff) | [CivitAI](https://civitai.com/models/108836/animatediff-motion-modules)
35
+ - Place Motion LoRAs in one of these locations (you can rename Motion LoRAs if you wish):
36
+ - ```ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved/motion_lora```
37
+ - ```ComfyUI/models/animatediff_motion_lora```
38
+ 4. Get creative! If it works for normal image generation, it (probably) will work for AnimateDiff generations. Latent upscales? Go for it. ControlNets, one or more stacked? You betcha. Masking the conditioning of ControlNets to only affect part of the animation? Sure. Try stuff and you will be surprised by what you can do. Samples with workflows are included below.
39
+
40
+ NOTE: you can also use custom locations for models/motion loras by making use of the ComfyUI ```extra_model_paths.yaml``` file. The id for motion model folder is ```animatediff_models``` and the id for motion lora folder is ```animatediff_motion_lora```.
41
+
42
+
43
+ # Features
44
+ - Compatible with almost any vanilla or custom KSampler node.
45
+ - ControlNet, SparseCtrl, and IPAdapter support
46
+ - Infinite animation length support via sliding context windows across whole unet (Context Options) and/or within motion module (View Options)
47
+ - Scheduling Context Options to change across different points in the sampling process
48
+ - FreeInit and FreeNoise support (FreeInit is under iteration opts, FreeNoise is in SampleSettings' noise_type dropdown)
49
+ - Mixable Motion LoRAs from [original AnimateDiff repository](https://github.com/guoyww/animatediff/) implemented. Caveat: the original loras really only work on v2-based motion models like ```mm_sd_v15_v2```, ```mm-p_0.5.pth```, and ```mm-p_0.75.pth```.
50
+ - UPDATE: New motion LoRAs without the v2 limitation can now be trained via the [AnimateDiff-MotionDirector repo](https://github.com/ExponentialML/AnimateDiff-MotionDirector). Shoutout to ExponentialML for implementing MotionDirector for AnimateDiff purposes!
51
+ - Prompt travel using BatchPromptSchedule node from [ComfyUI_FizzNodes](https://github.com/FizzleDorf/ComfyUI_FizzNodes)
52
+ - Scale and Effect multival inputs to control motion amount and motion model influence on generation.
53
+ - Can be float, list of floats, or masks
54
+ - Custom noise scheduling via Noise Types, Noise Layers, and seed_override/seed_offset/batch_offset in Sample Settings and related nodes
55
+ - AnimateDiff model v1/v2/v3 support
56
+ - Using multiple motion models at once via Gen2 nodes (each supporting
57
+ - [HotshotXL](https://huggingface.co/hotshotco/Hotshot-XL/tree/main) support (an SDXL motion module arch), ```hsxl_temporal_layers.safetensors```.
58
+ - NOTE: You will need to use ```autoselect``` or ```linear (HotshotXL/default)``` beta_schedule, the sweetspot for context_length or total frames (when not using context) is 8 frames, and you will need to use an SDXL checkpoint.
59
+ - AnimateDiff-SDXL support, with corresponding model. Currently, a beta version is out, which you can find info about at [AnimateDiff](https://github.com/guoyww/AnimateDiff/).
60
+ - NOTE: You will need to use ```autoselect``` or ```linear (AnimateDiff-SDXL)``` beta_schedule. Other than that, same rules of thumb apply to AnimateDiff-SDXL as AnimateDiff.
61
+ - [AnimateLCM](https://github.com/G-U-N/AnimateLCM) support
62
+ - NOTE: You will need to use ```autoselect``` or ```lcm``` or ```lcm[100_ots]``` beta_schedule. To use fully with LCM, be sure to use appropriate LCM lora, use the ```lcm``` sampler_name in KSampler nodes, and lower cfg to somewhere around 1.0 to 2.0. Don't forget to decrease steps (minimum = ~4 steps), since LCM converges faster (less steps). Increase step count to increase detail as desired.
63
+ - AnimateDiff Keyframes to change Scale and Effect at different points in the sampling process.
64
+ - fp8 support; requires newest ComfyUI and torch >= 2.1 (decreases VRAM usage, but changes outputs)
65
+ - Mac M1/M2/M3 support
66
+ - Usage of Context Options and Sample Settings outside of AnimateDiff via Gen2 Use Evolved Sampling node
67
+
68
+ ## Upcoming Features
69
+ - Maskable Motion LoRA
70
+ - Maskable SD LoRA (and perhaps maskable SD Models as well)
71
+ - [PIA](https://github.com/open-mmlab/PIA) support
72
+ - Anything else AnimateDiff-related that comes out
73
+
74
+
75
+ # Basic Usage And Nodes
76
+
77
+ There are two families of nodes that can be used to use AnimateDiff/Evolved Sampling - **Gen1** and **Gen2**. Other than nodes marked specifically for Gen1/Gen2, all other nodes can be used for both Gen1 and Gen2.
78
+
79
+ Gen1 and Gen2 produce the exact same results (the backend code is identical), the only difference is in how the modes are used. Overall, Gen1 is the simplest way to use basic AnimateDiff features, while Gen2 separates model loading and application from the Evolved Sampling features. This means in practice, Gen2's Use Evolved Sampling node can be used without a model model, letting Context Options and Sample Settings be used without AnimateDiff.
80
+
81
+ In the following documentation, inputs/outputs will be color coded as follows:
82
+ - 🟩 - required inputs
83
+ - 🟨 - optional inputs
84
+ - 🟦 - start as widgets, can be converted to inputs
85
+ - 🟪 - output
86
+
87
+ ## Gen1/Gen2 Nodes
88
+
89
+ | ① Gen1 ① | ② Gen2 ② |
90
+ |---|---|
91
+ | - All-in-One node<br/> - If same model is loaded by multiple Gen1 nodes, duplicates RAM usage. | - Separates model loading from application and Evolved Sampling<br/> - Enables no motion model usage while preserving Evolved Sampling features<br/> - Enables multiple motion model usage with Apply AnimateDiff Model (Adv.) Node|
92
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/a94029fd-5e74-467b-853c-c3ec4cf8a321)| ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/8c050151-6cfb-4350-932d-a105af78a1ec)|
93
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c7ae9ef3-b5cd-4800-b249-da2cb73c4c1e)| ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/cffa21f7-0e33-45d1-9950-ad22eb229134) |
94
+
95
+
96
+ ### Inputs
97
+ - 🟩*model*: StableDiffusion (SD) Model input.
98
+ - 🟦*model_name*: AnimateDiff (AD) model to load and/or apply during the sampling process. Certain motion models work with SD1.5, while others work with SDXL.
99
+ - 🟦*beta_schedule*: Applies selected beta_schedule to SD model; ```autoselect``` will automatically select the recommended beta_schedule for selected motion models - or will use_existing if no motion model selected for Gen2.
100
+ - 🟨*context_options*: Context Options node from the context_opts submenu - should be used when needing to go back the sweetspot of an AnimateDiff model. Works with no motion models as well (Gen2 only).
101
+ - 🟨*sample_settings*: Sample Settings node input - used to apply custom sampling options such as FreeNoise (noise_type), FreeInit (iter_opts), custom seeds, Noise Layers, etc. Works with no motion models as well (Gen2 only).
102
+ - 🟨*motion_lora*: For v2-based models, Motion LoRA will influence the generated movement. Only a few official motion LoRAs were released - soon, I will be working with some community members to create training code to create (and test) new Motion LoRAs that might work with non-v2 models.
103
+ - 🟨*ad_settings*: Modifies motion models during loading process, allowing the Positional Encoders (PEs) to be adjusted to extend a model's sweetspot or modify overall motion.
104
+ - 🟨*ad_keyframes*: Allows scheduling of ```scale_multival``` and ```effect_multival``` inputs across sampling timesteps.
105
+ - 🟨*scale_multival*: Uses a ```Multival``` input (defaults to ```1.0```). Previously called motion_scale, it directly influences the amount of motion generated by the model. With the Multival nodes, it can accept a float, list of floats, and/or mask inputs, allowing different scale to be applied to not only different frames, but different areas of frames (including per-frame).
106
+ - 🟨*effect_multival*: Uses a ```Multival``` input (defaults to ```1.0```). Determines the influence of the motion models on the sampling process. Value of ```0.0``` is equivalent to normal SD output with no AnimateDiff influence. With the Multival nodes, it can accept a float, list of floats, and/or mask inputs, allowing different effect amount to be applied to not only different frames, but different areas of frames (including per-frame).
107
+
108
+ #### Gen2-Only Inputs
109
+ - 🟨*motion_model*: Input for loaded motion_model.
110
+ - 🟨*m_models*: One (or more) motion models outputted from Apply AnimateDiff Model nodes.
111
+
112
+ #### Gen2 Adv.-Only Inputs
113
+ - 🟨*prev_m_models*: Previous applied motion models to use alongside this one.
114
+ - 🟨*start_percent*: Determines when connected motion_model should take effect (supercedes any ad_keyframes).
115
+ - 🟨*end_percent*: Determines when connected motion_model should stop taking effect (supercedes any ad_keyframes).
116
+
117
+ #### Gen1 (Legacy) Inputs
118
+ - 🟦*motion_scale*: legacy version of ```scale_multival```, can only be a float.
119
+ - 🟦*apply_v2_models_properly*: backwards compatible toggle for months-old workflows that used code that did not turn off groupnorm hack for v2 models. **Only affects v2 models, nothing else.** All nodes default this value to ```True``` now.
120
+
121
+ ### Outputs
122
+ - 🟪*MODEL*: Injected SD model with Evolved Sampling/AnimateDiff.
123
+
124
+ #### Gen2-Only Outputs
125
+ - 🟪*MOTION_MODEL*: Loaded motion model.
126
+ - 🟪*M_MODELS*: One (or more) applied motion models, to be either plugged into Use Evolved Sampling or another Apply AnimateDiff Model (Adv.) node.
127
+
128
+
129
+ ## Multival Nodes
130
+
131
+ For Multival inputs, these nodes allow the use of floats, list of floats, and/or masks to use as input. Scaled Mask node allows customization of dark/light areas of masks in terms of what the values correspond to.
132
+
133
+ | Node | Inputs |
134
+ |---|---|
135
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/d4c6a63f-703a-402b-989e-ab4d04141c7a) | 🟨*mask_optional*: Mask for float values - black means 0.0, white means 1.0 (multiplied by float_val). <br/> 🟦*float_val*: Float multiplier.|
136
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/bc100bec-0407-47c8-aebd-f74f2417711e) | 🟩*mask*: Mask for float values. <br/> 🟦*min_float_val*: Minimum value. <br/>🟦*max_float_val*: Maximum value. <br/> 🟦*scaling*: When ```absolute```, black means min_float_val, white means max_float_val. When ```relative```, darkest area in masks (total) means min_float_val, lighest area in massk (total) means max_float_val. |
137
+
138
+
139
+ ## AnimateDiff Keyframe
140
+
141
+ Allows scheduling (in terms of timesteps) for scale_multival and effect_multival.
142
+
143
+ The two settings to determine schedule are ***start_percent*** and ***guarantee_steps***. When multiple keyframes have the same start_percent, they will be executed in the order they are connected, and run for guarantee_steps before moving on to the next node.
144
+
145
+ | Node |
146
+ |---|
147
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/dca73cdc-157a-47db-bed2-6ba584dceccd) |
148
+
149
+ ### Inputs
150
+ - 🟨*prev_ad_keyframes*: Chained keyframes to create schedule.
151
+ - 🟨*scale_multival*: Value of scale to use for this keyframe.
152
+ - 🟨*effect_multival*: Value of effect to use for this keyframe.
153
+ - 🟦*start_percent*: Percent of timesteps to start usage of this keyframe. If multiple keyframes have same start_percent, order of execution is determined by their chained order, and will last for guarantee_steps timesteps.
154
+ - 🟦*guarantee_steps*: Minimum amount of steps the keyframe will be used - when set to 0, this keyframe will only be used when no other keyframes are better matches for current timestep.
155
+ - 🟦*inherit_missing*: When set to ```True```, any missing scale_multival or effect_multival inputs will inherit the previous keyframe's values - if the previous keyframe also inherits missing, the last inherited value will be used.
156
+
157
+
158
+ ## Context Options and View Options
159
+
160
+ These nodes provide techniques used to extend the lengths of animations to get around the sweetspot limitations of AnimateDiff models (typically 16 frames) and HotshotXL model (8 frames).
161
+
162
+ Context Options works by diffusing portions of the animation at a time, including main SD diffusion, ControlNets, IPAdapters, etc., effectively limiting VRAM usage to be equivalent to be context_length latents.
163
+
164
+ View Options, in contrast, work by portioning the latents seen by the motion model. This does NOT decrease VRAM usage, but in general is more stable and faster than Context Options, since the latents don't have to go through the whole SD unet.
165
+
166
+ Context Options and View Options can be combined to get the best of both worlds - longer context_length can be used to gain more stable output, at the cost of using more VRAM (since context_length determines how much SD sampling is done at the same time on the GPU). Provided you have the VRAM, you could also use Views Only Context Options to use only View Options (and automatically make context_length equivalent to full latents) to get a speed boost in return for the higher VRAM usage.
167
+
168
+ There are two types of Context/View Options: ***Standard*** and ***Looped***. ***Standard*** options do not cause looping in the output. ***Looped*** options, as the name implies, causes looping in the output (from end to beginning). Prior to the code rework, the only context available was the looping kind.
169
+
170
+ ***I recommend using Standard Static at first when not wanting looped outputs.***
171
+
172
+ In the below animations, ***green*** shows the Contexts, and ***red*** shows the Views. TL;DR green is the amount of latents that are loaded into VRAM (and sampled), while red is the amount of latents that get passed into the motion model at a time.
173
+
174
+ ### Context Options◆Standard Static
175
+ | Behavior |
176
+ |---|
177
+ | ![anim__00005](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/b26792d6-0f41-4f07-93aa-e5ee83f4d90e) <br/> (latent count: 64, context_length: 16, context_overlap: 4, total steps: 20)|
178
+
179
+ | Node | Inputs |
180
+ |---|---|
181
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/a4a5f38e-3a1b-4328-9537-ad17567aed75) | 🟦*context_length*: Amount of latents to diffuse at once.<br/> 🟦*context_overlap*: Minimum common latents between adjacent windows.<br/> 🟦*fuse_method*: Method for averaging results of windows.<br/> 🟦*use_on_equal_length*: When True, allows context to be used when latent count matches context_length.<br/> 🟦*start_percent*: When multiple Context Options are chained, allows scheduling.<br/> 🟦*guarantee_steps*: When scheduling contexts, determines the *minimum* amount of sampling steps context should be used.<br/> 🟦*context_length*: Amount of latents to diffuse at once.<br/> 🟨*prev_context*: Allows chaining of contexts.<br/> 🟨*view_options*: When context_length > view_length (unless otherwise specified), allows view_options to be used within each context window.|
182
+
183
+ ### Context Options◆Standard Uniform
184
+ | Behavior |
185
+ |---|
186
+ | ![anim__00006](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/69707e3d-f49e-4368-89d5-616af2631594) <br/> (latent count: 64, context_length: 16, context_overlap: 4, context_stride: 1, total steps: 20) |
187
+ | ![anim__00010](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/7fc083b4-406f-4809-94ca-b389784adcab) <br/> (latent count: 64, context_length: 16, context_overlap: 4, context_stride: 2, total steps: 20) |
188
+
189
+ | Node | Inputs |
190
+ |---|---|
191
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c2c8c7ea-66b6-408d-be46-1d805ecd64d1) | 🟦*context_length*: Amount of latents to diffuse at once.<br/> 🟦*context_overlap*: Minimum common latents between adjacent windows.<br/> 🟦*context_stride*: Maximum 2^(stride-1) distance between adjacent latents.<br/> 🟦*fuse_method*: Method for averaging results of windows.<br/> 🟦*use_on_equal_length*: When True, allows context to be used when latent count matches context_length.<br/> 🟦*start_percent*: When multiple Context Options are chained, allows scheduling.<br/> 🟦*guarantee_steps*: When scheduling contexts, determines the *minimum* amount of sampling steps context should be used.<br/> 🟦*context_length*: Amount of latents to diffuse at once.<br/> 🟨*prev_context*: Allows chaining of contexts.<br/> 🟨*view_options*: When context_length > view_length (unless otherwise specified), allows view_options to be used within each context window.|
192
+
193
+ ### Context Options◆Looped Uniform
194
+ | Behavior |
195
+ |---|
196
+ | ![anim__00008](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/d08ac1c9-2cec-4c9e-b257-0a804448d41b) <br/> (latent count: 64, context_length: 16, context_overlap: 4, context_stride: 1, closed_loop: False, total steps: 20) |
197
+ | ![anim__00009](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/61e0311b-b623-423f-bbcb-eb4eb02e9002) <br/> (latent count: 64, context_length: 16, context_overlap: 4, context_stride: 1, closed_loop: True, total steps: 20) |
198
+
199
+ | Node | Inputs |
200
+ |---|---|
201
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c2c8c7ea-66b6-408d-be46-1d805ecd64d1) | 🟦*context_length*: Amount of latents to diffuse at once.<br/> 🟦*context_overlap*: Minimum common latents between adjacent windows.<br/> 🟦*context_stride*: Maximum 2^(stride-1) distance between adjacent latents.<br/> 🟦*closed_loop*: When True, adds additional windows to enhance looping.<br/> 🟦*fuse_method*: Method for averaging results of windows.<br/> 🟦*use_on_equal_length*: When True, allows context to be used when latent count matches context_length - allows loops to be made when latent count == context_length.<br/> 🟦*start_percent*: When multiple Context Options are chained, allows scheduling.<br/> 🟦*guarantee_steps*: When scheduling contexts, determines the *minimum* amount of sampling steps context should be used.<br/> 🟦*context_length*: Amount of latents to diffuse at once.<br/> 🟨*prev_context*: Allows chaining of contexts.<br/> 🟨*view_options*: When context_length > view_length (unless otherwise specified), allows view_options to be used within each context window.|
202
+
203
+ ### Context Options◆Views Only [VRAM⇈]
204
+ | Behavior |
205
+ |---|
206
+ | ![anim__00011](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/f2e422a4-c894-4e89-8f35-1964b89f369d) <br/> (latent count: 64, view_length: 16, view_overlap: 4, View Options◆Standard Static, total steps: 20) |
207
+
208
+ | Node | Inputs |
209
+ |---|---|
210
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/8cd6a0a4-ee8a-46c3-b04b-a100f87025b3) | 🟩*view_opts_req*: View_options to be used across all latents. <br/> 🟨*prev_context*: Allows chaining of contexts.<br/> |
211
+
212
+
213
+ There are View Options equivalent of these schedules:
214
+
215
+ ### View Options◆Standard Static
216
+ | Behavior |
217
+ |---|
218
+ | ![anim__00012](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/7aee4ccb-b669-42fd-a1b5-2005003d5f8d) <br/> (latent count: 64, view_length: 16, view_overlap: 4, Context Options◆Standard Static, context_length: 32, context_overlap: 8, total steps: 20) |
219
+
220
+ | Node | Inputs |
221
+ |---|---|
222
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/4b22c73f-99cb-4781-bd33-e1b3db848207) | 🟦*view_length*: Amount of latents in context to pass into motion model at a time.<br/> 🟦*view_overlap*: Minimum common latents between adjacent windows.<br/> 🟦*fuse_method*: Method for averaging results of windows.<br/> |
223
+
224
+ ### View Options◆Standard Uniform
225
+ | Behavior |
226
+ |---|
227
+ | ![anim__00015](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/faa2cd26-9f94-4fce-90b2-8acec84b444e ) <br/> (latent count: 64, view_length: 16, view_overlap: 4, view_stride: 1, Context Options◆Standard Static, context_length: 32, context_overlap: 8, total steps: 20) |
228
+
229
+ | Node | Inputs |
230
+ |---|---|
231
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/bbf017e6-3545-4043-ba41-fcbe2f54496a) | 🟦*view_length*: Amount of latents in context to pass into motion model at a time.<br/> 🟦*view_overlap*: Minimum common latents between adjacent windows.<br/> 🟦*view_stride*: Maximum 2^(stride-1) distance between adjacent latents.<br/> 🟦*fuse_method*: Method for averaging results of windows.<br/> |
232
+
233
+ ### View Options◆Looped Uniform
234
+ | Behavior |
235
+ |---|
236
+ | ![anim__00016](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/8922b44b-cb19-4b2a-8486-2df8a46bf573) <br/> (latent count: 64, view_length: 16, view_overlap: 4, view_stride: 1, closed_loop: False, Context Options◆Standard Static, context_length: 32, context_overlap: 8, total steps: 20) |
237
+ | NOTE: this one is probably not going to come out looking well unless you are using this for a very specific reason. |
238
+
239
+ | Node | Inputs |
240
+ |---|---|
241
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c58fe4d4-81a8-436b-8028-9e81c2ace18a) | 🟦*view_length*: Amount of latents in context to pass into motion model at a time.<br/> 🟦*view_overlap*: Minimum common latents between adjacent windows.<br/> 🟦*view_stride*: Maximum 2^(stride-1) distance between adjacent latents.<br/> 🟦*closed_loop*: When True, adds additional windows to enhance looping.<br/> 🟦*use_on_equal_length*: When True, allows context to be used when latent count matches context_length - allows loops to be made when latent count == context_length.<br/> 🟦*fuse_method*: Method for averaging results of windows.<br/> |
242
+
243
+ ## Sample Settings
244
+
245
+ The Sample Settings node allows customization of the sampling process beyond what is exposed on most KSampler nodes. With its default values, it will NOT have any effect, and can safely be attached without changing any behavior.
246
+
247
+ TL;DR To use FreeNoise, select ```FreeNoise``` from the noise_type dropdown. FreeNoise does not decrease performance in any way. To use FreeInit, attach the FreeInit Iteration Options to the iteration_opts input. NOTE: FreeInit, despite it's name, works by resampling the latents ```iterations``` amount of times - this means if you use iteration=2, total sampling time will be exactly twice as slow since it will be performing the sampling twice.
248
+
249
+ Noise Layers with the inputs of the same name (or very close to same name) have same intended behavior as the ones for Sample Settings - refer to the inputs below.
250
+
251
+ | Node |
252
+ |---|
253
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/563a13cf-7aed-4acc-9ce3-1556660a34c2) |
254
+
255
+ ### Inputs
256
+ - 🟨*noise_layers*: Customizable, stackable noise to add to/modify initial noise.
257
+ - 🟨*iteration_opts*: Options for determining if (and how) sampling should be repeated consecutively; if you want to check out FreeInit, this is how to use it.
258
+ - 🟨*seed_override*: Accepts a single int to use a seed instead of the seed passed into the KSampler, or a list of ints (like via FizzNodes' BatchedValueSchedule) to assign individual seeds to each latent in the batch.
259
+ - 🟦*seed_offset*: When not set to 0, adds value to current seed, predictably changing it, whatever the original seed may have been.
260
+ - 🟦*batch_offset*: When not set to 0, will 'offset' the noise as if the first latent was actually the batch_offset-nth latent, shifting all the noises over.
261
+ - 🟦*noise_type*: Selects type of noise to be generated. Values include:
262
+ - **default**: generates different noise for all latents as usual.
263
+ - **constant**: generates exact same noise for all latents (based on seed).
264
+ - **empty**: generates no noise for all latents (as if noise was turned off).
265
+ - **repeated_context**: repeats noise every context_length (or view_length) amount of latents; stabilizes longer generations, but has very obvious repetition.
266
+ - **FreeNoise**: repeats noise such that it is repeated every context_length (or view_length), but the overlapped noise between contexts/views is shuffled to make repetition less prevelant while still achieving stabilization.
267
+ - 🟦*seed_gen*: Allows choosing between ComfyUI and Auto1111 methods of noise generation. One is not better than the other (noise distributions are the same), they are just different methods.
268
+ - **comfy**: Noise is generated for the entire latent batch tensor at once based on the provided seed.
269
+ - **auto1111**: Noise is generated individually for each latent, with each latent receiving an increasing +1 seed offset (first latent uses seed, second latent uses seed+1, etc.).
270
+ - 🟦*adapt_denoise_steps*: When True, KSamplers with a 'denoise' input will automatically scale down the total steps to run like the default options in Auto1111.
271
+ - **True**: Steps will decrease with lower denoise, i.e. 20 steps with 0.5 denoise will be 10 total steps executed, but sigmas will be selected that still achieve 0.5 denoise. Trades speed for quality (since less steps are sampled).
272
+ - **False**: Default behavior; 20 steps with 0.5 denoise will execute 20 steps.
273
+
274
+
275
+ ## Iteration Options
276
+
277
+ These options allow KSamplers to re-sample the same latents without needing to chain multiple KSamplers together, and also allows specialized iteration behavior to implement features such as FreeInit.
278
+
279
+ ### Default Iteration Options
280
+
281
+ Simply re-runs the KSampler, plugging in the output of the previous iteration into the next one. At the dafault iterations=1, it is no different than not having this node plugged in at all.
282
+
283
+ | Node | Inputs |
284
+ |---|---|
285
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/23c5e698-6eff-43cc-92e9-488e9b5ca96a) | 🟦*iterations*: Total amount of times KSampler should run back-to-back. <br/> 🟦*iter_batch_offset*: batch_offset to apply on each subsequent iteration. <br/> 🟦*iter_seed_offset*: seed_offset to apply on each subsequent iteration. |
286
+
287
+ ### FreeInit Iteration Options
288
+
289
+ Implements [FreeInit](https://github.com/TianxingWu/FreeInit), which is the idea that AnimateDiff was trained on latents of existing videos (images with temporal coherence between them) that were then noised rather than from random initial noise, and that when noising existing latents, low-frequency data still remains in the noised latents. It combines the low-frequency noise from existing videos (or, as is the default behavior, the previous iteration) with the high-frequency noise in randomly generated noise to run the subsequent iterations. ***Each iteration is a full sample - 2 iterations means it will take twice as long to run as compared to having 1 iteration/no iteration_opts connected.***
290
+
291
+ When apply_to_1st_iter is False, the noising/low-freq/high-freq combination will not occur on the first iteration, with the assumption that there are no useful latents passed in to do the noise combining in the first place, thus requiring at least 2 iterations for FreeInit to take effect.
292
+
293
+ If you have an existing set of latents to use to get low-freq noise from, you may set apply_to_1st_iter to True, and then even if you set iterations=1, FreeInit will still take effect.
294
+
295
+ | Node |
296
+ |---|
297
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/21404e4f-ab67-44ed-8bf9-e510bc2571de) |
298
+
299
+ #### Inputs
300
+ - 🟦*iterations*: Total amount of times KSampler should run back-to-back. Refer to explanation above why it is 2 by default (and when it can be set to 1 instead).
301
+ - 🟦*init_type*: Code implementation for applying FreeInit.
302
+ - ***FreeInit [sampler sigma]***: likely closest to intended implementation, and gets the sigma for noising from the sampler instead of the model (when possible).
303
+ - ***FreeInit [model sigma]***: gets sigma for noising from the model; when using Custom KSampler, this is the method that will be used for both FreeInit options.
304
+ - ***DinkInit_v1***: my initial, flawed implementation of FreeInit before I figured out how to exactly copy the noising behavior. By sheer luck and trial and error, I managed to have it actually sort of work with this method. Mainly for backwards compatibility now, but might produce useful results too.
305
+
306
+ - 🟦*apply_to_1st_iter*: When set to True, will do FreeInit low-freq/high-freq combo work even on the 1st iteration it runs Refer to explanation in the above FreeInit Iteration Options section for when this can be set to True.
307
+ - 🟦*init_type*: Code implementation for applying FreeInit.
308
+ - 🟦*iter_batch_offset*: batch_offset to apply on each subsequent iteration.
309
+ - 🟦*iter_seed_offset*: seed_offset to apply on each subsequent iteration. Defaults to 1 so that new random noise is used for each iteration.
310
+
311
+ - 🟦*filter*: Determines low-freq filter to apply to noise. Very technical, look into code/online resources to figure out how the individual filters act.
312
+ - 🟦*d_s*: Spatial parameter of filter (within latents, I think); very technical. Look into code/online resources if you wish to know what exactly it does.
313
+ - 🟦*d_t*: Temporal parameter of filter (across latents, I think); very technical. Look into code/online resources if you wish to know what exactly it does.
314
+ - 🟦*n_butterworth*: Only applies to ```butterworth``` filter; very technical. Look into code/online resources if you wish to know what exactly it does.
315
+ - 🟦*sigma_step*: Noising step to use/emulate when noising latents to then get low-freq noise out of. 999 actually means last (-1), and any number under 999 will mean the distance away from last. Leave at 999 unless you know what you're trying to do with it.
316
+
317
+
318
+ ## Noise Layers
319
+
320
+ These nodes allow initial noise to be added onto, weighted, or replaced. In near future, I will add the ability for masks to 'move' the noise relative to the masks' movement instead of just 'cutting and pasting' the noise.
321
+
322
+ The inputs that are shared with Sample Settings have the same exact effect - only new option is in seed_gen_override, which by default will use same seed_gen as Sample Settings (use existing). You can make a noise layer use a different seed_gen strategy at will, or use a different seed/set of seeds, etc.
323
+
324
+ The ```mask_optional``` parameter determines where on the initial noise the noise layer should be applied.
325
+
326
+ | Node | Behavior + Inputs |
327
+ |---|---|
328
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/66487969-669d-47d3-9742-85ae26606903) | [Add]; Adds noise directly on top. <br/> 🟦*noise_weight*: Multiplier for noise layer before being added on top. |
329
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/52acb25c-9116-4594-b3fb-01b7b15bb79d) | [Add Weighted]; Adds noise, but takes a weighted average between what is already there and itself. <br/> 🟦*noise_weight*: Weight of new noise in the weighted average with existing noise. <br/> 🟦*balance_multipler*: Scale for how much noise_weight should affect existing noise; 1.0 means normal weighted average, and below 1.0 will lessen the weighted reduction by that amount (i.e. if balance_multiplier is set to 0.5 and noise_weight is 0.25, existing noise will only be reduced by 0.125 instead of 0.25, but new noise will be added with the unmodified 0.25 weight). |
330
+ | ![image](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/4feb586e-9920-4f35-8f92-e2e36fabb2df) | [Replace]; Directly replaces existing noise from layers underneath with itself. |
331
+
332
+
333
+ # Samples (download or drag images of the workflows into ComfyUI to instantly load the corresponding workflows!)
334
+
335
+ NOTE: I've scaled down the gifs to 0.75x size to make them take up less space on the README.
336
+
337
+ ### txt2img
338
+
339
+ | Result |
340
+ |---|
341
+ | ![readme_00006](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/b615a4aa-db3e-4b24-b88f-b694e52f6364) |
342
+ | Workflow |
343
+ | ![t2i_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/6eb47506-b503-482b-9baf-4c238f30a9c2) |
344
+
345
+ ### txt2img - (prompt travel)
346
+
347
+ | Result |
348
+ |---|
349
+ | ![readme_00010](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c27a2029-2c69-4272-b40f-64408e9e2ea6) |
350
+ | Workflow |
351
+ | ![t2i_prompttravel_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/e5a72ea1-628d-423e-98ed-f20e1bcc5320) |
352
+
353
+
354
+
355
+ ### txt2img - 48 frame animation with 16 context_length (Context Options◆Standard Static) + FreeNoise
356
+
357
+ | Result |
358
+ |---|
359
+ | ![readme_00012](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/684f6e79-d653-482f-899a-1900dc56cd8f) |
360
+ | Workflow |
361
+ | ![t2i_context_freenoise_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/9d0e53fa-49d6-483d-a660-3f41d7451002) |
362
+
363
+
364
+ # Old Samples (TODO: update all of these + add new ones when I get sleep)
365
+
366
+ ### txt2img - 32 frame animation with 16 context_length (uniform) - PanLeft and ZoomOut Motion LoRAs
367
+
368
+ ![t2i_context_mlora_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/41ec4141-389c-4ef4-ae3e-a963a0fa841f)
369
+
370
+ ![aaa_readme_00094_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/14abee9a-5500-4d14-8632-15ac77ba5709)
371
+
372
+ [aaa_readme_00095_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/d730ae2e-188c-4a61-8a6d-bd48f60a2d07)
373
+
374
+
375
+ ### txt2img w/ latent upscale (partial denoise on upscale)
376
+
377
+ ![t2i_lat_ups_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/521991dd-8e39-4fed-9970-514507c75067)
378
+
379
+ ![aaa_readme_up_00001_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/f4199e25-c839-41ed-8986-fb7dbbe2ac52)
380
+
381
+ [aaa_readme_up_00002_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/2f44342f-3fd8-4863-8e3d-360377d608b7)
382
+
383
+
384
+
385
+ ### txt2img w/ latent upscale (partial denoise on upscale) - PanLeft and ZoomOut Motion LoRAs
386
+
387
+ ![t2i_mlora_lat_ups_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/f34882de-7dd4-4264-8f59-e24da350be2a)
388
+
389
+ ![aaa_readme_up_00023_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/e2ca5c0c-b5d9-42de-b877-4ed29db81eb9)
390
+
391
+ [aaa_readme_up_00024_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/414c16d8-231c-422f-8dfc-a93d4b68ffcc)
392
+
393
+
394
+
395
+ ### txt2img w/ latent upscale (partial denoise on upscale) - 48 frame animation with 16 context_length (uniform)
396
+
397
+ ![t2i_lat_ups_full_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/a1ebc14e-853e-4cda-9cda-9a7553fa3d85)
398
+
399
+ [aaa_readme_up_00009_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/f7a45f81-e700-4bfe-9fdd-fbcaa4fa8a4e)
400
+
401
+
402
+
403
+ ### txt2img w/ latent upscale (full denoise on upscale)
404
+
405
+ ![t2i_lat_ups_full_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/5058f201-3f52-4c48-ac7e-525c3c8f3df3)
406
+
407
+ ![aaa_readme_up_00010_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/804610de-18ec-43af-9af2-4a83cf31d16b)
408
+
409
+ [aaa_readme_up_00012_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/3eb575cf-92dd-434a-b3db-1a2064ff0033)
410
+
411
+
412
+
413
+ ### txt2img w/ latent upscale (full denoise on upscale) - 48 frame animation with 16 context_length (uniform)
414
+
415
+ ![t2i_context_lat_ups_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/7b9ec22b-d4e0-4083-9846-5743ed90583e)
416
+
417
+ [aaa_readme_up_00014_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/034aff4c-f814-4b87-b5d1-407b1089af0d)
418
+
419
+
420
+
421
+ ### txt2img w/ ControlNet-stabilized latent-upscale (partial denoise on upscale, Scaled Soft ControlNet Weights)
422
+
423
+ ![t2i_lat_ups_softcontrol_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c769c2bd-5aac-48d0-92b7-d73c422d4863)
424
+
425
+ ![aaa_readme_up_00017_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/221954cc-95df-4e0c-8ec9-266d0108dad4)
426
+
427
+ [aaa_readme_up_00019_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/b562251d-a4fb-4141-94dd-9f8bca9f3ce8)
428
+
429
+
430
+
431
+ ### txt2img w/ ControlNet-stabilized latent-upscale (partial denoise on upscale, Scaled Soft ControlNet Weights) 48 frame animation with 16 context_length (uniform)
432
+
433
+ ![t2i_context_lat_ups_softcontrol_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/798567a8-4ef0-4814-aeeb-4f770df8d783)
434
+
435
+ [aaa_readme_up_00003_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/0f57c949-0af3-4da4-b7c4-5c1fb1549927)
436
+
437
+
438
+
439
+ ### txt2img w/ Initial ControlNet input (using Normal LineArt preprocessor on first txt2img as an example)
440
+
441
+ ![t2i_initcn_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/caa7abdf-7ba0-456c-9fa4-547944ea6e72)
442
+
443
+ ![aaa_readme_cn_00002_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/055ef87c-50c6-4bb9-b35e-dd97916b47cc)
444
+
445
+ [aaa_readme_cn_00003_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/9c9d425d-2378-4af0-8464-2c6c0d1a68bf)
446
+
447
+
448
+
449
+ ### txt2img w/ Initial ControlNet input (using Normal LineArt preprocessor on first txt2img 48 frame as an example) 48 frame animation with 16 context_length (uniform)
450
+
451
+ ![t2i_context_initcn_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/f9de2711-dcfd-4fea-8b3b-31e3794fbff9)
452
+
453
+ ![aaa_readme_cn_00005_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/6bf14361-5b09-4305-b2a7-f7babad4bd14)
454
+
455
+ [aaa_readme_cn_00006_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/5d3665b7-c2da-46a1-88d8-ab43ba8eb0c6)
456
+
457
+
458
+
459
+ ### txt2img w/ Initial ControlNet input (using OpenPose images) + latent upscale w/ full denoise
460
+
461
+ ![t2i_openpose_upscale_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/306a40c4-0591-496d-a320-c33f0fc4b3d2)
462
+
463
+ (open_pose images provided courtesy of toyxyz)
464
+
465
+ ![AA_openpose_cn_gif_00001_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff/assets/7365912/23291941-864d-495a-8ba8-d02e05756396)
466
+
467
+ ![aaa_readme_cn_00032_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/621a2ca6-2f08-4ed1-96ad-8e6635303173)
468
+
469
+ [aaa_readme_cn_00033_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/c5df09a5-8c64-4811-9ecf-57ac73d82377)
470
+
471
+
472
+
473
+ ### txt2img w/ Initial ControlNet input (using OpenPose images) + latent upscale w/ full denoise, 48 frame animation with 16 context_length (uniform)
474
+
475
+ ![t2i_context_openpose_upscale_wf](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/a931af6f-bf6a-40d3-bd55-1d7bad32e665)
476
+
477
+ (open_pose images provided courtesy of toyxyz)
478
+
479
+ ![aaa_readme_preview_00002_](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/028a1e9e-37b5-477d-8665-0e8723306d65)
480
+
481
+ [aaa_readme_cn_00024_.webm](https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved/assets/7365912/8f4c840c-06a2-4c64-b97e-568dd5ff6f46)
482
+
483
+
484
+
485
+ ### img2img
486
+
487
+ TODO: fill this out with a few useful ways, some using control net tile. I'm sorry there is nothing here right now, I have a lot of code to write. I'll try to fill this section out + Advance ControlNet use piece by piece.
488
+
489
+
490
+
491
+ ## Known Issues
492
+
493
+ ### Some motion models have visible watermark on resulting images (especially when using mm_sd_v15)
494
+
495
+ Training data used by the authors of the AnimateDiff paper contained Shutterstock watermarks. Since mm_sd_v15 was finetuned on finer, less drastic movement, the motion module attempts to replicate the transparency of that watermark and does not get blurred away like mm_sd_v14. Using other motion modules, or combinations of them using Advanced KSamplers should alleviate watermark issues.
custom_nodes/ComfyUI-AnimateDiff-Evolved/__init__.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import folder_paths
2
+ from .animatediff.logger import logger
3
+ from .animatediff.utils_model import get_available_motion_models, Folders
4
+ from .animatediff.nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
5
+
6
+ if len(get_available_motion_models()) == 0:
7
+ logger.error(f"No motion models found. Please download one and place in: {folder_paths.get_folder_paths(Folders.ANIMATEDIFF_MODELS)}")
8
+
9
+ WEB_DIRECTORY = "./web"
10
+ __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/ad_settings.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import Tensor
2
+
3
+ from .utils_motion import normalize_min_max
4
+
5
+
6
+ class AnimateDiffSettings:
7
+ def __init__(self,
8
+ adjust_pe: 'AdjustPEGroup'=None,
9
+ pe_strength: float=1.0,
10
+ attn_strength: float=1.0,
11
+ attn_q_strength: float=1.0,
12
+ attn_k_strength: float=1.0,
13
+ attn_v_strength: float=1.0,
14
+ attn_out_weight_strength: float=1.0,
15
+ attn_out_bias_strength: float=1.0,
16
+ other_strength: float=1.0,
17
+ attn_scale: float=1.0,
18
+ mask_attn_scale: Tensor=None,
19
+ mask_attn_scale_min: float=1.0,
20
+ mask_attn_scale_max: float=1.0,
21
+ ):
22
+ # PE-interpolation settings
23
+ self.adjust_pe = adjust_pe if adjust_pe is not None else AdjustPEGroup()
24
+ # general strengths
25
+ self.pe_strength = pe_strength
26
+ self.attn_strength = attn_strength
27
+ self.other_strength = other_strength
28
+ # specific attn strengths
29
+ self.attn_q_strength = attn_q_strength
30
+ self.attn_k_strength = attn_k_strength
31
+ self.attn_v_strength = attn_v_strength
32
+ self.attn_out_weight_strength = attn_out_weight_strength
33
+ self.attn_out_bias_strength = attn_out_bias_strength
34
+ # attention scale settings - DEPRECATED
35
+ self.attn_scale = attn_scale
36
+ # attention scale mask settings - DEPRECATED
37
+ self.mask_attn_scale = mask_attn_scale.clone() if mask_attn_scale is not None else mask_attn_scale
38
+ self.mask_attn_scale_min = mask_attn_scale_min
39
+ self.mask_attn_scale_max = mask_attn_scale_max
40
+ self._prepare_mask_attn_scale()
41
+
42
+ def _prepare_mask_attn_scale(self):
43
+ if self.mask_attn_scale is not None:
44
+ self.mask_attn_scale = normalize_min_max(self.mask_attn_scale, self.mask_attn_scale_min, self.mask_attn_scale_max)
45
+
46
+ def has_mask_attn_scale(self) -> bool:
47
+ return self.mask_attn_scale is not None
48
+
49
+ def has_pe_strength(self) -> bool:
50
+ return self.pe_strength != 1.0
51
+
52
+ def has_attn_strength(self) -> bool:
53
+ return self.attn_strength != 1.0
54
+
55
+ def has_other_strength(self) -> bool:
56
+ return self.other_strength != 1.0
57
+
58
+ def has_anything_to_apply(self) -> bool:
59
+ return self.adjust_pe.has_anything_to_apply() \
60
+ or self.has_pe_strength() \
61
+ or self.has_attn_strength() \
62
+ or self.has_other_strength() \
63
+ or self.has_any_attn_sub_strength()
64
+
65
+ def has_any_attn_sub_strength(self) -> bool:
66
+ return self.has_attn_q_strength() \
67
+ or self.has_attn_k_strength() \
68
+ or self.has_attn_v_strength() \
69
+ or self.has_attn_out_weight_strength() \
70
+ or self.has_attn_out_bias_strength()
71
+
72
+ def has_attn_q_strength(self) -> bool:
73
+ return self.attn_q_strength != 1.0
74
+
75
+ def has_attn_k_strength(self) -> bool:
76
+ return self.attn_k_strength != 1.0
77
+
78
+ def has_attn_v_strength(self) -> bool:
79
+ return self.attn_v_strength != 1.0
80
+
81
+ def has_attn_out_weight_strength(self) -> bool:
82
+ return self.attn_out_weight_strength != 1.0
83
+
84
+ def has_attn_out_bias_strength(self) -> bool:
85
+ return self.attn_out_bias_strength != 1.0
86
+
87
+
88
+ class AdjustPE:
89
+ def __init__(self,
90
+ cap_initial_pe_length: int=0, interpolate_pe_to_length: int=0,
91
+ initial_pe_idx_offset: int=0, final_pe_idx_offset: int=0,
92
+ motion_pe_stretch: int=0, print_adjustment=False):
93
+ # PE-interpolation settings
94
+ self.cap_initial_pe_length = cap_initial_pe_length
95
+ self.interpolate_pe_to_length = interpolate_pe_to_length
96
+ self.initial_pe_idx_offset = initial_pe_idx_offset
97
+ self.final_pe_idx_offset = final_pe_idx_offset
98
+ self.motion_pe_stretch = motion_pe_stretch
99
+ self.print_adjustment = print_adjustment
100
+
101
+ def has_cap_initial_pe_length(self) -> bool:
102
+ return self.cap_initial_pe_length > 0
103
+
104
+ def has_interpolate_pe_to_length(self) -> bool:
105
+ return self.interpolate_pe_to_length > 0
106
+
107
+ def has_initial_pe_idx_offset(self) -> bool:
108
+ return self.initial_pe_idx_offset > 0
109
+
110
+ def has_final_pe_idx_offset(self) -> bool:
111
+ return self.final_pe_idx_offset > 0
112
+
113
+ def has_motion_pe_stretch(self) -> bool:
114
+ return self.motion_pe_stretch > 0
115
+
116
+ def has_anything_to_apply(self) -> bool:
117
+ return self.has_cap_initial_pe_length() \
118
+ or self.has_interpolate_pe_to_length() \
119
+ or self.has_initial_pe_idx_offset() \
120
+ or self.has_final_pe_idx_offset() \
121
+ or self.has_motion_pe_stretch()
122
+
123
+
124
+ class AdjustPEGroup:
125
+ def __init__(self, initial: AdjustPE=None):
126
+ self.adjusts: list[AdjustPE] = []
127
+ if initial is not None:
128
+ self.add(initial)
129
+
130
+ def add(self, adjust_pe: AdjustPE):
131
+ self.adjusts.append(adjust_pe)
132
+
133
+ def has_anything_to_apply(self):
134
+ for adjust in self.adjusts:
135
+ if adjust.has_anything_to_apply():
136
+ return True
137
+ return False
138
+
139
+ def clone(self):
140
+ new_group = AdjustPEGroup()
141
+ for adjust in self.adjusts:
142
+ new_group.add(adjust)
143
+ return new_group
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/context.py ADDED
@@ -0,0 +1,389 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Callable, Optional, Union
2
+
3
+ import numpy as np
4
+ from torch import Tensor
5
+
6
+ from comfy.model_base import BaseModel
7
+
8
+ from .utils_motion import get_sorted_list_via_attr
9
+
10
+ class ContextFuseMethod:
11
+ FLAT = "flat"
12
+ PYRAMID = "pyramid"
13
+ RELATIVE = "relative"
14
+
15
+ LIST = [PYRAMID, FLAT]
16
+ LIST_STATIC = [PYRAMID, RELATIVE, FLAT]
17
+
18
+
19
+ class ContextType:
20
+ UNIFORM_WINDOW = "uniform window"
21
+
22
+
23
+ class ContextOptions:
24
+ def __init__(self, context_length: int=None, context_stride: int=None, context_overlap: int=None,
25
+ context_schedule: str=None, closed_loop: bool=False, fuse_method: str=ContextFuseMethod.FLAT,
26
+ use_on_equal_length: bool=False, view_options: 'ContextOptions'=None,
27
+ start_percent=0.0, guarantee_steps=1):
28
+ # permanent settings
29
+ self.context_length = context_length
30
+ self.context_stride = context_stride
31
+ self.context_overlap = context_overlap
32
+ self.context_schedule = context_schedule
33
+ self.closed_loop = closed_loop
34
+ self.fuse_method = fuse_method
35
+ self.sync_context_to_pe = False # this feature is likely bad and stay unused, so I might remove this
36
+ self.use_on_equal_length = use_on_equal_length
37
+ self.view_options = view_options.clone() if view_options else view_options
38
+ # scheduling
39
+ self.start_percent = float(start_percent)
40
+ self.start_t = 999999999.9
41
+ self.guarantee_steps = guarantee_steps
42
+ # temporary vars
43
+ self._step: int = 0
44
+
45
+ @property
46
+ def step(self):
47
+ return self._step
48
+ @step.setter
49
+ def step(self, value: int):
50
+ self._step = value
51
+ if self.view_options:
52
+ self.view_options.step = value
53
+
54
+ def clone(self):
55
+ n = ContextOptions(context_length=self.context_length, context_stride=self.context_stride,
56
+ context_overlap=self.context_overlap, context_schedule=self.context_schedule,
57
+ closed_loop=self.closed_loop, fuse_method=self.fuse_method,
58
+ use_on_equal_length=self.use_on_equal_length, view_options=self.view_options,
59
+ start_percent=self.start_percent, guarantee_steps=self.guarantee_steps)
60
+ n.start_t = self.start_t
61
+ return n
62
+
63
+
64
+ class ContextOptionsGroup:
65
+ def __init__(self):
66
+ self.contexts: list[ContextOptions] = []
67
+ self._current_context: ContextOptions = None
68
+ self._current_used_steps: int = 0
69
+ self._current_index: int = 0
70
+ self.step = 0
71
+
72
+ def reset(self):
73
+ self._current_context = None
74
+ self._current_used_steps = 0
75
+ self._current_index = 0
76
+ self.step = 0
77
+ self._set_first_as_current()
78
+
79
+ @classmethod
80
+ def default(cls):
81
+ def_context = ContextOptions()
82
+ new_group = ContextOptionsGroup()
83
+ new_group.add(def_context)
84
+ return new_group
85
+
86
+ def add(self, context: ContextOptions):
87
+ # add to end of list, then sort
88
+ self.contexts.append(context)
89
+ self.contexts = get_sorted_list_via_attr(self.contexts, "start_percent")
90
+ self._set_first_as_current()
91
+
92
+ def add_to_start(self, context: ContextOptions):
93
+ # add to start of list, then sort
94
+ self.contexts.insert(0, context)
95
+ self.contexts = get_sorted_list_via_attr(self.contexts, "start_percent")
96
+ self._set_first_as_current()
97
+
98
+ def has_index(self, index: int) -> int:
99
+ return index >=0 and index < len(self.contexts)
100
+
101
+ def is_empty(self) -> bool:
102
+ return len(self.contexts) == 0
103
+
104
+ def clone(self):
105
+ cloned = ContextOptionsGroup()
106
+ for context in self.contexts:
107
+ cloned.contexts.append(context)
108
+ cloned._set_first_as_current()
109
+ return cloned
110
+
111
+ def initialize_timesteps(self, model: BaseModel):
112
+ for context in self.contexts:
113
+ context.start_t = model.model_sampling.percent_to_sigma(context.start_percent)
114
+
115
+ def prepare_current_context(self, t: Tensor):
116
+ curr_t: float = t[0]
117
+ prev_index = self._current_index
118
+ # if met guaranteed steps, look for next context in case need to switch
119
+ if self._current_used_steps >= self._current_context.guarantee_steps:
120
+ # if has next index, loop through and see if need to switch
121
+ if self.has_index(self._current_index+1):
122
+ for i in range(self._current_index+1, len(self.contexts)):
123
+ eval_c = self.contexts[i]
124
+ # check if start_t is greater or equal to curr_t
125
+ # NOTE: t is in terms of sigmas, not percent, so bigger number = earlier step in sampling
126
+ if eval_c.start_t >= curr_t:
127
+ self._current_index = i
128
+ self._current_context = eval_c
129
+ self._current_used_steps = 0
130
+ # if guarantee_steps greater than zero, stop searching for other keyframes
131
+ if self._current_context.guarantee_steps > 0:
132
+ break
133
+ # if eval_c is outside the percent range, stop looking further
134
+ else:
135
+ break
136
+ # update steps current context is used
137
+ self._current_used_steps += 1
138
+
139
+ def _set_first_as_current(self):
140
+ if len(self.contexts) > 0:
141
+ self._current_context = self.contexts[0]
142
+
143
+ # properties shadow those of ContextOptions
144
+ @property
145
+ def context_length(self):
146
+ return self._current_context.context_length
147
+
148
+ @property
149
+ def context_overlap(self):
150
+ return self._current_context.context_overlap
151
+
152
+ @property
153
+ def context_stride(self):
154
+ return self._current_context.context_stride
155
+
156
+ @property
157
+ def context_schedule(self):
158
+ return self._current_context.context_schedule
159
+
160
+ @property
161
+ def closed_loop(self):
162
+ return self._current_context.closed_loop
163
+
164
+ @property
165
+ def fuse_method(self):
166
+ return self._current_context.fuse_method
167
+
168
+ @property
169
+ def use_on_equal_length(self):
170
+ return self._current_context.use_on_equal_length
171
+
172
+ @property
173
+ def view_options(self):
174
+ return self._current_context.view_options
175
+
176
+
177
+ class ContextSchedules:
178
+ UNIFORM_LOOPED = "looped_uniform"
179
+ UNIFORM_STANDARD = "standard_uniform"
180
+ STATIC_STANDARD = "standard_static"
181
+ BATCHED = "batched"
182
+ VIEW_AS_CONTEXT = "view_as_context"
183
+
184
+ LEGACY_UNIFORM_LOOPED = "uniform"
185
+ LEGACY_UNIFORM_SCHEDULE_LIST = [LEGACY_UNIFORM_LOOPED]
186
+
187
+
188
+ # from https://github.com/neggles/animatediff-cli/blob/main/src/animatediff/pipelines/context.py
189
+ def create_windows_uniform_looped(num_frames: int, opts: Union[ContextOptionsGroup, ContextOptions]):
190
+ windows = []
191
+ if num_frames < opts.context_length:
192
+ windows.append(list(range(num_frames)))
193
+ return windows
194
+
195
+ context_stride = min(opts.context_stride, int(np.ceil(np.log2(num_frames / opts.context_length))) + 1)
196
+ # obtain uniform windows as normal, looping and all
197
+ for context_step in 1 << np.arange(context_stride):
198
+ pad = int(round(num_frames * ordered_halving(opts.step)))
199
+ for j in range(
200
+ int(ordered_halving(opts.step) * context_step) + pad,
201
+ num_frames + pad + (0 if opts.closed_loop else -opts.context_overlap),
202
+ (opts.context_length * context_step - opts.context_overlap),
203
+ ):
204
+ windows.append([e % num_frames for e in range(j, j + opts.context_length * context_step, context_step)])
205
+
206
+ return windows
207
+
208
+
209
+ def create_windows_uniform_standard(num_frames: int, opts: Union[ContextOptionsGroup, ContextOptions]):
210
+ # unlike looped, uniform_straight does NOT allow windows that loop back to the beginning;
211
+ # instead, they get shifted to the corresponding end of the frames.
212
+ # in the case that a window (shifted or not) is identical to the previous one, it gets skipped.
213
+ windows = []
214
+ if num_frames <= opts.context_length:
215
+ windows.append(list(range(num_frames)))
216
+ return windows
217
+
218
+ context_stride = min(opts.context_stride, int(np.ceil(np.log2(num_frames / opts.context_length))) + 1)
219
+ # first, obtain uniform windows as normal, looping and all
220
+ for context_step in 1 << np.arange(context_stride):
221
+ pad = int(round(num_frames * ordered_halving(opts.step)))
222
+ for j in range(
223
+ int(ordered_halving(opts.step) * context_step) + pad,
224
+ num_frames + pad + (-opts.context_overlap),
225
+ (opts.context_length * context_step - opts.context_overlap),
226
+ ):
227
+ windows.append([e % num_frames for e in range(j, j + opts.context_length * context_step, context_step)])
228
+
229
+ # now that windows are created, shift any windows that loop, and delete duplicate windows
230
+ delete_idxs = []
231
+ win_i = 0
232
+ while win_i < len(windows):
233
+ # if window is rolls over itself, need to shift it
234
+ is_roll, roll_idx = does_window_roll_over(windows[win_i], num_frames)
235
+ if is_roll:
236
+ roll_val = windows[win_i][roll_idx] # roll_val might not be 0 for windows of higher strides
237
+ shift_window_to_end(windows[win_i], num_frames=num_frames)
238
+ # check if next window (cyclical) is missing roll_val
239
+ if roll_val not in windows[(win_i+1) % len(windows)]:
240
+ # need to insert new window here - just insert window starting at roll_val
241
+ windows.insert(win_i+1, list(range(roll_val, roll_val + opts.context_length)))
242
+ # delete window if it's not unique
243
+ for pre_i in range(0, win_i):
244
+ if windows[win_i] == windows[pre_i]:
245
+ delete_idxs.append(win_i)
246
+ break
247
+ win_i += 1
248
+
249
+ # reverse delete_idxs so that they will be deleted in an order that doesn't break idx correlation
250
+ delete_idxs.reverse()
251
+ for i in delete_idxs:
252
+ windows.pop(i)
253
+
254
+ return windows
255
+
256
+
257
+ def create_windows_static_standard(num_frames: int, opts: Union[ContextOptionsGroup, ContextOptions]):
258
+ windows = []
259
+ if num_frames <= opts.context_length:
260
+ windows.append(list(range(num_frames)))
261
+ return windows
262
+ # always return the same set of windows
263
+ delta = opts.context_length - opts.context_overlap
264
+ for start_idx in range(0, num_frames, delta):
265
+ # if past the end of frames, move start_idx back to allow same context_length
266
+ ending = start_idx + opts.context_length
267
+ if ending >= num_frames:
268
+ final_delta = ending - num_frames
269
+ final_start_idx = start_idx - final_delta
270
+ windows.append(list(range(final_start_idx, final_start_idx + opts.context_length)))
271
+ break
272
+ windows.append(list(range(start_idx, start_idx + opts.context_length)))
273
+ return windows
274
+
275
+
276
+ def create_windows_batched(num_frames: int, opts: Union[ContextOptionsGroup, ContextOptions]):
277
+ windows = []
278
+ if num_frames <= opts.context_length:
279
+ windows.append(list(range(num_frames)))
280
+ return windows
281
+ # always return the same set of windows;
282
+ # no overlap, just cut up based on context_length;
283
+ # last window size will be different if num_frames % opts.context_length != 0
284
+ for start_idx in range(0, num_frames, opts.context_length):
285
+ windows.append(list(range(start_idx, min(start_idx + opts.context_length, num_frames))))
286
+ return windows
287
+
288
+
289
+ def create_windows_default(num_frames: int, opts: Union[ContextOptionsGroup, ContextOptions]):
290
+ return [list(range(num_frames))]
291
+
292
+
293
+ def get_context_windows(num_frames: int, opts: Union[ContextOptionsGroup, ContextOptions]):
294
+ context_func = CONTEXT_MAPPING.get(opts.context_schedule, None)
295
+ if not context_func:
296
+ raise ValueError(f"Unknown context_schedule '{opts.context_schedule}'.")
297
+ return context_func(num_frames, opts)
298
+
299
+
300
+ CONTEXT_MAPPING = {
301
+ ContextSchedules.UNIFORM_LOOPED: create_windows_uniform_looped,
302
+ ContextSchedules.UNIFORM_STANDARD: create_windows_uniform_standard,
303
+ ContextSchedules.STATIC_STANDARD: create_windows_static_standard,
304
+ ContextSchedules.BATCHED: create_windows_batched,
305
+ ContextSchedules.VIEW_AS_CONTEXT: create_windows_default, # just return all to allow Views to do all the work
306
+ }
307
+
308
+
309
+ def get_context_weights(num_frames: int, fuse_method: str):
310
+ weights_func = FUSE_MAPPING.get(fuse_method, None)
311
+ if not weights_func:
312
+ raise ValueError(f"Unknown fuse_method '{fuse_method}'.")
313
+ return weights_func(num_frames)
314
+
315
+
316
+ def create_weights_flat(length: int, **kwargs) -> list[float]:
317
+ # weight is the same for all
318
+ return [1.0] * length
319
+
320
+
321
+ def create_weights_pyramid(length: int, **kwargs) -> list[float]:
322
+ # weight is based on the distance away from the edge of the context window;
323
+ # based on weighted average concept in FreeNoise paper
324
+ if length % 2 == 0:
325
+ max_weight = length // 2
326
+ weight_sequence = list(range(1, max_weight + 1, 1)) + list(range(max_weight, 0, -1))
327
+ else:
328
+ max_weight = (length + 1) // 2
329
+ weight_sequence = list(range(1, max_weight, 1)) + [max_weight] + list(range(max_weight - 1, 0, -1))
330
+ return weight_sequence
331
+
332
+
333
+ FUSE_MAPPING = {
334
+ ContextFuseMethod.FLAT: create_weights_flat,
335
+ ContextFuseMethod.PYRAMID: create_weights_pyramid,
336
+ ContextFuseMethod.RELATIVE: create_weights_pyramid,
337
+ }
338
+
339
+
340
+ # Returns fraction that has denominator that is a power of 2
341
+ def ordered_halving(val):
342
+ # get binary value, padded with 0s for 64 bits
343
+ bin_str = f"{val:064b}"
344
+ # flip binary value, padding included
345
+ bin_flip = bin_str[::-1]
346
+ # convert binary to int
347
+ as_int = int(bin_flip, 2)
348
+ # divide by 1 << 64, equivalent to 2**64, or 18446744073709551616,
349
+ # or b10000000000000000000000000000000000000000000000000000000000000000 (1 with 64 zero's)
350
+ return as_int / (1 << 64)
351
+
352
+
353
+ def get_missing_indexes(windows: list[list[int]], num_frames: int) -> list[int]:
354
+ all_indexes = list(range(num_frames))
355
+ for w in windows:
356
+ for val in w:
357
+ try:
358
+ all_indexes.remove(val)
359
+ except ValueError:
360
+ pass
361
+ return all_indexes
362
+
363
+
364
+ def does_window_roll_over(window: list[int], num_frames: int) -> tuple[bool, int]:
365
+ prev_val = -1
366
+ for i, val in enumerate(window):
367
+ val = val % num_frames
368
+ if val < prev_val:
369
+ return True, i
370
+ prev_val = val
371
+ return False, -1
372
+
373
+
374
+ def shift_window_to_start(window: list[int], num_frames: int):
375
+ start_val = window[0]
376
+ for i in range(len(window)):
377
+ # 1) subtract each element by start_val to move vals relative to the start of all frames
378
+ # 2) add num_frames and take modulus to get adjusted vals
379
+ window[i] = ((window[i] - start_val) + num_frames) % num_frames
380
+
381
+
382
+ def shift_window_to_end(window: list[int], num_frames: int):
383
+ # 1) shift window to start
384
+ shift_window_to_start(window, num_frames)
385
+ end_val = window[-1]
386
+ end_delta = num_frames - end_val - 1
387
+ for i in range(len(window)):
388
+ # 2) add end_delta to each val to slide windows to end
389
+ window[i] = window[i] + end_delta
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/freeinit.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # S-Lab License 1.0
2
+
3
+ # Copyright 2023 S-Lab
4
+ # Redistribution and use for non-commercial purpose in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+ # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
8
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9
+ # 4. In the event that redistribution and/or use for commercial purpose in source or binary forms, with or without modification is required, please contact the contributor(s) of the work.
10
+
11
+ # Code has been modified from https://github.com/TianxingWu/FreeInit
12
+
13
+ import torch
14
+ import torch.fft as fft
15
+ import math
16
+
17
+
18
+ class FreeInitFilter:
19
+ GAUSSIAN = "gaussian"
20
+ IDEAL = "ideal"
21
+ BOX = "box"
22
+ BUTTERWORTH = "butterworth"
23
+
24
+ LIST = [GAUSSIAN, BUTTERWORTH, IDEAL, BOX]
25
+
26
+
27
+ def freq_mix_3d(x, noise, LPF):
28
+ """
29
+ Noise reinitialization.
30
+
31
+ Args:
32
+ x: diffused latent
33
+ noise: randomly sampled noise
34
+ LPF: low pass filter
35
+ """
36
+ # FFT
37
+ x_freq = fft.fftn(x, dim=(-4, -2, -1))
38
+ x_freq = fft.fftshift(x_freq, dim=(-4, -2, -1))
39
+ noise_freq = fft.fftn(noise, dim=(-4, -2, -1))
40
+ noise_freq = fft.fftshift(noise_freq, dim=(-4, -2, -1))
41
+
42
+ # frequency mix
43
+ HPF = 1 - LPF
44
+ x_freq_low = x_freq * LPF
45
+ noise_freq_high = noise_freq * HPF
46
+ x_freq_mixed = x_freq_low + noise_freq_high # mix in freq domain
47
+
48
+ # IFFT
49
+ x_freq_mixed = fft.ifftshift(x_freq_mixed, dim=(-4, -2, -1))
50
+ x_mixed = fft.ifftn(x_freq_mixed, dim=(-4, -2, -1)).real
51
+
52
+ return x_mixed
53
+
54
+
55
+ def get_freq_filter(shape, device, filter_type, n, d_s, d_t):
56
+ """
57
+ Form the frequency filter for noise reinitialization.
58
+
59
+ Args:
60
+ shape: shape of latent (T, C, H, W)
61
+ filter_type: type of the freq filter
62
+ n: (only for butterworth) order of the filter, larger n ~ ideal, smaller n ~ gaussian
63
+ d_s: normalized stop frequency for spatial dimensions (0.0-1.0)
64
+ d_t: normalized stop frequency for temporal dimension (0.0-1.0)
65
+ """
66
+ if filter_type == FreeInitFilter.GAUSSIAN:
67
+ return gaussian_low_pass_filter(shape=shape, d_s=d_s, d_t=d_t).to(device)
68
+ elif filter_type == FreeInitFilter.IDEAL:
69
+ return ideal_low_pass_filter(shape=shape, d_s=d_s, d_t=d_t).to(device)
70
+ elif filter_type == FreeInitFilter.BOX:
71
+ return box_low_pass_filter(shape=shape, d_s=d_s, d_t=d_t).to(device)
72
+ elif filter_type == FreeInitFilter.BUTTERWORTH:
73
+ return butterworth_low_pass_filter(shape=shape, n=n, d_s=d_s, d_t=d_t).to(device)
74
+ else:
75
+ raise NotImplementedError
76
+
77
+ def gaussian_low_pass_filter(shape, d_s=0.25, d_t=0.25):
78
+ """
79
+ Compute the gaussian low pass filter mask.
80
+
81
+ Args:
82
+ shape: shape of the filter (volume)
83
+ d_s: normalized stop frequency for spatial dimensions (0.0-1.0)
84
+ d_t: normalized stop frequency for temporal dimension (0.0-1.0)
85
+ """
86
+ T, H, W = shape[-4], shape[-2], shape[-1]
87
+ mask = torch.zeros(shape)
88
+ if d_s==0 or d_t==0:
89
+ return mask
90
+ for t in range(T):
91
+ for h in range(H):
92
+ for w in range(W):
93
+ d_square = (((d_s/d_t)*(2*t/T-1))**2 + (2*h/H-1)**2 + (2*w/W-1)**2)
94
+ mask[t, ..., h,w] = math.exp(-1/(2*d_s**2) * d_square)
95
+ return mask
96
+
97
+
98
+ def butterworth_low_pass_filter(shape, n=4, d_s=0.25, d_t=0.25):
99
+ """
100
+ Compute the butterworth low pass filter mask.
101
+
102
+ Args:
103
+ shape: shape of the filter (volume)
104
+ n: order of the filter, larger n ~ ideal, smaller n ~ gaussian
105
+ d_s: normalized stop frequency for spatial dimensions (0.0-1.0)
106
+ d_t: normalized stop frequency for temporal dimension (0.0-1.0)
107
+ """
108
+ T, H, W = shape[-4], shape[-2], shape[-1]
109
+ mask = torch.zeros(shape)
110
+ if d_s==0 or d_t==0:
111
+ return mask
112
+ for t in range(T):
113
+ for h in range(H):
114
+ for w in range(W):
115
+ d_square = (((d_s/d_t)*(2*t/T-1))**2 + (2*h/H-1)**2 + (2*w/W-1)**2)
116
+ mask[t, ..., h,w] = 1 / (1 + (d_square / d_s**2)**n)
117
+ return mask
118
+
119
+
120
+ def ideal_low_pass_filter(shape, d_s=0.25, d_t=0.25):
121
+ """
122
+ Compute the ideal low pass filter mask.
123
+
124
+ Args:
125
+ shape: shape of the filter (volume)
126
+ d_s: normalized stop frequency for spatial dimensions (0.0-1.0)
127
+ d_t: normalized stop frequency for temporal dimension (0.0-1.0)
128
+ """
129
+ T, H, W = shape[-4], shape[-2], shape[-1]
130
+ mask = torch.zeros(shape)
131
+ if d_s==0 or d_t==0:
132
+ return mask
133
+ for t in range(T):
134
+ for h in range(H):
135
+ for w in range(W):
136
+ d_square = (((d_s/d_t)*(2*t/T-1))**2 + (2*h/H-1)**2 + (2*w/W-1)**2)
137
+ mask[t, ...,h,w] = 1 if d_square <= d_s*2 else 0
138
+ return mask
139
+
140
+
141
+ def box_low_pass_filter(shape, d_s=0.25, d_t=0.25):
142
+ """
143
+ Compute the ideal low pass filter mask (approximated version).
144
+
145
+ Args:
146
+ shape: shape of the filter (volume)
147
+ d_s: normalized stop frequency for spatial dimensions (0.0-1.0)
148
+ d_t: normalized stop frequency for temporal dimension (0.0-1.0)
149
+ """
150
+ T, H, W = shape[-4], shape[-2], shape[-1]
151
+ mask = torch.zeros(shape)
152
+ if d_s==0 or d_t==0:
153
+ return mask
154
+
155
+ threshold_s = round(int(H // 2) * d_s)
156
+ threshold_t = round(T // 2 * d_t)
157
+
158
+ cframe, crow, ccol = T // 2, H // 2, W //2
159
+ mask[cframe - threshold_t:cframe + threshold_t, ..., crow - threshold_s:crow + threshold_s, ccol - threshold_s:ccol + threshold_s] = 1.0
160
+
161
+ return mask
162
+
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/logger.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+ import logging
3
+ import sys
4
+
5
+
6
+ class ColoredFormatter(logging.Formatter):
7
+ COLORS = {
8
+ "DEBUG": "\033[0;36m", # CYAN
9
+ "INFO": "\033[0;32m", # GREEN
10
+ "WARNING": "\033[0;33m", # YELLOW
11
+ "ERROR": "\033[0;31m", # RED
12
+ "CRITICAL": "\033[0;37;41m", # WHITE ON RED
13
+ "RESET": "\033[0m", # RESET COLOR
14
+ }
15
+
16
+ def format(self, record):
17
+ colored_record = copy.copy(record)
18
+ levelname = colored_record.levelname
19
+ seq = self.COLORS.get(levelname, self.COLORS["RESET"])
20
+ colored_record.levelname = f"{seq}{levelname}{self.COLORS['RESET']}"
21
+ return super().format(colored_record)
22
+
23
+
24
+ # Create a new logger
25
+ logger = logging.getLogger("AnimateDiffEvo")
26
+ logger.propagate = False
27
+
28
+ # Add handler if we don't have one.
29
+ if not logger.handlers:
30
+ handler = logging.StreamHandler(sys.stdout)
31
+ handler.setFormatter(ColoredFormatter("[%(name)s] - %(levelname)s - %(message)s"))
32
+ logger.addHandler(handler)
33
+
34
+ # Configure logger
35
+ loglevel = logging.INFO
36
+ logger.setLevel(loglevel)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/model_injection.py ADDED
@@ -0,0 +1,581 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+ from typing import Union
3
+
4
+ from einops import rearrange
5
+ from torch import Tensor
6
+ import torch.nn.functional as F
7
+ import torch
8
+
9
+ import comfy.model_management
10
+ import comfy.utils
11
+ from comfy.model_patcher import ModelPatcher
12
+ from comfy.model_base import BaseModel
13
+
14
+ from .ad_settings import AnimateDiffSettings
15
+ from .context import ContextOptions, ContextOptions, ContextOptionsGroup
16
+ from .motion_module_ad import AnimateDiffModel, AnimateDiffFormat, has_mid_block, normalize_ad_state_dict
17
+ from .logger import logger
18
+ from .utils_motion import ADKeyframe, ADKeyframeGroup, MotionCompatibilityError, get_combined_multival, normalize_min_max
19
+ from .motion_lora import MotionLoraInfo, MotionLoraList
20
+ from .utils_model import get_motion_lora_path, get_motion_model_path, get_sd_model_type
21
+ from .sample_settings import SampleSettings, SeedNoiseGeneration
22
+
23
+
24
+ # some motion_model casts here might fail if model becomes metatensor or is not castable;
25
+ # should not really matter if it fails, so ignore raised Exceptions
26
+ class ModelPatcherAndInjector(ModelPatcher):
27
+ def __init__(self, m: ModelPatcher):
28
+ # replicate ModelPatcher.clone() to initialize ModelPatcherAndInjector
29
+ super().__init__(m.model, m.load_device, m.offload_device, m.size, m.current_device, weight_inplace_update=m.weight_inplace_update)
30
+ self.patches = {}
31
+ for k in m.patches:
32
+ self.patches[k] = m.patches[k][:]
33
+
34
+ self.object_patches = m.object_patches.copy()
35
+ self.model_options = copy.deepcopy(m.model_options)
36
+ self.model_keys = m.model_keys
37
+
38
+ # injection stuff
39
+ self.motion_injection_params: InjectionParams = None
40
+ self.sample_settings: SampleSettings = SampleSettings()
41
+ self.motion_models: MotionModelGroup = None
42
+
43
+ def model_patches_to(self, device):
44
+ super().model_patches_to(device)
45
+ if self.motion_models is not None:
46
+ for motion_model in self.motion_models.models:
47
+ try:
48
+ motion_model.model.to(device)
49
+ except Exception:
50
+ pass
51
+
52
+ def patch_model(self, device_to=None):
53
+ # first, perform model patching
54
+ patched_model = super().patch_model(device_to)
55
+ # finally, perform motion model injection
56
+ self.inject_model(device_to=device_to)
57
+ return patched_model
58
+
59
+ def unpatch_model(self, device_to=None):
60
+ # first, eject motion model from unet
61
+ self.eject_model(device_to=device_to)
62
+ # finally, do normal model unpatching
63
+ return super().unpatch_model(device_to)
64
+
65
+ def inject_model(self, device_to=None):
66
+ if self.motion_models is not None:
67
+ for motion_model in self.motion_models.models:
68
+ motion_model.model.inject(self)
69
+ try:
70
+ motion_model.model.to(device_to)
71
+ except Exception:
72
+ pass
73
+
74
+ def eject_model(self, device_to=None):
75
+ if self.motion_models is not None:
76
+ for motion_model in self.motion_models.models:
77
+ motion_model.model.eject(self)
78
+ try:
79
+ motion_model.model.to(device_to)
80
+ except Exception:
81
+ pass
82
+
83
+ def clone(self):
84
+ cloned = ModelPatcherAndInjector(self)
85
+ cloned.motion_models = self.motion_models.clone() if self.motion_models else self.motion_models
86
+ cloned.sample_settings = self.sample_settings
87
+ cloned.motion_injection_params = self.motion_injection_params.clone() if self.motion_injection_params else self.motion_injection_params
88
+ return cloned
89
+
90
+
91
+ class MotionModelPatcher(ModelPatcher):
92
+ # Mostly here so that type hints work in IDEs
93
+ def __init__(self, *args, **kwargs):
94
+ super().__init__(*args, **kwargs)
95
+ self.model: AnimateDiffModel = self.model
96
+ self.timestep_percent_range = (0.0, 1.0)
97
+ self.timestep_range: tuple[float, float] = None
98
+ self.keyframes: ADKeyframeGroup = ADKeyframeGroup()
99
+
100
+ self.scale_multival = None
101
+ self.effect_multival = None
102
+ # temporary variables
103
+ self.current_used_steps = 0
104
+ self.current_keyframe: ADKeyframe = None
105
+ self.current_index = -1
106
+ self.current_scale: Union[float, Tensor] = None
107
+ self.current_effect: Union[float, Tensor] = None
108
+ self.combined_scale: Union[float, Tensor] = None
109
+ self.combined_effect: Union[float, Tensor] = None
110
+ self.was_within_range = False
111
+
112
+ def patch_model(self, *args, **kwargs):
113
+ # patch as normal, but prepare_weights so that lowvram meta device works properly
114
+ patched_model = super().patch_model(*args, **kwargs)
115
+ self.prepare_weights()
116
+ return patched_model
117
+
118
+ def prepare_weights(self):
119
+ # in case lowvram is active and meta device is used, need to convert weights
120
+ # otherwise, will get exceptions thrown related to meta device
121
+ # TODO: with new comfy lowvram system, this is unnecessary
122
+ state_dict = self.model.state_dict()
123
+ for key in state_dict:
124
+ weight = comfy.model_management.resolve_lowvram_weight(state_dict[key], self.model, key)
125
+ try:
126
+ comfy.utils.set_attr(self.model, key, weight)
127
+ except Exception:
128
+ pass
129
+
130
+ def pre_run(self, model: ModelPatcherAndInjector):
131
+ self.cleanup()
132
+ self.model.reset()
133
+ # just in case, prepare_weights before every run
134
+ self.prepare_weights()
135
+ self.model.set_scale(self.scale_multival)
136
+ self.model.set_effect(self.effect_multival)
137
+
138
+ def initialize_timesteps(self, model: BaseModel):
139
+ self.timestep_range = (model.model_sampling.percent_to_sigma(self.timestep_percent_range[0]),
140
+ model.model_sampling.percent_to_sigma(self.timestep_percent_range[1]))
141
+ if self.keyframes is not None:
142
+ for keyframe in self.keyframes.keyframes:
143
+ keyframe.start_t = model.model_sampling.percent_to_sigma(keyframe.start_percent)
144
+
145
+ def prepare_current_keyframe(self, t: Tensor):
146
+ curr_t: float = t[0]
147
+ prev_index = self.current_index
148
+ # if met guaranteed steps, look for next keyframe in case need to switch
149
+ if self.current_keyframe is None or self.current_used_steps >= self.current_keyframe.guarantee_steps:
150
+ # if has next index, loop through and see if need to switch
151
+ if self.keyframes.has_index(self.current_index+1):
152
+ for i in range(self.current_index+1, len(self.keyframes)):
153
+ eval_kf = self.keyframes[i]
154
+ # check if start_t is greater or equal to curr_t
155
+ # NOTE: t is in terms of sigmas, not percent, so bigger number = earlier step in sampling
156
+ if eval_kf.start_t >= curr_t:
157
+ self.current_index = i
158
+ self.current_keyframe = eval_kf
159
+ self.current_used_steps = 0
160
+ # keep track of scale and effect multivals, accounting for inherit_missing
161
+ if self.current_keyframe.has_scale():
162
+ self.current_scale = self.current_keyframe.scale_multival
163
+ elif not self.current_keyframe.inherit_missing:
164
+ self.current_scale = None
165
+ if self.current_keyframe.has_effect():
166
+ self.current_effect = self.current_keyframe.effect_multival
167
+ elif not self.current_keyframe.inherit_missing:
168
+ self.current_effect = None
169
+ # if guarantee_steps greater than zero, stop searching for other keyframes
170
+ if self.current_keyframe.guarantee_steps > 0:
171
+ break
172
+ # if eval_kf is outside the percent range, stop looking further
173
+ else:
174
+ break
175
+ # if index changed, apply new combined values
176
+ if prev_index != self.current_index:
177
+ # combine model's scale and effect with keyframe's scale and effect
178
+ self.combined_scale = get_combined_multival(self.scale_multival, self.current_scale)
179
+ self.combined_effect = get_combined_multival(self.effect_multival, self.current_effect)
180
+ # apply scale and effect
181
+ self.model.set_scale(self.combined_scale)
182
+ self.model.set_effect(self.combined_effect)
183
+ # apply effect - if not within range, set effect to 0, effectively turning model off
184
+ if curr_t > self.timestep_range[0] or curr_t < self.timestep_range[1]:
185
+ self.model.set_effect(0.0)
186
+ self.was_within_range = False
187
+ else:
188
+ # if was not in range last step, apply effect to toggle AD status
189
+ if not self.was_within_range:
190
+ self.model.set_effect(self.combined_effect)
191
+ self.was_within_range = True
192
+ # update steps current keyframe is used
193
+ self.current_used_steps += 1
194
+
195
+ def cleanup(self):
196
+ if self.model is not None:
197
+ self.model.cleanup()
198
+ self.current_used_steps = 0
199
+ self.current_keyframe = None
200
+ self.current_index = -1
201
+ self.current_scale = None
202
+ self.current_effect = None
203
+ self.combined_scale = None
204
+ self.combined_effect = None
205
+ self.was_within_range = False
206
+
207
+ def clone(self):
208
+ # normal ModelPatcher clone actions
209
+ n = MotionModelPatcher(self.model, self.load_device, self.offload_device, self.size, self.current_device, weight_inplace_update=self.weight_inplace_update)
210
+ n.patches = {}
211
+ for k in self.patches:
212
+ n.patches[k] = self.patches[k][:]
213
+
214
+ n.object_patches = self.object_patches.copy()
215
+ n.model_options = copy.deepcopy(self.model_options)
216
+ n.model_keys = self.model_keys
217
+ # extra cloned params
218
+ n.timestep_percent_range = self.timestep_percent_range
219
+ n.timestep_range = self.timestep_range
220
+ n.keyframes = self.keyframes.clone()
221
+ n.scale_multival = self.scale_multival
222
+ n.effect_multival = self.effect_multival
223
+ return n
224
+
225
+
226
+ class MotionModelGroup:
227
+ def __init__(self, init_motion_model: MotionModelPatcher=None):
228
+ self.models: list[MotionModelPatcher] = []
229
+ if init_motion_model is not None:
230
+ self.add(init_motion_model)
231
+
232
+ def add(self, mm: MotionModelPatcher):
233
+ # add to end of list
234
+ self.models.append(mm)
235
+
236
+ def add_to_start(self, mm: MotionModelPatcher):
237
+ self.models.insert(0, mm)
238
+
239
+ def __getitem__(self, index) -> MotionModelPatcher:
240
+ return self.models[index]
241
+
242
+ def is_empty(self) -> bool:
243
+ return len(self.models) == 0
244
+
245
+ def clone(self) -> 'MotionModelGroup':
246
+ cloned = MotionModelGroup()
247
+ for mm in self.models:
248
+ cloned.add(mm)
249
+ return cloned
250
+
251
+ def set_sub_idxs(self, sub_idxs: list[int]):
252
+ for motion_model in self.models:
253
+ motion_model.model.set_sub_idxs(sub_idxs=sub_idxs)
254
+
255
+ def set_view_options(self, view_options: ContextOptions):
256
+ for motion_model in self.models:
257
+ motion_model.model.set_view_options(view_options)
258
+
259
+ def set_video_length(self, video_length: int, full_length: int):
260
+ for motion_model in self.models:
261
+ motion_model.model.set_video_length(video_length=video_length, full_length=full_length)
262
+
263
+ def initialize_timesteps(self, model: BaseModel):
264
+ for motion_model in self.models:
265
+ motion_model.initialize_timesteps(model)
266
+
267
+ def pre_run(self, model: ModelPatcherAndInjector):
268
+ for motion_model in self.models:
269
+ motion_model.pre_run(model)
270
+
271
+ def prepare_current_keyframe(self, t: Tensor):
272
+ for motion_model in self.models:
273
+ motion_model.prepare_current_keyframe(t=t)
274
+
275
+ def get_name_string(self, show_version=False):
276
+ identifiers = []
277
+ for motion_model in self.models:
278
+ id = motion_model.model.mm_info.mm_name
279
+ if show_version:
280
+ id += f":{motion_model.model.mm_info.mm_version}"
281
+ identifiers.append(id)
282
+ return ", ".join(identifiers)
283
+
284
+
285
+ def get_vanilla_model_patcher(m: ModelPatcher) -> ModelPatcher:
286
+ model = ModelPatcher(m.model, m.load_device, m.offload_device, m.size, m.current_device, weight_inplace_update=m.weight_inplace_update)
287
+ model.patches = {}
288
+ for k in m.patches:
289
+ model.patches[k] = m.patches[k][:]
290
+
291
+ model.object_patches = m.object_patches.copy()
292
+ model.model_options = copy.deepcopy(m.model_options)
293
+ model.model_keys = m.model_keys
294
+ return model
295
+
296
+ # adapted from https://github.com/guoyww/AnimateDiff/blob/main/animatediff/utils/convert_lora_safetensor_to_diffusers.py
297
+ # Example LoRA keys:
298
+ # down_blocks.0.motion_modules.0.temporal_transformer.transformer_blocks.0.attention_blocks.0.processor.to_q_lora.down.weight
299
+ # down_blocks.0.motion_modules.0.temporal_transformer.transformer_blocks.0.attention_blocks.0.processor.to_q_lora.up.weight
300
+ #
301
+ # Example model keys:
302
+ # down_blocks.0.motion_modules.0.temporal_transformer.transformer_blocks.0.attention_blocks.0.to_q.weight
303
+ #
304
+ def load_motion_lora_as_patches(motion_model: MotionModelPatcher, lora: MotionLoraInfo) -> None:
305
+ def get_version(has_midblock: bool):
306
+ return "v2" if has_midblock else "v1"
307
+
308
+ lora_path = get_motion_lora_path(lora.name)
309
+ logger.info(f"Loading motion LoRA {lora.name}")
310
+ state_dict = comfy.utils.load_torch_file(lora_path)
311
+
312
+ # remove all non-temporal keys (in case model has extra stuff in it)
313
+ for key in list(state_dict.keys()):
314
+ if "temporal" not in key:
315
+ del state_dict[key]
316
+ if len(state_dict) == 0:
317
+ raise ValueError(f"'{lora.name}' contains no temporal keys; it is not a valid motion LoRA!")
318
+
319
+ model_has_midblock = motion_model.model.mid_block != None
320
+ lora_has_midblock = has_mid_block(state_dict)
321
+ logger.info(f"Applying a {get_version(lora_has_midblock)} LoRA ({lora.name}) to a { motion_model.model.mm_info.mm_version} motion model.")
322
+
323
+ patches = {}
324
+ # convert lora state dict to one that matches motion_module keys and tensors
325
+ for key in state_dict:
326
+ # if motion_module doesn't have a midblock, skip mid_block entries
327
+ if not model_has_midblock:
328
+ if "mid_block" in key: continue
329
+ # only process lora down key (we will process up at the same time as down)
330
+ if "up." in key: continue
331
+
332
+ # get up key version of down key
333
+ up_key = key.replace(".down.", ".up.")
334
+
335
+ # adapt key to match motion_module key format - remove 'processor.', '_lora', 'down.', and 'up.'
336
+ model_key = key.replace("processor.", "").replace("_lora", "").replace("down.", "").replace("up.", "")
337
+ # motion_module keys have a '0.' after all 'to_out.' weight keys
338
+ model_key = model_key.replace("to_out.", "to_out.0.")
339
+
340
+ weight_down = state_dict[key]
341
+ weight_up = state_dict[up_key]
342
+ # actual weights obtained by matrix multiplication of up and down weights
343
+ # save as a tuple, so that (Motion)ModelPatcher's calculate_weight function detects len==1, applying it correctly
344
+ patches[model_key] = (torch.mm(weight_up, weight_down),)
345
+ del state_dict
346
+ # add patches to motion ModelPatcher
347
+ motion_model.add_patches(patches=patches, strength_patch=lora.strength)
348
+
349
+
350
+ def load_motion_module_gen1(model_name: str, model: ModelPatcher, motion_lora: MotionLoraList = None, motion_model_settings: AnimateDiffSettings = None) -> MotionModelPatcher:
351
+ model_path = get_motion_model_path(model_name)
352
+ logger.info(f"Loading motion module {model_name}")
353
+ mm_state_dict = comfy.utils.load_torch_file(model_path, safe_load=True)
354
+ # TODO: check for empty state dict?
355
+ # get normalized state_dict and motion model info
356
+ mm_state_dict, mm_info = normalize_ad_state_dict(mm_state_dict=mm_state_dict, mm_name=model_name)
357
+ # check that motion model is compatible with sd model
358
+ model_sd_type = get_sd_model_type(model)
359
+ if model_sd_type != mm_info.sd_type:
360
+ raise MotionCompatibilityError(f"Motion module '{mm_info.mm_name}' is intended for {mm_info.sd_type} models, " \
361
+ + f"but the provided model is type {model_sd_type}.")
362
+ # apply motion model settings
363
+ mm_state_dict = apply_mm_settings(model_dict=mm_state_dict, mm_settings=motion_model_settings)
364
+ # initialize AnimateDiffModelWrapper
365
+ ad_wrapper = AnimateDiffModel(mm_state_dict=mm_state_dict, mm_info=mm_info)
366
+ ad_wrapper.to(model.model_dtype())
367
+ ad_wrapper.to(model.offload_device)
368
+ is_animatelcm = mm_info.mm_format==AnimateDiffFormat.ANIMATELCM
369
+ load_result = ad_wrapper.load_state_dict(mm_state_dict, strict=not is_animatelcm)
370
+ # TODO: report load_result of motion_module loading?
371
+ # wrap motion_module into a ModelPatcher, to allow motion lora patches
372
+ motion_model = MotionModelPatcher(model=ad_wrapper, load_device=model.load_device, offload_device=model.offload_device)
373
+ # load motion_lora, if present
374
+ if motion_lora is not None:
375
+ for lora in motion_lora.loras:
376
+ load_motion_lora_as_patches(motion_model, lora)
377
+ return motion_model
378
+
379
+
380
+ def load_motion_module_gen2(model_name: str, motion_model_settings: AnimateDiffSettings = None) -> MotionModelPatcher:
381
+ model_path = get_motion_model_path(model_name)
382
+ logger.info(f"Loading motion module {model_name} via Gen2")
383
+ mm_state_dict = comfy.utils.load_torch_file(model_path, safe_load=True)
384
+ # TODO: check for empty state dict?
385
+ # get normalized state_dict and motion model info (converts alternate AD models like HotshotXL into AD keys)
386
+ mm_state_dict, mm_info = normalize_ad_state_dict(mm_state_dict=mm_state_dict, mm_name=model_name)
387
+ # apply motion model settings
388
+ mm_state_dict = apply_mm_settings(model_dict=mm_state_dict, mm_settings=motion_model_settings)
389
+ # initialize AnimateDiffModelWrapper
390
+ ad_wrapper = AnimateDiffModel(mm_state_dict=mm_state_dict, mm_info=mm_info)
391
+ ad_wrapper.to(comfy.model_management.unet_dtype())
392
+ ad_wrapper.to(comfy.model_management.unet_offload_device())
393
+ is_animatelcm = mm_info.mm_format==AnimateDiffFormat.ANIMATELCM
394
+ load_result = ad_wrapper.load_state_dict(mm_state_dict, strict=not is_animatelcm)
395
+ # TODO: manually check load_results for AnimateLCM models
396
+ if is_animatelcm:
397
+ pass
398
+ # TODO: report load_result of motion_module loading?
399
+ # wrap motion_module into a ModelPatcher, to allow motion lora patches
400
+ motion_model = MotionModelPatcher(model=ad_wrapper, load_device=comfy.model_management.get_torch_device(),
401
+ offload_device=comfy.model_management.unet_offload_device())
402
+ return motion_model
403
+
404
+
405
+ def create_fresh_motion_module(motion_model: MotionModelPatcher) -> MotionModelPatcher:
406
+ ad_wrapper = AnimateDiffModel(mm_state_dict=motion_model.model.state_dict(), mm_info=motion_model.model.mm_info)
407
+ ad_wrapper.to(comfy.model_management.unet_dtype())
408
+ ad_wrapper.to(comfy.model_management.unet_offload_device())
409
+ ad_wrapper.load_state_dict(motion_model.model.state_dict())
410
+ return MotionModelPatcher(model=ad_wrapper, load_device=comfy.model_management.get_torch_device(),
411
+ offload_device=comfy.model_management.unet_offload_device())
412
+
413
+
414
+ def validate_model_compatibility_gen2(model: ModelPatcher, motion_model: MotionModelPatcher):
415
+ # check that motion model is compatible with sd model
416
+ model_sd_type = get_sd_model_type(model)
417
+ mm_info = motion_model.model.mm_info
418
+ if model_sd_type != mm_info.sd_type:
419
+ raise MotionCompatibilityError(f"Motion module '{mm_info.mm_name}' is intended for {mm_info.sd_type} models, " \
420
+ + f"but the provided model is type {model_sd_type}.")
421
+
422
+
423
+ def interpolate_pe_to_length(model_dict: dict[str, Tensor], key: str, new_length: int):
424
+ pe_shape = model_dict[key].shape
425
+ temp_pe = rearrange(model_dict[key], "(t b) f d -> t b f d", t=1)
426
+ temp_pe = F.interpolate(temp_pe, size=(new_length, pe_shape[-1]), mode="bilinear")
427
+ temp_pe = rearrange(temp_pe, "t b f d -> (t b) f d", t=1)
428
+ model_dict[key] = temp_pe
429
+ del temp_pe
430
+
431
+
432
+ def interpolate_pe_to_length_diffs(model_dict: dict[str, Tensor], key: str, new_length: int):
433
+ # TODO: fill out and try out
434
+ pe_shape = model_dict[key].shape
435
+ temp_pe = rearrange(model_dict[key], "(t b) f d -> t b f d", t=1)
436
+ temp_pe = F.interpolate(temp_pe, size=(new_length, pe_shape[-1]), mode="bilinear")
437
+ temp_pe = rearrange(temp_pe, "t b f d -> (t b) f d", t=1)
438
+ model_dict[key] = temp_pe
439
+ del temp_pe
440
+
441
+
442
+ def interpolate_pe_to_length_pingpong(model_dict: dict[str, Tensor], key: str, new_length: int):
443
+ if model_dict[key].shape[1] < new_length:
444
+ temp_pe = model_dict[key]
445
+ flipped_temp_pe = torch.flip(temp_pe[:, 1:-1, :], [1])
446
+ use_flipped = True
447
+ preview_pe = None
448
+ while model_dict[key].shape[1] < new_length:
449
+ preview_pe = model_dict[key]
450
+ model_dict[key] = torch.cat([model_dict[key], flipped_temp_pe if use_flipped else temp_pe], dim=1)
451
+ use_flipped = not use_flipped
452
+ del temp_pe
453
+ del flipped_temp_pe
454
+ del preview_pe
455
+ model_dict[key] = model_dict[key][:, :new_length]
456
+
457
+
458
+ def freeze_mask_of_pe(model_dict: dict[str, Tensor], key: str):
459
+ pe_portion = model_dict[key].shape[2] // 64
460
+ first_pe = model_dict[key][:,:1,:]
461
+ model_dict[key][:,:,pe_portion:] = first_pe[:,:,pe_portion:]
462
+ del first_pe
463
+
464
+
465
+ def freeze_mask_of_attn(model_dict: dict[str, Tensor], key: str):
466
+ attn_portion = model_dict[key].shape[0] // 2
467
+ model_dict[key][:attn_portion,:attn_portion] *= 1.5
468
+
469
+
470
+ def apply_mm_settings(model_dict: dict[str, Tensor], mm_settings: AnimateDiffSettings) -> dict[str, Tensor]:
471
+ if mm_settings is None:
472
+ return model_dict
473
+ if not mm_settings.has_anything_to_apply():
474
+ return model_dict
475
+ # first, handle PE Adjustments
476
+ for adjust in mm_settings.adjust_pe.adjusts:
477
+ if adjust.has_anything_to_apply():
478
+ already_printed = False
479
+ for key in model_dict:
480
+ if "attention_blocks" in key and "pos_encoder" in key:
481
+ # apply simple motion pe stretch, if needed
482
+ if adjust.has_motion_pe_stretch():
483
+ original_length = model_dict[key].shape[1]
484
+ new_pe_length = original_length + adjust.motion_pe_stretch
485
+ interpolate_pe_to_length(model_dict, key, new_length=new_pe_length)
486
+ if adjust.print_adjustment and not already_printed:
487
+ logger.info(f"[Adjust PE]: PE Stretch from {original_length} to {new_pe_length}.")
488
+ # apply pe_idx_offset, if needed
489
+ if adjust.has_initial_pe_idx_offset():
490
+ original_length = model_dict[key].shape[1]
491
+ model_dict[key] = model_dict[key][:, adjust.initial_pe_idx_offset:]
492
+ if adjust.print_adjustment and not already_printed:
493
+ logger.info(f"[Adjust PE]: Offsetting PEs by {adjust.initial_pe_idx_offset}; PE length to shortens from {original_length} to {model_dict[key].shape[1]}.")
494
+ # apply has_cap_initial_pe_length, if needed
495
+ if adjust.has_cap_initial_pe_length():
496
+ original_length = model_dict[key].shape[1]
497
+ model_dict[key] = model_dict[key][:, :adjust.cap_initial_pe_length]
498
+ if adjust.print_adjustment and not already_printed:
499
+ logger.info(f"[Adjust PE]: Capping PEs (initial) from {original_length} to {model_dict[key].shape[1]}.")
500
+ # apply interpolate_pe_to_length, if needed
501
+ if adjust.has_interpolate_pe_to_length():
502
+ original_length = model_dict[key].shape[1]
503
+ interpolate_pe_to_length(model_dict, key, new_length=adjust.interpolate_pe_to_length)
504
+ if adjust.print_adjustment and not already_printed:
505
+ logger.info(f"[Adjust PE]: Interpolating PE length from {original_length} to {model_dict[key].shape[1]}.")
506
+ # apply final_pe_idx_offset, if needed
507
+ if adjust.has_final_pe_idx_offset():
508
+ original_length = model_dict[key].shape[1]
509
+ model_dict[key] = model_dict[key][:, adjust.final_pe_idx_offset:]
510
+ if adjust.print_adjustment and not already_printed:
511
+ logger.info(f"[Adjust PE]: Capping PEs (final) from {original_length} to {model_dict[key].shape[1]}.")
512
+ already_printed = True
513
+ # finally, apply any weight changes
514
+ for key in model_dict:
515
+ if "attention_blocks" in key:
516
+ if "pos_encoder" in key and mm_settings.adjust_pe.has_anything_to_apply():
517
+ # apply pe_strength, if needed
518
+ if mm_settings.has_pe_strength():
519
+ model_dict[key] *= mm_settings.pe_strength
520
+ else:
521
+ # apply attn_strenth, if needed
522
+ if mm_settings.has_attn_strength():
523
+ model_dict[key] *= mm_settings.attn_strength
524
+ # apply specific attn_strengths, if needed
525
+ if mm_settings.has_any_attn_sub_strength():
526
+ if "to_q" in key and mm_settings.has_attn_q_strength():
527
+ model_dict[key] *= mm_settings.attn_q_strength
528
+ elif "to_k" in key and mm_settings.has_attn_k_strength():
529
+ model_dict[key] *= mm_settings.attn_k_strength
530
+ elif "to_v" in key and mm_settings.has_attn_v_strength():
531
+ model_dict[key] *= mm_settings.attn_v_strength
532
+ elif "to_out" in key:
533
+ if key.strip().endswith("weight") and mm_settings.has_attn_out_weight_strength():
534
+ model_dict[key] *= mm_settings.attn_out_weight_strength
535
+ elif key.strip().endswith("bias") and mm_settings.has_attn_out_bias_strength():
536
+ model_dict[key] *= mm_settings.attn_out_bias_strength
537
+ # apply other strength, if needed
538
+ elif mm_settings.has_other_strength():
539
+ model_dict[key] *= mm_settings.other_strength
540
+ return model_dict
541
+
542
+
543
+ class InjectionParams:
544
+ def __init__(self, unlimited_area_hack: bool=False, apply_mm_groupnorm_hack: bool=True, model_name: str="",
545
+ apply_v2_properly: bool=True) -> None:
546
+ self.full_length = None
547
+ self.unlimited_area_hack = unlimited_area_hack
548
+ self.apply_mm_groupnorm_hack = apply_mm_groupnorm_hack
549
+ self.model_name = model_name
550
+ self.apply_v2_properly = apply_v2_properly
551
+ self.context_options: ContextOptionsGroup = ContextOptionsGroup.default()
552
+ self.motion_model_settings = AnimateDiffSettings() # Gen1
553
+ self.sub_idxs = None # value should NOT be included in clone, so it will auto reset
554
+
555
+ def set_noise_extra_args(self, noise_extra_args: dict):
556
+ noise_extra_args["context_options"] = self.context_options.clone()
557
+
558
+ def set_context(self, context_options: ContextOptionsGroup):
559
+ self.context_options = context_options.clone() if context_options else ContextOptionsGroup.default()
560
+
561
+ def is_using_sliding_context(self) -> bool:
562
+ return self.context_options.context_length is not None
563
+
564
+ def set_motion_model_settings(self, motion_model_settings: AnimateDiffSettings): # Gen1
565
+ if motion_model_settings is None:
566
+ self.motion_model_settings = AnimateDiffSettings()
567
+ else:
568
+ self.motion_model_settings = motion_model_settings
569
+
570
+ def reset_context(self):
571
+ self.context_options = ContextOptionsGroup.default()
572
+
573
+ def clone(self) -> 'InjectionParams':
574
+ new_params = InjectionParams(
575
+ self.unlimited_area_hack, self.apply_mm_groupnorm_hack,
576
+ self.model_name, apply_v2_properly=self.apply_v2_properly,
577
+ )
578
+ new_params.full_length = self.full_length
579
+ new_params.set_context(self.context_options)
580
+ new_params.set_motion_model_settings(self.motion_model_settings) # Gen1
581
+ return new_params
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/motion_lora.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class MotionLoraInfo:
2
+ def __init__(self, name: str, strength: float = 1.0, hash: str=""):
3
+ self.name = name
4
+ self.strength = strength
5
+ self.hash = ""
6
+
7
+ def set_hash(self, hash: str):
8
+ self.hash = hash
9
+
10
+ def clone(self):
11
+ return MotionLoraInfo(self.name, self.strength, self.hash)
12
+
13
+
14
+ class MotionLoraList:
15
+ def __init__(self):
16
+ self.loras: list[MotionLoraInfo] = []
17
+
18
+ def add_lora(self, lora: MotionLoraInfo):
19
+ self.loras.append(lora)
20
+
21
+ def clone(self):
22
+ new_list = MotionLoraList()
23
+ for lora in self.loras:
24
+ new_list.add_lora(lora.clone())
25
+ return new_list
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/motion_module_ad.py ADDED
@@ -0,0 +1,971 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from typing import Iterable, Tuple, Union
3
+ import re
4
+
5
+ import torch
6
+ from einops import rearrange, repeat
7
+ from torch import Tensor, nn
8
+
9
+ from comfy.ldm.modules.attention import FeedForward, SpatialTransformer
10
+ from comfy.model_patcher import ModelPatcher
11
+ from comfy.ldm.modules.diffusionmodules import openaimodel
12
+ from comfy.ldm.modules.diffusionmodules.openaimodel import SpatialTransformer
13
+ from comfy.controlnet import broadcast_image_to
14
+ from comfy.utils import repeat_to_batch_size
15
+ import comfy.ops
16
+ import comfy.model_management
17
+
18
+ from .context import ContextFuseMethod, ContextOptions, get_context_weights, get_context_windows
19
+ from .utils_motion import CrossAttentionMM, MotionCompatibilityError, extend_to_batch_size, prepare_mask_batch
20
+ from .utils_model import BetaSchedules, ModelTypeSD
21
+ from .logger import logger
22
+
23
+
24
+ def zero_module(module):
25
+ # Zero out the parameters of a module and return it.
26
+ for p in module.parameters():
27
+ p.detach().zero_()
28
+ return module
29
+
30
+
31
+ class AnimateDiffFormat:
32
+ ANIMATEDIFF = "AnimateDiff"
33
+ HOTSHOTXL = "HotshotXL"
34
+ ANIMATELCM = "AnimateLCM"
35
+
36
+
37
+ class AnimateDiffVersion:
38
+ V1 = "v1"
39
+ V2 = "v2"
40
+ V3 = "v3"
41
+
42
+
43
+ class AnimateDiffInfo:
44
+ def __init__(self, sd_type: str, mm_format: str, mm_version: str, mm_name: str):
45
+ self.sd_type = sd_type
46
+ self.mm_format = mm_format
47
+ self.mm_version = mm_version
48
+ self.mm_name = mm_name
49
+
50
+ def get_string(self):
51
+ return f"{self.mm_name}:{self.mm_version}:{self.mm_format}:{self.sd_type}"
52
+
53
+
54
+ def is_hotshotxl(mm_state_dict: dict[str, Tensor]) -> bool:
55
+ # use pos_encoder naming to determine if hotshotxl model
56
+ for key in mm_state_dict.keys():
57
+ if key.endswith("pos_encoder.positional_encoding"):
58
+ return True
59
+ return False
60
+
61
+
62
+ def is_animatelcm(mm_state_dict: dict[str, Tensor]) -> bool:
63
+ # use lack of ANY pos_encoder keys to determine if animatelcm model
64
+ for key in mm_state_dict.keys():
65
+ if "pos_encoder" in key:
66
+ return False
67
+ return True
68
+
69
+
70
+ def get_down_block_max(mm_state_dict: dict[str, Tensor]) -> int:
71
+ # keep track of biggest down_block count in module
72
+ biggest_block = 0
73
+ for key in mm_state_dict.keys():
74
+ if "down_blocks" in key:
75
+ try:
76
+ block_int = key.split(".")[1]
77
+ block_num = int(block_int)
78
+ if block_num > biggest_block:
79
+ biggest_block = block_num
80
+ except ValueError:
81
+ pass
82
+ return biggest_block
83
+
84
+
85
+ def has_mid_block(mm_state_dict: dict[str, Tensor]):
86
+ # check if keys contain mid_block
87
+ for key in mm_state_dict.keys():
88
+ if key.startswith("mid_block."):
89
+ return True
90
+ return False
91
+
92
+
93
+ def get_position_encoding_max_len(mm_state_dict: dict[str, Tensor], mm_name: str, mm_format: str) -> Union[int, None]:
94
+ # use pos_encoder.pe entries to determine max length - [1, {max_length}, {320|640|1280}]
95
+ for key in mm_state_dict.keys():
96
+ if key.endswith("pos_encoder.pe"):
97
+ return mm_state_dict[key].size(1) # get middle dim
98
+ # AnimateLCM models should have no pos_encoder entries, and assumed to be 64
99
+ if mm_format == AnimateDiffFormat.ANIMATELCM:
100
+ return 64
101
+ raise MotionCompatibilityError(f"No pos_encoder.pe found in mm_state_dict - {mm_name} is not a valid AnimateDiff motion module!")
102
+
103
+
104
+ _regex_hotshotxl_module_num = re.compile(r'temporal_attentions\.(\d+)\.')
105
+ def find_hotshot_module_num(key: str) -> Union[int, None]:
106
+ found = _regex_hotshotxl_module_num.search(key)
107
+ if found:
108
+ return int(found.group(1))
109
+ return None
110
+
111
+
112
+ def normalize_ad_state_dict(mm_state_dict: dict[str, Tensor], mm_name: str) -> Tuple[dict[str, Tensor], AnimateDiffInfo]:
113
+ # from pathlib import Path
114
+ # with open(Path(__file__).parent.parent.parent / f"keys_{mm_name}.txt", "w") as afile:
115
+ # for key, value in mm_state_dict.items():
116
+ # afile.write(f"{key}:\t{value.shape}\n")
117
+
118
+ # remove all non-temporal keys (in case model has extra stuff in it)
119
+ for key in list(mm_state_dict.keys()):
120
+ if "temporal" not in key:
121
+ del mm_state_dict[key]
122
+ # determine what SD model the motion module is intended for
123
+ sd_type: str = None
124
+ down_block_max = get_down_block_max(mm_state_dict)
125
+ if down_block_max == 3:
126
+ sd_type = ModelTypeSD.SD1_5
127
+ elif down_block_max == 2:
128
+ sd_type = ModelTypeSD.SDXL
129
+ else:
130
+ raise ValueError(f"'{mm_name}' is not a valid SD1.5 nor SDXL motion module - contained {down_block_max} downblocks.")
131
+ # determine the model's format
132
+ mm_format = AnimateDiffFormat.ANIMATEDIFF
133
+ if is_hotshotxl(mm_state_dict):
134
+ mm_format = AnimateDiffFormat.HOTSHOTXL
135
+ if is_animatelcm(mm_state_dict):
136
+ mm_format = AnimateDiffFormat.ANIMATELCM
137
+ # determine the model's version
138
+ mm_version = AnimateDiffVersion.V1
139
+ if has_mid_block(mm_state_dict):
140
+ mm_version = AnimateDiffVersion.V2
141
+ elif sd_type==ModelTypeSD.SD1_5 and get_position_encoding_max_len(mm_state_dict, mm_name, mm_format)==32:
142
+ mm_version = AnimateDiffVersion.V3
143
+ info = AnimateDiffInfo(sd_type=sd_type, mm_format=mm_format, mm_version=mm_version, mm_name=mm_name)
144
+ # convert to AnimateDiff format, if needed
145
+ if mm_format == AnimateDiffFormat.HOTSHOTXL:
146
+ # HotshotXL is AD-based architecture applied to SDXL instead of SD1.5
147
+ # By renaming the keys, no code needs to be adapted at all
148
+ #
149
+ # reformat temporal_attentions:
150
+ # HSXL: temporal_attentions.#.
151
+ # AD: motion_modules.#.temporal_transformer.
152
+ # HSXL: pos_encoder.positional_encoding
153
+ # AD: pos_encoder.pe
154
+ for key in list(mm_state_dict.keys()):
155
+ module_num = find_hotshot_module_num(key)
156
+ if module_num is not None:
157
+ new_key = key.replace(f"temporal_attentions.{module_num}",
158
+ f"motion_modules.{module_num}.temporal_transformer", 1)
159
+ new_key = new_key.replace("pos_encoder.positional_encoding", "pos_encoder.pe")
160
+ mm_state_dict[new_key] = mm_state_dict[key]
161
+ del mm_state_dict[key]
162
+ # return adjusted mm_state_dict and info
163
+ return mm_state_dict, info
164
+
165
+
166
+ class BlockType:
167
+ UP = "up"
168
+ DOWN = "down"
169
+ MID = "mid"
170
+
171
+
172
+ class AnimateDiffModel(nn.Module):
173
+ def __init__(self, mm_state_dict: dict[str, Tensor], mm_info: AnimateDiffInfo):
174
+ super().__init__()
175
+ self.mm_info = mm_info
176
+ self.down_blocks: Iterable[MotionModule] = nn.ModuleList([])
177
+ self.up_blocks: Iterable[MotionModule] = nn.ModuleList([])
178
+ self.mid_block: Union[MotionModule, None] = None
179
+ self.encoding_max_len = get_position_encoding_max_len(mm_state_dict, mm_info.mm_name, mm_info.mm_format)
180
+ self.has_position_encoding = self.encoding_max_len is not None
181
+ # determine ops to use (to support fp8 properly)
182
+ if comfy.model_management.unet_manual_cast(comfy.model_management.unet_dtype(), comfy.model_management.get_torch_device()) is None:
183
+ ops = comfy.ops.disable_weight_init
184
+ else:
185
+ ops = comfy.ops.manual_cast
186
+ # SDXL has 3 up/down blocks, SD1.5 has 4 up/down blocks
187
+ if mm_info.sd_type == ModelTypeSD.SDXL:
188
+ layer_channels = (320, 640, 1280)
189
+ else:
190
+ layer_channels = (320, 640, 1280, 1280)
191
+ # fill out down/up blocks and middle block, if present
192
+ for c in layer_channels:
193
+ self.down_blocks.append(MotionModule(c, temporal_position_encoding=self.has_position_encoding,
194
+ temporal_position_encoding_max_len=self.encoding_max_len, block_type=BlockType.DOWN, ops=ops))
195
+ for c in reversed(layer_channels):
196
+ self.up_blocks.append(MotionModule(c, temporal_position_encoding=self.has_position_encoding,
197
+ temporal_position_encoding_max_len=self.encoding_max_len, block_type=BlockType.UP, ops=ops))
198
+ if has_mid_block(mm_state_dict):
199
+ self.mid_block = MotionModule(1280, temporal_position_encoding=self.has_position_encoding,
200
+ temporal_position_encoding_max_len=self.encoding_max_len, block_type=BlockType.MID, ops=ops)
201
+ self.AD_video_length: int = 24
202
+
203
+ def get_device_debug(self):
204
+ return self.down_blocks[0].motion_modules[0].temporal_transformer.proj_in.weight.device
205
+
206
+ def is_length_valid_for_encoding_max_len(self, length: int):
207
+ if self.encoding_max_len is None:
208
+ return True
209
+ return length <= self.encoding_max_len
210
+
211
+ def get_best_beta_schedule(self, log=False) -> str:
212
+ to_return = None
213
+ if self.mm_info.sd_type == ModelTypeSD.SD1_5:
214
+ if self.mm_info.mm_format == AnimateDiffFormat.ANIMATELCM:
215
+ to_return = BetaSchedules.LCM # while LCM_100 is the intended schedule, I find LCM to have much less flicker
216
+ else:
217
+ to_return = BetaSchedules.SQRT_LINEAR
218
+ elif self.mm_info.sd_type == ModelTypeSD.SDXL:
219
+ if self.mm_info.mm_format == AnimateDiffFormat.HOTSHOTXL:
220
+ to_return = BetaSchedules.LINEAR
221
+ else:
222
+ to_return = BetaSchedules.LINEAR_ADXL
223
+ if to_return is not None:
224
+ if log: logger.info(f"[Autoselect]: '{to_return}' beta_schedule for {self.mm_info.get_string()}")
225
+ else:
226
+ to_return = BetaSchedules.USE_EXISTING
227
+ if log: logger.info(f"[Autoselect]: could not find beta_schedule for {self.mm_info.get_string()}, defaulting to '{to_return}'")
228
+ return to_return
229
+
230
+ def cleanup(self):
231
+ pass
232
+
233
+ def inject(self, model: ModelPatcher):
234
+ unet: openaimodel.UNetModel = model.model.diffusion_model
235
+ # inject input (down) blocks
236
+ # SD15 mm contains 4 downblocks, each with 2 TemporalTransformers - 8 in total
237
+ # SDXL mm contains 3 downblocks, each with 2 TemporalTransformers - 6 in total
238
+ self._inject(unet.input_blocks, self.down_blocks)
239
+ # inject output (up) blocks
240
+ # SD15 mm contains 4 upblocks, each with 3 TemporalTransformers - 12 in total
241
+ # SDXL mm contains 3 upblocks, each with 3 TemporalTransformers - 9 in total
242
+ self._inject(unet.output_blocks, self.up_blocks)
243
+ # inject mid block, if needed (encapsulate in list to make structure compatible)
244
+ if self.mid_block is not None:
245
+ self._inject([unet.middle_block], [self.mid_block])
246
+ del unet
247
+
248
+ def _inject(self, unet_blocks: nn.ModuleList, mm_blocks: nn.ModuleList):
249
+ # Rules for injection:
250
+ # For each component list in a unet block:
251
+ # if SpatialTransformer exists in list, place next block after last occurrence
252
+ # elif ResBlock exists in list, place next block after first occurrence
253
+ # else don't place block
254
+ injection_count = 0
255
+ unet_idx = 0
256
+ # details about blocks passed in
257
+ per_block = len(mm_blocks[0].motion_modules)
258
+ injection_goal = len(mm_blocks) * per_block
259
+ # only stop injecting when modules exhausted
260
+ while injection_count < injection_goal:
261
+ # figure out which VanillaTemporalModule from mm to inject
262
+ mm_blk_idx, mm_vtm_idx = injection_count // per_block, injection_count % per_block
263
+ # figure out layout of unet block components
264
+ st_idx = -1 # SpatialTransformer index
265
+ res_idx = -1 # first ResBlock index
266
+ # first, figure out indeces of relevant blocks
267
+ for idx, component in enumerate(unet_blocks[unet_idx]):
268
+ if type(component) == SpatialTransformer:
269
+ st_idx = idx
270
+ elif type(component).__name__ == "ResBlock" and res_idx < 0:
271
+ res_idx = idx
272
+ # if SpatialTransformer exists, inject right after
273
+ if st_idx >= 0:
274
+ #logger.info(f"AD: injecting after ST({st_idx})")
275
+ unet_blocks[unet_idx].insert(st_idx+1, mm_blocks[mm_blk_idx].motion_modules[mm_vtm_idx])
276
+ injection_count += 1
277
+ # otherwise, if only ResBlock exists, inject right after
278
+ elif res_idx >= 0:
279
+ #logger.info(f"AD: injecting after Res({res_idx})")
280
+ unet_blocks[unet_idx].insert(res_idx+1, mm_blocks[mm_blk_idx].motion_modules[mm_vtm_idx])
281
+ injection_count += 1
282
+ # increment unet_idx
283
+ unet_idx += 1
284
+
285
+ def eject(self, model: ModelPatcher):
286
+ unet: openaimodel.UNetModel = model.model.diffusion_model
287
+ # remove from input blocks (downblocks)
288
+ self._eject(unet.input_blocks)
289
+ # remove from output blocks (upblocks)
290
+ self._eject(unet.output_blocks)
291
+ # remove from middle block (encapsulate in list to make compatible)
292
+ self._eject([unet.middle_block])
293
+ del unet
294
+
295
+ def _eject(self, unet_blocks: nn.ModuleList):
296
+ # eject all VanillaTemporalModule objects from all blocks
297
+ for block in unet_blocks:
298
+ idx_to_pop = []
299
+ for idx, component in enumerate(block):
300
+ if type(component) == VanillaTemporalModule:
301
+ idx_to_pop.append(idx)
302
+ # pop in backwards order, as to not disturb what the indeces refer to
303
+ for idx in sorted(idx_to_pop, reverse=True):
304
+ block.pop(idx)
305
+
306
+ def set_video_length(self, video_length: int, full_length: int):
307
+ self.AD_video_length = video_length
308
+ for block in self.down_blocks:
309
+ block.set_video_length(video_length, full_length)
310
+ for block in self.up_blocks:
311
+ block.set_video_length(video_length, full_length)
312
+ if self.mid_block is not None:
313
+ self.mid_block.set_video_length(video_length, full_length)
314
+
315
+ def set_scale(self, multival: Union[float, Tensor]):
316
+ if multival is None:
317
+ multival = 1.0
318
+ if type(multival) == Tensor:
319
+ self._set_scale_multiplier(1.0)
320
+ self._set_scale_mask(multival)
321
+ else:
322
+ self._set_scale_multiplier(multival)
323
+ self._set_scale_mask(None)
324
+
325
+ def set_effect(self, multival: Union[float, Tensor]):
326
+ for block in self.down_blocks:
327
+ block.set_effect(multival)
328
+ for block in self.up_blocks:
329
+ block.set_effect(multival)
330
+ if self.mid_block is not None:
331
+ self.mid_block.set_effect(multival)
332
+
333
+ def set_sub_idxs(self, sub_idxs: list[int]):
334
+ for block in self.down_blocks:
335
+ block.set_sub_idxs(sub_idxs)
336
+ for block in self.up_blocks:
337
+ block.set_sub_idxs(sub_idxs)
338
+ if self.mid_block is not None:
339
+ self.mid_block.set_sub_idxs(sub_idxs)
340
+
341
+ def set_view_options(self, view_options: ContextOptions):
342
+ for block in self.down_blocks:
343
+ block.set_view_options(view_options)
344
+ for block in self.up_blocks:
345
+ block.set_view_options(view_options)
346
+ if self.mid_block is not None:
347
+ self.mid_block.set_view_options(view_options)
348
+
349
+ def reset(self):
350
+ self._reset_sub_idxs()
351
+ self._reset_scale_multiplier()
352
+ self._reset_temp_vars()
353
+
354
+ def _set_scale_multiplier(self, multiplier: Union[float, None]):
355
+ for block in self.down_blocks:
356
+ block.set_scale_multiplier(multiplier)
357
+ for block in self.up_blocks:
358
+ block.set_scale_multiplier(multiplier)
359
+ if self.mid_block is not None:
360
+ self.mid_block.set_scale_multiplier(multiplier)
361
+
362
+ def _set_scale_mask(self, mask: Tensor):
363
+ for block in self.down_blocks:
364
+ block.set_scale_mask(mask)
365
+ for block in self.up_blocks:
366
+ block.set_scale_mask(mask)
367
+ if self.mid_block is not None:
368
+ self.mid_block.set_scale_mask(mask)
369
+
370
+ def _reset_temp_vars(self):
371
+ for block in self.down_blocks:
372
+ block.reset_temp_vars()
373
+ for block in self.up_blocks:
374
+ block.reset_temp_vars()
375
+ if self.mid_block is not None:
376
+ self.mid_block.reset_temp_vars()
377
+
378
+ def _reset_scale_multiplier(self):
379
+ self._set_scale_multiplier(None)
380
+
381
+ def _reset_sub_idxs(self):
382
+ self.set_sub_idxs(None)
383
+
384
+
385
+ class MotionModule(nn.Module):
386
+ def __init__(self,
387
+ in_channels,
388
+ temporal_position_encoding=True,
389
+ temporal_position_encoding_max_len=24,
390
+ block_type: str=BlockType.DOWN,
391
+ ops=comfy.ops.disable_weight_init
392
+ ):
393
+ super().__init__()
394
+ if block_type == BlockType.MID:
395
+ # mid blocks contain only a single VanillaTemporalModule
396
+ self.motion_modules: Iterable[VanillaTemporalModule] = nn.ModuleList([get_motion_module(in_channels, temporal_position_encoding, temporal_position_encoding_max_len, ops=ops)])
397
+ else:
398
+ # down blocks contain two VanillaTemporalModules
399
+ self.motion_modules: Iterable[VanillaTemporalModule] = nn.ModuleList(
400
+ [
401
+ get_motion_module(in_channels, temporal_position_encoding, temporal_position_encoding_max_len, ops=ops),
402
+ get_motion_module(in_channels, temporal_position_encoding, temporal_position_encoding_max_len, ops=ops)
403
+ ]
404
+ )
405
+ # up blocks contain one additional VanillaTemporalModule
406
+ if block_type == BlockType.UP:
407
+ self.motion_modules.append(get_motion_module(in_channels, temporal_position_encoding, temporal_position_encoding_max_len, ops=ops))
408
+
409
+ def set_video_length(self, video_length: int, full_length: int):
410
+ for motion_module in self.motion_modules:
411
+ motion_module.set_video_length(video_length, full_length)
412
+
413
+ def set_scale_multiplier(self, multiplier: Union[float, None]):
414
+ for motion_module in self.motion_modules:
415
+ motion_module.set_scale_multiplier(multiplier)
416
+
417
+ def set_scale_mask(self, mask: Tensor):
418
+ for motion_module in self.motion_modules:
419
+ motion_module.set_scale_mask(mask)
420
+
421
+ def set_effect(self, multival: Union[float, Tensor]):
422
+ for motion_module in self.motion_modules:
423
+ motion_module.set_effect(multival)
424
+
425
+ def set_sub_idxs(self, sub_idxs: list[int]):
426
+ for motion_module in self.motion_modules:
427
+ motion_module.set_sub_idxs(sub_idxs)
428
+
429
+ def set_view_options(self, view_options: ContextOptions):
430
+ for motion_module in self.motion_modules:
431
+ motion_module.set_view_options(view_options=view_options)
432
+
433
+ def reset_temp_vars(self):
434
+ for motion_module in self.motion_modules:
435
+ motion_module.reset_temp_vars()
436
+
437
+
438
+ def get_motion_module(in_channels, temporal_position_encoding, temporal_position_encoding_max_len, ops=comfy.ops.disable_weight_init):
439
+ return VanillaTemporalModule(in_channels=in_channels, temporal_position_encoding=temporal_position_encoding, temporal_position_encoding_max_len=temporal_position_encoding_max_len, ops=ops)
440
+
441
+
442
+ class VanillaTemporalModule(nn.Module):
443
+ def __init__(
444
+ self,
445
+ in_channels,
446
+ num_attention_heads=8,
447
+ num_transformer_block=1,
448
+ attention_block_types=("Temporal_Self", "Temporal_Self"),
449
+ cross_frame_attention_mode=None,
450
+ temporal_position_encoding=True,
451
+ temporal_position_encoding_max_len=24,
452
+ temporal_attention_dim_div=1,
453
+ zero_initialize=True,
454
+ ops=comfy.ops.disable_weight_init,
455
+ ):
456
+ super().__init__()
457
+
458
+ self.video_length = 16
459
+ self.full_length = 16
460
+ self.sub_idxs = None
461
+ self.view_options = None
462
+
463
+ self.effect = None
464
+ self.temp_effect_mask: Tensor = None
465
+ self.prev_input_tensor_batch = 0
466
+
467
+ self.temporal_transformer = TemporalTransformer3DModel(
468
+ in_channels=in_channels,
469
+ num_attention_heads=num_attention_heads,
470
+ attention_head_dim=in_channels
471
+ // num_attention_heads
472
+ // temporal_attention_dim_div,
473
+ num_layers=num_transformer_block,
474
+ attention_block_types=attention_block_types,
475
+ cross_frame_attention_mode=cross_frame_attention_mode,
476
+ temporal_position_encoding=temporal_position_encoding,
477
+ temporal_position_encoding_max_len=temporal_position_encoding_max_len,
478
+ ops=ops
479
+ )
480
+
481
+ if zero_initialize:
482
+ self.temporal_transformer.proj_out = zero_module(
483
+ self.temporal_transformer.proj_out
484
+ )
485
+
486
+ def set_video_length(self, video_length: int, full_length: int):
487
+ self.video_length = video_length
488
+ self.full_length = full_length
489
+ self.temporal_transformer.set_video_length(video_length, full_length)
490
+
491
+ def set_scale_multiplier(self, multiplier: Union[float, None]):
492
+ self.temporal_transformer.set_scale_multiplier(multiplier)
493
+
494
+ def set_scale_mask(self, mask: Tensor):
495
+ self.temporal_transformer.set_scale_mask(mask)
496
+
497
+ def set_effect(self, multival: Union[float, Tensor]):
498
+ if type(multival) == Tensor:
499
+ self.effect = multival
500
+ elif multival is not None and math.isclose(multival, 1.0):
501
+ self.effect = None
502
+ else:
503
+ self.effect = multival
504
+ self.temp_effect_mask = None
505
+
506
+ def set_sub_idxs(self, sub_idxs: list[int]):
507
+ self.sub_idxs = sub_idxs
508
+ self.temporal_transformer.set_sub_idxs(sub_idxs)
509
+
510
+ def set_view_options(self, view_options: ContextOptions):
511
+ self.view_options = view_options
512
+
513
+ def reset_temp_vars(self):
514
+ self.set_effect(None)
515
+ self.set_view_options(None)
516
+ self.temporal_transformer.reset_temp_vars()
517
+
518
+ def get_effect_mask(self, input_tensor: Tensor):
519
+ batch, channel, height, width = input_tensor.shape
520
+ batched_number = batch // self.video_length
521
+ full_batched_idxs = list(range(self.video_length))*batched_number
522
+ # if there is a cached temp_effect_mask and it is valid for current input, return it
523
+ if batch == self.prev_input_tensor_batch and self.temp_effect_mask is not None:
524
+ if self.sub_idxs is not None:
525
+ return self.temp_effect_mask[self.sub_idxs*batched_number]
526
+ return self.temp_effect_mask[full_batched_idxs]
527
+ # clear any existing mask
528
+ del self.temp_effect_mask
529
+ self.temp_effect_mask = None
530
+ # recalculate temp mask
531
+ self.prev_input_tensor_batch = batch
532
+ # make sure mask matches expected dimensions
533
+ mask = prepare_mask_batch(self.effect, shape=(self.full_length, 1, height, width))
534
+ # make sure mask is as long as full_length - clone last element of list if too short
535
+ self.temp_effect_mask = extend_to_batch_size(mask, self.full_length).to(
536
+ dtype=input_tensor.dtype, device=input_tensor.device)
537
+ # return finalized mask
538
+ if self.sub_idxs is not None:
539
+ return self.temp_effect_mask[self.sub_idxs*batched_number]
540
+ return self.temp_effect_mask[full_batched_idxs]
541
+
542
+ def forward(self, input_tensor: Tensor, encoder_hidden_states=None, attention_mask=None):
543
+ if self.effect is None:
544
+ return self.temporal_transformer(input_tensor, encoder_hidden_states, attention_mask, self.view_options)
545
+ # return weighted average of input_tensor and AD output
546
+ if type(self.effect) != Tensor:
547
+ effect = self.effect
548
+ # do nothing if effect is 0
549
+ if math.isclose(effect, 0.0):
550
+ return input_tensor
551
+ else:
552
+ effect = self.get_effect_mask(input_tensor)
553
+ return input_tensor*(1.0-effect) + self.temporal_transformer(input_tensor, encoder_hidden_states, attention_mask, self.view_options)*effect
554
+
555
+
556
+ class TemporalTransformer3DModel(nn.Module):
557
+ def __init__(
558
+ self,
559
+ in_channels,
560
+ num_attention_heads,
561
+ attention_head_dim,
562
+ num_layers,
563
+ attention_block_types=(
564
+ "Temporal_Self",
565
+ "Temporal_Self",
566
+ ),
567
+ dropout=0.0,
568
+ norm_num_groups=32,
569
+ cross_attention_dim=768,
570
+ activation_fn="geglu",
571
+ attention_bias=False,
572
+ upcast_attention=False,
573
+ cross_frame_attention_mode=None,
574
+ temporal_position_encoding=False,
575
+ temporal_position_encoding_max_len=24,
576
+ ops=comfy.ops.disable_weight_init,
577
+ ):
578
+ super().__init__()
579
+ self.video_length = 16
580
+ self.full_length = 16
581
+ self.raw_scale_mask: Union[Tensor, None] = None
582
+ self.temp_scale_mask: Union[Tensor, None] = None
583
+ self.sub_idxs: Union[list[int], None] = None
584
+ self.prev_hidden_states_batch = 0
585
+
586
+
587
+ inner_dim = num_attention_heads * attention_head_dim
588
+
589
+ self.norm = ops.GroupNorm(
590
+ num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True
591
+ )
592
+ self.proj_in = ops.Linear(in_channels, inner_dim)
593
+
594
+ self.transformer_blocks: Iterable[TemporalTransformerBlock] = nn.ModuleList(
595
+ [
596
+ TemporalTransformerBlock(
597
+ dim=inner_dim,
598
+ num_attention_heads=num_attention_heads,
599
+ attention_head_dim=attention_head_dim,
600
+ attention_block_types=attention_block_types,
601
+ dropout=dropout,
602
+ norm_num_groups=norm_num_groups,
603
+ cross_attention_dim=cross_attention_dim,
604
+ activation_fn=activation_fn,
605
+ attention_bias=attention_bias,
606
+ upcast_attention=upcast_attention,
607
+ cross_frame_attention_mode=cross_frame_attention_mode,
608
+ temporal_position_encoding=temporal_position_encoding,
609
+ temporal_position_encoding_max_len=temporal_position_encoding_max_len,
610
+ ops=ops,
611
+ )
612
+ for d in range(num_layers)
613
+ ]
614
+ )
615
+ self.proj_out = ops.Linear(inner_dim, in_channels)
616
+
617
+ def set_video_length(self, video_length: int, full_length: int):
618
+ self.video_length = video_length
619
+ self.full_length = full_length
620
+
621
+ def set_scale_multiplier(self, multiplier: Union[float, None]):
622
+ for block in self.transformer_blocks:
623
+ block.set_scale_multiplier(multiplier)
624
+
625
+ def set_scale_mask(self, mask: Tensor):
626
+ self.raw_scale_mask = mask
627
+ self.temp_scale_mask = None
628
+
629
+ def set_sub_idxs(self, sub_idxs: list[int]):
630
+ self.sub_idxs = sub_idxs
631
+ for block in self.transformer_blocks:
632
+ block.set_sub_idxs(sub_idxs)
633
+
634
+ def reset_temp_vars(self):
635
+ del self.temp_scale_mask
636
+ self.temp_scale_mask = None
637
+ self.prev_hidden_states_batch = 0
638
+
639
+ def get_scale_mask(self, hidden_states: Tensor) -> Union[Tensor, None]:
640
+ # if no raw mask, return None
641
+ if self.raw_scale_mask is None:
642
+ return None
643
+ shape = hidden_states.shape
644
+ batch, channel, height, width = shape
645
+ # if temp mask already calculated, return it
646
+ if self.temp_scale_mask != None:
647
+ # check if hidden_states batch matches
648
+ if batch == self.prev_hidden_states_batch:
649
+ if self.sub_idxs is not None:
650
+ return self.temp_scale_mask[:, self.sub_idxs, :]
651
+ return self.temp_scale_mask
652
+ # if does not match, reset cached temp_scale_mask and recalculate it
653
+ del self.temp_scale_mask
654
+ self.temp_scale_mask = None
655
+ # otherwise, calculate temp mask
656
+ self.prev_hidden_states_batch = batch
657
+ mask = prepare_mask_batch(self.raw_scale_mask, shape=(self.full_length, 1, height, width))
658
+ mask = repeat_to_batch_size(mask, self.full_length)
659
+ # if mask not the same amount length as full length, make it match
660
+ if self.full_length != mask.shape[0]:
661
+ mask = broadcast_image_to(mask, self.full_length, 1)
662
+ # reshape mask to attention K shape (h*w, latent_count, 1)
663
+ batch, channel, height, width = mask.shape
664
+ # first, perform same operations as on hidden_states,
665
+ # turning (b, c, h, w) -> (b, h*w, c)
666
+ mask = mask.permute(0, 2, 3, 1).reshape(batch, height*width, channel)
667
+ # then, make it the same shape as attention's k, (h*w, b, c)
668
+ mask = mask.permute(1, 0, 2)
669
+ # make masks match the expected length of h*w
670
+ batched_number = shape[0] // self.video_length
671
+ if batched_number > 1:
672
+ mask = torch.cat([mask] * batched_number, dim=0)
673
+ # cache mask and set to proper device
674
+ self.temp_scale_mask = mask
675
+ # move temp_scale_mask to proper dtype + device
676
+ self.temp_scale_mask = self.temp_scale_mask.to(dtype=hidden_states.dtype, device=hidden_states.device)
677
+ # return subset of masks, if needed
678
+ if self.sub_idxs is not None:
679
+ return self.temp_scale_mask[:, self.sub_idxs, :]
680
+ return self.temp_scale_mask
681
+
682
+ def forward(self, hidden_states, encoder_hidden_states=None, attention_mask=None, view_options: ContextOptions=None):
683
+ batch, channel, height, width = hidden_states.shape
684
+ residual = hidden_states
685
+ scale_mask = self.get_scale_mask(hidden_states)
686
+ # add some casts for fp8 purposes - does not affect speed otherwise
687
+ hidden_states = self.norm(hidden_states).to(hidden_states.dtype)
688
+ inner_dim = hidden_states.shape[1]
689
+ hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(
690
+ batch, height * width, inner_dim
691
+ )
692
+ hidden_states = self.proj_in(hidden_states).to(hidden_states.dtype)
693
+
694
+ # Transformer Blocks
695
+ for block in self.transformer_blocks:
696
+ hidden_states = block(
697
+ hidden_states,
698
+ encoder_hidden_states=encoder_hidden_states,
699
+ attention_mask=attention_mask,
700
+ video_length=self.video_length,
701
+ scale_mask=scale_mask,
702
+ view_options=view_options
703
+ )
704
+
705
+ # output
706
+ hidden_states = self.proj_out(hidden_states)
707
+ hidden_states = (
708
+ hidden_states.reshape(batch, height, width, inner_dim)
709
+ .permute(0, 3, 1, 2)
710
+ .contiguous()
711
+ )
712
+
713
+ output = hidden_states + residual
714
+
715
+ return output
716
+
717
+
718
+ class TemporalTransformerBlock(nn.Module):
719
+ def __init__(
720
+ self,
721
+ dim,
722
+ num_attention_heads,
723
+ attention_head_dim,
724
+ attention_block_types=(
725
+ "Temporal_Self",
726
+ "Temporal_Self",
727
+ ),
728
+ dropout=0.0,
729
+ norm_num_groups=32,
730
+ cross_attention_dim=768,
731
+ activation_fn="geglu",
732
+ attention_bias=False,
733
+ upcast_attention=False,
734
+ cross_frame_attention_mode=None,
735
+ temporal_position_encoding=False,
736
+ temporal_position_encoding_max_len=24,
737
+ ops=comfy.ops.disable_weight_init,
738
+ ):
739
+ super().__init__()
740
+
741
+ attention_blocks = []
742
+ norms = []
743
+
744
+ for block_name in attention_block_types:
745
+ attention_blocks.append(
746
+ VersatileAttention(
747
+ attention_mode=block_name.split("_")[0],
748
+ context_dim=cross_attention_dim # called context_dim for ComfyUI impl
749
+ if block_name.endswith("_Cross")
750
+ else None,
751
+ query_dim=dim,
752
+ heads=num_attention_heads,
753
+ dim_head=attention_head_dim,
754
+ dropout=dropout,
755
+ #bias=attention_bias, # remove for Comfy CrossAttention
756
+ #upcast_attention=upcast_attention, # remove for Comfy CrossAttention
757
+ cross_frame_attention_mode=cross_frame_attention_mode,
758
+ temporal_position_encoding=temporal_position_encoding,
759
+ temporal_position_encoding_max_len=temporal_position_encoding_max_len,
760
+ ops=ops,
761
+ )
762
+ )
763
+ norms.append(ops.LayerNorm(dim))
764
+
765
+ self.attention_blocks: Iterable[VersatileAttention] = nn.ModuleList(attention_blocks)
766
+ self.norms = nn.ModuleList(norms)
767
+
768
+ self.ff = FeedForward(dim, dropout=dropout, glu=(activation_fn == "geglu"), operations=ops)
769
+ self.ff_norm = ops.LayerNorm(dim)
770
+
771
+ def set_scale_multiplier(self, multiplier: Union[float, None]):
772
+ for block in self.attention_blocks:
773
+ block.set_scale_multiplier(multiplier)
774
+
775
+ def set_sub_idxs(self, sub_idxs: list[int]):
776
+ for block in self.attention_blocks:
777
+ block.set_sub_idxs(sub_idxs)
778
+
779
+ def forward(
780
+ self,
781
+ hidden_states: Tensor,
782
+ encoder_hidden_states: Tensor=None,
783
+ attention_mask: Tensor=None,
784
+ video_length: int=None,
785
+ scale_mask: Tensor=None,
786
+ view_options: ContextOptions=None,
787
+ ):
788
+ # make view_options None if context_length > video_length, or if equal and equal not allowed
789
+ if view_options:
790
+ if view_options.context_length > video_length:
791
+ view_options = None
792
+ elif view_options.context_length == video_length and not view_options.use_on_equal_length:
793
+ view_options = None
794
+ if not view_options:
795
+ for attention_block, norm in zip(self.attention_blocks, self.norms):
796
+ norm_hidden_states = norm(hidden_states).to(hidden_states.dtype)
797
+ hidden_states = (
798
+ attention_block(
799
+ norm_hidden_states,
800
+ encoder_hidden_states=encoder_hidden_states
801
+ if attention_block.is_cross_attention
802
+ else None,
803
+ attention_mask=attention_mask,
804
+ video_length=video_length,
805
+ scale_mask=scale_mask
806
+ ) + hidden_states
807
+ )
808
+ else:
809
+ # views idea gotten from diffusers AnimateDiff FreeNoise implementation:
810
+ # https://github.com/arthur-qiu/FreeNoise-AnimateDiff/blob/main/animatediff/models/motion_module.py
811
+ # apply sliding context windows (views)
812
+ views = get_context_windows(num_frames=video_length, opts=view_options)
813
+ hidden_states = rearrange(hidden_states, "(b f) d c -> b f d c", f=video_length)
814
+ value_final = torch.zeros_like(hidden_states)
815
+ count_final = torch.zeros_like(hidden_states)
816
+ # bias_final = [0.0] * video_length
817
+ batched_conds = hidden_states.size(1) // video_length
818
+ for sub_idxs in views:
819
+ sub_hidden_states = rearrange(hidden_states[:, sub_idxs], "b f d c -> (b f) d c")
820
+ for attention_block, norm in zip(self.attention_blocks, self.norms):
821
+ norm_hidden_states = norm(sub_hidden_states).to(sub_hidden_states.dtype)
822
+ sub_hidden_states = (
823
+ attention_block(
824
+ norm_hidden_states,
825
+ encoder_hidden_states=encoder_hidden_states # do these need to be changed for sub_idxs too?
826
+ if attention_block.is_cross_attention
827
+ else None,
828
+ attention_mask=attention_mask,
829
+ video_length=len(sub_idxs),
830
+ scale_mask=scale_mask[:, sub_idxs, :] if scale_mask is not None else scale_mask
831
+ ) + sub_hidden_states
832
+ )
833
+ sub_hidden_states = rearrange(sub_hidden_states, "(b f) d c -> b f d c", f=len(sub_idxs))
834
+
835
+ # if view_options.fuse_method == ContextFuseMethod.RELATIVE:
836
+ # for pos, idx in enumerate(sub_idxs):
837
+ # # bias is the influence of a specific index in relation to the whole context window
838
+ # bias = 1 - abs(idx - (sub_idxs[0] + sub_idxs[-1]) / 2) / ((sub_idxs[-1] - sub_idxs[0] + 1e-2) / 2)
839
+ # bias = max(1e-2, bias)
840
+ # # take weighted averate relative to total bias of current idx
841
+ # bias_total = bias_final[idx]
842
+ # prev_weight = torch.tensor([bias_total / (bias_total + bias)],
843
+ # dtype=value_final.dtype, device=value_final.device).unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
844
+ # #prev_weight = torch.cat([prev_weight]*value_final.shape[1], dim=1)
845
+ # new_weight = torch.tensor([bias / (bias_total + bias)],
846
+ # dtype=value_final.dtype, device=value_final.device).unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
847
+ # #new_weight = torch.cat([new_weight]*value_final.shape[1], dim=1)
848
+ # test = value_final[:, idx:idx+1, :, :]
849
+ # value_final[:, idx:idx+1, :, :] = value_final[:, idx:idx+1, :, :] * prev_weight + sub_hidden_states[:, pos:pos+1, : ,:] * new_weight
850
+ # bias_final[idx] = bias_total + bias
851
+ # else:
852
+ weights = get_context_weights(len(sub_idxs), view_options.fuse_method) * batched_conds
853
+ weights_tensor = torch.Tensor(weights).to(device=hidden_states.device).unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
854
+ value_final[:, sub_idxs] += sub_hidden_states * weights_tensor
855
+ count_final[:, sub_idxs] += weights_tensor
856
+
857
+ # get weighted average of sub_hidden_states, if fuse method requires it
858
+ # if view_options.fuse_method != ContextFuseMethod.RELATIVE:
859
+ hidden_states = value_final / count_final
860
+ hidden_states = rearrange(hidden_states, "b f d c -> (b f) d c")
861
+ del value_final
862
+ del count_final
863
+ # del bias_final
864
+
865
+ hidden_states = self.ff(self.ff_norm(hidden_states)) + hidden_states
866
+
867
+ output = hidden_states
868
+ return output
869
+
870
+
871
+ class PositionalEncoding(nn.Module):
872
+ def __init__(self, d_model, dropout=0.0, max_len=24):
873
+ super().__init__()
874
+ self.dropout = nn.Dropout(p=dropout)
875
+ position = torch.arange(max_len).unsqueeze(1)
876
+ div_term = torch.exp(
877
+ torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model)
878
+ )
879
+ pe = torch.zeros(1, max_len, d_model)
880
+ pe[0, :, 0::2] = torch.sin(position * div_term)
881
+ pe[0, :, 1::2] = torch.cos(position * div_term)
882
+ self.register_buffer("pe", pe)
883
+ self.sub_idxs = None
884
+
885
+ def set_sub_idxs(self, sub_idxs: list[int]):
886
+ self.sub_idxs = sub_idxs
887
+
888
+ def forward(self, x):
889
+ #if self.sub_idxs is not None:
890
+ # x = x + self.pe[:, self.sub_idxs]
891
+ #else:
892
+ x = x + self.pe[:, : x.size(1)]
893
+ return self.dropout(x)
894
+
895
+
896
+ class VersatileAttention(CrossAttentionMM):
897
+ def __init__(
898
+ self,
899
+ attention_mode=None,
900
+ cross_frame_attention_mode=None,
901
+ temporal_position_encoding=False,
902
+ temporal_position_encoding_max_len=24,
903
+ ops=comfy.ops.disable_weight_init,
904
+ *args,
905
+ **kwargs,
906
+ ):
907
+ super().__init__(operations=ops, *args, **kwargs)
908
+ assert attention_mode == "Temporal"
909
+
910
+ self.attention_mode = attention_mode
911
+ self.is_cross_attention = kwargs["context_dim"] is not None
912
+
913
+ self.pos_encoder = (
914
+ PositionalEncoding(
915
+ kwargs["query_dim"],
916
+ dropout=0.0,
917
+ max_len=temporal_position_encoding_max_len,
918
+ )
919
+ if (temporal_position_encoding and attention_mode == "Temporal")
920
+ else None
921
+ )
922
+
923
+ def extra_repr(self):
924
+ return f"(Module Info) Attention_Mode: {self.attention_mode}, Is_Cross_Attention: {self.is_cross_attention}"
925
+
926
+ def set_scale_multiplier(self, multiplier: Union[float, None]):
927
+ if multiplier is None or math.isclose(multiplier, 1.0):
928
+ self.scale = 1.0
929
+ else:
930
+ self.scale = multiplier
931
+
932
+ def set_sub_idxs(self, sub_idxs: list[int]):
933
+ if self.pos_encoder != None:
934
+ self.pos_encoder.set_sub_idxs(sub_idxs)
935
+
936
+ def forward(
937
+ self,
938
+ hidden_states: Tensor,
939
+ encoder_hidden_states=None,
940
+ attention_mask=None,
941
+ video_length=None,
942
+ scale_mask=None,
943
+ ):
944
+ if self.attention_mode != "Temporal":
945
+ raise NotImplementedError
946
+
947
+ d = hidden_states.shape[1]
948
+ hidden_states = rearrange(
949
+ hidden_states, "(b f) d c -> (b d) f c", f=video_length
950
+ )
951
+
952
+ if self.pos_encoder is not None:
953
+ hidden_states = self.pos_encoder(hidden_states).to(hidden_states.dtype)
954
+
955
+ encoder_hidden_states = (
956
+ repeat(encoder_hidden_states, "b n c -> (b d) n c", d=d)
957
+ if encoder_hidden_states is not None
958
+ else encoder_hidden_states
959
+ )
960
+
961
+ hidden_states = super().forward(
962
+ hidden_states,
963
+ encoder_hidden_states,
964
+ value=None,
965
+ mask=attention_mask,
966
+ scale_mask=scale_mask,
967
+ )
968
+
969
+ hidden_states = rearrange(hidden_states, "(b d) f c -> (b f) d c", d=d)
970
+
971
+ return hidden_states
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import comfy.sample as comfy_sample
2
+
3
+ from .sampling import motion_sample_factory
4
+
5
+ from .nodes_gen1 import (AnimateDiffLoaderGen1, LegacyAnimateDiffLoaderWithContext, AnimateDiffModelSettings,
6
+ AnimateDiffModelSettingsSimple, AnimateDiffModelSettingsAdvanced, AnimateDiffModelSettingsAdvancedAttnStrengths)
7
+ from .nodes_gen2 import UseEvolvedSamplingNode, ApplyAnimateDiffModelNode, ApplyAnimateDiffModelBasicNode, LoadAnimateDiffModelNode, ADKeyframeNode
8
+ from .nodes_multival import MultivalDynamicNode, MultivalScaledMaskNode
9
+ from .nodes_sample import (FreeInitOptionsNode, NoiseLayerAddWeightedNode, SampleSettingsNode, NoiseLayerAddNode, NoiseLayerReplaceNode, IterationOptionsNode,
10
+ CustomCFGNode, CustomCFGKeyframeNode)
11
+ from .nodes_sigma_schedule import (SigmaScheduleNode, RawSigmaScheduleNode, WeightedAverageSigmaScheduleNode, InterpolatedWeightedAverageSigmaScheduleNode, SplitAndCombineSigmaScheduleNode)
12
+ from .nodes_context import (LegacyLoopedUniformContextOptionsNode, LoopedUniformContextOptionsNode, LoopedUniformViewOptionsNode, StandardUniformContextOptionsNode, StandardStaticContextOptionsNode, BatchedContextOptionsNode,
13
+ StandardStaticViewOptionsNode, StandardUniformViewOptionsNode, ViewAsContextOptionsNode)
14
+ from .nodes_ad_settings import AnimateDiffSettingsNode, ManualAdjustPENode, SweetspotStretchPENode, FullStretchPENode
15
+ from .nodes_extras import AnimateDiffUnload, EmptyLatentImageLarge, CheckpointLoaderSimpleWithNoiseSelect
16
+ from .nodes_deprecated import AnimateDiffLoader_Deprecated, AnimateDiffLoaderAdvanced_Deprecated, AnimateDiffCombine_Deprecated
17
+ from .nodes_lora import AnimateDiffLoraLoader, MaskedLoraLoader
18
+
19
+ from .logger import logger
20
+
21
+ # override comfy_sample.sample with animatediff-support version
22
+ comfy_sample.sample = motion_sample_factory(comfy_sample.sample)
23
+ comfy_sample.sample_custom = motion_sample_factory(comfy_sample.sample_custom, is_custom=True)
24
+
25
+
26
+ NODE_CLASS_MAPPINGS = {
27
+ # Unencapsulated
28
+ "ADE_AnimateDiffLoRALoader": AnimateDiffLoraLoader,
29
+ "ADE_AnimateDiffSamplingSettings": SampleSettingsNode,
30
+ "ADE_AnimateDiffKeyframe": ADKeyframeNode,
31
+ # Multival Nodes
32
+ "ADE_MultivalDynamic": MultivalDynamicNode,
33
+ "ADE_MultivalScaledMask": MultivalScaledMaskNode,
34
+ # Context Opts
35
+ "ADE_StandardStaticContextOptions": StandardStaticContextOptionsNode,
36
+ "ADE_StandardUniformContextOptions": StandardUniformContextOptionsNode,
37
+ "ADE_LoopedUniformContextOptions": LoopedUniformContextOptionsNode,
38
+ "ADE_ViewsOnlyContextOptions": ViewAsContextOptionsNode,
39
+ "ADE_BatchedContextOptions": BatchedContextOptionsNode,
40
+ "ADE_AnimateDiffUniformContextOptions": LegacyLoopedUniformContextOptionsNode, # Legacy
41
+ # View Opts
42
+ "ADE_StandardStaticViewOptions": StandardStaticViewOptionsNode,
43
+ "ADE_StandardUniformViewOptions": StandardUniformViewOptionsNode,
44
+ "ADE_LoopedUniformViewOptions": LoopedUniformViewOptionsNode,
45
+ # Iteration Opts
46
+ "ADE_IterationOptsDefault": IterationOptionsNode,
47
+ "ADE_IterationOptsFreeInit": FreeInitOptionsNode,
48
+ # Noise Layer Nodes
49
+ "ADE_NoiseLayerAdd": NoiseLayerAddNode,
50
+ "ADE_NoiseLayerAddWeighted": NoiseLayerAddWeightedNode,
51
+ "ADE_NoiseLayerReplace": NoiseLayerReplaceNode,
52
+ # AnimateDiff Settings
53
+ "ADE_AnimateDiffSettings": AnimateDiffSettingsNode,
54
+ "ADE_AdjustPESweetspotStretch": SweetspotStretchPENode,
55
+ "ADE_AdjustPEFullStretch": FullStretchPENode,
56
+ "ADE_AdjustPEManual": ManualAdjustPENode,
57
+ # Sample Settings
58
+ "ADE_CustomCFG": CustomCFGNode,
59
+ "ADE_CustomCFGKeyframe": CustomCFGKeyframeNode,
60
+ "ADE_SigmaSchedule": SigmaScheduleNode,
61
+ "ADE_RawSigmaSchedule": RawSigmaScheduleNode,
62
+ "ADE_SigmaScheduleWeightedAverage": WeightedAverageSigmaScheduleNode,
63
+ "ADE_SigmaScheduleWeightedAverageInterp": InterpolatedWeightedAverageSigmaScheduleNode,
64
+ "ADE_SigmaScheduleSplitAndCombine": SplitAndCombineSigmaScheduleNode,
65
+ # Extras Nodes
66
+ "ADE_AnimateDiffUnload": AnimateDiffUnload,
67
+ "ADE_EmptyLatentImageLarge": EmptyLatentImageLarge,
68
+ "CheckpointLoaderSimpleWithNoiseSelect": CheckpointLoaderSimpleWithNoiseSelect,
69
+ # Gen1 Nodes
70
+ "ADE_AnimateDiffLoaderGen1": AnimateDiffLoaderGen1,
71
+ "ADE_AnimateDiffLoaderWithContext": LegacyAnimateDiffLoaderWithContext,
72
+ "ADE_AnimateDiffModelSettings_Release": AnimateDiffModelSettings,
73
+ "ADE_AnimateDiffModelSettingsSimple": AnimateDiffModelSettingsSimple,
74
+ "ADE_AnimateDiffModelSettings": AnimateDiffModelSettingsAdvanced,
75
+ "ADE_AnimateDiffModelSettingsAdvancedAttnStrengths": AnimateDiffModelSettingsAdvancedAttnStrengths,
76
+ # Gen2 Nodes
77
+ "ADE_UseEvolvedSampling": UseEvolvedSamplingNode,
78
+ "ADE_ApplyAnimateDiffModelSimple": ApplyAnimateDiffModelBasicNode,
79
+ "ADE_ApplyAnimateDiffModel": ApplyAnimateDiffModelNode,
80
+ "ADE_LoadAnimateDiffModel": LoadAnimateDiffModelNode,
81
+ # MaskedLoraLoader
82
+ #"ADE_MaskedLoadLora": MaskedLoraLoader,
83
+ # Deprecated Nodes
84
+ "AnimateDiffLoaderV1": AnimateDiffLoader_Deprecated,
85
+ "ADE_AnimateDiffLoaderV1Advanced": AnimateDiffLoaderAdvanced_Deprecated,
86
+ "ADE_AnimateDiffCombine": AnimateDiffCombine_Deprecated,
87
+ }
88
+ NODE_DISPLAY_NAME_MAPPINGS = {
89
+ # Unencapsulated
90
+ "ADE_AnimateDiffLoRALoader": "Load AnimateDiff LoRA 🎭🅐🅓",
91
+ "ADE_AnimateDiffSamplingSettings": "Sample Settings 🎭🅐🅓",
92
+ "ADE_AnimateDiffKeyframe": "AnimateDiff Keyframe 🎭🅐🅓",
93
+ # Multival Nodes
94
+ "ADE_MultivalDynamic": "Multival Dynamic 🎭🅐🅓",
95
+ "ADE_MultivalScaledMask": "Multival Scaled Mask 🎭🅐🅓",
96
+ # Context Opts
97
+ "ADE_StandardStaticContextOptions": "Context Options◆Standard Static 🎭🅐🅓",
98
+ "ADE_StandardUniformContextOptions": "Context Options◆Standard Uniform 🎭🅐🅓",
99
+ "ADE_LoopedUniformContextOptions": "Context Options◆Looped Uniform 🎭🅐🅓",
100
+ "ADE_ViewsOnlyContextOptions": "Context Options◆Views Only [VRAM⇈] 🎭🅐🅓",
101
+ "ADE_BatchedContextOptions": "Context Options◆Batched [Non-AD] 🎭🅐🅓",
102
+ "ADE_AnimateDiffUniformContextOptions": "Context Options◆Looped Uniform 🎭🅐🅓", # Legacy
103
+ # View Opts
104
+ "ADE_StandardStaticViewOptions": "View Options◆Standard Static 🎭🅐🅓",
105
+ "ADE_StandardUniformViewOptions": "View Options◆Standard Uniform 🎭🅐🅓",
106
+ "ADE_LoopedUniformViewOptions": "View Options◆Looped Uniform 🎭🅐🅓",
107
+ # Iteration Opts
108
+ "ADE_IterationOptsDefault": "Default Iteration Options 🎭🅐🅓",
109
+ "ADE_IterationOptsFreeInit": "FreeInit Iteration Options 🎭🅐🅓",
110
+ # Noise Layer Nodes
111
+ "ADE_NoiseLayerAdd": "Noise Layer [Add] 🎭🅐🅓",
112
+ "ADE_NoiseLayerAddWeighted": "Noise Layer [Add Weighted] 🎭🅐🅓",
113
+ "ADE_NoiseLayerReplace": "Noise Layer [Replace] 🎭🅐🅓",
114
+ # AnimateDiff Settings
115
+ "ADE_AnimateDiffSettings": "AnimateDiff Settings 🎭🅐🅓",
116
+ "ADE_AdjustPESweetspotStretch": "Adjust PE [Sweetspot Stretch] 🎭🅐🅓",
117
+ "ADE_AdjustPEFullStretch": "Adjust PE [Full Stretch] 🎭🅐🅓",
118
+ "ADE_AdjustPEManual": "Adjust PE [Manual] 🎭🅐🅓",
119
+ # Sample Settings
120
+ "ADE_CustomCFG": "Custom CFG 🎭🅐🅓",
121
+ "ADE_CustomCFGKeyframe": "Custom CFG Keyframe 🎭🅐🅓",
122
+ "ADE_SigmaSchedule": "Create Sigma Schedule 🎭🅐🅓",
123
+ "ADE_RawSigmaSchedule": "Create Raw Sigma Schedule 🎭🅐🅓",
124
+ "ADE_SigmaScheduleWeightedAverage": "Sigma Schedule Weighted Mean 🎭🅐🅓",
125
+ "ADE_SigmaScheduleWeightedAverageInterp": "Sigma Schedule Interpolated Mean 🎭🅐🅓",
126
+ "ADE_SigmaScheduleSplitAndCombine": "Sigma Schedule Split Combine 🎭🅐🅓",
127
+ # Extras Nodes
128
+ "ADE_AnimateDiffUnload": "AnimateDiff Unload 🎭🅐🅓",
129
+ "ADE_EmptyLatentImageLarge": "Empty Latent Image (Big Batch) 🎭🅐🅓",
130
+ "CheckpointLoaderSimpleWithNoiseSelect": "Load Checkpoint w/ Noise Select 🎭🅐🅓",
131
+ # Gen1 Nodes
132
+ "ADE_AnimateDiffLoaderGen1": "AnimateDiff Loader 🎭🅐🅓①",
133
+ "ADE_AnimateDiffLoaderWithContext": "AnimateDiff Loader [Legacy] 🎭🅐🅓①",
134
+ "ADE_AnimateDiffModelSettings_Release": "[DEPR] Motion Model Settings 🎭🅐🅓①",
135
+ "ADE_AnimateDiffModelSettingsSimple": "[DEPR] Motion Model Settings (Simple) 🎭🅐🅓①",
136
+ "ADE_AnimateDiffModelSettings": "[DEPR] Motion Model Settings (Advanced) 🎭🅐🅓①",
137
+ "ADE_AnimateDiffModelSettingsAdvancedAttnStrengths": "[DEPR] Motion Model Settings (Adv. Attn) 🎭🅐🅓①",
138
+ # Gen2 Nodes
139
+ "ADE_UseEvolvedSampling": "Use Evolved Sampling 🎭🅐🅓②",
140
+ "ADE_ApplyAnimateDiffModelSimple": "Apply AnimateDiff Model 🎭🅐🅓②",
141
+ "ADE_ApplyAnimateDiffModel": "Apply AnimateDiff Model (Adv.) 🎭🅐🅓②",
142
+ "ADE_LoadAnimateDiffModel": "Load AnimateDiff Model 🎭🅐🅓②",
143
+ # MaskedLoraLoader
144
+ #"ADE_MaskedLoadLora": "Load LoRA (Masked) 🎭🅐🅓",
145
+ # Deprecated Nodes
146
+ "AnimateDiffLoaderV1": "AnimateDiff Loader [DEPRECATED] 🎭🅐🅓",
147
+ "ADE_AnimateDiffLoaderV1Advanced": "AnimateDiff Loader (Advanced) [DEPRECATED] 🎭🅐🅓",
148
+ "ADE_AnimateDiffCombine": "AnimateDiff Combine [DEPRECATED, Use Video Combine (VHS) Instead!] 🎭🅐🅓",
149
+ }
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_ad_settings.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .ad_settings import AdjustPE, AdjustPEGroup, AnimateDiffSettings
2
+ from .utils_model import BIGMAX
3
+
4
+
5
+ class AnimateDiffSettingsNode:
6
+ @classmethod
7
+ def INPUT_TYPES(s):
8
+ return {
9
+ "optional": {
10
+ "pe_adjust": ("PE_ADJUST",),
11
+ }
12
+ }
13
+
14
+ RETURN_TYPES = ("AD_SETTINGS",)
15
+ CATEGORY = "Animate Diff 🎭🅐🅓/ad settings"
16
+ FUNCTION = "get_ad_settings"
17
+
18
+ def get_ad_settings(self, pe_adjust: AdjustPEGroup=None):
19
+ return (AnimateDiffSettings(adjust_pe=pe_adjust),)
20
+
21
+
22
+ class ManualAdjustPENode:
23
+ @classmethod
24
+ def INPUT_TYPES(s):
25
+ return {
26
+ "required": {
27
+ "cap_initial_pe_length": ("INT", {"default": 0, "min": 0, "step": 1}),
28
+ "interpolate_pe_to_length": ("INT", {"default": 0, "min": 0, "step": 1}),
29
+ "initial_pe_idx_offset": ("INT", {"default": 0, "min": 0, "step": 1}),
30
+ "final_pe_idx_offset": ("INT", {"default": 0, "min": 0, "step": 1}),
31
+ "print_adjustment": ("BOOLEAN", {"default": False}),
32
+ },
33
+ "optional": {
34
+ "prev_pe_adjust": ("PE_ADJUST",),
35
+ }
36
+ }
37
+
38
+ RETURN_TYPES = ("PE_ADJUST",)
39
+ CATEGORY = "Animate Diff 🎭🅐🅓/ad settings/pe adjust"
40
+ FUNCTION = "get_pe_adjust"
41
+
42
+ def get_pe_adjust(self, cap_initial_pe_length: int, interpolate_pe_to_length: int,
43
+ initial_pe_idx_offset: int, final_pe_idx_offset: int, print_adjustment: bool,
44
+ prev_pe_adjust: AdjustPEGroup=None):
45
+ if prev_pe_adjust is None:
46
+ prev_pe_adjust = AdjustPEGroup()
47
+ prev_pe_adjust = prev_pe_adjust.clone()
48
+ adjust = AdjustPE(cap_initial_pe_length=cap_initial_pe_length, interpolate_pe_to_length=interpolate_pe_to_length,
49
+ initial_pe_idx_offset=initial_pe_idx_offset, final_pe_idx_offset=final_pe_idx_offset,
50
+ print_adjustment=print_adjustment)
51
+ prev_pe_adjust.add(adjust)
52
+ return (prev_pe_adjust,)
53
+
54
+
55
+ class SweetspotStretchPENode:
56
+ @classmethod
57
+ def INPUT_TYPES(s):
58
+ return {
59
+ "required": {
60
+ "sweetspot": ("INT", {"default": 16, "min": 0, "max": BIGMAX},),
61
+ "new_sweetspot": ("INT", {"default": 16, "min": 0, "max": BIGMAX},),
62
+ "print_adjustment": ("BOOLEAN", {"default": False}),
63
+ },
64
+ "optional": {
65
+ "prev_pe_adjust": ("PE_ADJUST",),
66
+ }
67
+ }
68
+
69
+ RETURN_TYPES = ("PE_ADJUST",)
70
+ CATEGORY = "Animate Diff 🎭🅐🅓/ad settings/pe adjust"
71
+ FUNCTION = "get_pe_adjust"
72
+
73
+ def get_pe_adjust(self, sweetspot: int, new_sweetspot: int, print_adjustment: bool, prev_pe_adjust: AdjustPEGroup=None):
74
+ if prev_pe_adjust is None:
75
+ prev_pe_adjust = AdjustPEGroup()
76
+ prev_pe_adjust = prev_pe_adjust.clone()
77
+ adjust = AdjustPE(cap_initial_pe_length=sweetspot, interpolate_pe_to_length=new_sweetspot,
78
+ print_adjustment=print_adjustment)
79
+ prev_pe_adjust.add(adjust)
80
+ return (prev_pe_adjust,)
81
+
82
+
83
+ class FullStretchPENode:
84
+ @classmethod
85
+ def INPUT_TYPES(s):
86
+ return {
87
+ "required": {
88
+ "pe_stretch": ("INT", {"default": 0, "min": 0, "max": BIGMAX},),
89
+ "print_adjustment": ("BOOLEAN", {"default": False}),
90
+ },
91
+ "optional": {
92
+ "prev_pe_adjust": ("PE_ADJUST",),
93
+ }
94
+ }
95
+
96
+ RETURN_TYPES = ("PE_ADJUST",)
97
+ CATEGORY = "Animate Diff 🎭🅐🅓/ad settings/pe adjust"
98
+ FUNCTION = "get_pe_adjust"
99
+
100
+ def get_pe_adjust(self, pe_stretch: int, print_adjustment: bool, prev_pe_adjust: AdjustPEGroup=None):
101
+ if prev_pe_adjust is None:
102
+ prev_pe_adjust = AdjustPEGroup()
103
+ prev_pe_adjust = prev_pe_adjust.clone()
104
+ adjust = AdjustPE(motion_pe_stretch=pe_stretch,
105
+ print_adjustment=print_adjustment)
106
+ prev_pe_adjust.add(adjust)
107
+ return (prev_pe_adjust,)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_context.py ADDED
@@ -0,0 +1,347 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .context import ContextFuseMethod, ContextOptions, ContextOptionsGroup, ContextSchedules
2
+ from .utils_model import BIGMAX
3
+
4
+
5
+ LENGTH_MAX = 128 # keep an eye on these max values;
6
+ STRIDE_MAX = 32 # would need to be updated
7
+ OVERLAP_MAX = 128 # if new motion modules come out
8
+
9
+
10
+ class LoopedUniformContextOptionsNode:
11
+ @classmethod
12
+ def INPUT_TYPES(s):
13
+ return {
14
+ "required": {
15
+ "context_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
16
+ "context_stride": ("INT", {"default": 1, "min": 1, "max": STRIDE_MAX}),
17
+ "context_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
18
+ "closed_loop": ("BOOLEAN", {"default": False},),
19
+ #"sync_context_to_pe": ("BOOLEAN", {"default": False},),
20
+ },
21
+ "optional": {
22
+ "fuse_method": (ContextFuseMethod.LIST,),
23
+ "use_on_equal_length": ("BOOLEAN", {"default": False},),
24
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
25
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
26
+ "prev_context": ("CONTEXT_OPTIONS",),
27
+ "view_opts": ("VIEW_OPTS",),
28
+ }
29
+ }
30
+
31
+ RETURN_TYPES = ("CONTEXT_OPTIONS",)
32
+ RETURN_NAMES = ("CONTEXT_OPTS",)
33
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts"
34
+ FUNCTION = "create_options"
35
+
36
+ def create_options(self, context_length: int, context_stride: int, context_overlap: int, closed_loop: bool,
37
+ fuse_method: str=ContextFuseMethod.FLAT, use_on_equal_length=False, start_percent: float=0.0, guarantee_steps: int=1,
38
+ view_opts: ContextOptions=None, prev_context: ContextOptionsGroup=None):
39
+ if prev_context is None:
40
+ prev_context = ContextOptionsGroup()
41
+ prev_context = prev_context.clone()
42
+
43
+ context_options = ContextOptions(
44
+ context_length=context_length,
45
+ context_stride=context_stride,
46
+ context_overlap=context_overlap,
47
+ context_schedule=ContextSchedules.UNIFORM_LOOPED,
48
+ closed_loop=closed_loop,
49
+ fuse_method=fuse_method,
50
+ use_on_equal_length=use_on_equal_length,
51
+ start_percent=start_percent,
52
+ guarantee_steps=guarantee_steps,
53
+ view_options=view_opts,
54
+ )
55
+ #context_options.set_sync_context_to_pe(sync_context_to_pe)
56
+ prev_context.add(context_options)
57
+ return (prev_context,)
58
+
59
+
60
+ # This Legacy version exists to maintain compatiblity with old workflows
61
+ class LegacyLoopedUniformContextOptionsNode:
62
+ @classmethod
63
+ def INPUT_TYPES(s):
64
+ return {
65
+ "required": {
66
+ "context_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
67
+ "context_stride": ("INT", {"default": 1, "min": 1, "max": STRIDE_MAX}),
68
+ "context_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
69
+ "context_schedule": (ContextSchedules.LEGACY_UNIFORM_SCHEDULE_LIST,),
70
+ "closed_loop": ("BOOLEAN", {"default": False},),
71
+ #"sync_context_to_pe": ("BOOLEAN", {"default": False},),
72
+ },
73
+ "optional": {
74
+ "fuse_method": (ContextFuseMethod.LIST, {"default": ContextFuseMethod.FLAT}),
75
+ "use_on_equal_length": ("BOOLEAN", {"default": False},),
76
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
77
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
78
+ "prev_context": ("CONTEXT_OPTIONS",),
79
+ "view_opts": ("VIEW_OPTS",),
80
+ }
81
+ }
82
+
83
+ RETURN_TYPES = ("CONTEXT_OPTIONS",)
84
+ RETURN_NAMES = ("CONTEXT_OPTS",)
85
+ CATEGORY = "" # No Category, so will not appear in menu
86
+ FUNCTION = "create_options"
87
+
88
+ def create_options(self, fuse_method: str=ContextFuseMethod.FLAT, context_schedule: str=None, **kwargs):
89
+ return LoopedUniformContextOptionsNode.create_options(self, fuse_method=fuse_method, **kwargs)
90
+
91
+
92
+ class StandardUniformContextOptionsNode:
93
+ @classmethod
94
+ def INPUT_TYPES(s):
95
+ return {
96
+ "required": {
97
+ "context_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
98
+ "context_stride": ("INT", {"default": 1, "min": 1, "max": STRIDE_MAX}),
99
+ "context_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
100
+ },
101
+ "optional": {
102
+ "fuse_method": (ContextFuseMethod.LIST,),
103
+ "use_on_equal_length": ("BOOLEAN", {"default": False},),
104
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
105
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
106
+ "prev_context": ("CONTEXT_OPTIONS",),
107
+ "view_opts": ("VIEW_OPTS",),
108
+ }
109
+ }
110
+
111
+ RETURN_TYPES = ("CONTEXT_OPTIONS",)
112
+ RETURN_NAMES = ("CONTEXT_OPTS",)
113
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts"
114
+ FUNCTION = "create_options"
115
+
116
+ def create_options(self, context_length: int, context_stride: int, context_overlap: int,
117
+ fuse_method: str=ContextFuseMethod.PYRAMID, use_on_equal_length=False, start_percent: float=0.0, guarantee_steps: int=1,
118
+ view_opts: ContextOptions=None, prev_context: ContextOptionsGroup=None):
119
+ if prev_context is None:
120
+ prev_context = ContextOptionsGroup()
121
+ prev_context = prev_context.clone()
122
+
123
+ context_options = ContextOptions(
124
+ context_length=context_length,
125
+ context_stride=context_stride,
126
+ context_overlap=context_overlap,
127
+ context_schedule=ContextSchedules.UNIFORM_STANDARD,
128
+ closed_loop=False,
129
+ fuse_method=fuse_method,
130
+ use_on_equal_length=use_on_equal_length,
131
+ start_percent=start_percent,
132
+ guarantee_steps=guarantee_steps,
133
+ view_options=view_opts,
134
+ )
135
+ prev_context.add(context_options)
136
+ return (prev_context,)
137
+
138
+
139
+ class StandardStaticContextOptionsNode:
140
+ @classmethod
141
+ def INPUT_TYPES(s):
142
+ return {
143
+ "required": {
144
+ "context_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
145
+ "context_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
146
+ },
147
+ "optional": {
148
+ "fuse_method": (ContextFuseMethod.LIST_STATIC,),
149
+ "use_on_equal_length": ("BOOLEAN", {"default": False},),
150
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
151
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
152
+ "prev_context": ("CONTEXT_OPTIONS",),
153
+ "view_opts": ("VIEW_OPTS",),
154
+ }
155
+ }
156
+
157
+ RETURN_TYPES = ("CONTEXT_OPTIONS",)
158
+ RETURN_NAMES = ("CONTEXT_OPTS",)
159
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts"
160
+ FUNCTION = "create_options"
161
+
162
+ def create_options(self, context_length: int, context_overlap: int,
163
+ fuse_method: str=ContextFuseMethod.PYRAMID, use_on_equal_length=False, start_percent: float=0.0, guarantee_steps: int=1,
164
+ view_opts: ContextOptions=None, prev_context: ContextOptionsGroup=None):
165
+ if prev_context is None:
166
+ prev_context = ContextOptionsGroup()
167
+ prev_context = prev_context.clone()
168
+
169
+ context_options = ContextOptions(
170
+ context_length=context_length,
171
+ context_stride=None,
172
+ context_overlap=context_overlap,
173
+ context_schedule=ContextSchedules.STATIC_STANDARD,
174
+ fuse_method=fuse_method,
175
+ use_on_equal_length=use_on_equal_length,
176
+ start_percent=start_percent,
177
+ guarantee_steps=guarantee_steps,
178
+ view_options=view_opts,
179
+ )
180
+ prev_context.add(context_options)
181
+ return (prev_context,)
182
+
183
+
184
+ class BatchedContextOptionsNode:
185
+ @classmethod
186
+ def INPUT_TYPES(s):
187
+ return {
188
+ "required": {
189
+ "context_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
190
+ },
191
+ "optional": {
192
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
193
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
194
+ "prev_context": ("CONTEXT_OPTIONS",),
195
+ }
196
+ }
197
+
198
+ RETURN_TYPES = ("CONTEXT_OPTIONS",)
199
+ RETURN_NAMES = ("CONTEXT_OPTS",)
200
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts"
201
+ FUNCTION = "create_options"
202
+
203
+ def create_options(self, context_length: int, start_percent: float=0.0, guarantee_steps: int=1,
204
+ prev_context: ContextOptionsGroup=None):
205
+ if prev_context is None:
206
+ prev_context = ContextOptionsGroup()
207
+ prev_context = prev_context.clone()
208
+
209
+ context_options = ContextOptions(
210
+ context_length=context_length,
211
+ context_overlap=0,
212
+ context_schedule=ContextSchedules.BATCHED,
213
+ start_percent=start_percent,
214
+ guarantee_steps=guarantee_steps,
215
+ )
216
+ prev_context.add(context_options)
217
+ return (prev_context,)
218
+
219
+
220
+ class ViewAsContextOptionsNode:
221
+ @classmethod
222
+ def INPUT_TYPES(s):
223
+ return {
224
+ "required": {
225
+ "view_opts_req": ("VIEW_OPTS",),
226
+ },
227
+ "optional": {
228
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
229
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
230
+ "prev_context": ("CONTEXT_OPTIONS",),
231
+ }
232
+ }
233
+
234
+ RETURN_TYPES = ("CONTEXT_OPTIONS",)
235
+ RETURN_NAMES = ("CONTEXT_OPTS",)
236
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts"
237
+ FUNCTION = "create_options"
238
+
239
+ def create_options(self, view_opts_req: ContextOptions, start_percent: float=0.0, guarantee_steps: int=1,
240
+ prev_context: ContextOptionsGroup=None):
241
+ if prev_context is None:
242
+ prev_context = ContextOptionsGroup()
243
+ prev_context = prev_context.clone()
244
+ context_options = ContextOptions(
245
+ context_schedule=ContextSchedules.VIEW_AS_CONTEXT,
246
+ start_percent=start_percent,
247
+ guarantee_steps=guarantee_steps,
248
+ view_options=view_opts_req,
249
+ use_on_equal_length=True
250
+ )
251
+ prev_context.add(context_options)
252
+ return (prev_context,)
253
+
254
+
255
+ #########################
256
+ # View Options
257
+ class StandardStaticViewOptionsNode:
258
+ @classmethod
259
+ def INPUT_TYPES(s):
260
+ return {
261
+ "required": {
262
+ "view_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
263
+ "view_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
264
+ },
265
+ "optional": {
266
+ "fuse_method": (ContextFuseMethod.LIST,),
267
+ }
268
+ }
269
+
270
+ RETURN_TYPES = ("VIEW_OPTS",)
271
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts/view opts"
272
+ FUNCTION = "create_options"
273
+
274
+ def create_options(self, view_length: int, view_overlap: int,
275
+ fuse_method: str=ContextFuseMethod.FLAT,):
276
+ view_options = ContextOptions(
277
+ context_length=view_length,
278
+ context_stride=None,
279
+ context_overlap=view_overlap,
280
+ context_schedule=ContextSchedules.STATIC_STANDARD,
281
+ fuse_method=fuse_method,
282
+ )
283
+ return (view_options,)
284
+
285
+
286
+ class StandardUniformViewOptionsNode:
287
+ @classmethod
288
+ def INPUT_TYPES(s):
289
+ return {
290
+ "required": {
291
+ "view_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
292
+ "view_stride": ("INT", {"default": 1, "min": 1, "max": STRIDE_MAX}),
293
+ "view_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
294
+ },
295
+ "optional": {
296
+ "fuse_method": (ContextFuseMethod.LIST,),
297
+ }
298
+ }
299
+
300
+ RETURN_TYPES = ("VIEW_OPTS",)
301
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts/view opts"
302
+ FUNCTION = "create_options"
303
+
304
+ def create_options(self, view_length: int, view_overlap: int, view_stride: int,
305
+ fuse_method: str=ContextFuseMethod.PYRAMID,):
306
+ view_options = ContextOptions(
307
+ context_length=view_length,
308
+ context_stride=view_stride,
309
+ context_overlap=view_overlap,
310
+ context_schedule=ContextSchedules.UNIFORM_STANDARD,
311
+ fuse_method=fuse_method,
312
+ )
313
+ return (view_options,)
314
+
315
+
316
+ class LoopedUniformViewOptionsNode:
317
+ @classmethod
318
+ def INPUT_TYPES(s):
319
+ return {
320
+ "required": {
321
+ "view_length": ("INT", {"default": 16, "min": 1, "max": LENGTH_MAX}),
322
+ "view_stride": ("INT", {"default": 1, "min": 1, "max": STRIDE_MAX}),
323
+ "view_overlap": ("INT", {"default": 4, "min": 0, "max": OVERLAP_MAX}),
324
+ "closed_loop": ("BOOLEAN", {"default": False},),
325
+ },
326
+ "optional": {
327
+ "fuse_method": (ContextFuseMethod.LIST,),
328
+ "use_on_equal_length": ("BOOLEAN", {"default": False},),
329
+ }
330
+ }
331
+
332
+ RETURN_TYPES = ("VIEW_OPTS",)
333
+ CATEGORY = "Animate Diff 🎭🅐🅓/context opts/view opts"
334
+ FUNCTION = "create_options"
335
+
336
+ def create_options(self, view_length: int, view_overlap: int, view_stride: int, closed_loop: bool,
337
+ fuse_method: str=ContextFuseMethod.PYRAMID, use_on_equal_length=False):
338
+ view_options = ContextOptions(
339
+ context_length=view_length,
340
+ context_stride=view_stride,
341
+ context_overlap=view_overlap,
342
+ context_schedule=ContextSchedules.UNIFORM_LOOPED,
343
+ closed_loop=closed_loop,
344
+ fuse_method=fuse_method,
345
+ use_on_equal_length=use_on_equal_length,
346
+ )
347
+ return (view_options,)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_deprecated.py ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import shutil
4
+ import subprocess
5
+ from typing import Dict, List
6
+
7
+ import numpy as np
8
+ import torch
9
+ from PIL import Image
10
+ from PIL.PngImagePlugin import PngInfo
11
+
12
+ import folder_paths
13
+ from comfy.model_patcher import ModelPatcher
14
+
15
+ from .context import ContextOptionsGroup, ContextOptions, ContextSchedules
16
+ from .logger import logger
17
+ from .utils_model import Folders, BetaSchedules, get_available_motion_models
18
+ from .model_injection import ModelPatcherAndInjector, InjectionParams, MotionModelGroup, load_motion_module_gen1
19
+
20
+
21
+ class AnimateDiffLoader_Deprecated:
22
+ @classmethod
23
+ def INPUT_TYPES(s):
24
+ return {
25
+ "required": {
26
+ "model": ("MODEL",),
27
+ "latents": ("LATENT",),
28
+ "model_name": (get_available_motion_models(),),
29
+ "unlimited_area_hack": ("BOOLEAN", {"default": False},),
30
+ "beta_schedule": (BetaSchedules.get_alias_list_with_first_element(BetaSchedules.SQRT_LINEAR),),
31
+ },
32
+ }
33
+
34
+ RETURN_TYPES = ("MODEL", "LATENT")
35
+ CATEGORY = ""
36
+ FUNCTION = "load_mm_and_inject_params"
37
+
38
+ def load_mm_and_inject_params(
39
+ self,
40
+ model: ModelPatcher,
41
+ latents: Dict[str, torch.Tensor],
42
+ model_name: str, unlimited_area_hack: bool, beta_schedule: str,
43
+ ):
44
+ # load motion module
45
+ motion_model = load_motion_module_gen1(model_name, model)
46
+ # get total frames
47
+ init_frames_len = len(latents["samples"]) # deprecated - no longer used for anything lol
48
+ # set injection params
49
+ params = InjectionParams(
50
+ unlimited_area_hack=unlimited_area_hack,
51
+ apply_mm_groupnorm_hack=True,
52
+ model_name=model_name,
53
+ apply_v2_properly=False,
54
+ )
55
+ # inject for use in sampling code
56
+ model = ModelPatcherAndInjector(model)
57
+ model.motion_models = MotionModelGroup(motion_model)
58
+ model.motion_injection_params = params
59
+
60
+ # save model sampling from BetaSchedule as object patch
61
+ # if autoselect, get suggested beta_schedule from motion model
62
+ if beta_schedule == BetaSchedules.AUTOSELECT and not model.motion_models.is_empty():
63
+ beta_schedule = model.motion_models[0].model.get_best_beta_schedule(log=True)
64
+ new_model_sampling = BetaSchedules.to_model_sampling(beta_schedule, model)
65
+ if new_model_sampling is not None:
66
+ model.add_object_patch("model_sampling", new_model_sampling)
67
+
68
+ del motion_model
69
+ return (model, latents)
70
+
71
+
72
+ class AnimateDiffLoaderAdvanced_Deprecated:
73
+ @classmethod
74
+ def INPUT_TYPES(s):
75
+ return {
76
+ "required": {
77
+ "model": ("MODEL",),
78
+ "latents": ("LATENT",),
79
+ "model_name": (get_available_motion_models(),),
80
+ "unlimited_area_hack": ("BOOLEAN", {"default": False},),
81
+ "context_length": ("INT", {"default": 16, "min": 0, "max": 1000}),
82
+ "context_stride": ("INT", {"default": 1, "min": 1, "max": 1000}),
83
+ "context_overlap": ("INT", {"default": 4, "min": 0, "max": 1000}),
84
+ "context_schedule": (ContextSchedules.LEGACY_UNIFORM_SCHEDULE_LIST,),
85
+ "closed_loop": ("BOOLEAN", {"default": False},),
86
+ "beta_schedule": (BetaSchedules.get_alias_list_with_first_element(BetaSchedules.SQRT_LINEAR),),
87
+ },
88
+ }
89
+
90
+ RETURN_TYPES = ("MODEL", "LATENT")
91
+ CATEGORY = ""
92
+ FUNCTION = "load_mm_and_inject_params"
93
+
94
+ def load_mm_and_inject_params(self,
95
+ model: ModelPatcher,
96
+ latents: Dict[str, torch.Tensor],
97
+ model_name: str, unlimited_area_hack: bool,
98
+ context_length: int, context_stride: int, context_overlap: int, context_schedule: str, closed_loop: bool,
99
+ beta_schedule: str,
100
+ ):
101
+ # load motion module
102
+ motion_model = load_motion_module_gen1(model_name, model)
103
+ # get total frames
104
+ init_frames_len = len(latents["samples"]) # deprecated - no longer used for anything lol
105
+ # set injection params
106
+ params = InjectionParams(
107
+ unlimited_area_hack=unlimited_area_hack,
108
+ apply_mm_groupnorm_hack=True,
109
+ model_name=model_name,
110
+ apply_v2_properly=False,
111
+ )
112
+ context_group = ContextOptionsGroup()
113
+ context_group.add(
114
+ ContextOptions(
115
+ context_length=context_length,
116
+ context_stride=context_stride,
117
+ context_overlap=context_overlap,
118
+ context_schedule=context_schedule,
119
+ closed_loop=closed_loop,
120
+ )
121
+ )
122
+ # set context settings
123
+ params.set_context(context_options=context_group)
124
+ # inject for use in sampling code
125
+ model = ModelPatcherAndInjector(model)
126
+ model.motion_models = MotionModelGroup(motion_model)
127
+ model.motion_injection_params = params
128
+
129
+ # save model sampling from BetaSchedule as object patch
130
+ # if autoselect, get suggested beta_schedule from motion model
131
+ if beta_schedule == BetaSchedules.AUTOSELECT and not model.motion_models.is_empty():
132
+ beta_schedule = model.motion_models[0].model.get_best_beta_schedule(log=True)
133
+ new_model_sampling = BetaSchedules.to_model_sampling(beta_schedule, model)
134
+ if new_model_sampling is not None:
135
+ model.add_object_patch("model_sampling", new_model_sampling)
136
+
137
+ del motion_model
138
+ return (model, latents)
139
+
140
+
141
+ class AnimateDiffCombine_Deprecated:
142
+ ffmpeg_warning_already_shown = False
143
+ @classmethod
144
+ def INPUT_TYPES(s):
145
+ ffmpeg_path = shutil.which("ffmpeg")
146
+ #Hide ffmpeg formats if ffmpeg isn't available
147
+ if ffmpeg_path is not None:
148
+ ffmpeg_formats = ["video/"+x[:-5] for x in folder_paths.get_filename_list(Folders.VIDEO_FORMATS)]
149
+ else:
150
+ ffmpeg_formats = []
151
+ if not s.ffmpeg_warning_already_shown:
152
+ # Deprecated node are now hidden, so no need to show warning unless node is used.
153
+ # logger.warning("This warning can be ignored, you should not be using the deprecated AnimateDiff Combine node anyway. If you are, use Video Combine from ComfyUI-VideoHelperSuite instead. ffmpeg could not be found. Outputs that require it have been disabled")
154
+ s.ffmpeg_warning_already_shown = True
155
+ return {
156
+ "required": {
157
+ "images": ("IMAGE",),
158
+ "frame_rate": (
159
+ "INT",
160
+ {"default": 8, "min": 1, "max": 24, "step": 1},
161
+ ),
162
+ "loop_count": ("INT", {"default": 0, "min": 0, "max": 100, "step": 1}),
163
+ "filename_prefix": ("STRING", {"default": "AnimateDiff"}),
164
+ "format": (["image/gif", "image/webp"] + ffmpeg_formats,),
165
+ "pingpong": ("BOOLEAN", {"default": False}),
166
+ "save_image": ("BOOLEAN", {"default": True}),
167
+ },
168
+ "hidden": {
169
+ "prompt": "PROMPT",
170
+ "extra_pnginfo": "EXTRA_PNGINFO",
171
+ },
172
+ }
173
+
174
+ RETURN_TYPES = ("GIF",)
175
+ OUTPUT_NODE = True
176
+ CATEGORY = ""
177
+ FUNCTION = "generate_gif"
178
+
179
+ def generate_gif(
180
+ self,
181
+ images,
182
+ frame_rate: int,
183
+ loop_count: int,
184
+ filename_prefix="AnimateDiff",
185
+ format="image/gif",
186
+ pingpong=False,
187
+ save_image=True,
188
+ prompt=None,
189
+ extra_pnginfo=None,
190
+ ):
191
+ logger.warning("Do not use AnimateDiff Combine node, it is deprecated. Use Video Combine node from ComfyUI-VideoHelperSuite instead. Video nodes from VideoHelperSuite are actively maintained, more feature-rich, and also automatically attempts to get ffmpeg.")
192
+ # convert images to numpy
193
+ frames: List[Image.Image] = []
194
+ for image in images:
195
+ img = 255.0 * image.cpu().numpy()
196
+ img = Image.fromarray(np.clip(img, 0, 255).astype(np.uint8))
197
+ frames.append(img)
198
+
199
+ # get output information
200
+ output_dir = (
201
+ folder_paths.get_output_directory()
202
+ if save_image
203
+ else folder_paths.get_temp_directory()
204
+ )
205
+ (
206
+ full_output_folder,
207
+ filename,
208
+ counter,
209
+ subfolder,
210
+ _,
211
+ ) = folder_paths.get_save_image_path(filename_prefix, output_dir)
212
+
213
+ metadata = PngInfo()
214
+ if prompt is not None:
215
+ metadata.add_text("prompt", json.dumps(prompt))
216
+ if extra_pnginfo is not None:
217
+ for x in extra_pnginfo:
218
+ metadata.add_text(x, json.dumps(extra_pnginfo[x]))
219
+
220
+ # save first frame as png to keep metadata
221
+ file = f"{filename}_{counter:05}_.png"
222
+ file_path = os.path.join(full_output_folder, file)
223
+ frames[0].save(
224
+ file_path,
225
+ pnginfo=metadata,
226
+ compress_level=4,
227
+ )
228
+ if pingpong:
229
+ frames = frames + frames[-2:0:-1]
230
+
231
+ format_type, format_ext = format.split("/")
232
+ file = f"{filename}_{counter:05}_.{format_ext}"
233
+ file_path = os.path.join(full_output_folder, file)
234
+ if format_type == "image":
235
+ # Use pillow directly to save an animated image
236
+ frames[0].save(
237
+ file_path,
238
+ format=format_ext.upper(),
239
+ save_all=True,
240
+ append_images=frames[1:],
241
+ duration=round(1000 / frame_rate),
242
+ loop=loop_count,
243
+ compress_level=4,
244
+ )
245
+ else:
246
+ # Use ffmpeg to save a video
247
+ ffmpeg_path = shutil.which("ffmpeg")
248
+ if ffmpeg_path is None:
249
+ #Should never be reachable
250
+ raise ProcessLookupError("Could not find ffmpeg")
251
+
252
+ video_format_path = folder_paths.get_full_path("video_formats", format_ext + ".json")
253
+ with open(video_format_path, 'r') as stream:
254
+ video_format = json.load(stream)
255
+ file = f"{filename}_{counter:05}_.{video_format['extension']}"
256
+ file_path = os.path.join(full_output_folder, file)
257
+ dimensions = f"{frames[0].width}x{frames[0].height}"
258
+ args = [ffmpeg_path, "-v", "error", "-f", "rawvideo", "-pix_fmt", "rgb24",
259
+ "-s", dimensions, "-r", str(frame_rate), "-i", "-"] \
260
+ + video_format['main_pass'] + [file_path]
261
+
262
+ env=os.environ.copy()
263
+ if "environment" in video_format:
264
+ env.update(video_format["environment"])
265
+ with subprocess.Popen(args, stdin=subprocess.PIPE, env=env) as proc:
266
+ for frame in frames:
267
+ proc.stdin.write(frame.tobytes())
268
+
269
+ previews = [
270
+ {
271
+ "filename": file,
272
+ "subfolder": subfolder,
273
+ "type": "output" if save_image else "temp",
274
+ "format": format,
275
+ }
276
+ ]
277
+ return {"ui": {"gifs": previews}}
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_extras.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ import folder_paths
4
+ import nodes as comfy_nodes
5
+ from comfy.model_patcher import ModelPatcher
6
+ from comfy.sd import load_checkpoint_guess_config
7
+
8
+ from .logger import logger
9
+ from .utils_model import BetaSchedules
10
+ from .model_injection import get_vanilla_model_patcher
11
+
12
+
13
+ class AnimateDiffUnload:
14
+ def __init__(self) -> None:
15
+ pass
16
+
17
+ @classmethod
18
+ def INPUT_TYPES(s):
19
+ return {"required": {"model": ("MODEL",)}}
20
+
21
+ RETURN_TYPES = ("MODEL",)
22
+ CATEGORY = "Animate Diff 🎭🅐🅓/extras"
23
+ FUNCTION = "unload_motion_modules"
24
+
25
+ def unload_motion_modules(self, model: ModelPatcher):
26
+ # return model clone with ejected params
27
+ #model = eject_params_from_model(model)
28
+ model = get_vanilla_model_patcher(model)
29
+ return (model.clone(),)
30
+
31
+
32
+ class CheckpointLoaderSimpleWithNoiseSelect:
33
+ @classmethod
34
+ def INPUT_TYPES(s):
35
+ return {
36
+ "required": {
37
+ "ckpt_name": (folder_paths.get_filename_list("checkpoints"), ),
38
+ "beta_schedule": (BetaSchedules.ALIAS_LIST, {"default": BetaSchedules.USE_EXISTING}, )
39
+ },
40
+ "optional": {
41
+ "use_custom_scale_factor": ("BOOLEAN", {"default": False}),
42
+ "scale_factor": ("FLOAT", {"default": 0.18215, "min": 0.0, "max": 1.0, "step": 0.00001})
43
+ }
44
+ }
45
+ RETURN_TYPES = ("MODEL", "CLIP", "VAE")
46
+ FUNCTION = "load_checkpoint"
47
+
48
+ CATEGORY = "Animate Diff 🎭🅐🅓/extras"
49
+
50
+ def load_checkpoint(self, ckpt_name, beta_schedule, output_vae=True, output_clip=True, use_custom_scale_factor=False, scale_factor=0.18215):
51
+ ckpt_path = folder_paths.get_full_path("checkpoints", ckpt_name)
52
+ out = load_checkpoint_guess_config(ckpt_path, output_vae=True, output_clip=True, embedding_directory=folder_paths.get_folder_paths("embeddings"))
53
+ # register chosen beta schedule on model - convert to beta_schedule name recognized by ComfyUI
54
+ new_model_sampling = BetaSchedules.to_model_sampling(beta_schedule, out[0])
55
+ if new_model_sampling is not None:
56
+ out[0].model.model_sampling = new_model_sampling
57
+ if use_custom_scale_factor:
58
+ out[0].model.latent_format.scale_factor = scale_factor
59
+ return out
60
+
61
+
62
+ class EmptyLatentImageLarge:
63
+ def __init__(self, device="cpu"):
64
+ self.device = device
65
+
66
+ @classmethod
67
+ def INPUT_TYPES(s):
68
+ return {"required": { "width": ("INT", {"default": 512, "min": 64, "max": comfy_nodes.MAX_RESOLUTION, "step": 8}),
69
+ "height": ("INT", {"default": 512, "min": 64, "max": comfy_nodes.MAX_RESOLUTION, "step": 8}),
70
+ "batch_size": ("INT", {"default": 1, "min": 1, "max": 262144})}}
71
+ RETURN_TYPES = ("LATENT",)
72
+ FUNCTION = "generate"
73
+
74
+ CATEGORY = "Animate Diff 🎭🅐🅓/extras"
75
+
76
+ def generate(self, width, height, batch_size=1):
77
+ latent = torch.zeros([batch_size, 4, height // 8, width // 8])
78
+ return ({"samples":latent}, )
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_gen1.py ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import torch
3
+
4
+ import comfy.sample as comfy_sample
5
+ from comfy.model_patcher import ModelPatcher
6
+
7
+ from .ad_settings import AdjustPEGroup, AnimateDiffSettings, AdjustPE
8
+ from .context import ContextOptions, ContextOptionsGroup, ContextSchedules
9
+ from .logger import logger
10
+ from .utils_model import BetaSchedules, get_available_motion_loras, get_available_motion_models, get_motion_lora_path
11
+ from .utils_motion import ADKeyframeGroup, get_combined_multival
12
+ from .motion_lora import MotionLoraInfo, MotionLoraList
13
+ from .model_injection import InjectionParams, ModelPatcherAndInjector, MotionModelGroup, load_motion_lora_as_patches, load_motion_module_gen1, load_motion_module_gen2, validate_model_compatibility_gen2
14
+ from .sample_settings import SampleSettings, SeedNoiseGeneration
15
+ from .sampling import motion_sample_factory
16
+
17
+
18
+ class AnimateDiffLoaderGen1:
19
+ @classmethod
20
+ def INPUT_TYPES(s):
21
+ return {
22
+ "required": {
23
+ "model": ("MODEL",),
24
+ "model_name": (get_available_motion_models(),),
25
+ "beta_schedule": (BetaSchedules.ALIAS_LIST, {"default": BetaSchedules.AUTOSELECT}),
26
+ #"apply_mm_groupnorm_hack": ("BOOLEAN", {"default": True}),
27
+ },
28
+ "optional": {
29
+ "context_options": ("CONTEXT_OPTIONS",),
30
+ "motion_lora": ("MOTION_LORA",),
31
+ "ad_settings": ("AD_SETTINGS",),
32
+ "ad_keyframes": ("AD_KEYFRAMES",),
33
+ "sample_settings": ("SAMPLE_SETTINGS",),
34
+ "scale_multival": ("MULTIVAL",),
35
+ "effect_multival": ("MULTIVAL",),
36
+ }
37
+ }
38
+
39
+ RETURN_TYPES = ("MODEL",)
40
+ CATEGORY = "Animate Diff 🎭🅐🅓/① Gen1 nodes ①"
41
+ FUNCTION = "load_mm_and_inject_params"
42
+
43
+ def load_mm_and_inject_params(self,
44
+ model: ModelPatcher,
45
+ model_name: str, beta_schedule: str,# apply_mm_groupnorm_hack: bool,
46
+ context_options: ContextOptionsGroup=None, motion_lora: MotionLoraList=None, ad_settings: AnimateDiffSettings=None,
47
+ sample_settings: SampleSettings=None, scale_multival=None, effect_multival=None, ad_keyframes: ADKeyframeGroup=None,
48
+ ):
49
+ # load motion module and motion settings, if included
50
+ motion_model = load_motion_module_gen2(model_name=model_name, motion_model_settings=ad_settings)
51
+ # confirm that it is compatible with SD model
52
+ validate_model_compatibility_gen2(model=model, motion_model=motion_model)
53
+ # apply motion model to loaded_mm
54
+ if motion_lora is not None:
55
+ for lora in motion_lora.loras:
56
+ load_motion_lora_as_patches(motion_model, lora)
57
+ motion_model.scale_multival = scale_multival
58
+ motion_model.effect_multival = effect_multival
59
+ motion_model.keyframes = ad_keyframes.clone() if ad_keyframes else ADKeyframeGroup()
60
+
61
+ # create injection params
62
+ params = InjectionParams(unlimited_area_hack=False, model_name=motion_model.model.mm_info.mm_name)
63
+ # apply context options
64
+ if context_options:
65
+ params.set_context(context_options)
66
+
67
+ # set motion_scale and motion_model_settings
68
+ if not ad_settings:
69
+ ad_settings = AnimateDiffSettings()
70
+ ad_settings.attn_scale = 1.0
71
+ params.set_motion_model_settings(ad_settings)
72
+
73
+ # backwards compatibility to support old way of masking scale
74
+ if params.motion_model_settings.mask_attn_scale is not None:
75
+ motion_model.scale_multival = get_combined_multival(scale_multival, (params.motion_model_settings.mask_attn_scale * params.motion_model_settings.attn_scale))
76
+
77
+ # need to use a ModelPatcher that supports injection of motion modules into unet
78
+ # need to use a ModelPatcher that supports injection of motion modules into unet
79
+ model = ModelPatcherAndInjector(model)
80
+ model.motion_models = MotionModelGroup(motion_model)
81
+ model.sample_settings = sample_settings if sample_settings is not None else SampleSettings()
82
+ model.motion_injection_params = params
83
+
84
+ if model.sample_settings.custom_cfg is not None:
85
+ logger.info("[Sample Settings] custom_cfg is set; will override any KSampler cfg values or patches.")
86
+
87
+ if model.sample_settings.sigma_schedule is not None:
88
+ logger.info("[Sample Settings] sigma_schedule is set; will override beta_schedule.")
89
+ model.add_object_patch("model_sampling", model.sample_settings.sigma_schedule.clone().model_sampling)
90
+ else:
91
+ # save model sampling from BetaSchedule as object patch
92
+ # if autoselect, get suggested beta_schedule from motion model
93
+ if beta_schedule == BetaSchedules.AUTOSELECT and not model.motion_models.is_empty():
94
+ beta_schedule = model.motion_models[0].model.get_best_beta_schedule(log=True)
95
+ new_model_sampling = BetaSchedules.to_model_sampling(beta_schedule, model)
96
+ if new_model_sampling is not None:
97
+ model.add_object_patch("model_sampling", new_model_sampling)
98
+
99
+ del motion_model
100
+ return (model,)
101
+
102
+
103
+ class LegacyAnimateDiffLoaderWithContext:
104
+ @classmethod
105
+ def INPUT_TYPES(s):
106
+ return {
107
+ "required": {
108
+ "model": ("MODEL",),
109
+ "model_name": (get_available_motion_models(),),
110
+ "beta_schedule": (BetaSchedules.ALIAS_LIST, {"default": BetaSchedules.AUTOSELECT}),
111
+ #"apply_mm_groupnorm_hack": ("BOOLEAN", {"default": True}),
112
+ },
113
+ "optional": {
114
+ "context_options": ("CONTEXT_OPTIONS",),
115
+ "motion_lora": ("MOTION_LORA",),
116
+ "ad_settings": ("AD_SETTINGS",),
117
+ "sample_settings": ("SAMPLE_SETTINGS",),
118
+ "motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
119
+ "apply_v2_models_properly": ("BOOLEAN", {"default": True}),
120
+ "ad_keyframes": ("AD_KEYFRAMES",),
121
+ }
122
+ }
123
+
124
+ RETURN_TYPES = ("MODEL",)
125
+ CATEGORY = "Animate Diff 🎭🅐🅓/① Gen1 nodes ①"
126
+ FUNCTION = "load_mm_and_inject_params"
127
+
128
+
129
+ def load_mm_and_inject_params(self,
130
+ model: ModelPatcher,
131
+ model_name: str, beta_schedule: str,# apply_mm_groupnorm_hack: bool,
132
+ context_options: ContextOptionsGroup=None, motion_lora: MotionLoraList=None, ad_settings: AnimateDiffSettings=None, motion_model_settings: AnimateDiffSettings=None,
133
+ sample_settings: SampleSettings=None, motion_scale: float=1.0, apply_v2_models_properly: bool=False, ad_keyframes: ADKeyframeGroup=None,
134
+ ):
135
+ if ad_settings is not None:
136
+ motion_model_settings = ad_settings
137
+ # load motion module
138
+ motion_model = load_motion_module_gen1(model_name, model, motion_lora=motion_lora, motion_model_settings=motion_model_settings)
139
+ # set injection params
140
+ params = InjectionParams(
141
+ unlimited_area_hack=False,
142
+ model_name=model_name,
143
+ apply_v2_properly=apply_v2_models_properly,
144
+ )
145
+ if context_options:
146
+ params.set_context(context_options)
147
+ # set motion_scale and motion_model_settings
148
+ if not motion_model_settings:
149
+ motion_model_settings = AnimateDiffSettings()
150
+ motion_model_settings.attn_scale = motion_scale
151
+ params.set_motion_model_settings(motion_model_settings)
152
+
153
+ if params.motion_model_settings.mask_attn_scale is not None:
154
+ motion_model.scale_multival = params.motion_model_settings.mask_attn_scale * params.motion_model_settings.attn_scale
155
+ else:
156
+ motion_model.scale_multival = params.motion_model_settings.attn_scale
157
+
158
+ motion_model.keyframes = ad_keyframes.clone() if ad_keyframes else ADKeyframeGroup()
159
+
160
+ model = ModelPatcherAndInjector(model)
161
+ model.motion_models = MotionModelGroup(motion_model)
162
+ model.sample_settings = sample_settings if sample_settings is not None else SampleSettings()
163
+ model.motion_injection_params = params
164
+
165
+ # save model sampling from BetaSchedule as object patch
166
+ # if autoselect, get suggested beta_schedule from motion model
167
+ if beta_schedule == BetaSchedules.AUTOSELECT and not model.motion_models.is_empty():
168
+ beta_schedule = model.motion_models[0].model.get_best_beta_schedule(log=True)
169
+ new_model_sampling = BetaSchedules.to_model_sampling(beta_schedule, model)
170
+ if new_model_sampling is not None:
171
+ model.add_object_patch("model_sampling", new_model_sampling)
172
+
173
+ del motion_model
174
+ return (model,)
175
+
176
+
177
+ class AnimateDiffModelSettings:
178
+ @classmethod
179
+ def INPUT_TYPES(s):
180
+ return {
181
+ "required": {
182
+ "min_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
183
+ "max_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
184
+ },
185
+ "optional": {
186
+ "mask_motion_scale": ("MASK",),
187
+ }
188
+ }
189
+
190
+ RETURN_TYPES = ("AD_SETTINGS",)
191
+ CATEGORY = "" #"Animate Diff 🎭🅐🅓/① Gen1 nodes ①/motion settings"
192
+ FUNCTION = "get_motion_model_settings"
193
+
194
+ def get_motion_model_settings(self, mask_motion_scale: torch.Tensor=None, min_motion_scale: float=1.0, max_motion_scale: float=1.0):
195
+ motion_model_settings = AnimateDiffSettings(
196
+ mask_attn_scale=mask_motion_scale,
197
+ mask_attn_scale_min=min_motion_scale,
198
+ mask_attn_scale_max=max_motion_scale,
199
+ )
200
+
201
+ return (motion_model_settings,)
202
+
203
+
204
+ class AnimateDiffModelSettingsSimple:
205
+ @classmethod
206
+ def INPUT_TYPES(s):
207
+ return {
208
+ "required": {
209
+ "motion_pe_stretch": ("INT", {"default": 0, "min": 0, "step": 1}),
210
+ },
211
+ "optional": {
212
+ "mask_motion_scale": ("MASK",),
213
+ "min_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
214
+ "max_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
215
+ }
216
+ }
217
+
218
+ RETURN_TYPES = ("AD_SETTINGS",)
219
+ CATEGORY = "" #"Animate Diff 🎭🅐🅓/① Gen1 nodes ①/motion settings/experimental"
220
+ FUNCTION = "get_motion_model_settings"
221
+
222
+ def get_motion_model_settings(self, motion_pe_stretch: int,
223
+ mask_motion_scale: torch.Tensor=None, min_motion_scale: float=1.0, max_motion_scale: float=1.0):
224
+ adjust_pe = AdjustPEGroup(AdjustPE(motion_pe_stretch=motion_pe_stretch))
225
+ motion_model_settings = AnimateDiffSettings(
226
+ adjust_pe=adjust_pe,
227
+ mask_attn_scale=mask_motion_scale,
228
+ mask_attn_scale_min=min_motion_scale,
229
+ mask_attn_scale_max=max_motion_scale,
230
+ )
231
+
232
+ return (motion_model_settings,)
233
+
234
+
235
+ class AnimateDiffModelSettingsAdvanced:
236
+ @classmethod
237
+ def INPUT_TYPES(s):
238
+ return {
239
+ "required": {
240
+ "pe_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
241
+ "attn_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
242
+ "other_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
243
+ "motion_pe_stretch": ("INT", {"default": 0, "min": 0, "step": 1}),
244
+ "cap_initial_pe_length": ("INT", {"default": 0, "min": 0, "step": 1}),
245
+ "interpolate_pe_to_length": ("INT", {"default": 0, "min": 0, "step": 1}),
246
+ "initial_pe_idx_offset": ("INT", {"default": 0, "min": 0, "step": 1}),
247
+ "final_pe_idx_offset": ("INT", {"default": 0, "min": 0, "step": 1}),
248
+ },
249
+ "optional": {
250
+ "mask_motion_scale": ("MASK",),
251
+ "min_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
252
+ "max_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
253
+ }
254
+ }
255
+
256
+ RETURN_TYPES = ("AD_SETTINGS",)
257
+ CATEGORY = "" #"Animate Diff 🎭🅐🅓/① Gen1 nodes ①/motion settings/experimental"
258
+ FUNCTION = "get_motion_model_settings"
259
+
260
+ def get_motion_model_settings(self, pe_strength: float, attn_strength: float, other_strength: float,
261
+ motion_pe_stretch: int,
262
+ cap_initial_pe_length: int, interpolate_pe_to_length: int,
263
+ initial_pe_idx_offset: int, final_pe_idx_offset: int,
264
+ mask_motion_scale: torch.Tensor=None, min_motion_scale: float=1.0, max_motion_scale: float=1.0):
265
+ adjust_pe = AdjustPEGroup(AdjustPE(motion_pe_stretch=motion_pe_stretch,
266
+ cap_initial_pe_length=cap_initial_pe_length, interpolate_pe_to_length=interpolate_pe_to_length,
267
+ initial_pe_idx_offset=initial_pe_idx_offset, final_pe_idx_offset=final_pe_idx_offset))
268
+ motion_model_settings = AnimateDiffSettings(
269
+ adjust_pe=adjust_pe,
270
+ pe_strength=pe_strength,
271
+ attn_strength=attn_strength,
272
+ other_strength=other_strength,
273
+ mask_attn_scale=mask_motion_scale,
274
+ mask_attn_scale_min=min_motion_scale,
275
+ mask_attn_scale_max=max_motion_scale,
276
+ )
277
+
278
+ return (motion_model_settings,)
279
+
280
+
281
+ class AnimateDiffModelSettingsAdvancedAttnStrengths:
282
+ @classmethod
283
+ def INPUT_TYPES(s):
284
+ return {
285
+ "required": {
286
+ "pe_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
287
+ "attn_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
288
+ "attn_q_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
289
+ "attn_k_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
290
+ "attn_v_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
291
+ "attn_out_weight_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
292
+ "attn_out_bias_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
293
+ "other_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.0001}),
294
+ "motion_pe_stretch": ("INT", {"default": 0, "min": 0, "step": 1}),
295
+ "cap_initial_pe_length": ("INT", {"default": 0, "min": 0, "step": 1}),
296
+ "interpolate_pe_to_length": ("INT", {"default": 0, "min": 0, "step": 1}),
297
+ "initial_pe_idx_offset": ("INT", {"default": 0, "min": 0, "step": 1}),
298
+ "final_pe_idx_offset": ("INT", {"default": 0, "min": 0, "step": 1}),
299
+ },
300
+ "optional": {
301
+ "mask_motion_scale": ("MASK",),
302
+ "min_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
303
+ "max_motion_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
304
+ }
305
+ }
306
+
307
+ RETURN_TYPES = ("AD_SETTINGS",)
308
+ CATEGORY = "" #"Animate Diff 🎭🅐🅓/① Gen1 nodes ①/motion settings/experimental"
309
+ FUNCTION = "get_motion_model_settings"
310
+
311
+ def get_motion_model_settings(self, pe_strength: float, attn_strength: float,
312
+ attn_q_strength: float,
313
+ attn_k_strength: float,
314
+ attn_v_strength: float,
315
+ attn_out_weight_strength: float,
316
+ attn_out_bias_strength: float,
317
+ other_strength: float,
318
+ motion_pe_stretch: int,
319
+ cap_initial_pe_length: int, interpolate_pe_to_length: int,
320
+ initial_pe_idx_offset: int, final_pe_idx_offset: int,
321
+ mask_motion_scale: torch.Tensor=None, min_motion_scale: float=1.0, max_motion_scale: float=1.0):
322
+ adjust_pe = AdjustPEGroup(AdjustPE(motion_pe_stretch=motion_pe_stretch,
323
+ cap_initial_pe_length=cap_initial_pe_length, interpolate_pe_to_length=interpolate_pe_to_length,
324
+ initial_pe_idx_offset=initial_pe_idx_offset, final_pe_idx_offset=final_pe_idx_offset))
325
+ motion_model_settings = AnimateDiffSettings(
326
+ adjust_pe=adjust_pe,
327
+ pe_strength=pe_strength,
328
+ attn_strength=attn_strength,
329
+ attn_q_strength=attn_q_strength,
330
+ attn_k_strength=attn_k_strength,
331
+ attn_v_strength=attn_v_strength,
332
+ attn_out_weight_strength=attn_out_weight_strength,
333
+ attn_out_bias_strength=attn_out_bias_strength,
334
+ other_strength=other_strength,
335
+ mask_attn_scale=mask_motion_scale,
336
+ mask_attn_scale_min=min_motion_scale,
337
+ mask_attn_scale_max=max_motion_scale,
338
+ )
339
+
340
+ return (motion_model_settings,)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_gen2.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import torch
3
+
4
+ import comfy.sample as comfy_sample
5
+ from comfy.model_patcher import ModelPatcher
6
+
7
+ from .ad_settings import AnimateDiffSettings
8
+ from .context import ContextOptions, ContextOptionsGroup, ContextSchedules
9
+ from .logger import logger
10
+ from .utils_model import BIGMAX, BetaSchedules, get_available_motion_loras, get_available_motion_models, get_motion_lora_path
11
+ from .utils_motion import ADKeyframeGroup, ADKeyframe
12
+ from .motion_lora import MotionLoraInfo, MotionLoraList
13
+ from .model_injection import (InjectionParams, ModelPatcherAndInjector, MotionModelGroup, MotionModelPatcher, create_fresh_motion_module,
14
+ load_motion_module_gen1, load_motion_module_gen2, load_motion_lora_as_patches, validate_model_compatibility_gen2)
15
+ from .sample_settings import SampleSettings, SeedNoiseGeneration
16
+ from .sampling import motion_sample_factory
17
+
18
+
19
+ class UseEvolvedSamplingNode:
20
+ @classmethod
21
+ def INPUT_TYPES(s):
22
+ return {
23
+ "required": {
24
+ "model": ("MODEL",),
25
+ "beta_schedule": (BetaSchedules.ALIAS_LIST, {"default": BetaSchedules.AUTOSELECT}),
26
+ },
27
+ "optional": {
28
+ "m_models": ("M_MODELS",),
29
+ "context_options": ("CONTEXT_OPTIONS",),
30
+ "sample_settings": ("SAMPLE_SETTINGS",),
31
+ #"beta_schedule_override": ("BETA_SCHEDULE",),
32
+ }
33
+ }
34
+
35
+ RETURN_TYPES = ("MODEL",)
36
+ CATEGORY = "Animate Diff 🎭🅐🅓/② Gen2 nodes ②"
37
+ FUNCTION = "use_evolved_sampling"
38
+
39
+ def use_evolved_sampling(self, model: ModelPatcher, beta_schedule: str, m_models: MotionModelGroup=None, context_options: ContextOptionsGroup=None,
40
+ sample_settings: SampleSettings=None, beta_schedule_override=None):
41
+ if m_models is not None:
42
+ m_models = m_models.clone()
43
+ # for each motion model, confirm that it is compatible with SD model
44
+ for motion_model in m_models.models:
45
+ validate_model_compatibility_gen2(model=model, motion_model=motion_model)
46
+ # create injection params
47
+ model_name_list = [motion_model.model.mm_info.mm_name for motion_model in m_models.models]
48
+ model_names = ",".join(model_name_list)
49
+ # TODO: check if any apply_v2_properly is set to False
50
+ params = InjectionParams(unlimited_area_hack=False, model_name=model_names)
51
+ else:
52
+ params = InjectionParams()
53
+ # apply context options
54
+ if context_options:
55
+ params.set_context(context_options)
56
+ # need to use a ModelPatcher that supports injection of motion modules into unet
57
+ model = ModelPatcherAndInjector(model)
58
+ model.motion_models = m_models
59
+ model.sample_settings = sample_settings if sample_settings is not None else SampleSettings()
60
+ model.motion_injection_params = params
61
+
62
+ if model.sample_settings.custom_cfg is not None:
63
+ logger.info("[Sample Settings] custom_cfg is set; will override any KSampler cfg values or patches.")
64
+
65
+ if model.sample_settings.sigma_schedule is not None:
66
+ logger.info("[Sample Settings] sigma_schedule is set; will override beta_schedule.")
67
+ model.add_object_patch("model_sampling", model.sample_settings.sigma_schedule.clone().model_sampling)
68
+ else:
69
+ # save model_sampling from BetaSchedule as object patch
70
+ # if autoselect, get suggested beta_schedule from motion model
71
+ if beta_schedule == BetaSchedules.AUTOSELECT and not model.motion_models.is_empty():
72
+ beta_schedule = model.motion_models[0].model.get_best_beta_schedule(log=True)
73
+ new_model_sampling = BetaSchedules.to_model_sampling(beta_schedule, model)
74
+ if new_model_sampling is not None:
75
+ model.add_object_patch("model_sampling", new_model_sampling)
76
+
77
+ del m_models
78
+ return (model,)
79
+
80
+
81
+ class ApplyAnimateDiffModelNode:
82
+ @classmethod
83
+ def INPUT_TYPES(s):
84
+ return {
85
+ "required": {
86
+ "motion_model": ("MOTION_MODEL_ADE",),
87
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
88
+ "end_percent": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}),
89
+ },
90
+ "optional": {
91
+ "motion_lora": ("MOTION_LORA",),
92
+ "scale_multival": ("MULTIVAL",),
93
+ "effect_multival": ("MULTIVAL",),
94
+ "ad_keyframes": ("AD_KEYFRAMES",),
95
+ "prev_m_models": ("M_MODELS",),
96
+ }
97
+ }
98
+
99
+ RETURN_TYPES = ("M_MODELS",)
100
+ CATEGORY = "Animate Diff 🎭🅐🅓/② Gen2 nodes ②"
101
+ FUNCTION = "apply_motion_model"
102
+
103
+ def apply_motion_model(self, motion_model: MotionModelPatcher, start_percent: float=0.0, end_percent: float=1.0,
104
+ motion_lora: MotionLoraList=None, ad_keyframes: ADKeyframeGroup=None,
105
+ scale_multival=None, effect_multival=None,
106
+ prev_m_models: MotionModelGroup=None,):
107
+ # set up motion models list
108
+ if prev_m_models is None:
109
+ prev_m_models = MotionModelGroup()
110
+ prev_m_models = prev_m_models.clone()
111
+ motion_model = motion_model.clone()
112
+ # check if internal motion model already present in previous model - create new if so
113
+ for prev_model in prev_m_models.models:
114
+ if motion_model.model is prev_model.model:
115
+ # need to create new internal model based on same state_dict
116
+ motion_model = create_fresh_motion_module(motion_model)
117
+ # apply motion model to loaded_mm
118
+ if motion_lora is not None:
119
+ for lora in motion_lora.loras:
120
+ load_motion_lora_as_patches(motion_model, lora)
121
+ motion_model.scale_multival = scale_multival
122
+ motion_model.effect_multival = effect_multival
123
+ motion_model.keyframes = ad_keyframes.clone() if ad_keyframes else ADKeyframeGroup()
124
+ motion_model.timestep_percent_range = (start_percent, end_percent)
125
+ # add to beginning, so that after injection, it will be the earliest of prev_m_models to be run
126
+ prev_m_models.add_to_start(mm=motion_model)
127
+ return (prev_m_models,)
128
+
129
+
130
+ class ApplyAnimateDiffModelBasicNode:
131
+ @classmethod
132
+ def INPUT_TYPES(s):
133
+ return {
134
+ "required": {
135
+ "motion_model": ("MOTION_MODEL_ADE",),
136
+ },
137
+ "optional": {
138
+ "motion_lora": ("MOTION_LORA",),
139
+ "scale_multival": ("MULTIVAL",),
140
+ "effect_multival": ("MULTIVAL",),
141
+ "ad_keyframes": ("AD_KEYFRAMES",),
142
+ }
143
+ }
144
+
145
+ RETURN_TYPES = ("M_MODELS",)
146
+ CATEGORY = "Animate Diff 🎭🅐🅓/② Gen2 nodes ②"
147
+ FUNCTION = "apply_motion_model"
148
+
149
+ def apply_motion_model(self,
150
+ motion_model: MotionModelPatcher, motion_lora: MotionLoraList=None,
151
+ scale_multival=None, effect_multival=None, ad_keyframes=None):
152
+ # just a subset of normal ApplyAnimateDiffModelNode inputs
153
+ return ApplyAnimateDiffModelNode.apply_motion_model(self, motion_model, motion_lora=motion_lora,
154
+ scale_multival=scale_multival, effect_multival=effect_multival,
155
+ ad_keyframes=ad_keyframes)
156
+
157
+
158
+ class LoadAnimateDiffModelNode:
159
+ @classmethod
160
+ def INPUT_TYPES(s):
161
+ return {
162
+ "required": {
163
+ "model_name": (get_available_motion_models(),),
164
+ },
165
+ "optional": {
166
+ "ad_settings": ("AD_SETTINGS",),
167
+ }
168
+ }
169
+
170
+ RETURN_TYPES = ("MOTION_MODEL_ADE",)
171
+ RETURN_NAMES = ("MOTION_MODEL",)
172
+ CATEGORY = "Animate Diff 🎭🅐🅓/② Gen2 nodes ②"
173
+ FUNCTION = "load_motion_model"
174
+
175
+ def load_motion_model(self, model_name: str, ad_settings: AnimateDiffSettings=None):
176
+ # load motion module and motion settings, if included
177
+ motion_model = load_motion_module_gen2(model_name=model_name, motion_model_settings=ad_settings)
178
+ return (motion_model,)
179
+
180
+
181
+ class ADKeyframeNode:
182
+ @classmethod
183
+ def INPUT_TYPES(s):
184
+ return {
185
+ "required": {
186
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}, ),
187
+ },
188
+ "optional": {
189
+ "prev_ad_keyframes": ("AD_KEYFRAMES", ),
190
+ "scale_multival": ("MULTIVAL",),
191
+ "effect_multival": ("MULTIVAL",),
192
+ "inherit_missing": ("BOOLEAN", {"default": True}, ),
193
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
194
+ }
195
+ }
196
+
197
+ RETURN_TYPES = ("AD_KEYFRAMES", )
198
+ FUNCTION = "load_keyframe"
199
+
200
+ CATEGORY = "Animate Diff 🎭🅐🅓"
201
+
202
+ def load_keyframe(self,
203
+ start_percent: float, prev_ad_keyframes=None,
204
+ scale_multival: [float, torch.Tensor]=None, effect_multival: [float, torch.Tensor]=None,
205
+ inherit_missing: bool=True, guarantee_steps: int=1):
206
+ if not prev_ad_keyframes:
207
+ prev_ad_keyframes = ADKeyframeGroup()
208
+ prev_ad_keyframes = prev_ad_keyframes.clone()
209
+ keyframe = ADKeyframe(start_percent=start_percent, scale_multival=scale_multival, effect_multival=effect_multival,
210
+ inherit_missing=inherit_missing, guarantee_steps=guarantee_steps)
211
+ prev_ad_keyframes.add(keyframe)
212
+ return (prev_ad_keyframes,)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_lora.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ import folder_paths
4
+ import comfy.utils
5
+ import comfy.sd
6
+
7
+ from .logger import logger
8
+ from .utils_model import get_available_motion_loras, get_motion_lora_path
9
+ from .motion_lora import MotionLoraInfo, MotionLoraList
10
+
11
+
12
+ class AnimateDiffLoraLoader:
13
+ @classmethod
14
+ def INPUT_TYPES(s):
15
+ return {
16
+ "required": {
17
+ "lora_name": (get_available_motion_loras(),),
18
+ "strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.001}),
19
+ },
20
+ "optional": {
21
+ "prev_motion_lora": ("MOTION_LORA",),
22
+ }
23
+ }
24
+
25
+ RETURN_TYPES = ("MOTION_LORA",)
26
+ CATEGORY = "Animate Diff 🎭🅐🅓"
27
+ FUNCTION = "load_motion_lora"
28
+
29
+ def load_motion_lora(self, lora_name: str, strength: float, prev_motion_lora: MotionLoraList=None):
30
+ if prev_motion_lora is None:
31
+ prev_motion_lora = MotionLoraList()
32
+ else:
33
+ prev_motion_lora = prev_motion_lora.clone()
34
+ # check if motion lora with name exists
35
+ lora_path = get_motion_lora_path(lora_name)
36
+ if not Path(lora_path).is_file():
37
+ raise FileNotFoundError(f"Motion lora with name '{lora_name}' not found.")
38
+ # create motion lora info to be loaded in AnimateDiff Loader
39
+ lora_info = MotionLoraInfo(name=lora_name, strength=strength)
40
+ prev_motion_lora.add_lora(lora_info)
41
+
42
+ return (prev_motion_lora,)
43
+
44
+
45
+ class MaskedLoraLoader:
46
+ def __init__(self):
47
+ self.loaded_lora = None
48
+
49
+ @classmethod
50
+ def INPUT_TYPES(s):
51
+ return {"required": { "model": ("MODEL",),
52
+ "clip": ("CLIP", ),
53
+ "lora_name": (folder_paths.get_filename_list("loras"), ),
54
+ "strength_model": ("FLOAT", {"default": 1.0, "min": -20.0, "max": 20.0, "step": 0.01}),
55
+ "strength_clip": ("FLOAT", {"default": 1.0, "min": -20.0, "max": 20.0, "step": 0.01}),
56
+ }}
57
+ #RETURN_TYPES = ()
58
+ RETURN_TYPES = ("MODEL", "CLIP")
59
+ FUNCTION = "load_lora"
60
+
61
+ CATEGORY = "loaders"
62
+
63
+ def load_lora(self, model, clip, lora_name, strength_model, strength_clip):
64
+ if strength_model == 0 and strength_clip == 0:
65
+ return (model, clip)
66
+
67
+ lora_path = folder_paths.get_full_path("loras", lora_name)
68
+ lora = None
69
+ if self.loaded_lora is not None:
70
+ if self.loaded_lora[0] == lora_path:
71
+ lora = self.loaded_lora[1]
72
+ else:
73
+ temp = self.loaded_lora
74
+ self.loaded_lora = None
75
+ del temp
76
+
77
+ if lora is None:
78
+ lora = comfy.utils.load_torch_file(lora_path, safe_load=True)
79
+ self.loaded_lora = (lora_path, lora)
80
+
81
+ from pathlib import Path
82
+ with open(Path(__file__).parent.parent.parent / "sd_lora_keys.txt", "w") as lfile:
83
+ for key in lora:
84
+ lfile.write(f"{key}:\t{lora[key].size()}\n")
85
+
86
+ #model_lora, clip_lora = comfy.sd.load_lora_for_models(model, clip, lora, strength_model, strength_clip)
87
+ #return (model_lora, clip_lora)
88
+ return (model, clip)
89
+
90
+
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_multival.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections.abc import Iterable
2
+ from typing import Union
3
+
4
+ import torch
5
+ from torch import Tensor
6
+
7
+ from .utils_motion import linear_conversion, normalize_min_max, extend_to_batch_size
8
+
9
+
10
+ class ScaleType:
11
+ ABSOLUTE = "absolute"
12
+ RELATIVE = "relative"
13
+ LIST = [ABSOLUTE, RELATIVE]
14
+
15
+
16
+ class MultivalDynamicNode:
17
+ @classmethod
18
+ def INPUT_TYPES(s):
19
+ return {
20
+ "required": {
21
+ "float_val": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001},),
22
+ },
23
+ "optional": {
24
+ "mask_optional": ("MASK",)
25
+ }
26
+ }
27
+
28
+ RETURN_TYPES = ("MULTIVAL",)
29
+ CATEGORY = "Animate Diff 🎭🅐🅓/multival"
30
+ FUNCTION = "create_multival"
31
+
32
+ def create_multival(self, float_val: Union[float, list[float]]=1.0, mask_optional: Tensor=None):
33
+ # first, normalize inputs
34
+ # if float_val is iterable, treat as a list and assume inputs are floats
35
+ float_is_iterable = False
36
+ if isinstance(float_val, Iterable):
37
+ float_is_iterable = True
38
+ float_val = list(float_val)
39
+ # if mask present, make sure float_val list can be applied to list - match lengths
40
+ if mask_optional is not None:
41
+ if len(float_val) < mask_optional.shape[0]:
42
+ # copies last entry enough times to match mask shape
43
+ float_val = float_val + float_val[-1]*(mask_optional.shape[0]-len(float_val))
44
+ if mask_optional.shape[0] < len(float_val):
45
+ mask_optional = extend_to_batch_size(mask_optional, len(float_val))
46
+ float_val = float_val[:mask_optional.shape[0]]
47
+ float_val: Tensor = torch.tensor(float_val).unsqueeze(-1).unsqueeze(-1)
48
+ # now that inputs are normalized, figure out what value to actually return
49
+ if mask_optional is not None:
50
+ mask_optional = mask_optional.clone()
51
+ if float_is_iterable:
52
+ mask_optional = mask_optional[:] * float_val.to(mask_optional.dtype).to(mask_optional.device)
53
+ else:
54
+ mask_optional = mask_optional * float_val
55
+ return (mask_optional,)
56
+ else:
57
+ if not float_is_iterable:
58
+ return (float_val,)
59
+ # create a dummy mask of b,h,w=float_len,1,1 (sigle pixel)
60
+ # purpose is for float input to work with mask code, without special cases
61
+ float_len = float_val.shape[0] if float_is_iterable else 1
62
+ shape = (float_len,1,1)
63
+ mask_optional = torch.ones(shape)
64
+ mask_optional = mask_optional[:] * float_val.to(mask_optional.dtype).to(mask_optional.device)
65
+ return (mask_optional,)
66
+
67
+
68
+ class MultivalScaledMaskNode:
69
+ @classmethod
70
+ def INPUT_TYPES(s):
71
+ return {
72
+ "required": {
73
+ "min_float_val": ("FLOAT", {"default": 0.0, "min": 0.0, "step": 0.001}),
74
+ "max_float_val": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
75
+ "mask": ("MASK",),
76
+ },
77
+ "optional": {
78
+ "scaling": (ScaleType.LIST,),
79
+ }
80
+ }
81
+
82
+ RETURN_TYPES = ("MULTIVAL",)
83
+ CATEGORY = "Animate Diff 🎭🅐🅓/multival"
84
+ FUNCTION = "create_multival"
85
+
86
+ def create_multival(self, min_float_val: float, max_float_val: float, mask: Tensor, scaling: str=ScaleType.ABSOLUTE):
87
+ # TODO: allow min_float_val and max_float_val to be list[float]
88
+ if isinstance(min_float_val, Iterable):
89
+ raise ValueError(f"min_float_val must be type float (no lists allowed here), not {type(min_float_val).__name__}.")
90
+ if isinstance(max_float_val, Iterable):
91
+ raise ValueError(f"max_float_val must be type float (no lists allowed here), not {type(max_float_val).__name__}.")
92
+
93
+ if scaling == ScaleType.ABSOLUTE:
94
+ mask = linear_conversion(mask.clone(), new_min=min_float_val, new_max=max_float_val)
95
+ elif scaling == ScaleType.RELATIVE:
96
+ mask = normalize_min_max(mask.clone(), new_min=min_float_val, new_max=max_float_val)
97
+ else:
98
+ raise ValueError(f"scaling '{scaling}' not recognized.")
99
+ return MultivalDynamicNode.create_multival(self, mask_optional=mask)
100
+
101
+
102
+ class MultivalDynamicFloatInputNode:
103
+ @classmethod
104
+ def INPUT_TYPES(s):
105
+ return {
106
+ "required": {
107
+ "float_val": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.001, "forceInput": True},),
108
+ },
109
+ "optional": {
110
+ "mask_optional": ("MASK",)
111
+ }
112
+ }
113
+
114
+ RETURN_TYPES = ("MULTIVAL",)
115
+ CATEGORY = "Animate Diff 🎭🅐🅓/multival"
116
+ FUNCTION = "create_multival"
117
+
118
+ def create_multival(self, float_val: Union[float, list[float]]=None, mask_optional: Tensor=None):
119
+ return MultivalDynamicNode.create_multival(self, float_val=float_val, mask_optional=mask_optional)
120
+
121
+
122
+ class MultivalFloatNode:
123
+ @classmethod
124
+ def INPUT_TYPES(s):
125
+ return {
126
+ "required": {
127
+ "float_val": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.001},),
128
+ },
129
+ }
130
+
131
+ RETURN_TYPES = ("MULTIVAL",)
132
+ CATEGORY = "Animate Diff 🎭🅐🅓/multival"
133
+ FUNCTION = "create_multival"
134
+
135
+ def create_multival(self, float_val: Union[float, list[float]]=None):
136
+ return MultivalDynamicNode.create_multival(self, float_val=float_val)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_sample.py ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Union
2
+ from torch import Tensor
3
+
4
+ from .freeinit import FreeInitFilter
5
+ from .sample_settings import (FreeInitOptions, IterationOptions,
6
+ NoiseLayerAdd, NoiseLayerAddWeighted, NoiseLayerGroup, NoiseLayerReplace, NoiseLayerType,
7
+ SeedNoiseGeneration, SampleSettings, CustomCFGKeyframeGroup, CustomCFGKeyframe)
8
+ from .utils_model import BIGMIN, BIGMAX, SigmaSchedule
9
+
10
+
11
+ class SampleSettingsNode:
12
+ @classmethod
13
+ def INPUT_TYPES(s):
14
+ return {
15
+ "required": {
16
+ "batch_offset": ("INT", {"default": 0, "min": 0, "max": BIGMAX}),
17
+ "noise_type": (NoiseLayerType.LIST,),
18
+ "seed_gen": (SeedNoiseGeneration.LIST,),
19
+ "seed_offset": ("INT", {"default": 0, "min": BIGMIN, "max": BIGMAX}),
20
+ },
21
+ "optional": {
22
+ "noise_layers": ("NOISE_LAYERS",),
23
+ "iteration_opts": ("ITERATION_OPTS",),
24
+ "seed_override": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "forceInput": True}),
25
+ "adapt_denoise_steps": ("BOOLEAN", {"default": False},),
26
+ "custom_cfg": ("CUSTOM_CFG",),
27
+ "sigma_schedule": ("SIGMA_SCHEDULE",),
28
+ }
29
+ }
30
+
31
+ RETURN_TYPES = ("SAMPLE_SETTINGS",)
32
+ RETURN_NAMES = ("settings",)
33
+ CATEGORY = "Animate Diff 🎭🅐🅓"
34
+ FUNCTION = "create_settings"
35
+
36
+ def create_settings(self, batch_offset: int, noise_type: str, seed_gen: str, seed_offset: int, noise_layers: NoiseLayerGroup=None,
37
+ iteration_opts: IterationOptions=None, seed_override: int=None, adapt_denoise_steps=False,
38
+ custom_cfg: CustomCFGKeyframeGroup=None, sigma_schedule: SigmaSchedule=None):
39
+ sampling_settings = SampleSettings(batch_offset=batch_offset, noise_type=noise_type, seed_gen=seed_gen, seed_offset=seed_offset, noise_layers=noise_layers,
40
+ iteration_opts=iteration_opts, seed_override=seed_override, adapt_denoise_steps=adapt_denoise_steps,
41
+ custom_cfg=custom_cfg, sigma_schedule=sigma_schedule)
42
+ return (sampling_settings,)
43
+
44
+
45
+ class NoiseLayerReplaceNode:
46
+ @classmethod
47
+ def INPUT_TYPES(s):
48
+ return {
49
+ "required": {
50
+ "batch_offset": ("INT", {"default": 0, "min": 0, "max": BIGMAX}),
51
+ "noise_type": (NoiseLayerType.LIST,),
52
+ "seed_gen_override": (SeedNoiseGeneration.LIST_WITH_OVERRIDE,),
53
+ "seed_offset": ("INT", {"default": 0, "min": BIGMIN, "max": BIGMAX}),
54
+ },
55
+ "optional": {
56
+ "prev_noise_layers": ("NOISE_LAYERS",),
57
+ "mask_optional": ("MASK",),
58
+ "seed_override": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "forceInput": True}),
59
+ }
60
+ }
61
+
62
+ RETURN_TYPES = ("NOISE_LAYERS",)
63
+ CATEGORY = "Animate Diff 🎭🅐🅓/noise layers"
64
+ FUNCTION = "create_layers"
65
+
66
+ def create_layers(self, batch_offset: int, noise_type: str, seed_gen_override: str, seed_offset: int,
67
+ prev_noise_layers: NoiseLayerGroup=None, mask_optional: Tensor=None, seed_override: int=None,):
68
+ # prepare prev_noise_layers
69
+ if prev_noise_layers is None:
70
+ prev_noise_layers = NoiseLayerGroup()
71
+ prev_noise_layers = prev_noise_layers.clone()
72
+ # create layer
73
+ layer = NoiseLayerReplace(noise_type=noise_type, batch_offset=batch_offset, seed_gen_override=seed_gen_override, seed_offset=seed_offset,
74
+ seed_override=seed_override, mask=mask_optional)
75
+ prev_noise_layers.add_to_start(layer)
76
+ return (prev_noise_layers,)
77
+
78
+
79
+ class NoiseLayerAddNode:
80
+ @classmethod
81
+ def INPUT_TYPES(s):
82
+ return {
83
+ "required": {
84
+ "batch_offset": ("INT", {"default": 0, "min": 0, "max": BIGMAX}),
85
+ "noise_type": (NoiseLayerType.LIST,),
86
+ "seed_gen_override": (SeedNoiseGeneration.LIST_WITH_OVERRIDE,),
87
+ "seed_offset": ("INT", {"default": 0, "min": BIGMIN, "max": BIGMAX}),
88
+ "noise_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 10.0, "step": 0.001}),
89
+ },
90
+ "optional": {
91
+ "prev_noise_layers": ("NOISE_LAYERS",),
92
+ "mask_optional": ("MASK",),
93
+ "seed_override": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "forceInput": True}),
94
+ }
95
+ }
96
+
97
+ RETURN_TYPES = ("NOISE_LAYERS",)
98
+ CATEGORY = "Animate Diff 🎭🅐🅓/noise layers"
99
+ FUNCTION = "create_layers"
100
+
101
+ def create_layers(self, batch_offset: int, noise_type: str, seed_gen_override: str, seed_offset: int,
102
+ noise_weight: float,
103
+ prev_noise_layers: NoiseLayerGroup=None, mask_optional: Tensor=None, seed_override: int=None,):
104
+ # prepare prev_noise_layers
105
+ if prev_noise_layers is None:
106
+ prev_noise_layers = NoiseLayerGroup()
107
+ prev_noise_layers = prev_noise_layers.clone()
108
+ # create layer
109
+ layer = NoiseLayerAdd(noise_type=noise_type, batch_offset=batch_offset, seed_gen_override=seed_gen_override, seed_offset=seed_offset,
110
+ seed_override=seed_override, mask=mask_optional,
111
+ noise_weight=noise_weight)
112
+ prev_noise_layers.add_to_start(layer)
113
+ return (prev_noise_layers,)
114
+
115
+
116
+ class NoiseLayerAddWeightedNode:
117
+ @classmethod
118
+ def INPUT_TYPES(s):
119
+ return {
120
+ "required": {
121
+ "batch_offset": ("INT", {"default": 0, "min": 0, "max": BIGMAX}),
122
+ "noise_type": (NoiseLayerType.LIST,),
123
+ "seed_gen_override": (SeedNoiseGeneration.LIST_WITH_OVERRIDE,),
124
+ "seed_offset": ("INT", {"default": 0, "min": BIGMIN, "max": BIGMAX}),
125
+ "noise_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 10.0, "step": 0.001}),
126
+ "balance_multiplier": ("FLOAT", {"default": 1.0, "min": 0.0, "step": 0.001}),
127
+ },
128
+ "optional": {
129
+ "prev_noise_layers": ("NOISE_LAYERS",),
130
+ "mask_optional": ("MASK",),
131
+ "seed_override": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "forceInput": True}),
132
+ }
133
+ }
134
+
135
+ RETURN_TYPES = ("NOISE_LAYERS",)
136
+ CATEGORY = "Animate Diff 🎭🅐🅓/noise layers"
137
+ FUNCTION = "create_layers"
138
+
139
+ def create_layers(self, batch_offset: int, noise_type: str, seed_gen_override: str, seed_offset: int,
140
+ noise_weight: float, balance_multiplier: float,
141
+ prev_noise_layers: NoiseLayerGroup=None, mask_optional: Tensor=None, seed_override: int=None,):
142
+ # prepare prev_noise_layers
143
+ if prev_noise_layers is None:
144
+ prev_noise_layers = NoiseLayerGroup()
145
+ prev_noise_layers = prev_noise_layers.clone()
146
+ # create layer
147
+ layer = NoiseLayerAddWeighted(noise_type=noise_type, batch_offset=batch_offset, seed_gen_override=seed_gen_override, seed_offset=seed_offset,
148
+ seed_override=seed_override, mask=mask_optional,
149
+ noise_weight=noise_weight, balance_multiplier=balance_multiplier)
150
+ prev_noise_layers.add_to_start(layer)
151
+ return (prev_noise_layers,)
152
+
153
+
154
+ class IterationOptionsNode:
155
+ @classmethod
156
+ def INPUT_TYPES(s):
157
+ return {
158
+ "required": {
159
+ "iterations": ("INT", {"default": 1, "min": 1}),
160
+ },
161
+ "optional": {
162
+ "iter_batch_offset": ("INT", {"default": 0, "min": 0, "max": BIGMAX}),
163
+ "iter_seed_offset": ("INT", {"default": 0, "min": BIGMIN, "max": BIGMAX}),
164
+ }
165
+ }
166
+
167
+ RETURN_TYPES = ("ITERATION_OPTS",)
168
+ CATEGORY = "Animate Diff 🎭🅐🅓/iteration opts"
169
+ FUNCTION = "create_iter_opts"
170
+
171
+ def create_iter_opts(self, iterations: int, iter_batch_offset: int=0, iter_seed_offset: int=0):
172
+ iter_opts = IterationOptions(iterations=iterations, iter_batch_offset=iter_batch_offset, iter_seed_offset=iter_seed_offset)
173
+ return (iter_opts,)
174
+
175
+
176
+ class FreeInitOptionsNode:
177
+ @classmethod
178
+ def INPUT_TYPES(s):
179
+ return {
180
+ "required": {
181
+ "iterations": ("INT", {"default": 2, "min": 1}),
182
+ "filter": (FreeInitFilter.LIST,),
183
+ "d_s": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.001}),
184
+ "d_t": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.001}),
185
+ "n_butterworth": ("INT", {"default": 4, "min": 1, "max": 100},),
186
+ "sigma_step": ("INT", {"default": 999, "min": 1, "max": 999}),
187
+ "apply_to_1st_iter": ("BOOLEAN", {"default": False}),
188
+ "init_type": (FreeInitOptions.LIST,)
189
+ },
190
+ "optional": {
191
+ "iter_batch_offset": ("INT", {"default": 0, "min": 0, "max": BIGMAX}),
192
+ "iter_seed_offset": ("INT", {"default": 1, "min": BIGMIN, "max": BIGMAX}),
193
+ }
194
+ }
195
+
196
+ RETURN_TYPES = ("ITERATION_OPTS",)
197
+ CATEGORY = "Animate Diff 🎭🅐🅓/iteration opts"
198
+ FUNCTION = "create_iter_opts"
199
+
200
+ def create_iter_opts(self, iterations: int, filter: str, d_s: float, d_t: float, n_butterworth: int,
201
+ sigma_step: int, apply_to_1st_iter: bool, init_type: str,
202
+ iter_batch_offset: int=0, iter_seed_offset: int=1):
203
+ # init_type does nothing for now, not until I add more methods of applying low+high freq noise
204
+ iter_opts = FreeInitOptions(iterations=iterations, step=sigma_step, apply_to_1st_iter=apply_to_1st_iter,
205
+ filter=filter, d_s=d_s, d_t=d_t, n=n_butterworth, init_type=init_type,
206
+ iter_batch_offset=iter_batch_offset, iter_seed_offset=iter_seed_offset)
207
+ return (iter_opts,)
208
+
209
+
210
+ class CustomCFGNode:
211
+ @classmethod
212
+ def INPUT_TYPES(s):
213
+ return {
214
+ "required": {
215
+ "cfg_multival": ("MULTIVAL",),
216
+ }
217
+ }
218
+
219
+ RETURN_TYPES = ("CUSTOM_CFG",)
220
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings"
221
+ FUNCTION = "create_custom_cfg"
222
+
223
+ def create_custom_cfg(self, cfg_multival: Union[float, Tensor]):
224
+ keyframe = CustomCFGKeyframe(cfg_multival=cfg_multival)
225
+ cfg_custom = CustomCFGKeyframeGroup()
226
+ cfg_custom.add(keyframe)
227
+ return (cfg_custom,)
228
+
229
+
230
+ class CustomCFGKeyframeNode:
231
+ @classmethod
232
+ def INPUT_TYPES(s):
233
+ return {
234
+ "required": {
235
+ "cfg_multival": ("MULTIVAL",),
236
+ "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
237
+ "guarantee_steps": ("INT", {"default": 1, "min": 0, "max": BIGMAX}),
238
+ },
239
+ "optional": {
240
+ "prev_custom_cfg": ("CUSTOM_CFG",),
241
+ }
242
+ }
243
+
244
+ RETURN_TYPES = ("CUSTOM_CFG",)
245
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings"
246
+ FUNCTION = "create_custom_cfg"
247
+
248
+ def create_custom_cfg(self, cfg_multival: Union[float, Tensor], start_percent: float=0.0, guarantee_steps: int=1,
249
+ prev_custom_cfg: CustomCFGKeyframeGroup=None):
250
+ if not prev_custom_cfg:
251
+ prev_custom_cfg = CustomCFGKeyframeGroup()
252
+ prev_custom_cfg = prev_custom_cfg.clone()
253
+ keyframe = CustomCFGKeyframe(cfg_multival=cfg_multival, start_percent=start_percent, guarantee_steps=guarantee_steps)
254
+ prev_custom_cfg.add(keyframe)
255
+ return (prev_custom_cfg,)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/nodes_sigma_schedule.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from .utils_model import BetaSchedules, SigmaSchedule, ModelSamplingType, ModelSamplingConfig, InterpolationMethod
4
+
5
+
6
+ def validate_sigma_schedule_compatibility(schedule_A: SigmaSchedule, schedule_B: SigmaSchedule,
7
+ name_a: str="sigma_schedule_A", name_b: str="sigma_schedule_B"):
8
+ if schedule_A.total_sigmas() != schedule_B.total_sigmas():
9
+ raise Exception(f"Weighted Average cannot be taken of Sigma Schedules that do not have the same amount of sigmas; " +
10
+ f"{name_a} has {schedule_A.total_sigmas()} sigmas (lcm={schedule_A.is_lcm()}), " +
11
+ f"{name_b} has {schedule_B.total_sigmas()} sigmas (lcm={schedule_B.is_lcm()}).")
12
+
13
+
14
+ class SigmaScheduleNode:
15
+ @classmethod
16
+ def INPUT_TYPES(s):
17
+ return {
18
+ "required": {
19
+ "beta_schedule": (BetaSchedules.ALIAS_ACTIVE_LIST,),
20
+ }
21
+ }
22
+
23
+ RETURN_TYPES = ("SIGMA_SCHEDULE",)
24
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings/sigma schedule"
25
+ FUNCTION = "get_sigma_schedule"
26
+
27
+ def get_sigma_schedule(self, beta_schedule: str):
28
+ model_type = ModelSamplingType.from_alias(ModelSamplingType.EPS)
29
+ new_model_sampling = BetaSchedules._to_model_sampling(alias=beta_schedule,
30
+ model_type=model_type)
31
+ return (SigmaSchedule(model_sampling=new_model_sampling, model_type=model_type),)
32
+
33
+
34
+ class RawSigmaScheduleNode:
35
+ @classmethod
36
+ def INPUT_TYPES(s):
37
+ return {
38
+ "required": {
39
+ "raw_beta_schedule": (BetaSchedules.RAW_BETA_SCHEDULE_LIST,),
40
+ "linear_start": ("FLOAT", {"default": 0.00085, "min": 0.0, "max": 1.0, "step": 0.000001}),
41
+ "linear_end": ("FLOAT", {"default": 0.012, "min": 0.0, "max": 1.0, "step": 0.000001}),
42
+ #"cosine_s": ("FLOAT", {"default": 8e-3, "min": 0.0, "max": 1.0, "step": 0.000001}),
43
+ "sampling": (ModelSamplingType._FULL_LIST,),
44
+ "lcm_original_timesteps": ("INT", {"default": 50, "min": 1, "max": 1000}),
45
+ "lcm_zsnr": ("BOOLEAN", {"default": False}),
46
+ }
47
+ }
48
+
49
+ RETURN_TYPES = ("SIGMA_SCHEDULE",)
50
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings/sigma schedule"
51
+ FUNCTION = "get_sigma_schedule"
52
+
53
+ def get_sigma_schedule(self, raw_beta_schedule: str, linear_start: float, linear_end: float,# cosine_s: float,
54
+ sampling: str, lcm_original_timesteps: int, lcm_zsnr: bool):
55
+ new_config = ModelSamplingConfig(beta_schedule=raw_beta_schedule, linear_start=linear_start, linear_end=linear_end)
56
+ if sampling != ModelSamplingType.LCM:
57
+ lcm_original_timesteps=None
58
+ lcm_zsnr=False
59
+ model_type = ModelSamplingType.from_alias(sampling)
60
+ new_model_sampling = BetaSchedules._to_model_sampling(alias=BetaSchedules.AUTOSELECT, model_type=model_type, config_override=new_config, original_timesteps=lcm_original_timesteps)
61
+ if lcm_zsnr:
62
+ SigmaSchedule.apply_zsnr(new_model_sampling=new_model_sampling)
63
+ return (SigmaSchedule(model_sampling=new_model_sampling, model_type=model_type),)
64
+
65
+
66
+ class WeightedAverageSigmaScheduleNode:
67
+ @classmethod
68
+ def INPUT_TYPES(s):
69
+ return {
70
+ "required": {
71
+ "schedule_A": ("SIGMA_SCHEDULE",),
72
+ "schedule_B": ("SIGMA_SCHEDULE",),
73
+ "weight_A": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.001}),
74
+ }
75
+ }
76
+
77
+ RETURN_TYPES = ("SIGMA_SCHEDULE",)
78
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings/sigma schedule"
79
+ FUNCTION = "get_sigma_schedule"
80
+
81
+ def get_sigma_schedule(self, schedule_A: SigmaSchedule, schedule_B: SigmaSchedule, weight_A: float):
82
+ validate_sigma_schedule_compatibility(schedule_A, schedule_B)
83
+ new_sigmas = schedule_A.model_sampling.sigmas * weight_A + schedule_B.model_sampling.sigmas * (1-weight_A)
84
+ combo_schedule = schedule_A.clone()
85
+ combo_schedule.model_sampling.set_sigmas(new_sigmas)
86
+ return (combo_schedule,)
87
+
88
+
89
+ class InterpolatedWeightedAverageSigmaScheduleNode:
90
+ @classmethod
91
+ def INPUT_TYPES(s):
92
+ return {
93
+ "required": {
94
+ "schedule_A": ("SIGMA_SCHEDULE",),
95
+ "schedule_B": ("SIGMA_SCHEDULE",),
96
+ "weight_A_Start": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.001}),
97
+ "weight_A_End": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.001}),
98
+ "interpolation": (InterpolationMethod._LIST,),
99
+ }
100
+ }
101
+
102
+ RETURN_TYPES = ("SIGMA_SCHEDULE",)
103
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings/sigma schedule"
104
+ FUNCTION = "get_sigma_schedule"
105
+
106
+ def get_sigma_schedule(self, schedule_A: SigmaSchedule, schedule_B: SigmaSchedule,
107
+ weight_A_Start: float, weight_A_End: float, interpolation: str):
108
+ validate_sigma_schedule_compatibility(schedule_A, schedule_B)
109
+ # get reverse weights, since sigmas are currently reversed
110
+ weights = InterpolationMethod.get_weights(num_from=weight_A_Start, num_to=weight_A_End,
111
+ length=schedule_A.total_sigmas(), method=interpolation, reverse=True)
112
+ weights = weights.to(schedule_A.model_sampling.sigmas.dtype).to(schedule_A.model_sampling.sigmas.device)
113
+ new_sigmas = schedule_A.model_sampling.sigmas * weights + schedule_B.model_sampling.sigmas * (1.0-weights)
114
+ combo_schedule = schedule_A.clone()
115
+ combo_schedule.model_sampling.set_sigmas(new_sigmas)
116
+ return (combo_schedule,)
117
+
118
+
119
+ class SplitAndCombineSigmaScheduleNode:
120
+ @classmethod
121
+ def INPUT_TYPES(s):
122
+ return {
123
+ "required": {
124
+ "schedule_Start": ("SIGMA_SCHEDULE",),
125
+ "schedule_End": ("SIGMA_SCHEDULE",),
126
+ "idx_split_percent": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.001})
127
+ }
128
+ }
129
+
130
+ RETURN_TYPES = ("SIGMA_SCHEDULE",)
131
+ CATEGORY = "Animate Diff 🎭🅐🅓/sample settings/sigma schedule"
132
+ FUNCTION = "get_sigma_schedule"
133
+
134
+ def get_sigma_schedule(self, schedule_Start: SigmaSchedule, schedule_End: SigmaSchedule, idx_split_percent: float):
135
+ validate_sigma_schedule_compatibility(schedule_Start, schedule_End)
136
+ # first, calculate index to act as split; get diff from 1.0 since sigmas are flipped at this stage
137
+ idx = int((1.0-idx_split_percent) * schedule_Start.total_sigmas())
138
+ new_sigmas = torch.cat([schedule_End.model_sampling.sigmas[:idx], schedule_Start.model_sampling.sigmas[idx:]], dim=0)
139
+ new_schedule = schedule_Start.clone()
140
+ new_schedule.model_sampling.set_sigmas(new_sigmas)
141
+ return (new_schedule,)
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/sample_settings.py ADDED
@@ -0,0 +1,555 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections.abc import Iterable
2
+ from typing import Union
3
+ import torch
4
+ from torch import Tensor
5
+
6
+ import comfy.sample
7
+ import comfy.samplers
8
+ from comfy.model_patcher import ModelPatcher
9
+ from comfy.model_base import BaseModel
10
+
11
+ from . import freeinit
12
+ from .context import ContextOptions, ContextOptionsGroup
13
+ from .utils_model import SigmaSchedule
14
+ from .utils_motion import extend_to_batch_size, get_sorted_list_via_attr, prepare_mask_batch
15
+ from .logger import logger
16
+
17
+
18
+ def prepare_mask_ad(noise_mask, shape, device):
19
+ """ensures noise mask is of proper dimensions"""
20
+ noise_mask = torch.nn.functional.interpolate(noise_mask.reshape((-1, 1, noise_mask.shape[-2], noise_mask.shape[-1])), size=(shape[2], shape[3]), mode="bilinear")
21
+ #noise_mask = noise_mask.round()
22
+ noise_mask = torch.cat([noise_mask] * shape[1], dim=1)
23
+ noise_mask = comfy.utils.repeat_to_batch_size(noise_mask, shape[0])
24
+ noise_mask = noise_mask.to(device)
25
+ return noise_mask
26
+
27
+
28
+ class NoiseLayerType:
29
+ DEFAULT = "default"
30
+ CONSTANT = "constant"
31
+ EMPTY = "empty"
32
+ REPEATED_CONTEXT = "repeated_context"
33
+ FREENOISE = "FreeNoise"
34
+
35
+ LIST = [DEFAULT, CONSTANT, EMPTY, REPEATED_CONTEXT, FREENOISE]
36
+
37
+
38
+ class NoiseApplication:
39
+ ADD = "add"
40
+ ADD_WEIGHTED = "add_weighted"
41
+ REPLACE = "replace"
42
+
43
+ LIST = [ADD, ADD_WEIGHTED, REPLACE]
44
+
45
+
46
+ class NoiseNormalize:
47
+ DISABLE = "disable"
48
+ NORMAL = "normal"
49
+
50
+ LIST = [DISABLE, NORMAL]
51
+
52
+
53
+ class SampleSettings:
54
+ def __init__(self, batch_offset: int=0, noise_type: str=None, seed_gen: str=None, seed_offset: int=0, noise_layers: 'NoiseLayerGroup'=None,
55
+ iteration_opts=None, seed_override:int=None, negative_cond_flipflop=False, adapt_denoise_steps: bool=False,
56
+ custom_cfg: 'CustomCFGKeyframeGroup'=None, sigma_schedule: SigmaSchedule=None):
57
+ self.batch_offset = batch_offset
58
+ self.noise_type = noise_type if noise_type is not None else NoiseLayerType.DEFAULT
59
+ self.seed_gen = seed_gen if seed_gen is not None else SeedNoiseGeneration.COMFY
60
+ self.noise_layers = noise_layers if noise_layers else NoiseLayerGroup()
61
+ self.iteration_opts = iteration_opts if iteration_opts else IterationOptions()
62
+ self.seed_offset = seed_offset
63
+ self.seed_override = seed_override
64
+ self.negative_cond_flipflop = negative_cond_flipflop
65
+ self.adapt_denoise_steps = adapt_denoise_steps
66
+ self.custom_cfg = custom_cfg.clone() if custom_cfg else custom_cfg
67
+ self.sigma_schedule = sigma_schedule
68
+
69
+ def prepare_noise(self, seed: int, latents: Tensor, noise: Tensor, extra_seed_offset=0, extra_args:dict={}, force_create_noise=True):
70
+ if self.seed_override is not None:
71
+ seed = self.seed_override
72
+ # if seed is iterable, attempt to do per-latent noises
73
+ if isinstance(seed, Iterable):
74
+ noise = SeedNoiseGeneration.create_noise_individual_seeds(seeds=seed, latents=latents, seed_offset=self.seed_offset+extra_seed_offset, extra_args=extra_args)
75
+ seed = seed[0]+self.seed_offset
76
+ else:
77
+ seed += self.seed_offset
78
+ # replace initial noise if not batch_offset 0 or Comfy seed_gen or not NoiseType default
79
+ if self.batch_offset != 0 or self.seed_offset != 0 or self.noise_type != NoiseLayerType.DEFAULT or self.seed_gen != SeedNoiseGeneration.COMFY or force_create_noise:
80
+ noise = SeedNoiseGeneration.create_noise(seed=seed+extra_seed_offset, latents=latents, existing_seed_gen=self.seed_gen, seed_gen=self.seed_gen,
81
+ noise_type=self.noise_type, batch_offset=self.batch_offset, extra_args=extra_args)
82
+ # apply noise layers
83
+ for noise_layer in self.noise_layers.layers:
84
+ # first, generate new noise matching seed gen override
85
+ layer_noise = noise_layer.create_layer_noise(existing_seed_gen=self.seed_gen, seed=seed, latents=latents,
86
+ extra_seed_offset=extra_seed_offset, extra_args=extra_args)
87
+ # next, get noise after applying layer
88
+ noise = noise_layer.apply_layer_noise(new_noise=layer_noise, old_noise=noise)
89
+ # noise prepared now
90
+ return noise
91
+
92
+ def pre_run(self, model: ModelPatcher):
93
+ if self.custom_cfg is not None:
94
+ self.custom_cfg.reset()
95
+
96
+ def cleanup(self):
97
+ if self.custom_cfg is not None:
98
+ self.custom_cfg.reset()
99
+
100
+ def clone(self):
101
+ return SampleSettings(batch_offset=self.batch_offset, noise_type=self.noise_type, seed_gen=self.seed_gen, seed_offset=self.seed_offset,
102
+ noise_layers=self.noise_layers.clone(), iteration_opts=self.iteration_opts, seed_override=self.seed_override,
103
+ negative_cond_flipflop=self.negative_cond_flipflop, adapt_denoise_steps=self.adapt_denoise_steps, custom_cfg=self.custom_cfg, sigma_schedule=self.sigma_schedule)
104
+
105
+
106
+ class NoiseLayer:
107
+ def __init__(self, noise_type: str, batch_offset: int, seed_gen_override: str, seed_offset: int, seed_override: int=None, mask: Tensor=None):
108
+ self.application: str = NoiseApplication.REPLACE
109
+ self.noise_type = noise_type
110
+ self.batch_offset = batch_offset
111
+ self.seed_gen_override = seed_gen_override
112
+ self.seed_offset = seed_offset
113
+ self.seed_override = seed_override
114
+ self.mask = mask
115
+
116
+ def create_layer_noise(self, existing_seed_gen: str, seed: int, latents: Tensor, extra_seed_offset=0, extra_args:dict={}) -> Tensor:
117
+ if self.seed_override is not None:
118
+ seed = self.seed_override
119
+ # if seed is iterable, attempt to do per-latent noises
120
+ if isinstance(seed, Iterable):
121
+ return SeedNoiseGeneration.create_noise_individual_seeds(seeds=seed, latents=latents, seed_offset=self.seed_offset+extra_seed_offset, extra_args=extra_args)
122
+ seed += self.seed_offset + extra_seed_offset
123
+ return SeedNoiseGeneration.create_noise(seed=seed, latents=latents, existing_seed_gen=existing_seed_gen, seed_gen=self.seed_gen_override,
124
+ noise_type=self.noise_type, batch_offset=self.batch_offset, extra_args=extra_args)
125
+
126
+ def apply_layer_noise(self, new_noise: Tensor, old_noise: Tensor) -> Tensor:
127
+ return old_noise
128
+
129
+ def get_noise_mask(self, noise: Tensor) -> Tensor:
130
+ if self.mask is None:
131
+ return 1
132
+ noise_mask = self.mask.reshape((-1, 1, self.mask.shape[-2], self.mask.shape[-1]))
133
+ return prepare_mask_ad(noise_mask, noise.shape, noise.device)
134
+
135
+
136
+ class NoiseLayerReplace(NoiseLayer):
137
+ def __init__(self, noise_type: str, batch_offset: int, seed_gen_override: str, seed_offset: int, seed_override: int=None, mask: Tensor=None):
138
+ super().__init__(noise_type, batch_offset, seed_gen_override, seed_offset, seed_override, mask)
139
+ self.application = NoiseApplication.REPLACE
140
+
141
+ def apply_layer_noise(self, new_noise: Tensor, old_noise: Tensor) -> Tensor:
142
+ noise_mask = self.get_noise_mask(old_noise)
143
+ return (1-noise_mask)*old_noise + noise_mask*new_noise
144
+
145
+
146
+ class NoiseLayerAdd(NoiseLayer):
147
+ def __init__(self, noise_type: str, batch_offset: int, seed_gen_override: str, seed_offset: int, seed_override: int=None, mask: Tensor=None,
148
+ noise_weight=1.0):
149
+ super().__init__(noise_type, batch_offset, seed_gen_override, seed_offset, seed_override, mask)
150
+ self.noise_weight = noise_weight
151
+ self.application = NoiseApplication.ADD
152
+
153
+ def apply_layer_noise(self, new_noise: Tensor, old_noise: Tensor) -> Tensor:
154
+ noise_mask = self.get_noise_mask(old_noise)
155
+ return (1-noise_mask)*old_noise + noise_mask*(old_noise + new_noise * self.noise_weight)
156
+
157
+
158
+ class NoiseLayerAddWeighted(NoiseLayerAdd):
159
+ def __init__(self, noise_type: str, batch_offset: int, seed_gen_override: str, seed_offset: int, seed_override: int=None, mask: Tensor=None,
160
+ noise_weight=1.0, balance_multiplier=1.0):
161
+ super().__init__(noise_type, batch_offset, seed_gen_override, seed_offset, seed_override, mask, noise_weight)
162
+ self.balance_multiplier = balance_multiplier
163
+ self.application = NoiseApplication.ADD_WEIGHTED
164
+
165
+ def apply_layer_noise(self, new_noise: Tensor, old_noise: Tensor) -> Tensor:
166
+ noise_mask = self.get_noise_mask(old_noise)
167
+ return (1-noise_mask)*old_noise + noise_mask*(old_noise * (1.0-(self.noise_weight*self.balance_multiplier)) + new_noise * self.noise_weight)
168
+
169
+
170
+ class NoiseLayerGroup:
171
+ def __init__(self):
172
+ self.layers: list[NoiseLayer] = []
173
+
174
+ def add(self, layer: NoiseLayer) -> None:
175
+ # add to the end of list
176
+ self.layers.append(layer)
177
+
178
+ def add_to_start(self, layer: NoiseLayer) -> None:
179
+ # add to the beginning of list
180
+ self.layers.insert(0, layer)
181
+
182
+ def __getitem__(self, index) -> NoiseLayer:
183
+ return self.layers[index]
184
+
185
+ def is_empty(self) -> bool:
186
+ return len(self.layers) == 0
187
+
188
+ def clone(self) -> 'NoiseLayerGroup':
189
+ cloned = NoiseLayerGroup()
190
+ for layer in self.layers:
191
+ cloned.add(layer)
192
+ return cloned
193
+
194
+ class SeedNoiseGeneration:
195
+ COMFY = "comfy"
196
+ AUTO1111 = "auto1111"
197
+ AUTO1111GPU = "auto1111 [gpu]" # TODO: implement this
198
+ USE_EXISTING = "use existing"
199
+
200
+ LIST = [COMFY, AUTO1111]
201
+ LIST_WITH_OVERRIDE = [USE_EXISTING, COMFY, AUTO1111]
202
+
203
+ @classmethod
204
+ def create_noise(cls, seed: int, latents: Tensor, existing_seed_gen: str=COMFY, seed_gen: str=USE_EXISTING, noise_type: str=NoiseLayerType.DEFAULT, batch_offset: int=0, extra_args: dict={}):
205
+ # determine if should use existing type
206
+ if seed_gen == cls.USE_EXISTING:
207
+ seed_gen = existing_seed_gen
208
+ if seed_gen == cls.COMFY:
209
+ return cls.create_noise_comfy(seed, latents, noise_type, batch_offset, extra_args)
210
+ elif seed_gen in [cls.AUTO1111, cls.AUTO1111GPU]:
211
+ return cls.create_noise_auto1111(seed, latents, noise_type, batch_offset, extra_args)
212
+ raise ValueError(f"Noise seed_gen {seed_gen} is not recognized.")
213
+
214
+ @staticmethod
215
+ def create_noise_comfy(seed: int, latents: Tensor, noise_type: str=NoiseLayerType.DEFAULT, batch_offset: int=0, extra_args: dict={}):
216
+ common_noise = SeedNoiseGeneration._create_common_noise(seed, latents, noise_type, batch_offset, extra_args)
217
+ if common_noise is not None:
218
+ return common_noise
219
+ if noise_type == NoiseLayerType.CONSTANT:
220
+ generator = torch.manual_seed(seed)
221
+ length = latents.shape[0]
222
+ single_shape = (1 + batch_offset, latents.shape[1], latents.shape[2], latents.shape[3])
223
+ single_noise = torch.randn(single_shape, dtype=latents.dtype, layout=latents.layout, generator=generator, device="cpu")
224
+ return torch.cat([single_noise[batch_offset:]] * length, dim=0)
225
+ # comfy creates noise with a single seed for the entire shape of the latents batched tensor
226
+ generator = torch.manual_seed(seed)
227
+ offset_shape = (latents.shape[0] + batch_offset, latents.shape[1], latents.shape[2], latents.shape[3])
228
+ final_noise = torch.randn(offset_shape, dtype=latents.dtype, layout=latents.layout, generator=generator, device="cpu")
229
+ final_noise = final_noise[batch_offset:]
230
+ # convert to derivative noise type, if needed
231
+ derivative_noise = SeedNoiseGeneration._create_derivative_noise(final_noise, noise_type=noise_type, seed=seed, extra_args=extra_args)
232
+ if derivative_noise is not None:
233
+ return derivative_noise
234
+ return final_noise
235
+
236
+ @staticmethod
237
+ def create_noise_auto1111(seed: int, latents: Tensor, noise_type: str=NoiseLayerType.DEFAULT, batch_offset: int=0, extra_args: dict={}):
238
+ common_noise = SeedNoiseGeneration._create_common_noise(seed, latents, noise_type, batch_offset, extra_args)
239
+ if common_noise is not None:
240
+ return common_noise
241
+ if noise_type == NoiseLayerType.CONSTANT:
242
+ generator = torch.manual_seed(seed+batch_offset)
243
+ length = latents.shape[0]
244
+ single_shape = (1, latents.shape[1], latents.shape[2], latents.shape[3])
245
+ single_noise = torch.randn(single_shape, dtype=latents.dtype, layout=latents.layout, generator=generator, device="cpu")
246
+ return torch.cat([single_noise] * length, dim=0)
247
+ # auto1111 applies growing seeds for a batch
248
+ length = latents.shape[0]
249
+ single_shape = (1, latents.shape[1], latents.shape[2], latents.shape[3])
250
+ all_noises = []
251
+ # i starts at 0
252
+ for i in range(length):
253
+ generator = torch.manual_seed(seed+i+batch_offset)
254
+ all_noises.append(torch.randn(single_shape, dtype=latents.dtype, layout=latents.layout, generator=generator, device="cpu"))
255
+ final_noise = torch.cat(all_noises, dim=0)
256
+ # convert to derivative noise type, if needed
257
+ derivative_noise = SeedNoiseGeneration._create_derivative_noise(final_noise, noise_type=noise_type, seed=seed, extra_args=extra_args)
258
+ if derivative_noise is not None:
259
+ return derivative_noise
260
+ return final_noise
261
+
262
+ @staticmethod
263
+ def create_noise_individual_seeds(seeds: list[int], latents: Tensor, seed_offset: int=0, extra_args: dict={}):
264
+ length = latents.shape[0]
265
+ if len(seeds) < length:
266
+ raise ValueError(f"{len(seeds)} seeds in seed_override were provided, but at least {length} are required to work with the current latents.")
267
+ seeds = seeds[:length]
268
+ single_shape = (1, latents.shape[1], latents.shape[2], latents.shape[3])
269
+ all_noises = []
270
+ for seed in seeds:
271
+ generator = torch.manual_seed(seed+seed_offset)
272
+ all_noises.append(torch.randn(single_shape, dtype=latents.dtype, layout=latents.layout, generator=generator, device="cpu"))
273
+ return torch.cat(all_noises, dim=0)
274
+
275
+ @staticmethod
276
+ def _create_common_noise(seed: int, latents: Tensor, noise_type: str=NoiseLayerType.DEFAULT, batch_offset: int=0, extra_args: dict={}):
277
+ if noise_type == NoiseLayerType.EMPTY:
278
+ return torch.zeros_like(latents)
279
+ return None
280
+
281
+ @staticmethod
282
+ def _create_derivative_noise(noise: Tensor, noise_type: str, seed: int, extra_args: dict):
283
+ derivative_func = DERIVATIVE_NOISE_FUNC_MAP.get(noise_type, None)
284
+ if derivative_func is None:
285
+ return None
286
+ return derivative_func(noise=noise, seed=seed, extra_args=extra_args)
287
+
288
+ @staticmethod
289
+ def _convert_to_repeated_context(noise: Tensor, extra_args: dict, **kwargs):
290
+ # if no context_length, return unmodified noise
291
+ opts: ContextOptionsGroup = extra_args["context_options"]
292
+ context_length: int = opts.context_length if not opts.view_options else opts.view_options.context_length
293
+ if context_length is None:
294
+ return noise
295
+ length = noise.shape[0]
296
+ noise = noise[:context_length]
297
+ cat_count = (length // context_length) + 1
298
+ return torch.cat([noise] * cat_count, dim=0)[:length]
299
+
300
+ @staticmethod
301
+ def _convert_to_freenoise(noise: Tensor, seed: int, extra_args: dict, **kwargs):
302
+ # if no context_length, return unmodified noise
303
+ opts: ContextOptionsGroup = extra_args["context_options"]
304
+ context_length: int = opts.context_length if not opts.view_options else opts.view_options.context_length
305
+ context_overlap: int = opts.context_overlap if not opts.view_options else opts.view_options.context_overlap
306
+ video_length: int = noise.shape[0]
307
+ if context_length is None:
308
+ return noise
309
+ delta = context_length - context_overlap
310
+ generator = torch.manual_seed(seed)
311
+
312
+ for start_idx in range(0, video_length-context_length, delta):
313
+ # start_idx corresponds to the beginning of a context window
314
+ # goal: place shuffled in the delta region right after the end of the context window
315
+ # if space after context window is not enough to place the noise, adjust and finish
316
+ place_idx = start_idx + context_length
317
+ # if place_idx is outside the valid indexes, we are already finished
318
+ if place_idx >= video_length:
319
+ break
320
+ end_idx = place_idx - 1
321
+ # if there is not enough room to copy delta amount of indexes, copy limited amount and finish
322
+ if end_idx + delta >= video_length:
323
+ final_delta = video_length - place_idx
324
+ # generate list of indexes in final delta region
325
+ list_idx = torch.Tensor(list(range(start_idx,start_idx+final_delta))).to(torch.long)
326
+ # shuffle list
327
+ list_idx = list_idx[torch.randperm(final_delta, generator=generator)]
328
+ # apply shuffled indexes
329
+ noise[place_idx:place_idx+final_delta] = noise[list_idx]
330
+ break
331
+ # otherwise, do normal behavior
332
+ # generate list of indexes in delta region
333
+ list_idx = torch.Tensor(list(range(start_idx,start_idx+delta))).to(torch.long)
334
+ # shuffle list
335
+ list_idx = list_idx[torch.randperm(delta, generator=generator)]
336
+ # apply shuffled indexes
337
+ noise[place_idx:place_idx+delta] = noise[list_idx]
338
+ return noise
339
+
340
+
341
+ DERIVATIVE_NOISE_FUNC_MAP = {
342
+ NoiseLayerType.REPEATED_CONTEXT: SeedNoiseGeneration._convert_to_repeated_context,
343
+ NoiseLayerType.FREENOISE: SeedNoiseGeneration._convert_to_freenoise,
344
+ }
345
+
346
+
347
+ class IterationOptions:
348
+ SAMPLER = "sampler"
349
+
350
+ def __init__(self, iterations: int=1, cache_init_noise=False, cache_init_latents=False,
351
+ iter_batch_offset: int=0, iter_seed_offset: int=0):
352
+ self.iterations = iterations
353
+ self.cache_init_noise = cache_init_noise
354
+ self.cache_init_latents = cache_init_latents
355
+ self.iter_batch_offset = iter_batch_offset
356
+ self.iter_seed_offset = iter_seed_offset
357
+ self.need_sampler = False
358
+
359
+ def get_sigma(self, model: ModelPatcher, step: int):
360
+ model_sampling = model.model.model_sampling
361
+ if "model_sampling" in model.object_patches:
362
+ model_sampling = model.object_patches["model_sampling"]
363
+ return model_sampling.sigmas[step]
364
+
365
+ def initialize(self, latents: Tensor):
366
+ pass
367
+
368
+ def preprocess_latents(self, curr_i: int, model: ModelPatcher, latents: Tensor, noise: Tensor,
369
+ seed: int, sample_settings: SampleSettings, noise_extra_args: dict, **kwargs):
370
+ if curr_i == 0 or (self.iter_batch_offset == 0 and self.iter_seed_offset == 0):
371
+ return latents, noise
372
+ temp_sample_settings = sample_settings.clone()
373
+ temp_sample_settings.batch_offset += self.iter_batch_offset * curr_i
374
+ temp_sample_settings.seed_offset += self.iter_seed_offset * curr_i
375
+ return latents, temp_sample_settings.prepare_noise(seed=seed, latents=latents, noise=None,
376
+ extra_args=noise_extra_args, force_create_noise=True)
377
+
378
+
379
+ class FreeInitOptions(IterationOptions):
380
+ FREEINIT_SAMPLER = "FreeInit [sampler sigma]"
381
+ FREEINIT_MODEL = "FreeInit [model sigma]"
382
+ DINKINIT_V1 = "DinkInit_v1"
383
+
384
+ LIST = [FREEINIT_SAMPLER, FREEINIT_MODEL, DINKINIT_V1]
385
+
386
+ def __init__(self, iterations: int, step: int=999, apply_to_1st_iter: bool=False,
387
+ filter=freeinit.FreeInitFilter.GAUSSIAN, d_s=0.25, d_t=0.25, n=4, init_type=FREEINIT_SAMPLER,
388
+ iter_batch_offset: int=0, iter_seed_offset: int=1):
389
+ super().__init__(iterations=iterations, cache_init_noise=True, cache_init_latents=True,
390
+ iter_batch_offset=iter_batch_offset, iter_seed_offset=iter_seed_offset)
391
+ self.apply_to_1st_iter = apply_to_1st_iter
392
+ self.step = step
393
+ self.filter = filter
394
+ self.d_s = d_s
395
+ self.d_t = d_t
396
+ self.n = n
397
+ self.freq_filter = None
398
+ self.freq_filter2 = None
399
+ self.need_sampler = True if init_type in [self.FREEINIT_SAMPLER] else False
400
+ self.init_type = init_type
401
+
402
+ def initialize(self, latents: Tensor):
403
+ self.freq_filter = freeinit.get_freq_filter(latents.shape, device=latents.device, filter_type=self.filter,
404
+ n=self.n, d_s=self.d_s, d_t=self.d_t)
405
+
406
+ def preprocess_latents(self, curr_i: int, model: ModelPatcher, latents: Tensor, noise: Tensor, cached_latents: Tensor, cached_noise: Tensor,
407
+ seed:int, sample_settings: SampleSettings, noise_extra_args: dict, sampler: comfy.samplers.KSampler=None, **kwargs):
408
+ # if first iter and should not apply, do nothing
409
+ if curr_i == 0 and not self.apply_to_1st_iter:
410
+ return latents, noise
411
+ # otherwise, do FreeInit stuff
412
+ if self.init_type in [self.FREEINIT_SAMPLER, self.FREEINIT_MODEL]:
413
+ # NOTE: This should be very close (if not exactly) to how FreeInit is intended to initialize noise the latents.
414
+ # The trick is that FreeInit is dependent on the behavior of diffuser's DDIMScheduler.add_noise function.
415
+ # The typical noising method of latents + noise * sigma will NOT work.
416
+ # 1. apply initial noise with appropriate step sigma, normalized against scale_factor
417
+ if sampler is not None:
418
+ sigma = sampler.sigmas[999-self.step].to(latents.device) / (model.model.latent_format.scale_factor)
419
+ else:
420
+ sigma = self.get_sigma(model, self.step-1000).to(latents.device) / (model.model.latent_format.scale_factor)
421
+ alpha_cumprod = 1 / ((sigma * sigma) + 1)
422
+ sqrt_alpha_prod = alpha_cumprod ** 0.5
423
+ sqrt_one_minus_alpha_prod = (1 - alpha_cumprod) ** 0.5
424
+ noised_latents = latents * sqrt_alpha_prod + noise * sqrt_one_minus_alpha_prod
425
+ # 2. create random noise z_rand for high frequency
426
+ temp_sample_settings = sample_settings.clone()
427
+ temp_sample_settings.batch_offset += self.iter_batch_offset * curr_i
428
+ temp_sample_settings.seed_offset += self.iter_seed_offset * curr_i
429
+ z_rand = temp_sample_settings.prepare_noise(seed=seed, latents=latents, noise=None,
430
+ extra_args=noise_extra_args, force_create_noise=True)
431
+ # 3. noise reinitialization - combines low freq. noise from noised_latents and high freq. noise from z_rand
432
+ noised_latents = freeinit.freq_mix_3d(x=noised_latents, noise=z_rand.to(dtype=latents.dtype, device=latents.device), LPF=self.freq_filter)
433
+ return cached_latents, noised_latents
434
+ elif self.init_type == self.DINKINIT_V1:
435
+ # NOTE: This was my first attempt at implementing FreeInit; it sorta works due to my alpha_cumprod shenanigans,
436
+ # but completely by accident.
437
+ # 1. apply initial noise with appropriate step sigma
438
+ sigma = self.get_sigma(model, self.step-1000).to(latents.device)
439
+ alpha_cumprod = 1 / ((sigma * sigma) + 1) #1 / ((sigma * sigma)) # 1 / ((sigma * sigma) + 1)
440
+ noised_latents = (latents + (cached_noise * sigma)) * alpha_cumprod
441
+ # 2. create random noise z_rand for high frequency
442
+ temp_sample_settings = sample_settings.clone()
443
+ temp_sample_settings.batch_offset += self.iter_batch_offset * curr_i
444
+ temp_sample_settings.seed_offset += self.iter_seed_offset * curr_i
445
+ z_rand = temp_sample_settings.prepare_noise(seed=seed, latents=latents, noise=None,
446
+ extra_args=noise_extra_args, force_create_noise=True)
447
+ ####z_rand = torch.randn_like(latents, dtype=latents.dtype, device=latents.device)
448
+ # 3. noise reinitialization - combines low freq. noise from noised_latents and high freq. noise from z_rand
449
+ noised_latents = freeinit.freq_mix_3d(x=noised_latents, noise=z_rand.to(dtype=latents.dtype, device=latents.device), LPF=self.freq_filter)
450
+ return cached_latents, noised_latents
451
+ else:
452
+ raise ValueError(f"FreeInit init_type '{self.init_type}' is not recognized.")
453
+
454
+
455
+ class CustomCFGKeyframe:
456
+ def __init__(self, cfg_multival: Union[float, Tensor], start_percent=0.0, guarantee_steps=1):
457
+ self.cfg_multival = cfg_multival
458
+ # scheduling
459
+ self.start_percent = float(start_percent)
460
+ self.start_t = 999999999.9
461
+ self.guarantee_steps = guarantee_steps
462
+
463
+ def clone(self):
464
+ c = CustomCFGKeyframe(cfg_multival=self.cfg_multival,
465
+ start_percent=self.start_percent, guarantee_steps=self.guarantee_steps)
466
+ c.start_t = self.start_t
467
+ return c
468
+
469
+
470
+ class CustomCFGKeyframeGroup:
471
+ def __init__(self):
472
+ self.keyframes: list[CustomCFGKeyframe] = []
473
+ self._current_keyframe: CustomCFGKeyframe = None
474
+ self._current_used_steps: int = 0
475
+ self._current_index: int = 0
476
+
477
+ def reset(self):
478
+ self._current_keyframe = None
479
+ self._current_used_steps = 0
480
+ self._current_index = 0
481
+ self._set_first_as_current()
482
+
483
+ def add(self, keyframe: CustomCFGKeyframe):
484
+ # add to end of list, then sort
485
+ self.keyframes.append(keyframe)
486
+ self.keyframes = get_sorted_list_via_attr(self.keyframes, "start_percent")
487
+ self._set_first_as_current()
488
+
489
+ def _set_first_as_current(self):
490
+ if len(self.keyframes) > 0:
491
+ self._current_keyframe = self.keyframes[0]
492
+ else:
493
+ self._current_keyframe = None
494
+
495
+ def has_index(self, index: int) -> int:
496
+ return index >=0 and index < len(self.keyframes)
497
+
498
+ def is_empty(self) -> bool:
499
+ return len(self.keyframes) == 0
500
+
501
+ def clone(self):
502
+ cloned = CustomCFGKeyframeGroup()
503
+ for keyframe in self.keyframes:
504
+ cloned.keyframes.append(keyframe)
505
+ cloned._set_first_as_current()
506
+ return cloned
507
+
508
+ def initialize_timesteps(self, model: BaseModel):
509
+ for keyframe in self.keyframes:
510
+ keyframe.start_t = model.model_sampling.percent_to_sigma(keyframe.start_percent)
511
+
512
+ def prepare_current_keyframe(self, t: Tensor):
513
+ curr_t: float = t[0]
514
+ prev_index = self._current_index
515
+ # if met guaranteed steps, look for next keyframe in case need to switch
516
+ if self._current_used_steps >= self._current_keyframe.guarantee_steps:
517
+ # if has next index, loop through and see if need t oswitch
518
+ if self.has_index(self._current_index+1):
519
+ for i in range(self._current_index+1, len(self.keyframes)):
520
+ eval_c = self.keyframes[i]
521
+ # check if start_t is greater or equal to curr_t
522
+ # NOTE: t is in terms of sigmas, not percent, so bigger number = earlier step in sampling
523
+ if eval_c.start_t >= curr_t:
524
+ self._current_index = i
525
+ self._current_keyframe = eval_c
526
+ self._current_used_steps = 0
527
+ # if guarantee_steps greater than zero, stop searching for other keyframes
528
+ if self._current_keyframe.guarantee_steps > 0:
529
+ break
530
+ # if eval_c is outside the percent range, stop looking further
531
+ else: break
532
+ # update steps current context is used
533
+ self._current_used_steps += 1
534
+
535
+ def patch_model(self, model: ModelPatcher) -> ModelPatcher:
536
+ def evolved_custom_cfg(args):
537
+ cond: Tensor = args["cond"]
538
+ uncond: Tensor = args["uncond"]
539
+ # cond scale is based purely off of CustomCFG - cond_scale input in sampler is ignored!
540
+ cond_scale = self.cfg_multival
541
+ if isinstance(cond_scale, Tensor):
542
+ cond_scale = prepare_mask_batch(cond_scale.to(cond.dtype).to(cond.device), cond.shape)
543
+ cond_scale = extend_to_batch_size(cond_scale, cond.shape[0])
544
+ return uncond + (cond - uncond) * cond_scale
545
+
546
+ model = model.clone()
547
+ model.set_model_sampler_cfg_function(evolved_custom_cfg)
548
+ return model
549
+
550
+ # properties shadow those of CustomCFGKeyframe
551
+ @property
552
+ def cfg_multival(self):
553
+ if self._current_keyframe != None:
554
+ return self._current_keyframe.cfg_multival
555
+ return None
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/sampling.py ADDED
@@ -0,0 +1,528 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Callable
2
+
3
+ import math
4
+ import torch
5
+ from torch import Tensor
6
+ from torch.nn.functional import group_norm
7
+ from einops import rearrange
8
+
9
+ import comfy.ldm.modules.attention as attention
10
+ from comfy.ldm.modules.diffusionmodules import openaimodel
11
+ import comfy.model_management as model_management
12
+ import comfy.samplers
13
+ import comfy.sample
14
+ import comfy.utils
15
+ from comfy.controlnet import ControlBase
16
+ import comfy.ops
17
+
18
+ from .context import ContextFuseMethod, ContextSchedules, get_context_weights, get_context_windows
19
+ from .sample_settings import IterationOptions, SampleSettings, SeedNoiseGeneration, prepare_mask_ad
20
+ from .utils_model import ModelTypeSD, wrap_function_to_inject_xformers_bug_info
21
+ from .model_injection import InjectionParams, ModelPatcherAndInjector, MotionModelGroup, MotionModelPatcher
22
+ from .motion_module_ad import AnimateDiffFormat, AnimateDiffInfo, AnimateDiffVersion, VanillaTemporalModule
23
+ from .logger import logger
24
+
25
+
26
+ ##################################################################################
27
+ ######################################################################
28
+ # Global variable to use to more conveniently hack variable access into samplers
29
+ class AnimateDiffHelper_GlobalState:
30
+ def __init__(self):
31
+ self.motion_models: MotionModelGroup = None
32
+ self.params: InjectionParams = None
33
+ self.sample_settings: SampleSettings = None
34
+ self.reset()
35
+
36
+ def initialize(self, model):
37
+ # this function is to be run in sampling func
38
+ if not self.initialized:
39
+ self.initialized = True
40
+ if self.motion_models is not None:
41
+ self.motion_models.initialize_timesteps(model)
42
+ if self.params.context_options is not None:
43
+ self.params.context_options.initialize_timesteps(model)
44
+ if self.sample_settings.custom_cfg is not None:
45
+ self.sample_settings.custom_cfg.initialize_timesteps(model)
46
+
47
+ def reset(self):
48
+ self.initialized = False
49
+ self.start_step: int = 0
50
+ self.last_step: int = 0
51
+ self.current_step: int = 0
52
+ self.total_steps: int = 0
53
+ if self.motion_models is not None:
54
+ del self.motion_models
55
+ self.motion_models = None
56
+ if self.params is not None:
57
+ del self.params
58
+ self.params = None
59
+ if self.sample_settings is not None:
60
+ del self.sample_settings
61
+ self.sample_settings = None
62
+
63
+ def update_with_inject_params(self, params: InjectionParams):
64
+ self.params = params
65
+
66
+ def is_using_sliding_context(self):
67
+ return self.params is not None and self.params.is_using_sliding_context()
68
+
69
+ def create_exposed_params(self):
70
+ # This dict will be exposed to be used by other extensions
71
+ # DO NOT change any of the key names
72
+ # or I will find you 👁.👁
73
+ return {
74
+ "full_length": self.params.full_length,
75
+ "context_length": self.params.context_options.context_length,
76
+ "sub_idxs": self.params.sub_idxs,
77
+ }
78
+
79
+ ADGS = AnimateDiffHelper_GlobalState()
80
+ ######################################################################
81
+ ##################################################################################
82
+
83
+
84
+ ##################################################################################
85
+ #### Code Injection ##################################################
86
+
87
+ # refer to forward_timestep_embed in comfy/ldm/modules/diffusionmodules/openaimodel.py
88
+ def forward_timestep_embed_factory() -> Callable:
89
+ def forward_timestep_embed(ts, x, emb, context=None, transformer_options={}, output_shape=None, time_context=None, num_video_frames=None, image_only_indicator=None):
90
+ for layer in ts:
91
+ if isinstance(layer, openaimodel.VideoResBlock):
92
+ x = layer(x, emb, num_video_frames, image_only_indicator)
93
+ elif isinstance(layer, openaimodel.TimestepBlock):
94
+ x = layer(x, emb)
95
+ elif isinstance(layer, VanillaTemporalModule):
96
+ x = layer(x, context)
97
+ elif isinstance(layer, attention.SpatialVideoTransformer):
98
+ x = layer(x, context, time_context, num_video_frames, image_only_indicator, transformer_options)
99
+ if "transformer_index" in transformer_options:
100
+ transformer_options["transformer_index"] += 1
101
+ if "current_index" in transformer_options: # keep this for backward compat, for now
102
+ transformer_options["current_index"] += 1
103
+ elif isinstance(layer, attention.SpatialTransformer):
104
+ x = layer(x, context, transformer_options)
105
+ if "transformer_index" in transformer_options:
106
+ transformer_options["transformer_index"] += 1
107
+ if "current_index" in transformer_options: # keep this for backward compat, for now
108
+ transformer_options["current_index"] += 1
109
+ elif isinstance(layer, openaimodel.Upsample):
110
+ x = layer(x, output_shape=output_shape)
111
+ else:
112
+ x = layer(x)
113
+ return x
114
+ return forward_timestep_embed
115
+
116
+
117
+ def unlimited_memory_required(*args, **kwargs):
118
+ return 0
119
+
120
+
121
+ def groupnorm_mm_factory(params: InjectionParams, manual_cast=False):
122
+ def groupnorm_mm_forward(self, input: Tensor) -> Tensor:
123
+ # axes_factor normalizes batch based on total conds and unconds passed in batch;
124
+ # the conds and unconds per batch can change based on VRAM optimizations that may kick in
125
+ if not params.is_using_sliding_context():
126
+ batched_conds = input.size(0)//params.full_length
127
+ else:
128
+ batched_conds = input.size(0)//params.context_options.context_length
129
+
130
+ input = rearrange(input, "(b f) c h w -> b c f h w", b=batched_conds)
131
+ if manual_cast:
132
+ weight, bias = comfy.ops.cast_bias_weight(self, input)
133
+ else:
134
+ weight, bias = self.weight, self.bias
135
+ input = group_norm(input, self.num_groups, weight, bias, self.eps)
136
+ input = rearrange(input, "b c f h w -> (b f) c h w", b=batched_conds)
137
+ return input
138
+ return groupnorm_mm_forward
139
+
140
+
141
+ def get_additional_models_factory(orig_get_additional_models: Callable, motion_models: MotionModelGroup):
142
+ def get_additional_models_with_motion(*args, **kwargs):
143
+ models, inference_memory = orig_get_additional_models(*args, **kwargs)
144
+ if motion_models is not None:
145
+ for motion_model in motion_models.models:
146
+ models.append(motion_model)
147
+ # TODO: account for inference memory as well?
148
+ return models, inference_memory
149
+ return get_additional_models_with_motion
150
+ ######################################################################
151
+ ##################################################################################
152
+
153
+
154
+ def apply_params_to_motion_models(motion_models: MotionModelGroup, params: InjectionParams):
155
+ params = params.clone()
156
+ for context in params.context_options.contexts:
157
+ if context.context_schedule == ContextSchedules.VIEW_AS_CONTEXT:
158
+ context.context_length = params.full_length
159
+ # TODO: check (and message) should be different based on use_on_equal_length setting
160
+ if params.context_options.context_length:
161
+ pass
162
+
163
+ allow_equal = params.context_options.use_on_equal_length
164
+ if params.context_options.context_length:
165
+ enough_latents = params.full_length >= params.context_options.context_length if allow_equal else params.full_length > params.context_options.context_length
166
+ else:
167
+ enough_latents = False
168
+ if params.context_options.context_length and enough_latents:
169
+ logger.info(f"Sliding context window activated - latents passed in ({params.full_length}) greater than context_length {params.context_options.context_length}.")
170
+ else:
171
+ logger.info(f"Regular AnimateDiff activated - latents passed in ({params.full_length}) less or equal to context_length {params.context_options.context_length}.")
172
+ params.reset_context()
173
+ if motion_models is not None:
174
+ # if no context_length, treat video length as intended AD frame window
175
+ if not params.context_options.context_length:
176
+ for motion_model in motion_models.models:
177
+ if not motion_model.model.is_length_valid_for_encoding_max_len(params.full_length):
178
+ raise ValueError(f"Without a context window, AnimateDiff model {motion_model.model.mm_info.mm_name} has upper limit of {motion_model.model.encoding_max_len} frames, but received {params.full_length} latents.")
179
+ motion_models.set_video_length(params.full_length, params.full_length)
180
+ # otherwise, treat context_length as intended AD frame window
181
+ else:
182
+ for motion_model in motion_models.models:
183
+ view_options = params.context_options.view_options
184
+ context_length = view_options.context_length if view_options else params.context_options.context_length
185
+ if not motion_model.model.is_length_valid_for_encoding_max_len(context_length):
186
+ raise ValueError(f"AnimateDiff model {motion_model.model.mm_info.mm_name} has upper limit of {motion_model.model.encoding_max_len} frames for a context window, but received context length of {params.context_options.context_length}.")
187
+ motion_models.set_video_length(params.context_options.context_length, params.full_length)
188
+ # inject model
189
+ module_str = "modules" if len(motion_models.models) > 1 else "module"
190
+ logger.info(f"Using motion {module_str} {motion_models.get_name_string(show_version=True)}.")
191
+ return params
192
+
193
+
194
+ class FunctionInjectionHolder:
195
+ def __init__(self):
196
+ pass
197
+
198
+ def inject_functions(self, model: ModelPatcherAndInjector, params: InjectionParams):
199
+ # Save Original Functions
200
+ self.orig_forward_timestep_embed = openaimodel.forward_timestep_embed # needed to account for VanillaTemporalModule
201
+ self.orig_memory_required = model.model.memory_required # allows for "unlimited area hack" to prevent halving of conds/unconds
202
+ self.orig_groupnorm_forward = torch.nn.GroupNorm.forward # used to normalize latents to remove "flickering" of colors/brightness between frames
203
+ self.orig_groupnorm_manual_cast_forward = comfy.ops.manual_cast.GroupNorm.forward_comfy_cast_weights
204
+ self.orig_sampling_function = comfy.samplers.sampling_function # used to support sliding context windows in samplers
205
+ self.orig_prepare_mask = comfy.sample.prepare_mask
206
+ self.orig_get_additional_models = comfy.sample.get_additional_models
207
+ # Inject Functions
208
+ openaimodel.forward_timestep_embed = forward_timestep_embed_factory()
209
+ if params.unlimited_area_hack:
210
+ model.model.memory_required = unlimited_memory_required
211
+ if model.motion_models is not None:
212
+ # only apply groupnorm hack if not [v3 or ([not Hotshot] and SD1.5 and v2 and apply_v2_properly)]
213
+ info: AnimateDiffInfo = model.motion_models[0].model.mm_info
214
+ if not (info.mm_version == AnimateDiffVersion.V3 or
215
+ (info.mm_format not in [AnimateDiffFormat.HOTSHOTXL] and info.sd_type == ModelTypeSD.SD1_5 and info.mm_version == AnimateDiffVersion.V2 and params.apply_v2_properly)):
216
+ torch.nn.GroupNorm.forward = groupnorm_mm_factory(params)
217
+ comfy.ops.manual_cast.GroupNorm.forward_comfy_cast_weights = groupnorm_mm_factory(params, manual_cast=True)
218
+ # if mps device (Apple Silicon), disable batched conds to avoid black images with groupnorm hack
219
+ try:
220
+ if model.load_device.type == "mps":
221
+ model.model.memory_required = unlimited_memory_required
222
+ except Exception:
223
+ pass
224
+ del info
225
+ comfy.samplers.sampling_function = evolved_sampling_function
226
+ comfy.sample.prepare_mask = prepare_mask_ad
227
+ comfy.sample.get_additional_models = get_additional_models_factory(self.orig_get_additional_models, model.motion_models)
228
+
229
+ def restore_functions(self, model: ModelPatcherAndInjector):
230
+ # Restoration
231
+ try:
232
+ model.model.memory_required = self.orig_memory_required
233
+ openaimodel.forward_timestep_embed = self.orig_forward_timestep_embed
234
+ torch.nn.GroupNorm.forward = self.orig_groupnorm_forward
235
+ comfy.ops.manual_cast.GroupNorm.forward_comfy_cast_weights = self.orig_groupnorm_manual_cast_forward
236
+ comfy.samplers.sampling_function = self.orig_sampling_function
237
+ comfy.sample.prepare_mask = self.orig_prepare_mask
238
+ comfy.sample.get_additional_models = self.orig_get_additional_models
239
+ except AttributeError:
240
+ logger.error("Encountered AttributeError while attempting to restore functions - likely, an error occured while trying " + \
241
+ "to save original functions before injection, and a more specific error was thrown by ComfyUI.")
242
+
243
+
244
+ def motion_sample_factory(orig_comfy_sample: Callable, is_custom: bool=False) -> Callable:
245
+ def motion_sample(model: ModelPatcherAndInjector, noise: Tensor, *args, **kwargs):
246
+ # check if model is intended for injecting
247
+ if type(model) != ModelPatcherAndInjector:
248
+ return orig_comfy_sample(model, noise, *args, **kwargs)
249
+ # otherwise, injection time
250
+ latents = None
251
+ cached_latents = None
252
+ cached_noise = None
253
+ function_injections = FunctionInjectionHolder()
254
+ try:
255
+ if model.sample_settings.custom_cfg is not None:
256
+ model = model.sample_settings.custom_cfg.patch_model(model)
257
+ # clone params from model
258
+ params = model.motion_injection_params.clone()
259
+ # get amount of latents passed in, and store in params
260
+ latents: Tensor = args[-1]
261
+ params.full_length = latents.size(0)
262
+ # reset global state
263
+ ADGS.reset()
264
+
265
+ # apply custom noise, if needed
266
+ disable_noise = kwargs.get("disable_noise") or False
267
+ seed = kwargs["seed"]
268
+
269
+ # apply params to motion model
270
+ params = apply_params_to_motion_models(model.motion_models, params)
271
+
272
+ # store and inject functions
273
+ function_injections.inject_functions(model, params)
274
+
275
+ # prepare noise_extra_args for noise generation purposes
276
+ noise_extra_args = {"disable_noise": disable_noise}
277
+ params.set_noise_extra_args(noise_extra_args)
278
+ # if noise is not disabled, do noise stuff
279
+ if not disable_noise:
280
+ noise = model.sample_settings.prepare_noise(seed, latents, noise, extra_args=noise_extra_args, force_create_noise=False)
281
+
282
+ # callback setup
283
+ original_callback = kwargs.get("callback", None)
284
+ def ad_callback(step, x0, x, total_steps):
285
+ if original_callback is not None:
286
+ original_callback(step, x0, x, total_steps)
287
+ # update GLOBALSTATE for next iteration
288
+ ADGS.current_step = ADGS.start_step + step + 1
289
+ kwargs["callback"] = ad_callback
290
+ ADGS.motion_models = model.motion_models
291
+ ADGS.sample_settings = model.sample_settings
292
+
293
+ # apply adapt_denoise_steps
294
+ args = list(args)
295
+ if model.sample_settings.adapt_denoise_steps and not is_custom:
296
+ # only applicable when denoise and steps are provided (from simple KSampler nodes)
297
+ denoise = kwargs.get("denoise", None)
298
+ steps = args[0]
299
+ if denoise is not None and type(steps) == int:
300
+ args[0] = max(int(denoise * steps), 1)
301
+
302
+
303
+ iter_opts = IterationOptions()
304
+ if model.sample_settings is not None:
305
+ iter_opts = model.sample_settings.iteration_opts
306
+ iter_opts.initialize(latents)
307
+ # cache initial noise and latents, if needed
308
+ if iter_opts.cache_init_latents:
309
+ cached_latents = latents.clone()
310
+ if iter_opts.cache_init_noise:
311
+ cached_noise = noise.clone()
312
+ # prepare iter opts preprocess kwargs, if needed
313
+ iter_kwargs = {}
314
+ if iter_opts.need_sampler:
315
+ # -5 for sampler_name (not custom) and sampler (custom)
316
+ model_management.load_model_gpu(model)
317
+ if is_custom:
318
+ iter_kwargs[IterationOptions.SAMPLER] = None #args[-5]
319
+ else:
320
+ iter_kwargs[IterationOptions.SAMPLER] = comfy.samplers.KSampler(
321
+ model.model, steps=999, #steps=args[-7],
322
+ device=model.current_device, sampler=args[-5],
323
+ scheduler=args[-4], denoise=kwargs.get("denoise", None),
324
+ model_options=model.model_options)
325
+
326
+ for curr_i in range(iter_opts.iterations):
327
+ # handle GLOBALSTATE vars and step tally
328
+ ADGS.update_with_inject_params(params)
329
+ ADGS.start_step = kwargs.get("start_step") or 0
330
+ ADGS.current_step = ADGS.start_step
331
+ ADGS.last_step = kwargs.get("last_step") or 0
332
+ if iter_opts.iterations > 1:
333
+ logger.info(f"Iteration {curr_i+1}/{iter_opts.iterations}")
334
+ # perform any iter_opts preprocessing on latents
335
+ latents, noise = iter_opts.preprocess_latents(curr_i=curr_i, model=model, latents=latents, noise=noise,
336
+ cached_latents=cached_latents, cached_noise=cached_noise,
337
+ seed=seed,
338
+ sample_settings=model.sample_settings, noise_extra_args=noise_extra_args,
339
+ **iter_kwargs)
340
+ args[-1] = latents
341
+
342
+ if model.motion_models is not None:
343
+ model.motion_models.pre_run(model)
344
+ if model.sample_settings is not None:
345
+ model.sample_settings.pre_run(model)
346
+ latents = wrap_function_to_inject_xformers_bug_info(orig_comfy_sample)(model, noise, *args, **kwargs)
347
+ return latents
348
+ finally:
349
+ del latents
350
+ del noise
351
+ del cached_latents
352
+ del cached_noise
353
+ # reset global state
354
+ ADGS.reset()
355
+ # restore injected functions
356
+ function_injections.restore_functions(model)
357
+ del function_injections
358
+ return motion_sample
359
+
360
+
361
+ def evolved_sampling_function(model, x, timestep, uncond, cond, cond_scale, model_options: dict={}, seed=None):
362
+ ADGS.initialize(model)
363
+ if ADGS.motion_models is not None:
364
+ ADGS.motion_models.prepare_current_keyframe(t=timestep)
365
+ if ADGS.params.context_options is not None:
366
+ ADGS.params.context_options.prepare_current_context(t=timestep)
367
+ if ADGS.sample_settings.custom_cfg is not None:
368
+ ADGS.sample_settings.custom_cfg.prepare_current_keyframe(t=timestep)
369
+
370
+ # never use cfg1 optimization if using custom_cfg (since can have timesteps and such)
371
+ if ADGS.sample_settings.custom_cfg is None and math.isclose(cond_scale, 1.0) and model_options.get("disable_cfg1_optimization", False) == False:
372
+ uncond_ = None
373
+ else:
374
+ uncond_ = uncond
375
+
376
+ # add AD/evolved-sampling params to model_options (transformer_options)
377
+ model_options = model_options.copy()
378
+ if "tranformer_options" not in model_options:
379
+ model_options["tranformer_options"] = {}
380
+ model_options["transformer_options"]["ad_params"] = ADGS.create_exposed_params()
381
+
382
+ if not ADGS.is_using_sliding_context():
383
+ cond_pred, uncond_pred = comfy.samplers.calc_cond_uncond_batch(model, cond, uncond_, x, timestep, model_options)
384
+ else:
385
+ cond_pred, uncond_pred = sliding_calc_cond_uncond_batch(model, cond, uncond_, x, timestep, model_options)
386
+
387
+ if "sampler_cfg_function" in model_options:
388
+ args = {"cond": x - cond_pred, "uncond": x - uncond_pred, "cond_scale": cond_scale, "timestep": timestep, "input": x, "sigma": timestep,
389
+ "cond_denoised": cond_pred, "uncond_denoised": uncond_pred, "model": model, "model_options": model_options}
390
+ cfg_result = x - model_options["sampler_cfg_function"](args)
391
+ else:
392
+ cfg_result = uncond_pred + (cond_pred - uncond_pred) * cond_scale
393
+
394
+ for fn in model_options.get("sampler_post_cfg_function", []):
395
+ args = {"denoised": cfg_result, "cond": cond, "uncond": uncond, "model": model, "uncond_denoised": uncond_pred, "cond_denoised": cond_pred,
396
+ "sigma": timestep, "model_options": model_options, "input": x}
397
+ cfg_result = fn(args)
398
+
399
+ return cfg_result
400
+
401
+
402
+ # sliding_calc_cond_uncond_batch inspired by ashen's initial hack for 16-frame sliding context:
403
+ # https://github.com/comfyanonymous/ComfyUI/compare/master...ashen-sensored:ComfyUI:master
404
+ def sliding_calc_cond_uncond_batch(model, cond, uncond, x_in: Tensor, timestep, model_options):
405
+ def prepare_control_objects(control: ControlBase, full_idxs: list[int]):
406
+ if control.previous_controlnet is not None:
407
+ prepare_control_objects(control.previous_controlnet, full_idxs)
408
+ control.sub_idxs = full_idxs
409
+ control.full_latent_length = ADGS.params.full_length
410
+ control.context_length = ADGS.params.context_options.context_length
411
+
412
+ def get_resized_cond(cond_in, full_idxs) -> list:
413
+ # reuse or resize cond items to match context requirements
414
+ resized_cond = []
415
+ # cond object is a list containing a dict - outer list is irrelevant, so just loop through it
416
+ for actual_cond in cond_in:
417
+ resized_actual_cond = actual_cond.copy()
418
+ # now we are in the inner dict - "pooled_output" is a tensor, "control" is a ControlBase object, "model_conds" is dictionary
419
+ for key in actual_cond:
420
+ try:
421
+ cond_item = actual_cond[key]
422
+ if isinstance(cond_item, Tensor):
423
+ # check that tensor is the expected length - x.size(0)
424
+ if cond_item.size(0) == x_in.size(0):
425
+ # if so, it's subsetting time - tell controls the expected indeces so they can handle them
426
+ actual_cond_item = cond_item[full_idxs]
427
+ resized_actual_cond[key] = actual_cond_item
428
+ else:
429
+ resized_actual_cond[key] = cond_item
430
+ # look for control
431
+ elif key == "control":
432
+ control_item = cond_item
433
+ if hasattr(control_item, "sub_idxs"):
434
+ prepare_control_objects(control_item, full_idxs)
435
+ else:
436
+ raise ValueError(f"Control type {type(control_item).__name__} may not support required features for sliding context window; \
437
+ use Control objects from Kosinkadink/ComfyUI-Advanced-ControlNet nodes, or make sure Advanced-ControlNet is updated.")
438
+ resized_actual_cond[key] = control_item
439
+ del control_item
440
+ elif isinstance(cond_item, dict):
441
+ new_cond_item = cond_item.copy()
442
+ # when in dictionary, look for tensors and CONDCrossAttn [comfy/conds.py] (has cond attr that is a tensor)
443
+ for cond_key, cond_value in new_cond_item.items():
444
+ if isinstance(cond_value, Tensor):
445
+ if cond_value.size(0) == x_in.size(0):
446
+ new_cond_item[cond_key] = cond_value[full_idxs]
447
+ # if has cond that is a Tensor, check if needs to be subset
448
+ elif hasattr(cond_value, "cond") and isinstance(cond_value.cond, Tensor):
449
+ if cond_value.cond.size(0) == x_in.size(0):
450
+ new_cond_item[cond_key] = cond_value._copy_with(cond_value.cond[full_idxs])
451
+ resized_actual_cond[key] = new_cond_item
452
+ else:
453
+ resized_actual_cond[key] = cond_item
454
+ finally:
455
+ del cond_item # just in case to prevent VRAM issues
456
+ resized_cond.append(resized_actual_cond)
457
+ return resized_cond
458
+
459
+ # get context windows
460
+ ADGS.params.context_options.step = ADGS.current_step
461
+ context_windows = get_context_windows(ADGS.params.full_length, ADGS.params.context_options)
462
+ # figure out how input is split
463
+ batched_conds = x_in.size(0)//ADGS.params.full_length
464
+
465
+ if ADGS.motion_models is not None:
466
+ ADGS.motion_models.set_view_options(ADGS.params.context_options.view_options)
467
+
468
+ # prepare final cond, uncond, and out_count
469
+ cond_final = torch.zeros_like(x_in)
470
+ uncond_final = torch.zeros_like(x_in)
471
+ out_count_final = torch.zeros((x_in.shape[0], 1, 1, 1), device=x_in.device)
472
+ bias_final = [0.0] * x_in.shape[0]
473
+
474
+ # perform calc_cond_uncond_batch per context window
475
+ for ctx_idxs in context_windows:
476
+ ADGS.params.sub_idxs = ctx_idxs
477
+ if ADGS.motion_models is not None:
478
+ ADGS.motion_models.set_sub_idxs(ctx_idxs)
479
+ ADGS.motion_models.set_video_length(len(ctx_idxs), ADGS.params.full_length)
480
+ # update exposed params
481
+ model_options["transformer_options"]["ad_params"]["sub_idxs"] = ctx_idxs
482
+ model_options["transformer_options"]["ad_params"]["context_length"] = len(ctx_idxs)
483
+ # account for all portions of input frames
484
+ full_idxs = []
485
+ for n in range(batched_conds):
486
+ for ind in ctx_idxs:
487
+ full_idxs.append((ADGS.params.full_length*n)+ind)
488
+ # get subsections of x, timestep, cond, uncond, cond_concat
489
+ sub_x = x_in[full_idxs]
490
+ sub_timestep = timestep[full_idxs]
491
+ sub_cond = get_resized_cond(cond, full_idxs) if cond is not None else None
492
+ sub_uncond = get_resized_cond(uncond, full_idxs) if uncond is not None else None
493
+
494
+ sub_cond_out, sub_uncond_out = comfy.samplers.calc_cond_uncond_batch(model, sub_cond, sub_uncond, sub_x, sub_timestep, model_options)
495
+
496
+ if ADGS.params.context_options.fuse_method == ContextFuseMethod.RELATIVE:
497
+ full_length = ADGS.params.full_length
498
+ for pos, idx in enumerate(ctx_idxs):
499
+ # bias is the influence of a specific index in relation to the whole context window
500
+ bias = 1 - abs(idx - (ctx_idxs[0] + ctx_idxs[-1]) / 2) / ((ctx_idxs[-1] - ctx_idxs[0] + 1e-2) / 2)
501
+ bias = max(1e-2, bias)
502
+ # take weighted average relative to total bias of current idx
503
+ # and account for batched_conds
504
+ for n in range(batched_conds):
505
+ bias_total = bias_final[(full_length*n)+idx]
506
+ prev_weight = (bias_total / (bias_total + bias))
507
+ new_weight = (bias / (bias_total + bias))
508
+ cond_final[(full_length*n)+idx] = cond_final[(full_length*n)+idx] * prev_weight + sub_cond_out[(full_length*n)+pos] * new_weight
509
+ uncond_final[(full_length*n)+idx] = uncond_final[(full_length*n)+idx] * prev_weight + sub_uncond_out[(full_length*n)+pos] * new_weight
510
+ bias_final[(full_length*n)+idx] = bias_total + bias
511
+ else:
512
+ # add conds and counts based on weights of fuse method
513
+ weights = get_context_weights(len(ctx_idxs), ADGS.params.context_options.fuse_method) * batched_conds
514
+ weights_tensor = torch.Tensor(weights).to(device=x_in.device).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
515
+ cond_final[full_idxs] += sub_cond_out * weights_tensor
516
+ uncond_final[full_idxs] += sub_uncond_out * weights_tensor
517
+ out_count_final[full_idxs] += weights_tensor
518
+
519
+ if ADGS.params.context_options.fuse_method == ContextFuseMethod.RELATIVE:
520
+ # already normalized, so return as is
521
+ del out_count_final
522
+ return cond_final, uncond_final
523
+ else:
524
+ # normalize cond and uncond via division by context usage counts
525
+ cond_final /= out_count_final
526
+ uncond_final /= out_count_final
527
+ del out_count_final
528
+ return cond_final, uncond_final
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/utils_model.py ADDED
@@ -0,0 +1,417 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ from pathlib import Path
3
+ from typing import Callable, Union
4
+ from collections.abc import Iterable
5
+ from time import time
6
+ import copy
7
+
8
+ import torch
9
+ import numpy as np
10
+
11
+ import folder_paths
12
+ from comfy.model_base import SD21UNCLIP, SDXL, BaseModel, SDXLRefiner, SVD_img2vid, model_sampling, ModelType
13
+ from comfy.model_management import xformers_enabled
14
+ from comfy.model_patcher import ModelPatcher
15
+
16
+ import comfy.model_sampling
17
+ import comfy_extras.nodes_model_advanced
18
+
19
+
20
+ BIGMIN = -(2**53-1)
21
+ BIGMAX = (2**53-1)
22
+
23
+
24
+ class ModelSamplingConfig:
25
+ def __init__(self, beta_schedule: str, linear_start: float=None, linear_end: float=None):
26
+ self.sampling_settings = {"beta_schedule": beta_schedule}
27
+ if linear_start is not None:
28
+ self.sampling_settings["linear_start"] = linear_start
29
+ if linear_end is not None:
30
+ self.sampling_settings["linear_end"] = linear_end
31
+ self.beta_schedule = beta_schedule # keeping this for backwards compatibility
32
+
33
+
34
+ class ModelSamplingType:
35
+ EPS = "eps"
36
+ V_PREDICTION = "v_prediction"
37
+ LCM = "lcm"
38
+
39
+ _NON_LCM_LIST = [EPS, V_PREDICTION]
40
+ _FULL_LIST = [EPS, V_PREDICTION, LCM]
41
+
42
+ MAP = {
43
+ EPS: ModelType.EPS,
44
+ V_PREDICTION: ModelType.V_PREDICTION,
45
+ LCM: comfy_extras.nodes_model_advanced.LCM,
46
+ }
47
+
48
+ @classmethod
49
+ def from_alias(cls, alias: str):
50
+ return cls.MAP[alias]
51
+
52
+
53
+ def factory_model_sampling_discrete_distilled(original_timesteps=50):
54
+ class ModelSamplingDiscreteDistilledEvolved(comfy_extras.nodes_model_advanced.ModelSamplingDiscreteDistilled):
55
+ def __init__(self, *args, **kwargs):
56
+ self.original_timesteps = original_timesteps # normal LCM has 50
57
+ super().__init__(*args, **kwargs)
58
+ return ModelSamplingDiscreteDistilledEvolved
59
+
60
+
61
+ # based on code in comfy_extras/nodes_model_advanced.py
62
+ def evolved_model_sampling(model_config: ModelSamplingConfig, model_type: ModelType, alias: str, original_timesteps: int=None):
63
+ # if LCM, need to handle manually
64
+ if BetaSchedules.is_lcm(alias) or original_timesteps is not None:
65
+ sampling_type = comfy_extras.nodes_model_advanced.LCM
66
+ if original_timesteps is not None:
67
+ sampling_base = factory_model_sampling_discrete_distilled(original_timesteps=original_timesteps)
68
+ elif alias == BetaSchedules.LCM_100:
69
+ sampling_base = factory_model_sampling_discrete_distilled(original_timesteps=100)
70
+ elif alias == BetaSchedules.LCM_25:
71
+ sampling_base = factory_model_sampling_discrete_distilled(original_timesteps=25)
72
+ else:
73
+ sampling_base = comfy_extras.nodes_model_advanced.ModelSamplingDiscreteDistilled
74
+ class ModelSamplingAdvancedEvolved(sampling_base, sampling_type):
75
+ pass
76
+ # NOTE: if I want to support zsnr, this is where I would add that code
77
+ return ModelSamplingAdvancedEvolved(model_config)
78
+ # otherwise, use vanilla model_sampling function
79
+ return model_sampling(model_config, model_type)
80
+
81
+
82
+ class BetaSchedules:
83
+ AUTOSELECT = "autoselect"
84
+ SQRT_LINEAR = "sqrt_linear (AnimateDiff)"
85
+ LINEAR_ADXL = "linear (AnimateDiff-SDXL)"
86
+ LINEAR = "linear (HotshotXL/default)"
87
+ AVG_LINEAR_SQRT_LINEAR = "avg(sqrt_linear,linear)"
88
+ LCM_AVG_LINEAR_SQRT_LINEAR = "lcm avg(sqrt_linear,linear)"
89
+ LCM = "lcm"
90
+ LCM_100 = "lcm[100_ots]"
91
+ LCM_25 = "lcm[25_ots]"
92
+ LCM_SQRT_LINEAR = "lcm >> sqrt_linear"
93
+ USE_EXISTING = "use existing"
94
+ SQRT = "sqrt"
95
+ COSINE = "cosine"
96
+ SQUAREDCOS_CAP_V2 = "squaredcos_cap_v2"
97
+ RAW_LINEAR = "linear"
98
+ RAW_SQRT_LINEAR = "sqrt_linear"
99
+
100
+ RAW_BETA_SCHEDULE_LIST = [RAW_LINEAR, RAW_SQRT_LINEAR, SQRT, COSINE, SQUAREDCOS_CAP_V2]
101
+
102
+ ALIAS_LCM_LIST = [LCM, LCM_100, LCM_25, LCM_SQRT_LINEAR]
103
+
104
+ ALIAS_ACTIVE_LIST = [SQRT_LINEAR, LINEAR_ADXL, LINEAR, AVG_LINEAR_SQRT_LINEAR, LCM_AVG_LINEAR_SQRT_LINEAR, LCM, LCM_100, LCM_SQRT_LINEAR, # LCM_25 is purposely omitted
105
+ SQRT, COSINE, SQUAREDCOS_CAP_V2]
106
+
107
+ ALIAS_LIST = [AUTOSELECT, USE_EXISTING] + ALIAS_ACTIVE_LIST
108
+
109
+
110
+
111
+ ALIAS_MAP = {
112
+ SQRT_LINEAR: "sqrt_linear",
113
+ LINEAR_ADXL: "linear", # also linear, but has different linear_end (0.020)
114
+ LINEAR: "linear",
115
+ LCM_100: "linear", # distilled, 100 original timesteps
116
+ LCM_25: "linear", # distilled, 25 original timesteps
117
+ LCM: "linear", # distilled
118
+ LCM_SQRT_LINEAR: "sqrt_linear", # distilled, sqrt_linear
119
+ SQRT: "sqrt",
120
+ COSINE: "cosine",
121
+ SQUAREDCOS_CAP_V2: "squaredcos_cap_v2",
122
+ RAW_LINEAR: "linear",
123
+ RAW_SQRT_LINEAR: "sqrt_linear"
124
+ }
125
+
126
+ @classmethod
127
+ def is_lcm(cls, alias: str):
128
+ return alias in cls.ALIAS_LCM_LIST
129
+
130
+ @classmethod
131
+ def to_name(cls, alias: str):
132
+ return cls.ALIAS_MAP[alias]
133
+
134
+ @classmethod
135
+ def to_config(cls, alias: str) -> ModelSamplingConfig:
136
+ linear_start = None
137
+ linear_end = None
138
+ if alias == cls.LINEAR_ADXL:
139
+ # uses linear_end=0.020
140
+ linear_end = 0.020
141
+ return ModelSamplingConfig(cls.to_name(alias), linear_start=linear_start, linear_end=linear_end)
142
+
143
+ @classmethod
144
+ def _to_model_sampling(cls, alias: str, model_type: ModelType, config_override: ModelSamplingConfig=None, original_timesteps: int=None):
145
+ if alias == cls.USE_EXISTING:
146
+ return None
147
+ elif config_override != None:
148
+ return evolved_model_sampling(config_override, model_type=model_type, alias=alias, original_timesteps=original_timesteps)
149
+ elif alias == cls.AVG_LINEAR_SQRT_LINEAR:
150
+ ms_linear = evolved_model_sampling(cls.to_config(cls.LINEAR), model_type=model_type, alias=cls.LINEAR)
151
+ ms_sqrt_linear = evolved_model_sampling(cls.to_config(cls.SQRT_LINEAR), model_type=model_type, alias=cls.SQRT_LINEAR)
152
+ avg_sigmas = (ms_linear.sigmas + ms_sqrt_linear.sigmas) / 2
153
+ ms_linear.set_sigmas(avg_sigmas)
154
+ return ms_linear
155
+ elif alias == cls.LCM_AVG_LINEAR_SQRT_LINEAR:
156
+ ms_linear = evolved_model_sampling(cls.to_config(cls.LCM), model_type=model_type, alias=cls.LCM)
157
+ ms_sqrt_linear = evolved_model_sampling(cls.to_config(cls.LCM_SQRT_LINEAR), model_type=model_type, alias=cls.LCM_SQRT_LINEAR)
158
+ avg_sigmas = (ms_linear.sigmas + ms_sqrt_linear.sigmas) / 2
159
+ ms_linear.set_sigmas(avg_sigmas)
160
+ return ms_linear
161
+ # average out the sigmas
162
+ ms_obj = evolved_model_sampling(cls.to_config(alias), model_type=model_type, alias=alias, original_timesteps=original_timesteps)
163
+ return ms_obj
164
+
165
+ @classmethod
166
+ def to_model_sampling(cls, alias: str, model: ModelPatcher):
167
+ return cls._to_model_sampling(alias=alias, model_type=model.model.model_type)
168
+
169
+ @staticmethod
170
+ def get_alias_list_with_first_element(first_element: str):
171
+ new_list = BetaSchedules.ALIAS_LIST.copy()
172
+ element_index = new_list.index(first_element)
173
+ new_list[0], new_list[element_index] = new_list[element_index], new_list[0]
174
+ return new_list
175
+
176
+
177
+ class SigmaSchedule:
178
+ def __init__(self, model_sampling: comfy.model_sampling.ModelSamplingDiscrete, model_type: ModelType):
179
+ self.model_sampling = model_sampling
180
+ #self.config = config
181
+ self.model_type = model_type
182
+ self.original_timesteps = getattr(self.model_sampling, "original_timesteps", None)
183
+
184
+ def is_lcm(self):
185
+ return self.original_timesteps is not None
186
+
187
+ def total_sigmas(self):
188
+ return len(self.model_sampling.sigmas)
189
+
190
+ def clone(self) -> 'SigmaSchedule':
191
+ new_model_sampling = copy.deepcopy(self.model_sampling)
192
+ #new_config = copy.deepcopy(self.config)
193
+ return SigmaSchedule(model_sampling=new_model_sampling, model_type=self.model_type)
194
+
195
+ # def clone(self):
196
+ # pass
197
+
198
+ @staticmethod
199
+ def apply_zsnr(new_model_sampling: comfy.model_sampling.ModelSamplingDiscrete):
200
+ new_model_sampling.set_sigmas(comfy_extras.nodes_model_advanced.rescale_zero_terminal_snr_sigmas(new_model_sampling.sigmas))
201
+
202
+ # def get_lcmified(self, original_timesteps=50, zsnr=False) -> 'SigmaSchedule':
203
+ # new_model_sampling = evolved_model_sampling(model_config=self.config, model_type=self.model_type, alias=None, original_timesteps=original_timesteps)
204
+ # if zsnr:
205
+ # new_model_sampling.set_sigmas(comfy_extras.nodes_model_advanced.rescale_zero_terminal_snr_sigmas(new_model_sampling.sigmas))
206
+ # return SigmaSchedule(model_sampling=new_model_sampling, config=self.config, model_type=self.model_type, is_lcm=True)
207
+
208
+
209
+ class InterpolationMethod:
210
+ LINEAR = "linear"
211
+ EASE_IN = "ease_in"
212
+ EASE_OUT = "ease_out"
213
+ EASE_IN_OUT = "ease_in_out"
214
+
215
+ _LIST = [LINEAR, EASE_IN, EASE_OUT, EASE_IN_OUT]
216
+
217
+ @classmethod
218
+ def get_weights(cls, num_from: float, num_to: float, length: int, method: str, reverse=False):
219
+ diff = num_to - num_from
220
+ if method == cls.LINEAR:
221
+ weights = torch.linspace(num_from, num_to, length)
222
+ elif method == cls.EASE_IN:
223
+ index = torch.linspace(0, 1, length)
224
+ weights = diff * np.power(index, 2) + num_from
225
+ elif method == cls.EASE_OUT:
226
+ index = torch.linspace(0, 1, length)
227
+ weights = diff * (1 - np.power(1 - index, 2)) + num_from
228
+ elif method == cls.EASE_IN_OUT:
229
+ index = torch.linspace(0, 1, length)
230
+ weights = diff * ((1 - np.cos(index * np.pi)) / 2) + num_from
231
+ else:
232
+ raise ValueError(f"Unrecognized interpolation method '{method}'.")
233
+ if reverse:
234
+ weights = weights.flip(dims=(0,))
235
+ return weights
236
+
237
+
238
+ class Folders:
239
+ ANIMATEDIFF_MODELS = "animatediff_models"
240
+ MOTION_LORA = "animatediff_motion_lora"
241
+ VIDEO_FORMATS = "animatediff_video_formats"
242
+
243
+
244
+ def add_extension_to_folder_path(folder_name: str, extensions: Union[str, list[str]]):
245
+ if folder_name in folder_paths.folder_names_and_paths:
246
+ if isinstance(extensions, str):
247
+ folder_paths.folder_names_and_paths[folder_name][1].add(extensions)
248
+ elif isinstance(extensions, Iterable):
249
+ for ext in extensions:
250
+ folder_paths.folder_names_and_paths[folder_name][1].add(ext)
251
+
252
+
253
+ def try_mkdir(full_path: str):
254
+ try:
255
+ Path(full_path).mkdir()
256
+ except Exception:
257
+ pass
258
+
259
+
260
+ # register motion models folder(s)
261
+ folder_paths.add_model_folder_path(Folders.ANIMATEDIFF_MODELS, str(Path(__file__).parent.parent / "models"))
262
+ folder_paths.add_model_folder_path(Folders.ANIMATEDIFF_MODELS, str(Path(folder_paths.models_dir) / Folders.ANIMATEDIFF_MODELS))
263
+ add_extension_to_folder_path(Folders.ANIMATEDIFF_MODELS, folder_paths.supported_pt_extensions)
264
+ try_mkdir(str(Path(folder_paths.models_dir) / Folders.ANIMATEDIFF_MODELS))
265
+
266
+ # register motion LoRA folder(s)
267
+ folder_paths.add_model_folder_path(Folders.MOTION_LORA, str(Path(__file__).parent.parent / "motion_lora"))
268
+ folder_paths.add_model_folder_path(Folders.MOTION_LORA, str(Path(folder_paths.models_dir) / Folders.MOTION_LORA))
269
+ add_extension_to_folder_path(Folders.MOTION_LORA, folder_paths.supported_pt_extensions)
270
+ try_mkdir(str(Path(folder_paths.models_dir) / Folders.MOTION_LORA))
271
+
272
+ # register video_formats folder
273
+ folder_paths.add_model_folder_path(Folders.VIDEO_FORMATS, str(Path(__file__).parent.parent / "video_formats"))
274
+ add_extension_to_folder_path(Folders.VIDEO_FORMATS, ".json")
275
+
276
+
277
+ def get_available_motion_models():
278
+ return folder_paths.get_filename_list(Folders.ANIMATEDIFF_MODELS)
279
+
280
+
281
+ def get_motion_model_path(model_name: str):
282
+ return folder_paths.get_full_path(Folders.ANIMATEDIFF_MODELS, model_name)
283
+
284
+
285
+ def get_available_motion_loras():
286
+ return folder_paths.get_filename_list(Folders.MOTION_LORA)
287
+
288
+
289
+ def get_motion_lora_path(lora_name: str):
290
+ return folder_paths.get_full_path(Folders.MOTION_LORA, lora_name)
291
+
292
+
293
+ # modified from https://stackoverflow.com/questions/22058048/hashing-a-file-in-python
294
+ def calculate_file_hash(filename: str, hash_every_n: int = 50):
295
+ h = hashlib.sha256()
296
+ b = bytearray(1024*1024)
297
+ mv = memoryview(b)
298
+ with open(filename, 'rb', buffering=0) as f:
299
+ i = 0
300
+ # don't hash entire file, only portions of it
301
+ while n := f.readinto(mv):
302
+ if i%hash_every_n == 0:
303
+ h.update(mv[:n])
304
+ i += 1
305
+ return h.hexdigest()
306
+
307
+
308
+ def calculate_model_hash(model: ModelPatcher):
309
+ unet = model.model.diff
310
+ t = unet.input_blocks[1]
311
+ m = hashlib.sha256()
312
+ for buf in t.buffers():
313
+ m.update(buf.cpu().numpy().view(np.uint8))
314
+ return m.hexdigest()
315
+
316
+
317
+ class ModelTypeSD:
318
+ SD1_5 = "SD1.5"
319
+ SD2_1 = "SD2.1"
320
+ SDXL = "SDXL"
321
+ SDXL_REFINER = "SDXL_Refiner"
322
+ SVD = "SVD"
323
+
324
+
325
+ def get_sd_model_type(model: ModelPatcher) -> str:
326
+ if model is None:
327
+ return None
328
+ elif type(model.model) == BaseModel:
329
+ return ModelTypeSD.SD1_5
330
+ elif type(model.model) == SDXL:
331
+ return ModelTypeSD.SDXL
332
+ elif type(model.model) == SD21UNCLIP:
333
+ return ModelTypeSD.SD2_1
334
+ elif type(model.model) == SDXLRefiner:
335
+ return ModelTypeSD.SDXL_REFINER
336
+ elif type(model.model) == SVD_img2vid:
337
+ return ModelTypeSD.SVD
338
+ else:
339
+ return str(type(model.model).__name__)
340
+
341
+ def is_checkpoint_sd1_5(model: ModelPatcher):
342
+ return False if model is None else type(model.model) == BaseModel
343
+
344
+ def is_checkpoint_sdxl(model: ModelPatcher):
345
+ return False if model is None else type(model.model) == SDXL
346
+
347
+
348
+ def raise_if_not_checkpoint_sd1_5(model: ModelPatcher):
349
+ if not is_checkpoint_sd1_5(model):
350
+ raise ValueError(f"For AnimateDiff, SD Checkpoint (model) is expected to be SD1.5-based (BaseModel), but was: {type(model.model).__name__}")
351
+
352
+
353
+ # TODO: remove this filth when xformers bug gets fixed in future xformers version
354
+ def wrap_function_to_inject_xformers_bug_info(function_to_wrap: Callable) -> Callable:
355
+ if not xformers_enabled:
356
+ return function_to_wrap
357
+ else:
358
+ def wrapped_function(*args, **kwargs):
359
+ try:
360
+ return function_to_wrap(*args, **kwargs)
361
+ except RuntimeError as e:
362
+ if str(e).startswith("CUDA error: invalid configuration argument"):
363
+ raise RuntimeError(f"An xformers bug was encountered in AnimateDiff - this is unexpected, \
364
+ report this to Kosinkadink/ComfyUI-AnimateDiff-Evolved repo as an issue, \
365
+ and a workaround for now is to run ComfyUI with the --disable-xformers argument.")
366
+ raise
367
+ return wrapped_function
368
+
369
+
370
+ class Timer(object):
371
+ __slots__ = ("start_time", "end_time")
372
+
373
+ def __init__(self) -> None:
374
+ self.start_time = 0.0
375
+ self.end_time = 0.0
376
+
377
+ def start(self) -> None:
378
+ self.start_time = time()
379
+
380
+ def update(self) -> None:
381
+ self.start()
382
+
383
+ def stop(self) -> float:
384
+ self.end_time = time()
385
+ return self.get_time_diff()
386
+
387
+ def get_time_diff(self) -> float:
388
+ return self.end_time - self.start_time
389
+
390
+ def get_time_current(self) -> float:
391
+ return time() - self.start_time
392
+
393
+
394
+ # TODO: possibly add configuration file in future when needed?
395
+ # # Load config settings
396
+ # ADE_DIR = Path(__file__).parent.parent
397
+ # ADE_CONFIG_FILE = ADE_DIR / "ade_config.json"
398
+
399
+ # class ADE_Settings:
400
+ # USE_XFORMERS_IN_VERSATILE_ATTENTION = "use_xformers_in_VersatileAttention"
401
+
402
+ # # Create ADE config if not present
403
+ # ABS_CONFIG = {
404
+ # ADE_Settings.USE_XFORMERS_IN_VERSATILE_ATTENTION: True
405
+ # }
406
+ # if not ADE_CONFIG_FILE.exists():
407
+ # with ADE_CONFIG_FILE.open("w") as f:
408
+ # json.dumps(ABS_CONFIG, indent=4)
409
+ # # otherwise, load it and use values
410
+ # else:
411
+ # loaded_values: dict = None
412
+ # with ADE_CONFIG_FILE.open("r") as f:
413
+ # loaded_values = json.load(f)
414
+ # if loaded_values is not None:
415
+ # for key, value in loaded_values.items():
416
+ # if key in ABS_CONFIG:
417
+ # ABS_CONFIG[key] = value
custom_nodes/ComfyUI-AnimateDiff-Evolved/animatediff/utils_motion.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Union
2
+ import torch
3
+ import torch.nn.functional as F
4
+ from torch import Tensor, nn
5
+
6
+ import comfy.model_management as model_management
7
+ import comfy.ops
8
+ import comfy.utils
9
+ from comfy.cli_args import args
10
+ from comfy.ldm.modules.attention import attention_basic, attention_pytorch, attention_split, attention_sub_quad, default
11
+
12
+ from .logger import logger
13
+
14
+
15
+ # until xformers bug is fixed, do not use xformers for VersatileAttention! TODO: change this when fix is out
16
+ # logic for choosing optimized_attention method taken from comfy/ldm/modules/attention.py
17
+ optimized_attention_mm = attention_basic
18
+ if model_management.xformers_enabled():
19
+ pass
20
+ #optimized_attention_mm = attention_xformers
21
+ if model_management.pytorch_attention_enabled():
22
+ optimized_attention_mm = attention_pytorch
23
+ else:
24
+ if args.use_split_cross_attention:
25
+ optimized_attention_mm = attention_split
26
+ else:
27
+ optimized_attention_mm = attention_sub_quad
28
+
29
+
30
+ class CrossAttentionMM(nn.Module):
31
+ def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0., dtype=None, device=None,
32
+ operations=comfy.ops.disable_weight_init):
33
+ super().__init__()
34
+ inner_dim = dim_head * heads
35
+ context_dim = default(context_dim, query_dim)
36
+
37
+ self.heads = heads
38
+ self.dim_head = dim_head
39
+ self.scale = None
40
+ self.default_scale = dim_head ** -0.5
41
+
42
+ self.to_q = operations.Linear(query_dim, inner_dim, bias=False, dtype=dtype, device=device)
43
+ self.to_k = operations.Linear(context_dim, inner_dim, bias=False, dtype=dtype, device=device)
44
+ self.to_v = operations.Linear(context_dim, inner_dim, bias=False, dtype=dtype, device=device)
45
+
46
+ self.to_out = nn.Sequential(operations.Linear(inner_dim, query_dim, dtype=dtype, device=device), nn.Dropout(dropout))
47
+
48
+ def forward(self, x, context=None, value=None, mask=None, scale_mask=None):
49
+ q = self.to_q(x)
50
+ context = default(context, x)
51
+ k: Tensor = self.to_k(context)
52
+ if value is not None:
53
+ v = self.to_v(value)
54
+ del value
55
+ else:
56
+ v = self.to_v(context)
57
+
58
+ # apply custom scale by multiplying k by scale factor
59
+ if self.scale is not None:
60
+ k *= self.scale
61
+
62
+ # apply scale mask, if present
63
+ if scale_mask is not None:
64
+ k *= scale_mask
65
+
66
+ out = optimized_attention_mm(q, k, v, self.heads, mask)
67
+ return self.to_out(out)
68
+
69
+ # TODO: set up comfy.ops style classes for groupnorm and other functions
70
+ class GroupNormAD(torch.nn.GroupNorm):
71
+ def __init__(self, num_groups: int, num_channels: int, eps: float = 1e-5, affine: bool = True,
72
+ device=None, dtype=None) -> None:
73
+ super().__init__(num_groups=num_groups, num_channels=num_channels, eps=eps, affine=affine, device=device, dtype=dtype)
74
+
75
+ def forward(self, input: Tensor) -> Tensor:
76
+ return F.group_norm(
77
+ input, self.num_groups, self.weight, self.bias, self.eps)
78
+
79
+
80
+ # applies min-max normalization, from:
81
+ # https://stackoverflow.com/questions/68791508/min-max-normalization-of-a-tensor-in-pytorch
82
+ def normalize_min_max(x: Tensor, new_min = 0.0, new_max = 1.0):
83
+ return linear_conversion(x, x_min=x.min(), x_max=x.max(), new_min=new_min, new_max=new_max)
84
+
85
+
86
+ def linear_conversion(x, x_min=0.0, x_max=1.0, new_min=0.0, new_max=1.0):
87
+ x_min = float(x_min)
88
+ x_max = float(x_max)
89
+ new_min = float(new_min)
90
+ new_max = float(new_max)
91
+ return (((x - x_min)/(x_max - x_min)) * (new_max - new_min)) + new_min
92
+
93
+
94
+ # adapted from comfy/sample.py
95
+ def prepare_mask_batch(mask: Tensor, shape: Tensor, multiplier: int=1, match_dim1=False):
96
+ mask = mask.clone()
97
+ mask = torch.nn.functional.interpolate(mask.reshape((-1, 1, mask.shape[-2], mask.shape[-1])), size=(shape[2]*multiplier, shape[3]*multiplier), mode="bilinear")
98
+ if match_dim1:
99
+ mask = torch.cat([mask] * shape[1], dim=1)
100
+ return mask
101
+
102
+
103
+ def extend_to_batch_size(tensor: Tensor, batch_size: int):
104
+ if tensor.shape[0] > batch_size:
105
+ return tensor[:batch_size]
106
+ elif tensor.shape[0] < batch_size:
107
+ remainder = batch_size-tensor.shape[0]
108
+ return torch.cat([tensor] + [tensor[-1:]]*remainder, dim=0)
109
+ return tensor
110
+
111
+
112
+ def get_sorted_list_via_attr(objects: list, attr: str) -> list:
113
+ if not objects:
114
+ return objects
115
+ elif len(objects) <= 1:
116
+ return [x for x in objects]
117
+ # now that we know we have to sort, do it following these rules:
118
+ # a) if objects have same value of attribute, maintain their relative order
119
+ # b) perform sorting of the groups of objects with same attributes
120
+ unique_attrs = {}
121
+ for o in objects:
122
+ val_attr = getattr(o, attr)
123
+ attr_list = unique_attrs.get(val_attr, list())
124
+ attr_list.append(o)
125
+ if val_attr not in unique_attrs:
126
+ unique_attrs[val_attr] = attr_list
127
+ # now that we have the unique attr values grouped together in relative order, sort them by key
128
+ sorted_attrs = dict(sorted(unique_attrs.items()))
129
+ # now flatten out the dict into a list to return
130
+ sorted_list = []
131
+ for object_list in sorted_attrs.values():
132
+ sorted_list.extend(object_list)
133
+ return sorted_list
134
+
135
+
136
+ class MotionCompatibilityError(ValueError):
137
+ pass
138
+
139
+
140
+ def get_combined_multival(multivalA: Union[float, Tensor], multivalB: Union[float, Tensor]) -> Union[float, Tensor]:
141
+ # if one is None, use the other
142
+ if multivalA == None:
143
+ return multivalB
144
+ elif multivalB == None:
145
+ return multivalA
146
+ # both have a value - combine them based on type
147
+ # if both are Tensors, make dims match before multiplying
148
+ if type(multivalA) == Tensor and type(multivalB) == Tensor:
149
+ areaA = multivalA.shape[1]*multivalA.shape[2]
150
+ areaB = multivalB.shape[1]*multivalB.shape[2]
151
+ # match height/width to mask with larger area
152
+ leader,follower = multivalA,multivalB if areaA >= areaB else multivalB,multivalA
153
+ batch_size = multivalA.shape[0] if multivalA.shape[0] >= multivalB.shape[0] else multivalB.shape[0]
154
+ # make follower same dimensions as leader
155
+ follower = torch.unsqueeze(follower, 1)
156
+ follower = comfy.utils.common_upscale(follower, leader.shape[2], leader.shape[1], "bilinear", "center")
157
+ follower = torch.squeeze(follower, 1)
158
+ # make sure batch size will match
159
+ leader = extend_to_batch_size(leader, batch_size)
160
+ follower = extend_to_batch_size(follower, batch_size)
161
+ return leader * follower
162
+ # otherwise, just multiply them together - one of them is a float
163
+ return multivalA * multivalB
164
+
165
+
166
+ class ADKeyframe:
167
+ def __init__(self,
168
+ start_percent: float = 0.0,
169
+ scale_multival: Union[float, Tensor]=None,
170
+ effect_multival: Union[float, Tensor]=None,
171
+ inherit_missing: bool=True,
172
+ guarantee_steps: int=1,
173
+ default: bool=False,
174
+ ):
175
+ self.start_percent = start_percent
176
+ self.start_t = 999999999.9
177
+ self.scale_multival = scale_multival
178
+ self.effect_multival = effect_multival
179
+ self.inherit_missing = inherit_missing
180
+ self.guarantee_steps = guarantee_steps
181
+ self.default = default
182
+
183
+ def has_scale(self):
184
+ return self.scale_multival is not None
185
+
186
+ def has_effect(self):
187
+ return self.effect_multival is not None
188
+
189
+
190
+ class ADKeyframeGroup:
191
+ def __init__(self):
192
+ self.keyframes: list[ADKeyframe] = []
193
+ self.keyframes.append(ADKeyframe(guarantee_steps=1, default=True))
194
+
195
+ def add(self, keyframe: ADKeyframe):
196
+ # remove any default keyframes that match start_percent of new keyframe
197
+ default_to_delete = []
198
+ for i in range(len(self.keyframes)):
199
+ if self.keyframes[i].default and self.keyframes[i].start_percent == keyframe.start_percent:
200
+ default_to_delete.append(i)
201
+ for i in reversed(default_to_delete):
202
+ self.keyframes.pop(i)
203
+ # add to end of list, then sort
204
+ self.keyframes.append(keyframe)
205
+ self.keyframes = get_sorted_list_via_attr(self.keyframes, "start_percent")
206
+
207
+ def get_index(self, index: int) -> Union[ADKeyframe, None]:
208
+ try:
209
+ return self.keyframes[index]
210
+ except IndexError:
211
+ return None
212
+
213
+ def has_index(self, index: int) -> int:
214
+ return index >=0 and index < len(self.keyframes)
215
+
216
+ def __getitem__(self, index) -> ADKeyframe:
217
+ return self.keyframes[index]
218
+
219
+ def __len__(self) -> int:
220
+ return len(self.keyframes)
221
+
222
+ def is_empty(self) -> bool:
223
+ return len(self.keyframes) == 0
224
+
225
+ def clone(self) -> 'ADKeyframeGroup':
226
+ cloned = ADKeyframeGroup()
227
+ for tk in self.keyframes:
228
+ if not tk.default:
229
+ cloned.add(tk)
230
+ return cloned
custom_nodes/ComfyUI-AnimateDiff-Evolved/models/.gitkeep ADDED
File without changes
custom_nodes/ComfyUI-AnimateDiff-Evolved/models/mm_sd_v15_v2.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:69ed0f5fef82b110aca51bcab73b21104242bc65d6ab4b8b2a2a94d31cad1bf0
3
+ size 1817888431
custom_nodes/ComfyUI-AnimateDiff-Evolved/motion_lora/.gitkeep ADDED
File without changes
custom_nodes/ComfyUI-AnimateDiff-Evolved/motion_lora/v2_lora_ZoomIn.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:70ce8b9057b173b9249c48aca5d66c8aa1d8aaa040fda394e50e37f3e278195e
3
+ size 77474499
custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/av1-webm.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "main_pass":
3
+ [
4
+ "-n", "-c:v", "libsvtav1",
5
+ "-pix_fmt", "yuv420p10le",
6
+ "-crf", "23"
7
+ ],
8
+ "extension": "webm",
9
+ "environment": {"SVT_LOG": "1"}
10
+ }
custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/h264-mp4.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "main_pass":
3
+ [
4
+ "-n", "-c:v", "libx264",
5
+ "-pix_fmt", "yuv420p",
6
+ "-crf", "19"
7
+ ],
8
+ "extension": "mp4"
9
+ }
custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/h265-mp4.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "main_pass":
3
+ [
4
+ "-n", "-c:v", "libx265",
5
+ "-pix_fmt", "yuv420p10le",
6
+ "-preset", "medium",
7
+ "-crf", "22",
8
+ "-x265-params", "log-level=quiet"
9
+ ],
10
+ "extension": "mp4"
11
+ }
custom_nodes/ComfyUI-AnimateDiff-Evolved/video_formats/webm.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "main_pass":
3
+ [
4
+ "-n",
5
+ "-pix_fmt", "yuv420p",
6
+ "-crf", "23"
7
+ ],
8
+ "extension": "webm"
9
+ }
custom_nodes/ComfyUI-AnimateDiff-Evolved/web/js/gif_preview.js ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { app } from '../../../scripts/app.js'
2
+ import { api } from '../../../scripts/api.js'
3
+
4
+ function offsetDOMWidget(
5
+ widget,
6
+ ctx,
7
+ node,
8
+ widgetWidth,
9
+ widgetY,
10
+ height
11
+ ) {
12
+ const margin = 10
13
+ const elRect = ctx.canvas.getBoundingClientRect()
14
+ const transform = new DOMMatrix()
15
+ .scaleSelf(
16
+ elRect.width / ctx.canvas.width,
17
+ elRect.height / ctx.canvas.height
18
+ )
19
+ .multiplySelf(ctx.getTransform())
20
+ .translateSelf(0, widgetY + margin)
21
+
22
+ const scale = new DOMMatrix().scaleSelf(transform.a, transform.d)
23
+ Object.assign(widget.inputEl.style, {
24
+ transformOrigin: '0 0',
25
+ transform: scale,
26
+ left: `${transform.e}px`,
27
+ top: `${transform.d + transform.f}px`,
28
+ width: `${widgetWidth}px`,
29
+ height: `${(height || widget.parent?.inputHeight || 32) - margin}px`,
30
+ position: 'absolute',
31
+ background: !node.color ? '' : node.color,
32
+ color: !node.color ? '' : 'white',
33
+ zIndex: 5, //app.graph._nodes.indexOf(node),
34
+ })
35
+ }
36
+
37
+ export const hasWidgets = (node) => {
38
+ if (!node.widgets || !node.widgets?.[Symbol.iterator]) {
39
+ return false
40
+ }
41
+ return true
42
+ }
43
+
44
+ export const cleanupNode = (node) => {
45
+ if (!hasWidgets(node)) {
46
+ return
47
+ }
48
+
49
+ for (const w of node.widgets) {
50
+ if (w.canvas) {
51
+ w.canvas.remove()
52
+ }
53
+ if (w.inputEl) {
54
+ w.inputEl.remove()
55
+ }
56
+ // calls the widget remove callback
57
+ w.onRemoved?.()
58
+ }
59
+ }
60
+
61
+ const CreatePreviewElement = (name, val, format) => {
62
+ const [type] = format.split('/')
63
+ const w = {
64
+ name,
65
+ type,
66
+ value: val,
67
+ draw: function (ctx, node, widgetWidth, widgetY, height) {
68
+ const [cw, ch] = this.computeSize(widgetWidth)
69
+ offsetDOMWidget(this, ctx, node, widgetWidth, widgetY, ch)
70
+ },
71
+ computeSize: function (_) {
72
+ const ratio = this.inputRatio || 1
73
+ const width = Math.max(220, this.parent.size[0])
74
+ return [width, (width / ratio + 10)]
75
+ },
76
+ onRemoved: function () {
77
+ if (this.inputEl) {
78
+ this.inputEl.remove()
79
+ }
80
+ },
81
+ }
82
+
83
+ w.inputEl = document.createElement(type === 'video' ? 'video' : 'img')
84
+ w.inputEl.src = w.value
85
+ if (type === 'video') {
86
+ w.inputEl.setAttribute('type', 'video/webm');
87
+ w.inputEl.autoplay = true
88
+ w.inputEl.loop = true
89
+ w.inputEl.controls = false;
90
+ }
91
+ w.inputEl.onload = function () {
92
+ w.inputRatio = w.inputEl.naturalWidth / w.inputEl.naturalHeight
93
+ }
94
+ document.body.appendChild(w.inputEl)
95
+ return w
96
+ }
97
+
98
+ const gif_preview = {
99
+ name: 'AnimateDiff.gif_preview',
100
+ async beforeRegisterNodeDef(nodeType, nodeData, app) {
101
+ switch (nodeData.name) {
102
+ case 'ADE_AnimateDiffCombine':{
103
+ const onExecuted = nodeType.prototype.onExecuted
104
+ nodeType.prototype.onExecuted = function (message) {
105
+ const prefix = 'ad_gif_preview_'
106
+ const r = onExecuted ? onExecuted.apply(this, message) : undefined
107
+
108
+ if (this.widgets) {
109
+ const pos = this.widgets.findIndex((w) => w.name === `${prefix}_0`)
110
+ if (pos !== -1) {
111
+ for (let i = pos; i < this.widgets.length; i++) {
112
+ this.widgets[i].onRemoved?.()
113
+ }
114
+ this.widgets.length = pos
115
+ }
116
+ if (message?.gifs) {
117
+ message.gifs.forEach((params, i) => {
118
+ const previewUrl = api.apiURL(
119
+ '/view?' + new URLSearchParams(params).toString()
120
+ )
121
+ const w = this.addCustomWidget(
122
+ CreatePreviewElement(`${prefix}_${i}`, previewUrl, params.format || 'image/gif')
123
+ )
124
+ w.parent = this
125
+ })
126
+ }
127
+ const onRemoved = this.onRemoved
128
+ this.onRemoved = () => {
129
+ cleanupNode(this)
130
+ return onRemoved?.()
131
+ }
132
+ }
133
+ this.setSize([this.size[0], this.computeSize([this.size[0], this.size[1]])[1]])
134
+ return r
135
+ }
136
+ break
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ app.registerExtension(gif_preview)
custom_nodes/ComfyUI-Impact-Pack/LICENSE.txt ADDED
@@ -0,0 +1,674 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+ Preamble
9
+
10
+ The GNU General Public License is a free, copyleft license for
11
+ software and other kinds of works.
12
+
13
+ The licenses for most software and other practical works are designed
14
+ to take away your freedom to share and change the works. By contrast,
15
+ the GNU General Public License is intended to guarantee your freedom to
16
+ share and change all versions of a program--to make sure it remains free
17
+ software for all its users. We, the Free Software Foundation, use the
18
+ GNU General Public License for most of our software; it applies also to
19
+ any other work released this way by its authors. You can apply it to
20
+ your programs, too.
21
+
22
+ When we speak of free software, we are referring to freedom, not
23
+ price. Our General Public Licenses are designed to make sure that you
24
+ have the freedom to distribute copies of free software (and charge for
25
+ them if you wish), that you receive source code or can get it if you
26
+ want it, that you can change the software or use pieces of it in new
27
+ free programs, and that you know you can do these things.
28
+
29
+ To protect your rights, we need to prevent others from denying you
30
+ these rights or asking you to surrender the rights. Therefore, you have
31
+ certain responsibilities if you distribute copies of the software, or if
32
+ you modify it: responsibilities to respect the freedom of others.
33
+
34
+ For example, if you distribute copies of such a program, whether
35
+ gratis or for a fee, you must pass on to the recipients the same
36
+ freedoms that you received. You must make sure that they, too, receive
37
+ or can get the source code. And you must show them these terms so they
38
+ know their rights.
39
+
40
+ Developers that use the GNU GPL protect your rights with two steps:
41
+ (1) assert copyright on the software, and (2) offer you this License
42
+ giving you legal permission to copy, distribute and/or modify it.
43
+
44
+ For the developers' and authors' protection, the GPL clearly explains
45
+ that there is no warranty for this free software. For both users' and
46
+ authors' sake, the GPL requires that modified versions be marked as
47
+ changed, so that their problems will not be attributed erroneously to
48
+ authors of previous versions.
49
+
50
+ Some devices are designed to deny users access to install or run
51
+ modified versions of the software inside them, although the manufacturer
52
+ can do so. This is fundamentally incompatible with the aim of
53
+ protecting users' freedom to change the software. The systematic
54
+ pattern of such abuse occurs in the area of products for individuals to
55
+ use, which is precisely where it is most unacceptable. Therefore, we
56
+ have designed this version of the GPL to prohibit the practice for those
57
+ products. If such problems arise substantially in other domains, we
58
+ stand ready to extend this provision to those domains in future versions
59
+ of the GPL, as needed to protect the freedom of users.
60
+
61
+ Finally, every program is threatened constantly by software patents.
62
+ States should not allow patents to restrict development and use of
63
+ software on general-purpose computers, but in those that do, we wish to
64
+ avoid the special danger that patents applied to a free program could
65
+ make it effectively proprietary. To prevent this, the GPL assures that
66
+ patents cannot be used to render the program non-free.
67
+
68
+ The precise terms and conditions for copying, distribution and
69
+ modification follow.
70
+
71
+ TERMS AND CONDITIONS
72
+
73
+ 0. Definitions.
74
+
75
+ "This License" refers to version 3 of the GNU General Public License.
76
+
77
+ "Copyright" also means copyright-like laws that apply to other kinds of
78
+ works, such as semiconductor masks.
79
+
80
+ "The Program" refers to any copyrightable work licensed under this
81
+ License. Each licensee is addressed as "you". "Licensees" and
82
+ "recipients" may be individuals or organizations.
83
+
84
+ To "modify" a work means to copy from or adapt all or part of the work
85
+ in a fashion requiring copyright permission, other than the making of an
86
+ exact copy. The resulting work is called a "modified version" of the
87
+ earlier work or a work "based on" the earlier work.
88
+
89
+ A "covered work" means either the unmodified Program or a work based
90
+ on the Program.
91
+
92
+ To "propagate" a work means to do anything with it that, without
93
+ permission, would make you directly or secondarily liable for
94
+ infringement under applicable copyright law, except executing it on a
95
+ computer or modifying a private copy. Propagation includes copying,
96
+ distribution (with or without modification), making available to the
97
+ public, and in some countries other activities as well.
98
+
99
+ To "convey" a work means any kind of propagation that enables other
100
+ parties to make or receive copies. Mere interaction with a user through
101
+ a computer network, with no transfer of a copy, is not conveying.
102
+
103
+ An interactive user interface displays "Appropriate Legal Notices"
104
+ to the extent that it includes a convenient and prominently visible
105
+ feature that (1) displays an appropriate copyright notice, and (2)
106
+ tells the user that there is no warranty for the work (except to the
107
+ extent that warranties are provided), that licensees may convey the
108
+ work under this License, and how to view a copy of this License. If
109
+ the interface presents a list of user commands or options, such as a
110
+ menu, a prominent item in the list meets this criterion.
111
+
112
+ 1. Source Code.
113
+
114
+ The "source code" for a work means the preferred form of the work
115
+ for making modifications to it. "Object code" means any non-source
116
+ form of a work.
117
+
118
+ A "Standard Interface" means an interface that either is an official
119
+ standard defined by a recognized standards body, or, in the case of
120
+ interfaces specified for a particular programming language, one that
121
+ is widely used among developers working in that language.
122
+
123
+ The "System Libraries" of an executable work include anything, other
124
+ than the work as a whole, that (a) is included in the normal form of
125
+ packaging a Major Component, but which is not part of that Major
126
+ Component, and (b) serves only to enable use of the work with that
127
+ Major Component, or to implement a Standard Interface for which an
128
+ implementation is available to the public in source code form. A
129
+ "Major Component", in this context, means a major essential component
130
+ (kernel, window system, and so on) of the specific operating system
131
+ (if any) on which the executable work runs, or a compiler used to
132
+ produce the work, or an object code interpreter used to run it.
133
+
134
+ The "Corresponding Source" for a work in object code form means all
135
+ the source code needed to generate, install, and (for an executable
136
+ work) run the object code and to modify the work, including scripts to
137
+ control those activities. However, it does not include the work's
138
+ System Libraries, or general-purpose tools or generally available free
139
+ programs which are used unmodified in performing those activities but
140
+ which are not part of the work. For example, Corresponding Source
141
+ includes interface definition files associated with source files for
142
+ the work, and the source code for shared libraries and dynamically
143
+ linked subprograms that the work is specifically designed to require,
144
+ such as by intimate data communication or control flow between those
145
+ subprograms and other parts of the work.
146
+
147
+ The Corresponding Source need not include anything that users
148
+ can regenerate automatically from other parts of the Corresponding
149
+ Source.
150
+
151
+ The Corresponding Source for a work in source code form is that
152
+ same work.
153
+
154
+ 2. Basic Permissions.
155
+
156
+ All rights granted under this License are granted for the term of
157
+ copyright on the Program, and are irrevocable provided the stated
158
+ conditions are met. This License explicitly affirms your unlimited
159
+ permission to run the unmodified Program. The output from running a
160
+ covered work is covered by this License only if the output, given its
161
+ content, constitutes a covered work. This License acknowledges your
162
+ rights of fair use or other equivalent, as provided by copyright law.
163
+
164
+ You may make, run and propagate covered works that you do not
165
+ convey, without conditions so long as your license otherwise remains
166
+ in force. You may convey covered works to others for the sole purpose
167
+ of having them make modifications exclusively for you, or provide you
168
+ with facilities for running those works, provided that you comply with
169
+ the terms of this License in conveying all material for which you do
170
+ not control copyright. Those thus making or running the covered works
171
+ for you must do so exclusively on your behalf, under your direction
172
+ and control, on terms that prohibit them from making any copies of
173
+ your copyrighted material outside their relationship with you.
174
+
175
+ Conveying under any other circumstances is permitted solely under
176
+ the conditions stated below. Sublicensing is not allowed; section 10
177
+ makes it unnecessary.
178
+
179
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180
+
181
+ No covered work shall be deemed part of an effective technological
182
+ measure under any applicable law fulfilling obligations under article
183
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184
+ similar laws prohibiting or restricting circumvention of such
185
+ measures.
186
+
187
+ When you convey a covered work, you waive any legal power to forbid
188
+ circumvention of technological measures to the extent such circumvention
189
+ is effected by exercising rights under this License with respect to
190
+ the covered work, and you disclaim any intention to limit operation or
191
+ modification of the work as a means of enforcing, against the work's
192
+ users, your or third parties' legal rights to forbid circumvention of
193
+ technological measures.
194
+
195
+ 4. Conveying Verbatim Copies.
196
+
197
+ You may convey verbatim copies of the Program's source code as you
198
+ receive it, in any medium, provided that you conspicuously and
199
+ appropriately publish on each copy an appropriate copyright notice;
200
+ keep intact all notices stating that this License and any
201
+ non-permissive terms added in accord with section 7 apply to the code;
202
+ keep intact all notices of the absence of any warranty; and give all
203
+ recipients a copy of this License along with the Program.
204
+
205
+ You may charge any price or no price for each copy that you convey,
206
+ and you may offer support or warranty protection for a fee.
207
+
208
+ 5. Conveying Modified Source Versions.
209
+
210
+ You may convey a work based on the Program, or the modifications to
211
+ produce it from the Program, in the form of source code under the
212
+ terms of section 4, provided that you also meet all of these conditions:
213
+
214
+ a) The work must carry prominent notices stating that you modified
215
+ it, and giving a relevant date.
216
+
217
+ b) The work must carry prominent notices stating that it is
218
+ released under this License and any conditions added under section
219
+ 7. This requirement modifies the requirement in section 4 to
220
+ "keep intact all notices".
221
+
222
+ c) You must license the entire work, as a whole, under this
223
+ License to anyone who comes into possession of a copy. This
224
+ License will therefore apply, along with any applicable section 7
225
+ additional terms, to the whole of the work, and all its parts,
226
+ regardless of how they are packaged. This License gives no
227
+ permission to license the work in any other way, but it does not
228
+ invalidate such permission if you have separately received it.
229
+
230
+ d) If the work has interactive user interfaces, each must display
231
+ Appropriate Legal Notices; however, if the Program has interactive
232
+ interfaces that do not display Appropriate Legal Notices, your
233
+ work need not make them do so.
234
+
235
+ A compilation of a covered work with other separate and independent
236
+ works, which are not by their nature extensions of the covered work,
237
+ and which are not combined with it such as to form a larger program,
238
+ in or on a volume of a storage or distribution medium, is called an
239
+ "aggregate" if the compilation and its resulting copyright are not
240
+ used to limit the access or legal rights of the compilation's users
241
+ beyond what the individual works permit. Inclusion of a covered work
242
+ in an aggregate does not cause this License to apply to the other
243
+ parts of the aggregate.
244
+
245
+ 6. Conveying Non-Source Forms.
246
+
247
+ You may convey a covered work in object code form under the terms
248
+ of sections 4 and 5, provided that you also convey the
249
+ machine-readable Corresponding Source under the terms of this License,
250
+ in one of these ways:
251
+
252
+ a) Convey the object code in, or embodied in, a physical product
253
+ (including a physical distribution medium), accompanied by the
254
+ Corresponding Source fixed on a durable physical medium
255
+ customarily used for software interchange.
256
+
257
+ b) Convey the object code in, or embodied in, a physical product
258
+ (including a physical distribution medium), accompanied by a
259
+ written offer, valid for at least three years and valid for as
260
+ long as you offer spare parts or customer support for that product
261
+ model, to give anyone who possesses the object code either (1) a
262
+ copy of the Corresponding Source for all the software in the
263
+ product that is covered by this License, on a durable physical
264
+ medium customarily used for software interchange, for a price no
265
+ more than your reasonable cost of physically performing this
266
+ conveying of source, or (2) access to copy the
267
+ Corresponding Source from a network server at no charge.
268
+
269
+ c) Convey individual copies of the object code with a copy of the
270
+ written offer to provide the Corresponding Source. This
271
+ alternative is allowed only occasionally and noncommercially, and
272
+ only if you received the object code with such an offer, in accord
273
+ with subsection 6b.
274
+
275
+ d) Convey the object code by offering access from a designated
276
+ place (gratis or for a charge), and offer equivalent access to the
277
+ Corresponding Source in the same way through the same place at no
278
+ further charge. You need not require recipients to copy the
279
+ Corresponding Source along with the object code. If the place to
280
+ copy the object code is a network server, the Corresponding Source
281
+ may be on a different server (operated by you or a third party)
282
+ that supports equivalent copying facilities, provided you maintain
283
+ clear directions next to the object code saying where to find the
284
+ Corresponding Source. Regardless of what server hosts the
285
+ Corresponding Source, you remain obligated to ensure that it is
286
+ available for as long as needed to satisfy these requirements.
287
+
288
+ e) Convey the object code using peer-to-peer transmission, provided
289
+ you inform other peers where the object code and Corresponding
290
+ Source of the work are being offered to the general public at no
291
+ charge under subsection 6d.
292
+
293
+ A separable portion of the object code, whose source code is excluded
294
+ from the Corresponding Source as a System Library, need not be
295
+ included in conveying the object code work.
296
+
297
+ A "User Product" is either (1) a "consumer product", which means any
298
+ tangible personal property which is normally used for personal, family,
299
+ or household purposes, or (2) anything designed or sold for incorporation
300
+ into a dwelling. In determining whether a product is a consumer product,
301
+ doubtful cases shall be resolved in favor of coverage. For a particular
302
+ product received by a particular user, "normally used" refers to a
303
+ typical or common use of that class of product, regardless of the status
304
+ of the particular user or of the way in which the particular user
305
+ actually uses, or expects or is expected to use, the product. A product
306
+ is a consumer product regardless of whether the product has substantial
307
+ commercial, industrial or non-consumer uses, unless such uses represent
308
+ the only significant mode of use of the product.
309
+
310
+ "Installation Information" for a User Product means any methods,
311
+ procedures, authorization keys, or other information required to install
312
+ and execute modified versions of a covered work in that User Product from
313
+ a modified version of its Corresponding Source. The information must
314
+ suffice to ensure that the continued functioning of the modified object
315
+ code is in no case prevented or interfered with solely because
316
+ modification has been made.
317
+
318
+ If you convey an object code work under this section in, or with, or
319
+ specifically for use in, a User Product, and the conveying occurs as
320
+ part of a transaction in which the right of possession and use of the
321
+ User Product is transferred to the recipient in perpetuity or for a
322
+ fixed term (regardless of how the transaction is characterized), the
323
+ Corresponding Source conveyed under this section must be accompanied
324
+ by the Installation Information. But this requirement does not apply
325
+ if neither you nor any third party retains the ability to install
326
+ modified object code on the User Product (for example, the work has
327
+ been installed in ROM).
328
+
329
+ The requirement to provide Installation Information does not include a
330
+ requirement to continue to provide support service, warranty, or updates
331
+ for a work that has been modified or installed by the recipient, or for
332
+ the User Product in which it has been modified or installed. Access to a
333
+ network may be denied when the modification itself materially and
334
+ adversely affects the operation of the network or violates the rules and
335
+ protocols for communication across the network.
336
+
337
+ Corresponding Source conveyed, and Installation Information provided,
338
+ in accord with this section must be in a format that is publicly
339
+ documented (and with an implementation available to the public in
340
+ source code form), and must require no special password or key for
341
+ unpacking, reading or copying.
342
+
343
+ 7. Additional Terms.
344
+
345
+ "Additional permissions" are terms that supplement the terms of this
346
+ License by making exceptions from one or more of its conditions.
347
+ Additional permissions that are applicable to the entire Program shall
348
+ be treated as though they were included in this License, to the extent
349
+ that they are valid under applicable law. If additional permissions
350
+ apply only to part of the Program, that part may be used separately
351
+ under those permissions, but the entire Program remains governed by
352
+ this License without regard to the additional permissions.
353
+
354
+ When you convey a copy of a covered work, you may at your option
355
+ remove any additional permissions from that copy, or from any part of
356
+ it. (Additional permissions may be written to require their own
357
+ removal in certain cases when you modify the work.) You may place
358
+ additional permissions on material, added by you to a covered work,
359
+ for which you have or can give appropriate copyright permission.
360
+
361
+ Notwithstanding any other provision of this License, for material you
362
+ add to a covered work, you may (if authorized by the copyright holders of
363
+ that material) supplement the terms of this License with terms:
364
+
365
+ a) Disclaiming warranty or limiting liability differently from the
366
+ terms of sections 15 and 16 of this License; or
367
+
368
+ b) Requiring preservation of specified reasonable legal notices or
369
+ author attributions in that material or in the Appropriate Legal
370
+ Notices displayed by works containing it; or
371
+
372
+ c) Prohibiting misrepresentation of the origin of that material, or
373
+ requiring that modified versions of such material be marked in
374
+ reasonable ways as different from the original version; or
375
+
376
+ d) Limiting the use for publicity purposes of names of licensors or
377
+ authors of the material; or
378
+
379
+ e) Declining to grant rights under trademark law for use of some
380
+ trade names, trademarks, or service marks; or
381
+
382
+ f) Requiring indemnification of licensors and authors of that
383
+ material by anyone who conveys the material (or modified versions of
384
+ it) with contractual assumptions of liability to the recipient, for
385
+ any liability that these contractual assumptions directly impose on
386
+ those licensors and authors.
387
+
388
+ All other non-permissive additional terms are considered "further
389
+ restrictions" within the meaning of section 10. If the Program as you
390
+ received it, or any part of it, contains a notice stating that it is
391
+ governed by this License along with a term that is a further
392
+ restriction, you may remove that term. If a license document contains
393
+ a further restriction but permits relicensing or conveying under this
394
+ License, you may add to a covered work material governed by the terms
395
+ of that license document, provided that the further restriction does
396
+ not survive such relicensing or conveying.
397
+
398
+ If you add terms to a covered work in accord with this section, you
399
+ must place, in the relevant source files, a statement of the
400
+ additional terms that apply to those files, or a notice indicating
401
+ where to find the applicable terms.
402
+
403
+ Additional terms, permissive or non-permissive, may be stated in the
404
+ form of a separately written license, or stated as exceptions;
405
+ the above requirements apply either way.
406
+
407
+ 8. Termination.
408
+
409
+ You may not propagate or modify a covered work except as expressly
410
+ provided under this License. Any attempt otherwise to propagate or
411
+ modify it is void, and will automatically terminate your rights under
412
+ this License (including any patent licenses granted under the third
413
+ paragraph of section 11).
414
+
415
+ However, if you cease all violation of this License, then your
416
+ license from a particular copyright holder is reinstated (a)
417
+ provisionally, unless and until the copyright holder explicitly and
418
+ finally terminates your license, and (b) permanently, if the copyright
419
+ holder fails to notify you of the violation by some reasonable means
420
+ prior to 60 days after the cessation.
421
+
422
+ Moreover, your license from a particular copyright holder is
423
+ reinstated permanently if the copyright holder notifies you of the
424
+ violation by some reasonable means, this is the first time you have
425
+ received notice of violation of this License (for any work) from that
426
+ copyright holder, and you cure the violation prior to 30 days after
427
+ your receipt of the notice.
428
+
429
+ Termination of your rights under this section does not terminate the
430
+ licenses of parties who have received copies or rights from you under
431
+ this License. If your rights have been terminated and not permanently
432
+ reinstated, you do not qualify to receive new licenses for the same
433
+ material under section 10.
434
+
435
+ 9. Acceptance Not Required for Having Copies.
436
+
437
+ You are not required to accept this License in order to receive or
438
+ run a copy of the Program. Ancillary propagation of a covered work
439
+ occurring solely as a consequence of using peer-to-peer transmission
440
+ to receive a copy likewise does not require acceptance. However,
441
+ nothing other than this License grants you permission to propagate or
442
+ modify any covered work. These actions infringe copyright if you do
443
+ not accept this License. Therefore, by modifying or propagating a
444
+ covered work, you indicate your acceptance of this License to do so.
445
+
446
+ 10. Automatic Licensing of Downstream Recipients.
447
+
448
+ Each time you convey a covered work, the recipient automatically
449
+ receives a license from the original licensors, to run, modify and
450
+ propagate that work, subject to this License. You are not responsible
451
+ for enforcing compliance by third parties with this License.
452
+
453
+ An "entity transaction" is a transaction transferring control of an
454
+ organization, or substantially all assets of one, or subdividing an
455
+ organization, or merging organizations. If propagation of a covered
456
+ work results from an entity transaction, each party to that
457
+ transaction who receives a copy of the work also receives whatever
458
+ licenses to the work the party's predecessor in interest had or could
459
+ give under the previous paragraph, plus a right to possession of the
460
+ Corresponding Source of the work from the predecessor in interest, if
461
+ the predecessor has it or can get it with reasonable efforts.
462
+
463
+ You may not impose any further restrictions on the exercise of the
464
+ rights granted or affirmed under this License. For example, you may
465
+ not impose a license fee, royalty, or other charge for exercise of
466
+ rights granted under this License, and you may not initiate litigation
467
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
468
+ any patent claim is infringed by making, using, selling, offering for
469
+ sale, or importing the Program or any portion of it.
470
+
471
+ 11. Patents.
472
+
473
+ A "contributor" is a copyright holder who authorizes use under this
474
+ License of the Program or a work on which the Program is based. The
475
+ work thus licensed is called the contributor's "contributor version".
476
+
477
+ A contributor's "essential patent claims" are all patent claims
478
+ owned or controlled by the contributor, whether already acquired or
479
+ hereafter acquired, that would be infringed by some manner, permitted
480
+ by this License, of making, using, or selling its contributor version,
481
+ but do not include claims that would be infringed only as a
482
+ consequence of further modification of the contributor version. For
483
+ purposes of this definition, "control" includes the right to grant
484
+ patent sublicenses in a manner consistent with the requirements of
485
+ this License.
486
+
487
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
488
+ patent license under the contributor's essential patent claims, to
489
+ make, use, sell, offer for sale, import and otherwise run, modify and
490
+ propagate the contents of its contributor version.
491
+
492
+ In the following three paragraphs, a "patent license" is any express
493
+ agreement or commitment, however denominated, not to enforce a patent
494
+ (such as an express permission to practice a patent or covenant not to
495
+ sue for patent infringement). To "grant" such a patent license to a
496
+ party means to make such an agreement or commitment not to enforce a
497
+ patent against the party.
498
+
499
+ If you convey a covered work, knowingly relying on a patent license,
500
+ and the Corresponding Source of the work is not available for anyone
501
+ to copy, free of charge and under the terms of this License, through a
502
+ publicly available network server or other readily accessible means,
503
+ then you must either (1) cause the Corresponding Source to be so
504
+ available, or (2) arrange to deprive yourself of the benefit of the
505
+ patent license for this particular work, or (3) arrange, in a manner
506
+ consistent with the requirements of this License, to extend the patent
507
+ license to downstream recipients. "Knowingly relying" means you have
508
+ actual knowledge that, but for the patent license, your conveying the
509
+ covered work in a country, or your recipient's use of the covered work
510
+ in a country, would infringe one or more identifiable patents in that
511
+ country that you have reason to believe are valid.
512
+
513
+ If, pursuant to or in connection with a single transaction or
514
+ arrangement, you convey, or propagate by procuring conveyance of, a
515
+ covered work, and grant a patent license to some of the parties
516
+ receiving the covered work authorizing them to use, propagate, modify
517
+ or convey a specific copy of the covered work, then the patent license
518
+ you grant is automatically extended to all recipients of the covered
519
+ work and works based on it.
520
+
521
+ A patent license is "discriminatory" if it does not include within
522
+ the scope of its coverage, prohibits the exercise of, or is
523
+ conditioned on the non-exercise of one or more of the rights that are
524
+ specifically granted under this License. You may not convey a covered
525
+ work if you are a party to an arrangement with a third party that is
526
+ in the business of distributing software, under which you make payment
527
+ to the third party based on the extent of your activity of conveying
528
+ the work, and under which the third party grants, to any of the
529
+ parties who would receive the covered work from you, a discriminatory
530
+ patent license (a) in connection with copies of the covered work
531
+ conveyed by you (or copies made from those copies), or (b) primarily
532
+ for and in connection with specific products or compilations that
533
+ contain the covered work, unless you entered into that arrangement,
534
+ or that patent license was granted, prior to 28 March 2007.
535
+
536
+ Nothing in this License shall be construed as excluding or limiting
537
+ any implied license or other defenses to infringement that may
538
+ otherwise be available to you under applicable patent law.
539
+
540
+ 12. No Surrender of Others' Freedom.
541
+
542
+ If conditions are imposed on you (whether by court order, agreement or
543
+ otherwise) that contradict the conditions of this License, they do not
544
+ excuse you from the conditions of this License. If you cannot convey a
545
+ covered work so as to satisfy simultaneously your obligations under this
546
+ License and any other pertinent obligations, then as a consequence you may
547
+ not convey it at all. For example, if you agree to terms that obligate you
548
+ to collect a royalty for further conveying from those to whom you convey
549
+ the Program, the only way you could satisfy both those terms and this
550
+ License would be to refrain entirely from conveying the Program.
551
+
552
+ 13. Use with the GNU Affero General Public License.
553
+
554
+ Notwithstanding any other provision of this License, you have
555
+ permission to link or combine any covered work with a work licensed
556
+ under version 3 of the GNU Affero General Public License into a single
557
+ combined work, and to convey the resulting work. The terms of this
558
+ License will continue to apply to the part which is the covered work,
559
+ but the special requirements of the GNU Affero General Public License,
560
+ section 13, concerning interaction through a network will apply to the
561
+ combination as such.
562
+
563
+ 14. Revised Versions of this License.
564
+
565
+ The Free Software Foundation may publish revised and/or new versions of
566
+ the GNU General Public License from time to time. Such new versions will
567
+ be similar in spirit to the present version, but may differ in detail to
568
+ address new problems or concerns.
569
+
570
+ Each version is given a distinguishing version number. If the
571
+ Program specifies that a certain numbered version of the GNU General
572
+ Public License "or any later version" applies to it, you have the
573
+ option of following the terms and conditions either of that numbered
574
+ version or of any later version published by the Free Software
575
+ Foundation. If the Program does not specify a version number of the
576
+ GNU General Public License, you may choose any version ever published
577
+ by the Free Software Foundation.
578
+
579
+ If the Program specifies that a proxy can decide which future
580
+ versions of the GNU General Public License can be used, that proxy's
581
+ public statement of acceptance of a version permanently authorizes you
582
+ to choose that version for the Program.
583
+
584
+ Later license versions may give you additional or different
585
+ permissions. However, no additional obligations are imposed on any
586
+ author or copyright holder as a result of your choosing to follow a
587
+ later version.
588
+
589
+ 15. Disclaimer of Warranty.
590
+
591
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599
+
600
+ 16. Limitation of Liability.
601
+
602
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610
+ SUCH DAMAGES.
611
+
612
+ 17. Interpretation of Sections 15 and 16.
613
+
614
+ If the disclaimer of warranty and limitation of liability provided
615
+ above cannot be given local legal effect according to their terms,
616
+ reviewing courts shall apply local law that most closely approximates
617
+ an absolute waiver of all civil liability in connection with the
618
+ Program, unless a warranty or assumption of liability accompanies a
619
+ copy of the Program in return for a fee.
620
+
621
+ END OF TERMS AND CONDITIONS
622
+
623
+ How to Apply These Terms to Your New Programs
624
+
625
+ If you develop a new program, and you want it to be of the greatest
626
+ possible use to the public, the best way to achieve this is to make it
627
+ free software which everyone can redistribute and change under these terms.
628
+
629
+ To do so, attach the following notices to the program. It is safest
630
+ to attach them to the start of each source file to most effectively
631
+ state the exclusion of warranty; and each file should have at least
632
+ the "copyright" line and a pointer to where the full notice is found.
633
+
634
+ <one line to give the program's name and a brief idea of what it does.>
635
+ Copyright (C) <year> <name of author>
636
+
637
+ This program is free software: you can redistribute it and/or modify
638
+ it under the terms of the GNU General Public License as published by
639
+ the Free Software Foundation, either version 3 of the License, or
640
+ (at your option) any later version.
641
+
642
+ This program is distributed in the hope that it will be useful,
643
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
644
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645
+ GNU General Public License for more details.
646
+
647
+ You should have received a copy of the GNU General Public License
648
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
649
+
650
+ Also add information on how to contact you by electronic and paper mail.
651
+
652
+ If the program does terminal interaction, make it output a short
653
+ notice like this when it starts in an interactive mode:
654
+
655
+ <program> Copyright (C) <year> <name of author>
656
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657
+ This is free software, and you are welcome to redistribute it
658
+ under certain conditions; type `show c' for details.
659
+
660
+ The hypothetical commands `show w' and `show c' should show the appropriate
661
+ parts of the General Public License. Of course, your program's commands
662
+ might be different; for a GUI interface, you would use an "about box".
663
+
664
+ You should also get your employer (if you work as a programmer) or school,
665
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
666
+ For more information on this, and how to apply and follow the GNU GPL, see
667
+ <https://www.gnu.org/licenses/>.
668
+
669
+ The GNU General Public License does not permit incorporating your program
670
+ into proprietary programs. If your program is a subroutine library, you
671
+ may consider it more useful to permit linking proprietary applications with
672
+ the library. If this is what you want to do, use the GNU Lesser General
673
+ Public License instead of this License. But first, please read
674
+ <https://www.gnu.org/licenses/why-not-lgpl.html>.
custom_nodes/ComfyUI-Impact-Pack/README.md ADDED
@@ -0,0 +1,454 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [![Youtube Badge](https://img.shields.io/badge/Youtube-FF0000?style=for-the-badge&logo=Youtube&logoColor=white&link=https://www.youtube.com/watch?v=AccoxDZIg3Y&list=PL_Ej2RDzjQLGfEeizq4GISeY3FtVyFmGP)](https://www.youtube.com/watch?v=AccoxDZIg3Y&list=PL_Ej2RDzjQLGfEeizq4GISeY3FtVyFmGP)
2
+
3
+ # ComfyUI-Impact-Pack
4
+
5
+ **Custom nodes pack for ComfyUI**
6
+ This custom node helps to conveniently enhance images through Detector, Detailer, Upscaler, Pipe, and more.
7
+
8
+
9
+ ## NOTICE
10
+ * V4.77: Compatibility patch applied. Requires ComfyUI version (Oct. 8th) or later.
11
+ * V4.73.3: ControlNetApply (SEGS) supports AnimateDiff
12
+ * V4.20.1: Due to the feature update in `RegionalSampler`, the parameter order has changed, causing malfunctions in previously created `RegionalSamplers`. Please adjust the parameters accordingly.
13
+ * V4.12: `MASKS` is changed to `MASK`.
14
+ * V4.7.2 isn't compatible with old version of `ControlNet Auxiliary Preprocessor`. If you will use `MediaPipe FaceMesh to SEGS` update to latest version(Sep. 17th).
15
+ * Selection weight syntax is changed(: -> ::) since V3.16. ([tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ImpactWildcardProcessor.md))
16
+ * Starting from V3.6, requires latest version(Aug 8, 9ccc965) of ComfyUI.
17
+ * **In versions below V3.3.1, there was an issue with the image quality generated after using the UltralyticsDetectorProvider. Please make sure to upgrade to a newer version.**
18
+ * Starting from V3.0, nodes related to `mmdet` are optional nodes that are activated only based on the configuration settings.
19
+ - Through ComfyUI-Impact-Subpack, you can utilize UltralyticsDetectorProvider to access various detection models.
20
+ * Between versions 2.22 and 2.21, there is partial compatibility loss regarding the Detailer workflow. If you continue to use the existing workflow, errors may occur during execution. An additional output called "enhanced_alpha_list" has been added to Detailer-related nodes.
21
+ * The permission error related to cv2 that occurred during the installation of Impact Pack has been patched in version 2.21.4. However, please note that the latest versions of ComfyUI and ComfyUI-Manager are required.
22
+ * The "PreviewBridge" feature may not function correctly on ComfyUI versions released before July 1, 2023.
23
+ * Attempting to load the "ComfyUI-Impact-Pack" on ComfyUI versions released before June 27, 2023, will result in a failure.
24
+ * With the addition of wildcard support in FaceDetailer, the structure of DETAILER_PIPE-related nodes and Detailer nodes has changed. There may be malfunctions when using the existing workflow.
25
+
26
+
27
+ ## Custom Nodes
28
+ * [Detectors](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/detectors.md)
29
+ * SAMLoader - Loads the SAM model.
30
+ * UltralyticsDetectorProvider - Loads the Ultralystics model to provide SEGM_DETECTOR, BBOX_DETECTOR.
31
+ - Unlike `MMDetDetectorProvider`, for segm models, `BBOX_DETECTOR` is also provided.
32
+ - The various models available in UltralyticsDetectorProvider can be downloaded through **ComfyUI-Manager**.
33
+ * ONNXDetectorProvider - Loads the ONNX model to provide BBOX_DETECTOR.
34
+ * CLIPSegDetectorProvider - Wrapper for CLIPSeg to provide BBOX_DETECTOR.
35
+ * You need to install the ComfyUI-CLIPSeg node extension.
36
+ * SEGM Detector (combined) - Detects segmentation and returns a mask from the input image.
37
+ * BBOX Detector (combined) - Detects bounding boxes and returns a mask from the input image.
38
+ * SAMDetector (combined) - Utilizes the SAM technology to extract the segment at the location indicated by the input SEGS on the input image and outputs it as a unified mask.
39
+ * SAMDetector (Segmented) - It is similar to `SAMDetector (combined)`, but it separates and outputs the detected segments. Multiple segments can be found for the same detected area, and currently, a policy is in place to group them arbitrarily in sets of three. This aspect is expected to be improved in the future.
40
+ * As a result, it outputs the `combined_mask`, which is a unified mask, and `batch_masks`, which are multiple masks grouped together in batch form.
41
+ * While `batch_masks` may not be completely separated, it provides functionality to perform some level of segmentation.
42
+ * Simple Detector (SEGS) - Operating primarily with `BBOX_DETECTOR`, and with the additional provision of `SAM_MODEL` or `SEGM_DETECTOR`, this node internally generates improved SEGS through mask operations on both *bbox* and *silhouette*. It serves as a convenient tool to simplify a somewhat intricate workflow.
43
+
44
+ * ControlNet
45
+ * ControlNetApply (SEGS) - To apply ControlNet in SEGS, you need to use the Preprocessor Provider node from the Inspire Pack to utilize this node.
46
+ * `segs_preprocessor` and `control_image` can be selectively applied. If an `control_image` is given, `segs_preprocessor` will be ignored.
47
+ * If set to `control_image`, you can preview the cropped cnet image through `SEGSPreview (CNET Image)`. Images generated by `segs_preprocessor` should be verified through the `cnet_images` output of each Detailer.
48
+ * The `segs_preprocessor` operates by applying preprocessing on-the-fly based on the cropped image during the detailing process, while `control_image` will be cropped and used as input to `ControlNetApply (SEGS)`.
49
+ * ControlNetClear (SEGS) - Clear applied ControlNet in SEGS
50
+
51
+ * Bitwise(SEGS & SEGS) - Performs a 'bitwise and' operation between two SEGS.
52
+ * Bitwise(SEGS - SEGS) - Subtracts one SEGS from another.
53
+ * Bitwise(SEGS & MASK) - Performs a bitwise AND operation between SEGS and MASK.
54
+ * Bitwise(SEGS & MASKS ForEach) - Performs a bitwise AND operation between SEGS and MASKS.
55
+ * Please note that this operation is performed with batches of MASKS, not just a single MASK.
56
+ * Bitwise(MASK & MASK) - Performs a 'bitwise and' operation between two masks.
57
+ * Bitwise(MASK - MASK) - Subtracts one mask from another.
58
+ * Bitwise(MASK + MASK) - Combine two masks.
59
+ * SEGM Detector (SEGS) - Detects segmentation and returns SEGS from the input image.
60
+ * BBOX Detector (SEGS) - Detects bounding boxes and returns SEGS from the input image.
61
+
62
+ * Detailer
63
+ * Detailer (SEGS) - Refines the image based on SEGS.
64
+ * DetailerDebug (SEGS) - Refines the image based on SEGS. Additionally, it provides the ability to monitor the cropped image and the refined image of the cropped image.
65
+ * To prevent regeneration caused by the seed that does not change every time when using 'external_seed', please disable the 'seed random generate' option in the 'Detailer...' node.
66
+ * MASK to SEGS - Generates SEGS based on the mask.
67
+ * MASK to SEGS For AnimateDiff - Generates SEGS based on the mask for AnimateDiff.
68
+ * MediaPipe FaceMesh to SEGS - Separate each landmark from the mediapipe facemesh image to create labeled SEGS.
69
+ * Usually, the size of images created through the MediaPipe facemesh preprocessor is downscaled. It resizes the MediaPipe facemesh image to the original size given as reference_image_opt for matching sizes during processing.
70
+ * ToBinaryMask - Separates the mask generated with alpha values between 0 and 255 into 0 and 255. The non-zero parts are always set to 255.
71
+ * Masks to Mask List - This node converts the MASKS in batch form to a list of individual masks.
72
+ * Mask List to Masks - This node converts the MASK list to MASK batch form.
73
+ * EmptySEGS - Provides an empty SEGS.
74
+ * MaskPainter - Provides a feature to draw masks.
75
+ * FaceDetailer - Easily detects faces and improves them.
76
+ * FaceDetailer (pipe) - Easily detects faces and improves them (for multipass).
77
+ * MaskDetailer (pipe) - This is a simple inpaint node that applies the Detailer to the mask area.
78
+
79
+ * `FromDetailer (SDXL/pipe), BasicPipe -> DetailerPipe (SDXL), Edit DetailerPipe (SDXL)` - These are pipe functions used in Detailer for utilizing the refiner model of SDXL.
80
+
81
+ * SEGS Manipulation nodes
82
+ * SEGSDetailer - Performs detailed work on SEGS without pasting it back onto the original image.
83
+ * SEGSPaste - Pastes the results of SEGS onto the original image.
84
+ * If `ref_image_opt` is present, the images contained within SEGS are ignored. Instead, the image within `ref_image_opt` corresponding to the crop area of SEGS is taken and pasted. The size of the image in `ref_image_opt` should be the same as the original image size.
85
+ * This node can be used in conjunction with the processing results of AnimateDiff.
86
+ * SEGSPreview - Provides a preview of SEGS.
87
+ * This option is used to preview the improved image through `SEGSDetailer` before merging it into the original. Prior to going through ```SEGSDetailer```, SEGS only contains mask information without image information. If fallback_image_opt is connected to the original image, SEGS without image information will generate a preview using the original image. However, if SEGS already contains image information, fallback_image_opt will be ignored.
88
+ * This node can be used in conjunction with the processing results of AnimateDiff.
89
+ * SEGSPreview (CNET Image) - Show images configured with `ControlNetApply (SEGS)` for debugging purposes.
90
+ * SEGSToImageList - Convert SEGS To Image List
91
+ * SEGSToMaskList - Convert SEGS To Mask List
92
+ * SEGS Filter (label) - This node filters SEGS based on the label of the detected areas.
93
+ * SEGS Filter (ordered) - This node sorts SEGS based on size and position and retrieves SEGs within a certain range.
94
+ * SEGS Filter (range) - This node retrieves only SEGs from SEGS that have a size and position within a certain range.
95
+ * SEGS Assign (label) - Assign labels sequentially to SEGS. This node is useful when used with `[LAB]` of FaceDetailer.
96
+ * SEGSConcat - Concatenate segs1 and segs2. If source shape of segs1 and segs2 are different from segs2 will be ignored.
97
+ * Picker (SEGS) - Among the input SEGS, you can select a specific SEG through a dialog. If no SEG is selected, it outputs an empty SEGS. Increasing the batch_size of SEGSDetailer can be used for the purpose of selecting from the candidates.
98
+ * Set Default Image For SEGS - Set a default image for SEGS. SEGS with images set this way do not need to have a fallback image set. When override is set to false, the original image is preserved.
99
+ * Remove Image from SEGS - Remove the image set for the SEGS that has been configured by "Set Default Image for SEGS" or SEGSDetailer. When the image for the SEGS is removed, the Detailer node will operate based on the currently processed image instead of the SEGS.
100
+ * Make Tile SEGS - [experimental] Create SEGS in the form of tiles from an image to facilitate experiments for Tiled Upscale using the Detailer.
101
+ * The `filter_in_segs_opt` and `filter_out_segs_opt` are optional inputs. If these inputs are provided, when creating the tiles, the mask for each tile is generated by overlapping with the mask of `filter_in_segs_opt` and excluding the overlap with the mask of `filter_out_segs_opt`. Tiles with an empty mask will not be created as SEGS.
102
+ * Dilate Mask (SEGS) - Dilate/Erosion Mask in SEGS
103
+ * Gaussian Blur Mask (SEGS) - Apply Gaussian Blur to Mask in SEGS
104
+ * SEGS_ELT Manipulation - experimental nodes
105
+ * DecomposeSEGS - Decompose SEGS to allow for detailed manipulation.
106
+ * AssembleSEGS - Reassemble the decomposed SEGS.
107
+ * From SEG_ELT - Extract detailed information from SEG_ELT.
108
+ * Edit SEG_ELT - Modify some of the information in SEG_ELT.
109
+ * Dilate SEG_ELT - Dilate the mask of SEG_ELT.
110
+
111
+ * Mask Manipulation
112
+ * Dilate Mask - Dilate Mask.
113
+ * Support erosion for negative value.
114
+ * Gaussian Blur Mask - Apply Gaussian Blur to Mask. You can utilize this for mask feathering.
115
+
116
+ * Pipe nodes
117
+ * ToDetailerPipe, FromDetailerPipe - These nodes are used to bundle multiple inputs used in the detailer, such as models and vae, ..., into a single DETAILER_PIPE or extract the elements that are bundled in the DETAILER_PIPE.
118
+ * ToBasicPipe, FromBasicPipe - These nodes are used to bundle model, clip, vae, positive conditioning, and negative conditioning into a single BASIC_PIPE, or extract each element from the BASIC_PIPE.
119
+ * EditBasicPipe, EditDetailerPipe - These nodes are used to replace some elements in BASIC_PIPE or DETAILER_PIPE.
120
+ * FromDetailerPipe_v2, FromBasicPipe_v2 - It has the same functionality as `FromDetailerPipe` and `FromBasicPipe`, but it has an additional output that directly exports the input pipe. It is useful when editing EditBasicPipe and EditDetailerPipe.
121
+ * Latent Scale (on Pixel Space) - This node converts latent to pixel space, upscales it, and then converts it back to latent.
122
+ * If upscale_model_opt is provided, it uses the model to upscale the pixel and then downscales it using the interpolation method provided in scale_method to the target resolution.
123
+ * PixelKSampleUpscalerProvider - An upscaler is provided that converts latent to pixels using VAEDecode, performs upscaling, converts back to latent using VAEEncode, and then performs k-sampling. This upscaler can be attached to nodes such as 'Iterative Upscale' for use.
124
+ * Similar to 'Latent Scale (on Pixel Space)', if upscale_model_opt is provided, it performs pixel upscaling using the model.
125
+ * PixelTiledKSampleUpscalerProvider - It is similar to PixelKSampleUpscalerProvider, but it uses ComfyUI_TiledKSampler and Tiled VAE Decoder/Encoder to avoid GPU VRAM issues at high resolutions.
126
+ * You need to install the [BlenderNeko/ComfyUI_TiledKSampler](https://github.com/BlenderNeko/ComfyUI_TiledKSampler) node extension.
127
+
128
+ * PK_HOOK
129
+ * DenoiseScheduleHookProvider - IterativeUpscale provides a hook that gradually changes the denoise to target_denoise as the iterative-step progresses.
130
+ * CfgScheduleHookProvider - IterativeUpscale provides a hook that gradually changes the cfg to target_cfg as the iterative-step progresses.
131
+ * StepsScheduleHookProvider - IterativeUpscale provides a hook that gradually changes the sampling-steps to target_steps as the iterative-step progresses.
132
+ * NoiseInjectionHookProvider - During each iteration of IterativeUpscale, noise is injected into the latent space while varying the strength according to a schedule.
133
+ * You need to install the [BlenderNeko/ComfyUI_Noise](https://github.com/BlenderNeko/ComfyUI_Noise) node extension.
134
+ * The seed serves as the initial value required for generating noise, and it increments by 1 with each iteration as the process unfolds.
135
+ * The source determines the types of CPU noise and GPU noise to be configured.
136
+ * Currently, there is only a simple schedule available, where the strength of the noise varies from start_strength to end_strength during the progression of each iteration.
137
+ * UnsamplerHookProvider - Apply Unsampler during each iteration. To use this node, ComfyUI_Noise must be installed.
138
+ * PixelKSampleHookCombine - This is used to connect two PK_HOOKs. hook1 is executed first and then hook2 is executed.
139
+ * If you want to simultaneously change cfg and denoise, you can combine the PK_HOOKs of CfgScheduleHookProvider and PixelKSampleHookCombine.
140
+
141
+ * DETAILER_HOOK
142
+ * NoiseInjectionDetailerHookProvider - The `detailer_hook` is a hook in the `Detailer` that injects noise during the processing of each SEGS.
143
+ * UnsamplerDetailerHookProvider - Apply Unsampler during each cycle. To use this node, ComfyUI_Noise must be installed.
144
+ * DenoiseSchedulerDetailerHookProvider - During the progress of the cycle, the detailer's denoise is altered up to the `target_denoise`.
145
+ * CoreMLDetailerHookProvider - CoreML supports only 512x512, 512x768, 768x512, 768x768 size sampling. CoreMLDetailerHookProvider precisely fixes the upscale of the crop_region to this size. When using this hook, it will always be selected size, regardless of the guide_size. However, if the guide_size is too small, skipping will occur.
146
+ * DetailerHookCombine - This is used to connect two DETAILER_HOOKs. Similar to PixelKSampleHookCombine.
147
+ * SEGSOrderedFilterDetailerHook, SEGSRangeFilterDetailerHook, SEGSLabelFilterDetailerHook - There are a wrapper node that provides SEGSFilter nodes to be applied in FaceDetailer or Detector by creating DETAILER_HOOK.
148
+ * PreviewDetailerHOok - Connecting this hook node helps provide assistance for viewing previews whenever SEGS Detailing tasks are completed. When working with a large number of SEGS, such as Make Tile SEGS, it allows for monitoring the situation as improvements progress incrementally.
149
+ * Since this is the hook applied when pasting onto the original image, it has no effect on nodes like `SEGSDetailer`.
150
+
151
+ * Iterative Upscale (Latent/on Pixel Space) - The upscaler takes the input upscaler and splits the scale_factor into steps, then iteratively performs upscaling.
152
+ This takes latent as input and outputs latent as the result.
153
+ * Iterative Upscale (Image) - The upscaler takes the input upscaler and splits the scale_factor into steps, then iteratively performs upscaling. This takes image as input and outputs image as the result.
154
+ * Internally, this node uses 'Iterative Upscale (Latent)'.
155
+
156
+ * TwoSamplersForMask - This node can apply two samplers depending on the mask area. The base_sampler is applied to the area where the mask is 0, while the mask_sampler is applied to the area where the mask is 1.
157
+ * Note: The latent encoded through VAEEncodeForInpaint cannot be used.
158
+ * KSamplerProvider - This is a wrapper that enables KSampler to be used in TwoSamplersForMask TwoSamplersForMaskUpscalerProvider.
159
+ * TiledKSamplerProvider - ComfyUI_TiledKSampler is a wrapper that provides KSAMPLER.
160
+ * You need to install the [BlenderNeko/ComfyUI_TiledKSampler](https://github.com/BlenderNeko/ComfyUI_TiledKSampler) node extension.
161
+
162
+ * TwoAdvancedSamplersForMask - TwoSamplersForMask is similar to TwoAdvancedSamplersForMask, but they differ in their operation. TwoSamplersForMask performs sampling in the mask area only after all the samples in the base area are finished. On the other hand, TwoAdvancedSamplersForMask performs sampling in both the base area and the mask area sequentially at each step.
163
+ * KSamplerAdvancedProvider - This is a wrapper that enables KSampler to be used in TwoAdvancedSamplersForMask, RegionalSampler.
164
+ * sigma_factor: By multiplying the denoise schedule by the sigma_factor, you can adjust the amount of denoising based on the configured denoise.
165
+
166
+ * TwoSamplersForMaskUpscalerProvider - This is an Upscaler that extends TwoSamplersForMask to be used in Iterative Upscale.
167
+ * TwoSamplersForMaskUpscalerProviderPipe - pipe version of TwoSamplersForMaskUpscalerProvider.
168
+
169
+ * Image Utils
170
+ * PreviewBridge (image) - This custom node can be used with a bridge for image when using the MaskEditor feature of Clipspace.
171
+ * PreviewBridge (latent) - This custom node can be used with a bridge for latent image when using the MaskEditor feature of Clipspace.
172
+ * If a latent with a mask is provided as input, it displays the mask. Additionally, the mask output provides the mask set in the latent.
173
+ * If a latent without a mask is provided as input, it outputs the original latent as is, but the mask output provides an output with the entire region set as a mask.
174
+ * When set mask through MaskEditor, a mask is applied to the latent, and the output includes the stored mask. The same mask is also output as the mask output.
175
+ * When connected to `vae_opt`, it takes higher priority than the `preview_method`.
176
+ * ImageSender, ImageReceiver - The images generated in ImageSender are automatically sent to the ImageReceiver with the same link_id.
177
+ * LatentSender, LatentReceiver - The latent generated in LatentSender are automatically sent to the LatentReceiver with the same link_id.
178
+ * Furthermore, LatentSender is implemented with PreviewLatent, which stores the latent in payload form within the image thumbnail.
179
+ * Due to the current structure of ComfyUI, it is unable to distinguish between SDXL latent and SD1.5/SD2.1 latent. Therefore, it generates thumbnails by decoding them using the SD1.5 method.
180
+
181
+ * Switch nodes
182
+ * Switch (image,mask), Switch (latent), Switch (SEGS) - Among multiple inputs, it selects the input designated by the selector and outputs it. The first input must be provided, while the others are optional. However, if the input specified by the selector is not connected, an error may occur.
183
+ * Switch (Any) - This is a Switch node that takes an arbitrary number of inputs and produces a single output. Its type is determined when connected to any node, and connecting inputs increases the available slots for connections.
184
+ * Inversed Switch (Any) - In contrast to `Switch (Any)`, it takes a single input and outputs one of many. Due to ComfyUI's functional limitations, the value of `select` must be determined at the time of queuing a prompt, and while it can serve as a `Primitive Node` or `ImpactInt`, it cannot function properly when connected through other nodes.
185
+ * Guide
186
+ * When the `Switch (Any)` and `Inversed Switch (Any)` selects are transformed into primitives, it's important to be cautious because the select range is not appropriately constrained, potentially leading to unintended behavior.
187
+ * `Switch (image,mask)`, `Switch (latent)`, `Switch (SEGS)`, `Switch (Any)` supports `sel_mode` param. The `sel_mode` sets the moment at which the `select` parameter is determined. `select_on_prompt` determines the `select` at the time of queuing the prompt, while `select_on_execution` determines it during the execution of the workflow. While `select_on_execution` offers more flexibility, it can potentially trigger workflow execution errors due to running nodes that may be impossible to execute within the limitations of ComfyUI. `select_on_prompt` bypasses this constraint by treating any inputs not selected as if they were disconnected. However, please note that when using `select_on_prompt`, the `select` can only be used with widgets or `Primitive Nodes` determined at the queue prompt.
188
+ * There is an issue when connecting the built-in reroute node with the switch's input/output slots. it can lead to forced disconnections during workflow loading. Therefore, it is advisable not to use reroute for making connections in such cases. However, there are no issues when using the reroute node in Pythongossss.
189
+
190
+ * [Wildcards](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ImpactWildcard.md) - These are nodes that supports syntax in the form of `__wildcard-name__` and dynamic prompt syntax like `{a|b|c}`.
191
+ * Wildcard files can be used by placing `.txt` or `.yaml` files under either `ComfyUI-Impact-Pack/wildcards` or `ComfyUI-Impact-Pack/custom_wildcards` paths.
192
+ * You can download and use [Wildcard YAML](https://civitai.com/models/138970/billions-of-wildcards-all-in-one) files in this format.
193
+ * After the first execution, you can change the custom wildcards path in the `custom_wildcards` entry within the `ComfyUI-Impact-Pack/impact-pack.ini` file created.
194
+ * ImpactWildcardProcessor - The text is generated by processing the wildcard in the Text. If the mode is set to "populate", a dynamic prompt is generated with each execution and the input is filled in the second textbox. If the mode is set to "fixed", the content of the second textbox remains unchanged.
195
+ * When an image is generated with the "fixed" mode, the prompt used for that particular generation is stored in the metadata.
196
+ * ImpactWildcardEncode - Similar to ImpactWildcardProcessor, this provides the loading functionality of LoRAs (e.g. `<lora:some_awesome_lora:0.7:1.2>`). Populated prompts are encoded using the clip after all the lora loading is done.
197
+ * If the `Inspire Pack` is installed, you can use **Lora Block Weight** in the form of `LBW=lbw spec;`
198
+ * `<lora:chunli:1.0:1.0:LBW=B11:0,0,0,0,0,0,0,0,0,0,A,0,0,0,0,0,0;A=0.;>`, `<lora:chunli:1.0:1.0:LBW=0,0,0,0,0,0,0,0,0,0,A,B,0,0,0,0,0;A=0.5;B=0.2;>`, `<lora:chunli:1.0:1.0:LBW=SD-MIDD;>`
199
+
200
+ * Regional Sampling - These nodes offer the capability to divide regions and perform partial sampling using a mask. Unlike TwoSamplersForMask, sampling for each region is applied during each step.
201
+ * RegionalPrompt - This node combines a **mask** for specifying regions and the **sampler** to apply to each region to create `REGIONAL_PROMPTS`.
202
+ * CombineRegionalPrompts - Combine multiple `REGIONAL_PROMPTS` to create a single `REGIONAL_PROMPTS`.
203
+ * RegionalSampler - This node performs sampling using a base sampler and regional prompts. Sampling by the base sampler is executed at each step, while sampling for each region is performed through the sampler bound to each region.
204
+ * overlap_factor - Specifies the amount of overlap for each region to blend well with the area outside the mask.
205
+ * restore_latent - When sampling each region, restore the areas outside the mask to the base latent, preventing additional noise from being introduced outside the mask during region sampling.
206
+ * RegionalSamplerAdvanced - This is the Advanced version of the RegionalSampler. You can control it using `step` instead of `denoise`.
207
+ * NOTE: The `sde` sampler and `uni_pc` sampler introduce additional noise during each step of the sampling process. To mitigate this, when sampling each region, the `uni_pc` sampler applies additional `dpmpp_fast`, and the sde sampler applies the `dpmpp_2m` sampler as an additional measure.
208
+
209
+ * KSampler (pipe), KSampler (advanced/pipe)
210
+
211
+ * Image batch To Image List - Convert Image batch to Image List
212
+ - You can use images generated in a multi batch to handle them
213
+ * Make Image List - Convert multiple images into a single image list
214
+ * Make Image Batch - Convert multiple images into a single image batch
215
+ - The input of images can be scaled up as needed
216
+
217
+ * String Selector - It selects and returns a portion of the string. When `multiline` mode is disabled, it simply returns the string of the line pointed to by the selector. When `multiline` mode is enabled, it divides the string based on lines that start with `#` and returns them. If the `select` value is larger than the number of items, it will start counting from the first line again and return accordingly.
218
+ * Combine Conditionings - It takes multiple conditionings as input and combines them into a single conditioning.
219
+ * Concat Conditionings - It takes multiple conditionings as input and concat them into a single conditioning.
220
+
221
+ * Logics (experimental) - These nodes are experimental nodes designed to implement the logic for loops and dynamic switching.
222
+ * ImpactCompare, ImpactConditionalBranch, ImpactConditionalBranchSelMode, ImpactInt, ImpactValueSender, ImpactValueReceiver, ImpactImageInfo, ImpactMinMax, ImpactNeg, ImpactConditionalStopIteration
223
+ * ImpactIsNotEmptySEGS - This node returns `true` only if the input SEGS is not empty.
224
+ * Queue Trigger - When this node is executed, it adds a new queue to assist with repetitive tasks. It will only execute if the signal's status changes.
225
+ * Queue Trigger (Countdown) - Like the Queue Trigger, it adds a queue, but only adds it if it's greater than 1, and decrements the count by one each time it runs.
226
+ * Sleep - Waits for the specified time (in seconds).
227
+ * Set Widget Value - This node sets one of the optional inputs to the specified node's widget. An error may occur if the types do not match.
228
+ * Set Mute State - This node changes the mute state of a specific node.
229
+ * Control Bridge - This node modifies the state of the connected control nodes based on the `mode` and `behavior` . If there are nodes that require a change, the current execution is paused, the mute status is updated, and a new prompt queue is inserted.
230
+ * When the `mode` is `active`, it makes the connected control nodes active regardless of the behavior.
231
+ * When the `mode` is `Bypass/Mute`, it changes the state of the connected nodes based on whether the behavior is `Bypass` or `Mute`.
232
+ * **Limitation**: Due to these characteristics, it does not function correctly when the batch count exceeds 1. Additionally, it does not guarantee proper operation when the seed is randomized or when the state of nodes is altered by actions such as `Queue Trigger`, `Set Widget Value`, `Set Mute`, before the Control Bridge.
233
+ * When utilizing this node, please structure the workflow in such a way that `Queue Trigger`, `Set Widget Value`, `Set Mute State`, and similar actions are executed at the end of the workflow.
234
+ * If you want to change the value of the seed at each iteration, please ensure that Set Widget Value is executed at the end of the workflow instead of using randomization.
235
+ * It is not a problem if the seed changes due to randomization as long as it occurs after the Control Bridge section.
236
+ * Remote Boolean (on prompt), Remote Int (on prompt) - At the start of the prompt, this node forcibly sets the `widget_value` of `node_id`. It is disregarded if the target widget type is different.
237
+ * You can find the `node_id` by checking through [ComfyUI-Manager](https://github.com/ltdrdata/ComfyUI-Manager) using the format `Badge: #ID Nickname`.
238
+ * Experimental set of nodes for implementing loop functionality (tutorial to be prepared later / [example workflow](test/loop-test.json)).
239
+
240
+ * HuggingFace - These nodes provide functionalities based on HuggingFace repository models.
241
+ * `HF Transformers Classifier Provider` - This is a node that provides a classifier based on HuggingFace's transformers models.
242
+ * The 'repo id' parameter should contain HuggingFace's repo id. When `preset_repo_id` is set to `Manual repo id`, use the manually entered repo id in `manual_repo_id`.
243
+ * e.g. 'rizvandwiki/gender-classification-2' is a repository that provides a model for gender classification.
244
+ * `SEGS Classify` - This node utilizes the `TRANSFORMERS_CLASSIFIER` loaded with 'HF Transformers Classifier Provider' to classify `SEGS`.
245
+ * The 'expr' allows for forms like `label > number`, and in the case of `preset_expr` being `Manual expr`, it uses the expression entered in `manual_expr`.
246
+ * For example, in the case of `male <= 0.4`, if the score of the `male` label in the classification result is less than or equal to 0.4, it is categorized as `filtered_SEGS`, otherwise, it is categorized as `remained_SEGS`.
247
+ * For supported labels, please refer to the `config.json` of the respective HuggingFace repository.
248
+ * `#Female` and `#Male` are symbols that group multiple labels such as `Female, women, woman, ...`, for convenience, rather than being single labels.
249
+
250
+ ## MMDet nodes
251
+ * MMDetDetectorProvider - Loads the MMDet model to provide BBOX_DETECTOR and SEGM_DETECTOR.
252
+ * To use the existing MMDetDetectorProvider, you need to enable the MMDet usage configuration.
253
+
254
+
255
+ ## Feature
256
+ * Interactive SAM Detector (Clipspace) - When you right-click on a node that has 'MASK' and 'IMAGE' outputs, a context menu will open. From this menu, you can either open a dialog to create a SAM Mask using 'Open in SAM Detector', or copy the content (likely mask data) using 'Copy (Clipspace)' and generate a mask using 'Impact SAM Detector' from the clipspace menu, and then paste it using 'Paste (Clipspace)'.
257
+ * Providing a feature to detect errors that occur when mixing models and clips from checkpoints such as `SDXL Base`, `SDXL Refiner`, `SD1.x`, `SD2.x` during sample execution, and reporting appropriate errors.
258
+
259
+ ## Deprecated
260
+ * The following nodes have been kept only for compatibility with existing workflows, and are no longer supported. Please replace them with new nodes.
261
+ * ONNX Detector (SEGS) - BBOX Detector (SEGS)
262
+ * MMDetLoader -> MMDetDetectorProvider
263
+ * SegsMaskCombine -> SEGS to MASK (combined)
264
+ * BboxDetectorForEach -> BBOX Detector (SEGS)
265
+ * SegmDetectorForEach -> SEGM Detector (SEGS)
266
+ * BboxDetectorCombined -> BBOX Detector (combined)
267
+ * SegmDetectorCombined -> SEGM Detector (combined)
268
+ * MaskPainter -> PreviewBridge
269
+ * To use the existing deprecated legacy nodes, you need to enable the MMDet usage configuration.
270
+
271
+
272
+ ## Ultralytics models
273
+ * huggingface.co/Bingsu/[adetailer](https://github.com/ultralytics/assets/releases/) - You can download face, people detection models, and clothing detection models.
274
+ * ultralytics/[assets](https://github.com/ultralytics/assets/releases/) - You can download various types of detection models other than faces or people.
275
+ * civitai/[adetailer](https://civitai.com/search/models?sortBy=models_v5&query=adetailer) - You can download various types detection models....Many models are associated with NSFW content.
276
+
277
+ ## How to activate 'MMDet usage'
278
+ * Upon the initial execution, an `impact-pack.ini` file will be generated in the custom_nodes/ComfyUI-Impact-Pack directory.
279
+ ```
280
+ [default]
281
+ dependency_version = 2
282
+ mmdet_skip = True
283
+ ```
284
+ * Change `mmdet_skip = True` to `mmdet_skip = False`
285
+ ```
286
+ [default]
287
+ dependency_version = 2
288
+ mmdet_skip = False
289
+ ```
290
+ * Restart ComfyUI
291
+
292
+
293
+ ## Installation
294
+
295
+ 1. `cd custom_nodes`
296
+ 1. `git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack.git`
297
+ 3. `cd ComfyUI-Impact-Pack`
298
+ 4. (optional) `git submodule update --init --recursive`
299
+ * Impact Pack will automatically download subpack during its initial launch.
300
+ 5. (optional) `python install.py`
301
+ * Impact Pack will automatically install its dependencies during its initial launch.
302
+ * For the portable version, you should execute the command `..\..\..\python_embeded\python.exe install.py` to run the installation script.
303
+
304
+
305
+ 6. Restart ComfyUI
306
+
307
+ * NOTE: If an error occurs during the installation process, please refer to [Troubleshooting Page](troubleshooting/TROUBLESHOOTING.md) for assistance.
308
+ * You can use this colab notebook [colab notebook](https://colab.research.google.com/github/ltdrdata/ComfyUI-Impact-Pack/blob/Main/notebook/comfyui_colab_impact_pack.ipynb) to launch it. This notebook automatically downloads the impact pack to the custom_nodes directory, installs the tested dependencies, and runs it.
309
+
310
+ ## Package Dependencies (If you need to manual setup.)
311
+
312
+ * pip install
313
+ * openmim
314
+ * segment-anything
315
+ * ultralytics
316
+ * scikit-image
317
+ * piexif
318
+ * (optional) pycocotools
319
+ * (optional) onnxruntime
320
+
321
+ * mim install (optional)
322
+ * mmcv==2.0.0, mmdet==3.0.0, mmengine==0.7.2
323
+
324
+ * linux packages (ubuntu)
325
+ * libgl1-mesa-glx
326
+ * libglib2.0-0
327
+
328
+
329
+ ## Config example
330
+ * Once you run the Impact Pack for the first time, an `impact-pack.ini` file will be automatically generated in the Impact Pack directory. You can modify this configuration file to customize the default behavior.
331
+ * `dependency_version` - don't touch this
332
+ * `mmdet_skip` - disable MMDet based nodes and legacy nodes if `True`
333
+ * `sam_editor_cpu` - use cpu for `SAM editor` instead of gpu
334
+ * sam_editor_model: Specify the SAM model for the SAM editor.
335
+ * You can download various SAM models using ComfyUI-Manager.
336
+ * Path to SAM model: `ComfyUI/models/sams`
337
+ ```
338
+ [default]
339
+ dependency_version = 9
340
+ mmdet_skip = True
341
+ sam_editor_cpu = False
342
+ sam_editor_model = sam_vit_b_01ec64.pth
343
+ ```
344
+
345
+
346
+ ## Other Materials (auto-download on initial startup)
347
+
348
+ * ComfyUI/models/mmdets/bbox <= https://huggingface.co/dustysys/ddetailer/resolve/main/mmdet/bbox/mmdet_anime-face_yolov3.pth
349
+ * ComfyUI/models/mmdets/bbox <= https://raw.githubusercontent.com/Bing-su/dddetailer/master/config/mmdet_anime-face_yolov3.py
350
+ * ComfyUI/models/sams <= https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
351
+
352
+ ## Troubleshooting page
353
+ * [Troubleshooting Page](troubleshooting/TROUBLESHOOTING.md)
354
+
355
+
356
+ ## How to use (DDetailer feature)
357
+
358
+ #### 1. Basic auto face detection and refine exapmle.
359
+ ![simple](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/simple.png)
360
+ * The face that has been damaged due to low resolution is restored with high resolution by generating and synthesizing it, in order to restore the details.
361
+ * The FaceDetailer node is a combination of a Detector node for face detection and a Detailer node for image enhancement. See the [Advanced Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/tutorial/advanced.md) for a more detailed explanation.
362
+ * Pass the MMDetLoader 's bbox model and the detection model loaded by SAMLoader to FaceDetailer . Since it performs the function of KSampler for image enhancement, it overlaps with KSampler's options.
363
+ * The MASK output of FaceDetailer provides a visualization of where the detected and enhanced areas are.
364
+
365
+ ![simple-orig](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/simple-original.png) ![simple-refined](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/simple-refined.png)
366
+ * You can see that the face in the image on the left has increased detail as in the image on the right.
367
+
368
+ #### 2. 2Pass refine (restore a severely damaged face)
369
+ ![2pass-workflow-example](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-simple.png)
370
+ * Although two FaceDetailers can be attached together for a 2-pass configuration, various common inputs used in KSampler can be passed through DETAILER_PIPE, so FaceDetailerPipe can be used to configure easily.
371
+ * In 1pass, only rough outline recovery is required, so restore with a reasonable resolution and low options. However, if you increase the dilation at this time, not only the face but also the surrounding parts are included in the recovery range, so it is useful when you need to reshape the face other than the facial part.
372
+
373
+ ![2pass-example-original](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-original.png) ![2pass-example-middle](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-1pass.png) ![2pass-example-result](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-2pass.png)
374
+ * In the first stage, the severely damaged face is restored to some extent, and in the second stage, the details are restored
375
+
376
+ #### 3. Face Bbox(bounding box) + Person silhouette segmentation (prevent distortion of the background.)
377
+ ![combination-workflow-example](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/combination.jpg)
378
+ ![combination-example-original](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/combination-original.png) ![combination-example-refined](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/combination-refined.png)
379
+
380
+ * Facial synthesis that emphasizes details is delicately aligned with the contours of the face, and it can be observed that it does not affect the image outside of the face.
381
+
382
+ * The BBoxDetectorForEach node is used to detect faces, and the SAMDetectorCombined node is used to find the segment related to the detected face. By using the Segs & Mask node with the two masks obtained in this way, an accurate mask that intersects based on segs can be generated. If this generated mask is input to the DetailerForEach node, only the target area can be created in high resolution from the image and then composited.
383
+
384
+ #### 4. Iterative Upscale
385
+ ![upscale-workflow-example](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/upscale-workflow.png)
386
+
387
+ * The IterativeUpscale node is a node that enlarges an image/latent by a scale_factor. In this process, the upscale is carried out progressively by dividing it into steps.
388
+ * IterativeUpscale takes an Upscaler as an input, similar to a plugin, and uses it during each iteration. PixelKSampleUpscalerProvider is an Upscaler that converts the latent representation to pixel space and applies ksampling.
389
+ * The upscale_model_opt is an optional parameter that determines whether to use the upscale function of the model base if available. Using the upscale function of the model base can significantly reduce the number of iterative steps required. If an x2 upscaler is used, the image/latent is first upscaled by a factor of 2 and then downscaled to the target scale at each step before further processing is done.
390
+
391
+ * The following image is an image of 304x512 pixels and the same image scaled up to three times its original size using IterativeUpscale.
392
+
393
+ ![combination-example-original](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/upscale-original.png) ![combination-example-refined](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/upscale-3x.png)
394
+
395
+
396
+ #### 5. Interactive SAM Detector (Clipspace)
397
+
398
+ * When you right-click on the node that outputs 'MASK' and 'IMAGE', a menu called "Open in SAM Detector" appears, as shown in the following picture. Clicking on the menu opens a dialog in SAM's functionality, allowing you to generate a segment mask.
399
+ ![samdetector-menu](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/SAMDetector-menu.png)
400
+
401
+ * By clicking the left mouse button on a coordinate, a positive prompt in blue color is entered, indicating the area that should be included. Clicking the right mouse button on a coordinate enters a negative prompt in red color, indicating the area that should be excluded. Positive prompts represent the areas that should be included, while negative prompts represent the areas that should be excluded.
402
+ * You can remove the points that were added by using the "undo" button. After selecting the points, pressing the "detect" button generates the mask. Additionally, you can adjust the fidelity slider to determine the extent to which the mask belongs to the confidence region.
403
+
404
+ ![samdetector-dialog](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/SAMDetector-dialog.jpg)
405
+
406
+ * If you opened the dialog through "Open in SAM Detector" from the node, you can directly apply the changes by clicking the "Save to node" button. However, if you opened the dialog through the "clipspace" menu, you can save it to clipspace by clicking the "Save" button.
407
+
408
+ ![samdetector-result](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/SAMDetector-result.jpg)
409
+
410
+ * When you execute using the reflected mask in the node, you can observe that the image and mask are displayed separately.
411
+
412
+
413
+ ## Others Tutorials
414
+ * [ComfyUI-extension-tutorials/ComfyUI-Impact-Pack](https://github.com/ltdrdata/ComfyUI-extension-tutorials/tree/Main/ComfyUI-Impact-Pack) - You can find various tutorials and workflows on this page.
415
+ * [Advanced Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/advanced.md)
416
+ * [SAM Application](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/sam.md)
417
+ * [PreviewBridge](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/previewbridge.md)
418
+ * [Mask Pointer](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/maskpointer.md)
419
+ * [ONNX Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ONNX.md)
420
+ * [CLIPSeg Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/clipseg.md)
421
+ * [Extreme Highresolution Upscale](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/extreme-upscale.md)
422
+ * [TwoSamplersForMask](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/TwoSamplers.md)
423
+ * [TwoAdvancedSamplersForMask](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/TwoAdvancedSamplers.md)
424
+ * [Advanced Iterative Upscale: PK_HOOK](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/pk_hook.md)
425
+ * [Advanced Iterative Upscale: TwoSamplersForMask Upscale Provider](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/TwoSamplersUpscale.md)
426
+ * [Interactive SAM + PreviewBridge](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/sam_with_preview_bridge.md)
427
+ * [ImageSender/ImageReceiver/LatentSender/LatentReceiver](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/sender_receiver.md)
428
+ * [ImpactWildcardProcessor](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ImpactWildcardProcessor.md)
429
+
430
+
431
+ ## Credits
432
+
433
+ ComfyUI/[ComfyUI](https://github.com/comfyanonymous/ComfyUI) - A powerful and modular stable diffusion GUI.
434
+
435
+ dustysys/[ddetailer](https://github.com/dustysys/ddetailer) - DDetailer for Stable-diffusion-webUI extension.
436
+
437
+ Bing-su/[dddetailer](https://github.com/Bing-su/dddetailer) - The anime-face-detector used in ddetailer has been updated to be compatible with mmdet 3.0.0, and we have also applied a patch to the pycocotools dependency for Windows environment in ddetailer.
438
+
439
+ facebook/[segment-anything](https://github.com/facebookresearch/segment-anything) - Segmentation Anything!
440
+
441
+ hysts/[anime-face-detector](https://github.com/hysts/anime-face-detector) - Creator of `anime-face_yolov3`, which has impressive performance on a variety of art styles.
442
+
443
+ open-mmlab/[mmdetection](https://github.com/open-mmlab/mmdetection) - Object detection toolset. `dd-person_mask2former` was trained via transfer learning using their [R-50 Mask2Former instance segmentation model](https://github.com/open-mmlab/mmdetection/tree/master/configs/mask2former#instance-segmentation) as a base.
444
+
445
+ biegert/[ComfyUI-CLIPSeg](https://github.com/biegert/ComfyUI-CLIPSeg) - This is a custom node that enables the use of CLIPSeg technology, which can find segments through prompts, in ComfyUI.
446
+
447
+ BlenderNeok/[ComfyUI-TiledKSampler](https://github.com/BlenderNeko/ComfyUI_TiledKSampler) -
448
+ The tile sampler allows high-resolution sampling even in places with low GPU VRAM.
449
+
450
+ BlenderNeok/[ComfyUI_Noise](https://github.com/BlenderNeko/ComfyUI_Noise) - The noise injection feature relies on this function.
451
+
452
+ WASasquatch/[was-node-suite-comfyui](https://github.com/WASasquatch/was-node-suite-comfyui) - A powerful custom node extensions of ComfyUI.
453
+
454
+ Trung0246/[ComfyUI-0246](https://github.com/Trung0246/ComfyUI-0246) - Nice bypass hack!
custom_nodes/ComfyUI-Impact-Pack/__init__.py ADDED
@@ -0,0 +1,502 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ @author: Dr.Lt.Data
3
+ @title: Impact Pack
4
+ @nickname: Impact Pack
5
+ @description: This extension offers various detector nodes and detailer nodes that allow you to configure a workflow that automatically enhances facial details. And provide iterative upscaler.
6
+ """
7
+
8
+ import shutil
9
+ import folder_paths
10
+ import os
11
+ import sys
12
+ import traceback
13
+
14
+ comfy_path = os.path.dirname(folder_paths.__file__)
15
+ impact_path = os.path.join(os.path.dirname(__file__))
16
+ subpack_path = os.path.join(os.path.dirname(__file__), "impact_subpack")
17
+ modules_path = os.path.join(os.path.dirname(__file__), "modules")
18
+ wildcards_path = os.path.join(os.path.dirname(__file__), "wildcards")
19
+ custom_wildcards_path = os.path.join(os.path.dirname(__file__), "custom_wildcards")
20
+
21
+ sys.path.append(modules_path)
22
+
23
+ import impact.config
24
+ import impact.sample_error_enhancer
25
+ print(f"### Loading: ComfyUI-Impact-Pack ({impact.config.version})")
26
+
27
+
28
+ def do_install():
29
+ import importlib
30
+ spec = importlib.util.spec_from_file_location('impact_install', os.path.join(os.path.dirname(__file__), 'install.py'))
31
+ impact_install = importlib.util.module_from_spec(spec)
32
+ spec.loader.exec_module(impact_install)
33
+
34
+
35
+ # ensure dependency
36
+ if not os.path.exists(os.path.join(subpack_path, ".git")) and os.path.exists(subpack_path):
37
+ print(f"### CompfyUI-Impact-Pack: corrupted subpack detected.")
38
+ shutil.rmtree(subpack_path)
39
+
40
+ if impact.config.get_config()['dependency_version'] < impact.config.dependency_version or not os.path.exists(subpack_path):
41
+ print(f"### ComfyUI-Impact-Pack: Updating dependencies [{impact.config.get_config()['dependency_version']} -> {impact.config.dependency_version}]")
42
+ do_install()
43
+
44
+ sys.path.append(subpack_path)
45
+
46
+ # Core
47
+ # recheck dependencies for colab
48
+ try:
49
+ import impact.subpack_nodes # This import must be done before cv2.
50
+
51
+ import folder_paths
52
+ import torch
53
+ import cv2
54
+ import numpy as np
55
+ import comfy.samplers
56
+ import comfy.sd
57
+ import warnings
58
+ from PIL import Image, ImageFilter
59
+ from skimage.measure import label, regionprops
60
+ from collections import namedtuple
61
+ import piexif
62
+
63
+ if not impact.config.get_config()['mmdet_skip']:
64
+ import mmcv
65
+ from mmdet.apis import (inference_detector, init_detector)
66
+ from mmdet.evaluation import get_classes
67
+ except:
68
+ import importlib
69
+ print("### ComfyUI-Impact-Pack: Reinstall dependencies (several dependencies are missing.)")
70
+ do_install()
71
+
72
+ import impact.impact_server # to load server api
73
+
74
+ def setup_js():
75
+ import nodes
76
+ js_dest_path = os.path.join(comfy_path, "web", "extensions", "impact-pack")
77
+
78
+ if hasattr(nodes, "EXTENSION_WEB_DIRS"):
79
+ if os.path.exists(js_dest_path):
80
+ shutil.rmtree(js_dest_path)
81
+ else:
82
+ print(f"[WARN] ComfyUI-Impact-Pack: Your ComfyUI version is outdated. Please update to the latest version.")
83
+ # setup js
84
+ if not os.path.exists(js_dest_path):
85
+ os.makedirs(js_dest_path)
86
+
87
+ js_src_path = os.path.join(impact_path, "js", "impact-pack.js")
88
+ shutil.copy(js_src_path, js_dest_path)
89
+
90
+ js_src_path = os.path.join(impact_path, "js", "impact-sam-editor.js")
91
+ shutil.copy(js_src_path, js_dest_path)
92
+
93
+ js_src_path = os.path.join(impact_path, "js", "comboBoolMigration.js")
94
+ shutil.copy(js_src_path, js_dest_path)
95
+
96
+
97
+ setup_js()
98
+
99
+ from impact.impact_pack import *
100
+ from impact.detectors import *
101
+ from impact.pipe import *
102
+ from impact.logics import *
103
+ from impact.util_nodes import *
104
+ from impact.segs_nodes import *
105
+ from impact.special_samplers import *
106
+ from impact.hf_nodes import *
107
+ from impact.bridge_nodes import *
108
+ from impact.hook_nodes import *
109
+ from impact.animatediff_nodes import *
110
+
111
+ import threading
112
+
113
+ wildcard_path = impact.config.get_config()['custom_wildcards']
114
+
115
+
116
+ def wildcard_load():
117
+ with wildcards.wildcard_lock:
118
+ impact.wildcards.read_wildcard_dict(wildcards_path)
119
+
120
+ try:
121
+ impact.wildcards.read_wildcard_dict(impact.config.get_config()['custom_wildcards'])
122
+ except Exception as e:
123
+ print(f"[Impact Pack] Failed to load custom wildcards directory.")
124
+
125
+ print(f"[Impact Pack] Wildcards loading done.")
126
+
127
+
128
+ threading.Thread(target=wildcard_load).start()
129
+
130
+
131
+ NODE_CLASS_MAPPINGS = {
132
+ "SAMLoader": SAMLoader,
133
+ "CLIPSegDetectorProvider": CLIPSegDetectorProvider,
134
+ "ONNXDetectorProvider": ONNXDetectorProvider,
135
+
136
+ "BitwiseAndMaskForEach": BitwiseAndMaskForEach,
137
+ "SubtractMaskForEach": SubtractMaskForEach,
138
+
139
+ "DetailerForEach": DetailerForEach,
140
+ "DetailerForEachDebug": DetailerForEachTest,
141
+ "DetailerForEachPipe": DetailerForEachPipe,
142
+ "DetailerForEachDebugPipe": DetailerForEachTestPipe,
143
+ "DetailerForEachPipeForAnimateDiff": DetailerForEachPipeForAnimateDiff,
144
+
145
+ "SAMDetectorCombined": SAMDetectorCombined,
146
+ "SAMDetectorSegmented": SAMDetectorSegmented,
147
+
148
+ "FaceDetailer": FaceDetailer,
149
+ "FaceDetailerPipe": FaceDetailerPipe,
150
+ "MaskDetailerPipe": MaskDetailerPipe,
151
+
152
+ "ToDetailerPipe": ToDetailerPipe,
153
+ "ToDetailerPipeSDXL": ToDetailerPipeSDXL,
154
+ "FromDetailerPipe": FromDetailerPipe,
155
+ "FromDetailerPipe_v2": FromDetailerPipe_v2,
156
+ "FromDetailerPipeSDXL": FromDetailerPipe_SDXL,
157
+ "ToBasicPipe": ToBasicPipe,
158
+ "FromBasicPipe": FromBasicPipe,
159
+ "FromBasicPipe_v2": FromBasicPipe_v2,
160
+ "BasicPipeToDetailerPipe": BasicPipeToDetailerPipe,
161
+ "BasicPipeToDetailerPipeSDXL": BasicPipeToDetailerPipeSDXL,
162
+ "DetailerPipeToBasicPipe": DetailerPipeToBasicPipe,
163
+ "EditBasicPipe": EditBasicPipe,
164
+ "EditDetailerPipe": EditDetailerPipe,
165
+ "EditDetailerPipeSDXL": EditDetailerPipeSDXL,
166
+
167
+ "LatentPixelScale": LatentPixelScale,
168
+ "PixelKSampleUpscalerProvider": PixelKSampleUpscalerProvider,
169
+ "PixelKSampleUpscalerProviderPipe": PixelKSampleUpscalerProviderPipe,
170
+ "IterativeLatentUpscale": IterativeLatentUpscale,
171
+ "IterativeImageUpscale": IterativeImageUpscale,
172
+ "PixelTiledKSampleUpscalerProvider": PixelTiledKSampleUpscalerProvider,
173
+ "PixelTiledKSampleUpscalerProviderPipe": PixelTiledKSampleUpscalerProviderPipe,
174
+ "TwoSamplersForMaskUpscalerProvider": TwoSamplersForMaskUpscalerProvider,
175
+ "TwoSamplersForMaskUpscalerProviderPipe": TwoSamplersForMaskUpscalerProviderPipe,
176
+
177
+ "PixelKSampleHookCombine": PixelKSampleHookCombine,
178
+ "DenoiseScheduleHookProvider": DenoiseScheduleHookProvider,
179
+ "StepsScheduleHookProvider": StepsScheduleHookProvider,
180
+ "CfgScheduleHookProvider": CfgScheduleHookProvider,
181
+ "NoiseInjectionHookProvider": NoiseInjectionHookProvider,
182
+ "UnsamplerHookProvider": UnsamplerHookProvider,
183
+ "CoreMLDetailerHookProvider": CoreMLDetailerHookProvider,
184
+ "PreviewDetailerHookProvider": PreviewDetailerHookProvider,
185
+
186
+ "DetailerHookCombine": DetailerHookCombine,
187
+ "NoiseInjectionDetailerHookProvider": NoiseInjectionDetailerHookProvider,
188
+ "UnsamplerDetailerHookProvider": UnsamplerDetailerHookProvider,
189
+ "DenoiseSchedulerDetailerHookProvider": DenoiseSchedulerDetailerHookProvider,
190
+ "SEGSOrderedFilterDetailerHookProvider": SEGSOrderedFilterDetailerHookProvider,
191
+ "SEGSRangeFilterDetailerHookProvider": SEGSRangeFilterDetailerHookProvider,
192
+ "SEGSLabelFilterDetailerHookProvider": SEGSLabelFilterDetailerHookProvider,
193
+
194
+ "BitwiseAndMask": BitwiseAndMask,
195
+ "SubtractMask": SubtractMask,
196
+ "AddMask": AddMask,
197
+ "ImpactSegsAndMask": SegsBitwiseAndMask,
198
+ "ImpactSegsAndMaskForEach": SegsBitwiseAndMaskForEach,
199
+ "EmptySegs": EmptySEGS,
200
+
201
+ "MediaPipeFaceMeshToSEGS": MediaPipeFaceMeshToSEGS,
202
+ "MaskToSEGS": MaskToSEGS,
203
+ "MaskToSEGS_for_AnimateDiff": MaskToSEGS_for_AnimateDiff,
204
+ "ToBinaryMask": ToBinaryMask,
205
+ "MasksToMaskList": MasksToMaskList,
206
+ "MaskListToMaskBatch": MaskListToMaskBatch,
207
+ "ImageListToImageBatch": ImageListToImageBatch,
208
+ "SetDefaultImageForSEGS": DefaultImageForSEGS,
209
+ "RemoveImageFromSEGS": RemoveImageFromSEGS,
210
+
211
+ "BboxDetectorSEGS": BboxDetectorForEach,
212
+ "SegmDetectorSEGS": SegmDetectorForEach,
213
+ "ONNXDetectorSEGS": BboxDetectorForEach,
214
+ "ImpactSimpleDetectorSEGS_for_AD": SimpleDetectorForAnimateDiff,
215
+ "ImpactSimpleDetectorSEGS": SimpleDetectorForEach,
216
+ "ImpactSimpleDetectorSEGSPipe": SimpleDetectorForEachPipe,
217
+ "ImpactControlNetApplySEGS": ControlNetApplySEGS,
218
+ "ImpactControlNetApplyAdvancedSEGS": ControlNetApplyAdvancedSEGS,
219
+ "ImpactControlNetClearSEGS": ControlNetClearSEGS,
220
+
221
+ "ImpactDecomposeSEGS": DecomposeSEGS,
222
+ "ImpactAssembleSEGS": AssembleSEGS,
223
+ "ImpactFrom_SEG_ELT": From_SEG_ELT,
224
+ "ImpactEdit_SEG_ELT": Edit_SEG_ELT,
225
+ "ImpactDilate_Mask_SEG_ELT": Dilate_SEG_ELT,
226
+ "ImpactDilateMask": DilateMask,
227
+ "ImpactGaussianBlurMask": GaussianBlurMask,
228
+ "ImpactDilateMaskInSEGS": DilateMaskInSEGS,
229
+ "ImpactGaussianBlurMaskInSEGS": GaussianBlurMaskInSEGS,
230
+ "ImpactScaleBy_BBOX_SEG_ELT": SEG_ELT_BBOX_ScaleBy,
231
+
232
+ "BboxDetectorCombined_v2": BboxDetectorCombined,
233
+ "SegmDetectorCombined_v2": SegmDetectorCombined,
234
+ "SegsToCombinedMask": SegsToCombinedMask,
235
+
236
+ "KSamplerProvider": KSamplerProvider,
237
+ "TwoSamplersForMask": TwoSamplersForMask,
238
+ "TiledKSamplerProvider": TiledKSamplerProvider,
239
+
240
+ "KSamplerAdvancedProvider": KSamplerAdvancedProvider,
241
+ "TwoAdvancedSamplersForMask": TwoAdvancedSamplersForMask,
242
+
243
+ "PreviewBridge": PreviewBridge,
244
+ "PreviewBridgeLatent": PreviewBridgeLatent,
245
+ "ImageSender": ImageSender,
246
+ "ImageReceiver": ImageReceiver,
247
+ "LatentSender": LatentSender,
248
+ "LatentReceiver": LatentReceiver,
249
+ "ImageMaskSwitch": ImageMaskSwitch,
250
+ "LatentSwitch": GeneralSwitch,
251
+ "SEGSSwitch": GeneralSwitch,
252
+ "ImpactSwitch": GeneralSwitch,
253
+ "ImpactInversedSwitch": GeneralInversedSwitch,
254
+
255
+ "ImpactWildcardProcessor": ImpactWildcardProcessor,
256
+ "ImpactWildcardEncode": ImpactWildcardEncode,
257
+
258
+ "SEGSDetailer": SEGSDetailer,
259
+ "SEGSPaste": SEGSPaste,
260
+ "SEGSPreview": SEGSPreview,
261
+ "SEGSPreviewCNet": SEGSPreviewCNet,
262
+ "SEGSToImageList": SEGSToImageList,
263
+ "ImpactSEGSToMaskList": SEGSToMaskList,
264
+ "ImpactSEGSToMaskBatch": SEGSToMaskBatch,
265
+ "ImpactSEGSConcat": SEGSConcat,
266
+ "ImpactSEGSPicker": SEGSPicker,
267
+ "ImpactMakeTileSEGS": MakeTileSEGS,
268
+
269
+ "SEGSDetailerForAnimateDiff": SEGSDetailerForAnimateDiff,
270
+
271
+ "ImpactKSamplerBasicPipe": KSamplerBasicPipe,
272
+ "ImpactKSamplerAdvancedBasicPipe": KSamplerAdvancedBasicPipe,
273
+
274
+ "ReencodeLatent": ReencodeLatent,
275
+ "ReencodeLatentPipe": ReencodeLatentPipe,
276
+
277
+ "ImpactImageBatchToImageList": ImageBatchToImageList,
278
+ "ImpactMakeImageList": MakeImageList,
279
+ "ImpactMakeImageBatch": MakeImageBatch,
280
+
281
+ "RegionalSampler": RegionalSampler,
282
+ "RegionalSamplerAdvanced": RegionalSamplerAdvanced,
283
+ "CombineRegionalPrompts": CombineRegionalPrompts,
284
+ "RegionalPrompt": RegionalPrompt,
285
+
286
+ "ImpactCombineConditionings": CombineConditionings,
287
+ "ImpactConcatConditionings": ConcatConditionings,
288
+
289
+ "ImpactSEGSLabelAssign": SEGSLabelAssign,
290
+ "ImpactSEGSLabelFilter": SEGSLabelFilter,
291
+ "ImpactSEGSRangeFilter": SEGSRangeFilter,
292
+ "ImpactSEGSOrderedFilter": SEGSOrderedFilter,
293
+
294
+ "ImpactCompare": ImpactCompare,
295
+ "ImpactConditionalBranch": ImpactConditionalBranch,
296
+ "ImpactConditionalBranchSelMode": ImpactConditionalBranchSelMode,
297
+ "ImpactIfNone": ImpactIfNone,
298
+ "ImpactConvertDataType": ImpactConvertDataType,
299
+ "ImpactLogicalOperators": ImpactLogicalOperators,
300
+ "ImpactInt": ImpactInt,
301
+ "ImpactFloat": ImpactFloat,
302
+ "ImpactValueSender": ImpactValueSender,
303
+ "ImpactValueReceiver": ImpactValueReceiver,
304
+ "ImpactImageInfo": ImpactImageInfo,
305
+ "ImpactLatentInfo": ImpactLatentInfo,
306
+ "ImpactMinMax": ImpactMinMax,
307
+ "ImpactNeg": ImpactNeg,
308
+ "ImpactConditionalStopIteration": ImpactConditionalStopIteration,
309
+ "ImpactStringSelector": StringSelector,
310
+
311
+ "RemoveNoiseMask": RemoveNoiseMask,
312
+
313
+ "ImpactLogger": ImpactLogger,
314
+ "ImpactDummyInput": ImpactDummyInput,
315
+
316
+ "ImpactQueueTrigger": ImpactQueueTrigger,
317
+ "ImpactQueueTriggerCountdown": ImpactQueueTriggerCountdown,
318
+ "ImpactSetWidgetValue": ImpactSetWidgetValue,
319
+ "ImpactNodeSetMuteState": ImpactNodeSetMuteState,
320
+ "ImpactControlBridge": ImpactControlBridge,
321
+ "ImpactIsNotEmptySEGS": ImpactNotEmptySEGS,
322
+ "ImpactSleep": ImpactSleep,
323
+ "ImpactRemoteBoolean": ImpactRemoteBoolean,
324
+ "ImpactRemoteInt": ImpactRemoteInt,
325
+
326
+ "ImpactHFTransformersClassifierProvider": HF_TransformersClassifierProvider,
327
+ "ImpactSEGSClassify": SEGS_Classify
328
+ }
329
+
330
+
331
+ NODE_DISPLAY_NAME_MAPPINGS = {
332
+ "SAMLoader": "SAMLoader (Impact)",
333
+
334
+ "BboxDetectorSEGS": "BBOX Detector (SEGS)",
335
+ "SegmDetectorSEGS": "SEGM Detector (SEGS)",
336
+ "ONNXDetectorSEGS": "ONNX Detector (SEGS/legacy) - use BBOXDetector",
337
+ "ImpactSimpleDetectorSEGS_for_AD": "Simple Detector for AnimateDiff (SEGS)",
338
+ "ImpactSimpleDetectorSEGS": "Simple Detector (SEGS)",
339
+ "ImpactSimpleDetectorSEGSPipe": "Simple Detector (SEGS/pipe)",
340
+ "ImpactControlNetApplySEGS": "ControlNetApply (SEGS)",
341
+ "ImpactControlNetApplyAdvancedSEGS": "ControlNetApplyAdvanced (SEGS)",
342
+
343
+ "BboxDetectorCombined_v2": "BBOX Detector (combined)",
344
+ "SegmDetectorCombined_v2": "SEGM Detector (combined)",
345
+ "SegsToCombinedMask": "SEGS to MASK (combined)",
346
+ "MediaPipeFaceMeshToSEGS": "MediaPipe FaceMesh to SEGS",
347
+ "MaskToSEGS": "MASK to SEGS",
348
+ "MaskToSEGS_for_AnimateDiff": "MASK to SEGS for AnimateDiff",
349
+ "BitwiseAndMaskForEach": "Bitwise(SEGS & SEGS)",
350
+ "SubtractMaskForEach": "Bitwise(SEGS - SEGS)",
351
+ "ImpactSegsAndMask": "Bitwise(SEGS & MASK)",
352
+ "ImpactSegsAndMaskForEach": "Bitwise(SEGS & MASKS ForEach)",
353
+ "BitwiseAndMask": "Bitwise(MASK & MASK)",
354
+ "SubtractMask": "Bitwise(MASK - MASK)",
355
+ "AddMask": "Bitwise(MASK + MASK)",
356
+ "DetailerForEach": "Detailer (SEGS)",
357
+ "DetailerForEachPipe": "Detailer (SEGS/pipe)",
358
+ "DetailerForEachDebug": "DetailerDebug (SEGS)",
359
+ "DetailerForEachDebugPipe": "DetailerDebug (SEGS/pipe)",
360
+ "SEGSDetailerForAnimateDiff": "SEGSDetailer For AnimateDiff (SEGS/pipe)",
361
+ "DetailerForEachPipeForAnimateDiff": "Detailer For AnimateDiff (SEGS/pipe)",
362
+
363
+ "SAMDetectorCombined": "SAMDetector (combined)",
364
+ "SAMDetectorSegmented": "SAMDetector (segmented)",
365
+ "FaceDetailerPipe": "FaceDetailer (pipe)",
366
+ "MaskDetailerPipe": "MaskDetailer (pipe)",
367
+
368
+ "FromDetailerPipeSDXL": "FromDetailer (SDXL/pipe)",
369
+ "BasicPipeToDetailerPipeSDXL": "BasicPipe -> DetailerPipe (SDXL)",
370
+ "EditDetailerPipeSDXL": "Edit DetailerPipe (SDXL)",
371
+
372
+ "BasicPipeToDetailerPipe": "BasicPipe -> DetailerPipe",
373
+ "DetailerPipeToBasicPipe": "DetailerPipe -> BasicPipe",
374
+ "EditBasicPipe": "Edit BasicPipe",
375
+ "EditDetailerPipe": "Edit DetailerPipe",
376
+
377
+ "LatentPixelScale": "Latent Scale (on Pixel Space)",
378
+ "IterativeLatentUpscale": "Iterative Upscale (Latent/on Pixel Space)",
379
+ "IterativeImageUpscale": "Iterative Upscale (Image)",
380
+
381
+ "TwoSamplersForMaskUpscalerProvider": "TwoSamplersForMask Upscaler Provider",
382
+ "TwoSamplersForMaskUpscalerProviderPipe": "TwoSamplersForMask Upscaler Provider (pipe)",
383
+
384
+ "ReencodeLatent": "Reencode Latent",
385
+ "ReencodeLatentPipe": "Reencode Latent (pipe)",
386
+
387
+ "ImpactKSamplerBasicPipe": "KSampler (pipe)",
388
+ "ImpactKSamplerAdvancedBasicPipe": "KSampler (Advanced/pipe)",
389
+ "ImpactSEGSLabelAssign": "SEGS Assign (label)",
390
+ "ImpactSEGSLabelFilter": "SEGS Filter (label)",
391
+ "ImpactSEGSRangeFilter": "SEGS Filter (range)",
392
+ "ImpactSEGSOrderedFilter": "SEGS Filter (ordered)",
393
+ "ImpactSEGSConcat": "SEGS Concat",
394
+ "ImpactSEGSToMaskList": "SEGS to Mask List",
395
+ "ImpactSEGSToMaskBatch": "SEGS to Mask Batch",
396
+ "ImpactSEGSPicker": "Picker (SEGS)",
397
+ "ImpactMakeTileSEGS": "Make Tile SEGS",
398
+
399
+ "ImpactDecomposeSEGS": "Decompose (SEGS)",
400
+ "ImpactAssembleSEGS": "Assemble (SEGS)",
401
+ "ImpactFrom_SEG_ELT": "From SEG_ELT",
402
+ "ImpactEdit_SEG_ELT": "Edit SEG_ELT",
403
+ "ImpactDilate_Mask_SEG_ELT": "Dilate Mask (SEG_ELT)",
404
+ "ImpactScaleBy_BBOX_SEG_ELT": "ScaleBy BBOX (SEG_ELT)",
405
+ "ImpactDilateMask": "Dilate Mask",
406
+ "ImpactGaussianBlurMask": "Gaussian Blur Mask",
407
+ "ImpactDilateMaskInSEGS": "Dilate Mask (SEGS)",
408
+ "ImpactGaussianBlurMaskInSEGS": "Gaussian Blur Mask (SEGS)",
409
+
410
+ "PreviewBridge": "Preview Bridge (Image)",
411
+ "PreviewBridgeLatent": "Preview Bridge (Latent)",
412
+ "ImageSender": "Image Sender",
413
+ "ImageReceiver": "Image Receiver",
414
+ "ImageMaskSwitch": "Switch (images, mask)",
415
+ "ImpactSwitch": "Switch (Any)",
416
+ "ImpactInversedSwitch": "Inversed Switch (Any)",
417
+
418
+ "MasksToMaskList": "Masks to Mask List",
419
+ "MaskListToMaskBatch": "Mask List to Masks",
420
+ "ImpactImageBatchToImageList": "Image batch to Image List",
421
+ "ImageListToImageBatch": "Image List to Image Batch",
422
+ "ImpactMakeImageList": "Make Image List",
423
+ "ImpactMakeImageBatch": "Make Image Batch",
424
+ "ImpactStringSelector": "String Selector",
425
+ "ImpactIsNotEmptySEGS": "SEGS isn't Empty",
426
+ "SetDefaultImageForSEGS": "Set Default Image for SEGS",
427
+ "RemoveImageFromSEGS": "Remove Image from SEGS",
428
+
429
+ "RemoveNoiseMask": "Remove Noise Mask",
430
+
431
+ "ImpactCombineConditionings": "Combine Conditionings",
432
+ "ImpactConcatConditionings": "Concat Conditionings",
433
+
434
+ "ImpactQueueTrigger": "Queue Trigger",
435
+ "ImpactQueueTriggerCountdown": "Queue Trigger (Countdown)",
436
+ "ImpactSetWidgetValue": "Set Widget Value",
437
+ "ImpactNodeSetMuteState": "Set Mute State",
438
+ "ImpactControlBridge": "Control Bridge",
439
+ "ImpactSleep": "Sleep",
440
+ "ImpactRemoteBoolean": "Remote Boolean (on prompt)",
441
+ "ImpactRemoteInt": "Remote Int (on prompt)",
442
+
443
+ "ImpactHFTransformersClassifierProvider": "HF Transformers Classifier Provider",
444
+ "ImpactSEGSClassify": "SEGS Classify",
445
+
446
+ "LatentSwitch": "Switch (latent/legacy)",
447
+ "SEGSSwitch": "Switch (SEGS/legacy)",
448
+
449
+ "SEGSPreviewCNet": "SEGSPreview (CNET Image)"
450
+ }
451
+
452
+ if not impact.config.get_config()['mmdet_skip']:
453
+ from impact.mmdet_nodes import *
454
+ import impact.legacy_nodes
455
+ NODE_CLASS_MAPPINGS.update({
456
+ "MMDetDetectorProvider": MMDetDetectorProvider,
457
+ "MMDetLoader": impact.legacy_nodes.MMDetLoader,
458
+ "MaskPainter": impact.legacy_nodes.MaskPainter,
459
+ "SegsMaskCombine": impact.legacy_nodes.SegsMaskCombine,
460
+ "BboxDetectorForEach": impact.legacy_nodes.BboxDetectorForEach,
461
+ "SegmDetectorForEach": impact.legacy_nodes.SegmDetectorForEach,
462
+ "BboxDetectorCombined": impact.legacy_nodes.BboxDetectorCombined,
463
+ "SegmDetectorCombined": impact.legacy_nodes.SegmDetectorCombined,
464
+ })
465
+
466
+ NODE_DISPLAY_NAME_MAPPINGS.update({
467
+ "MaskPainter": "MaskPainter (Deprecated)",
468
+ "MMDetLoader": "MMDetLoader (Legacy)",
469
+ "SegsMaskCombine": "SegsMaskCombine (Legacy)",
470
+ "BboxDetectorForEach": "BboxDetectorForEach (Legacy)",
471
+ "SegmDetectorForEach": "SegmDetectorForEach (Legacy)",
472
+ "BboxDetectorCombined": "BboxDetectorCombined (Legacy)",
473
+ "SegmDetectorCombined": "SegmDetectorCombined (Legacy)",
474
+ })
475
+
476
+ try:
477
+ import impact.subpack_nodes
478
+
479
+ NODE_CLASS_MAPPINGS.update(impact.subpack_nodes.NODE_CLASS_MAPPINGS)
480
+ NODE_DISPLAY_NAME_MAPPINGS.update(impact.subpack_nodes.NODE_DISPLAY_NAME_MAPPINGS)
481
+ except Exception as e:
482
+ print("### ComfyUI-Impact-Pack: (IMPORT FAILED) Subpack\n")
483
+ print(" The module at the `custom_nodes/ComfyUI-Impact-Pack/impact_subpack` path appears to be incomplete.")
484
+ print(" Recommended to delete the path and restart ComfyUI.")
485
+ print(" If the issue persists, please report it to https://github.com/ltdrdata/ComfyUI-Impact-Pack/issues.")
486
+ print("\n---------------------------------")
487
+ traceback.print_exc()
488
+ print("---------------------------------\n")
489
+
490
+ WEB_DIRECTORY = "js"
491
+ __all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
492
+
493
+
494
+ try:
495
+ import cm_global
496
+ cm_global.register_extension('ComfyUI-Impact-Pack',
497
+ {'version': config.version_code,
498
+ 'name': 'Impact Pack',
499
+ 'nodes': set(NODE_CLASS_MAPPINGS.keys()),
500
+ 'description': 'This extension provides inpainting functionality based on the detector and detailer, along with convenient workflow features like wildcards and logics.', })
501
+ except:
502
+ pass
custom_nodes/ComfyUI-Impact-Pack/custom_wildcards/put_wildcards_here ADDED
File without changes
custom_nodes/ComfyUI-Impact-Pack/disable.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import platform
5
+ import shutil
6
+ import subprocess
7
+
8
+ comfy_path = '../..'
9
+
10
+ def rmtree(path):
11
+ retry_count = 3
12
+
13
+ while True:
14
+ try:
15
+ retry_count -= 1
16
+
17
+ if platform.system() == "Windows":
18
+ subprocess.check_call(['attrib', '-R', path + '\\*', '/S'])
19
+
20
+ shutil.rmtree(path)
21
+
22
+ return True
23
+
24
+ except Exception as ex:
25
+ print(f"ex: {ex}")
26
+ time.sleep(3)
27
+
28
+ if retry_count < 0:
29
+ raise ex
30
+
31
+ print(f"Uninstall retry({retry_count})")
32
+
33
+ js_dest_path = os.path.join(comfy_path, "web", "extensions", "impact-pack")
34
+
35
+ if os.path.exists(js_dest_path):
36
+ rmtree(js_dest_path)
37
+
38
+
custom_nodes/ComfyUI-Impact-Pack/impact-pack.ini ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ [default]
2
+ dependency_version = 20
3
+ mmdet_skip = True
4
+ sam_editor_cpu = False
5
+ sam_editor_model = sam_vit_b_01ec64.pth
6
+ custom_wildcards = /home/tiger/Magic-ComfyUI/custom_nodes/ComfyUI-Impact-Pack/custom_wildcards
7
+ disable_gpu_opencv = True
8
+
custom_nodes/ComfyUI-Impact-Pack/impact_subpack/LICENSE ADDED
@@ -0,0 +1,661 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU AFFERO GENERAL PUBLIC LICENSE
2
+ Version 3, 19 November 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+ Preamble
9
+
10
+ The GNU Affero General Public License is a free, copyleft license for
11
+ software and other kinds of works, specifically designed to ensure
12
+ cooperation with the community in the case of network server software.
13
+
14
+ The licenses for most software and other practical works are designed
15
+ to take away your freedom to share and change the works. By contrast,
16
+ our General Public Licenses are intended to guarantee your freedom to
17
+ share and change all versions of a program--to make sure it remains free
18
+ software for all its users.
19
+
20
+ When we speak of free software, we are referring to freedom, not
21
+ price. Our General Public Licenses are designed to make sure that you
22
+ have the freedom to distribute copies of free software (and charge for
23
+ them if you wish), that you receive source code or can get it if you
24
+ want it, that you can change the software or use pieces of it in new
25
+ free programs, and that you know you can do these things.
26
+
27
+ Developers that use our General Public Licenses protect your rights
28
+ with two steps: (1) assert copyright on the software, and (2) offer
29
+ you this License which gives you legal permission to copy, distribute
30
+ and/or modify the software.
31
+
32
+ A secondary benefit of defending all users' freedom is that
33
+ improvements made in alternate versions of the program, if they
34
+ receive widespread use, become available for other developers to
35
+ incorporate. Many developers of free software are heartened and
36
+ encouraged by the resulting cooperation. However, in the case of
37
+ software used on network servers, this result may fail to come about.
38
+ The GNU General Public License permits making a modified version and
39
+ letting the public access it on a server without ever releasing its
40
+ source code to the public.
41
+
42
+ The GNU Affero General Public License is designed specifically to
43
+ ensure that, in such cases, the modified source code becomes available
44
+ to the community. It requires the operator of a network server to
45
+ provide the source code of the modified version running there to the
46
+ users of that server. Therefore, public use of a modified version, on
47
+ a publicly accessible server, gives the public access to the source
48
+ code of the modified version.
49
+
50
+ An older license, called the Affero General Public License and
51
+ published by Affero, was designed to accomplish similar goals. This is
52
+ a different license, not a version of the Affero GPL, but Affero has
53
+ released a new version of the Affero GPL which permits relicensing under
54
+ this license.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ TERMS AND CONDITIONS
60
+
61
+ 0. Definitions.
62
+
63
+ "This License" refers to version 3 of the GNU Affero General Public License.
64
+
65
+ "Copyright" also means copyright-like laws that apply to other kinds of
66
+ works, such as semiconductor masks.
67
+
68
+ "The Program" refers to any copyrightable work licensed under this
69
+ License. Each licensee is addressed as "you". "Licensees" and
70
+ "recipients" may be individuals or organizations.
71
+
72
+ To "modify" a work means to copy from or adapt all or part of the work
73
+ in a fashion requiring copyright permission, other than the making of an
74
+ exact copy. The resulting work is called a "modified version" of the
75
+ earlier work or a work "based on" the earlier work.
76
+
77
+ A "covered work" means either the unmodified Program or a work based
78
+ on the Program.
79
+
80
+ To "propagate" a work means to do anything with it that, without
81
+ permission, would make you directly or secondarily liable for
82
+ infringement under applicable copyright law, except executing it on a
83
+ computer or modifying a private copy. Propagation includes copying,
84
+ distribution (with or without modification), making available to the
85
+ public, and in some countries other activities as well.
86
+
87
+ To "convey" a work means any kind of propagation that enables other
88
+ parties to make or receive copies. Mere interaction with a user through
89
+ a computer network, with no transfer of a copy, is not conveying.
90
+
91
+ An interactive user interface displays "Appropriate Legal Notices"
92
+ to the extent that it includes a convenient and prominently visible
93
+ feature that (1) displays an appropriate copyright notice, and (2)
94
+ tells the user that there is no warranty for the work (except to the
95
+ extent that warranties are provided), that licensees may convey the
96
+ work under this License, and how to view a copy of this License. If
97
+ the interface presents a list of user commands or options, such as a
98
+ menu, a prominent item in the list meets this criterion.
99
+
100
+ 1. Source Code.
101
+
102
+ The "source code" for a work means the preferred form of the work
103
+ for making modifications to it. "Object code" means any non-source
104
+ form of a work.
105
+
106
+ A "Standard Interface" means an interface that either is an official
107
+ standard defined by a recognized standards body, or, in the case of
108
+ interfaces specified for a particular programming language, one that
109
+ is widely used among developers working in that language.
110
+
111
+ The "System Libraries" of an executable work include anything, other
112
+ than the work as a whole, that (a) is included in the normal form of
113
+ packaging a Major Component, but which is not part of that Major
114
+ Component, and (b) serves only to enable use of the work with that
115
+ Major Component, or to implement a Standard Interface for which an
116
+ implementation is available to the public in source code form. A
117
+ "Major Component", in this context, means a major essential component
118
+ (kernel, window system, and so on) of the specific operating system
119
+ (if any) on which the executable work runs, or a compiler used to
120
+ produce the work, or an object code interpreter used to run it.
121
+
122
+ The "Corresponding Source" for a work in object code form means all
123
+ the source code needed to generate, install, and (for an executable
124
+ work) run the object code and to modify the work, including scripts to
125
+ control those activities. However, it does not include the work's
126
+ System Libraries, or general-purpose tools or generally available free
127
+ programs which are used unmodified in performing those activities but
128
+ which are not part of the work. For example, Corresponding Source
129
+ includes interface definition files associated with source files for
130
+ the work, and the source code for shared libraries and dynamically
131
+ linked subprograms that the work is specifically designed to require,
132
+ such as by intimate data communication or control flow between those
133
+ subprograms and other parts of the work.
134
+
135
+ The Corresponding Source need not include anything that users
136
+ can regenerate automatically from other parts of the Corresponding
137
+ Source.
138
+
139
+ The Corresponding Source for a work in source code form is that
140
+ same work.
141
+
142
+ 2. Basic Permissions.
143
+
144
+ All rights granted under this License are granted for the term of
145
+ copyright on the Program, and are irrevocable provided the stated
146
+ conditions are met. This License explicitly affirms your unlimited
147
+ permission to run the unmodified Program. The output from running a
148
+ covered work is covered by this License only if the output, given its
149
+ content, constitutes a covered work. This License acknowledges your
150
+ rights of fair use or other equivalent, as provided by copyright law.
151
+
152
+ You may make, run and propagate covered works that you do not
153
+ convey, without conditions so long as your license otherwise remains
154
+ in force. You may convey covered works to others for the sole purpose
155
+ of having them make modifications exclusively for you, or provide you
156
+ with facilities for running those works, provided that you comply with
157
+ the terms of this License in conveying all material for which you do
158
+ not control copyright. Those thus making or running the covered works
159
+ for you must do so exclusively on your behalf, under your direction
160
+ and control, on terms that prohibit them from making any copies of
161
+ your copyrighted material outside their relationship with you.
162
+
163
+ Conveying under any other circumstances is permitted solely under
164
+ the conditions stated below. Sublicensing is not allowed; section 10
165
+ makes it unnecessary.
166
+
167
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168
+
169
+ No covered work shall be deemed part of an effective technological
170
+ measure under any applicable law fulfilling obligations under article
171
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172
+ similar laws prohibiting or restricting circumvention of such
173
+ measures.
174
+
175
+ When you convey a covered work, you waive any legal power to forbid
176
+ circumvention of technological measures to the extent such circumvention
177
+ is effected by exercising rights under this License with respect to
178
+ the covered work, and you disclaim any intention to limit operation or
179
+ modification of the work as a means of enforcing, against the work's
180
+ users, your or third parties' legal rights to forbid circumvention of
181
+ technological measures.
182
+
183
+ 4. Conveying Verbatim Copies.
184
+
185
+ You may convey verbatim copies of the Program's source code as you
186
+ receive it, in any medium, provided that you conspicuously and
187
+ appropriately publish on each copy an appropriate copyright notice;
188
+ keep intact all notices stating that this License and any
189
+ non-permissive terms added in accord with section 7 apply to the code;
190
+ keep intact all notices of the absence of any warranty; and give all
191
+ recipients a copy of this License along with the Program.
192
+
193
+ You may charge any price or no price for each copy that you convey,
194
+ and you may offer support or warranty protection for a fee.
195
+
196
+ 5. Conveying Modified Source Versions.
197
+
198
+ You may convey a work based on the Program, or the modifications to
199
+ produce it from the Program, in the form of source code under the
200
+ terms of section 4, provided that you also meet all of these conditions:
201
+
202
+ a) The work must carry prominent notices stating that you modified
203
+ it, and giving a relevant date.
204
+
205
+ b) The work must carry prominent notices stating that it is
206
+ released under this License and any conditions added under section
207
+ 7. This requirement modifies the requirement in section 4 to
208
+ "keep intact all notices".
209
+
210
+ c) You must license the entire work, as a whole, under this
211
+ License to anyone who comes into possession of a copy. This
212
+ License will therefore apply, along with any applicable section 7
213
+ additional terms, to the whole of the work, and all its parts,
214
+ regardless of how they are packaged. This License gives no
215
+ permission to license the work in any other way, but it does not
216
+ invalidate such permission if you have separately received it.
217
+
218
+ d) If the work has interactive user interfaces, each must display
219
+ Appropriate Legal Notices; however, if the Program has interactive
220
+ interfaces that do not display Appropriate Legal Notices, your
221
+ work need not make them do so.
222
+
223
+ A compilation of a covered work with other separate and independent
224
+ works, which are not by their nature extensions of the covered work,
225
+ and which are not combined with it such as to form a larger program,
226
+ in or on a volume of a storage or distribution medium, is called an
227
+ "aggregate" if the compilation and its resulting copyright are not
228
+ used to limit the access or legal rights of the compilation's users
229
+ beyond what the individual works permit. Inclusion of a covered work
230
+ in an aggregate does not cause this License to apply to the other
231
+ parts of the aggregate.
232
+
233
+ 6. Conveying Non-Source Forms.
234
+
235
+ You may convey a covered work in object code form under the terms
236
+ of sections 4 and 5, provided that you also convey the
237
+ machine-readable Corresponding Source under the terms of this License,
238
+ in one of these ways:
239
+
240
+ a) Convey the object code in, or embodied in, a physical product
241
+ (including a physical distribution medium), accompanied by the
242
+ Corresponding Source fixed on a durable physical medium
243
+ customarily used for software interchange.
244
+
245
+ b) Convey the object code in, or embodied in, a physical product
246
+ (including a physical distribution medium), accompanied by a
247
+ written offer, valid for at least three years and valid for as
248
+ long as you offer spare parts or customer support for that product
249
+ model, to give anyone who possesses the object code either (1) a
250
+ copy of the Corresponding Source for all the software in the
251
+ product that is covered by this License, on a durable physical
252
+ medium customarily used for software interchange, for a price no
253
+ more than your reasonable cost of physically performing this
254
+ conveying of source, or (2) access to copy the
255
+ Corresponding Source from a network server at no charge.
256
+
257
+ c) Convey individual copies of the object code with a copy of the
258
+ written offer to provide the Corresponding Source. This
259
+ alternative is allowed only occasionally and noncommercially, and
260
+ only if you received the object code with such an offer, in accord
261
+ with subsection 6b.
262
+
263
+ d) Convey the object code by offering access from a designated
264
+ place (gratis or for a charge), and offer equivalent access to the
265
+ Corresponding Source in the same way through the same place at no
266
+ further charge. You need not require recipients to copy the
267
+ Corresponding Source along with the object code. If the place to
268
+ copy the object code is a network server, the Corresponding Source
269
+ may be on a different server (operated by you or a third party)
270
+ that supports equivalent copying facilities, provided you maintain
271
+ clear directions next to the object code saying where to find the
272
+ Corresponding Source. Regardless of what server hosts the
273
+ Corresponding Source, you remain obligated to ensure that it is
274
+ available for as long as needed to satisfy these requirements.
275
+
276
+ e) Convey the object code using peer-to-peer transmission, provided
277
+ you inform other peers where the object code and Corresponding
278
+ Source of the work are being offered to the general public at no
279
+ charge under subsection 6d.
280
+
281
+ A separable portion of the object code, whose source code is excluded
282
+ from the Corresponding Source as a System Library, need not be
283
+ included in conveying the object code work.
284
+
285
+ A "User Product" is either (1) a "consumer product", which means any
286
+ tangible personal property which is normally used for personal, family,
287
+ or household purposes, or (2) anything designed or sold for incorporation
288
+ into a dwelling. In determining whether a product is a consumer product,
289
+ doubtful cases shall be resolved in favor of coverage. For a particular
290
+ product received by a particular user, "normally used" refers to a
291
+ typical or common use of that class of product, regardless of the status
292
+ of the particular user or of the way in which the particular user
293
+ actually uses, or expects or is expected to use, the product. A product
294
+ is a consumer product regardless of whether the product has substantial
295
+ commercial, industrial or non-consumer uses, unless such uses represent
296
+ the only significant mode of use of the product.
297
+
298
+ "Installation Information" for a User Product means any methods,
299
+ procedures, authorization keys, or other information required to install
300
+ and execute modified versions of a covered work in that User Product from
301
+ a modified version of its Corresponding Source. The information must
302
+ suffice to ensure that the continued functioning of the modified object
303
+ code is in no case prevented or interfered with solely because
304
+ modification has been made.
305
+
306
+ If you convey an object code work under this section in, or with, or
307
+ specifically for use in, a User Product, and the conveying occurs as
308
+ part of a transaction in which the right of possession and use of the
309
+ User Product is transferred to the recipient in perpetuity or for a
310
+ fixed term (regardless of how the transaction is characterized), the
311
+ Corresponding Source conveyed under this section must be accompanied
312
+ by the Installation Information. But this requirement does not apply
313
+ if neither you nor any third party retains the ability to install
314
+ modified object code on the User Product (for example, the work has
315
+ been installed in ROM).
316
+
317
+ The requirement to provide Installation Information does not include a
318
+ requirement to continue to provide support service, warranty, or updates
319
+ for a work that has been modified or installed by the recipient, or for
320
+ the User Product in which it has been modified or installed. Access to a
321
+ network may be denied when the modification itself materially and
322
+ adversely affects the operation of the network or violates the rules and
323
+ protocols for communication across the network.
324
+
325
+ Corresponding Source conveyed, and Installation Information provided,
326
+ in accord with this section must be in a format that is publicly
327
+ documented (and with an implementation available to the public in
328
+ source code form), and must require no special password or key for
329
+ unpacking, reading or copying.
330
+
331
+ 7. Additional Terms.
332
+
333
+ "Additional permissions" are terms that supplement the terms of this
334
+ License by making exceptions from one or more of its conditions.
335
+ Additional permissions that are applicable to the entire Program shall
336
+ be treated as though they were included in this License, to the extent
337
+ that they are valid under applicable law. If additional permissions
338
+ apply only to part of the Program, that part may be used separately
339
+ under those permissions, but the entire Program remains governed by
340
+ this License without regard to the additional permissions.
341
+
342
+ When you convey a copy of a covered work, you may at your option
343
+ remove any additional permissions from that copy, or from any part of
344
+ it. (Additional permissions may be written to require their own
345
+ removal in certain cases when you modify the work.) You may place
346
+ additional permissions on material, added by you to a covered work,
347
+ for which you have or can give appropriate copyright permission.
348
+
349
+ Notwithstanding any other provision of this License, for material you
350
+ add to a covered work, you may (if authorized by the copyright holders of
351
+ that material) supplement the terms of this License with terms:
352
+
353
+ a) Disclaiming warranty or limiting liability differently from the
354
+ terms of sections 15 and 16 of this License; or
355
+
356
+ b) Requiring preservation of specified reasonable legal notices or
357
+ author attributions in that material or in the Appropriate Legal
358
+ Notices displayed by works containing it; or
359
+
360
+ c) Prohibiting misrepresentation of the origin of that material, or
361
+ requiring that modified versions of such material be marked in
362
+ reasonable ways as different from the original version; or
363
+
364
+ d) Limiting the use for publicity purposes of names of licensors or
365
+ authors of the material; or
366
+
367
+ e) Declining to grant rights under trademark law for use of some
368
+ trade names, trademarks, or service marks; or
369
+
370
+ f) Requiring indemnification of licensors and authors of that
371
+ material by anyone who conveys the material (or modified versions of
372
+ it) with contractual assumptions of liability to the recipient, for
373
+ any liability that these contractual assumptions directly impose on
374
+ those licensors and authors.
375
+
376
+ All other non-permissive additional terms are considered "further
377
+ restrictions" within the meaning of section 10. If the Program as you
378
+ received it, or any part of it, contains a notice stating that it is
379
+ governed by this License along with a term that is a further
380
+ restriction, you may remove that term. If a license document contains
381
+ a further restriction but permits relicensing or conveying under this
382
+ License, you may add to a covered work material governed by the terms
383
+ of that license document, provided that the further restriction does
384
+ not survive such relicensing or conveying.
385
+
386
+ If you add terms to a covered work in accord with this section, you
387
+ must place, in the relevant source files, a statement of the
388
+ additional terms that apply to those files, or a notice indicating
389
+ where to find the applicable terms.
390
+
391
+ Additional terms, permissive or non-permissive, may be stated in the
392
+ form of a separately written license, or stated as exceptions;
393
+ the above requirements apply either way.
394
+
395
+ 8. Termination.
396
+
397
+ You may not propagate or modify a covered work except as expressly
398
+ provided under this License. Any attempt otherwise to propagate or
399
+ modify it is void, and will automatically terminate your rights under
400
+ this License (including any patent licenses granted under the third
401
+ paragraph of section 11).
402
+
403
+ However, if you cease all violation of this License, then your
404
+ license from a particular copyright holder is reinstated (a)
405
+ provisionally, unless and until the copyright holder explicitly and
406
+ finally terminates your license, and (b) permanently, if the copyright
407
+ holder fails to notify you of the violation by some reasonable means
408
+ prior to 60 days after the cessation.
409
+
410
+ Moreover, your license from a particular copyright holder is
411
+ reinstated permanently if the copyright holder notifies you of the
412
+ violation by some reasonable means, this is the first time you have
413
+ received notice of violation of this License (for any work) from that
414
+ copyright holder, and you cure the violation prior to 30 days after
415
+ your receipt of the notice.
416
+
417
+ Termination of your rights under this section does not terminate the
418
+ licenses of parties who have received copies or rights from you under
419
+ this License. If your rights have been terminated and not permanently
420
+ reinstated, you do not qualify to receive new licenses for the same
421
+ material under section 10.
422
+
423
+ 9. Acceptance Not Required for Having Copies.
424
+
425
+ You are not required to accept this License in order to receive or
426
+ run a copy of the Program. Ancillary propagation of a covered work
427
+ occurring solely as a consequence of using peer-to-peer transmission
428
+ to receive a copy likewise does not require acceptance. However,
429
+ nothing other than this License grants you permission to propagate or
430
+ modify any covered work. These actions infringe copyright if you do
431
+ not accept this License. Therefore, by modifying or propagating a
432
+ covered work, you indicate your acceptance of this License to do so.
433
+
434
+ 10. Automatic Licensing of Downstream Recipients.
435
+
436
+ Each time you convey a covered work, the recipient automatically
437
+ receives a license from the original licensors, to run, modify and
438
+ propagate that work, subject to this License. You are not responsible
439
+ for enforcing compliance by third parties with this License.
440
+
441
+ An "entity transaction" is a transaction transferring control of an
442
+ organization, or substantially all assets of one, or subdividing an
443
+ organization, or merging organizations. If propagation of a covered
444
+ work results from an entity transaction, each party to that
445
+ transaction who receives a copy of the work also receives whatever
446
+ licenses to the work the party's predecessor in interest had or could
447
+ give under the previous paragraph, plus a right to possession of the
448
+ Corresponding Source of the work from the predecessor in interest, if
449
+ the predecessor has it or can get it with reasonable efforts.
450
+
451
+ You may not impose any further restrictions on the exercise of the
452
+ rights granted or affirmed under this License. For example, you may
453
+ not impose a license fee, royalty, or other charge for exercise of
454
+ rights granted under this License, and you may not initiate litigation
455
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
456
+ any patent claim is infringed by making, using, selling, offering for
457
+ sale, or importing the Program or any portion of it.
458
+
459
+ 11. Patents.
460
+
461
+ A "contributor" is a copyright holder who authorizes use under this
462
+ License of the Program or a work on which the Program is based. The
463
+ work thus licensed is called the contributor's "contributor version".
464
+
465
+ A contributor's "essential patent claims" are all patent claims
466
+ owned or controlled by the contributor, whether already acquired or
467
+ hereafter acquired, that would be infringed by some manner, permitted
468
+ by this License, of making, using, or selling its contributor version,
469
+ but do not include claims that would be infringed only as a
470
+ consequence of further modification of the contributor version. For
471
+ purposes of this definition, "control" includes the right to grant
472
+ patent sublicenses in a manner consistent with the requirements of
473
+ this License.
474
+
475
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
476
+ patent license under the contributor's essential patent claims, to
477
+ make, use, sell, offer for sale, import and otherwise run, modify and
478
+ propagate the contents of its contributor version.
479
+
480
+ In the following three paragraphs, a "patent license" is any express
481
+ agreement or commitment, however denominated, not to enforce a patent
482
+ (such as an express permission to practice a patent or covenant not to
483
+ sue for patent infringement). To "grant" such a patent license to a
484
+ party means to make such an agreement or commitment not to enforce a
485
+ patent against the party.
486
+
487
+ If you convey a covered work, knowingly relying on a patent license,
488
+ and the Corresponding Source of the work is not available for anyone
489
+ to copy, free of charge and under the terms of this License, through a
490
+ publicly available network server or other readily accessible means,
491
+ then you must either (1) cause the Corresponding Source to be so
492
+ available, or (2) arrange to deprive yourself of the benefit of the
493
+ patent license for this particular work, or (3) arrange, in a manner
494
+ consistent with the requirements of this License, to extend the patent
495
+ license to downstream recipients. "Knowingly relying" means you have
496
+ actual knowledge that, but for the patent license, your conveying the
497
+ covered work in a country, or your recipient's use of the covered work
498
+ in a country, would infringe one or more identifiable patents in that
499
+ country that you have reason to believe are valid.
500
+
501
+ If, pursuant to or in connection with a single transaction or
502
+ arrangement, you convey, or propagate by procuring conveyance of, a
503
+ covered work, and grant a patent license to some of the parties
504
+ receiving the covered work authorizing them to use, propagate, modify
505
+ or convey a specific copy of the covered work, then the patent license
506
+ you grant is automatically extended to all recipients of the covered
507
+ work and works based on it.
508
+
509
+ A patent license is "discriminatory" if it does not include within
510
+ the scope of its coverage, prohibits the exercise of, or is
511
+ conditioned on the non-exercise of one or more of the rights that are
512
+ specifically granted under this License. You may not convey a covered
513
+ work if you are a party to an arrangement with a third party that is
514
+ in the business of distributing software, under which you make payment
515
+ to the third party based on the extent of your activity of conveying
516
+ the work, and under which the third party grants, to any of the
517
+ parties who would receive the covered work from you, a discriminatory
518
+ patent license (a) in connection with copies of the covered work
519
+ conveyed by you (or copies made from those copies), or (b) primarily
520
+ for and in connection with specific products or compilations that
521
+ contain the covered work, unless you entered into that arrangement,
522
+ or that patent license was granted, prior to 28 March 2007.
523
+
524
+ Nothing in this License shall be construed as excluding or limiting
525
+ any implied license or other defenses to infringement that may
526
+ otherwise be available to you under applicable patent law.
527
+
528
+ 12. No Surrender of Others' Freedom.
529
+
530
+ If conditions are imposed on you (whether by court order, agreement or
531
+ otherwise) that contradict the conditions of this License, they do not
532
+ excuse you from the conditions of this License. If you cannot convey a
533
+ covered work so as to satisfy simultaneously your obligations under this
534
+ License and any other pertinent obligations, then as a consequence you may
535
+ not convey it at all. For example, if you agree to terms that obligate you
536
+ to collect a royalty for further conveying from those to whom you convey
537
+ the Program, the only way you could satisfy both those terms and this
538
+ License would be to refrain entirely from conveying the Program.
539
+
540
+ 13. Remote Network Interaction; Use with the GNU General Public License.
541
+
542
+ Notwithstanding any other provision of this License, if you modify the
543
+ Program, your modified version must prominently offer all users
544
+ interacting with it remotely through a computer network (if your version
545
+ supports such interaction) an opportunity to receive the Corresponding
546
+ Source of your version by providing access to the Corresponding Source
547
+ from a network server at no charge, through some standard or customary
548
+ means of facilitating copying of software. This Corresponding Source
549
+ shall include the Corresponding Source for any work covered by version 3
550
+ of the GNU General Public License that is incorporated pursuant to the
551
+ following paragraph.
552
+
553
+ Notwithstanding any other provision of this License, you have
554
+ permission to link or combine any covered work with a work licensed
555
+ under version 3 of the GNU General Public License into a single
556
+ combined work, and to convey the resulting work. The terms of this
557
+ License will continue to apply to the part which is the covered work,
558
+ but the work with which it is combined will remain governed by version
559
+ 3 of the GNU General Public License.
560
+
561
+ 14. Revised Versions of this License.
562
+
563
+ The Free Software Foundation may publish revised and/or new versions of
564
+ the GNU Affero General Public License from time to time. Such new versions
565
+ will be similar in spirit to the present version, but may differ in detail to
566
+ address new problems or concerns.
567
+
568
+ Each version is given a distinguishing version number. If the
569
+ Program specifies that a certain numbered version of the GNU Affero General
570
+ Public License "or any later version" applies to it, you have the
571
+ option of following the terms and conditions either of that numbered
572
+ version or of any later version published by the Free Software
573
+ Foundation. If the Program does not specify a version number of the
574
+ GNU Affero General Public License, you may choose any version ever published
575
+ by the Free Software Foundation.
576
+
577
+ If the Program specifies that a proxy can decide which future
578
+ versions of the GNU Affero General Public License can be used, that proxy's
579
+ public statement of acceptance of a version permanently authorizes you
580
+ to choose that version for the Program.
581
+
582
+ Later license versions may give you additional or different
583
+ permissions. However, no additional obligations are imposed on any
584
+ author or copyright holder as a result of your choosing to follow a
585
+ later version.
586
+
587
+ 15. Disclaimer of Warranty.
588
+
589
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597
+
598
+ 16. Limitation of Liability.
599
+
600
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608
+ SUCH DAMAGES.
609
+
610
+ 17. Interpretation of Sections 15 and 16.
611
+
612
+ If the disclaimer of warranty and limitation of liability provided
613
+ above cannot be given local legal effect according to their terms,
614
+ reviewing courts shall apply local law that most closely approximates
615
+ an absolute waiver of all civil liability in connection with the
616
+ Program, unless a warranty or assumption of liability accompanies a
617
+ copy of the Program in return for a fee.
618
+
619
+ END OF TERMS AND CONDITIONS
620
+
621
+ How to Apply These Terms to Your New Programs
622
+
623
+ If you develop a new program, and you want it to be of the greatest
624
+ possible use to the public, the best way to achieve this is to make it
625
+ free software which everyone can redistribute and change under these terms.
626
+
627
+ To do so, attach the following notices to the program. It is safest
628
+ to attach them to the start of each source file to most effectively
629
+ state the exclusion of warranty; and each file should have at least
630
+ the "copyright" line and a pointer to where the full notice is found.
631
+
632
+ <one line to give the program's name and a brief idea of what it does.>
633
+ Copyright (C) <year> <name of author>
634
+
635
+ This program is free software: you can redistribute it and/or modify
636
+ it under the terms of the GNU Affero General Public License as published
637
+ by the Free Software Foundation, either version 3 of the License, or
638
+ (at your option) any later version.
639
+
640
+ This program is distributed in the hope that it will be useful,
641
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
642
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643
+ GNU Affero General Public License for more details.
644
+
645
+ You should have received a copy of the GNU Affero General Public License
646
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
647
+
648
+ Also add information on how to contact you by electronic and paper mail.
649
+
650
+ If your software can interact with users remotely through a computer
651
+ network, you should also make sure that it provides a way for users to
652
+ get its source. For example, if your program is a web application, its
653
+ interface could display a "Source" link that leads users to an archive
654
+ of the code. There are many ways you could offer source, and different
655
+ solutions will be better for different programs; see section 13 for the
656
+ specific requirements.
657
+
658
+ You should also get your employer (if you work as a programmer) or school,
659
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
660
+ For more information on this, and how to apply and follow the GNU AGPL, see
661
+ <https://www.gnu.org/licenses/>.
custom_nodes/ComfyUI-Impact-Pack/impact_subpack/README.md ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ComfyUI-Impact-Subpack
2
+ This extension serves as a complement to the Impact Pack, offering features that are not deemed suitable for inclusion by default in the ComfyUI Impact Pack
3
+
4
+ The nodes in this repository cannot be used standalone and depend on [ComfyUI-Impact-Pack](https://github.com/ltdrdata/ComfyUI-Impact-Pack).
5
+
6
+ ## Nodes
7
+ * UltralyticsDetectorProvider - This node provides an object detection detector based on Ultralystics.
8
+ * By using this Detector Provider, you can replace the existing mmdet-based detector.
9
+
10
+
11
+ ## Credits
12
+
13
+ ComfyUI/[ComfyUI](https://github.com/comfyanonymous/ComfyUI) - A powerful and modular stable diffusion GUI.
14
+
15
+ Bing-su/[adetailer](https://github.com/Bing-su/adetailer/) - This repo sitoryprovides an object detection model and features based on Ultralystics.
16
+
17
+ huggingface/Bingsu/[adetailer](https://huggingface.co/Bingsu/adetailer/tree/main) - This repository offers various models based on Ultralystics.
18
+ * You can download other models supported by the UltralyticsDetectorProvider from here.
custom_nodes/ComfyUI-Impact-Pack/impact_subpack/impact/subcore.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from PIL import Image
3
+
4
+ import impact.core as core
5
+ import cv2
6
+ import numpy as np
7
+ from torchvision.transforms.functional import to_pil_image
8
+ import torch
9
+
10
+ try:
11
+ from ultralytics import YOLO
12
+ except Exception as e:
13
+ print(e)
14
+ print(f"\n!!!!!\n\n[ComfyUI-Impact-Subpack] If this error occurs, please check the following link:\n\thttps://github.com/ltdrdata/ComfyUI-Impact-Pack/blob/Main/troubleshooting/TROUBLESHOOTING.md\n\n!!!!!\n")
15
+ raise e
16
+
17
+
18
+ def load_yolo(model_path: str):
19
+ try:
20
+ return YOLO(model_path)
21
+ except ModuleNotFoundError:
22
+ # https://github.com/ultralytics/ultralytics/issues/3856
23
+ YOLO("yolov8n.pt")
24
+ return YOLO(model_path)
25
+
26
+
27
+ def inference_bbox(
28
+ model,
29
+ image: Image.Image,
30
+ confidence: float = 0.3,
31
+ device: str = "",
32
+ ):
33
+ pred = model(image, conf=confidence, device=device)
34
+
35
+ bboxes = pred[0].boxes.xyxy.cpu().numpy()
36
+ cv2_image = np.array(image)
37
+ if len(cv2_image.shape) == 3:
38
+ cv2_image = cv2_image[:, :, ::-1].copy() # Convert RGB to BGR for cv2 processing
39
+ else:
40
+ # Handle the grayscale image here
41
+ # For example, you might want to convert it to a 3-channel grayscale image for consistency:
42
+ cv2_image = cv2.cvtColor(cv2_image, cv2.COLOR_GRAY2BGR)
43
+ cv2_gray = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2GRAY)
44
+
45
+ segms = []
46
+ for x0, y0, x1, y1 in bboxes:
47
+ cv2_mask = np.zeros(cv2_gray.shape, np.uint8)
48
+ cv2.rectangle(cv2_mask, (int(x0), int(y0)), (int(x1), int(y1)), 255, -1)
49
+ cv2_mask_bool = cv2_mask.astype(bool)
50
+ segms.append(cv2_mask_bool)
51
+
52
+ n, m = bboxes.shape
53
+ if n == 0:
54
+ return [[], [], [], []]
55
+
56
+ results = [[], [], [], []]
57
+ for i in range(len(bboxes)):
58
+ results[0].append(pred[0].names[int(pred[0].boxes[i].cls.item())])
59
+ results[1].append(bboxes[i])
60
+ results[2].append(segms[i])
61
+ results[3].append(pred[0].boxes[i].conf.cpu().numpy())
62
+
63
+ return results
64
+
65
+
66
+ def inference_segm(
67
+ model,
68
+ image: Image.Image,
69
+ confidence: float = 0.3,
70
+ device: str = "",
71
+ ):
72
+ pred = model(image, conf=confidence, device=device)
73
+
74
+ bboxes = pred[0].boxes.xyxy.cpu().numpy()
75
+ n, m = bboxes.shape
76
+ if n == 0:
77
+ return [[], [], [], []]
78
+
79
+ # NOTE: masks.data will be None when n == 0
80
+ segms = pred[0].masks.data.cpu().numpy()
81
+
82
+ results = [[], [], [], []]
83
+ for i in range(len(bboxes)):
84
+ results[0].append(pred[0].names[int(pred[0].boxes[i].cls.item())])
85
+ results[1].append(bboxes[i])
86
+
87
+ mask = torch.from_numpy(segms[i])
88
+ scaled_mask = torch.nn.functional.interpolate(mask.unsqueeze(0).unsqueeze(0), size=(image.size[1], image.size[0]),
89
+ mode='bilinear', align_corners=False)
90
+ scaled_mask = scaled_mask.squeeze().squeeze()
91
+
92
+ results[2].append(scaled_mask.numpy())
93
+ results[3].append(pred[0].boxes[i].conf.cpu().numpy())
94
+
95
+ return results
96
+
97
+
98
+ class UltraBBoxDetector:
99
+ bbox_model = None
100
+
101
+ def __init__(self, bbox_model):
102
+ self.bbox_model = bbox_model
103
+
104
+ def detect(self, image, threshold, dilation, crop_factor, drop_size=1, detailer_hook=None):
105
+ drop_size = max(drop_size, 1)
106
+ detected_results = inference_bbox(self.bbox_model, core.tensor2pil(image), threshold)
107
+ segmasks = core.create_segmasks(detected_results)
108
+
109
+ if dilation > 0:
110
+ segmasks = core.dilate_masks(segmasks, dilation)
111
+
112
+ items = []
113
+ h = image.shape[1]
114
+ w = image.shape[2]
115
+
116
+ for x, label in zip(segmasks, detected_results[0]):
117
+ item_bbox = x[0]
118
+ item_mask = x[1]
119
+
120
+ y1, x1, y2, x2 = item_bbox
121
+
122
+ if x2 - x1 > drop_size and y2 - y1 > drop_size: # minimum dimension must be (2,2) to avoid squeeze issue
123
+ crop_region = core.make_crop_region(w, h, item_bbox, crop_factor)
124
+
125
+ if detailer_hook is not None:
126
+ crop_region = detailer_hook.post_crop_region(w, h, item_bbox, crop_region)
127
+
128
+ cropped_image = core.crop_image(image, crop_region)
129
+ cropped_mask = core.crop_ndarray2(item_mask, crop_region)
130
+ confidence = x[2]
131
+ # bbox_size = (item_bbox[2]-item_bbox[0],item_bbox[3]-item_bbox[1]) # (w,h)
132
+
133
+ item = core.SEG(cropped_image, cropped_mask, confidence, crop_region, item_bbox, label, None)
134
+
135
+ items.append(item)
136
+
137
+ shape = image.shape[1], image.shape[2]
138
+ segs = shape, items
139
+
140
+ if detailer_hook is not None and hasattr(detailer_hook, "post_detection"):
141
+ segs = detailer_hook.post_detection(segs)
142
+
143
+ return segs
144
+
145
+ def detect_combined(self, image, threshold, dilation):
146
+ detected_results = inference_bbox(self.bbox_model, core.tensor2pil(image), threshold)
147
+ segmasks = core.create_segmasks(detected_results)
148
+ if dilation > 0:
149
+ segmasks = core.dilate_masks(segmasks, dilation)
150
+
151
+ return core.combine_masks(segmasks)
152
+
153
+ def setAux(self, x):
154
+ pass
155
+
156
+
157
+ class UltraSegmDetector:
158
+ bbox_model = None
159
+
160
+ def __init__(self, bbox_model):
161
+ self.bbox_model = bbox_model
162
+
163
+ def detect(self, image, threshold, dilation, crop_factor, drop_size=1, detailer_hook=None):
164
+ drop_size = max(drop_size, 1)
165
+ detected_results = inference_segm(self.bbox_model, core.tensor2pil(image), threshold)
166
+ segmasks = core.create_segmasks(detected_results)
167
+
168
+ if dilation > 0:
169
+ segmasks = core.dilate_masks(segmasks, dilation)
170
+
171
+ items = []
172
+ h = image.shape[1]
173
+ w = image.shape[2]
174
+
175
+ for x, label in zip(segmasks, detected_results[0]):
176
+ item_bbox = x[0]
177
+ item_mask = x[1]
178
+
179
+ y1, x1, y2, x2 = item_bbox
180
+
181
+ if x2 - x1 > drop_size and y2 - y1 > drop_size: # minimum dimension must be (2,2) to avoid squeeze issue
182
+ crop_region = core.make_crop_region(w, h, item_bbox, crop_factor)
183
+
184
+ if detailer_hook is not None:
185
+ crop_region = detailer_hook.post_crop_region(w, h, item_bbox, crop_region)
186
+
187
+ cropped_image = core.crop_image(image, crop_region)
188
+ cropped_mask = core.crop_ndarray2(item_mask, crop_region)
189
+ confidence = x[2]
190
+ # bbox_size = (item_bbox[2]-item_bbox[0],item_bbox[3]-item_bbox[1]) # (w,h)
191
+
192
+ item = core.SEG(cropped_image, cropped_mask, confidence, crop_region, item_bbox, label, None)
193
+
194
+ items.append(item)
195
+
196
+ shape = image.shape[1], image.shape[2]
197
+ segs = shape, items
198
+
199
+ if detailer_hook is not None and hasattr(detailer_hook, "post_detection"):
200
+ segs = detailer_hook.post_detection(segs)
201
+
202
+ return segs
203
+
204
+ def detect_combined(self, image, threshold, dilation):
205
+ detected_results = inference_segm(self.bbox_model, core.tensor2pil(image), threshold)
206
+ segmasks = core.create_segmasks(detected_results)
207
+ if dilation > 0:
208
+ segmasks = core.dilate_masks(segmasks, dilation)
209
+
210
+ return core.combine_masks(segmasks)
211
+
212
+ def setAux(self, x):
213
+ pass
custom_nodes/ComfyUI-Impact-Pack/impact_subpack/impact/subpack_nodes.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import folder_paths
3
+ import impact.core as core
4
+ import impact.subcore as subcore
5
+ from impact.utils import add_folder_path_and_extensions
6
+
7
+ version_code = 20
8
+
9
+ print(f"### Loading: ComfyUI-Impact-Pack (Subpack: V0.4)")
10
+
11
+ model_path = folder_paths.models_dir
12
+ add_folder_path_and_extensions("ultralytics_bbox", [os.path.join(model_path, "ultralytics", "bbox")], folder_paths.supported_pt_extensions)
13
+ add_folder_path_and_extensions("ultralytics_segm", [os.path.join(model_path, "ultralytics", "segm")], folder_paths.supported_pt_extensions)
14
+ add_folder_path_and_extensions("ultralytics", [os.path.join(model_path, "ultralytics")], folder_paths.supported_pt_extensions)
15
+
16
+
17
+ class UltralyticsDetectorProvider:
18
+ @classmethod
19
+ def INPUT_TYPES(s):
20
+ bboxs = ["bbox/"+x for x in folder_paths.get_filename_list("ultralytics_bbox")]
21
+ segms = ["segm/"+x for x in folder_paths.get_filename_list("ultralytics_segm")]
22
+ return {"required": {"model_name": (bboxs + segms, )}}
23
+ RETURN_TYPES = ("BBOX_DETECTOR", "SEGM_DETECTOR")
24
+ FUNCTION = "doit"
25
+
26
+ CATEGORY = "ImpactPack"
27
+
28
+ def doit(self, model_name):
29
+ model_path = folder_paths.get_full_path("ultralytics", model_name)
30
+ model = subcore.load_yolo(model_path)
31
+
32
+ if model_name.startswith("bbox"):
33
+ return subcore.UltraBBoxDetector(model), core.NO_SEGM_DETECTOR()
34
+ else:
35
+ return subcore.UltraBBoxDetector(model), subcore.UltraSegmDetector(model)
36
+
37
+
38
+ NODE_CLASS_MAPPINGS = {
39
+ "UltralyticsDetectorProvider": UltralyticsDetectorProvider
40
+ }
41
+
42
+
43
+ NODE_DISPLAY_NAME_MAPPINGS = {
44
+
45
+ }
custom_nodes/ComfyUI-Impact-Pack/impact_subpack/install.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ from torchvision.datasets.utils import download_url
4
+
5
+ subpack_path = os.path.join(os.path.dirname(__file__))
6
+ comfy_path = os.path.join(subpack_path, '..', '..', '..')
7
+
8
+ sys.path.append(comfy_path)
9
+
10
+ import folder_paths
11
+ model_path = folder_paths.models_dir
12
+ ultralytics_bbox_path = os.path.join(model_path, "ultralytics", "bbox")
13
+ ultralytics_segm_path = os.path.join(model_path, "ultralytics", "segm")
14
+
15
+ if not os.path.exists(os.path.join(subpack_path, '..', '..', 'skip_download_model')):
16
+ if not os.path.exists(ultralytics_bbox_path):
17
+ os.makedirs(ultralytics_bbox_path)
18
+
19
+ if not os.path.exists(ultralytics_segm_path):
20
+ os.makedirs(ultralytics_segm_path)
21
+
22
+ if not os.path.exists(os.path.join(ultralytics_bbox_path, "face_yolov8m.pt")):
23
+ download_url("https://huggingface.co/Bingsu/adetailer/resolve/main/face_yolov8m.pt",
24
+ ultralytics_bbox_path)
25
+
26
+ if not os.path.exists(os.path.join(ultralytics_bbox_path, "hand_yolov8s.pt")):
27
+ download_url("https://huggingface.co/Bingsu/adetailer/resolve/main/hand_yolov8s.pt",
28
+ ultralytics_bbox_path)
29
+
30
+ if not os.path.exists(os.path.join(ultralytics_segm_path, "person_yolov8m-seg.pt")):
31
+ download_url("https://huggingface.co/Bingsu/adetailer/resolve/main/person_yolov8m-seg.pt",
32
+ ultralytics_segm_path)
custom_nodes/ComfyUI-Impact-Pack/impact_subpack/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ultralytics!=8.0.177
custom_nodes/ComfyUI-Impact-Pack/install.py ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import sys
4
+ import subprocess
5
+ import threading
6
+ import locale
7
+ import traceback
8
+ import re
9
+
10
+
11
+ if sys.argv[0] == 'install.py':
12
+ sys.path.append('.') # for portable version
13
+
14
+
15
+ impact_path = os.path.join(os.path.dirname(__file__), "modules")
16
+ old_subpack_path = os.path.join(os.path.dirname(__file__), "subpack")
17
+ subpack_path = os.path.join(os.path.dirname(__file__), "impact_subpack")
18
+ subpack_repo = "https://github.com/ltdrdata/ComfyUI-Impact-Subpack"
19
+ comfy_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
20
+
21
+
22
+ sys.path.append(impact_path)
23
+ sys.path.append(comfy_path)
24
+
25
+
26
+ # ---
27
+ def handle_stream(stream, is_stdout):
28
+ stream.reconfigure(encoding=locale.getpreferredencoding(), errors='replace')
29
+
30
+ for msg in stream:
31
+ if is_stdout:
32
+ print(msg, end="", file=sys.stdout)
33
+ else:
34
+ print(msg, end="", file=sys.stderr)
35
+
36
+
37
+ def process_wrap(cmd_str, cwd=None, handler=None):
38
+ print(f"[Impact Pack] EXECUTE: {cmd_str} in '{cwd}'")
39
+ process = subprocess.Popen(cmd_str, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
40
+
41
+ if handler is None:
42
+ handler = handle_stream
43
+
44
+ stdout_thread = threading.Thread(target=handler, args=(process.stdout, True))
45
+ stderr_thread = threading.Thread(target=handler, args=(process.stderr, False))
46
+
47
+ stdout_thread.start()
48
+ stderr_thread.start()
49
+
50
+ stdout_thread.join()
51
+ stderr_thread.join()
52
+
53
+ return process.wait()
54
+ # ---
55
+
56
+
57
+ pip_list = None
58
+
59
+
60
+ def get_installed_packages():
61
+ global pip_list
62
+
63
+ if pip_list is None:
64
+ try:
65
+ result = subprocess.check_output([sys.executable, '-m', 'pip', 'list'], universal_newlines=True)
66
+ pip_list = set([line.split()[0].lower() for line in result.split('\n') if line.strip()])
67
+ except subprocess.CalledProcessError as e:
68
+ print(f"[ComfyUI-Manager] Failed to retrieve the information of installed pip packages.")
69
+ return set()
70
+
71
+ return pip_list
72
+
73
+
74
+ def is_installed(name):
75
+ name = name.strip()
76
+ pattern = r'([^<>!=]+)([<>!=]=?)'
77
+ match = re.search(pattern, name)
78
+
79
+ if match:
80
+ name = match.group(1)
81
+
82
+ result = name.lower() in get_installed_packages()
83
+ return result
84
+
85
+
86
+ def is_requirements_installed(file_path):
87
+ print(f"req_path: {file_path}")
88
+ if os.path.exists(file_path):
89
+ with open(file_path, 'r') as file:
90
+ lines = file.readlines()
91
+ for line in lines:
92
+ if not is_installed(line):
93
+ return False
94
+
95
+ return True
96
+
97
+ try:
98
+ import platform
99
+ import folder_paths
100
+ from torchvision.datasets.utils import download_url
101
+ import impact.config
102
+
103
+
104
+ print("### ComfyUI-Impact-Pack: Check dependencies")
105
+
106
+ if "python_embeded" in sys.executable or "python_embedded" in sys.executable:
107
+ pip_install = [sys.executable, '-s', '-m', 'pip', 'install']
108
+ mim_install = [sys.executable, '-s', '-m', 'mim', 'install']
109
+ else:
110
+ pip_install = [sys.executable, '-m', 'pip', 'install']
111
+ mim_install = [sys.executable, '-m', 'mim', 'install']
112
+
113
+
114
+ def ensure_subpack():
115
+ import git
116
+ if os.path.exists(subpack_path):
117
+ try:
118
+ repo = git.Repo(subpack_path)
119
+ repo.remotes.origin.pull()
120
+ except:
121
+ traceback.print_exc()
122
+ if platform.system() == 'Windows':
123
+ print(f"[ComfyUI-Impact-Pack] Please turn off ComfyUI and remove '{subpack_path}' and restart ComfyUI.")
124
+ else:
125
+ shutil.rmtree(subpack_path)
126
+ git.Repo.clone_from(subpack_repo, subpack_path)
127
+ else:
128
+ git.Repo.clone_from(subpack_repo, subpack_path)
129
+
130
+ if os.path.exists(old_subpack_path):
131
+ shutil.rmtree(old_subpack_path)
132
+
133
+
134
+ def remove_olds():
135
+ global comfy_path
136
+
137
+ comfy_path = os.path.dirname(folder_paths.__file__)
138
+ custom_nodes_path = os.path.join(comfy_path, "custom_nodes")
139
+ old_ini_path = os.path.join(custom_nodes_path, "impact-pack.ini")
140
+ old_py_path = os.path.join(custom_nodes_path, "comfyui-impact-pack.py")
141
+
142
+ if os.path.exists(impact.config.old_config_path):
143
+ impact.config.get_config()['mmdet_skip'] = False
144
+ os.remove(impact.config.old_config_path)
145
+
146
+ if os.path.exists(old_ini_path):
147
+ print(f"Delete legacy file: {old_ini_path}")
148
+ os.remove(old_ini_path)
149
+
150
+ if os.path.exists(old_py_path):
151
+ print(f"Delete legacy file: {old_py_path}")
152
+ os.remove(old_py_path)
153
+
154
+
155
+ def ensure_pip_packages_first():
156
+ subpack_req = os.path.join(subpack_path, "requirements.txt")
157
+ if os.path.exists(subpack_req) and not is_requirements_installed(subpack_req):
158
+ process_wrap(pip_install + ['-r', 'requirements.txt'], cwd=subpack_path)
159
+
160
+ if not impact.config.get_config()['mmdet_skip']:
161
+ process_wrap(pip_install + ['openmim'])
162
+
163
+ try:
164
+ import pycocotools
165
+ except Exception:
166
+ if platform.system() not in ["Windows"] or platform.machine() not in ["AMD64", "x86_64"]:
167
+ print(f"Your system is {platform.system()}; !! You need to install 'libpython3-dev' for this step. !!")
168
+
169
+ process_wrap(pip_install + ['pycocotools'])
170
+ else:
171
+ pycocotools = {
172
+ (3, 8): "https://github.com/Bing-su/dddetailer/releases/download/pycocotools/pycocotools-2.0.6-cp38-cp38-win_amd64.whl",
173
+ (3, 9): "https://github.com/Bing-su/dddetailer/releases/download/pycocotools/pycocotools-2.0.6-cp39-cp39-win_amd64.whl",
174
+ (3, 10): "https://github.com/Bing-su/dddetailer/releases/download/pycocotools/pycocotools-2.0.6-cp310-cp310-win_amd64.whl",
175
+ (3, 11): "https://github.com/Bing-su/dddetailer/releases/download/pycocotools/pycocotools-2.0.6-cp311-cp311-win_amd64.whl",
176
+ }
177
+
178
+ version = sys.version_info[:2]
179
+ url = pycocotools[version]
180
+ process_wrap(pip_install + [url])
181
+
182
+
183
+ def ensure_pip_packages_last():
184
+ my_path = os.path.dirname(__file__)
185
+ requirements_path = os.path.join(my_path, "requirements.txt")
186
+
187
+ if not is_requirements_installed(requirements_path):
188
+ process_wrap(pip_install + ['-r', requirements_path])
189
+
190
+ # fallback
191
+ try:
192
+ import segment_anything
193
+ from skimage.measure import label, regionprops
194
+ import piexif
195
+ except Exception:
196
+ process_wrap(pip_install + ['-r', requirements_path])
197
+
198
+ # !! cv2 importing test must be very last !!
199
+ try:
200
+ import cv2
201
+ except Exception:
202
+ try:
203
+ if not is_installed('opencv-python'):
204
+ process_wrap(pip_install + ['opencv-python'])
205
+ if not is_installed('opencv-python-headless'):
206
+ process_wrap(pip_install + ['opencv-python-headless'])
207
+ except:
208
+ print(f"[ERROR] ComfyUI-Impact-Pack: failed to install 'opencv-python'. Please, install manually.")
209
+
210
+ def ensure_mmdet_package():
211
+ try:
212
+ import mmcv
213
+ import mmdet
214
+ from mmdet.evaluation import get_classes
215
+ except Exception:
216
+ process_wrap(pip_install + ['opendatalab==0.0.9'])
217
+ process_wrap(pip_install + ['-U', 'openmim'])
218
+ process_wrap(mim_install + ['mmcv>=2.0.0rc4, <2.1.0'])
219
+ process_wrap(mim_install + ['mmdet==3.0.0'])
220
+ process_wrap(mim_install + ['mmengine==0.7.4'])
221
+
222
+
223
+ def install():
224
+ remove_olds()
225
+
226
+ subpack_install_script = os.path.join(subpack_path, "install.py")
227
+
228
+ print(f"### ComfyUI-Impact-Pack: Updating subpack")
229
+ try:
230
+ import git
231
+ except Exception:
232
+ if not is_installed('GitPython'):
233
+ process_wrap(pip_install + ['GitPython'])
234
+
235
+ ensure_subpack() # The installation of the subpack must take place before ensure_pip. cv2 triggers a permission error.
236
+
237
+ if os.path.exists(subpack_install_script):
238
+ process_wrap([sys.executable, 'install.py'], cwd=subpack_path)
239
+ if not is_requirements_installed(os.path.join(subpack_path, 'requirements.txt')):
240
+ process_wrap(pip_install + ['-r', 'requirements.txt'], cwd=subpack_path)
241
+ else:
242
+ print(f"### ComfyUI-Impact-Pack: (Install Failed) Subpack\nFile not found: `{subpack_install_script}`")
243
+
244
+ ensure_pip_packages_first()
245
+
246
+ if not impact.config.get_config()['mmdet_skip']:
247
+ ensure_mmdet_package()
248
+
249
+ ensure_pip_packages_last()
250
+
251
+ # Download model
252
+ print("### ComfyUI-Impact-Pack: Check basic models")
253
+
254
+ model_path = folder_paths.models_dir
255
+
256
+ bbox_path = os.path.join(model_path, "mmdets", "bbox")
257
+ sam_path = os.path.join(model_path, "sams")
258
+ onnx_path = os.path.join(model_path, "onnx")
259
+
260
+ if not os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'skip_download_model')):
261
+ if not os.path.exists(bbox_path):
262
+ os.makedirs(bbox_path)
263
+
264
+ if not impact.config.get_config()['mmdet_skip']:
265
+ if not os.path.exists(os.path.join(bbox_path, "mmdet_anime-face_yolov3.pth")):
266
+ download_url("https://huggingface.co/dustysys/ddetailer/resolve/main/mmdet/bbox/mmdet_anime-face_yolov3.pth", bbox_path)
267
+
268
+ if not os.path.exists(os.path.join(bbox_path, "mmdet_anime-face_yolov3.py")):
269
+ download_url("https://raw.githubusercontent.com/Bing-su/dddetailer/master/config/mmdet_anime-face_yolov3.py", bbox_path)
270
+
271
+ if not os.path.exists(os.path.join(sam_path, "sam_vit_b_01ec64.pth")):
272
+ download_url("https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth", sam_path)
273
+
274
+ if not os.path.exists(onnx_path):
275
+ print(f"### ComfyUI-Impact-Pack: onnx model directory created ({onnx_path})")
276
+ os.mkdir(onnx_path)
277
+
278
+ impact.config.write_config()
279
+
280
+
281
+ install()
282
+
283
+ except Exception as e:
284
+ print("[ERROR] ComfyUI-Impact-Pack: Dependency installation has failed. Please install manually.")
285
+ traceback.print_exc()
custom_nodes/ComfyUI-Impact-Pack/js/comboBoolMigration.js ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { ComfyApp, app } from "../../scripts/app.js";
2
+
3
+ let conflict_check = undefined;
4
+
5
+ app.registerExtension({
6
+ name: "Comfy.impact.comboBoolMigration",
7
+
8
+ nodeCreated(node, app) {
9
+ for(let i in node.widgets) {
10
+ let widget = node.widgets[i];
11
+
12
+ if(conflict_check == undefined) {
13
+ conflict_check = !!app.extensions.find((ext) => ext.name === "Comfy.comboBoolMigration");
14
+ }
15
+
16
+ if(conflict_check)
17
+ return;
18
+
19
+ if(widget.type == "toggle") {
20
+ let value = widget.value;
21
+
22
+ var v = Object.getOwnPropertyDescriptor(widget, 'value');
23
+ if(!v) {
24
+ Object.defineProperty(widget, "value", {
25
+ set: (value) => {
26
+ delete widget.value;
27
+ widget.value = value == true || value == widget.options.on;
28
+ },
29
+ get: () => { return value; }
30
+ });
31
+ }
32
+ }
33
+ }
34
+ }
35
+ });