
    ^(j͚                    l
   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZmZmZ d dlmZ d dlZd dlZej,                  j/                  d ej,                  j1                  ej,                  j3                  ej,                  j5                  e            d             d dlZd dlZd dlZd dl Zd dl!Zd dl"Zd dl#m$Z$m%Z%m&Z&m'Z' d dl(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/m0Z0 d dl1Zd dl2Zd d	l3m4Z4 d dl5Z5d dl6Z6d dl7Z7d dl8Z8e4jr                  rd dl:Z:d
 Z;ddZ<dZ= G d de%      Z> G d d      Z? G d d      Z@ G d d      ZA G d d      ZB G d d      ZC G d d      ZD G d d      ZE G d d      ZF G d d       ZG G d! d"      ZH G d# d$      ZI G d% d&      ZJ G d' d(      ZK G d) d*      ZL G d+ d,      ZM G d- d.      ZN G d/ d0      ZO G d1 d2      ZP G d3 d4      ZQ G d5 d6      ZR G d7 d8      ZS G d9 d:      ZT G d; d<      ZU G d= d>eU      ZV G d? d@      ZW G dA dB      ZX G dC dD      ZY G dE dF      ZZ G dG dH      Z[ G dI dJ      Z\ G dK dL      Z] G dM dN      Z^ G dO dP      Z_ G dQ dR      Z` G dS dT      Za G dU dV      Zb G dW dX      Zc G dY dZ      Zd G d[ d\      Ze G d] d^      Zf G d_ d`      Zg G da db      Zh G dc dd      Zi G de df      Zj G dg dh      Zk G di dj      Zl G dk dl      Zm G dm dn      Zn G do dp      Zo G dq dr      ZpddsZq G dt du      Zr G dv dw      Zs G dx dy      Zt G dz d{et      Zu G d| d}      Zv G d~ dev      Zw G d dev      Zx G d d      Zy G d d      Zz G d d      Z{ G d d      Z| G d d      Z} G d d      Z~i duerd4eQde>d:eTd"eHd&eJd*eLd@eWd^efddeidfejd`egdbehdyetd{eud}evdewi dexdeydezde{de|de~de}de@de?deAdeBdeCdeDdeEdwesdrepdlemi dnendhekdjeldpeod<eUdLe]dJe\dNe^dRe`dVebdXecdFeZdHe[dBeXdDeYdTeadPe_eIeKeSedeeeMePeReOeNeFeGeVdZi dududwdd2dd4dd@dd<dd>ddLddBddDddTddPddJdd8ddZddRddVdi ddd:ddddddddddddddFddHdd*ddrdd"dd&ddhddjddpdi d^dddddfddlddndd`ddbddddydd{dd}ddddddddddddddddddddddddd͜Zi Zi ZdedefdЄZ e       dfdedefd҄Zdӄ ZdԄ ZdՄ Zdք ZddׄZy)    N)ImageImageOpsImageSequence)PngInfocomfy)IOComfyNodeABCInputTypeDictFileLocator)register_versionsComfyAPIWithVersion)supported_versions)ioComfyExtension	InputImpl)argsc                  @    t         j                  j                          y N)r   model_management)throw_exception_if_processing_interrupted     //Users/danicosta/Desktop/Flux2/ComfyUI/nodes.pybefore_node_executionr   2   s    	DDFr   c                 B    t         j                  j                  |        y r   )r   r   interrupt_current_processing)values    r   interrupt_processingr   5   s    	77>r   i @  c                   Z    e Zd Zedefd       Zej                  fZdZ	dZ
dZdZg dZd Zy	)
CLIPTextEncodereturnc                 \    dt         j                  ddddft         j                  ddifdiS )NrequiredTzThe text to be encoded.)	multilinedynamicPromptstooltipr&   z*The CLIP model used for encoding the text.)textclip)r   STRINGCLIPss    r   INPUT_TYPESzCLIPTextEncode.INPUT_TYPES;   s<     $$[t$uv9.Z"[\
 	
r   )zNA conditioning containing the embedded text used to guide the diffusion model.encodemodel/conditioningzEncodes a text prompt using a CLIP model into an embedding that can be used to guide the diffusion model towards generating specific images.)r'   promptztext promptzpositive promptznegative promptzencode textztext encoderzencode promptc                 b    |t        d      |j                  |      }|j                  |      fS )NzERROR: clip input is invalid: None

If the clip is from a checkpoint loader node your checkpoint does not contain a valid clip or text encoder model.)RuntimeErrortokenizeencode_from_tokens_scheduled)selfr(   r'   tokenss       r   r.   zCLIPTextEncode.encodeK   s>    <   y  z  zt$11&9<<r   N)__name__
__module____qualname__classmethodr
   r-   r   CONDITIONINGRETURN_TYPESOUTPUT_TOOLTIPSFUNCTIONCATEGORYDESCRIPTIONSEARCH_ALIASESr.   r   r   r   r    r    :   sM    
- 
 
 OO%LiOH#H aK MN=r   r    c                   :    e Zd ZdZed        ZdZdZdZg dZ	d Z
y)	ConditioningCombineImage Generationc                     ddddiS )Nr#   r;   )conditioning_1conditioning_2r   r+   s    r   r-   zConditioningCombine.INPUT_TYPEST   s    /AUghiir   rF   combiner/   )rI   zmerge conditioningzcombine promptszmerge promptsmix promptsz
add promptc                     ||z   fS r   r   )r5   rG   rH   s      r   rI   zConditioningCombine.combine]   s    /22r   N)r7   r8   r9   ESSENTIALS_CATEGORYr:   r-   r<   r>   r?   rA   rI   r   r   r   rC   rC   R   s5    ,j j$LH#HwN3r   rC   c                   6    e Zd Zg dZed        ZdZdZdZd Z	y)ConditioningAverage)zblend promptszinterpolate conditioningrJ   zstyle fusionzweighted blendc           	           dddddddddfdiS )	Nr#   rF   FLOAT      ?        {Gz?defaultminmaxstep)conditioning_toconditioning_fromconditioning_to_strengthr   r+   s    r   r-   zConditioningAverage.INPUT_TYPESc   s/    0BYk;BPS\_hkuyDz:{   	 r   rF   addWeightedr/   c           
      b   g }t        |      dkD  rt        j                  d       |d   d   }|d   d   j                  dd       }t	        t        |            D ]P  }||   d   }||   d   j                  d|      }	|d d d |j
                  d   f   }
|
j
                  d   |j
                  d   k  r\t        j                  |
gt        j                  d|j
                  d   |
j
                  d   z
  |j
                  d   f      gz   d      }
t        j                  ||      t        j                  |
d|z
        z   }||   d   j                         }|6|	4t        j                  |	|      t        j                  |d|z
        z   |d<   n|||d<   ||g}|j                  |       S |fS )N   zWarning: ConditioningAverage conditioning_from contains more than 1 cond, only the first one will actually be applied to conditioning_to.r   pooled_output   dimrQ   )lenloggingwarninggetrangeshapetorchcatzerosmulcopyappend)r5   rY   rZ   r[   out	cond_frompooled_output_fromit1pooled_output_tot0twt_tons                 r   r\   zConditioningAverage.addWeightedm   s    !A%OO  h  i%a(+	.q1!488$Os?+,A #A&B.q1!488J\]1\bhhqk\>*Bxx{RXXa[(YYtu{{Abhhqk8QTVT\T\]^T_3`'a&bbhij278599R#PhJh;jjB"1%a(--/D!-2B2N(-		2BD\(]`e`i`ij|  @C  F^  @^  a`  )`_%#/(:_%T
AJJqM -  wr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r\   r   r   r   rN   rN   `   s/    sN    %LH#Hr   rN   c                   .    e Zd Zed        ZdZdZdZd Zy)ConditioningConcatc                     ddddiS )Nr#   rF   )rY   rZ   r   r+   s    r   r-   zConditioningConcat.INPUT_TYPES   s    0!2  	r   rF   concatr/   c                 &   g }t        |      dkD  rt        j                  d       |d   d   }t        t        |            D ]K  }||   d   }t	        j
                  ||fd      }|||   d   j                         g}|j                  |       M |fS )Nr^   zWarning: ConditioningConcat conditioning_from contains more than 1 cond, only the first one will actually be applied to conditioning_to.r   )rc   rd   re   rg   ri   rj   rm   rn   )	r5   rY   rZ   ro   rp   rr   rs   rv   rx   s	            r   r|   zConditioningConcat.concat   s     !A%OO  g  h%a(+	s?+,A #A&BB	?1-B_Q'*//12AJJqM	 - wr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r|   r   r   r   rz   rz      s*     
 %LH#Hr   rz   c                   6    e Zd Zg dZed        ZdZdZdZd Z	y)ConditioningSetArea)zregional promptzarea promptzspatial conditioningzlocalized promptc                 ~    dddddt         ddfdddt         ddfdddt         ddfdddt         ddfdd	d
dddfdiS )Nr#   rF   INT@      rT   r   rP   rQ   rR         $@rS   conditioningwidthheightxystrengthMAX_RESOLUTIONr+   s    r   r-   zConditioningSetArea.INPUT_TYPES   st    -?(-2bQ_ij/k'l).BrR`jk0l(m$)q>cd+e#f$)q>cd+e#f+2CX\fj4k*l   	 r   rF   rn   r/   c                 \    t        j                  ||dz  |dz  |dz  |dz  f|dd      }|fS )Nr   Farear   set_area_to_boundsnode_helpersconditioning_set_valuesr5   r   r   r   r   r   r   cs           r   rn   zConditioningSetArea.append   sP    00STV[_`V`bcghbhjkopjpHqLTV[@] ^ ur   N
r7   r8   r9   rA   r:   r-   r<   r>   r?   rn   r   r   r   r   r      s/    cN    %LH#Hr   r   c                   .    e Zd Zed        ZdZdZdZd Zy)ConditioningSetAreaPercentagec                 ^    ddddddddfddddddfddddddfddddddfdddd	ddfd
iS )Nr#   rF   rP   rQ   r   rS   rT   rR   r   r   r   r+   s    r   r-   z)ConditioningSetAreaPercentage.INPUT_TYPES   st    -?(/SSV`d1e'f)0c!TWae2f(g$+1SZ^-_#`$+1SZ^-_#`+2CX\fj4k*l   	 r   rF   rn   r/   c                 F    t        j                  |d||||f|dd      }|fS )N
percentageFr   r   r   s           r   rn   z$ConditioningSetAreaPercentage.append   s<    00W]_dfgijHkLTV[@] ^ ur   N	r7   r8   r9   r:   r-   r<   r>   r?   rn   r   r   r   r   r      s*        %LH#Hr   r   c                   .    e Zd Zed        ZdZdZdZd Zy)ConditioningSetAreaStrengthc                     ddddddddfd	iS )
Nr#   rF   rP   rQ   rR   r   rS   rT   )r   r   r   r+   s    r   r-   z'ConditioningSetAreaStrength.INPUT_TYPES   s*    -?+2CX\fj4k*l   	 r   rF   rn   r/   c                 8    t        j                  |d|i      }|fS )Nr   r   )r5   r   r   r   s       r   rn   z"ConditioningSetAreaStrength.append   s     00
H?UVur   Nr   r   r   r   r   r      s*        %LH#Hr   r   c                   6    e Zd Zg dZed        ZdZdZdZd Z	y)ConditioningSetMask)zmasked promptzregional inpaint conditioningzmask conditioningc           	      (    ddddddddd	fd
dgfdiS )Nr#   rF   MASKrP   rQ   rR   r   rS   rT   rU   zmask bounds)r   maskr   set_cond_arear   r+   s    r   r-   zConditioningSetMask.INPUT_TYPES   s8    -?&0+2CX\fj4k*l1:M0J/L   	 r   rF   rn   r/   c                     d}|dk7  rd}t        |j                        dk  r|j                  d      }t        j                  ||||d      }|fS )NFrU   T   r   )r   r   mask_strength)rc   rh   	unsqueezer   r   )r5   r   r   r   r   r   r   s          r   rn   zConditioningSetMask.append   s_    "I%!%tzz?Q>>!$D00VhQY@[ \ ur   Nr   r   r   r   r   r      s/    \N    %LH#H
r   r   c                   6    e Zd ZddgZed        ZdZdZdZd Z	y)	ConditioningZeroOutznull conditioningzclear conditioningc                     dddiiS )Nr#   r   rF   r   r+   s    r   r-   zConditioningZeroOut.INPUT_TYPES   s    ^-?@AAr   rF   zero_outadvanced/conditioningc                 F   g }|D ]  }|d   j                         }|j                  dd       }|t        j                  |      |d<   |j                  dd       }|t        j                  |      |d<   t        j                  |d         |g}|j	                  |        |fS )Nr^   r_   conditioning_lyricsr   )rm   rf   ri   
zeros_likern   )r5   r   r   tdr_   r   rx   s           r   r   zConditioningZeroOut.zero_out  s    A!		AEE/48M(%*%5%5m%D/""#%%(=t"D".+0+;+;<O+P'(!!!A$'+AHHQK  ur   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r   r   r   r   r   r      s5    )+?@NB B$LH&Hr   r   c                   .    e Zd Zed        ZdZdZdZd Zy)ConditioningSetTimestepRangec           	      .    ddddddddfddddddfdiS )	Nr#   rF   rP   rR   rQ   MbP?rT   )r   startendr   r+   s    r   r-   z(ConditioningSetTimestepRange.INPUT_TYPES  s=    -?'.CTWaf0g&h%,#cRU_d.e$f   	 r   rF   	set_ranger   c                 :    t        j                  |||d      }|fS )N)start_percentend_percentr   )r5   r   r   r   r   s        r   r   z&ConditioningSetTimestepRange.set_range   s)    00QVOR@T Uur   N)	r7   r8   r9   r:   r-   r<   r>   r?   r   r   r   r   r   r     s*       
 %LH&Hr   r   c                   >    e Zd Zed        ZdZdZdZdZdZ	g dZ
d Zy	)
	VAEDecodec                      ddddifdddifdiS )Nr#   LATENTr&   zThe latent to be decoded.VAEz+The VAE model used for decoding the latent.)samplesvaer   r+   s    r   r-   zVAEDecode.INPUT_TYPES&  s2     $y2M&NO	+XYZ
 	
r   IMAGE)zThe decoded image.decodemodel/latentz3Decodes latent images back into pixel space images.)r   zdecode latentzlatent to imagezrender latentc                    |d   }|j                   r|j                         d   }|j                  |      }t        |j                        dk(  r;|j                  d|j                  d   |j                  d   |j                  d         }|fS )Nr   r      )	is_nestedunbindr   rc   rh   reshape)r5   r   r   latentimagess        r   r   zVAEDecode.decode6  s|    #]]_Q'FF#v||!^^BR(8&,,r:JFLLY[L\]Fzr   N)r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r   r   r   r   r   r   %  s9    
 
 L-OHHGKTNr   r   c                   0    e Zd Zed        ZdZdZdZddZy)VAEDecodeTiledc                 \    ddddddddd	d
fdddddd	d
fddddddd	dfddddddd	dfdiS )Nr#   r   r   r      r          TrU   rV   rW   rX   advancedr   r      z?Only used for video VAEs: Amount of frames to decode at a time.rU   rV   rW   rX   r&   r   6Only used for video VAEs: Amount of frames to overlap.)r   r   	tile_sizeoverlaptemporal_sizetemporal_overlapr   r+   s    r   r-   zVAEDecodeTiled.INPUT_TYPESA  s    Y+0c"UYcesw2x*y).BqQU_aos0t(u/4"QW[ef  tu  CG  7H  /I27QqY]gh  vn  |@  :A  2B	  	r   r   r   experimentalc                    ||dz  k  r|dz  }||dz  k  r|dz  }|j                         }|,t        d||z        }t        dt        |dz  ||z              }nd }d }|j                         }|j	                  |d   ||z  ||z  ||z  ||      }	t        |	j                        dk(  r;|	j                  d|	j                  d   |	j                  d	   |	j                  d         }	|	fS )
Nr   r`   r^   r   tile_xtile_yr   tile_t	overlap_tr   r   r   r   )temporal_compression_decoderW   rV   spacial_compression_decodedecode_tiledrc   rh   r   )
r5   r   r   r   r   r   r   temporal_compressioncompressionr   s
             r   r   zVAEDecodeTiled.decodeN  s'   w{"1nG+a///14">>@+=4H#HIM"1c-1*<>NRf>f&gh M#446!!')"4Y+=U^gkv^v  AH  LW  AW  `m  yI!  Jv||!^^BR(8&,,r:JFLLY[L\]Fzr   N)r   r   r   )	r7   r8   r9   r:   r-   r<   r>   r?   r   r   r   r   r   r   @  s*      LHHr   r   c                   6    e Zd Zed        ZdZdZdZg dZd Z	y)	VAEEncodec                     ddddiS )Nr#   r   r   )pixelsr   r   r+   s    r   r-   zVAEEncode.INPUT_TYPESb  s    IFGGr   r   r.   r   )r.   zencode imagezimage to latentc                 .    |j                  |      }d|ifS )Nr   )r.   )r5   r   r   r   s       r   r.   zVAEEncode.encodek  s    JJv1  r   N)
r7   r8   r9   r:   r-   r<   r>   r?   rA   r.   r   r   r   r   r   a  s/    H HLHHBN!r   r   c                   0    e Zd Zed        ZdZdZdZddZy)VAEEncodeTiledc                 \    dddddddddd	fddd
dddd	fddddddddfddddddddfdiS )Nr#   r   r   r   r   r   r   Tr   r   r   r   r   z?Only used for video VAEs: Amount of frames to encode at a time.r   r   )r   r   r   r   r   r   r   r+   s    r   r-   zVAEEncodeTiled.INPUT_TYPESp  s    {9+0c"UYcesw2x*y).BqQU_aos0t(u/4"QW[ef  tu  CG  7H  /I27QqY]gh  vn  |@  :A  2B	  	r   r   r.   r   c                 :    |j                  ||||||      }d|ifS )Nr   r   )encode_tiled)r5   r   r   r   r   r   r   r   s           r   r.   zVAEEncodeTiled.encode}  s6    VIiQXan  {K  LA!!r   N)r   r   	r7   r8   r9   r:   r-   r<   r>   r?   r.   r   r   r   r   r   o  s*      LHH"r   r   c                   0    e Zd Zed        ZdZdZdZddZy)VAEEncodeForInpaintc           
      "    ddddddddd	d
fdiS )Nr#   r   r   r   r      r   r   r^   rT   )r   r   r   grow_mask_byr   r+   s    r   r-   zVAEEncodeForInpaint.INPUT_TYPES  sJ    Izlq  @A  JK  TV  `a  tb  lc  e  f  	fr   r   r.   model/latent/inpaintc           	         |j                         }|j                  d   |z  |z  }|j                  d   |z  |z  }t        j                  j                  j                  |j                  dd|j                  d   |j                  d   f      |j                  d   |j                  d   fd      }|j                         }|j                  d   |k7  s|j                  d   |k7  rX|j                  d   |z  dz  }|j                  d   |z  dz  }	|d d |||z   |	||	z   d d f   }|d d d d |||z   |	||	z   f   }|dk(  r|}
nt        j                  dd||f      }t        j                  |dz
  dz        }t        j                  t        j                  j                  j                  |j                         ||      dd      }
d	|j                         z
  j                  d      }t        d
      D ]G  }|d d d d d d |fxx   dz  cc<   |d d d d d d |fxx   |z  cc<   |d d d d d d |fxx   dz  cc<   I |j!                  |      }||
d d d d d |d |f   j                         dfS )Nr^   r`   r   r   bilinearsizemoder   )paddingrQ   r         ?)r   
noise_mask)spacial_compression_encoderh   ri   nn
functionalinterpolater   cloneonesmathceilclampconv2droundsqueezerg   r.   )r5   r   r   r   r  downscale_ratior   r   x_offsety_offsetmask_erosionkernel_tensorr	  mrr   r   s                   r   r.   zVAEEncodeForInpaint.encode  s   88:\\!_/?B\\!_/?Bxx""..t||RDJJrNTXT^T^_aTb<c/dlrlxlxyzl{  ~D  ~J  ~J  KL  ~M  lN  U_.  `<<?a6<<?a#7Q/9a?HQ/9a?HAhq8|3Xa(l5J1LMF!HQ\18AL3HHID 1L!JJ1lL'IJMii!1Q 67G ;;uxx':':'A'A$**,P]gn'A'oqrtuvL4::<((+qA1Qq7Os"O1Qq7Oq O1Qq7Os"O  JJv\!Abqb!)-D-J-J-LNQQr   N)r  r   r   r   r   r   r     s+    f fLH%HRr   r   c                   4    e Zd Zed        ZdZdZdZdZddZ	y)	InpaintModelConditioningc           
      "    dddddddddd	fd
iS )Nr#   rF   r   r   r   BOOLEANTzAdd a noise mask to the latent so sampling will only happen within the mask. Might improve results or completely break things depending on the model.rU   r&   )positivenegativer   r   r   r  r   r+   s    r   r-   z$InpaintModelConditioning.INPUT_TYPES  s;    ););$-'2%/,54  Ul  8m  ,n   	 r   )r;   r;   r   )r#  r$  r   r.   zmodel/conditioning/inpaintc           	      *   |j                   d   dz  dz  }|j                   d   dz  dz  }t        j                  j                  j	                  |j                  dd|j                   d   |j                   d   f      |j                   d   |j                   d   fd      }|}	|	j                         }|j                   d   |k7  s|j                   d   |k7  rX|j                   d   dz  dz  }
|j                   d   dz  dz  }|d d |
||
z   |||z   d d f   }|d d d d |
||
z   |||z   f   }d|j                         z
  j                  d      }t        d	      D ]G  }|d d d d d d |fxx   d
z  cc<   |d d d d d d |fxx   |z  cc<   |d d d d d d |fxx   d
z  cc<   I |j                  |      }|j                  |	      }i }||d<   |r||d<   g }||fD ],  }t        j                  |||d      }|j                  |       . |d   |d   |fS )Nr^   r   r`   r   r   r  r  rQ   r   r
  r   r  )concat_latent_imageconcat_maskr   )rh   ri   r  r  r  r   r  r  r  rg   r.   r   r   rn   )r5   r#  r$  r   r   r   r  r   r   orig_pixelsr  r  r  rr   concat_latentorig_latent
out_latentro   r   r   s                       r   r.   zInpaintModelConditioning.encode  s\   \\!_!Q&\\!_!Q&xx""..t||RDJJrNTXT^T^_aTb<c/dlrlxlxyzl{  ~D  ~J  ~J  KL  ~M  lN  U_.  `""$<<?a6<<?a#7Q!+1HQ!+1HAhq8|3Xa(l5J1LMF!HQ\18AL3HHID4::<((+qA1Qq7Os"O1Qq7Oq O1Qq7Os"O  

6*jj-
 +
9'+J|$%x0L44\[hSWDY ZAJJqM 1 AA
++r   NT)
r7   r8   r9   r:   r-   r<   RETURN_NAMESr>   r?   r.   r   r   r   r  r    s/        <L5LH+H ,r   r  c                   @    e Zd ZdgZd Zed        ZdZdZdZ	dZ
d
d	Zy)
SaveLatentzexport latentc                 6    t        j                         | _        y r   )folder_pathsget_output_directory
output_dirr5   s    r   __init__zSaveLatent.__init__  s    &;;=r   c                      ddddifddddd	S )
Nr   r)   rU   zlatents/ComfyUI)r   filename_prefixPROMPTEXTRA_PNGINFOr0   extra_pnginfor#   hiddenr   r+   s    r   r-   zSaveLatent.INPUT_TYPES  s+    )52:YHY<Z1[]%-P 	r   r   saveTr   Nc                 $   t        j                  || j                        \  }}}}}d}	|t        j                  |      }	d }
t
        j                  s(d|	i}
|"|D ]  }t        j                  ||         |
|<    | d|dd}g }|j                  ||dd       t        j                  j                  ||      }i }|d   j                         |d	<   t        j                  g       |d
<   t        j                  j!                  |||
       dd|iiS )N r0   _05z_.latentoutputfilename	subfoldertyper   latent_tensorlatent_format_version_0)metadatauilatents)r1  get_save_image_pathr3  jsondumpsr   disable_metadatarn   ospathjoin
contiguousri   tensorr   utilssave_torch_file)r5   r   r7  r0   r;  full_output_folderrE  counterrF  prompt_inforJ  r   fileresultsrC  s                  r   r>  zSaveLatent.save  s8   LXLlLlm|  C  N  N  MOIHgy/ **V,K$$ +.H(&A"&**]1-=">HQK ' 1WRL1%'"
 	 ww||.5"))"4"?"?"A,1LL,<()##FD8#DG-//r   ComfyUINN)r7   r8   r9   rA   r5  r:   r-   r<   r>   OUTPUT_NODEr?   r>  r   r   r   r/  r/    s>    %&N>  
 LHKH0r   r/  c                   V    e Zd ZddgZed        ZdZdZdZd Z	ed        Z
ed	        Zy
)
LoadLatentzimport latentzopen latentc           	      8   t        j                         }t        j                  |      D cg c]T  }t        j                  j                  t        j                  j                  ||            sA|j                  d      sS|V }}ddt        |      giiS c c}w )Nz.latentr#   r   )	r1  get_input_directoryrQ  listdirrR  isfilerS  endswithsortedr,   	input_dirffiless       r   r-   zLoadLatent.INPUT_TYPES  s{     446	JJy1z1qRWW^^BGGLLQZ\]D^5_dedndnoxdy1zXu'89<< {s   AB/BBr   r   loadc                     t        j                  |      }t        j                  j	                  |d      }d}d|vrd}d|d   j                         |z  i}|fS )NcpudevicerQ   rI  g!ޅ@r   rH  )r1  get_annotated_filepathsafetensorsri   	load_filefloat)r5   r   latent_path
multiplierr   s        r   rl  zLoadLatent.load!  sd    "99&A"",,[,G
$F2&Jf_5;;=
JK{r   c                    t        j                  |      }t        j                         }t	        |d      5 }|j                  |j                                d d d        |j                         j                         S # 1 sw Y   'xY wNrb	r1  rq  hashlibsha256openupdatereaddigesthex)r,   r   
image_pathr  rj  s        r   
IS_CHANGEDzLoadLatent.IS_CHANGED*  s]    !88@
NN*d#qHHQVVX $xxz~~ $#    A<<Bc                 P    t        j                  |      sdj                  |      S y)NzInvalid latent file: {}Tr1  exists_annotated_filepathformat)r,   r   s     r   VALIDATE_INPUTSzLoadLatent.VALIDATE_INPUTS2  s$    55f=,33F;;r   N)r7   r8   r9   rA   r:   r-   r?   r<   r>   rl  r  r  r   r   r   ra  ra    s\    %}5N= =
 HLH      r   ra  c                   :    e Zd ZddgZed        ZdZdZdZdZ	d Z
y	)
CheckpointLoader
load modelmodel loaderc                 `    dt        j                  d      ft        j                  d      fdiS )Nr#   configscheckpoints)config_name	ckpt_namer1  get_filename_listr+   s    r   r-   zCheckpointLoader.INPUT_TYPES<  s8    l.L.LY.W-Z,8,J,J=,Y+\^ _ 	_r   MODELr*   r   load_checkpointadvanced/loadersTc           	          t        j                  d|      }t        j                  d|      }t        j                  j                  ||ddt        j                  d            S )Nr  r  T
embeddings
output_vaeoutput_clipembedding_directory)r1  get_full_pathget_full_path_or_raiser   sdr  get_folder_paths)r5   r  r  config_path	ckpt_paths        r   r  z CheckpointLoader.load_checkpointF  sn    "00KH 77yQ	xx''Y4]a  xD  xU  xU  Vb  xc'  d  	dr   N)r7   r8   r9   rA   r:   r-   r<   r>   r?   
DEPRECATEDr  r   r   r   r  r  9  s<    "N3N_ _ ,L H!HJdr   r  c                   >    e Zd Zed        ZdZdZdZdZdZ	g dZ
d Zy	)
CheckpointLoaderSimplec                 <    ddt        j                  d      ddifiiS )Nr#   r  r  r&   z+The name of the checkpoint (model) to load.r  r+   s    r   r-   z"CheckpointLoaderSimple.INPUT_TYPESL  s<     l<<]Ki  ZG  NH  I
 	
r   r  )z%The model used for denoising latents.z.The CLIP model used for encoding text prompts.zMThe VAE model used for encoding and decoding images to and from latent space.r  model/loaderszQLoads a diffusion model checkpoint, diffusion models are used to denoise latents.)r  
checkpointr  zload checkpointckptmodelc                     t        j                  d|      }t        j                  j	                  |ddt        j
                  d            }|d d S )Nr  Tr  r  r   r1  r  r   r  load_checkpoint_guess_configr  )r5   r  r  ro   s       r   r  z&CheckpointLoaderSimple.load_checkpoint]  sa     77yQ	hh33I$\`  wC  wT  wT  Ua  wb3  c2Awr   N)r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r  K  s>    
 
 ,LhO !HHeKeNr   r  c                   6    e Zd ZdgZed        ZdZdZdZddZ	y)	DiffusersLoaderzload diffusers modelc                 2   g }t        j                  d      D ]w  }t        j                  j	                  |      s#t        j
                  |d      D ];  \  }}}d|v s|j                  t        j                  j                  ||             = y dd|fiiS )N	diffusersT)followlinkszmodel_index.json)r   r#   
model_path)r1  r  rQ  rR  existswalkrn   relpath)clspathssearch_pathrootsubdirrk  s         r   r-   zDiffusersLoader.INPUT_TYPESe  s    '88EKww~~k*+-77;D+Q'D&%)U2RWW__T_%MN ,R F \E8677r   r  r  zadvanced/loaders/deprecatedc                 h   t        j                  d      D ]e  }t        j                  j	                  |      s#t        j                  j                  ||      }t        j                  j	                  |      sc|} n t        j                  j                  |||t        j                  d            S )Nr  r  r  )	r1  r  rQ  rR  r  rS  r   diffusers_loadload_diffusers)r5   r  r  r  r  rR  s         r   r  zDiffusersLoader.load_checkpointt  s    '88EKww~~k*ww||K<77>>$'!%J F ##22:*bm  DP  Da  Da  bn  Do2  p  	pr   NTT)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r  r  b  s3    ,-N8 8 ,L H,Hpr   r  c                   0    e Zd Zed        ZdZdZdZddZy)unCLIPCheckpointLoaderc                 6    ddt        j                  d      fiiS )Nr#   r  r  r  r+   s    r   r-   z"unCLIPCheckpointLoader.INPUT_TYPES  &    kL,J,J=,Y+\    	 r   )r  r*   r   CLIP_VISIONr  r  c           	          t        j                  d|      }t        j                  j	                  |dddt        j
                  d            }|S )Nr  Tr  )r  r  output_clipvisionr  r  )r5   r  r  r  r  ro   s         r   r  z&unCLIPCheckpointLoader.load_checkpoint  s^     77yQ	hh33I$\`tx  O[  Ol  Ol  my  Oz3  {
r   Nr  )	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    s*        ;L HHr   r  c                   .    e Zd Zed        ZdZdZdZd Zy)CLIPSetLastLayerc           	           dddddddddfd	iS )
Nr#   r*   r   r   ir^   Tr   )r(   stop_at_clip_layerr   r+   s    r   r-   zCLIPSetLastLayer.INPUT_TYPES  s0    j5:SV_aklz~<  5A  ! 	!r   r  set_last_layerr/   c                 J    |j                         }|j                  |       |fS r   )r  
clip_layer)r5   r(   r  s      r   r  zCLIPSetLastLayer.set_last_layer  s!    zz|*+wr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    s*    ! ! LH#Hr   r  c                   H    e Zd ZdZd Zed        ZdZdZdZ	dZ
dZg d	Zd
 Zy)
LoraLoaderrD   c                     d | _         y r   )loaded_lorar4  s    r   r5  zLoraLoader.__init__  s
    r   c                 t    ddddifdddift        j                  d      ddifd	d
dddddfd	d
dddddfdiS )Nr#   r  r&   z0The diffusion model the LoRA will be applied to.r*   z+The CLIP model the LoRA will be applied to.loraszThe name of the LoRA.rP   rQ         Y      Y@rS   zGHow strongly to modify the diffusion model. This value can be negative.rU   rV   rW   rX   r&   zBHow strongly to modify the CLIP model. This value can be negative.)r  r(   	lora_namestrength_modelstrength_clipr  r+   s    r   r-   zLoraLoader.INPUT_TYPES  s     !I/a#bc)-Z![\*<<WE	SjGkl#*FSXbf  t}  -~  #")s6RWae  sw  ,x  "y
 	
r   )r  r*   )zThe modified diffusion model.zThe modified CLIP model.	load_lorar  zThis LoRA loader is used to modify both diffusion and CLIP models, altering the way in which latents are denoised such as applying styles. Multiple LoRA nodes can be linked together.)loraz	load loraz
apply lorazlora loaderz
lora modelc                    |dk(  r	|dk(  r||fS t        j                  d|      }d }d }| j                  R| j                  d   |k(  r9| j                  d   }t        | j                        dkD  r| j                  d   nd }nd | _        |/t        j
                  j                  |dd      \  }}|||f| _        t        j                  j                  ||||||      \  }	}
|	|
fS )Nr   r  r^   r`   T)	safe_loadreturn_metadata)lora_metadata)	r1  r  r  rc   r   rV  load_torch_filer  load_lora_for_models)r5   r  r(   r  r  r  	lora_pathr  r  
model_lora	clip_loras              r   r  zLoraLoader.load_lora  s   Q=A#54=  77K	'"i/''*7:4;K;K7Lq7P 0 0 3VZ#' <"'++"="=iSWim"="nD- )4?D % = =eT4Q_an  L =  !M
II&&r   N)r7   r8   r9   rL   r5  r:   r-   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r    sI    ,  	
 	
 %LSOHH KKUN'r   r  c                   .    e Zd Zed        ZdZdZdZd Zy)LoraLoaderModelOnlyc           	      H    ddt        j                  d      fdddddd	fd
iS )Nr#   r  r  rP   rQ   r  r  rS   rT   )r  r  r  r  r+   s    r   r-   zLoraLoaderModelOnly.INPUT_TYPES  s;    z,8,J,J7,S+V18cRXafpt:u0v  ! 	!r   r  zThis LoRAs loader is used to modify the diffusion model, altering the way in which latents are denoised such as applying styles. Multiple LoRA nodes can be linked together.load_lora_model_onlyc                 4    | j                  |d ||d      d   fS )Nr   )r  )r5   r  r  r  s       r   r  z(LoraLoaderModelOnly.load_lora_model_only  s!    udI~qI!LNNr   N)	r7   r8   r9   r:   r-   r<   r@   r>   r  r   r   r   r  r    s.    ! !
 L AK%HOr   r  c                   ^    e Zd Zg dZg dZed        Zed        Zed        Z	dZ
dZdZd	 Zy
)	VAELoader)taehvlighttaew2_2lighttaew2_1lighttaehy1_5taeltx_2)taesdtaesdxltaesd3taef1taef2c                 ^   t        j                  d      }t        j                  d      }t               t               }}|D ]  }|j                  dd      }t	        |      dk7  s|d   | j
                  vr6| j                  D ]&  }|j                  |      s|j                  |        n j|d   j                  d      r|j                  |d          |d   j                  d      s|j                  |d           ||D cg c]	  }||v s| c}z  }|j                  d	       |S c c}w )
Nr   
vae_approxrA  r^   r`   r   zencoder.zdecoder.pixel_space)
r1  r  setsplitrc   
image_taes
video_taes
startswithrn   add)	r,   vaesapprox_vaeshave_img_encoderhave_img_decodervpartstaeks	            r   vae_listzVAELoader.vae_list  s   --e4"44\B-0UCE*AGGCOE5zQ%(!,,"><<C||C(A ( Qx"":. $$U1X.q$$Z0 $$U1X.  	,F,q5E0E,FFM" Gs   	D*D*c                     i }t        j                  d      }t        t         fd|            }t        t         fd|            }t        j
                  j                  t        j                  d|            }|D ]  }||   |dj                  |      <    t        j
                  j                  t        j                  d|            }|D ]  }||   |dj                  |      <     dk(  r2t        j                  d      |d<   t        j                  d	      |d
<   |S  dk(  r2t        j                  d      |d<   t        j                  d	      |d
<   |S  dk(  r2t        j                  d      |d<   t        j                  d      |d
<   |S  dk(  r0t        j                  d      |d<   t        j                  d      |d
<   |S )Nr  c                 D    | j                  dj                              S )Nz{}_encoder.r  r  anames    r   <lambda>z&VAELoader.load_taesd.<locals>.<lambda>      ]5I5I$5O(Pr   c                 D    | j                  dj                              S )Nz{}_decoder.r  r  s    r   r  z&VAELoader.load_taesd.<locals>.<lambda>  r  r   ztaesd_encoder.{}ztaesd_decoder.{}r  g{P?	vae_scalerR   	vae_shiftr  gy&1?r  grh|?gH.?r  gxz,C?g=U?)r1  r  nextfilterr   rV  r  r  r  ri   rU  )r  r  r  encoderdecoderencr
  decs   `       r   
load_taesdzVAELoader.load_taesd  s   "44\BvPR]^_vPR]^_kk)),*M*Ml\c*deA/21vB!((+,  kk)),*M*Ml\c*deA/21vB!((+,  7?#ll73B{O#ll3/B{O 	 Y#ll73B{O#ll3/B{O 	 X#ll62B{O#ll62B{O 	 W_#ll62B{O#ll62B{O	r   c                 .    dd| j                  |       fiiS )Nr#   vae_name)r  r+   s    r   r-   zVAELoader.INPUT_TYPES  s    j1::a=*;<==r   r   load_vaer  c                 r   d }d }|dk(  ri }t        j                  d      |d<   n|| j                  v r| j                  |      }nt        j
                  j                  |      d   | j                  v rt        j                  d|      }nt        j                  d|      }t        j                  j                  |d      \  }}|d	k(  r|d
di}nd|d
<   t        j                  j                  ||      }|j                          |.t        j                  j                   ||d ff|j"                  _        |fS )Nr  rQ   pixel_space_vaer   r  r   T)r  r  tae_latent_channels   )r  rJ  )ri   rU  r  r  rQ  rR  splitextr   r1  r  r   rV  r  r  r   throw_exception_if_invalidload_vae_patcherpatchercached_patcher_init)r5   r  rJ  vae_pathr  r   s         r   r   zVAELoader.load_vae  s!   }$B$)LL$5B !(*Bww)!,?'>>|XV'>>uhO ;;66xQU6VLBw13725./hhllb8l4&&( /4xx/H/H8U]_cJd.eCKK+vr   N)r7   r8   r9   r   r  staticmethodr  r  r:   r-   r<   r>   r?   r   r   r   r   r  r    sZ    WJAJ (  : > >LHHr   r  c                   6    e Zd Zed        ZdZdZdZg dZd Z	y)ControlNetLoaderc                 6    ddt        j                  d      fiiS )Nr#   control_net_name
controlnetr  r+   s    r   r-   zControlNetLoader.INPUT_TYPES;  s"    0<3Q3QR^3_2bcddr   CONTROL_NETload_controlnetr  )r0  control netcnzload controlnetzcontrolnet loaderc                     t        j                  d|      }t        j                  j	                  |      }|t        d      |fS )Nr0  zPERROR: controlnet file is invalid and does not contain a valid controlnet model.)r1  r  r   r0  r3  r2   )r5   r/  controlnet_pathr0  s       r   r3  z ControlNetLoader.load_controlnetE  sE    &==lL\]%%55oF
qrr}r   N)
r7   r8   r9   r:   r-   r<   r>   r?   rA   r3  r   r   r   r-  r-  :  s1    e e $L HH`Nr   r-  c                   .    e Zd Zed        ZdZdZdZd Zy)DiffControlNetLoaderc                 8    ddt        j                  d      fdiS )Nr#   r  r0  )r  r/  r  r+   s    r   r-   z DiffControlNetLoader.INPUT_TYPESM  s+    z3?3Q3QR^3_2bd e 	er   r1  r3  r  c                 t    t        j                  d|      }t        j                  j	                  ||      }|fS )Nr0  )r1  r  r   r0  r3  )r5   r  r/  r7  r0  s        r   r3  z$DiffControlNetLoader.load_controlnetW  s5    &==lL\]%%55ouM
}r   N)	r7   r8   r9   r:   r-   r<   r>   r?   r3  r   r   r   r9  r9  L  s,    e e $L HHr   r9  c                   2    e Zd Zed        ZdZdZdZdZd Z	y)ControlNetApplyc           
      "    ddddddddd	d
fdiS )Nr#   rF   r1  r   rP   rQ   rR   r   rS   rT   )r   control_netimager   r   r+   s    r   r-   zControlNetApply.INPUT_TYPES^  s0    -?,=&1*1s3W[ei3j)k   	 r   rF   apply_controlnetTmodel/conditioning/controlnetc                 <   |dk(  r|fS g }|j                  dd      }|D ]y  }|d   |d   j                         g}|j                         j                  ||      }	d|d   v r|	j                  |d   d          |	|d   d<   d|d   d<   |j	                  |       { |fS )Nr   r   r^   controlTcontrol_apply_to_uncond)movedimrm   set_cond_hintset_previous_controlnetrn   )
r5   r   r?  r@  r   r   control_hintr   rx   c_nets
             r   rA  z ControlNetApply.apply_controlnetk  s    q= ##}}R*A1qtyy{#A$$&44\8LEAaD --ad9o>#AaDO.2AaD*+HHQK  ur   N)
r7   r8   r9   r:   r-   r<   r>   r  r?   rA  r   r   r   r=  r=  ]  s/        %L!HJ.Hr   r=  c                   @    e Zd Zed        ZdZdZdZdZg dZ	dg fdZ
y)	ControlNetApplyAdvancedc                 J    dddddddddd	fddddd
d	fddddd
d	fdddidS )NrF   r1  r   rP   rQ   rR   r   rS   rT   r   )r#  r$  r?  r@  r   r   r   r   r   r#   optionalr   r+   s    r   r-   z#ControlNetApplyAdvanced.INPUT_TYPES}  sc    ););,=&1*1s3W[ei3j)k/6CPS\_in8o.p-4#cZ]gl6m,n #I 
 
	r   )r;   r;   )r#  r$  rA  rB  )r0  zapply controlnetzuse controlnetr4  Nc
           	         |dk(  r||fS |j                  dd      }
i }g }||fD ]  }g }|D ]  }|d   j                         }|j                  dd       }||v r||   }n<|j                         j                  |
|||f||	      }|j	                  |       |||<   ||d<   d|d<   |d   |g}|j                  |        |j                  |        |d   |d   fS )Nr   r   r^   rD  )r   extra_concatFrE  )rF  rm   rf   rG  rH  rn   )r5   r#  r$  r?  r@  r   r   r   r   rQ  rI  cnetsro   r   r   r   r   	prev_cnetrJ  rx   s                       r   rA  z(ControlNetApplyAdvanced.apply_controlnet  s   q=h''}}R*%x0LA!aDIIKEE)T2	%!),E',,.<<\8VcepUqwz  JV<  WE11)<',E)$$)/4+,qT1I " JJqM# 1$ AAr   )r7   r8   r9   r:   r-   r<   r-  r>   r?   rA   rA  r   r   r   rL  rL  |  s>      3L+L!H.HXNqu  EG  r   rL  c                   .    e Zd Zed        ZdZdZdZd Zy)
UNETLoaderc                 D    dt        j                  d      fg dddifdiS )Nr#   diffusion_models)rU   
fp8_e4m3fnfp8_e4m3fn_fastfp8_e5m2r   T)	unet_nameweight_dtyper  r+   s    r   r-   zUNETLoader.INPUT_TYPES  s4    L,J,JK],^+a/gjtvzi{.|   	 r   r  	load_unetr  c                    i }|dk(  rt         j                  |d<   n6|dk(  rt         j                  |d<   d|d<   n|dk(  rt         j                  |d<   t        j                  d|      }t
        j                  j                  ||      }|fS )	NrX  dtyperY  Tfp8_optimizationsrZ  rW  )model_options)ri   float8_e4m3fnfloat8_e5m2r1  r  r   r  load_diffusion_model)r5   r[  r\  ra  	unet_pathr  s         r   r]  zUNETLoader.load_unet  s    <'%*%8%8M'"..%*%8%8M'"15M-.Z'%*%6%6M'" 778JIV	--i}-Uxr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r]  r   r   r   rU  rU    s*        LH!Hr   rU  c                   4    e Zd Zed        ZdZdZdZdZddZ	y)	
CLIPLoaderc                 P    t        j                  d      fg dfddddgddifid	S )
Ntext_encoders)stable_diffusionstable_cascadesd3stable_audiomochiltxvpixartcosmoslumina2wanhidreamchromaaceomnigen2
qwen_imagehunyuan_imageflux2ovislongcat_image	cogvideoxlenspixeldit	ideogram4)	clip_namerG  rp  rU   rn  r   TrN  r  r+   s    r   r-   zCLIPLoader.INPUT_TYPES  sI    ,8,J,J?,[+^ (s  'v  ')U);j$=O(P  	 r   r  	load_clipr  aA  [Recipes]

stable_diffusion: clip-l
stable_cascade: clip-g
sd3: t5 xxl/ clip-g / clip-l
stable_audio: t5 base
mochi: t5 xxl
cogvideox: t5 xxl (226-token padding)
cosmos: old t5 xxl
lumina2: gemma 2 2B
wan: umt5 xxl
 hidream: llama-3.1 (Recommend) or t5
omnigen2: qwen vl 2.5 3B
lens: gpt-oss-20b
 pixeldit: gemma 2 2B elmc                    t        t        j                  j                  |j	                         t        j                  j                  j
                        }i }|dk(  rt        j                  d      x|d<   |d<   t        j                  d|      }t        j                  j                  |gt        j                  d      ||      }|fS )Nrn  load_deviceoffload_deviceri  r  
ckpt_pathsr  	clip_typera  )getattrr   r  CLIPTypeupperSTABLE_DIFFUSIONri   rp  r1  r  r  r  )r5   r  rG  rp  r  ra  	clip_pathr(   s           r   r  zCLIPLoader.load_clip  s    EHH--tzz|UXX=N=N=_=_`	U?MR\\Z_M``M-(=9I+J 77S	xx!!i[lNkNklxNy  FO  _l!  mwr   N)rj  rU   
r7   r8   r9   r:   r-   r<   r>   r?   r@   r  r   r   r   rg  rg    s2        LH!H dK	r   rg  c                   4    e Zd Zed        ZdZdZdZdZddZ	y)	DualCLIPLoaderc                 z    t        j                  d      ft        j                  d      fg dfddddgddifid	S )
Nri  )sdxlrl  fluxhunyuan_videort  ry  hunyuan_video_15
kandinsky5kandinsky5_imagero  newbierv  )
clip_name1
clip_name2rG  rp  rU   rn  r   TrN  r  r+   s    r   r-   zDualCLIPLoader.INPUT_TYPES  sZ    -9-K-KO-\,_-9-K-KO-\,_ ({  '~ 
 ')U);j$=O(P	  	 r   r  r  r  z[Recipes]

sdxl: clip-l, clip-g
sd3: clip-l, clip-g / clip-l, t5 / clip-g, t5
flux: clip-l, t5
hidream: at least one of t5 or llama, recommended t5 and llama
hunyuan_image: qwen2.5vl 7b and byt5 small
newbie: gemma-3-4b-it, jina clip v2c                    t        t        j                  j                  |j	                         t        j                  j                  j
                        }t        j                  d|      }t        j                  d|      }i }|dk(  rt        j                  d      x|d<   |d<   t        j                  j                  ||gt        j                  d      ||      }	|	fS )Nri  rn  r  r  r  r  )r  r   r  r  r  r  r1  r  ri   rp  r  r  )
r5   r  r  rG  rp  r  
clip_path1
clip_path2ra  r(   s
             r   r  zDualCLIPLoader.load_clip  s    EHH--tzz|UXX=N=N=_=_`	!88*U
!88*U
U?MR\\Z_M``M-(=9I+Jxx!!j*-E[g[x[x  zF  \G  S\  ly!  zwr   N)rU   r  r   r   r   r  r    s2        LH!H HKr   r  c                   .    e Zd Zed        ZdZdZdZd Zy)CLIPVisionLoaderc                 6    ddt        j                  d      fiiS )Nr#   r  clip_visionr  r+   s    r   r-   zCLIPVisionLoader.INPUT_TYPES  r  r   r  r  r  c                     t        j                  d|      }t        j                  j	                  |      }|t        d      |fS )Nr  zMERROR: clip vision file is invalid and does not contain a valid vision model.)r1  r  r   r  rl  r2   )r5   r  r  r  s       r   r  zCLIPVisionLoader.load_clip  sD     77yQ	'',,Y7noo~r   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    s*        $LHHr   r  c                   .    e Zd Zed        ZdZdZdZd Zy)CLIPVisionEncodec                     dddddgfdiS )Nr#   r  r   centernone)r  r@  cropr   r+   s    r   r-   zCLIPVisionEncode.INPUT_TYPES  s%    -='1(0&'9&;   	 r   CLIP_VISION_OUTPUTr.   r/   c                 @    d}|dk7  rd}|j                  ||      }|fS )NTr  Fr  )encode_image)r5   r  r@  r  
crop_imagerC  s         r   r.   zCLIPVisionEncode.encode  s1    
8J))%j)Ayr   Nr   r   r   r   r  r    s*       
 +LH#Hr   r  c                   .    e Zd Zed        ZdZdZdZd Zy)StyleModelLoaderc                 6    ddt        j                  d      fiiS )Nr#   style_model_namestyle_modelsr  r+   s    r   r-   zStyleModelLoader.INPUT_TYPES&  s"    0<3Q3QR`3a2deffr   STYLE_MODELload_style_modelr  c                 r    t        j                  d|      }t        j                  j	                  |      }|fS )Nr  )r1  r  r   r  r  )r5   r  style_model_pathstyle_models       r   r  z!StyleModelLoader.load_style_model/  s3    '>>~O_`hh//0@A~r   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r  %  s,    g g $L!HHr   r  c                   4    e Zd ZdgZed        ZdZdZdZd Z	y)StyleModelApplyzstyle transferc           
      *    ddddddddd	d
fddgfdiS )Nr#   rF   r  r  rP   rQ   rR   r   r   rT   multiply	attn_bias)r   r  clip_vision_outputr   strength_typer   r+   s    r   r-   zStyleModelApply.INPUT_TYPES8  s;    -?,=3K*1s3W[ej3k)l0:K/H.K	   	 r   rF   apply_stylemodelzmodel/conditioning/style_modelc                 B   |j                  |      j                  dd      j                  d      }|dk(  r||z  }|j                  d   }g }|D ]J  }	|	\  }
}|j	                         }d|v s|dk(  r|dk7  rt        j                  t        j                  |dk(  r|ndg            }|j                  d	d
      }|d   |d   z  }|
j                  d   }|j                  dd       }|;t        j                  |
j                  d   ||z   ||z   ft
        j                        }|j                  t
        j                  k(  r3t        j                  |j                  t
        j                              }t        j                  |
j                  d   ||z   |z   ||z   |z   ft
        j                        }|d d d |d |f   |d d d |d |f<   |d d d ||d f   |d d d |||z   d f<   |d d |d d |f   |d d ||z   d d |f<   |d d |d |d f   |d d ||z   d ||z   d f<   ||d d d ||||z   f<   ||d d ||z   d |||z   f<   |j                  |
j                        |d<   ||d	<   |j                  t        j                   |
|fd      |g       M |fS )Nr   r^   )	start_dimend_dimra   r  attention_maskr  rQ   attention_mask_img_shape)r^   r^   r_  )get_condflattenr   rh   rm   ri   logTensorrf   rk   float16r_  booltorp  rn   rj   )r5   r   r  r  r   r  condrx   c_outr   txtkeysr  mask_ref_sizen_refn_txtr   new_masks                     r   r  z StyleModelApply.apply_stylemodelE  s   ##$67??!UV?WaafgahJ&HDJJqMAKS$99;D4'M[,HXY\_ "IIellQ\@\Hbe3f&gh	 $)CV L%a(=+;;		!xx 0$7< ;;		!eemUU]'S[`[h[hiD ::+ !99TWW5==W%ABD ;;		!eai%6GQRUZIZ'[chcpcpq.21fuffuf3D.EFUFFUF*+04Q5F0GFUFE!GH,-04Q5F0GE!GHfuf,-26q%&%&7H2IE!GHeAgh./5>FUFE%'M127@E!GHeE!Gm34)1SZZ)@%&3@/0LL%))S$KQ7>?I L xr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r  r  5  s2    &'N    %L!H/H-r   r  c                   .    e Zd Zed        ZdZdZdZd Zy)unCLIPConditioningc           
      0    ddddddddd	fdd
d
ddd	fdiS )Nr#   rF   r  rP   rQ   g      $r   rS   rT   rR   )r   r  r   noise_augmentationr   r+   s    r   r-   zunCLIPConditioning.INPUT_TYPESu  sA    -?3K*1s5Y]gk3l)m4;UXadnr=s3t   	 r   rF   	apply_admr/   c                 V    |dk(  r|fS t        j                  |d|||dgid      }|fS )Nr   unclip_conditioning)r  r   r  T)rn   r   )r5   r   r  r   r  r   s         r   r  zunCLIPConditioning.apply_adm  s`    q= ##00@U  pB  PX  pB  YC  XD  @E  NR  Sur   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r  t  s*        %LH#Hr   r  c                   .    e Zd Zed        ZdZdZdZd Zy)GLIGENLoaderc                 6    ddt        j                  d      fiiS )Nr#   gligen_namegligenr  r+   s    r   r-   zGLIGENLoader.INPUT_TYPES  s     ml.L.LX.V-YZ[[r   GLIGENload_gligenr  c                 r    t        j                  d|      }t        j                  j	                  |      }|fS )Nr  )r1  r  r   r  r  )r5   r  gligen_pathr  s       r   r  zGLIGENLoader.load_gligen  s0    "99(KP%%k2yr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    s,    \ \ LHHr   r  c                   .    e Zd Zed        ZdZdZdZd Zy)GLIGENTextBoxApplyc                 ~    ddddddddfdd	d
t         d
dfdd	d
t         d
dfdddt         d
dfdddt         d
dfdiS )Nr#   rF   r  r  r)   T)r$   r%   r   r   r   rT   r   )rY   r(   gligen_textbox_modelr'   r   r   r   r   r   r+   s    r   r-   zGLIGENTextBoxApply.INPUT_TYPES  su    0B&06B'/tW[1\&](-2aP^hi/j'k).BqQ_ij0k(l$)q>cd+e#f$)q>cd+e#f   	 r   rF   rn   zmodel/conditioning/gligenc	                 "   g }	|j                  |j                  |      d      \  }
}|D ]a  }|d   |d   j                         g}||dz  |dz  |dz  |dz  fg}g }d|d   v r|d   d   d   }d|||z   f|d   d<   |	j                  |       c |	fS )	Nunprojected)return_pooledr   r^   r   r  r`   position)encode_from_tokensr3   rm   rn   )r5   rY   r(   r  r'   r   r   r   r   r   r  cond_pooledr   rx   position_paramsprevs                   r   rn   zGLIGENTextBoxApply.append  s     33DMM$4GWd3ek A1qtyy{#A +Vq[%1*a1faSTfUVOD1Q4tH~a((*>@VWAaDNHHQK ! ur   Nr   r   r   r   r  r    s*    	  	  %LH*Hr   r  c                   @    e Zd Zed        ZdZdZdZdZdZ	g dZ
d
dZy	)EmptyLatentImagec           	      P    ddddt         dddfdddt         dddfdd	d	d
ddfdiS )Nr#   r   r      r   z)The width of the latent images in pixels.r  z*The height of the latent images in pixels.r^   r   z)The number of latent images in the batch.rU   rV   rW   r&   )r   r   
batch_sizer   r+   s    r   r-   zEmptyLatentImage.INPUT_TYPES  sp     SN\]  kV  "W  X c"^]^  lX  #Y  Z$!Ad  XC  'D  E
 	
r   r   )zThe empty latent image batch.generater   zFCreate a new batch of empty latent images to be denoised via sampling.)emptyzempty latentz
new latentzcreate latentzblank latentblankc                     t        j                  |d|dz  |dz  gt        j                  j	                         t        j                  j                               }|ddfS )Nr   r   rp  r_  )r   downscale_ratio_spacial)ri   rk   r   r   intermediate_deviceintermediate_dtype)r5   r   r   r  r   s        r   r  zEmptyLatentImage.generate  sm    j!Vq[%1*EeNdNdNxNxNz  CH  CY  CY  Cl  Cl  Cn  o"qADDr   Nr^   )r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r    s:    
 
 L8OHHZKfNEr   r  c                   6    e Zd Zg dZed        ZdZdZdZd Z	y)LatentFromBatch)zselect from batchzpick latentzbatch subsetc                 <    ddddt          t         dfdddddfdiS )	Nr#   r   r   r   rU   rV   rW   r^   r   )r   batch_indexlengthr   r+   s    r   r-   zLatentFromBatch.INPUT_TYPES  s8    .3N?cq5r-s).AaPR0S(T  ! 	!r   r   	frombatchmodel/latent/batchc                     |j                         }|d   }|dk  r||j                  d   z  }t        dt        |j                  d   dz
  |            }t        |j                  d   |z
  |      }||||z    j	                         |d<   d|v r|d   }|j                  d   dk(  r|j	                         |d<   n|j                  d   |j                  d   k  rT|j                  t        j                  |j                  d   |j                  d   z        ddd      d |j                  d    }||||z    j	                         |d<   d|vr#t        |||z         D cg c]  }| c}|d<   |fS |d   |||z    |d<   |fS c c}w )Nr   r   r^   r  r  )	rm   rh   rW   rV   r  repeatr  r  rg   )r5   r   r  r	  r,   s_inmasksr   s           r   r
  zLatentFromBatch.frombatch  s   LLNy!?4::a=(K!SA!2K@ATZZ][0&9Kf(<=CCE)7"L)E{{1~""'++-,;;q>DJJqM1!LL4::a=5;;q>3Q)RTUWXZ[\]k^b^h^hij^klE"'K&4H"I"O"O"Q,!+0k&>P+QR+Qa+QRAm t  '}5k+PVBVWAmt  Ss   	E;N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r
  r   r   r   r  r    s/    IN! !
 LH#Hr   r  c                   6    e Zd ZddgZed        ZdZdZdZd Z	y)	RepeatLatentBatchzduplicate latentzclone latentc                     dddddddfdiS )Nr#   r   r   r^   r   r  )r   amountr   r+   s    r   r-   zRepeatLatentBatch.INPUT_TYPES  s&    ).AaPR0S(T  ! 	!r   r   r  r  c           	         |j                         }|d   }|j                  |fd|j                  dz
  z  z         |d<   d|v r|d   j                  d   dkD  r|d   }|j                  d   |j                  d   k  re|j                  t	        j
                  |j                  d   |j                  d   z        fd|j                  dz
  z  z         d |j                  d    }|d   j                  |fd|d   j                  dz
  z  z         |d<   d|v rUt        |d         t        |d         z
  dz   }|d   t        d|      D cg c]  }|d   D ]
  }|||z  z     c}}z   |d<   |fS c c}}w )Nr   r  r^   r  r   r  )	rm   r  ndimrh   r  r  rW   rV   rg   )	r5   r   r  r,   r  r  offsetrr   r   s	            r   r  zRepeatLatentBatch.repeat  s   LLNy!{{F9		A0F#GH)7"w|'<'B'B1'E'IL)E{{1~

1-dii

1A0N&O%QUY]b]g]gjk]kUl%mno}ptpzpz{|p}~%l3::F9PWXdPePjPjmnPnHo;pqAlOA=)*S=1A-BBQFF /USTV\M]2xM]ghivgwbc1F
3Cgw3CM]2xxAmt 3ys   E*N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r  r    s4    (.9N! ! LH#Hr   r  c                   F    e Zd ZddgZg dZddgZed        ZdZdZ	d	Z
d
 Zy)LatentUpscaleenlarge latentresize latentnearest-exactr  r   bicubicbislerpdisabledr  c           
      n    dd| j                   fdddt        ddfdddt        ddf| j                  fdiS )	Nr#   r   r   r   r   r   rT   )r   upscale_methodr   r   r  upscale_methodsr   crop_methodsr+   s    r   r-   zLatentUpscale.INPUT_TYPES  sQ    IZIZH\(-3qQ_ij/k'l).CR`jk0l(m'(~~&79 : 	:r   r   upscaler   c                    |dk(  r
|dk(  r|}|fS |j                         }|dk(  rHt        d|      }t        dt        |d   j                  d   |z  |d   j                  d   z              }ne|dk(  rHt        d|      }t        dt        |d   j                  d   |z  |d   j                  d   z              }nt        d|      }t        d|      }t        j
                  j                  |d   |dz  |dz  ||      |d<   |fS )Nr   r   r   r   r   r   )rm   rW   r  rh   r   rV  common_upscale)r5   r   r!  r   r   r  r,   s          r   r%  zLatentUpscale.upscale  s   A:&A+A t AzRBgi&8&>&>r&BV&KgV_N`NfNfgiNj&j kl1BRwy'9'?'?'Ce'KgV_N`NfNfgiNj'j!klBR ;;55gi6H%ST*V\`aVacqswxAiLtr   N)r7   r8   r9   rA   r#  r$  r:   r-   r<   r>   r?   r%  r   r   r   r  r    sC    &8NQO)L: :
 LHHr   r  c                   >    e Zd Zg dZg dZed        ZdZdZdZ	d Z
y)	LatentUpscaleBy)r  r  zscale latentr  c           	      6    dd| j                   fddddddfdiS )	Nr#   r   rP   g      ?rS          @rT   )r   r!  scale_byr#  r+   s    r   r-   zLatentUpscaleBy.INPUT_TYPES6  s7    IZIZH\+2DY\fj4k*lo p 	pr   r   r%  r   c                     |j                         }t        |d   j                  d   |z        }t        |d   j                  d   |z        }t        j                  j                  |d   |||d      |d<   |fS )Nr   r   r   r  )rm   r  rh   r   rV  r'  )r5   r   r!  r,  r,   r   r   s          r   r%  zLatentUpscaleBy.upscale?  sy    LLNgi(..r2X=>wy)//3h>?{{11')2DeVUceop)tr   N)r7   r8   r9   rA   r#  r:   r-   r<   r>   r?   r%  r   r   r   r)  r)  1  s6    HNQOp p LHHr   r)  c                   .    e Zd Zed        ZdZdZdZd Zy)LatentRotatec                     ddg dfdiS )Nr#   r   )r  z
90 degreesz180 degreesz270 degrees)r   rotationr   r+   s    r   r-   zLatentRotate.INPUT_TYPESG  s    +_*a  ! 	!r   r   rotatemodel/latent/transformc                     |j                         }d}|j                  d      rd}n'|j                  d      rd}n|j                  d      rd}t        j                  |d   |ddg	      |d<   |fS )
Nr   90r^   180r`   270r   r   )r
  dims)rm   r  ri   rot90)r5   r   r2  r,   	rotate_bys        r   r3  zLatentRotate.rotateQ  sq    LLN	t$I  'I  'I{{79#5!QP)tr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r3  r   r   r   r0  r0  F  s*    ! ! LH'Hr   r0  c                   4    e Zd ZdgZed        ZdZdZdZd Z	y)
LatentFlipzmirror latentc                     ddddgfdiS )Nr#   r   zx-axis: verticallyzy-axis: horizontally)r   flip_methodr   r+   s    r   r-   zLatentFlip.INPUT_TYPESa  s"    /CE[.\-^  ! 	!r   r   flipr4  c                     |j                         }|j                  d      r!t        j                  |d   dg      |d<   |fS |j                  d      rt        j                  |d   dg      |d<   |fS )Nr   r   r`   )r9  r   r   )rm   r  ri   r@  )r5   r   r?  r,   s       r   r@  zLatentFlip.flipk  sq    LLN!!#& ::gi&8sCAiL t ##C( ::gi&8sCAiLtr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r@  r   r   r   r=  r=  ^  s2    %&N! ! LH'Hr   r=  c                   8    e Zd Zg dZed        ZdZdZdZddZ	y)	LatentComposite)zoverlay latentzlayer latentzpaste latentc                 X    ddddddt         ddfdddt         ddfdddt         ddfdiS )Nr#   r   r   r   r   rT   )
samples_tosamples_fromr   r   featherr   r+   s    r   r-   zLatentComposite.INPUT_TYPESw  sP    K.9$)q>cd+e#f$)q>cd+e#f*/QqQ_ij1k)l	  ! 	!r   r   	compositer   c           	         |dz  }|dz  }|dz  }|j                         }|d   j                         }|d   }|d   }|dk(  rb|d d d d d |j                  d   |z
  d |j                  d   |z
  f   |d d d d |||j                  d   z   |||j                  d   z   f<   n+|d d d d d |j                  d   |z
  d |j                  d   |z
  f   }t        j                  |      }	t        |      D ]&  }
|dk7  r%|	d d d d |
d|
z   d d fxx   d|z  |
dz   z  z  cc<   ||j                  d   z   |j                  d   k  rE|	d d d d |	j                  d   dz
  |
z
  |	j                  d   |
z
  d d fxx   d|z  |
dz   z  z  cc<   |dk7  r%|	d d d d d d |
d|
z   fxx   d|z  |
dz   z  z  cc<   ||j                  d   z   |j                  d   k  s|	d d d d d d |	j                  d   dz
  |
z
  |	j                  d   |
z
  fxx   d|z  |
dz   z  z  cc<   ) t        j                  |	      |	z
  }|d d d d d |j                  d   |z
  d |j                  d   |z
  f   |	z  |d d d d |||j                  d   z   |||j                  d   z   f   |z  z   |d d d d |||j                  d   z   |||j                  d   z   f<   ||d<   |fS )Nr   r   r   r`   r   r^   rQ   )rm   r  rh   ri   	ones_likerg   )r5   rE  rF  r   r   composite_methodrG  samples_outr,   r   r   rev_masks               r   rH  zLatentComposite.composite  s5   !VFQ, oo'y!'')	*
#I.a<IUVWXYZr[e[k[klm[nqr[rZr  uMu  vF  vF  GH  vI  LM  vM  uM  WM  JNAa!Al((+++Aa0B0B10E.E,EEF'!,DZ-=-=a-@1-D,DF^zGWGWXYGZ]^G^F^(^_L??<0D7^61QqsU1%3w;1q5*AB%|))!,,z/?/?/BB1TZZ]A-q0$**Q-/A!CD#g+Z[^_Z_I`aD61Qq1u%3w;1q5*AB%|))!,,z/?/?/BB1Qtzz!}a/!3TZZ]A5EEEFCPWK\]`a\aKbcF $ t,t3HIUVWXYZr[e[k[klm[nqr[rZr  uMu  vF  vF  GH  vI  LM  vM  uM  WM  JN  QU  JU  XY  Z[  \]  ^_  `a  bn  bt  bt  uv  bw  `w  ^w  xy  z{  |H  |N  |N  OP  |Q  zQ  xQ  ZQ  XR  U]  X]  J]Aa!Al((+++Aa0B0B10E.E,EEF!"I~r   N)normalr   )
r7   r8   r9   rA   r:   r-   r<   r>   r?   rH  r   r   r   rC  rC  t  s/    GN! ! LHHr   rC  c                   H    e Zd ZddgZed        ZdZdZdZdde	de
fd	Zd
 Zy)LatentBlendzmix latentszinterpolate latentsc           	           dddddddddfd	iS )
Nr#   r   rP   r
  r   r^   rS   rT   )samples1samples2blend_factorr   r+   s    r   r-   zLatentBlend.INPUT_TYPES  s3    ##$	' 	
 	 		r   r   blendr   rT  
blend_modec                    |j                         }|d   }|d   }|j                  |j                  k7  rf|j                  dddd       t        j                  j                  ||j                  d   |j                  d   dd      }|j                  dddd       | j                  |||      }||z  |d|z
  z  z   }||d<   |fS )	Nr   r   r   r^   r`   r  r  r  )rm   rh   permuter   rV  r'  rV  )r5   rR  rS  rT  rV  rL  samples_blendeds          r   rU  zLatentBlend.blend  s    mmoI&I&>>X^^+Q1a({{11(HNN1<Mx~~^_O`bkrz1{HQ1a(//(HjI"\1Oq<GW4XX!0I~r   c                 ,    |dk(  r|S t        d|       )NrN  zUnsupported blend mode: )
ValueError)r5   img1img2r  s       r   rV  zLatentBlend.blend_mode  s"    8K7v>??r   N)rN  )r7   r8   r9   rA   r:   r-   r<   r>   r?   rt  strrU  rV  r   r   r   rP  rP    sI    #%:;N
 
 LHHU   @r   rP  c                   6    e Zd ZddgZed        ZdZdZdZd Z	y)	
LatentCropztrim latentz
cut latentc                 n    dddddt         ddfdddt         ddfdddt         ddfdddt         ddfd	iS )
Nr#   r   r   r   r   r   rT   r   )r   r   r   r   r   r   r+   s    r   r-   zLatentCrop.INPUT_TYPES  s`    (-3rR`jk/l'm).CSakl0m(n$)q>cd+e#f$)q>cd+e#f	  ! 	!r   r   r  r4  c                 2   |j                         }|d   }|dz  }|dz  }||j                  d   dz
  kD  r|j                  d   dz
  }||j                  d   dz
  kD  r|j                  d   dz
  }|dz  }|dz  }||z   }	||z   }
|d d d d ||
||	f   |d<   |fS )Nr   r   r   r`   )rm   rh   )r5   r   r   r   r   r   r,   
new_height	new_widthto_xto_ys              r   r  zLatentCrop.crop  s    LLN)$!VF a 1$%a 1$Aa 1$%a 1$Aq[
QJ	1}A~q1T61T612)tr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r`  r`    s4    #\2N! ! LH'Hr   r`  c                   .    e Zd Zed        ZdZdZdZd Zy)SetLatentNoiseMaskc                     ddddiS )Nr#   r   r   )r   r   r   r+   s    r   r-   zSetLatentNoiseMask.INPUT_TYPES  s    &/  ! 	!r   r   set_maskr  c                     |j                         }|j                  dd|j                  d   |j                  d   f      |d<   |fS )Nr   r^   r   r  )rm   r   rh   )r5   r   r   r,   s       r   rj  zSetLatentNoiseMask.set_mask  s?    LLN,,Atzz"~tzz"~'NO,tr   N)	r7   r8   r9   r:   r-   r<   r>   r?   rj  r   r   r   rh  rh    s*    ! ! LH%Hr   rh  c                    |d   }t         j                  j                  | ||j                  dd       |j                  dd             }|
r<t	        j
                  |j                         |j                  |j                  d      }n,d|v r|d   nd }t         j                  j                  |||      }d }d|v r|d   }t        j                  | |      }t         j                  j                   }t         j                  j                  | |||||||||	|
|||||||      }|j                         }|j                  dd        |j                  dd        ||d<   |fS )	Nr   r   downscale_ratio_temporalrn  )r_  layoutrp  r  r  )	denoisedisable_noise
start_step	last_stepforce_full_denoiser  callbackdisable_pbarseed)r   samplefix_empty_latent_channelsrf   ri   rk   r  r_  rn  prepare_noiselatent_previewprepare_callbackrV  PROGRESS_BAR_ENABLEDrm   pop)r  rv  stepscfgsampler_name	schedulerr#  r$  r   ro  rp  rq  rr  rs  latent_imagenoise
batch_indsr  rt  ru  r   ro   s                         r   common_ksamplerr    s~   )$L<<99%vzzZsuyOz  }C  }G  }G  Hb  dh  }i  jLL--/|7I7IR^ReRenst.;v.EVM*4
**<zJJvL)
..ue<H{{777Lll!!%sL)U]_giu*1[eqz5GT^iq  AM  TX " YG ++-CGG%t,GG&-C	N7Nr   c                   @    e Zd Zed        ZdZdZdZdZdZ	g dZ
d
dZy	)KSamplerc                    ddddifdddddd	d
fddddddfddddddddft         j                  j                  j                  ddift         j                  j                  j                  ddifdddifdddifdddifdddddd d!fd"
iS )#Nr#   r  r&   z.The model used for denoising the input latent.r   r       Tz,The random seed used for creating the noise.)rU   rV   rW   control_after_generater&      r^   '  z2The number of steps used in the denoising process.r  rP   r+  rR   r  皙?rS   zThe Classifier-Free Guidance scale balances creativity and adherence to the prompt. Higher values result in images more closely matching the prompt however too high values will negatively impact quality.)rU   rV   rW   rX   r  r&   zhThe algorithm used when sampling, this can affect the quality, speed, and style of the generated output.zHThe scheduler controls how noise is gradually removed to form the image.r;   zLThe conditioning describing the attributes you want to include in the image.zNThe conditioning describing the attributes you want to exclude from the image.r   zThe latent image to denoise.rQ   zThe amount of denoising applied, lower values will maintain the structure of the initial image allowing for image to image sampling.r  )
r  rv  r~  r  r  r  r#  r$  r  ro  r   samplersr  SAMPLERS
SCHEDULERSr+   s    r   r-   zKSampler.INPUT_TYPES  s/    !I/_#`aAa@Rnr  @n  !o  pR%  UI  "J  KSUSVae  s@  "A  B!&!8!8!A!AI  Pz  D{  !|#nn55@@9  OY  CZ  [+i  :H  .I  J+i  :J  .K  L!)I7U+V W#SVZ  hn  &o  p
 	
r   r   )zThe denoised latent.rw  model/samplingzXUses the provided model, positive and negative conditioning to denoise the latent image.)samplerrw  r  ro  diffusetxt2imgimg2imgc                 ,    t        |||||||||	|

      S )N)ro  r  )r5   r  rv  r~  r  r  r  r#  r$  r  ro  s              r   rw  zKSampler.sample1  s-    udE3iQY[ceq  |C  D  	Dr   NrQ   )r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   rw  r   r   r   r  r    s:    
 
  L/OHHlKbNDr   r  c                   0    e Zd Zed        ZdZdZdZddZy)KSamplerAdvancedc                    ddddgddifdddd	dd
fdddddfdddddddft         j                  j                  j                  ft         j                  j                  j                  fdddddddddfddddddfddgddifdiS )Nr#   r  enabledisabler   Tr   r   r  )rU   rV   rW   r  r  r^   r  r  rP   r+  rR   r  r  rS   )rU   rV   rW   rX   r  rF   r   )rU   rV   rW   r   )r  	add_noise
noise_seedr~  r  r  r  r#  r$  r  start_at_stepend_at_stepreturn_with_leftover_noiser  r+   s    r   r-   zKSamplerAdvanced.INPUT_TYPES5  s    (#+Y"7*d9K!L#(aJ\x|*}"~#Ae%LM#SWZei%jk%*^^%<%<%E%E$H"'.."9"9"D"D!G 2 2$0&+1U`d-e%f$)uQubf+g#h4=x3H:W[J\2] 	r   r   rw  r  c                 X    d}|dk(  rd}d}|dk(  rd}t        ||||||||	|
|||||      S )NTr  Fr  )ro  rp  rq  rr  rs  r  )r5   r  r  r  r~  r  r  r  r#  r$  r  r  r  r  ro  rs  rp  s                    r   rw  zKSamplerAdvanced.sampleM  so    !%1!&	! Muj%lIW_aikw  BI  Yf  s@  LW  l~    	r   Nr  )	r7   r8   r9   r:   r-   r<   r>   r?   rw  r   r   r   r  r  4  s+     $ LHHr   r  c                   J    e Zd Zd Zed        ZdZdZdZdZ	dZ
dZg d	ZddZy
)	SaveImagec                 `    t        j                         | _        d| _        d| _        d| _        y )NrC  r@  r   )r1  r2  r3  rG  prefix_appendcompress_levelr4  s    r   r5  zSaveImage.__init__W  s)    &;;=	r   c                 *    dddifddddfdd	d
ddS )Nr   r&   zThe images to save.r)   r^  zThe prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes.r"  )r   r7  r8  r9  r:  r<  r   r+   s    r   r-   zSaveImage.INPUT_TYPES]  sE     #Y0E$FG$,)  Qq  /r  $s
 #_
 	
r   r   save_imagesTr@  Basicsz8Saves the input images to your ComfyUI output directory.)r>  z
save imagezexport imageoutput imagezwrite imagedownloadNc           	         || j                   z  }t        j                  || j                  |d   j                  d   |d   j                  d         \  }}}}}t               }	t        |      D ]f  \  }
}d|j                         j                         z  }t        j                  t        j                  |dd      j                  t        j                              }d }t        j                   sbt#               }|%|j%                  dt'        j(                  |             |/|D ]*  }|j%                  |t'        j(                  ||                , |j+                  dt-        |
            }| d|dd	}|j/                  t0        j2                  j5                  ||      || j6                  
       |	j9                  ||| j:                  d       |dz  }i dd|	iiS )Nr   r^        o@   r0   z%batch_num%rA  rB  z_.png)pnginfor  rD  rK  r   )r  r1  rM  r3  rh   list	enumeratern  numpyr   	fromarraynpr(   astypeuint8r   rP  r   add_textrN  rO  replacer^  r>  rQ  rR  rS  r  rn   rG  )r5   r   r7  r0   r;  rX  rE  rY  rF  r\  batch_numberr@  rr   imgrJ  r   filename_with_batch_numr[  s                     r   r  zSaveImage.save_imagess  s   4---LXLlLlm|  C  N  N  PV  WX  PY  P_  P_  `a  Pb  dj  kl  dm  ds  ds  tu  dv  MwIHgy/&%.v%6!\5uyy{((**A//"''!Q"4";";BHH"EFCH(("9%%%h

60BC ,* ))!TZZa8H-IJ + '/&6&6}c,FW&X#-.a|5ADHHRWW\\"4d;X^b^q^qHrNN &		 
 qLG) &7, 7,..r   r]  )r7   r8   r9   r5  r:   r-   r<   r>   r_  r?   rL   r@   rA   r  r   r   r   r  r  V  sF      	
 	
 LHKH"LKfN/r   r  c                   *    e Zd Zd Zg dZed        Zy)PreviewImagec                     t        j                         | _        d| _        ddj	                  d t        d      D              z   | _        d| _        y )Ntemp_temp_r@  c              3   F   K   | ]  }t        j                  d         yw)abcdefghijklmnopqrstupvxyzN)randomchoice).0r   s     r   	<genexpr>z(PreviewImage.__init__.<locals>.<genexpr>  s     /nem`a>Z0[ems   !r   r^   )r1  get_temp_directoryr3  rG  rS  rg   r  r  r4  s    r   r5  zPreviewImage.__init__  sD    &99;	%/nejklem/n(nnr   )previewzpreview imagez
show imagez
view imagezdisplay imagezimage viewerc                     ddiddddS )Nr   r   r8  r9  r:  r<  r   r+   s    r   r-   zPreviewImage.INPUT_TYPES  s     {-%-P 	r   N)r7   r8   r9   r5  rA   r:   r-   r   r   r   r  r    s!      oN r   r  c                   Z    e Zd Zed        ZdZdZg dZdZdZ	d Z
ed        Zed	        Zy
)	LoadImagec           	      H   t        j                         }t        j                  |      D cg c]B  }t        j                  j                  t        j                  j                  ||            sA|D }}t        j                  |dg      }ddt        |      ddifiiS c c}w )Nr@  r#   image_uploadT)	r1  rc  rQ  rd  rR  re  rS  filter_files_content_typesrg  rh  s       r   r-   zLoadImage.INPUT_TYPES  s     446	JJy1`1qRWW^^BGGLLQZ\]D^5_1`77yIve}~t.DEF 	 as   AB/Br@  r  )z
load imagez
open imagezimport imagezimage inputzupload imagez
read imagezimage loaderr   r   
load_imagec                    t        j                  |      }t        j                  j	                         }t        j                  j                         }t        j                  |      j                         }|j                  j                  d   dkD  r|j                  j                  ||      |j                  $d|j                  d   z
  j                  ||      fS t        j                  |j                  j                  d   ddf||      fS t        j                   t"        j$                  |      }g }g }d\  }	}
t'        j(                  |      D ]  }t        j                   t*        j,                  |      }|j/                  d      }t1        |      dk(  r|j2                  d   }	|j2                  d	   }
|j2                  d   |	k7  s|j2                  d	   |
k7  rt5        j6                  |      j9                  t4        j:                        d
z  }t        j<                  |      d   }d|j?                         v r]t5        j6                  |jA                  d            j9                  t4        j:                        d
z  }dt        j<                  |      z
  }n&t        j                  dt        j:                  d      }|jC                  |j                  |             |jC                  |jE                  d      j                  |              t        jF                  |d      }t        jF                  |d      }|j                  ||      |j                  ||      fS )Nr   r  rQ   ).r   r   r_  rp  )NNRGBr^   r  r   A)r   r   rn  r  ra   )$r1  rq  r   r   r  r  r   VideoFromFileget_componentsr   rh   r  alphari   rk   r   pillowr   r}  r   Iteratorr   exif_transposeconvertrc   r  r  arrayr  float32
from_numpygetbands
getchannelrn   r   rj   )r5   r@  r  r_  rp  
componentsr  output_imagesoutput_maskswhrr   r   output_imageoutput_masks                  r   r  zLoadImage.load_image  s&   !88?
&&99;'';;=,,Z8GGI
""1%)%%((e(D  KU  K[  K[  KgsZM]M]^eMfGfFjFjrx  AFFj  GG  {  {  mr  mx  mx  zD  zK  zK  zQ  zQ  RS  zT  VX  Z\  y]  ej  sy  mz  {  { !!%**j91'',A##H$;$;Q?AIIe$E=!Q&JJqMJJqMzz!}!UZZ]a%7HHUO**2::6>E$$U+E2Eajjl"xxS 1299"**EME,,T22{{85==O  !67q 1 4 45 4 AB) -, yyA6ii!4vU;[^^SYaf^=ghhr   c                    t        j                  |      }t        j                         }t	        |d      5 }|j                  |j                                d d d        |j                         j                         S # 1 sw Y   'xY wrx  rz  )r,   r@  r  r  rj  s        r   r  zLoadImage.IS_CHANGED  s]    !88?
NN*d#qHHQVVX $xxz~~ $#r  c                 P    t        j                  |      sdj                  |      S y)NzInvalid image file: {}Tr  )r,   r@  s     r   r  zLoadImage.VALIDATE_INPUTS  s$    55e<+22599r   N)r7   r8   r9   r:   r-   r?   rL   rA   r<   r>   r  r  r  r   r   r   r  r    s_      H"~N$LH*iX      r   r  c                   f     e Zd ZdZg dZg dZe fd       ZdZdZ	dZ
 fdZe fd	       Z xZS )
LoadImageMaskImage Tools)zimport maskz
alpha maskzchannel mask)r  redgreenbluec                 P    t         |          }di |d   d| j                  fiiS )Nr#   channel)superr-   _color_channels)r,   types	__class__s     r   r-   zLoadImageMask.INPUT_TYPES  sB    #% 
#A--0
 	
r   r@  r   load_image_maskc                 R   t         |   |      \  }}|d   j                         }|dk(  r|fS ddddj                  |d      }||j                  d   k  r|d|f   j                         fS t        j                  |j                  d d |j                  |j                        }|fS )	Nr   r  r^   r`   )RGBr   .r  )
r  r  r  rf   rh   r  ri   rk   r_  rp  )	r5   r@  r  image_tensormask_tensorr   channel_idx
empty_maskr  s	           r   r  zLoadImageMask.load_image_mask   s    $)G$6u$=!kAJ8>!AA.221a8++B// k!1288:<<""3B'"((#**J
 = r   c                 "    t         |   |      S r   )r  r  )r,   r@  r  r  s      r   r  zLoadImageMask.IS_CHANGED  s    w!%((r   )r7   r8   r9   rL   rA   r  r:   r-   r?   r<   r>   r  r  __classcell__)r  s   @r   r  r    sO    'BN7O
 
 HL H!& ) )r   r  c                   0    e Zd ZddgZed        ZdZdZdZy)LoadImageOutputr  zprevious generationc           	      "    dddddddddd	fiiS )
Nr#   r@  COMBOTrC  z/internal/files/outputfirst)routerefresh_buttoncontrol_after_refresh)r  image_folderremoter   r+   s    r   r-   zLoadImageOutput.INPUT_TYPES  s9     '$($,!9*.18$ 

 	
r   zLoad an image from the output folder. When the refresh button is clicked, the node will update the image list and automatically select the first image, allowing for easy iteration.Tr  N)	r7   r8   r9   rA   r:   r-   r@   EXPERIMENTALr>   r   r   r   r  r    s1    $&;<N
 
 IKLHr   r  c                   J    e Zd Zg dZddgZed        ZdZdZdZ	dZ
g d	Zd
 Zy)
ImageScaler  r  r   r  lanczosr  r  c           
      n    dd| j                   fdddt        ddfdddt        ddf| j                  fdiS )	Nr#   r   r   r   r   r^   rT   )r@  r!  r   r   r  r"  r+   s    r   r-   zImageScale.INPUT_TYPES4  sQ    zaFWFWEY(-3qQ_ij/k'l).CR`jk0l(m'(~~&79 : 	:r   r   r%  image/upscalingr  )resizezresize imagezscale imagezimage resizezoomzzoom inzchange sizec                    |dk(  r
|dk(  r|}|fS |j                  dd      }|dk(  r6t        dt        |j                  d   |z  |j                  d   z              }n:|dk(  r5t        dt        |j                  d   |z  |j                  d   z              }t        j
                  j                  |||||      }|j                  dd      }|fS )Nr   r   r^   r   r`   )rF  rW   r  rh   r   rV  r'  )r5   r@  r!  r   r   r  r,   r   s           r   r%  zImageScale.upscaleA  s    A:&A+A t mmBq)GzAuW]]1%5%>qAQ%QRS1QgmmA&6&>qAQ&Q RS**7E6>SWXA		!BAtr   N)r7   r8   r9   r#  r$  r:   r-   r<   r>   r?   rL   rA   r%  r   r   r   r  r  0  sD    QO)L: :
 LH H'pNr   r  c                   :    e Zd ZdZg dZed        ZdZdZdZ	d Z
y)	ImageScaleByr  r  c           	      6    dd| j                   fddddddfdiS )	Nr#   r   rP   rQ   rS   r+  rT   )r@  r!  r,  r-  r+   s    r   r-   zImageScaleBy.INPUT_TYPEST  s7    zaFWFWEY+2DY\fj4k*lo p 	pr   r   r%  r  c                    |j                  dd      }t        |j                  d   |z        }t        |j                  d   |z        }t        j                  j                  ||||d      }|j                  dd      }|fS )Nr   r^   r   r`   r  )rF  r  rh   r   rV  r'  )r5   r@  r!  r,  r   r   r   r,   s           r   r%  zImageScaleBy.upscale]  st    --1%gmmA&12w}}Q'(23KK&&wv~zZIIaOtr   N)r7   r8   r9   rL   r#  r:   r-   r<   r>   r?   r%  r   r   r   r  r  P  s7    'QOp p LH Hr   r  c                   8    e Zd ZdgZdZed        ZdZdZdZ	d Z
y)	ImageInvertzreverse colorsr  c                     dddiiS )Nr#   r@  r   r   r+   s    r   r-   zImageInvert.INPUT_TYPESi  s    gz233r   r   invertzimage/colorc                     d|z
  }|fS )NrQ   r   )r5   r@  r,   s      r   r  zImageInvert.invertr  s    %Ktr   N)r7   r8   r9   rA   rL   r:   r-   r<   r>   r?   r  r   r   r   r  r  e  s8    &'N'4 4 LHHr   r  c                   :    e Zd Zg dZed        ZdZdZdZdZ	d Z
y)	
ImageBatch)zcombine imageszmerge imageszstack imagesc                     ddddiS )Nr#   r   )image1image2r   r+   s    r   r-   zImageBatch.INPUT_TYPESy  s    
jIJJr   r   batchzimage/batchTc                 d   |j                   d   |j                   d   k7  rz|j                   d   |j                   d   kD  r.t        j                  j                  j	                  |ddd      }n-t        j                  j                  j	                  |ddd      }|j                   dd  |j                   dd  k7  r]t
        j                  j                  |j                  dd      |j                   d   |j                   d   dd	      j                  dd      }t        j                  ||fd
      }|fS )Nr   )r   r^   constantrQ   )r  r   r^   r`   r  r  r   ra   )
rh   ri   r  r  padr   rV  r'  rF  rj   )r5   r$  r%  r,   s       r   r&  zImageBatch.batch  s   <<v||B//||B&,,r"22,,00ZWZ0[,,00ZWZ0[<<v||AB//[[//r!0DfllSToW]WcWcdeWfhrt|}  F  F  GH  IK  LFIIvv&A.tr   N)r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r&  r   r   r   r"  r"  v  s6    GNK K LHHJ	r   r"  c                   8    e Zd ZddZed        ZdZdZdZd	dZ	y)

EmptyImagec                     || _         y r   ro  )r5   rp  s     r   r5  zEmptyImage.__init__  s	    r   c                 \    ddddt         ddfdddt         ddfdddddfdddd	dd
dfdiS )Nr#   r   r   r^   rT   r   r  r   i color)rU   rV   rW   rX   display)r   r   r  r.  r   r+   s    r   r-   zEmptyImage.INPUT_TYPES  s^    3qQ_ij/k'l).CR`jk0l(m-2!TX4Y,Z(-1Qxabov/w'x  ! 	!r   r   r  r@  c                    t         j                  j                         }t         j                  j                         }t	        j
                  |||dg|dz	  dz  dz  ||      }t	        j
                  |||dg|dz	  dz  dz  ||      }t	        j
                  |||dg|dz  dz  ||      }	t	        j                  |||	fd      fS )Nr^   r  r  r  r   r   ra   )r   r   r  r  ri   fullrj   )
r5   r   r   r  r.  r_  rp  rgbs
             r   r  zEmptyImage.generate  s    &&99;'';;=JJ
FE15"8LPT7T]ckpqJJ
FE15!t8Kt7S\bjopJJ
FE15$$7NW]ejk		1a),//r   N)rn  )r^   r   )
r7   r8   r9   r5  r:   r-   r<   r>   r?   r  r   r   r   r+  r+    s1     ! ! LHH0r   r+  c                   6    e Zd ZddgZed        ZdZdZdZd Z	y)	ImagePadForOutpaintzextend canvaszexpand imagec                     dddddt         ddfdddt         ddfdddt         ddfdddt         ddfdddt         dd	d
fdiS )Nr#   r   r   r   r   rT   (   r^   Tr   )r@  lefttoprightbottom
featheringr   r+   s    r   r-   zImagePadForOutpaint.INPUT_TYPES  sv     #AaXY Z[1Q~WXYZQqYZ![\ a.Z["\]$"Q~_`nr&st	
 		
r   r  expand_imagezimage/transformc                    |j                         \  }}}	}
t        j                  |||z   |z   |	|z   |z   |
ft        j                        dz  }||d d |||z   |||	z   d d f<   t        j                  ||z   |z   |	|z   |z   ft        j                        }t        j                  ||	ft        j                        }|dkD  r|dz  |k  rx|dz  |	k  rpt        |      D ]b  }t        |	      D ]R  }|dk7  r|n|}|dk7  r||z
  n|}|dk7  r|n|	}|dk7  r|	|z
  n|	}t        ||||      }||k\  rA||z
  |z  }||z  |||f<   T d |||||z   |||	z   f<   ||j                  d      fS )Nr  r
  r   r`   )r  ri   r  r  rk   rg   rV   r   )r5   r@  r9  r:  r;  r<  r=  d1d2d3d4	new_imager   r   rr   jdtdbdldrr   r  s                         r   r>  z ImagePadForOutpaint.expand_image  s   BBJJcF"BI$5r:--
 	
 9>	!Sr\4r	>145zz#XT	E 12--

 KKH--

 >j1nr1j1nr6I2YrA!QhBB#)Q;aBB"aiRB#(A:a2BBB+AJ #a:5A!eAadG # " ./Sr\4r	>)*4>>!,--r   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r>  r   r   r   r6  r6    s4    %~6N

 

 %LH H).r   r6  )r   r   r  r  r  r  r  r  ra  r/  r   r   r  zKSampler (Advanced)z(Load Checkpoint With Config (DEPRECATED)zLoad CheckpointzLoad VAEzLoad LoRA (Model and CLIP)z	Load LoRAz	Load CLIPzLoad ControlNet ModelzLoad ControlNet Model (diff)zLoad Style ModelzLoad CLIP VisionzLoad Diffusion ModelzLoad unCLIP CheckpointzLoad GLIGEN ModelzCLIP Vision EncodezApply Style ModelzCLIP Text Encode (Prompt)zCLIP Set Last LayerzConditioning (Combine)zConditioningAverage zConditioning (Average)zConditioning (Concat)zConditioning (Set Area)z'Conditioning (Set Area with Percentage)zConditioning (Set Mask)zApply ControlNet (DEPRECATED)zApply ControlNetzVAE Encode (for Inpainting)zSet Latent Noise Maskz
VAE Decodez
VAE EncodezRotate LatentzFlip LatentzCrop LatentzEmpty Latent ImagezUpscale LatentzUpscale Latent ByzLatent CompositezLatent BlendzLatent From BatchzRepeat Latent BatchzEmpty Imagez
Save ImagezPreview Imagez
Load ImagezLoad Image (as Mask)zLoad Image (from Outputs)zUpscale ImagezUpscale Image ByzInvert Image ColorszPad Image for OutpaintingzBatch Images (DEPRECATED)z
Crop ImagezStitch ImageszBlend Imagesz
Blur ImagezQuantize ImagezSharpen ImagezScale Image to Total PixelszGet Image SizezVAE Decode (Tiled)zVAE Encode (Tiled))r"  	ImageCropImageStitch
ImageBlend	ImageBlurImageQuantizeImageSharpenImageScaleToTotalPixelsGetImageSizer   r   module_pathr!   c                     t         j                  j                  |       }t         j                  j                  |       r"t         j                  j	                  |      d   }|S )a  
    Returns the module name based on the given module path.
    Examples:
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node.py") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__.py") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__/") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node.disabled") -> "custom_nodes
    Args:
        module_path (str): The path of the module.
    Returns:
        str: The module name.
    r   )rQ  rR  basenamere  r%  )rR  	base_paths     r   get_module_namerV  z  sG        -I	ww~~k"GG$$Y/2	r   custom_nodesc           	        K   t        |       }t        j                  j                  |       r't        j                  j	                  |       }|d   }|}n1t        j                  j                  |       r| j                  dd      }	 t        j                  dj                  |              t        j                  j                  |       rCt        j                  j                  |       }t        j                  j                  |       d   }n@t        j                  j                  t        j                  j                  | d            }| }t        j                  j                  |      }|t         j"                  |<   |j$                  j'                  |       t        j                  j)                  |      t*        |<   	 ddlm}	 |	j1                  |       }
|
j2                  j4                  }|rt        j                  j                  | |      }t        j                  j                  |      rD|
j6                  j8                  }|t:        |<   t        j<                  dj                  ||             tC        |d	      r{tE        |d	      ot        j                  j)                  t        j                  j                  |tE        |d	                  }t        j                  j                  |      r	|t:        |<   tC        |d
      rtE        |d
      |jF                  jI                         D ]3  \  }}||vs|tF        |<   dj                  |t        |             |_%        5 tC        |d      r+tE        |d      tL        jO                  |jL                         ytC        |d      retE        |d      }tQ        |      st        j@                  d|  d       y	 tS        jT                  |      r |        d {   }n |       }tW        |tX              st        j@                  d|  d       y|j[                          d {    |j]                          d {   }tW        |t^              st        j@                  d|  d       y|D ]}  }|ja                         }|jb                  |vr3|tF        |jb                  <   dj                  |t        |             |_%        |jd                  a|jd                  tL        |jb                  <    yt        j@                  d|  d       y# t>        $ r#}t        j@                  d|        Y d }~d }~ww xY w7 ]7 7 # t>        $ r%}t        j@                  d|  d|        Y d }~yd }~ww xY w# t>        $ rL}t        j@                  tg        jh                                t        j@                  d|  d|        Y d }~yd }~ww xY ww)Nr   ._x_zTrying to load custom node {}z__init__.py)config_parserz+Automatically register web folder {} for {}zwUnable to parse pyproject.toml due to lack dependency pydantic-settings, please run 'pip install -r requirements.txt': WEB_DIRECTORYNODE_CLASS_MAPPINGSz{}.{}NODE_DISPLAY_NAME_MAPPINGSTcomfy_entrypointzcomfy_entrypoint in z is not callable, skipping.Fz+ did not return a ComfyExtension, skipping.z* did not return a list of nodes, skipping.z(Error while calling comfy_entrypoint in z: zSkip z_ module for custom nodes due to the lack of NODE_CLASS_MAPPINGS or comfy_entrypoint (need one).zCannot import z module for custom nodes: )5rV  rQ  rR  re  r%  isdirr  rd   debugr  	importlibutilspec_from_file_locationr  rS  module_from_specsysmodulesloaderexec_moduleabspathLOADED_MODULE_DIRScomfy_configr[  extract_node_configuration
tool_comfywebprojectr  EXTENSION_WEB_DIRSinfo	Exceptionre   hasattrr  r]  itemsRELATIVE_PYTHON_MODULEr^  r~  callableinspectiscoroutinefunction
isinstancer   on_loadget_node_listr  
GET_SCHEMAnode_iddisplay_name	traceback
format_exc)rR  ignoremodule_parentmodule_namespsys_module_namemodule_spec
module_dirmoduler[  project_configweb_dir_nameweb_dir_pathproject_nameeweb_dirr  node_cls
entrypoint	extension	node_listschemas                         r   load_custom_noder    s    !+.K	ww~~k"WWk*e%	{	#%--c59U5<<[IJ77>>+&#..@@R]^K{3A6J#..@@RTRYRYR^R^_jlyRz{K$J00='-O$&&v.*,''//**E;'	[2*EEkRN)4488L!ww||KF77==.#1#9#9#>#>L7C&|4LL!N!U!UVbdp!qr 6?+0P0\ggoobggll:wv?_&`aGww}}W%29";/ 601gfF[6\6h"("<"<"B"B"Dhv%08'-6=nn]TcdoTp6qH3 #E v;<QmAnAz*11&2S2STV/0 );<JJ'"6{mC^ _`..z:&0l 2I *I!)^<OO&:;-Gr$st '')))"+"9"9";;	!)T2OO&:;-Gq$rs  )H%002F~~V3>F+FNN;:A..XghsXt:u7**6EKEXEX26>>B !* 
 OOeK=  1P  Q  Re  	[OO  V  WX  VY  Z  [  [	[2 !3 *;  "J;-WYZ[Y\ ]^  	,,./.5OPQsSTs  BW&D9V  B2T% 2CV 6A"V W&=V W&U 7U85U -W&.U UU U-U W&A"U +U 
W&V $W&%	U.UV UV U U U 	V&VV W&VV 	W#AWW&W##W&c            	        K   t        t        j                               } t        j                  d      }g }|D ]  }t        j                  t
        j                  j                  |            }d|v r|j                  d       |D ]G  }t
        j                  j                  ||      }t
        j                  j                  |      r&t
        j                  j                  |      d   dk7  ri|j                  d      r{t        j                  r,|t        j                   vrt#        j$                  d| d       t        j&                  r.t)        j*                  |      rt#        j$                  d|        t-        j.                         }t1        || d	       d
{   }|j3                  t-        j.                         |z
  ||f       J  t5        |      dkD  rqt#        j$                  d       t7        |      D ]8  }	|	d   rd}
nd}
t#        j$                  dj9                  |	d   |
|	d                : t#        j$                  d       y
y
7 w)a  
    Initializes the external custom nodes.

    This function loads custom nodes from the specified folder paths and imports them into the application.
    It measures the import times for each custom node and logs the results.

    Returns:
        None
    rW  __pycache__r^   z.pyz	.disabledz	Skipping z; due to disable_all_custom_nodes and whitelist_custom_nodeszBlocked by policy: r  Nr   z
Import times for custom nodes:r`   r@  z (IMPORT FAILED)z{:6.1f} seconds{}: {})r  r]  r  r1  r  rQ  rd  rR  realpathremoverS  re  r%  rf  r   disable_all_custom_nodeswhitelist_custom_nodesrd   rr  enable_managercomfyui_managershould_be_disabledtimeperf_counterr  rn   rc   rg  r  )base_node_names
node_pathsnode_import_timescustom_node_pathpossible_modulespossible_modulerR  time_beforesuccessrx   import_messages              r   init_external_custom_nodesr    s     -2245O..~>J&::bgg&6&67G&HI,,##M2/O'',,'7IKww~~k*rww/?/?/LQ/OSX/X##K0,,HcHc1cy(99tuv"""55kBLL#6{m!DE++-K,[/YghhG$$d&7&7&9K&GV]%^_#  0 '0 !78)*At!#!3LL077!naPQdST + 	R " is   FI I!B4Ic                  r  K   t         j                  j                  t         j                  j                  t         j                  j	                  t
                    d      } g d}g }|D ]G  }t        t         j                  j                  | |      d       d{   r7|j                  |       I |S 7 w)a  
    Initializes the built-in extra nodes in ComfyUI.

    This function loads the extra node files located in the "comfy_extras" directory and imports them into ComfyUI.
    If any of the extra node files fail to import, a warning message is logged.

    Returns:
        None
    comfy_extras)rznodes_latent.pyznodes_hypernetwork.pyznodes_upscale_model.pyznodes_post_processing.pyznodes_mask.pyznodes_compositing.pyznodes_rebatch.pyznodes_model_merging.pyznodes_tomesd.pyznodes_clip_sdxl.pyznodes_canny.pyznodes_freelunch.pyznodes_custom_sampler.pyznodes_hypertile.pyznodes_model_advanced.pyznodes_model_downscale.pyznodes_images.pyznodes_video_model.pyznodes_ideogram4.pyznodes_train.pyznodes_dataset.pyznodes_sag.pyznodes_perpneg.pyznodes_stable3d.pyznodes_sdupscale.pyznodes_photomaker.pyznodes_pixart.pyznodes_cond.pyznodes_morphology.pyznodes_stable_cascade.pyznodes_differential_diffusion.pyznodes_ip2p.pyz%nodes_model_merging_model_specific.pyznodes_pag.pyznodes_align_your_steps.pyznodes_attention_multiply.pyznodes_advanced_samplers.pyznodes_webcam.pyznodes_audio.pyznodes_sd3.pyznodes_gits.pyznodes_controlnet.pyznodes_hunyuan.pyznodes_eps.pyznodes_flux.pyznodes_lora_extract.pyznodes_torch_compile.pyznodes_mochi.pyznodes_slg.pyznodes_mahiro.pyznodes_lt_upsampler.pyznodes_lt_audio.pyznodes_lt.pyznodes_hooks.pyznodes_multigpu.pyznodes_load_3d.pyznodes_cosmos.pyznodes_video.pyznodes_lumina2.pyznodes_wan.pyznodes_lotus.pyznodes_hunyuan3d.pyznodes_primitive.pyznodes_cfg.pyznodes_optimalsteps.pyznodes_hidream.pyznodes_fresca.pyznodes_apg.pyznodes_preview_any.pyznodes_ace.pyznodes_string.pyznodes_camera_trajectory.pyznodes_edit_model.pyznodes_tcfg.pyznodes_context_windows.pyznodes_qwen.pyznodes_chroma_radiance.pyznodes_pid.pyznodes_model_patch.pyznodes_easycache.pyznodes_audio_encoder.pyznodes_rope.pyznodes_logic.pyznodes_resolution.pyznodes_nop.pyznodes_kandinsky5.pyznodes_wanmove.pyznodes_ar_video.pyznodes_image_compare.pyznodes_zimage.pyznodes_glsl.pyznodes_lora_debug.pyznodes_textgen.pyznodes_color.pyznodes_toolkit.pyznodes_replacements.pyznodes_nag.pyznodes_sdpose.pyznodes_math.pyznodes_number_convert.pyznodes_painter.pyznodes_curve.pyznodes_bg_removal.pyznodes_rtdetr.pyznodes_frame_interpolation.pyznodes_sam3.pyznodes_void.pyznodes_wandancer.pyznodes_hidream_o1.pyznodes_save_3d.pyznodes_moge.pyznodes_mediapipe.pyznodes_gaussian_splat.pyznodes_triposplat.pyr  N)rQ  rR  rS  dirnamer  __file__r  rn   )
extras_dirextras_filesimport_failed	node_files       r   init_builtin_extra_nodesr  	  s      bggoobgg.>.>x.HI>ZJsLj M!	%bggll:y&IYghhh  + "  is   BB7B5B7 B7c                    K   t         j                  j                  t         j                  j                  t         j                  j	                  t
                    d      } t        t        j                  t         j                  j                  | d                  }g }|D ]F  }t        |d       d {   r|j                  t         j                  j                  |             H |S 7 7w)Ncomfy_api_nodesz
nodes_*.pyr  )rQ  rR  rS  r  r  r  rg  globr  rn   rT  )api_nodes_dirapi_nodes_filesr  r  s       r   init_builtin_api_nodesr  	  s     GGLL1A1A(1K!LN_`MTYYrww||M<'PQROM$	%i?PQQQ  !1!1)!<= %  Rs   B2C.4C,5C.:3C.c                  z   K   t        t        D  cg c]  } t        t        | d      |        c}        y c c} w w)NVERSION)version	api_class)r   r   r   r  )r  s    r   init_public_apisr  	  sF      &	 &a 	Ay)	
 &	  s   ;6;c                   K   t                d {    t                d {   }g }|rt                d {   }| rt                d {    nt	        j
                  d       t        |      dkD  rt	        j                  d       |D ]&  }t	        j                  dj                  |             ( t	        j                  d       t        j                  rt	        j                  d       nt	        j                  d       t	        j                  d       t        |      dkD  rt	        j                  d	       |D ]&  }t	        j                  dj                  |             ( t	        j                  d       t        j                  rt	        j                  d       nt	        j                  d       t	        j                  d       |S 7 7 7 7 w)
Nz Skipping loading of custom nodesr   zwWARNING: some comfy_api_nodes/ nodes did not import correctly. This may be because they are missing some dependencies.
zIMPORT FAILED: {}z`
This issue might be caused by new missing dependencies added the last time you updated ComfyUI.z7Please run the update script: update/update_comfyui.batz,Please do a: pip install -r requirements.txtr@  ztWARNING: some comfy_extras/ nodes did not import correctly. This may be because they are missing some dependencies.
)r  r  r  r  rd   rr  rc   re   r  r   windows_standalone_build)init_custom_nodesinit_api_nodesr  import_failed_apinodes        r   init_extra_nodesr  	  sg    

244M"8"::(***78
!  S  	T%DOO/66t<= &{|((OOUVOOJK
=A  P  	Q!DOO/66t<= "{|((OOUVOOJKG 4 ; 	+sB   GGGGGGGGFGGGGr,  )rQ   FNNFr  )ri   rQ  rf  rN  r  r{  rx  r  r  r  r  rd   PILr   r   r   PIL.PngImagePluginr   r  r  safetensors.torchrr  rR  insertrS  r  r  r  comfy.diffusers_loadr   comfy.samplerscomfy.samplecomfy.sdcomfy.utilscomfy.controlnetcomfy.comfy_typesr   r	   r
   r   comfy_api.internalr   r   comfy_api.version_listr   comfy_api.latestr   r   r   comfy.clip_visioncomfy.model_managementcomfy.cli_argsr   rb  r1  rz  r   r  r  r   r   r   r    rC   rN   rz   r   r   r   r   r   r   r   r   r   r   r   r  r/  ra  r  r  r  r  r  r  r  r  r-  r9  r=  rL  rU  rg  r  r  r  r  r  r  r  r  r  r  r  r  r)  r0  r=  rC  rP  r`  rh  r  r  r  r  r  r  r  r  r  r  r  r"  r+  r6  r]  r^  rq  rk  r^  rV  r  r  r  r  r  r  r  r  r   r   r   <module>r     s
    
 
          . . &   277<<0@0@0J KWU V       J J E 5 : :       G? =\ =03 3& &P 8 . *   6 2 " 6 B! !" "$&R &RR1, 1,h20 20j# #Ld d$ .p p:   -' -'^O* O] ]~ $ " >0  0 f 2 6 < " &  = =~ (  >E E." "H 8" "H * 0 ,* *X)@ )@V! !F  2D D:   D7/ 7/r9  J JZ))I ))Xi 0 @ * " 00 00>. >.BDD4D nD (	D
 D D .D D (D ]D D D *D D LD  !D" ]#D$ %D& *'D( L)D* ;+D, *-D. ./D0 *1D2 .3D4 .5D6 ,7D8 .9D: $%B;D< "#>=D> .?D@ (ADB ,CDD EDF ;GDH LIDJ *KDL *MDN *ODP *QDR *SDT nUDV (WDX YDZ ,[D\ ]D^ 6_D` (aDb 0cDd (eDf (gDh %$4 , 8(&.$@.GD LE
E -E
 BE /E E .E ;E +E /E :E *E *E (E  6!E" '#E& ,'E( *)E* 1+E, --E. 3/E0 41E2 13E4 45E6 $%N7E8 49E: 6;E< 1=E@ 8AEB 1CED EEF GEH OIEJ -KEL -MEN ,OEP %QER *SET )UEV >WEX +YEZ .[E^ -_E` aEb OcEd eEf +gEh 2iEj /kEl &mEn (oEp 6qEr ." %#<$**IE N      , 58E ^ ^\` ^@-^EP	$r   