
    +jz                    
   d Z ddlmZ ddlZddlmZ ddlZddlmZ	 ddl
ZddlZddlmZ ddlmZ ddlmZmZmZ ddlmZ  ej        d	          Z ej        d
          Z ej        d          Zd8dZd9dZd:dZd;dZ d<dZ!d=dZ"d>d$Z# G d% d&ej$                  Z%d?d@d,Z& G d- d.ej$                  Z' G d/ d0ej$                  Z( G d1 d2ej$                  Z) G d3 d4ej$                  Z* G d5 d6e          Z+dAd7Z,dS )Bap  ComfyUI nodes for Depth Anything 3.
Model capability matrix:

Variant               head_type  has_sky  has_conf  cam_dec
DA3-Small             dualdpt    False    True      yes
DA3-Base              dualdpt    False    True      yes
DA3-Mono-Large        dpt        True     False     no
DA3-Metric-Large      dpt        True     False     no  (raw output is metres)
    )annotationsN)override)turbo)
preprocess)ComfyExtensionTypesio)triangulate_grid_mesh	DA3_MODELDA3_GEOMETRYDA3_POINT_CLOUDdepthtorch.TensorKreturnc                   | j         \  }}t          j        |t          j        | j                  }t          j        |t          j        | j                  }t          j        ||d          \  }}t          j        ||t          j        |          gd          }t          j        dt          j	        
                    |                    | j                            |          }||                     d          z  S )uS   Pixel-space K⁻¹ unprojection: (H,W) depth → (H,W,3) point map in OpenCV space.)dtypedevicexy)indexingdimzij,hwj->hwi)shapetorcharangefloat32r   meshgridstack	ones_likeeinsumlinalginvto	unsqueeze)r   r   HWuvpixrayss           C/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_depth_anything_3.py_da3_unprojectr-   1   s    ;DAqQemELAAAAQemELAAAA>!Q...DAq
+q!U_Q//0b
9
9
9C<u|'7'7U\8J8J'K'KSQQD%//"%%%%    r&   intr'   c                    t          |          dz  x}}t          j        |d|dz
  dz  gd|| dz
  dz  gg dgt          j                  S )uK   Fallback ~60° FOV pinhole K for mono-mode DA3 (no intrinsics in geometry).gffffff?           g       @)r1   r1         ?r   )floatr   tensorr   )r&   r'   fxfys       r,   _da3_default_Kr9   <   sg    AhhnB<"cAES=1rAES=1(*16@ @ @ @r.   geometrydictbc                    d| v r"| d         d|f                                          S t          j        d                              d           t	          ||          S )zMReturn pixel-space K for batch element b, falling back to a default estimate.
intrinsicsr   comfyus   DA3_GEOMETRY has no intrinsics (mono-mode model). Using a ~60° FOV estimate; 3-D reconstruction may be inaccurate.)r5   logging	getLoggerwarningr9   )r:   r<   r&   r'   s       r,   
_da3_get_KrC   D   sh    x%ad+11333g&&	L   !Qr.   torch.Tensor | Nonec                R    d| vrdS | d         d|f                                          S )zReturn the world-to-camera extrinsic for batch element b, or None in mono mode.

    The model outputs (1, S, 3, 4) [R|t] matrices; the fallback identity is (4, 4).
    _da3_apply_extrinsic handles both shapes via [:3, :3] / [:3, 3] slicing.
    
extrinsicsNr   )r5   )r:   r<   s     r,   _da3_get_extrinsicrG   P   s4     8##tL!!Q$'--///r.   
points_camEc                   |                     | j                                                  }t          j        |                                          s)t          j        d                              d           | S | j	        \  }}}|ddddf         }|dddf         }|j
        }||z   }|                     dd          }	|	|j
        z  |z   }
|
                    ||d          S )z<Transform (H,W,3) OpenCV camera-space points to world space.r?   z|DA3 extrinsic matrix contains non-finite values (pose estimation may have failed). Falling back to camera-space coordinates.N   r   )r$   r   r5   r   isfiniteallr@   rA   rB   r   Treshape)rH   rI   r&   r'   _RtR_invt_invpts	pts_worlds              r,   _da3_apply_extrinsicrW   [   s    	Z%%''A>!  "" '""**8	
 	
 	
 GAq!	"1"bqb&	A	"1"a%ACEaiLE


R
#
#Ceg%IQ1%%%r.   confc                @   | j         d         }g }t          |          D ]j}| |         }|                                |                                }}|                    ||k    r||z
  ||z
  z  nt          j        |                     kt          j        |d          S )z'Map raw confidence to [0, 1] per image.r   r   )r   rangeminmaxappendr   r    r   )rX   Bouticc_minc_maxs          r,   _normalize_confidencerd   n   s    
1A
C1XX [ [Guuwwu

EEMMAI%%-00uWXGYGYZZZZ;s""""r.   confidence_thresholdr5   use_sky_maskboolc                    t          j        ||t           j                  }|rd| v r|| d         |         dk     z  }d| v r5|dk    r/t          | d         ||dz                      d         }|||k    z  }|S )z?Build (H,W) bool keep-mask from sky probability and confidence.r4   skyg      ?
confidencer1   r2   r   )r   onesrg   rd   )r:   r<   r&   r'   re   rf   mask	conf_norms           r,   _da3_build_maskrn   y   s    :a%*---D 1))xq)C/0x$83$>$>)(<*@1q5*IJJ1M	y$889Kr.   c                  <    e Zd Zed             Zedd            ZdS )LoadDA3Modelc                   t          j        dddt           j                            dt	          j        d                    t           j                            dg dd	
          gt                                          g          S )Nrp   zLoad Depth Anything 3zmodel/loaders
model_namegeometry_estimation)optionsweight_dtype)defaultfp16bf16fp32rv   )rt   rv   )node_iddisplay_namecategoryinputsoutputs)r	   SchemaComboInputfolder_pathsget_filename_listDA3ModelTypeOutputclss    r,   define_schemazLoadDA3Model.define_schema   s    y"0$ (:;PQQ    "???%   
 "((**+
 
 
 	
r.   r   io.NodeOutputc                   i }|dk    rt           j        |d<   n+|dk    rt           j        |d<   n|dk    rt           j        |d<   t	          j        d|          }t          j                            ||          }t          j
        |          S )Nrw   r   rx   ry   rs   )model_options)r   float16bfloat16r   r   get_full_path_or_raiser?   sdload_diffusion_modelr	   
NodeOutput)r   rr   ru   r   pathmodels         r,   executezLoadDA3Model.execute   s    6!!%*]M'""V##%*^M'""V##%*]M'"23H*UU--d--PP}U###r.   Nr   r   )__name__
__module____qualname__classmethodr   r    r.   r,   rp   rp      sM        
 
 [
& $ $ $ [$ $ $r.   rp   upper_bound_resizeimageprocess_resmethodstrc                p   |j         dk    r|j        d         dk    sJ dt          |j                               |j        \  }}}}t          j        |            | j        j        }t          j                    }	|j        |j        nt          j
        }
g g g }}}t          |          D ]=}|||dz                                |	          }t          j        |||          }|                    |
          }t          j                    5   ||          }ddd           n# 1 swxY w Y   |d	         }t          j        j                            |                    d                                          ||fd
d                              d                                          }|                    |           d|v rt          j        j                            |d                             d                                          ||fd
d                              d                                          }|                    |           d|v rt          j        j                            |d                             d                                          ||fd
d                              d                                          }|                    |           ?t          j        |d          }|rt          j        |d          nd}|rt          j        |d          nd}|||fS )zNRun DA3 on (B,H,W,3), returns depth/conf/sky at original resolution (or None).   r   rK   expected (B,H,W,3) IMAGE; got Nr2   r   r   r4   r   bilinearFsizemodealign_corners
depth_confri   r   r   )ndimr   tuplemmload_model_gpur   diffusion_modelget_torch_devicer   r   r   rZ   r$   da3_preprocesspreprocess_imageno_gradnn
functionalinterpolater%   r5   squeezecpur]   cat)model_patcherr   r   r   r^   r&   r'   rP   	diffusionr   r   depthsconfsskiesr`   singlexr_   depth_lr
depth_full	conf_fullsky_fullr   rj   ri   s                            r,   _run_da3r      sG   :??u{2!3335jV[\a\gVhVh5j5j333JAq!Qm$$$#3I ""F(:IOOEr25EF1XX # #qQw""6**+FTZ[[[DDuD]__ 	 	)A,,C	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 w<X(44q!!''))A5 5 
 
 '!**SSUU 	 	j!!!3+77L!++A..4466aVu 8   gajj  LL###C<<x*66E
$$Q''--//q!fu 7   gajj  LL"""If!$$$E,1;5a((((tJ%*
4%)Eq
!
!
!
!C*c!!s   D%%D)	,D)	c                  l    e Zd Zed             Zedd            Zedd            Zedd            ZdS )	DA3Inferencec                   t          j        dg ddddt                              d          t           j                            d          t           j                            dd	d
ddd          t           j                            dddgdd          t           j                            ddt           j                            dg           t           j                            dt           j                            dg ddd          t           j                            dddgdd          g          g           gt          
                    d!d"#          g$          S )%Nr   )
r   r:   da3depth anything	monocularpointmapri   3dzmetric depth	disparityzRun Depth Anything 3image/geometry estimationzpRun Depth Anything 3 on an image. In multi-view mode each image is treated as a separate view of the same scene.	da3_modelr   
resolutioni     i	     zResolution the model runs at (longest side, multiple of 14).
Lower = faster / less VRAM.
Higher = more detail.
Output is upsampled back to the original size.rv   r[   r\   steptooltipresize_methodr   lower_bound_resizezupper_bound_resize: scale so the longest side = resolution (caps memory, default).
lower_bound_resize: scale so the shortest side = resolution (preserves more detail on tall/wide images, uses more memory).rt   rv   r   r   zmono: single view image (works with any model variant).
multiview: all images processed together for geometric consistency + camera pose (for Small/Base models only).mono	multiviewref_view_strategy)saddle_balancedsaddle_sim_rangefirstmiddler   zWhich view acts as the geometric anchor.
- saddle_balanced: the view most 'average' across all others (best general choice).
- saddle_sim_range: the view most visually distinct from the others.
- first / middle: fixed positional picks.pose_methodcam_decray_posea  How the camera field-of-view is estimated (for Small/Base models only).
- cam_dec: learned from image features.
- ray_pose: derived geometrically from the model's 3D ray output.
Affects perspective correctness of the 3D output. Try both if results look distorted.r   rt   da3_geometryzDictionary of non-normalized tensors.
Always has the keys: depth, image, mode.
Optional keys: sky (for Mono/Metric), confidence (for Small/Base), extrinsics + intrinsics (for multi-view).)r   rz   search_aliasesr{   r|   descriptionr}   r~   )r	   r   r   r   ImageIntr   DynamicComboOptionDA3Geometryr   r   s    r,   r   zDA3Inference.define_schema   s   y" M  M  M/0 K"";//w''\3CTPRI  J J
 9MOc8d  oCU  V V %%f 7E ..vr::..{':D~D~D~  IZ%L ' M M
 }y*>U_h%x ' y y=   &  < ""> <C" D DG(
 (
 (
 (	
r.   r   r   c                   |d         }|dk    r|                      ||||          S |j        j        }|d         }|d         }	|j        d u}
|j        dk    }|
s|st          d|j         d          |dk    r|
st          d	|j         d
          |dk    r|st          d|j         d          |                     |||||	|          S )Nr   r   r   r   dualdptzKmulti-view mode requires Small or Base model. The loaded model (head_type='z') does not support cross-view attention or camera pose estimation. Switch mode to 'mono', or load Small or Base model for mult-view.r   zRpose_method='cam_dec' requires a camera decoder, but the loaded model (head_type='z9') does not have one. Use pose_method='ray_pose' instead.r   zLpose_method='ray_pose' requires a DualDPT head, but the loaded model has a 'z*' head. Use pose_method='cam_dec' instead.)_execute_monor   r   r   	head_type
ValueError_execute_multiview)r   r   r   r   r   r   mode_valr   r   r   has_cam_dechas_dualdpts               r,   r   zDA3Inference.execute  sK   <v$$Yz=QQQ O3	=) !45't3)Y6 	; 	:(2: : :   )##K#6%.%86 6 6  
 *$$[$5 ) 35 5 5   %%uj-{
 
 	
r.   c                $   t          ||||          \  }}}|                                |dd df                                         dd}||                                |d<   ||                                |d<   t          j        |          S )N)r   .rK   r   )r   r   r   ri   rj   )r   
contiguousr   r	   r   )	r   r   r   r   r   r   rj   ri   r:   s	            r,   r   zDA3Inference._execute_mono*  s    !)%
=!Y!Y!Yz3 %%''37^''))
 

 ?!nn..HUO!%/%:%:%<%<H\"}X&&&r.   c                H   |j         dk    r|j        d         dk    sJ dt          |j                               |j        \  }}}	}
t          j        |           |j        j        }t          j                    }|j        |j        nt          j
        }|                    |          }t          j        |||          }|                    |                              d          }|dk    }t          j                    5   ||||	          }d d d            n# 1 swxY w Y   t          j        j                            |d
                                                             d          ||	fdd                              d                                          }d }d|v rzt          j        j                            |d                             d                                          ||	fdd                              d                                          }d|v r]d|v rY|d                                                                         }|d                                                                         }nt          j        d          d                             d|dd                                          }t          j        d          d                             d|dd                                          }|                                |dd df                                         d|                                |                                d}||                                |d<   d|v rt          j        j                            |d                             d                                          ||	fdd                              d                                          }|                                |d<   t5          j        |          S )Nr   r   rK   r   r   r4   r   r   )use_ray_poser   r   r2   r   Fr   ri   rF   r>   )NN.r   )r   r   r   rF   r>   r   rj   )r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r%   r   r   r   r   r5   r   r   eyeexpandcloner   r	   r   )r   r   r   r   r   r   r   Sr&   r'   rP   r   r   r   r   r   r_   r   ri   rF   r>   r:   rX   s                          r,   r   zDA3Inference._execute_multiview9  s   zQ5;r?a#7#7#7AU5;-?-?AA $8#77[
1a
%   K/	$&&#,?#>	EM HHV+A:m\\\DDuD''**#z1]__ 	_ 	_)ALL]^^^C	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ 	_ #//L  **1--QF5 0 
 
 '!**SSUU 	
 C<<(%11E
$$Q''--//q!fu 2   gajj 
 3<3#6#6\*00226688J\*00226688JJ1j188Aq!DDJJLLJ1j188Aq!DDJJLLJ %%''37^''))$//11$//11
 
 ?!nn..HUO38&22L!++A..4466aVu 3   gajj  &*__%6%6H\"}X&&&s   DD"%D"Nr   )r   r   r   r   r   r   r   r   r   r.   r,   r   r      s        )
 )
 [)
V &
 &
 &
 [&
P ' ' ' [' 5' 5' 5' [5' 5' 5'r.   r   c                      e Zd ZdZej                            dg ddd          ej                            ddd	
          gZe	d             Z
e	dd            Zedd            ZdS )	DA3Renderz2Render a visualization from a DA3_GEOMETRY packet.normalization)v2_stylemin_maxrawr  z- v2_style: mean/std normalisation for perceptually balanced results (default).
- min_max: stretches the full depth range to [0, 1] for maximum contrast.
- raw: no scaling,preserves metric units for Metric model.r   apply_sky_clipFzClip sky-region depth to the 99th percentile of foreground depth before normalisation. Requires a sky key in the da3_geometry input (for Mono/Metric models only).rv   r   c                t   t          j        ddddt                              d          t           j                            ddt           j                            d| j                  t           j                            d	| j                  t           j                            d
t           j                            ddd          g          t           j                            dt           j                            ddd          g          g          gt           j        	                                g          S )Nr   zRender Depth Anything 3r   zTRender a depth map, confidence map, or sky mask from Depth Anything 3 geometry data.r   outputz- depth: normalised greyscale depth image.
- depth_colored: depth mapped through the Turbo colormap.
- sky_mask: sky probability in [0, 1] (for Mono/Metric models only).
- confidence: normalised depth confidence (for Small/Base models only).r   depth_coloredsky_maskcoloredFz)Apply the Turbo colormap to the sky mask.r  rj   z/Apply the Turbo colormap to the confidence map.r   )rz   r{   r|   r   r}   r~   )
r	   r   r   r   r   r   _DEPTH_RENDER_INPUTSBooleanr   r   r   s    r,   r   zDA3Render.define_schema  s   y20n!!.11%%hb
 O**7C4LMMO**?C<TUUO**:
((EKv(ww8   O**<
((EK|(}}:  	 &  $ X__&&'/
 
 
 	
r.   r   r   c                  	
 |d         }|dv r|d         }|d         }|rd|vrt          d          |d         	|                    d          
|r=
;t          j        	
fdt	          	j        d	                   D             d	
          	|                     	
|          }|dk    rt          |d                   n|}n|dk    rmd|vrt          d          |d         
|d         rt          
          }n 
                    d          j	        g 
j        dR  
                                }n|dk    rzd|vrt          d          t          |d                   }|d         rt          |          }nL |                    d          j	        g |j        dR  
                                }nt          d|           t          j        |                                          S )Nr  )r   r  r   r  ri   zapply_sky_clip=True requires a sky tensor in the da3_geometry input, but none is present. Run with Mono/Metric models or set apply_sky_clip=False.r   c                R    g | ]#}t          j        |         |                   $S r   )r   apply_sky_aware_clip).0r`   r   ri   s     r,   
<listcomp>z%DA3Render.execute.<locals>.<listcomp>  s>     % % % #7a#a&II% % %r.   r   r   r  ).r   r	  z8geometry has no sky output; run with Mono/Metric models.r
  r   rK   rj   zBda3_geometry has no confidence output; run with Small/Base models.zUnknown output mode: )r   getr   r   rZ   r   _depth_to_image_turbor%   r   r   rd   r	   r   r5   )r   r   r  
output_valr   r  greyresultrX   r   ri   s            @@r,   r   zDA3Render.execute  sS   H%
333"?3M#$45N %|";"; O   !)E""5))C #/ % % % % %"5;q>22% % %    &&uc=AAD-7?-J-JVDL)))PTFF:%%L(( ![\\\u%Ci  N1r**1@39@a@@@KKMM<''<// !efff(l)CDDDi  P2++2BDJBBBBMMOO AZAABBB}V\\^^,,,r.   r   r   sky_for_normrD   r   c                ~     j         d         }|dk    r1t          j         fdt          |          D             d          }n|dk    rt	          j                   }n }|                    d                              dddd          }|d	k    r|                    d
d          }|	                                S )z6Normalise depth and pack as an (B,H,W,3) image tensor.r   r  c                Z    g | ]'}t          j        |         |         nd           (S N)r   normalize_depth_v2_style)r  r`   r   r  s     r,   r  z-DA3Render._depth_to_image.<locals>.<listcomp>  sP            7!H1Il1ootU U     r.   r   r  r   r2   rK   r  r1   r3   )
r   r   r   rZ   r   normalize_depth_min_maxr%   repeatclampr   )r   r  r   Nnormr_   s   ``    r,   r  zDA3Render._depth_to_image  s     KNJ&&;           q      	  DD
 i''!9%@@DDDnnR  ''1a33E!!))C%%C~~r.   Nr   )r   r   r  rD   r   r   r   r   )r   r   r   __doc__r	   r   r   r  r  r   r   r   staticmethodr  r   r.   r,   r   r   r  s        << 	222M 	 	N 	N 	
)5^ 	 	_ 	_
 
 
 [
6 *- *- *- [*-X       \     r.   r   c                  @    e Zd ZdZed             Zedd            ZdS )DA3GeometryToMeshzXConvert a DA3_GEOMETRY packet into a Types.MESH by unprojecting depth and triangulating.c                H   t          j        dg ddddt                              d          t           j                            dddd	d
          t           j                            ddddd          t           j                            dddddd          t           j                            dddddd          t           j                            ddd          t           j                            ddd          gt           j                                        g           S )!Nr%  )r   r   meshr:   r   triangulatezConvert DA3 Geometry to Meshr   z0Convert a depth map into a triangulated 3D mesh.r   batch_indexr      z_Which image of a batch to convert. Per-image vertex counts differ so batches cannot be stacked.rv   r[   r\   r   
decimationr2      z2Vertex stride. 1 = full resolution, 2 = half, etc.discontinuity_thresholdg{Gz?r1   r3   {Gz?zCDrop triangles whose 3x3 depth span exceeds this fraction. 0 = off.r   re   皙?zExclude pixels whose per-image normalised confidence is below this value (0 = keep all, 1 = keep only the single most confident pixel). Used when the geometry has a confidence map (Small/Base models).rf   TzuExclude sky-probability pixels (sky >= 0.5) from the mesh. Used when the geometry has a sky map (Mono/Metric models).r  texturez-Use the source image as a base color texture.r   )	r	   r   r   r   r   Floatr  Meshr   r   s    r,   r   zDA3GeometryToMesh.define_schema  s@   y']]]70J!!.11]A1$  Qr  s  s\1!  MA  B  B8$CUX_c  ns  t  t5sQT[_[  \ \ 
    H   @  @
  DBq rr
 W^^%%&#
 
 
 	
r.   r   r   c                   |d         }|j         d         }	||	k    rt          d| d|	 d          ||         }
|
j         \  }}|
                                }
t          j        |
                                                                           }|r;t          j        d          	                    d| d| d	d
|z  ||z  z  dd           d|
t          j        |
           <   t          j        d          
                    d| d|
                                dd|
                                dd|
                                d           t          ||||          }t          |
|          }t!          ||          }|t#          ||          }t%          ||||||          }|||         dk    z  t          j        ||                   z  }|                                }t'          d          || <   t)          ||||
          \  }}}|j         d         dk    s|j         d         dk    rt          d          |t          j        g d|j                  z  }|d d g df                                         }|r|d         ||dz            nd }t1          j        |                    d          |                    d          |                    d          |          }t7          j        |          S )Nr   r   batch_index . is out of range; DA3_GEOMETRY has batch size .r?   zDA3GeometryToMesh: depth[z] has z non-finite pixels (d   z.1fz%) - zeroed before unproject.r1   z	] range [z.4gz, z], mean=inf)r,  r.  r   zDA3GeometryToMesh produced an empty mesh. Try raising discontinuity_threshold, lowering confidence_threshold, or disabling use_sky_mask.)r3         r:  r4   )r      r2   r   r2   )verticesfacesuvsr1  )r   r   r   r   rL   sumitemr@   rA   rB   debugr[   r\   meanrC   r-   rG   rW   rn   r5   r
   r6   r   r   r   MESHr%   r	   r   )r   r   r)  r,  r.  re   rf   r1  	depth_allr^   r   r&   r'   n_badr   pointsrI   rl   vertsr=  r>  texr'  s                          r,   r   zDA3GeometryToMesh.execute  so    )	OA!kKkkghkkklll+&{1
 .''',,..3355 	g&&..GK G Gu G GIqsOFG G G   ),u~e$$$%'""((O O O		NO O#(99;;NO O<AJJLLNO O	
 	
 	

 |[!Q77q)) |[99=)&!44F |[!Q@TVbccy-12U^IkDZ5[5[[eu1!$;	
 
 
uc ;q>Q%+a.A"5"5-   %6%6%6ekJJJJaaal#..00DKUl7#Ka$?@@QUz__Q''//!$$a  	
 
 
 }T"""r.   Nr   r   r   r   r"  r   r   r   r   r.   r,   r%  r%    sY        bb
 
 [
* A# A# A# [A# A# A#r.   r%  c                  @    e Zd ZdZed             Zedd            ZdS )DA3GeometryToPointCloudzCUnproject a DA3_GEOMETRY depth map into a filtered DA3_POINT_CLOUD.c                   t          j        dg ddddt                              d          t           j                            dddd	d
          t           j                            dddddd          t           j                            ddd          t           j                            ddddd          gt                              d          g          S )NrK  )r   r   zpoint cloud
pointcloudr   r:   z#Convert DA3 Geometry to Point Cloudr   z*Convert a depth map into a 3D point cloud.r   r)  r   r*  z"Which image of a batch to convert.r+  re   r0  r1   r3   r/  zExclude pixels whose per-image normalised confidence is below this value (0 = keep all). Used when the geometry has a confidence map (Small/Base models).r   rf   TzgExclude sky-probability pixels (sky >= 0.5). Used when the geometry has a sky map (Mono/Metric models).r  
downsampler2      zbTake every Nth pixel (1 = full resolution). Higher values give fewer points and faster processing.point_cloud)r{   r   )	r	   r   r   r   r   r2  r  DA3PointCloudr   r   s    r,   r   z%DA3GeometryToPointCloud.define_schemaA  s    y-ccc>0D!!.11]A1$Ptuu5sQT[_ x  y y
   F ! G G\1! A  B B	 #))})EEF#
 
 
 	
r.   r   r   c                Z   |d         }|j         d         }||k    rt          d| d| d          ||                                         }d|t          j        |           <   |j         \  }	}
t          |||	|
          }|dk    r^|d d |d d |f                                         }|                                }|dd d fxx         |z  cc<   |dd d fxx         |z  cc<   |j         \  }}t          ||          }t          ||          }|t          ||          }t          |||	|
||          }|dk    r|d d |d d |f         }|t          j        |          z  }|                                }|dxx         d	z  cc<   |d
xx         d	z  cc<   |                    dd          |                    d                   }d }d|v rS|d         |         }|dk    r|d d |d d |f         }|                    dd          |                    d                   }d }d|v rR|d         |         }|dk    r|d d |d d |f         }|                    d          |                    d                   }|j         d         dk    rt          d          t          j        |||d          S )Nr   r   r5  r6  r7  r1   r2   ).r2   r:  ).r;  r   rK   r   rj   zzDA3GeometryToPointCloud produced zero points after filtering. Try lowering confidence_threshold or disabling use_sky_mask.)rF  colorsrj   )r   r   r   r   rL   rC   r   r-   rG   rW   rn   rO   r	   r   )r   r   r)  re   rf   rN  rD  r^   r   r&   r'   r   H_dsW_dsrF  rI   rl   points_gltfpts_flatcolors_flatimg	conf_flatrX   s                          r,   r   zDA3GeometryToPointCloud.executeW  s(    )	OA!kKkkghkkklll+&,,..(+u~e$$$%{1|[!Q77>>,,J,*45@@BBE		AadGGGz!GGGadGGGz!GGG[
dq)) |[99=)&!44F |[!Q@TVbcc>>*ll
l23DenU+++ llnnFt#Ft#&&r1--dll2.>.>?l""w'4CA~~,,J,*45++b!,,T\\"-=-=>K	<''-k:DA~~LLjL,,J,67R((b)9)9:I>!!!O  
 }!#
 
   	r.   Nr   rI  r   r.   r,   rK  rK  >  sY        MM
 
 [
* A A A [A A Ar.   rK  c                  &    e Zd Zedd            ZdS )DA3Extensionr   list[type[io.ComfyNode]]c                :   K   t           t          t          t          gS r  )rp   r   r   r%  )selfs    r,   get_node_listzDA3Extension.get_node_list  s       	
 	
r.   N)r   r]  )r   r   r   r   r`  r   r.   r,   r\  r\    s2        
 
 
 X
 
 
r.   r\  c                 "   K   t                      S r  )r\  r   r.   r,   comfy_entrypointrb    s      >>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   rD   )rH   r   rI   r   r   r   )rX   r   r   r   )r:   r;   r<   r/   r&   r/   r'   r/   re   r5   rf   rg   r   r   )r   )r   r   r   r/   r   r   )r   r\  )-r"  
__future__r   r@   typing_extensionsr   r   comfy.model_managementmodel_managementr   comfy.sdr?   r   comfy.ldm.colormapr   r  comfy.ldm.depth_anything_3r   r   comfy_api.latestr   r   r	   comfy.ldm.moge.geometryr
   Customr   r   rQ  r-   r9   rC   rG   rW   rd   rn   	ComfyNoderp   r   r   r   r%  rK  r\  rb  r   r.   r,   <module>rn     s    # " " " " "  & & & & & &  # # # # # #      . . . . . . C C C C C C 6 6 6 6 6 6 6 6 6 6 9 9 9 9 9 9ry%%bi''	+,,*& & & &@ @ @ @	  	  	  	 0 0 0 0& & & &&# # # #   !$ !$ !$ !$ !$2< !$ !$ !$H)" )" )" )" )"X[' [' [' [' ['2< [' [' ['|k  k  k  k  k  k  k  k \[# [# [# [# [# [# [# [#|[ [ [ [ [bl [ [ [|	
 	
 	
 	
 	
> 	
 	
 	
     r.   