
    +jv5              
          d dl Z d dlZd dlmc mZ d dlm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ZdZdZg dZg dZd	Zd
Zdej        dej        dededej        f
dZ G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d de          Z de fdZ!dS )    N)override)ComfyExtensionIOTypesi          )g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?i   i   imagemaskerode_radiussizereturnc                    | dd df                              dd                              dd          }|                     dd          d          }t          j        ||gd          d          }|j        dd          \  }}|t          ||          z  }	t          j                            |t          dt          ||	z                      t          dt          ||	z                      dd                               dd          }|d d dd	f         }
|dk    rCt          j        |
 d
|z  dz   d|           }
t          j        |d d d df         |
gd          }t          j        |
d         dk    d          \  }}|                                dk    rt          d          t!          |                                          t!          |	                                          }}t!          |                                          t!          |	                                          }}||z   d
z  ||z   d
z  }}t          ||z
  ||z
            d
z  dz  }t!          ||z
            t!          ||z
            t!          ||z             t!          ||z             f\  }}}}|j        dd          \  }}|                    dd	||z
  ||z
  f          }t          |d          t          |d          t          ||          t          ||          f\  }}}}||k    r5||k    r/|d d d d ||||f         |d d d d ||z
  ||z
  ||z
  ||z
  f<   t          j                            |||dd                               dd          }|d d d df         |d d dd	f         z  d                             dd          }|                    d          S )N.   r      lanczosdisabled      )stridepadding)r   r   T)as_tuplez@TripoSplatPreprocessImage: mask is empty (no foreground pixels).g333333?)clampmovedimtorchcatshapemincomfyutilscommon_upscalemaxroundF
max_pool2dnonzeronumel
ValueErrorint	new_zeros	unsqueeze) r	   r
   r   r   rgbalphargbahwsaysxsx0x1y0y1cxcyhalfleftupperrightlowerHWcropsx0sy0sx1sy1outs                                    =/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_triposplat.py_preprocessrJ      s    RaR.

q!
$
$
,
,R
3
3CJJq!T"E9c5\1%%d+D:bcc?DAqs1ayyA;%%dC5Q<<,@,@#aqSTuBVBVXacmnnttuvxyzzDQQQ!VAa\1"a,.21lSSSSy$qqq"1"u+q)1--]1T7Q;666FB	xxzzQ[\\\]]CMMB]]CMMB2g]R"WMBrBwR  1$s*D #BIBIBIPSTVY]T]P^P^ ^D%:bcc?DAq>>1a=>>DT1s5!}}c%mmSPQ]]RCc3
SyyS3YYEI!!!QQQPSTWPWY\]`Y`J`EaQQQ3;sU{*C$JsTz,AAB;%%dD$	:NNTTUVXYZZD2A2;aaa1f%q
)
1
1!R
8
8C==    c                   J    e Zd Zed             Zedej        fd            ZdS )TripoSplatPreprocessImagec                 z   t          j        ddddt           j                            d          t           j                            d          t           j                            ddd	d
d          t           j                            ddddd
d          gt           j                            d          g          S )NrM   zTripoSplat Preprocess Image3d/conditioningzPCrop center each image to a square canvas on a black background and add padding.r	   r
   r   r   r   r   zQErode the alpha matte by this pixel radius before cropping (avoids border bleed).defaultr   r#   tooltipr         i   zZSquare image size. The model is trained at 1024; other sizes run but are off-distribution.rQ   r   r#   steprR   display_namenode_idrX   categorydescriptioninputsoutputs)r   SchemaImageInputMaskIntOutputclss    rI   define_schemaz'TripoSplatPreprocessImage.define_schema?   s    y/6&jw''f%%^QA2%x  z zVTs2 &B  C C X__'_::;
 
 
 	
rK   r   c                    t          dt                    dz  dz            j        d         j        d         k    r+t          j                            j        d                   t          j        dd                    t          j        dd                   k    rXt          j        d d d f         	                                t          j        dd                   dd          d d df         t          j        fdt          j        d                   D             d	          }t          j        |          S )
Nr   r   r   r   bilinearF)r   modealign_cornersc                 L    g | ] }t          |         |                   !S  )rJ   ).0ir   r	   r
   r   s     rI   
<listcomp>z5TripoSplatPreprocessImage.execute.<locals>.<listcomp>X   s0    pppUVk%(DG\4PPppprK   )dim)r#   r*   r   r    r!   repeat_to_batch_sizetupler%   interpolatefloatr   r   ranger   
NodeOutput)rf   r	   r
   r   r   prepareds    ```` rI   executez!TripoSplatPreprocessImage.executeQ   sA   2D		R2-..:a=EKN**;33D%+a.IIDABB  E%+ac*:$;$;;;=aaag!4!4!6!6U5;qQRsCS=T=T[euz{{{|}|}|}  @A  }A  BD9pppppppZ_`e`klm`nZoZopppvwxxx}X&&&rK   N__name__
__module____qualname__classmethodrg   r   rw   ry   rm   rK   rI   rM   rM   >   sV        
 
 [
" ' ' ' ' [' ' 'rK   rM   c                   J    e Zd Zed             Zedej        fd            ZdS )TripoSplatConditioningc                    t          j        ddddt           j                            dd          t           j                            dd	          t           j                            d
          gt           j                            d          t           j                            d          t           j                            dd          g          S )Nr   zTripoSplat ConditioningrO   zEncode the image with DINOv3 and the Flux2 VAE into TripoSplat positive/negative conditioning, and create the fixed size noise target (latent + camera) for the KSamplerclip_visionzDINOv3 ViT-H/16+ image encoderrR   vaez	Flux2 VAEr	   positiverW   negativelatentz-The fixed size noise target (latent +camera).)rX   rR   rY   )	r   r_   
ClipVisionra   Vaer`   Conditioningrd   Latentre   s    rI   rg   z$TripoSplatConditioning.define_schema]   s    y,2&r ##M;[#\\UK88w'' &&J&??&&J&??	  h@o pp
 
 
 	
rK   r   c                    t           j                            |j                   |j        }|                    dd                              |          }t          j        t          |          
                    dddd          }t          j        t          |          
                    dddd          }||z
  |z  }|                    |                                          d         }t          j        |                                |j        dd                                        t           j                                                  }	|                    |                              t           j                                                  }
|
j        d         }|	d|
gigg}t          j        |	          dt          j        |
          gigg}t           j                                        }t          j        |t*          t,          g|          }t          j        |dt.          g|          }t           j                            ||f          }t5          j        ||d|i          S )	Nr   r   devicer   )pixel_valuesr   reference_latentssamples)r    model_managementload_model_gpupatcherload_devicer   tor   tensor_DINOV3_MEANview_DINOV3_STDmodelru   r%   
layer_normr   intermediate_deviceencode
zeros_likezeros_Q_TOKEN_LENGTH_LATENT_CHANNELS_CAM_CHANNELSnested_tensorNestedTensorr   rw   )rf   r   r   r	   r   imgmeanstdseqfeature1refbr   r   dev
latent_seqcamerar   s                     rI   ry   zTripoSplatConditioning.executeq   s    	--k.ABBB(mmB""%%f--|L888==aAqIIl;v666;;Aq!QGGTzS SYY[[99!<<		SYrss^<<??@V@j@j@l@lmm jj""5#9#M#M#O#OPPIaL 3cU;<=%h//2EHXY\H]H]G^1_`a $88::[!_6F!GPSTTT
aM23???%22J3GHH}Xx)W1EFFFrK   Nrz   rm   rK   rI   r   r   \   s]        
 
 [
& G G G G [G G GrK   r   c                   J    e Zd Zed             Zedej        fd            ZdS )VAEDecodeTripoSplatc                    t          j        ddddt           j                            d          t           j                            dd          t           j                            d	d
t          t          dd          t           j                            ddddd          gt           j        	                    d          g          S )Nr   zTripoSplat Decode	3d/latentzrDecode the sampled TripoSplat latent into a 3D gaussian splat. Modify the number of gaussians to vary the density.r   r   TripoSplat VAE decoderr   num_gaussians       zNumber of gaussians to produce (rounded to a multiple of 32). 262144 matches the octree's point density; higher oversamples the same points (denser, but no new detail) and costs proportionally more VRAM/time.rU   seedr   l    zFSeeds the octree point sampler (global RNG) for deterministic decodes.rP   splatrW   rY   )
r   r_   r   ra   r   rc   _NUM_GAUSSIANS_MIN_NUM_GAUSSIANS_MAXSplatrd   re   s    rI   rg   z!VAEDecodeTripoSplat.define_schema   s    y), N 		**U,DEE_fBTZlsu&l  m m VQA;M%m  o o	 X__'_::;!
 
 
 	
rK   r   c           	      z   |d         }t          |dd          r|                                d         n|}|j        }|j        }t	          t
          t          t          t          |                              }	|	|z  dk    rt          |	|z            |z  }	t          j                            |j                  }
|j        j        }|j        d         }|dz  |	|z  dz  z   |z  |
z  }t          j                            |j        g|           |                    |j        |j        	          }t+          j        d
                              |          }d |                    ||	|          D             }d t3          | D             \  }}}}}t5          j        t9          j        |||||                    S )Nr   	is_nestedFr   r   r   
   memory_required)r   dtypecpur   c                 6    g | ]}|                                 S rm   )render_tensors)rn   gs     rI   rp   z/VAEDecodeTripoSplat.execute.<locals>.<listcomp>   s$    jjj!!##jjjrK   )r   	generatorc              3   >   K   | ]}t          j        |          V  d S N)r   stack)rn   ts     rI   	<genexpr>z.VAEDecodeTripoSplat.execute.<locals>.<genexpr>   s*      6[6[!u{1~~6[6[6[6[6[6[rK   )getattrunbindfirst_stage_modelgaussians_per_pointr#   r   r   r   r*   r$   r    r   
dtype_size	vae_dtypegsmodel_channelsr   load_models_gpur   r   r   r   	Generatormanual_seeddecodezipr   rw   r   SPLAT)rf   r   r   r   r   r2   r   decodergppnr   hiddencond_tokensr   r   parts	positionsscales	rotations	opacitiesshs                        rI   ry   zVAEDecodeTripoSplat.execute   s   I")![%"@"@GAa')"C(:C<N<N$O$OPPs7a<<a#g$A+66s}EE
*l1o&?a3h"_<FS..}o.^^^#*CMBBO5111==dCC	jjW^^FRS_h^-i-ijjj6[6[sTY{6[6[6[3	69i}U[FIyRTUUVVVrK   Nrz   rm   rK   rI   r   r      s]        
 
 [
( W2= W W W [W W WrK   r   c                   J    e Zd Zed             Zedej        fd            ZdS )TripoSplatSamplingPreviewc                 V   t          j        ddddt           j                            d          t           j                            dd          t           j                            d	d
dddd          t           j                            dddddd          t           j                            ddddddd          t           j                            ddd d!dd"d          t           j                            d#d$d%d&d'(          gt           j                                        g)          S )*Nr   zTripoSplat Sampling Previewr   zuPatch the TripoSplat model for the standard Ksampler node to show a live decoded gaussian splat preview at each step.r   r   r   r   octree_levelr   r      Tz>Octree depth for the preview decode (lower = cheaper/coarser).)rQ   r   r#   advancedrR   r   i @  rS   r   r   zMNumber of gaussians to produce for the preview (rounded to a multiple of 32).rU   yawg     V@g     vg     v@g      ?zPreview camera yaw in degrees.)rQ   r   r#   rV   rR   r   pitchg      .@g     @Vg     @V@z Preview camera pitch in degrees.
point_sizer   r   r   zMaximum splat radius in pixels. Each gaussian is sized from its scale and capped here; lower = finer/pointier, higher = chunkier.rP   rY   )r   r_   Modelra   r   rc   Floatrd   re   s    rI   rg   z'TripoSplatSamplingPreview.define_schema   s:   y/6 ? w''U,DEE^QA1t%e  g g_e6XZ%t  v vudEPS]}  IM  O  Ow%TPS]  KO  Q  Q\1!&R  S S X__&&''
 
 
 	
rK   r   c                    ddl m |||||dj        }|j        j        j        }	|	dz  ||j        z  dz  z   |j        j        z  t          j
                            j                  z  fd}
|                                }|                    t          j        j        j        d|
           t%          j        |          S )Nr   )decode_x0_to_image)	gaussianslevelr   r   r   r   r   c                     t          |          }d}t          |          |k    r||         n|                    d          dd dd	
fd}t          |          |k    r|||<   n||d<    | |i |S )Nr   callbackTF)okpbarloadedc                 J   
 
| |||           d         sd S 	 d         s`t           j                            d          }|                    j                   t           j                            |	           dd<    |          }d         "t           j                            |          d<   d                             | dz   |d|d	f           d S # t          $ r7}t          j        d
                    |                     dd<   Y d }~d S d }~ww xY w)Nr   r   T)only_currently_usedr   r   r   JPEGi   z9TripoSplatSamplingPreview: preview failed, disabling ({})F)r    r   loaded_modelsappendr   r   r!   ProgressBarupdate_absolute	Exceptionloggingwarningformat)rV   r6   xtotal_stepsr   r   ecfgr   r   orig_cbstater   s          rI   r   zQTripoSplatSamplingPreview.execute.<locals>.outer_sample_wrapper.<locals>.callback   sQ   &GD"a555T{ F( ? /(-(>(L(Lae(L(f(f%,,S[999.>>}^m>nnn*.h,,S"c::CV},(-(?(?(L(Lf&M11$(K&RUWZI[\\\\\  ( ( (O$_$f$fgh$i$ijjj"'E$KKKKKKK(s   CC! !
D"+,DD")listlenget)executorargskwargscb_idxr   r	  r
  r  r   r   r   s        @@rI   outer_sample_wrapperz?TripoSplatSamplingPreview.execute.<locals>.outer_sample_wrapper   s    ::DF&)$ii&&8&8d6llfjj>T>TG??E( ( ( ( ( ( ( ( ( (& 4yy6!!'V%-z"8T,V,,,rK   triposplat_sampling_preview)comfy.ldm.triposplat.previewr   r   r   diffusion_modelq_token_lengthr   r   r   r    r   r   r   cloneadd_wrapper_with_keypatcher_extension
WrappersMPOUTER_SAMPLEr   rw   )rf   r   r   r   r   r   r   r   fsmr   r  mr  r   r   s     `         @@@rI   ry   z!TripoSplatSamplingPreview.execute   s!   CCCCCC)LW\') ) #k1@&?ms?V.VZ\-\\`c`f`uux}  yO  yZ  yZ  [^  [h  yi  yi  i	- 	- 	- 	- 	- 	- 	- 	-> KKMM	u6ANPm  pD  	E  	E  	E}QrK   Nrz   rm   rK   rI   r   r      sX        
 
 [
. , Y[Yf ,  ,  ,  [,  ,  , rK   r   c                   L    e Zd Zedeeej                          fd            ZdS )TripoSplatExtensionr   c                 :   K   t           t          t          t          gS r   )rM   r   r   r   )selfs    rI   get_node_listz!TripoSplatExtension.get_node_list  s       &"%	
 	
rK   N)	r{   r|   r}   r   r  typer   	ComfyNoder"  rm   rK   rI   r  r    sE        
T$r|*<%= 
 
 
 X
 
 
rK   r  c                  "   K   t                      S r   )r  rm   rK   rI   comfy_entrypointr&    s         rK   )"r  r   torch.nn.functionalnn
functionalr%   typing_extensionsr   comfy.model_managementr    comfy.nested_tensorcomfy.patcher_extensioncomfy.utilscomfy_api.latestr   r   r   r   r   r   r   r   r   r   Tensorr*   rJ   r$  rM   r   r   r   r  r&  rm   rK   rI   <module>r1     s$              & & & & & &                 6 6 6 6 6 6 6 6 6 6  $$$###  "u| "5< "s "RU "Z_Zf " " " "J' ' ' ' ' ' ' '<-G -G -G -G -GR\ -G -G -G`*W *W *W *W *W", *W *W *WZF  F  F  F  F  F  F  F R
 
 
 
 
. 
 
 
! 3 ! ! ! ! ! !rK   