
    L3i                   \
   d dl m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Zd dlmZmZmZ d dlmZ d dlZd dlZej0                  j3                  d ej0                  j5                  ej0                  j7                  ej0                  j9                  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/m0Z0m1Z1 d dl2Zd dl3Zd d
l4m5Z5 d dl6Z6d dl7Z7d dl8Z8d dl9Z9e5jt                  rd dl;Z;d Z<ddZ=dZ> G d de'      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. d/      ZO G d0 d1      ZP G d2 d3      ZQ G d4 d5      ZR G d6 d7      ZS G d8 d9      ZT G d: d;      ZU G d< d=      ZV G d> d?eV      ZW G d@ dA      ZX G dB dC      ZY G dD dE      ZZ G dF dG      Z[ G dH dI      Z\ G dJ dK      Z] G dL dM      Z^ G dN dO      Z_ G dP dQ      Z` G dR dS      Za G dT dU      Zb G dV dW      Zc G dX dY      Zd G dZ d[      Ze G d\ d]      Zf G d^ d_      Zg G d` da      Zh G db dc      Zi G dd de      Zj G df dg      Zk G dh di      Zl G dj dk      Zm G dl dm      Zn G dn do      Zo G dp dq      Zp G dr ds      ZqddtZr G du dv      Zs G dw dx      Zt G dy dz      Zu G d{ d|eu      Zv G d} d~      Zw G d d      Zx G d dew      Zy G d d      Zz G d d      Z{ G d d      Z| G d d      Z} G d d      Z~ G d d      Zi dvesd5eRde?d;eUd#eId'eKd+eMdAeXd_egdeejdgekdaehdceidzeud|evd~ewdexi deydezde{de|de}dede~deAde@deBdeCdeDdeEdeFdxetdseqdmeni doeodieldkemdqepd=eVdMe^dKe]dOe_dSeadWecdYeddGe[dIe\dCeYdEeZdUebdQe`eJeLeTeeefeNeQeSePeOeGeHeWdZi dvdvdxdd3dd5ddAdd=dd?ddMddCddEddUddQddKddSddWdddd;di dddddddddddddGddIdd+ddsdd#dd'ddiddkddqdd_ddedi dgddmddoddaddcddzdd|dd~dddddddddddddddddddēddddddddd͜Zi Zi Zdd΄Z e       dfddЄZdф Zd҄ Zdӄ ZdԄ ZddՄZy)    )annotationsN)ImageImageOpsImageSequence)PngInfocomfy)IOComfyNodeABCInputTypeDictFileLocator)register_versionsComfyAPIWithVersion)supported_versions)ioComfyExtension)argsc                 @    t         j                  j                          y N)r   model_management)throw_exception_if_processing_interrupted     /home/serv3090/ComfyUI/nodes.pybefore_node_executionr   3   s    	DDFr   c                B    t         j                  j                  |        y r   )r   r   interrupt_current_processing)values    r   interrupt_processingr   6   s    	77>r   i @  c                  V    e Zd Zed	d       Zej                  fZdZdZ	dZ
dZg dZd Zy)
CLIPTextEncodec                \    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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.encodeL   s>    <   y  z  zt$11&9<<r   N)returnr   )__name__
__module____qualname__classmethodr,   r	   CONDITIONINGRETURN_TYPESOUTPUT_TOOLTIPSFUNCTIONCATEGORYDESCRIPTIONSEARCH_ALIASESr-   r   r   r   r    r    ;   sF    
 
 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_TYPESU   s    /AUghiir   rF   combiner.   )rI   zmerge conditioningzcombine promptszmerge promptsmix promptsz
add promptc                    ||z   fS r   r   )r4   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   S   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_TYPESd   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)r4   rY   rZ   r[   out	cond_frompooled_output_fromit1pooled_output_tot0twt_tons                 r   r\   zConditioningAverage.addWeightedn   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   a   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   )	r4   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   r.   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r4   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   )r4   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   )r4   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   )r4   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   )r4   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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)r4   r   r   r   imagess        r   r   zVAEDecode.decode7  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_TYPESB  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   _for_testingc                   ||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   )
r4   r   r   r   r   r   r   temporal_compressioncompressionr   s
             r   r   zVAEDecodeTiled.decodeO  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   A  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_TYPESc  s    IFGGr   r   r-   r   )r-   zencode imagezimage to latentc                .    |j                  |      }d|ifS )Nr   )r-   )r4   r   r   r   s       r   r-   zVAEEncode.encodel  s    JJv1  r   N)
r7   r8   r9   r:   r,   r<   r>   r?   rA   r-   r   r   r   r   r   b  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_TYPESq  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)r4   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   p  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-   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-   )r4   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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   )r4   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  sf   \\!_!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0 	L44\[hSWDY ZAJJqM	 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r4   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r/   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 r/   _05z_.latentoutputfilename	subfoldertyper   latent_tensorlatent_format_version_0)metadatauilatents)r/  get_save_image_pathr1  jsondumpsr   disable_metadatarn   ospathjoin
contiguousri   tensorr   utilssave_torch_file)r4   r   r5  r/   r9  full_output_folderrC  counterrD  prompt_inforH  r   fileresultsrA  s                  r   r<  zSaveLatent.save  s=   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   r3  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   )	r/  get_input_directoryrO  listdirrP  isfilerQ  endswithsortedr+   	input_dirffiless       r   r,   zLoadLatent.INPUT_TYPES  sx     446	JJy1zqRWW^^BGGLLQZ\]D^5_dedndnoxdyz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   rG  g!ޅ@r   rF  )r/  get_annotated_filepathsafetensorsri   	load_filefloat)r4   r   latent_path
multiplierr   s        r   rj  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	r/  ro  hashlibsha256openupdatereaddigesthex)r+   r   
image_pathr  rh  s        r   
IS_CHANGEDzLoadLatent.IS_CHANGED+  sd    !88@
NN*d# 	qHHQVVX	xxz~~	 	    A<<Bc                P    t        j                  |      sdj                  |      S y)NzInvalid latent file: {}Tr/  exists_annotated_filepathformat)r+   r   s     r   VALIDATE_INPUTSzLoadLatent.VALIDATE_INPUTS3  s$    55f=,33F;;r   N)r7   r8   r9   rA   r:   r,   r?   r<   r>   rj  r  r  r   r   r   r_  r_    s\    %}5N= =
 HLH      r   r_  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r/  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)r/  get_full_pathget_full_path_or_raiser   sdr  get_folder_paths)r4   r  r  config_path	ckpt_paths        r   r  z CheckpointLoader.load_checkpointG  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  :  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_TYPESM  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  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   r/  r  r   r  load_checkpoint_guess_configr  )r4   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  L  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)r/  r  rO  rP  existswalkrn   relpath)clspathssearch_pathrootsubdirri  s         r   r,   zDiffusersLoader.INPUT_TYPESf  s    '88E 	OKww~~k*+-77;D+Q O'D&%)U2RWW__T_%MNO	O \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  )	r/  r  rO  rP  r  rQ  r   diffusers_loadload_diffusers)r4   r  r  r  r  rP  s         r   r  zDiffusersLoader.load_checkpointu  s    '88E 	Kww~~k*ww||K<77>>$'!%J	 ##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  c  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  )r4   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)r4   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_lorar2  s    r   r3  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  zLoRAs are used to modify 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   |dk(  r	|dk(  r||fS t        j                  d|      }d }| j                  )| j                  d   |k(  r| j                  d   }nd | _        |*t        j                  j                  |d      }||f| _        t        j                  j                  |||||      \  }}	||	fS )Nr   r  r^   T)	safe_load)r/  r  r  r   rT  load_torch_filer  load_lora_for_models)
r4   r  r'   r  r  r  	lora_pathr  
model_lora	clip_loras
             r   r  zLoraLoader.load_lora  s    Q=A#54=  77K	'"i/''*#' <;;..yD.ID )40D % = =eT4Q_an o
II&&r   N)r7   r8   r9   rL   r3  r:   r,   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r    sI    ,  	
 	
 %LSOHH |KUN'r   r  c                  *    e Zd Ze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  load_lora_model_onlyc                4    | j                  |d ||d      d   fS )Nr   )r  )r4   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    s&    ! !
 L%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taef1c                   t        j                  d      }t        j                  d      }d}d}d}d}d}d}d}	d}
|D ]  }|j                  d      rd}|j                  d      rd}+|j                  d      rd}?|j                  d      rd}S|j                  d	      rd}g|j                  d
      rd}{|j                  d      rd}
|j                  d      rd}	| j                  D ]%  }|j                  |      s|j	                  |       '  |r|r|j	                  d       |r|r|j	                  d       |r|r|j	                  d       |
r|	r|j	                  d       |j	                  d       |S )Nr   
vae_approxFztaesd_decoder.Tztaesd_encoder.ztaesdxl_decoder.ztaesdxl_encoder.ztaesd3_decoder.ztaesd3_encoder.ztaef1_encoder.ztaef1_decoder.r  r  r  r  pixel_space)r/  r  
startswith
video_taesrn   )r+   vaesapprox_vaessdxl_taesd_encsdxl_taesd_decsd1_taesd_encsd1_taesd_decsd3_taesd_encsd3_taesd_decf1_taesd_encf1_taesd_decvtaes                r   vae_listzVAELoader.vae_list  sj   --e4"44\B 	'A||,- $./ $01!%01!%/0 $/0 $./#./#<< 'C||C(A'%	', ]KK nKK	"]KK!LKK M"r   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(P r   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?)r/  r  nextfilterr   rT  r  r  r  ri   rS  )r  r  r  encoderdecoderenckdecs   `       r   
load_taesdzVAELoader.load_taesd
  s   "44\BvPR]^_vPR]^_kk)),*M*Ml\c*de 	6A/21vB!((+,	6 kk)),*M*Ml\c*de 	6A/21vB!((+,	6 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                   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      \  }}t        j                  j                  ||	      }|j                          |fS )
Nr  rQ   pixel_space_vaer   r  r   T)return_metadata)r  rH  )ri   rS  
image_taesr  rO  rP  splitextr  r/  r  r   rT  r  r  r   throw_exception_if_invalid)r4   r  rH  r  vae_pathr   s         r   r  zVAELoader.load_vae1  s    }$B$)LL$5B !(*Bww)!,?'>>|XV'>>uhO ;;66xQU6VLBhhllb8l4&&(vr   N)r7   r8   r9   r  r   staticmethodr  r  r:   r,   r<   r>   r?   r  r   r   r   r  r    s[    WJ8J+ +Z  : > >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_TYPESC  s"    0<3Q3QR^3_2bcddr   CONTROL_NETload_controlnetr  )r)  control netcnzload controlnetzcontrolnet loaderc                    t        j                  d|      }t        j                  j	                  |      }|t        d      |fS )Nr)  zPERROR: controlnet file is invalid and does not contain a valid controlnet model.)r/  r  r   r)  r,  r1   )r4   r(  controlnet_pathr)  s       r   r,  z ControlNetLoader.load_controlnetM  sE    &==lL\]%%55oF
qrr}r   N)
r7   r8   r9   r:   r,   r<   r>   r?   rA   r,  r   r   r   r&  r&  B  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  r)  )r  r(  r  r*   s    r   r,   z DiffControlNetLoader.INPUT_TYPESU  s+    z3?3Q3QR^3_2bd e 	er   r*  r,  r  c                t    t        j                  d|      }t        j                  j	                  ||      }|fS )Nr)  )r/  r  r   r)  r,  )r4   r  r(  r0  r)  s        r   r,  z$DiffControlNetLoader.load_controlnet_  s5    &==lL\]%%55ouM
}r   N)	r7   r8   r9   r:   r,   r<   r>   r?   r,  r   r   r   r2  r2  T  s,    e e $L HHr   r2  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   r*  r   rP   rQ   rR   r   rS   rT   )r.   control_netimager   r   r*   s    r   r,   zControlNetApply.INPUT_TYPESf  s0    -?,=&1*1s3W[ei3j)k   	 r   rF   apply_controlnetT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   )
r4   r.   r8  r9  r   r   control_hintr   rx   c_nets
             r   r:  z ControlNetApply.apply_controlnets  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?   r:  r   r   r   r6  r6  e  s/        %L!HJ(Hr   r6  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   r*  r   rP   rQ   rR   r   rS   rT   r   )r!  r"  r8  r9  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"  r:  r;  )r)  zapply controlnetzuse controlnetr-  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^   r=  )r   extra_concatFr>  )r?  rm   rf   r@  rA  rn   )r4   r!  r"  r8  r9  r   r   r   r   rJ  rB  cnetsro   r.   r   r   r   	prev_cnetrC  rx   s                       r   r:  z(ControlNetApplyAdvanced.apply_controlnet  s!   q=h''}}R*%x0 	LA! aDIIKEE)T2	%!),E',,.<<\8VcepUqwz  JV<  WE11)<',E)$$)/4+,qT1I JJqM#	$ AAr   )r7   r8   r9   r:   r,   r<   r+  r>   r?   rA   r:  r   r   r   rE  rE    s>      3L+L!H(HXNqu  EG  r   rE  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 )	NrQ  dtyperR  Tfp8_optimizationsrS  rP  )model_options)ri   float8_e4m3fnfloat8_e5m2r/  r  r   r  load_diffusion_model)r4   rT  rU  rZ  	unet_pathr  s         r   rV  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?   rV  r   r   r   rN  rN    s*        LH!Hr   rN  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)	clip_namerE  rn  rU   rl  r   TrG  r  r*   s    r   r,   zCLIPLoader.INPUT_TYPES  sI    ,8,J,J?,[+^ (E  'H  ')U);j$=O(P  	 r   r  	load_clipr  z[Recipes]

stable_diffusion: clip-l
stable_cascade: clip-g
sd3: t5 xxl/ clip-g / clip-l
stable_audio: t5 base
mochi: t5 xxl
cosmos: old t5 xxl
lumina2: gemma 2 2B
wan: umt5 xxl
 hidream: llama-3.1 (Recommend) or t5
omnigen2: qwen vl 2.5 3Bc                   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 )Nrl  load_deviceoffload_devicerb  r  
ckpt_pathsr  	clip_typerZ  )getattrr   r  CLIPTypeupperSTABLE_DIFFUSIONri   rn  r/  r  rw  r  )r4   rv  rE  rn  r}  rZ  	clip_pathr'   s           r   rw  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)rc  rU   
r7   r8   r9   r:   r,   r<   r>   r?   r@   rw  r   r   r   r`  r`    s2        LH!H OK	r   r`  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 )
Nrb  )sdxlre  fluxhunyuan_videorm  rr  hunyuan_video_15
kandinsky5kandinsky5_imagerh  newbiero  )
clip_name1
clip_name2rE  rn  rU   rl  r   TrG  r  r*   s    r   r,   zDualCLIPLoader.INPUT_TYPES  sZ    -9-K-KO-\,_-9-K-KO-\,_ ({  '~ 
 ')U);j$=O(P	  	 r   r  rw  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 )Nrb  rl  ry  rz  r  r{  )r~  r   r  r  r  r  r/  r  ri   rn  rw  r  )
r4   r  r  rE  rn  r}  
clip_path1
clip_path2rZ  r'   s
             r   rw  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"   rv  clip_visionr  r*   s    r   r,   zCLIPVisionLoader.INPUT_TYPES
  r  r   r  rw  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.)r/  r  r   r  rj  r1   )r4   rv  r  r  s       r   rw  zCLIPVisionLoader.load_clip  sD     77yQ	'',,Y7noo~r   N)	r7   r8   r9   r:   r,   r<   r>   r?   rw  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  r9  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)r4   r  r9  r  
crop_imagerA  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  )r/  r  r   r  r  )r4   r  style_model_pathstyle_models       r   r  z!StyleModelLoader.load_style_model7  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_TYPES@  s;    -?,=3K*1s3W[ej3k)l0:K/H.K	   	 r   rF   apply_stylemodelz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^   rX  )get_condflattenr   rh   rm   ri   logTensorrf   rk   float16rX  booltorn  rn   rj   )r4   r.   r  r  r   r  condrx   c_outr   txtkeysr  mask_ref_sizen_refn_txtr   new_masks                     r   r  z StyleModelApply.apply_stylemodelM  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  =  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_TYPES}  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   )r4   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  |  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  )r/  r  r   r  r  )r4   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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_tokensr2   rm   rn   )r4   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   rn  rX  )r   downscale_ratio_spacial)ri   rk   r   r   intermediate_deviceintermediate_dtype)r4   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dd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  s6    .31UW5X-Y).AaPR0S(T  ! 	!r   r   	frombatchlatent/batchc                   |j                         }|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   rV   rh   r  repeatr  r  rg   )r4   r   r  r  r+   s_inmasksr   s           r   r   zLatentFromBatch.frombatch  su   LLNy!$**Q-!+[9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RaRAm t  '}5k+PVBVWAmt  Ss   7	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   )	r4   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]2xghivgw2xbc1F
3C2x3C2x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   rT  common_upscale)r4   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_TYPES<  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   rT  r  )r4   r   r  r"  r+   r   r   s          r   r  zLatentUpscaleBy.upscaleE  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  7  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_TYPESM  s    +_*a  ! 	!r   r   rotate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)r4   r   r(  r+   	rotate_bys        r   r)  zLatentRotate.rotateW  sq    LLN	t$I  'I  'I{{79#5!QP)tr   N)	r7   r8   r9   r:   r,   r<   r>   r?   r)  r   r   r   r&  r&  L  s*    ! ! 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)
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_TYPESg  s"    /CE[.\-^  ! 	!r   r   flipr*  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`   )r/  r   r   )rm   r  ri   r6  )r4   r   r5  r+   s       r   r6  zLatentFlip.flipq  sq    LLN!!#& ::gi&8sCAiL t ##C( ::gi&8sCAiLtr   N)
r7   r8   r9   rA   r:   r,   r<   r>   r?   r6  r   r   r   r3  r3  d  s2    %&N! ! LH!Hr   r3  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_TYPES}  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   )r4   r;  r<  r   r   composite_methodr=  samples_outr+   r   r   rev_masks               r   r>  zLatentComposite.composite  s<   !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^ 	d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	d 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?   r>  r   r   r   r9  r9  z  s/    GN! ! LHHr   r9  c                  @    e Zd ZddgZed        ZdZdZdZd
d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   c                   |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   rT  r  
blend_mode)r4   rH  rI  rJ  rN  rB  samples_blendeds          r   rK  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|       )NrD  zUnsupported blend mode: )
ValueError)r4   img1img2r  s       r   rN  zLatentBlend.blend_mode  s"    8K7v>??r   N)rD  )rJ  rr  rN  str)r7   r8   r9   rA   r:   r,   r<   r>   r?   rK  rN  r   r   r   rF  rF    s;    #%:;N
 
 LHH @r   rF  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  r*  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   )r4   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   rV  rV    s4    #\2N! ! LH!Hr   rV  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   )r4   r   r   r+   s       r   r`  zSetLatentNoiseMask.set_mask  s?    LLN,,Atzz"~tzz"~'NO,tr   N)	r7   r8   r9   r:   r,   r<   r>   r?   r`  r   r   r   r^  r^    s*    ! ! LHHr   r^  c                p   |d   }t         j                  j                  | ||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        ||d<   |fS )Nr   r  rl  )rX  layoutrn  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  rX  rc  prepare_noiselatent_previewprepare_callbackrT  PROGRESS_BAR_ENABLEDrm   pop)r  rk  stepscfgsampler_name	schedulerr!  r"  r   rd  re  rf  rg  rh  latent_imagenoise
batch_indsr	  ri  rj  r   ro   s                         r   common_ksamplerrz    sB   )$L<<99%vzzZsuyOz{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,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  rk  rs  rt  ru  rv  r!  r"  rw  rd  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.rl  samplingzXUses the provided model, positive and negative conditioning to denoise the latent image.)samplerrl  r  rd  diffusetxt2imgimg2imgc                ,    t        |||||||||	|

      S )N)rd  rz  )r4   r  rk  rs  rt  ru  rv  r!  r"  rw  rd  s              r   rl  zKSampler.sample6  s-    udE3iQY[ceq  |C  D  	Dr   NrQ   )r7   r8   r9   r:   r,   r<   r=   r>   r?   r@   rA   rl  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_seedrs  rt  ru  rv  r!  r"  rw  start_at_stepend_at_stepreturn_with_leftover_noiser  r*   s    r   r,   zKSamplerAdvanced.INPUT_TYPES:  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   rl  r  c                X    d}|dk(  rd}d}|dk(  rd}t        ||||||||	|
|||||      S )NTr  Fr  )rd  re  rf  rg  rh  r  )r4   r  r  r  rs  rt  ru  rv  r!  r"  rw  r  r  r  rd  rh  re  s                    r   rl  zKSamplerAdvanced.sampleR  so    !%1!&	! Muj%lIW_aikw  BI  Yf  s@  LW  l~    	r   Nr  )	r7   r8   r9   r:   r,   r<   r>   r?   rl  r   r   r   r  r  9  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 )NrA  r>  r   )r/  r0  r1  rE  prefix_appendcompress_levelr2  s    r   r3  zSaveImage.__init__\  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   r5  r6  r7  r8  r:  r   r*   s    r   r,   zSaveImage.INPUT_TYPESb  sE     #Y0E$FG$,)  Qq  /r  $s
 #_
 	
r   r   save_imagesTr9  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@   r/   z%batch_num%r?  r@  z_.png)pnginfor  rB  rI  r   )r  r/  rK  r1  rh   list	enumeraterl  numpyr   	fromarraynpr'   astypeuint8r   rN  r   add_textrL  rM  replacerT  r<  rO  rP  rQ  r  rn   rE  )r4   r   r5  r/   r9  rV  rC  rW  rD  rZ  batch_numberr9  rr   imgrH  r   filename_with_batch_numrY  s                     r   r  zSaveImage.save_imagesx  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 ,* K ))!TZZa8H-IJK '/&6&6}c,FW&X#-.a|5ADHHRWW\\"4d;X^b^q^qHrNN &		 
 qLG)	, 7,..r   r[  )r7   r8   r9   r3  r:   r,   r<   r>   r]  r?   rL   r@   rA   r  r   r   r   r  r  [  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     /n`a>Z0[/ns   !r   r^   )r/  get_temp_directoryr1  rE  rQ  rg   r  r  r2  s    r   r3  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   r6  r7  r8  r:  r   r*   s    r   r,   zPreviewImage.INPUT_TYPES  s     {-%-P 	r   N)r7   r8   r9   r3  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 )Nr9  r"   image_uploadT)	r/  ra  rO  rb  rP  rc  rQ  filter_files_content_typesre  rf  s       r   r,   zLoadImage.INPUT_TYPES  s     446	JJy1`qRWW^^BGGLLQZ\]D^5_``77yIve}~t.DEF 	 as   AB/Br9  r  )z
load imagez
open imagezimport imagezimage inputzupload imagez
read imagezimage loaderr   r   
load_imagec                ~   t        j                  |      }t        j                  t        j
                  |      }g }g }d\  }}t        j                  j                         }t        j                  |      D ]w  }	t        j                  t        j                  |	      }	|	j                  dk(  r|	j                  d       }	|	j                  d      }t!        |      dk(  r|j"                  d   }|j"                  d   }|j"                  d   |k7  s|j"                  d   |k7  rt%        j&                  |      j)                  t$        j*                        dz  }t-        j.                  |      d   }d	|	j1                         v r]t%        j&                  |	j3                  d	            j)                  t$        j*                        dz  }
d
t-        j.                  |
      z
  }
n|	j                  dk(  rzd|	j4                  v rlt%        j&                  |	j                  d      j3                  d	            j)                  t$        j*                        dz  }
d
t-        j.                  |
      z
  }
n&t-        j6                  dt,        j*                  d      }
|j9                  |j;                  |             |j9                  |
j=                  d      j;                  |             |j>                  dk(  sx n t!        |      dkD  r2t-        j@                  |d      }t-        j@                  |d      }||fS |d   }|d   }||fS )N)NNIc                    | dz  S Ngp?r   rr   s    r   r  z&LoadImage.load_image.<locals>.<lambda>  
    a7m r   RGBr   r^   r  r   ArQ   PtransparencyRGBAr   r   rl  rX  rn  r  MPOra   )!r/  ro  r   pillowr   r{  r   r   r  r   Iteratorr   exif_transposer  pointconvertrc   r  r  arrayr  float32ri   
from_numpygetbands
getchannelinfork   rn   r  r   r  rj   )r4   r9  r  r  output_imagesoutput_maskswhrX  rr   r   output_imageoutput_masks                r   r  zLoadImage.load_image  s   !88?
!!%**j91&&99;'', 	A##H$;$;Q?Avv}GG34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3>QVV#;xx		& 1 < <S ABII"**UX]]E,,T22{{7%--N  !67q 1 4 45 4 ABzzU"9	< }! 99]:L))La8K
 k** )+L&q/K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rv  rx  )r+   r9  r  r  rh  s        r   r  zLoadImage.IS_CHANGED  d    !88?
NN*d# 	qHHQVVX	xxz~~	 	r  c                P    t        j                  |      sdj                  |      S yNzInvalid image file: {}Tr  r+   r9  s     r   r  zLoadImage.VALIDATE_INPUTS  $    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0+d      r   r  c                  b    e Zd ZdZg dZg dZed        ZdZdZ	dZ
d Zed	        Zed
        Zy)LoadImageMaskImage Tools)zimport maskz
alpha maskzchannel mask)alpharedgreenbluec           	     2   t        j                         }t        j                  |      D cg c]B  }t        j                  j                  t        j                  j                  ||            sA|D }}dt        |      ddif| j                  fdiS c c}w )Nr"   r  T)r9  channel)	r/  ra  rO  rb  rP  rc  rQ  re  _color_channelsrf  s       r   r,   zLoadImageMask.INPUT_TYPES  s     446	JJy1`qRWW^^BGGLLQZ\]D^5_``%e}~t.DE!"!2!2 59 	 as   AB/Br   r   r  c                   t        j                  |      }t        j                  t        j
                  |      }t        j                  t        j                  |      }|j                         dk7  r2|j                  dk(  r|j                  d       }|j                  d      }d }|d   j                         }||j                         v rdt        j                  |j                  |            j!                  t        j"                        dz  }t%        j&                  |      }|dk(  r,d|z
  }n&t%        j(                  d	t$        j"                  d
      }|j+                  d      fS )N)RGBr  r  c                    | dz  S r  r   r  s    r   r  z*LoadImageMask.load_image.<locals>.<lambda>  r  r   r  r   r  r  rQ   r  rl  r  )r/  ro  r   r  r   r{  r   r  r  r  r  r  r  r  r  r  r  r  ri   r  rk   r   )r4   r9  r  r  rr   r   r   s          r   r  zLoadImageMask.load_image  s   !88?


J7 7 7;::<//vv}GG34		&!AAJ

88ALLO,33BJJ?%GD##D)DCxDy;;wemmEJDq!##r   c                   t        j                  |      }t        j                         }t	        |d      5 }|j                  |j                                d d d        |j                         j                         S # 1 sw Y   'xY wrv  rx  )r+   r9  r  r  r  rh  s         r   r  zLoadImageMask.IS_CHANGED  r  r  c                P    t        j                  |      sdj                  |      S yr  r  r  s     r   r  zLoadImageMask.VALIDATE_INPUTS"  r  r   N)r7   r8   r9   rL   rA   r  r:   r,   r?   r<   r>   r  r  r  r   r   r   r  r    sb    '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"   r9  COMBOTrA  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   )r9  r  r   r   r  r  r*   s    r   r,   zImageScale.INPUT_TYPESF  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`   )r?  rW   r  rh   r   rT  r  )r4   r9  r  r   r   r  r+   r   s           r   r  zImageScale.upscaleS  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  B  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   )r9  r  r"  r#  r*   s    r   r,   zImageScaleBy.INPUT_TYPESf  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  )r?  r  rh   r   rT  r  )r4   r9  r  r"  r   r   r   r+   s           r   r  zImageScaleBy.upscaleo  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  b  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"   r9  r   r   r*   s    r   r,   zImageInvert.INPUT_TYPES{  s    gz233r   r   invertr9  c                    d|z
  }|fS )NrQ   r   )r4   r9  r+   s      r   r  zImageInvert.invert  s    %Ktr   N)r7   r8   r9   rA   rL   r:   r,   r<   r>   r?   r  r   r   r   r  r  w  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_TYPES  s    
jIJJr   r   batchr9  Tc                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   rT  r  r?  rj   )r4   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    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   rm  )r4   rn  s     r   r3  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  r9  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   )
r4   r   r   r  r)  rX  rn  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)rl  )r^   r   )
r7   r8   r9   r3  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   )r9  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_imager9  c                   |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   )r4   r9  r4  r5  r6  r7  r8  d1d2d3d4	new_imager   r   rr   jdtdbdldrr   r  s                         r   r9  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?   r9  r   r   r   r1  r1    s4    %~6N

 

 %LHH).r   r1  )r   r   r  r  r  r  r  r  r_  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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 (OLD)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
Save ImagezPreview Imagez
Load ImagezLoad Image (as Mask)zLoad Image (from Outputs)zUpscale ImagezUpscale Image ByzInvert ImagezPad Image for OutpaintingzBatch Images	ImageCropz
Image CropImageStitchzImage StitchzImage Blendz
Image BlurzImage QuantizezImage SharpenzScale Image to Total PixelszGet Image SizezVAE Decode (Tiled)zVAE Encode (Tiled))
ImageBlend	ImageBlurImageQuantizeImageSharpenImageScaleToTotalPixelsGetImageSizer   r   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   )rO  rP  basenamerc  r!  )module_path	base_paths     r   get_module_namerQ    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 zY module for custom nodes due to the lack of NODE_CLASS_MAPPINGS or NODES_LIST (need one).zCannot import z module for custom nodes: )5rQ  rO  rP  rc  r!  isdirr  rd   debugr  	importlibutilspec_from_file_locationsplitrQ  module_from_specsysmodulesloaderexec_moduleabspathLOADED_MODULE_DIRScomfy_configrV  extract_node_configuration
tool_comfywebprojectr  EXTENSION_WEB_DIRSr  	Exceptionre   hasattrr~  rX  itemsRELATIVE_PYTHON_MODULErY  r|  callableinspectiscoroutinefunction
isinstancer   on_loadget_node_listr  
GET_SCHEMAnode_iddisplay_name	traceback
format_exc)rO  ignoremodule_parentmodule_namespsys_module_namemodule_spec
module_dirmodulerV  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 rhv%08'-6=nn]TcdoTp6qH3r 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  ) YH%002F~~V3>F+FNN;:A..XghsXt:u7**6EKEXEX26>>BY 
 OOeK=  1J  K  L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
    rR  __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{}: {})setrX  r  r/  r  rO  rb  rP  realpathremoverQ  rc  r!  rd  r   disable_all_custom_nodeswhitelist_custom_nodesrd   r  enable_managercomfyui_managershould_be_disabledtimeperf_counterr  rn   rc   re  r  )base_node_names
node_pathsnode_import_timescustom_node_pathpossible_modulespossible_modulerO  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 !78)* 	UAt!#!3LL077!naPQdST	U 	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)cz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_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_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_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_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_rtdetr.pyr  N)rO  rP  rQ  dirnamer  __file__r  rn   )
extras_dirextras_filesimport_failed	node_files       r   init_builtin_extra_nodesr  .	  s      bggoobgg.>.>x.HI>ZJdLL 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  )rO  rP  rQ  r  r  r  re  globr  rn   rN  )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  	  s?      &	  	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   r  rc   re   r  r   windows_standalone_build)init_custom_nodesinit_api_nodesr  import_failed_apinodes        r   init_extra_nodesr  	  sq    

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   FNNF)rO  rT  r6   rT  )rO  rT  r6   r  r  )
__future__r   ri   rO  rb  rL  r  ry  rs  r{  r  r  r  rd   PILr   r   r   PIL.PngImagePluginr   r  r  safetensors.torchrp  rP  insertrQ  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   comfy.clip_visioncomfy.model_managementcomfy.cli_argsr   r]  r/  ro  r   r  r  r   r   r   r    rC   rN   rz   r   r   r   r   r   r   r   r   r   r   r   r  r-  r_  r  r  r  r  r  r  r  r  r&  r2  r6  rE  rN  r`  r  r  r  r  r  r  r  r  r  r  r  r  r  r&  r3  r9  rF  rV  r^  rz  r|  r  r  r  r  r  r   r  r  r  r  r&  r1  rX  rY  rm  rg  rQ  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:   +' +'ZO* Og gR $ " >0  0 f 2 6 < " &  = =~ (  >E E.   D 8" "H * 0 ,* *X)@ )@V! !F  0D D:   D7/ 7/r9  O Ob2 2j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B
B -B
 BB /B B .B ;B +B /B :B *B *B (B" ,#B$ *%B& 1'B( -)B* 3+B, 4-B. 1/B0 41B2 $%N3B4 45B6 /7B8 19B< 8=B> 1?B@ ABB CBD OEBF -GBH -IBJ ,KBL %MBN *OBP )QBR >SBT +UBV .WBZ [B\ O]B^ _B` +aBb 2cBd /eBf &gBh >iBj 6kBl .mBn oBp >qBr  %#<$**CB H    , 58E ^@-^vr	$r   