o
    iՠ                    @  s.  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jd ejejej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l9Z9e5j:rd dl;Z;dd Z<dddZ=dZ>G dd de'Z?G dd dZ@G dd dZAG dd dZBG dd dZCG dd dZDG dd dZEG dd  d ZFG d!d" d"ZGG d#d$ d$ZHG d%d& d&ZIG d'd( d(ZJG d)d* d*ZKG d+d, d,ZLG d-d. d.ZMG d/d0 d0ZNG d1d2 d2ZOG d3d4 d4ZPG d5d6 d6ZQG d7d8 d8ZRG d9d: d:ZSG d;d< d<ZTG d=d> d>ZUG d?d@ d@ZVG dAdB dBeVZWG dCdD dDZXG dEdF dFZYG dGdH dHZZG dIdJ dJZ[G dKdL dLZ\G dMdN dNZ]G dOdP dPZ^G dQdR dRZ_G dSdT dTZ`G dUdV dVZaG dWdX dXZbG dYdZ dZZcG d[d\ d\ZdG d]d^ d^ZeG d_d` d`ZfG dadb dbZgG dcdd ddZhG dedf dfZiG dgdh dhZjG didj djZkG dkdl dlZlG dmdn dnZmG dodp dpZnG dqdr drZoG dsdt dtZpG dudv dvZqddydzZrG d{d| d|ZsG d}d~ d~ZtG dd dZuG dd deuZvG dd dZwG dd dZxG dd dewZyG dd dZzG dd dZ{G dd dZ|G dd dZ}G dd dZ~G dd dZi d|esd8eRde?d>eUd&eId*eKd.eMdDeXdbegdhejdjekddehdfeideudevdewdexi deydezde{de|de}dede~deAde@deBdeCdeDdeEd eFd~etdveqdpeni dreodleldnemdtepd@eVdPe^dNe]dRe_dVeadZecd\eddJe[dLe\dFeYdHeZdXebdTe`eJeLeTeeefeNeQeSePeOeGeHeWdZi d|d|d~dd6dd8ddDdd@ddBddPddFddHddXddTddNddVddZdddd>di ddddddddddd ddJddLdd.ddvdd&dd*ddlddnddtddbddhdi djddpddrdddddfdddddddddddddddÓddēddœddƓddȓddʓdddddddddӜZi Zi Zddd؄Ze dfddd܄Zddބ Zdd Zdd Zdd ZdddZdS )    )annotationsN)ImageImageOpsImageSequence)PngInfocomfy)IOComfyNodeABCInputTypeDictFileLocator)register_versionsComfyAPIWithVersion)supported_versions)ioComfyExtension)argsc                   C  s   t j  d S N)r   model_management)throw_exception_if_processing_interrupted r   r   #/mnt/c/Users/fbmor/ComfyUI/nodes.pybefore_node_execution3      r   Tc                 C  s   t j|  d S r   )r   r   interrupt_current_processing)valuer   r   r   interrupt_processing6   s   r   i @  c                   @  sB   e Zd ZedddZejfZdZdZ	dZ
dZg d	Zd
d ZdS )CLIPTextEncodereturnr
   c                 C  s&   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sr   r   r   INPUT_TYPES<   s
   zCLIPTextEncode.INPUT_TYPES)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                 C  s&   |d u rt d||}||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"   tokensr   r   r   r)   L   s   
zCLIPTextEncode.encodeN)r   r
   )__name__
__module____qualname__classmethodr(   r   CONDITIONINGRETURN_TYPESOUTPUT_TOOLTIPSFUNCTIONCATEGORYDESCRIPTIONSEARCH_ALIASESr)   r   r   r   r   r   ;   s    r   c                   @  s8   e Zd ZdZedd ZdZdZdZg dZ	dd	 Z
d
S )ConditioningCombineImage Generationc                 C     ddddiS )Nr   r5   )conditioning_1conditioning_2r   r&   r   r   r   r(   U      zConditioningCombine.INPUT_TYPESr?   combiner*   )rC   zmerge conditioningzcombine promptszmerge promptsmix promptsz
add promptc                 C  s
   || fS r   r   )r/   r@   rA   r   r   r   rC   ^      
zConditioningCombine.combineN)r1   r2   r3   ESSENTIALS_CATEGORYr4   r(   r6   r8   r9   r;   rC   r   r   r   r   r<   S   s    
r<   c                   @  4   e Zd Zg dZedd ZdZdZdZdd Z	d	S )
ConditioningAverage)zblend promptszinterpolate conditioningrD   zstyle fusionzweighted blendc              	   C  s   dddddddddfdiS )	Nr   r?   FLOAT      ?        {Gz?defaultminmaxstep)conditioning_toconditioning_fromconditioning_to_strengthr   r&   r   r   r   r(   d   s   zConditioningAverage.INPUT_TYPESr?   addWeightedr*   c              	   C  sX  g }t |dkrtd |d d }|d d dd }tt |D ]}|| d }|| d d|}	|d d d |jd f }
|
jd |jd k rhtj|
gtd|jd |
jd  |jd fg dd}
t	||t	|
d|  }|| d 
 }|d ur|	d urt	|	|t	|d|  |d< n|d ur||d< ||g}|| q#|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   dimrJ   )lenloggingwarninggetrangeshapetorchcatzerosmulcopyappend)r/   rR   rS   rT   out	cond_frompooled_output_fromit1pooled_output_tot0twt_tonr   r   r   rU   n   s(   
8"zConditioningAverage.addWeightedN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   rU   r   r   r   r   rH   a       
rH   c                   @  ,   e Zd Zedd ZdZdZdZdd ZdS )	ConditioningConcatc                 C  r>   )Nr   r?   )rR   rS   r   r&   r   r   r   r(      s   zConditioningConcat.INPUT_TYPESr?   concatr*   c           	      C  sx   g }t |dkrtd |d d }tt |D ]}|| d }t||fd}||| d  g}|| q|fS )NrV   zWarning: ConditioningConcat conditioning_from contains more than 1 cond, only the first one will actually be applied to conditioning_to.r   )r[   r\   r]   r_   ra   rb   re   rf   )	r/   rR   rS   rg   rh   rj   rk   rn   rp   r   r   r   rt      s   
zConditioningConcat.concatN)	r1   r2   r3   r4   r(   r6   r8   r9   rt   r   r   r   r   rs          
rs   c                   @  rG   )
ConditioningSetArea)zregional promptzarea promptzspatial conditioningzlocalized promptc                 C  s\   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   r?   INT@      rM   r   rI   rJ   rK         $@rL   r*   widthheightxystrengthMAX_RESOLUTIONr&   r   r   r   r(         zConditioningSetArea.INPUT_TYPESr?   rf   r*   c                 C  s2   t ||d |d |d |d f|dd}|fS )Nry   Farear   set_area_to_boundsnode_helpersconditioning_set_valuesr/   r*   r|   r}   r~   r   r   cr   r   r   rf      s
    zConditioningSetArea.appendN
r1   r2   r3   r;   r4   r(   r6   r8   r9   rf   r   r   r   r   rv      s    
rv   c                   @  rr   )	ConditioningSetAreaPercentagec                 C  s\   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   r?   rI   rJ   r   rL   rM   rK   rz   r{   r   r&   r   r   r   r(      r   z)ConditioningSetAreaPercentage.INPUT_TYPESr?   rf   r*   c                 C  s$   t |d||||f|dd}|fS )N
percentageFr   r   r   r   r   r   rf      s
   z$ConditioningSetAreaPercentage.appendN	r1   r2   r3   r4   r(   r6   r8   r9   rf   r   r   r   r   r      s    
r   c                   @  rr   )	ConditioningSetAreaStrengthc                 C  s   ddddddddfd	iS )
Nr   r?   rI   rJ   rK   rz   rL   rM   )r*   r   r   r&   r   r   r   r(      s   z'ConditioningSetAreaStrength.INPUT_TYPESr?   rf   r*   c                 C  s   t |d|i}|fS )Nr   r   )r/   r*   r   r   r   r   r   rf      s   z"ConditioningSetAreaStrength.appendNr   r   r   r   r   r          
r   c                   @  rG   )
ConditioningSetMask)zmasked promptzregional inpaint conditioningzmask conditioningc              	   C  s&   ddddddddd	fd
dgfdiS )Nr   r?   MASKrI   rJ   rK   rz   rL   rM   rN   zmask bounds)r*   maskr   set_cond_arear   r&   r   r   r   r(      s
   zConditioningSetMask.INPUT_TYPESr?   rf   r*   c                 C  sB   d}|dkrd}t |jdk r|d}t||||d}|fS )NFrN   T   r   )r   r   mask_strength)r[   r`   	unsqueezer   r   )r/   r*   r   r   r   r   r   r   r   r   rf      s   
zConditioningSetMask.appendNr   r   r   r   r   r      s    
r   c                   @  4   e Zd ZddgZedd ZdZdZdZdd	 Z	d
S )ConditioningZeroOutznull conditioningzclear conditioningc                 C     dddiiS )Nr   r*   r?   r   r&   r   r   r   r(         zConditioningZeroOut.INPUT_TYPESr?   zero_outadvanced/conditioningc                 C  s   g }|D ]8}|d   }|dd }|d urt||d< |dd }|d ur.t||d< t|d |g}|| q|fS )NrV   rW   conditioning_lyricsr   )re   r^   ra   
zeros_likerf   )r/   r*   r   tdrW   r   rp   r   r   r   r     s   zConditioningZeroOut.zero_outN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   r   r   r   r   r   r      s    
r   c                   @  rr   )	ConditioningSetTimestepRangec              	   C  s,   ddddddddfddddddfdiS )	Nr   r?   rI   rK   rJ   MbP?rM   )r*   startendr   r&   r   r   r   r(     s   z(ConditioningSetTimestepRange.INPUT_TYPESr?   	set_ranger   c                 C  s   t |||d}|fS )N)start_percentend_percentr   )r/   r*   r   r   r   r   r   r   r   !  s   z&ConditioningSetTimestepRange.set_rangeN)	r1   r2   r3   r4   r(   r6   r8   r9   r   r   r   r   r   r     ru   r   c                   @  <   e Zd Zedd ZdZdZdZdZdZ	g dZ
d	d
 ZdS )	VAEDecodec                 C  s   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&   r   r   r   r(   '  s
   

zVAEDecode.INPUT_TYPESIMAGE)zThe decoded image.decodelatentz3Decodes latent images back into pixel space images.)r   zdecode latentzlatent to imagezrender latentc                 C  sZ   |d }|j r| d }||}t|jdkr*|d|jd |jd |jd }|fS )Nr   r      )	is_nestedunbindr   r[   r`   reshape)r/   r   r   r   imagesr   r   r   r   7  s   
"zVAEDecode.decodeN)r1   r2   r3   r4   r(   r6   r7   r8   r9   r:   r;   r   r   r   r   r   r   &  s    
r   c                   @  .   e Zd Zedd ZdZdZdZddd	Zd
S )VAEDecodeTiledc                 C  sZ   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   rw      rx          TrN   rO   rP   rQ   advancedr   ry      z?Only used for video VAEs: Amount of frames to decode at a time.rN   rO   rP   rQ   r!   r   6Only used for video VAEs: Amount of frames to overlap.)r   r   	tile_sizeoverlaptemporal_sizetemporal_overlapr   r&   r   r   r   r(   B     zVAEDecodeTiled.INPUT_TYPESr   r   _for_testingrx   ry   c           
      C  s   ||d k r
|d }||d k r|d }|  }|d ur0td|| }tdt|d || }nd }d }| }|j|d || || || ||d}	t|	jdkrc|	d|	jd |	jd	 |	jd }	|	fS )
Nr   rX   rV   r   tile_xtile_yr   tile_t	overlap_tr   r   r   r   )temporal_compression_decoderP   rO   spacial_compression_decodedecode_tiledr[   r`   r   )
r/   r   r   r   r   r   r   temporal_compressioncompressionr   r   r   r   r   O  s   &"zVAEDecodeTiled.decodeN)rx   rx   ry   )	r1   r2   r3   r4   r(   r6   r8   r9   r   r   r   r   r   r   A      
r   c                   @  4   e Zd Zedd ZdZdZdZg dZdd Z	d	S )
	VAEEncodec                 C     ddddiS )Nr   r   r   )pixelsr   r   r&   r   r   r   r(   c  rB   zVAEEncode.INPUT_TYPESr   r)   r   )r)   zencode imagezimage to latentc                 C  s   | |}d|ifS )Nr   )r)   )r/   r   r   r   r   r   r   r)   l  s   

zVAEEncode.encodeN)
r1   r2   r3   r4   r(   r6   r8   r9   r;   r)   r   r   r   r   r   b  s    
r   c                   @  r   )VAEEncodeTiledc                 C  sZ   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   rw   r   rx   r   Tr   r   r   ry   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&   r   r   r   r(   q  r   zVAEEncodeTiled.INPUT_TYPESr   r)   r   rx   ry   c                 C  s    |j ||||||d}d|ifS )Nr   r   )encode_tiled)r/   r   r   r   r   r   r   r   r   r   r   r)   ~  s   
zVAEEncodeTiled.encodeN)rx   ry   	r1   r2   r3   r4   r(   r6   r8   r9   r)   r   r   r   r   r   p  r   r   c                   @  .   e Zd Zedd ZdZdZdZd
ddZd	S )VAEEncodeForInpaintc              
   C      ddddddddd	d
fdiS )Nr   r   r   r   rw      r   rx   rV   rM   )r   r   r   grow_mask_byr   r&   r   r   r   r(     s    zVAEEncodeForInpaint.INPUT_TYPESr   r)   latent/inpaintr   c                 C  s  |  }|jd | | }|jd | | }tjjj|dd|jd |jd f|jd |jd fdd}| }|jd |ksG|jd |kr|jd | d }|jd | d }	|d d ||| |	||	 d d f }|d d d d ||| |	||	 f }|dkr|}
n#tdd||f}t	
|d d }ttjjj| ||ddd}
d	|  d}td
D ]8}|d d d d d d |f  d8  < |d d d d d d |f  |9  < |d d d d d d |f  d7  < q||}||
d d d d d |d |f  dfS )NrV   rX   r   r   bilinearsizemoder   )paddingrJ   r         ?)r   
noise_mask)spacial_compression_encoder`   ra   nn
functionalinterpolater   cloneonesmathceilclampconv2droundsqueezer_   r)   )r/   r   r   r   r   downscale_ratior~   r   x_offsety_offsetmask_erosionkernel_tensorr   mrj   r   r   r   r   r)     s,   >(("$$&
,zVAEEncodeForInpaint.encodeN)r   r   r   r   r   r   r     s    
r   c                   @  2   e Zd Zedd ZdZdZdZdZddd	Z	d
S )InpaintModelConditioningc              
   C  s    dddddddddd	fd
iS )Nr   r?   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.rN   r!   )positivenegativer   r   r   r   r   r&   r   r   r   r(     s   z$InpaintModelConditioning.INPUT_TYPES)r5   r5   r   )r  r  r   r)   zconditioning/inpaintTc                 C  s  |j d d d }|j d d d }tjjj|dd|j d |j d f|j d |j d fdd}|}	|	 }|j d |ksE|j d |kr|j d d d }
|j d d d }|d d |
||
 ||| d d f }|d d d d |
||
 ||| f }d|  d}t	d	D ]8}|d d d d d d |f  d
8  < |d d d d d d |f  |9  < |d d d d d d |f  d
7  < q|
|}|
|	}i }||d< |r||d< g }||fD ]}t|||d}|| q|d |d |fS )NrV   ry   rX   r   r   r   r   rJ   r   r   r   r   )concat_latent_imageconcat_maskr   )r`   ra   r   r   r   r   r   r   r   r_   r)   r   r   rf   )r/   r  r  r   r   r   r   r~   r   orig_pixelsr   r   r   rj   concat_latentorig_latent
out_latentrg   r*   r   r   r   r   r)     s8   >(($$&

zInpaintModelConditioning.encodeNT)
r1   r2   r3   r4   r(   r6   RETURN_NAMESr8   r9   r)   r   r   r   r   r    s    
	r  c                   @  s@   e Zd ZdgZdd Zedd ZdZdZdZ	d	Z
dddZdS )
SaveLatentzexport latentc                 C  s   t  | _d S r   )folder_pathsget_output_directory
output_dirr/   r   r   r   __init__  r   zSaveLatent.__init__c                 C  s   ddddifddddd	S )
Nr   r$   rN   zlatents/ComfyUI)r   filename_prefixPROMPTEXTRA_PNGINFOr+   extra_pnginfor   hiddenr   r&   r   r   r   r(     s
   
zSaveLatent.INPUT_TYPESr   saveTr   ComfyUINc                 C  s   t || j\}}}}}d}	|d urt|}	d }
tjs2d|	i}
|d ur2|D ]}t|| |
|< q&| d|dd}g }|||dd tj	
||}i }|d  |d	< tg |d
< tjj|||
d 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_pathr  jsondumpsr   disable_metadatarf   ospathjoin
contiguousra   tensorr   utilssave_torch_file)r/   r   r  r+   r  full_output_folderr"  counterr#  prompt_infor'  r~   fileresultsr   r   r   r   r    s.   
zSaveLatent.saver  NN)r1   r2   r3   r;   r  r4   r(   r6   r8   OUTPUT_NODEr9   r  r   r   r   r   r    s    
r  c                   @  sL   e Zd ZddgZedd ZdZdZdZdd	 Z	ed
d Z
edd ZdS )
LoadLatentzimport latentzopen latentc                   s2   t    fddt D }ddt|giiS )Nc                   s0   g | ]}t jt j |r|d r|qS )z.latent)r.  r/  isfiler0  endswith.0f	input_dirr   r   
<listcomp>  s   0 z*LoadLatent.INPUT_TYPES.<locals>.<listcomp>r   r   )r  get_input_directoryr.  listdirsortedr'   filesr   rB  r   r(     s   zLoadLatent.INPUT_TYPESr   r   loadc                 C  sD   t |}tjj|dd}d}d|vrd}d|d  | i}|fS )NcpudevicerJ   r&  g!ޅ@r   r%  )r  get_annotated_filepathsafetensorsra   	load_filefloat)r/   r   latent_path
multiplierr   r   r   r   rJ  "  s   
zLoadLatent.loadc                 C  V   t |}t }t|d}||  W d    n1 s w   Y  |  S Nrb	r  rN  hashlibsha256openupdatereaddigesthex)r'   r   
image_pathr   rA  r   r   r   
IS_CHANGED+     
zLoadLatent.IS_CHANGEDc                 C     t |s
d|S dS )NzInvalid latent file: {}Tr  exists_annotated_filepathformat)r'   r   r   r   r   VALIDATE_INPUTS3  s   

zLoadLatent.VALIDATE_INPUTSN)r1   r2   r3   r;   r4   r(   r9   r6   r8   rJ  r`  rf  r   r   r   r   r<    s    
	
r<  c                   @  s8   e Zd ZddgZedd ZdZdZdZdZ	d	d
 Z
dS )CheckpointLoader
load modelmodel loaderc                 C  s   dt dft dfdiS )Nr   configscheckpoints)config_name	ckpt_namer  get_filename_listr&   r   r   r   r(   =  s   
zCheckpointLoader.INPUT_TYPESMODELr%   r   load_checkpointadvanced/loadersTc                 C  s4   t d|}t d|}tjj||ddt ddS )Nrj  rk  T
embeddings
output_vaeoutput_clipembedding_directory)r  get_full_pathget_full_path_or_raiser   sdrr  get_folder_paths)r/   rl  rm  config_path	ckpt_pathr   r   r   rr  G  s   z CheckpointLoader.load_checkpointN)r1   r2   r3   r;   r4   r(   r6   r8   r9   
DEPRECATEDrr  r   r   r   r   rg  :      
rg  c                   @  r   )CheckpointLoaderSimplec                 C  s   ddt dddifiiS )Nr   rm  rk  r!   z+The name of the checkpoint (model) to load.rn  r&   r   r   r   r(   M  s   z"CheckpointLoaderSimple.INPUT_TYPESrp  )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.rr  loaderszQLoads a diffusion model checkpoint, diffusion models are used to denoise latents.)rh  
checkpointri  zload checkpointckptmodelc                 C  s2   t d|}tjj|ddt dd}|d d S )Nrk  Trt  ru  r   r  rz  r   r{  load_checkpoint_guess_configr|  )r/   rm  r~  rg   r   r   r   rr  ^  s   z&CheckpointLoaderSimple.load_checkpointN)r1   r2   r3   r4   r(   r6   r7   r8   r9   r:   r;   rr  r   r   r   r   r  L  s    
r  c                   @  s4   e Zd ZdgZedd ZdZdZdZddd	Z	d
S )DiffusersLoaderzload diffusers modelc                 C  sf   g }t dD ]$}tj|r+tj|ddD ]\}}}d|v r*|tjj||d qqdd|fiiS )N	diffusersT)followlinkszmodel_index.json)r   r   
model_path)r  r|  r.  r/  existswalkrf   relpath)clspathssearch_pathrootsubdirrI  r   r   r   r(   f  s   zDiffusersLoader.INPUT_TYPESrp  rr  zadvanced/loaders/deprecatedTc                 C  sX   t dD ]}tj|rtj||}tj|r|} nqtjj|||t ddS )Nr  rt  ru  )	r  r|  r.  r/  r  r0  r   diffusers_loadload_diffusers)r/   r  rv  rw  r  r/  r   r   r   rr  u  s   zDiffusersLoader.load_checkpointNTT)
r1   r2   r3   r;   r4   r(   r6   r8   r9   rr  r   r   r   r   r  c  s    
	r  c                   @  r   )unCLIPCheckpointLoaderc                 C     ddt dfiiS )Nr   rm  rk  rn  r&   r   r   r   r(        z"unCLIPCheckpointLoader.INPUT_TYPES)rq  r%   r   CLIP_VISIONrr  r  Tc                 C  s,   t d|}tjj|dddt dd}|S )Nrk  Trt  )rv  rw  output_clipvisionrx  r  )r/   rm  rv  rw  r~  rg   r   r   r   rr    s   z&unCLIPCheckpointLoader.load_checkpointNr  )	r1   r2   r3   r4   r(   r6   r8   r9   rr  r   r   r   r   r    s    
r  c                   @  rr   )	CLIPSetLastLayerc              	   C  s   dddddddddfd	iS )
Nr   r%   rw   r   irV   Tr   )r#   stop_at_clip_layerr   r&   r   r   r   r(     s   zCLIPSetLastLayer.INPUT_TYPESr  set_last_layerr*   c                 C  s   |  }|| |fS r   )r   
clip_layer)r/   r#   r  r   r   r   r    s   
zCLIPSetLastLayer.set_last_layerN)	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r    r   r  c                   @  sH   e Zd ZdZdd Zedd ZdZdZdZ	d	Z
d
Zg dZdd ZdS )
LoraLoaderr=   c                 C  s
   d | _ d S r   )loaded_lorar  r   r   r   r    rE   zLoraLoader.__init__c                 C  sR   ddddifdddift 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   rq  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.rI   rJ         Y      Y@rL   zGHow strongly to modify the diffusion model. This value can be negative.rN   rO   rP   rQ   r!   zBHow strongly to modify the CLIP model. This value can be negative.)r  r#   	lora_namestrength_modelstrength_cliprn  r&   r   r   r   r(     s   

zLoraLoader.INPUT_TYPES)rq  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           
      C  s   |dkr|dkr||fS t d|}d }| jd ur)| jd |kr&| jd }nd | _|d u r:tjj|dd}||f| _tj|||||\}}	||	fS )Nr   r  rV   T)	safe_load)r  rz  r  r   r3  load_torch_filer{  load_lora_for_models)
r/   r  r#   r  r  r  	lora_pathr  
model_lora	clip_lorar   r   r   r    s   

zLoraLoader.load_loraN)r1   r2   r3   rF   r  r4   r(   r6   r7   r8   r9   r:   r;   r  r   r   r   r   r    s    
r  c                   @  s(   e Zd Zedd ZdZdZdd ZdS )LoraLoaderModelOnlyc              	   C  s&   ddt dfdddddd	fd
iS )Nr   rq  r  rI   rJ   r  r  rL   rM   )r  r  r  rn  r&   r   r   r   r(     s   
zLoraLoaderModelOnly.INPUT_TYPESr  load_lora_model_onlyc                 C  s   |  |d ||dd fS )Nr   )r  )r/   r  r  r  r   r   r   r    s   z(LoraLoaderModelOnly.load_lora_model_onlyN)r1   r2   r3   r4   r(   r6   r8   r  r   r   r   r   r    s    
r  c                   @  sT   e Zd Zg dZg dZedd Zedd Zedd Z	d	Z
d
ZdZdd ZdS )	VAELoader)taehvlighttaew2_2lighttaew2_1lighttaehy1_5taeltx_2)taesdtaesdxltaesd3taef1c                 C  s4  t d}t d}d}d}d}d}d}d}d}	d}
|D ]R}|dr&d}q|dr.d}q|dr6d}q|dr>d}q|d	rFd}q|d
rNd}q|drVd}
q|dr^d}	q| jD ]}||rm|| qaq|rx|rx|d |r|r|d |r|r|d |
r|	r|d |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  ro  
startswith
video_taesrf   )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taer   r   r   vae_list  sT   

















zVAELoader.vae_listc                   s:  i }t d}tt fdd|}tt fdd|}tjt d|}|D ]}|| |d|< q)tjt d|}|D ]}|| |d|< qA dkrat	
d|d	< t	
d
|d< |S  dkrut	
d|d	< t	
d
|d< |S  dkrt	
d|d	< t	
d|d< |S  dkrt	
d|d	< t	
d|d< |S )Nr  c                      |  d S )Nz{}_encoder.r  re  anamer   r   <lambda>      z&VAELoader.load_taesd.<locals>.<lambda>c                   r  )Nz{}_decoder.r  r  r  r   r   r    r  ztaesd_encoder.{}ztaesd_decoder.{}r  g{P?	vae_scalerK   	vae_shiftr  gy&1?r  grh|?gH.?r  gxz,C?g=U?)r  ro  nextfilterr   r3  r  rz  re  ra   r2  )r  r{  r  encoderdecoderenckdecr   r  r   
load_taesd
  s4   

zVAELoader.load_taesdc                 C  s   dd|  | fiiS )Nr   vae_name)r  r&   r   r   r   r(   (  r  zVAELoader.INPUT_TYPESr   load_vaer  c                 C  s   d }|dkri }t d|d< n-|| jv r| |}n"tj|d | jv r-t	d|}nt	d|}t
jj|dd\}}t
jj||d	}|  |fS )
Nr  rJ   pixel_space_vaer   r  r   T)return_metadata)r{  r'  )ra   r2  
image_taesr  r.  r/  splitextr  r  rz  r   r3  r  r{  r   throw_exception_if_invalid)r/   r  r'  r{  vae_pathr   r   r   r   r  1  s   
zVAELoader.load_vaeN)r1   r2   r3   r  r  staticmethodr  r  r4   r(   r6   r8   r9   r  r   r   r   r   r    s    
-

r  c                   @  r   )
ControlNetLoaderc                 C  r  )Nr   control_net_name
controlnetrn  r&   r   r   r   r(   C  r  zControlNetLoader.INPUT_TYPESCONTROL_NETload_controlnetr  )r  control netcnzload controlnetzcontrolnet loaderc                 C  .   t d|}tj|}|d u rtd|fS )Nr  zPERROR: controlnet file is invalid and does not contain a valid controlnet model.)r  rz  r   r  r  r,   )r/   r  controlnet_pathr  r   r   r   r  M  
   z ControlNetLoader.load_controlnetN)
r1   r2   r3   r4   r(   r6   r8   r9   r;   r  r   r   r   r   r  B  s    
r  c                   @  rr   )	DiffControlNetLoaderc                 C  s   ddt dfdiS )Nr   r  r  )r  r  rn  r&   r   r   r   r(   U  s   
z DiffControlNetLoader.INPUT_TYPESr  r  r  c                 C  s    t d|}tj||}|fS )Nr  )r  rz  r   r  r  )r/   r  r  r  r  r   r   r   r  _  s   z$DiffControlNetLoader.load_controlnetN)	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r  T  r   r  c                   @  s0   e Zd Zedd ZdZdZdZdZdd Z	d	S )
ControlNetApplyc              
   C  r   )Nr   r?   r  r   rI   rJ   rK   rz   rL   rM   )r*   control_netimager   r   r&   r   r   r   r(   f  s
   zControlNetApply.INPUT_TYPESr?   apply_controlnetTconditioning/controlnetc           
      C  s   |dkr|fS g }| dd}|D ]4}|d |d  g}| ||}	d|d v r4|	|d d  |	|d d< d|d d< || q|fS )Nr   r   rV   controlTcontrol_apply_to_uncond)movedimre   set_cond_hintset_previous_controlnetrf   )
r/   r*   r  r  r   r   control_hintr   rp   c_netr   r   r   r  s  s   z ControlNetApply.apply_controlnetN)
r1   r2   r3   r4   r(   r6   r8   r  r9   r  r   r   r   r   r  e  s    
r  c                   @  s>   e Zd Zedd ZdZdZdZdZg dZ	dg fd	d
Z
dS )ControlNetApplyAdvancedc                 C  sH   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 )Nr?   r  r   rI   rJ   rK   rz   rL   rM   r   )r  r  r  r  r   r   r   r   r   r   optionalr   r&   r   r   r   r(     s   z#ControlNetApplyAdvanced.INPUT_TYPES)r5   r5   )r  r  r  r  )r  zapply controlnetzuse controlnetr  Nc
              	   C  s   |dkr||fS | dd}
i }g }||fD ]M}g }|D ]A}|d  }|dd }||v r3|| }n| j|
|||f||	d}|| |||< ||d< d|d< |d |g}|| q|| q|d |d fS )Nr   r   rV   r  )r   extra_concatFr  )r  re   r^   r  r   rf   )r/   r  r  r  r  r   r   r   r   r  r  cnetsrg   r*   r   r   r   	prev_cnetr  rp   r   r   r   r    s*   

z(ControlNetApplyAdvanced.apply_controlnet)r1   r2   r3   r4   r(   r6   r  r8   r9   r;   r  r   r   r   r   r    s    
r  c                   @  rr   )	
UNETLoaderc                 C  s"   dt dfg dddifdiS )Nr   diffusion_models)rN   
fp8_e4m3fnfp8_e4m3fn_fastfp8_e5m2r   T)	unet_nameweight_dtypern  r&   r   r   r   r(     s   zUNETLoader.INPUT_TYPESr  	load_unetrs  c                 C  sh   i }|dkrt j|d< n|dkrt j|d< d|d< n	|dkr#t j|d< td|}tjj||d}|fS )	Nr  dtyper  Tfp8_optimizationsr  r
  )model_options)ra   float8_e4m3fnfloat8_e5m2r  rz  r   r{  load_diffusion_model)r/   r  r  r  	unet_pathr  r   r   r   r    s   


zUNETLoader.load_unetN)	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r	    r   r	  c                   @  s2   e Zd Zedd ZdZdZdZdZdd	d
Z	dS )
CLIPLoaderc                 C  s.   t 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_namer$  rM  rN   rK  r   Tr  rn  r&   r   r   r   r(     s   
zCLIPLoader.INPUT_TYPESr  	load_cliprs  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 3Br  rN   c                 C  sj   t tjj| tjjj}i }|dkrtd |d< |d< t	d|}tjj
|gtd||d}|fS )NrK  load_deviceoffload_devicer  rt  
ckpt_pathsrx  	clip_typer  )getattrr   r{  CLIPTypeupperSTABLE_DIFFUSIONra   rM  r  rz  r.  r|  )r/   r-  r$  rM  r3  r  	clip_pathr#   r   r   r   r.    s   zCLIPLoader.load_clipN)r  rN   
r1   r2   r3   r4   r(   r6   r8   r9   r:   r.  r   r   r   r   r    s    
r  c                   @  r   )DualCLIPLoaderc                 C  s8   t dft dfg dfddddgddifid	S )
Nr  )sdxlr  fluxhunyuan_videor$  r)  hunyuan_video_15
kandinsky5kandinsky5_imager  newbier&  )
clip_name1
clip_name2r$  rM  rN   rK  r   Tr  rn  r&   r   r   r   r(     s   

zDualCLIPLoader.INPUT_TYPESr  r.  rs  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 v2rN   c           
      C  sx   t tjj| tjjj}td|}td|}i }|dkr*t	d |d< |d< tjj
||gtd||d}	|	fS )Nr  rK  r/  r0  rt  r1  )r4  r   r{  r5  r6  r7  r  rz  ra   rM  r.  r|  )
r/   rB  rC  r$  rM  r3  
clip_path1
clip_path2r  r#   r   r   r   r.    s   zDualCLIPLoader.load_clipN)rN   r9  r   r   r   r   r:    s    
r:  c                   @  rr   )	CLIPVisionLoaderc                 C  r  )Nr   r-  clip_visionrn  r&   r   r   r   r(   
  r  zCLIPVisionLoader.INPUT_TYPESr  r.  r  c                 C  r  )NrG  zMERROR: clip vision file is invalid and does not contain a valid vision model.)r  rz  r   rG  rJ  r,   )r/   r-  r8  rG  r   r   r   r.    r  zCLIPVisionLoader.load_clipN)	r1   r2   r3   r4   r(   r6   r8   r9   r.  r   r   r   r   rF  	      
rF  c                   @  rr   )	CLIPVisionEncodec                 C  s   dddddgfdiS )Nr   rH  r   centernone)rG  r  cropr   r&   r   r   r   r(     s   zCLIPVisionEncode.INPUT_TYPESCLIP_VISION_OUTPUTr)   r*   c                 C  s$   d}|dkrd}|j ||d}|fS )NTrK  FrM  )encode_image)r/   rG  r  rM  
crop_imager   r   r   r   r)   &  s
   zCLIPVisionEncode.encodeNr   r   r   r   r   rJ    ru   rJ  c                   @  rr   )	StyleModelLoaderc                 C  r  )Nr   style_model_namestyle_modelsrn  r&   r   r   r   r(   .  r  zStyleModelLoader.INPUT_TYPESSTYLE_MODELload_style_modelr  c                 C     t d|}tj|}|fS )NrU  )r  rz  r   r{  rX  )r/   rT  style_model_pathstyle_modelr   r   r   rX  7     z!StyleModelLoader.load_style_modelN)	r1   r2   r3   r4   r(   r6   r8   r9   rX  r   r   r   r   rS  -  rI  rS  c                   @  2   e Zd ZdgZedd ZdZdZdZdd Z	d	S )
StyleModelApplyzstyle transferc              
   C  s(   ddddddddd	d
fddgfdiS )Nr   r?   rV  rN  rI   rJ   rK   rz   r   rM   multiply	attn_bias)r*   r[  clip_vision_outputr   strength_typer   r&   r   r   r   r(   @  s   zStyleModelApply.INPUT_TYPESr?   apply_stylemodelzconditioning/style_modelc                 C  s  | |jdddjdd}|dkr||9 }|jd }g }|D ]}	|	\}
}| }d|v s8|dkr0|dkr0tt|dkrB|ndg}|d	d
}|d |d  }|
jd }|dd }|d u rvtj	|
jd || || ftj
d}|jtjkrt|jtj
d}tj	|
jd || | || | ftj
d}|d d d |d |f |d d d |d |f< |d d d ||d f |d d d ||| d f< |d d |d d |f |d d || d d |f< |d d |d |d f |d d || d || d f< ||d d d |||| f< ||d d || d ||| f< ||
j|d< ||d	< |tj|
|fdd|g q|fS )Nr   rV   )	start_dimend_dimrY   r_  attention_maskr`  rJ   attention_mask_img_shape)rV   rV   r  )get_condflattenr   r`   re   ra   logTensorr^   rc   float16r  booltorM  rf   rb   )r/   r*   r[  ra  r   rb  condrp   c_outr   txtkeysr`  mask_ref_sizen_refn_txtr   new_maskr   r   r   rc  M  s:   


$,0448"z StyleModelApply.apply_stylemodelN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   rc  r   r   r   r   r^  =  s    
r^  c                   @  rr   )	unCLIPConditioningc              
   C  s.   ddddddddd	fdd
d
ddd	fdiS )Nr   r?   rN  rI   rJ   g      $rz   rL   rM   rK   )r*   ra  r   noise_augmentationr   r&   r   r   r   r(   }  s
   zunCLIPConditioning.INPUT_TYPESr?   	apply_admr*   c                 C  s2   |dkr|fS t j|d|||dgidd}|fS )Nr   unclip_conditioning)ra  r   ry  T)rf   r   )r/   r*   ra  r   ry  r   r   r   r   rz    s   zunCLIPConditioning.apply_admN)	r1   r2   r3   r4   r(   r6   r8   r9   rz  r   r   r   r   rx  |  s    
rx  c                   @  rr   )	GLIGENLoaderc                 C  r  )Nr   gligen_namegligenrn  r&   r   r   r   r(     r  zGLIGENLoader.INPUT_TYPESGLIGENload_gligenr  c                 C  rY  )Nr~  )r  rz  r   r{  r  )r/   r}  gligen_pathr~  r   r   r   r    r\  zGLIGENLoader.load_gligenN)	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r|    rI  r|  c                   @  rr   )	GLIGENTextBoxApplyc                 C  s\   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   r?   r  r  r$   T)r   r    rw   rx   ry   rM   r   )rR   r#   gligen_textbox_modelr"   r|   r}   r~   r   r   r&   r   r   r   r(     s   zGLIGENTextBoxApply.INPUT_TYPESr?   rf   zconditioning/gligenc	                 C  s   g }	|j ||dd\}
}|D ]<}|d |d  g}||d |d |d |d fg}g }d|d v r<|d d d }d||| f|d d< |	| q|	fS )	Nunprojected)return_pooledr   rV   ry   r~  rX   position)encode_from_tokensr-   re   rf   )r/   rR   r#   r  r"   r|   r}   r~   r   r   rp  cond_pooledr   rp   position_paramsprevr   r   r   rf     s    zGLIGENTextBoxApply.appendNr   r   r   r   r   r    s    

r  c                   @  >   e Zd Zedd ZdZdZdZdZdZ	g dZ
dd
dZdS )EmptyLatentImagec              	   C  s>   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   rw   r      ry   z)The width of the latent images in pixels.r  z*The height of the latent images in pixels.rV   r   z)The number of latent images in the batch.rN   rO   rP   r!   )r|   r}   
batch_sizer   r&   r   r   r   r(     s   zEmptyLatentImage.INPUT_TYPESr   )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blankrV   c                 C  s8   t j|d|d |d gtj tj d}|ddfS )Nr   ry   rM  r  )r   downscale_ratio_spacial)ra   rc   r   r   intermediate_deviceintermediate_dtype)r/   r|   r}   r  r   r   r   r   r    s   ,zEmptyLatentImage.generateNrV   )r1   r2   r3   r4   r(   r6   r7   r8   r9   r:   r;   r  r   r   r   r   r    s    
r  c                   @  rG   )
LatentFromBatch)zselect from batchzpick latentzbatch subsetc                 C  s(   dddddddfdddddfd	iS )
Nr   r   rw   r   ?   rN   rO   rP   rV   rx   )r   batch_indexlengthr   r&   r   r   r   r(     s   zLatentFromBatch.INPUT_TYPESr   	frombatchlatent/batchc                 C  s$  |  }|d }t|jd d |}t|jd | |}||||   |d< d|v rn|d }|jd dkr>| |d< n0|jd |jd k rb|t|jd |jd  dddd |jd  }||||   |d< d|vrdd t||| D |d< |fS |d |||  |d< |fS )Nr   r   rV   r   r  c                 S  s   g | ]}|qS r   r   r@  r~   r   r   r   rD    r  z-LatentFromBatch.frombatch.<locals>.<listcomp>)re   rO   r`   r   repeatr   r   r_   )r/   r   r  r  r'   s_inmasksr   r   r   r    s"   4zLatentFromBatch.frombatchN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r   r   r   r   r    s    
r  c                   @  r   )RepeatLatentBatchzduplicate latentzclone latentc                 C  s   dddddddfdiS )Nr   r   rw   rV   rx   r  )r   amountr   r&   r   r   r   r(     s   zRepeatLatentBatch.INPUT_TYPESr   r  r  c                   s  |  |d }||fd|jd   d< d|v rd|d jd dkrd|d }|jd |jd k rQ|t|jd |jd  fd|jd   d |jd  }|d |fd|d jd   d< dv rtd td  d  d  fddtd|D  d< fS )	Nr   r  rV   r   r   r  c                   s&   g | ]}d  D ]}||   qqS )r  r   )r@  rj   r~   offsetr'   r   r   rD       & z,RepeatLatentBatch.repeat.<locals>.<listcomp>)	re   r  ndimr`   r   r   rP   rO   r_   )r/   r   r  r  r  r   r  r   r    s   >&&zRepeatLatentBatch.repeatN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r   r   r   r   r    rq   r  c                   @  sD   e Zd ZddgZg dZddgZedd ZdZd	Z	d
Z
dd ZdS )LatentUpscaleenlarge latentresize latentnearest-exactr   r   bicubicbislerpdisabledrK  c              
   C  8   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   rw   r   r   ry   rM   )r   upscale_methodr|   r}   rM  upscale_methodsr   crop_methodsr&   r   r   r   r(     
   
zLatentUpscale.INPUT_TYPESr   upscaler   c                 C  s   |dkr|dkr|}|fS |  }|dkr0td|}tdt|d jd | |d jd  }n)|dkrOtd|}tdt|d jd | |d jd  }n
td|}td|}tj|d |d |d |||d< |fS )Nr   rx   r   r   r   ry   )re   rP   r   r`   r   r3  common_upscale)r/   r   r  r|   r}   rM  r'   r   r   r   r  $  s   
,
,

$zLatentUpscale.upscaleN)r1   r2   r3   r;   r  r  r4   r(   r6   r8   r9   r  r   r   r   r   r    s    
r  c                   @  s<   e Zd Zg dZg dZedd ZdZdZdZ	dd	 Z
d
S )LatentUpscaleBy)r  r  zscale latentr  c              	   C  "   dd| j fddddddfdiS )	Nr   r   rI   g      ?rL          @rM   )r   r  scale_byr  r&   r   r   r   r(   <     
zLatentUpscaleBy.INPUT_TYPESr   r  r   c                 C  sV   |  }t|d jd | }t|d jd | }tj|d |||d|d< |fS )Nr   r   r   r  )re   r   r`   r   r3  r  )r/   r   r  r  r'   r|   r}   r   r   r   r  E  s
   zLatentUpscaleBy.upscaleN)r1   r2   r3   r;   r  r4   r(   r6   r8   r9   r  r   r   r   r   r  7  s    
r  c                   @  rr   )	LatentRotatec                 C  s   ddg dfdiS )Nr   r   )rL  z
90 degreesz180 degreesz270 degrees)r   rotationr   r&   r   r   r   r(   M     zLatentRotate.INPUT_TYPESr   rotatelatent/transformc                 C  s\   |  }d}|drd}n|drd}n|drd}tj|d |ddgd	|d< |fS )
Nr   90rV   180rX   270r   r   )r  dims)re   r  ra   rot90)r/   r   r  r'   	rotate_byr   r   r   r  W  s   


zLatentRotate.rotateN)	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r  L  r   r  c                   @  r]  )

LatentFlipzmirror latentc                 C  s   ddddgfdiS )Nr   r   zx-axis: verticallyzy-axis: horizontally)r   flip_methodr   r&   r   r   r   r(   g  r  zLatentFlip.INPUT_TYPESr   flipr  c                 C  sX   |  }|drtj|d dgd|d< |fS |dr)tj|d dgd|d< |fS )Nr~   r   rX   )r  r   r   )re   r  ra   r  )r/   r   r  r'   r   r   r   r  q  s   

zLatentFlip.flipN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r   r   r   r   r  d  s    
r  c                   @  s6   e Zd Zg dZedd ZdZdZdZdd	d
Z	dS )LatentComposite)zoverlay latentzlayer latentzpaste latentc                 C  s>   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   rw   r   ry   rM   )
samples_tosamples_fromr~   r   featherr   r&   r   r   r   r(   }  s   zLatentComposite.INPUT_TYPESr   	compositer   normalr   c              	   C  s  |d }|d }|d }|  }|d  }|d }|d }|dkrV|d d d d d |jd | d |jd | f |d d d d |||jd  |||jd  f< n"|d d d d d |jd | d |jd | f }t|}	t|D ]}
|dkr|	d d d d |
d|
 d d f  d| |
d  9  < ||jd  |jd k r|	d d d d |	jd d |
 |	jd |
 d d f  d| |
d  9  < |dkr|	d d d d d d |
d|
 f  d| |
d  9  < ||jd  |jd k r |	d d d d d d |	jd d |
 |	jd |
 f  d| |
d  9  < qyt|	|	 }|d d d d d |jd | d |jd | f |	 |d d d d |||jd  |||jd  f |  |d d d d |||jd  |||jd  f< ||d< |fS )Nry   r   r   rX   r   rV   rJ   )re   r   r`   ra   	ones_liker_   )r/   r  r  r~   r   composite_methodr  samples_outr'   r   r   rev_maskr   r   r   r    s2   h4
8L8LzLatentComposite.compositeN)r  r   )
r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r   r   r   r   r  z  s    
r  c                   @  s@   e Zd ZddgZedd ZdZdZdZddddZ	dd Z
dS )LatentBlendzmix latentszinterpolate latentsc              	   C  s   dddddddddfd	iS )
Nr   r   rI   r   r   rV   rL   rM   )samples1samples2blend_factorr   r&   r   r   r   r(     s   zLatentBlend.INPUT_TYPESr   blendr   r  r  rQ  
blend_modestrc                 C  s   |  }|d }|d }|j|jkr3|dddd tjj||jd |jd ddd}|dddd | |||}|| |d|   }||d< |fS )	Nr   r   r   rV   rX   r  rK  rP  )re   r`   permuter   r3  r  r  )r/   r  r  r  r  r  samples_blendedr   r   r   r    s   "zLatentBlend.blendc                 C  s   |dkr|S t d| )Nr  zUnsupported blend mode: )
ValueError)r/   img1img2r   r   r   r   r    s   zLatentBlend.blend_modeN)r  )r  rQ  r  r  )r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r  r   r   r   r   r    s    
r  c                   @  r   )
LatentCropztrim latentz
cut latentc                 C  sL   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   rw   r   rx   ry   rM   r   )r   r|   r}   r~   r   r   r&   r   r   r   r(     s   zLatentCrop.INPUT_TYPESr   rM  r  c                 C  s   |  }|d }|d }|d }||jd d kr |jd d }||jd d kr0|jd d }|d }|d }|| }	|| }
|d d d d ||
||	f |d< |fS )Nr   ry   r   rX   )re   r`   )r/   r   r|   r}   r~   r   r'   
new_height	new_widthto_xto_yr   r   r   rM    s   $zLatentCrop.cropN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   rM  r   r   r   r   r    s    
r  c                   @  rr   )	SetLatentNoiseMaskc                 C  r   )Nr   r   r   )r   r   r   r&   r   r   r   r(     s   zSetLatentNoiseMask.INPUT_TYPESr   set_maskr   c                 C  s0   |  }|dd|jd |jd f|d< |fS )Nr   rV   r   r   )re   r   r`   )r/   r   r   r'   r   r   r   r    s   "zSetLatentNoiseMask.set_maskN)	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r    r   r  rJ   Fc                 C  s   |d }t j| ||dd }|
r tj| |j|jdd}nd|v r(|d nd }t j	|||}d }d|v r<|d }t
| |}t jj }t jj| |||||||||	|
|||||||d}| }|dd  ||d< |fS )Nr   r  rK  )r  layoutrM  r  r   )	denoisedisable_noise
start_step	last_stepforce_full_denoiser   callbackdisable_pbarseed)r   samplefix_empty_latent_channelsr^   ra   rc   r   r  r  prepare_noiselatent_previewprepare_callbackr3  PROGRESS_BAR_ENABLEDre   pop)r  r  stepscfgsampler_name	schedulerr  r  r   r  r  r  r  r  latent_imagenoise
batch_indsr   r  r  r   rg   r   r   r   common_ksampler  s&   

r  c                   @  r  )KSamplerc                 C  s   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   rq  r!   z.The model used for denoising the input latent.rw   r       Tz,The random seed used for creating the noise.)rN   rO   rP   control_after_generater!      rV   '  z2The number of steps used in the denoising process.r  rI   r  rK   r  皙?rL   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.)rN   rO   rP   rQ   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.r5   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.rJ   zThe amount of denoising applied, lower values will maintain the structure of the initial image allowing for image to image sampling.r  )
r  r  r  r  r  r  r  r  r  r  r   samplersr  SAMPLERS
SCHEDULERSr&   r   r   r   r(     s   



zKSampler.INPUT_TYPESr   )zThe denoised latent.r  samplingzXUses the provided model, positive and negative conditioning to denoise the latent image.)samplerr  r  r  diffusetxt2imgimg2imgrJ   c                 C  s   t |||||||||	|
d
S )N)r  r  )r/   r  r  r  r  r  r  r  r  r  r  r   r   r   r  6  s   zKSampler.sampleNrJ   )r1   r2   r3   r4   r(   r6   r7   r8   r9   r:   r;   r  r   r   r   r   r    s    
r  c                   @  r   )KSamplerAdvancedc                 C  s   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   Trw   r   r  )rN   rO   rP   r  r  rV   r  r  rI   r  rK   r  r  rL   )rN   rO   rP   rQ   r   r?   r   )rN   rO   rP   r   )r  	add_noise
noise_seedr  r  r  r  r  r  r  start_at_stepend_at_stepreturn_with_leftover_noiser	  r&   r   r   r   r(   :  s    

zKSamplerAdvanced.INPUT_TYPESr   r  r  rJ   c                 C  sD   d}|dkrd}d}|dkrd}t ||||||||	|
|||||dS )NTr  Fr  )r  r  r  r  r  r  )r/   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r  R  s   $zKSamplerAdvanced.sampleNr  )	r1   r2   r3   r4   r(   r6   r8   r9   r  r   r   r   r   r  9  s    
r  c                   @  sJ   e Zd Zdd Zedd ZdZdZdZdZ	d	Z
d
Zg dZdddZdS )	SaveImagec                 C  s    t  | _d| _d| _d| _d S )Nr   r  r   )r  r  r  r$  prefix_appendcompress_levelr  r   r   r   r  \  s   

zSaveImage.__init__c                 C  s(   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   r  r  r  r  r  r   r&   r   r   r   r(   b  s   
zSaveImage.INPUT_TYPESr   save_imagesTr  Basicsz8Saves the input images to your ComfyUI output directory.)r  z
save imagezexport imageoutput imagezwrite imagedownloadr  Nc              	   C  s6  || j 7 }t|| j|d jd |d jd \}}}}}t }	t|D ]p\}
}d|   }t	
t|ddtj}d }tjsgt }|d urS|dt| |d urg|D ]}||t||  qY|dt|
}| d|dd	}|jtj|||| jd
 |	||| jd |d7 }q$dd|	iiS )Nr   rV        o@   r+   z%batch_num%r  r  z_.png)pnginfor  r!  r(  r   )r  r  r*  r  r`   list	enumeraterK  numpyr   	fromarraynpr#   astypeuint8r   r-  r   add_textr+  r,  replacer  r  r.  r/  r0  r  rf   r$  )r/   r   r  r+   r  r5  r"  r6  r#  r9  batch_numberr  rj   imgr'  r~   filename_with_batch_numr8  r   r   r   r  x  s0   
0
zSaveImage.save_imagesr:  )r1   r2   r3   r  r4   r(   r6   r8   r;  r9   rF   r:   r;   r  r   r   r   r   r  [  s    
r  c                   @  s(   e Zd Zdd Zg dZedd ZdS )PreviewImagec                 C  s8   t  | _d| _dddd tdD  | _d| _d S )Ntemp_temp_r  c                 s  s    | ]}t d V  qdS )abcdefghijklmnopqrstupvxyzN)randomchoicer  r   r   r   	<genexpr>  s    z(PreviewImage.__init__.<locals>.<genexpr>r   rV   )r  get_temp_directoryr  r$  r0  r_   r  r  r  r   r   r   r    s   

zPreviewImage.__init__)previewzpreview imagez
show imagez
view imagezdisplay imagezimage viewerc                 C  s   ddiddddS )Nr   r   r  r  r  r  r   r&   r   r   r   r(     s   zPreviewImage.INPUT_TYPESN)r1   r2   r3   r  r;   r4   r(   r   r   r   r   r2    s
    r2  c                   @  sP   e Zd Zedd ZdZdZg dZdZdZ	dd	 Z
ed
d Zedd ZdS )	LoadImagec                   sF   t    fddt D }t |dg}ddt|ddifiiS )Nc                   &   g | ]}t jt j |r|qS r   r.  r/  r=  r0  r?  rB  r   r   rD    r  z)LoadImage.INPUT_TYPES.<locals>.<listcomp>r  r   image_uploadT)r  rE  r.  rF  filter_files_content_typesrG  rH  r   rB  r   r(     s   zLoadImage.INPUT_TYPESr  r   )z
load imagez
open imagezimport imagezimage inputzupload imagez
read imagezimage loaderr   r   
load_imagec                 C  s  t |}ttj|}g }g }d\}}tj }t	
|D ]}	ttj|	}	|	jdkr3|	dd }	|	d}t|dkrH|jd }|jd }|jd |ksV|jd |krWqt|tjd }t|d	 }d
|	 v rt|	d
tjd }
dt|
 }
n,|	jdkrd|	jv rt|	dd
tjd }
dt|
 }
n	tjdtjdd}
||j|d ||
dj|d |jdkr nqt|dkrtj |dd}tj |dd}||fS |d }|d }||fS )N)NNIc                 S     | d S Ngp?r   rj   r   r   r   r        z&LoadImage.load_image.<locals>.<lambda>RGBr   rV   r#  r   ArJ   PtransparencyRGBArx   rx   rK  r  rM  rh  MPOrY   )!r  rN  r   pillowr   rZ  r   r   r  r   Iteratorr   exif_transposer   pointconvertr[   r   r*  arrayr+  float32ra   
from_numpygetbands
getchannelinforc   rf   ro  r   re  rb   )r/   r  r_  r0  output_imagesoutput_maskswhr  rj   r   output_imageoutput_maskr   r   r   rA    sJ   





"
zLoadImage.load_imagec                 C  rT  rU  rW  )r'   r  r_  r   rA  r   r   r   r`    ra  zLoadImage.IS_CHANGEDc                 C  rb  NzInvalid image file: {}Trc  r'   r  r   r   r   rf       

zLoadImage.VALIDATE_INPUTSN)r1   r2   r3   r4   r(   r9   rF   r;   r6   r8   rA  r`  rf  r   r   r   r   r;    s    
2
r;  c                   @  sX   e Zd ZdZg dZg dZedd ZdZdZ	dZ
d	d
 Zedd Zedd ZdS )LoadImageMaskImage Tools)zimport maskz
alpha maskzchannel mask)alpharedgreenbluec                   s>   t    fddt D }dt|ddif| jfdiS )Nc                   r<  r   r=  r?  rB  r   r   rD    r  z-LoadImageMask.INPUT_TYPES.<locals>.<listcomp>r   r>  T)r  channel)r  rE  r.  rF  rG  _color_channelsrH  r   rB  r   r(     s   zLoadImageMask.INPUT_TYPESr   r   rA  c                 C  s   t |}ttj|}ttj|}| dkr*|j	dkr%|
dd }|d}d }|d  }|| v rTt||tjd }t|}|dkrSd	| }n	tjd
tjdd}|dfS )N)RGBrH  rB  c                 S  rC  rD  r   rE  r   r   r   r    rF  z*LoadImageMask.load_image.<locals>.<lambda>rK  r   r#  rH  rJ   rL  rK  rM  )r  rN  r   rO  r   rZ  r   rQ  rW  r   rR  rS  r6  r*  rT  rX  r+  rU  ra   rV  rc   r   )r/   r  ri  r_  rj   r   r   r   r   r   rA    s"   



zLoadImageMask.load_imagec                 C  sV   t |}t }t|d}||  W d    n1 s w   Y  |  S rU  rW  )r'   r  ri  r_  r   rA  r   r   r   r`    ra  zLoadImageMask.IS_CHANGEDc                 C  rb  r`  rc  ra  r   r   r   rf  "  rb  zLoadImageMask.VALIDATE_INPUTSN)r1   r2   r3   rF   r;   rj  r4   r(   r9   r6   r8   rA  r`  rf  r   r   r   r   rc    s    

rc  c                   @  s,   e Zd ZddgZedd ZdZdZdZdS )	LoadImageOutputr!  zprevious generationc              	   C  s    dddddddddd	fiiS )
Nr   r  COMBOTr   z/internal/files/outputfirst)routerefresh_buttoncontrol_after_refresh)r>  image_folderremoter   r&   r   r   r   r(   -  s   zLoadImageOutput.INPUT_TYPESzLoad 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.TrA  N)	r1   r2   r3   r;   r4   r(   r:   EXPERIMENTALr8   r   r   r   r   rn  *  s    
rn  c                   @  sH   e Zd Zg dZddgZedd ZdZdZdZ	d	Z
g d
Zdd ZdS )
ImageScaler  r   r   r  lanczosr  rK  c              
   C  r  )	Nr   r   rw   r   r   rV   rM   )r  r  r|   r}   rM  r  r&   r   r   r   r(   F  r  zImageScale.INPUT_TYPESr   r  image/upscalingrd  )resizezresize imagezscale imagezimage resizezoomzzoom inzchange sizec                 C  s   |dkr|dkr|}|fS | dd}|dkr)tdt|jd | |jd  }n|dkr>tdt|jd | |jd  }tj|||||}| dd}|fS )Nr   r   rV   r   rX   )r  rP   r   r`   r   r3  r  )r/   r  r  r|   r}   rM  r'   r   r   r   r   r  S  s   $"zImageScale.upscaleN)r1   r2   r3   r  r  r4   r(   r6   r8   r9   rF   r;   r  r   r   r   r   rw  B  s    
rw  c                   @  s8   e Zd ZdZg dZedd ZdZdZdZ	dd	 Z
d
S )ImageScaleByrd  rx  c              	   C  r  )	Nr   r   rI   rJ   rL   r  rM   )r  r  r  r  r&   r   r   r   r(   f  r  zImageScaleBy.INPUT_TYPESr   r  rz  c                 C  sV   | dd}t|jd | }t|jd | }tj||||d}| dd}|fS )Nr   rV   r   rX   r  )r  r   r`   r   r3  r  )r/   r  r  r  r   r|   r}   r'   r   r   r   r  o  s   zImageScaleBy.upscaleN)r1   r2   r3   rF   r  r4   r(   r6   r8   r9   r  r   r   r   r   r}  b  s    
r}  c                   @  s6   e Zd ZdgZdZedd ZdZdZdZ	dd	 Z
d
S )ImageInvertzreverse colorsrd  c                 C  r   )Nr   r  r   r   r&   r   r   r   r(   {  r   zImageInvert.INPUT_TYPESr   invertr  c                 C  s   d| }|fS )NrJ   r   )r/   r  r'   r   r   r   r    s   zImageInvert.invertN)r1   r2   r3   r;   rF   r4   r(   r6   r8   r9   r  r   r   r   r   r~  w  s    
r~  c                   @  s8   e Zd Zg dZedd ZdZdZdZdZ	dd	 Z
d
S )
ImageBatch)zcombine imageszmerge imageszstack imagesc                 C  r>   )Nr   r   )image1image2r   r&   r   r   r   r(     rB   zImageBatch.INPUT_TYPESr   batchr  Tc                 C  s   |j d |j d kr+|j d |j d kr tjjj|dddd}ntjjj|dddd}|j dd  |j dd  krQtj|dd|j d |j d dd	dd}tj	||fd
d}|fS )Nr   )r   rV   constantrJ   )r   r   rV   rX   r   rK  r   rY   )
r`   ra   r   r   padr   r3  r  r  rb   )r/   r  r  r'   r   r   r   r    s   0zImageBatch.batchN)r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r  r   r   r   r   r    r  r  c                   @  s8   e Zd ZdddZedd ZdZdZdZdddZ	dS )
EmptyImagerK  c                 C  s
   || _ d S r   rL  )r/   rM  r   r   r   r    rE   zEmptyImage.__init__c                 C  sJ   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   rw   r   rV   rM   r   r  r   i color)rN   rO   rP   rQ   display)r|   r}   r  r  r   r&   r   r   r   r(     s
   zEmptyImage.INPUT_TYPESr   r  r  rV   r   c           
      C  s   t j }t j }tj|||dg|d? d@ d ||d}tj|||dg|d? d@ d ||d}tj|||dg|d@ d ||d}	tj|||	fddfS )NrV   r  r$  r  ry   r   rY   )r   r   r  r  ra   fullrb   )
r/   r|   r}   r  r  r  rM  rgbr   r   r   r    s   

&&"zEmptyImage.generateN)rK  )rV   r   )
r1   r2   r3   r  r4   r(   r6   r8   r9   r  r   r   r   r   r    s    

r  c                   @  r   )ImagePadForOutpaintzextend canvaszexpand imagec                 C  s^   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   rw   r   ry   rM   (   rV   Tr   )r  lefttoprightbottom
featheringr   r&   r   r   r   r(     s   zImagePadForOutpaint.INPUT_TYPESr@  expand_imager  c                 C  s~  |  \}}}	}
tj||| | |	| | |
ftjdd }||d d ||| |||	 d d f< tj|| | |	| | ftjd}tj||	ftjd}|dkr|d |k r|d |	k rt|D ]G}t|	D ]@}|dkrp|n|}|dkrz|| n|}|dkr|n|	}|dkr|	| n|	}t||||}||krqh|| | }|| |||f< qhqb||||| |||	 f< ||dfS )Nrh  r   r   rX   )r   ra   r   rU  rc   r_   rO   r   )r/   r  r  r  r  r  r  d1d2d3d4	new_imager   r   rj   jdtdbdldrr   r  r   r   r   r    s>   ( z ImagePadForOutpaint.expand_imageN)
r1   r2   r3   r;   r4   r(   r6   r8   r9   r  r   r   r   r   r    s    
r  )r   r   r  r|  r  r  rg  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   module_pathr  r   c                 C  s,   t j| }t j| rt 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   )r.  r/  basenamer=  r  )r  	base_pathr   r   r   get_module_name  s   r  custom_nodesrn  c              
     s  t | }tj| rtj| }|d }|}ntj| r$| dd}ztd	|  tj| rDt
j|| }tj| d }nt
j|tj| d}| }t
j|}|tj|< |j| tj|t|< z1ddlm}	 |	| }
|
jj}|rtj| |}tj|r|
jj}|t|< td	|| W n ty } zt d|  W Y d }~nd }~ww t!|d	rt"|d	d urtjtj|t"|d	}tj|r|t|< t!|d
rt"|d
d ur|j#$ D ]\}}||vr|t#|< d	|t | |_%qt!|drt"|dd urt&'|j& W dS t!|drt"|d}t(|s8t d|  d W dS zrt)*|rF| I d H }n| }t+|t,s\t d|  d W W dS |- I d H  |. I d H }t+|t/s}t d|  d W W dS |D ]'}|0 }|j1|vr|t#|j1< d	|t | |_%|j2d ur|j2t&|j1< qW W dS  ty } zt d|  d|  W Y d }~W dS d }~ww t d|  d W dS  ty } zt t34  t d|  d|  W Y d }~dS d }~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: )5r  r.  r/  r=  r  isdirr.  r\   debugre  	importlibutilspec_from_file_locationsplitr0  module_from_specsysmodulesloaderexec_moduleabspathLOADED_MODULE_DIRScomfy_configr  extract_node_configuration
tool_comfywebprojectr  EXTENSION_WEB_DIRSrY  	Exceptionr]   hasattrr4  r  itemsRELATIVE_PYTHON_MODULEr  r[  callableinspectiscoroutinefunction
isinstancer   on_loadget_node_listr&  
GET_SCHEMAnode_iddisplay_name	traceback
format_exc)r  ignoremodule_parentmodule_namespsys_module_namemodule_spec
module_dirmoduler  project_configweb_dir_nameweb_dir_pathproject_nameeweb_dirr  node_cls
entrypoint	extension	node_listschemar   r   r   load_custom_node  s   





r  c               	     sn  t t } td}g }|D ]t}ttj|}d|v r$|	d |D ]]}tj
||}tj|r@tj|d dkr@q&|drFq&tjrX|tjvrXtd| d q&tjrit|ritd|  q&t }t|| dd	I d
H }|t | ||f q&qt|dkrtd t|D ]}	|	d rd}
nd}
td|	d |
|	d  qtd d
S d
S )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
    r  __pycache__rV   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:rX   r  z (IMPORT FAILED)z{:6.1f} seconds{}: {})setr  rs  r  r|  r.  rF  r/  realpathremover0  r=  r  r>  r   disable_all_custom_nodeswhitelist_custom_nodesr\   rY  enable_managercomfyui_managershould_be_disabledtimeperf_counterr  rf   r[   rG  re  )base_node_names
node_pathsnode_import_timescustom_node_pathpossible_modulespossible_moduler  time_beforesuccessrp   import_messager   r   r   init_external_custom_nodes  sD   


 


r  c                    s`   t jt jt jtd} g d}g }|D ]}tt j| |ddI dH s-|| q|S )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)r.  r/  r0  dirnamer  __file__r  rf   )
extras_dirextras_filesimport_failed	node_filer   r   r   init_builtin_extra_nodes.	  s   
f
r  c                    sn   t jt jt jtd} ttt j| d}g }|D ]}t|ddI d H s4|	t j
| q |S )Ncomfy_api_nodesz
nodes_*.pyr  )r.  r/  r0  r   r  r  rG  globr  rf   r  )api_nodes_dirapi_nodes_filesr  r  r   r   r   init_builtin_api_nodes	  s   r  c                     s   t dd tD  d S )Nc                 S  s   g | ]}t t|d |dqS )VERSION)version	api_class)r   r4  )r@  r  r   r   r   rD  	  s    z$init_public_apis.<locals>.<listcomp>)r   r   r   r   r   r   init_public_apis	  s   r  c                   s  t  I d H  t I d H }g }|rt I d H }| r t I d H  ntd t|dkrUtd |D ]
}td| q2td t	j
rKtd ntd td t|dkrtd	 |D ]
}td| qbtd t	j
r{td ntd td |S )
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  r\   rY  r[   r]   re  r   windows_standalone_build)init_custom_nodesinit_api_nodesr  import_failed_apinoder   r   r   init_extra_nodes	  s8   








r  r  )rJ   FNNF)r  r  r   r  )r  r  r   rn  r  )
__future__r   ra   r.  r  r+  r  rX  r  r  r   r  r6  r\   PILr   r   r   PIL.PngImagePluginr   r(  r*  safetensors.torchrO  r/  insertr0  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  r  r   r  r  r   r   r   r   r<   rH   rs   rv   r   r   r   r   r   r   r   r   r   r   r  r  r<  rg  r  r  r  r  r  r  r  r  r  r  r  r	  r  r:  rF  rJ  rS  r^  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r2  r;  rc  rn  rw  r}  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   <module>   s   (
(!)45&-i3?"$,+#
"9Q5 A	
 !"#$%&'()*+,-./01234F	
 !"#$%&'()*+-./0123456789D
`/y