
    +jz                    6   S r SSKJr  SSKrSSKJr  SSKrSSKJr	  SSK
rSSKrSSKJr  SSKJr  SSKJrJrJr  SSKJr  \R2                  " S	5      r\R2                  " S
5      r\R2                  " S5      rS!S jrS"S jrS#S jrS$S jr S%S jr!S&S jr"S'S jr# " S S\RH                  5      r%S(S)S jjr& " S S\RH                  5      r' " S S\RH                  5      r( " S S\RH                  5      r) " S S\RH                  5      r* " S S\5      r+S*S  jr,g)+ap  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_CLOUDc                   U R                   u  p#[        R                  " U[        R                  U R                  S9n[        R                  " U[        R                  U R                  S9n[        R
                  " XESS9u  pE[        R                  " XE[        R                  " U5      /SS9n[        R                  " S[        R                  R                  UR                  U R                  5      5      U5      nXpR                  S5      -  $ )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)depthKHWuvpixrayss           C/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_depth_anything_3.py_da3_unprojectr+   1   s    ;;DAQemmELLAAQemmELLAA>>!.DA
++qU__Q/0b
9C<<u||'7'7U\\8J'KSQD//"%%%    c                    [        U5      S-  =p#[        R                  " USUS-
  S-  /SX0S-
  S-  // SQ/[        R                  S9$ )uK   Fallback ~60° FOV pinhole K for mono-mode DA3 (no intrinsics in geometry).gffffff?           g       @)r.   r.         ?r   )floatr   tensorr   )r$   r%   fxfys       r*   _da3_default_Kr6   <   sU    AhnB<<"cAES=1rES=1(*16@ @r,   c                    SU ;   a  U S   SU4   R                  5       $ [        R                  " S5      R                  S5        [	        X#5      $ )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.)r2   logging	getLoggerwarningr6   )geometrybr$   r%   s       r*   
_da3_get_Kr?   D   sS    x%ad+1133g&&	L !r,   c                @    SU ;  a  gU S   SU4   R                  5       $ )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   )r2   )r=   r>   s     r*   _da3_get_extrinsicrB   P   s,     8#L!!Q$'--//r,   c                   UR                  U R                  5      R                  5       n[        R                  " U5      R                  5       (       d'  [        R                  " S5      R                  S5        U $ U R                  u  p#nUSS2SS24   nUSS2S4   nUR                  nXv-  * nU R                  SS5      n	XR                  -  U-   n
U
R                  X#S5      $ )z<Transform (H,W,3) OpenCV camera-space points to world space.r9   z|DA3 extrinsic matrix contains non-finite values (pose estimation may have failed). Falling back to camera-space coordinates.N   r   )r    r   r2   r   isfiniteallr:   r;   r<   r   Treshape)
points_camEr$   r%   _RtR_invt_invpts	pts_worlds              r*   _da3_apply_extrinsicrR   [   s    	Z%%'A>>!  ""'"**8	
 GA!	"1"bqb&	A	"1"a%ACCEiLE


R
#Cgg%IQ1%%r,   c                   U R                   S   n/ n[        U5       HY  nX   nUR                  5       UR                  5       peUR	                  Xe:  a	  XE-
  Xe-
  -  O[
        R                  " U5      5        M[     [
        R                  " USS9$ )z'Map raw confidence to [0, 1] per image.r   r   )r   rangeminmaxappendr   r   r   )confBouticc_minc_maxs          r*   _normalize_confidencer_   n   st    

1A
C1XGuuwu

EMAI%-0uWXGYZ  ;;s""r,   c                    [         R                  " X#[         R                  S9nU(       a  SU ;   a  X`S   U   S:  -  nSU ;   a#  US:  a  [        U S   XS-    5      S   nXgU:  -  nU$ )z?Build (H,W) bool keep-mask from sky probability and confidence.r1   skyg      ?
confidencer.   r/   r   )r   onesboolr_   )r=   r>   r$   r%   confidence_thresholduse_sky_maskmask	conf_norms           r*   _da3_build_maskri   y   sv    ::a%**-D)q)C/0x$83$>)(<*@q5*IJ1M	$889Kr,   c                  8    \ rS rSr\S 5       r\SS j5       rSrg)LoadDA3Model   c                    [         R                  " SSS[         R                  R                  S[        R
                  " S5      S9[         R                  R                  S/ SQS	S
9/[        R                  5       /S9$ )Nrk   zLoad Depth Anything 3zmodel/loaders
model_namegeometry_estimation)optionsweight_dtype)defaultfp16bf16fp32rr   )rp   rr   )node_iddisplay_namecategoryinputsoutputs)r	   SchemaComboInputfolder_pathsget_filename_listDA3ModelTypeOutputclss    r*   define_schemaLoadDA3Model.define_schema   sy    yy"0$ (::;PQ   "?%  
 "((*+
 	
r,   c                4   0 nUS:X  a  [         R                  US'   O3US:X  a  [         R                  US'   OUS:X  a  [         R                  US'   [        R
                  " SU5      n[        R                  R                  XCS9n[        R                  " U5      $ )Nrs   r   rt   ru   ro   )model_options)r   float16bfloat16r   r~   get_full_path_or_raiser9   sdload_diffusion_modelr	   
NodeOutput)r   rn   rq   r   pathmodels         r*   executeLoadDA3Model.execute   s    6!%*]]M'"V#%*^^M'"V#%*]]M'"223H*U--d-P}}U##r,    Nreturnzio.NodeOutput)__name__
__module____qualname____firstlineno__classmethodr   r   __static_attributes__r   r,   r*   rk   rk      s(    
 
& $ $r,   rk   c                .   UR                   S:X  a  UR                  S   S:X  d   S[        UR                  5       35       eUR                  u  pEpg[        R                  " U 5        U R
                  R                  n[        R                  " 5       n	UR                  b  UR                  O[        R                  n
/ / / pn[        U5       GH  nXUS-    R                  U	5      n[        R                  " XUS9nUR                  U
S9n[        R                  " 5          U" U5      nSSS5        WS	   n[        R                   R"                  R%                  UR'                  S5      R)                  5       XV4S
SS9R+                  S5      R-                  5       nUR/                  U5        SU;   ay  [        R                   R"                  R%                  US   R'                  S5      R)                  5       XV4S
SS9R+                  S5      R-                  5       nUR/                  U5        SU;   d  GMg  [        R                   R"                  R%                  US   R'                  S5      R)                  5       XV4S
SS9R+                  S5      R-                  5       nUR/                  U5        GM     [        R0                  " USS9nU(       a  [        R0                  " USS9OSnU(       a  [        R0                  " USS9OSnUUU4$ ! , (       d  f       GN= f)zNRun DA3 on (B,H,W,3), returns depth/conf/sky at original resolution (or None).   r   rD   expected (B,H,W,3) IMAGE; got Nr/   process_resmethodr1   r"   bilinearFsizemodealign_corners
depth_confra   r   r   )ndimr   tuplemmload_model_gpur   diffusion_modelget_torch_devicer   r   r   rT   r    da3_preprocesspreprocess_imageno_gradnn
functionalinterpolater!   r2   squeezecpurW   cat)model_patcherimager   r   rY   r$   r%   rK   	diffusionr   r   depthsconfsskiesr[   singlexrZ   depth_lr
depth_full	conf_fullsky_fullr"   rb   ra   s                            r*   _run_da3r      s   ::?u{{2!3j7UV[\a\g\gVhUi5jj3JA!m$##33I  "F(:IOOEr25F1XQ""6*++FTZ[DDuD]]_A,C  w<XX((44q!'')5 5 
 '!*SSU 	 	j!3++77L!++A.446aVu 8  gaj  LL#C<xx**66E
$$Q'--/qfu 7  gaj  LL"3 6 IIf!$E,15a(tJ%*%))Eq
!C*c!!5 _s   	L
L	c                  `    \ rS rSr\S 5       r\SS j5       r\SS j5       r\SS j5       rSr	g)	DA3Inference   c                   [         R                  " S/ SQSSS[        R                  S5      [         R                  R                  S5      [         R
                  R                  SS	S
SSSS9[         R                  R                  SSS/SSS9[         R                  R                  SS[         R                  R                  S/ 5      [         R                  R                  S[         R                  R                  S/ SQSSS9[         R                  R                  SSS/SSS9/5      /S 9/[        R                  S!S"S#9/S$9$ )%Nr   )
r"   r=   da3depth anything	monocularpointmapra   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.rr   rU   rV   steptooltipresize_methodupper_bound_resize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).rp   rr   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   rp   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   rv   search_aliasesrw   rx   descriptionry   rz   )r	   r{   r   r}   ImageIntr|   DynamicComboOptionDA3Geometryr   r   s    r*   r   DA3Inference.define_schema   sl   yy" M/0 K"";/w'\3CTPRI  J
 9MOc8d  oCU  V %%f 7E ..vr:..{':D~  IZ%L ' M
 }y*>U_h%x ' y=  & < ""> <C" DG(
 (	
r,   c                   US   nUS:X  a  U R                  XX45      $ UR                  R                  nUS   nUS   n	UR                  S Ln
UR                  S:H  nU
(       d   U(       d  [        SUR                   S35      eUS:X  a   U
(       d  [        S	UR                   S
35      eUS:X  a   U(       d  [        SUR                   S35      eU R                  XX4X5      $ )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   DA3Inference.execute  s   <v$$YzQQ OO33	=) !45''t3))Y6;(223 4::  )#K%%.%8%8$9 :66 
 *$[  ) 3 34 555  %%j
 	
r,   c                    [        XX4S9u  pVnUR                  5       USS S24   R                  5       SS.nUb  UR                  5       US'   Ub  UR                  5       US'   [        R                  " U5      $ )N)r   .rD   r   )r"   r   r   ra   rb   )r   
contiguousr   r	   r   )	r   r   r   r   r   r"   rb   ra   r=   s	            r*   r   DA3Inference._execute_mono*  s    !)%
!Y3 %%'37^'')

 ?!nn.HUO!%/%:%:%<H\"}}X&&r,   c                   UR                   S:X  a  UR                  S   S:X  d   S[        UR                  5       35       eUR                  u  pxp[        R                  " U5        UR
                  R                  n[        R                  " 5       nUR                  b  UR                  O[        R                  nUR                  U5      n[        R                  " XUS9nUR                  US9R                  S5      nUS:H  n[        R                  " 5          U" XUS	9nS S S 5        [        R                   R"                  R%                  WS
   R'                  5       R                  S5      X4SSS9R)                  S5      R+                  5       nS nSU;   ah  [        R                   R"                  R%                  US   R                  S5      R'                  5       X4SSS9R)                  S5      R+                  5       nSU;   aI  SU;   aC  US   R'                  5       R+                  5       nUS   R'                  5       R+                  5       nOr[        R,                  " S5      S   R/                  SUSS5      R1                  5       n[        R,                  " S5      S   R/                  SUSS5      R1                  5       nUR3                  5       USS S24   R+                  5       SUR3                  5       UR3                  5       S.nUb  UR3                  5       US'   SU;   a{  [        R                   R"                  R%                  US   R                  S5      R'                  5       X4SSS9R)                  S5      R+                  5       nUR3                  5       US'   [4        R6                  " U5      $ ! , (       d  f       GN= f)Nr   r   rD   r   r   r1   r   r   )use_ray_poser   r"   r/   r   Fr   ra   rA   r8   )NN.r   )r"   r   r   rA   r8   r   rb   )r   r   r   r   r   r   r   r   r   r   r   r    r   r   r!   r   r   r   r   r2   r   r   eyeexpandcloner   r	   r   )r   r   r   r   r   r   r   Sr$   r%   rK   r   r   r   r   r   rZ   r"   ra   rA   r8   r=   rX   s                          r*   r   DA3Inference._execute_multiview9  s:   zzQ5;;r?a#7 	B,U5;;-?,@A	B7[[
a
% KK//	$$&#,??#>	EMM HHV++Am\DDuD''*#z1]]_AL]^C  ##//L **1-QF5 0 
 '!*SSU 	
 C<((%%11E
$$Q'--/qfu 2  gaj 
 3<3#6\*002668J\*002668J1j188Aq!DJJLJ1j188Aq!DJJLJ %%'37^'')$//1$//1
 ?!nn.HUO388&&22L!++A.446aVu 3  gaj  &*__%6H\"}}X&&K _s   N55
Or   Nr   )
r   r   r   r   r   r   r   r   r   r   r   r,   r*   r   r      sR    )
 )
V &
 &
P ' ' 5' 5'r,   r   c                      \ rS rSrSr\R                  R                  S/ SQSSS9\R                  R                  SS	S
S9/r	\
S 5       r\
SS j5       r\SS j5       rSrg)	DA3Renderir  z2Render 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).rr   r   c                j   [         R                  " SSSS[        R                  S5      [         R                  R                  SS[         R                  R                  SU R                  5      [         R                  R                  S	U R                  5      [         R                  R                  S
[         R                  R                  SSSS9/5      [         R                  R                  S[         R                  R                  SSSS9/5      /S9/[         R                  R                  5       /S9$ )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	  rb   z/Apply the Turbo colormap to the confidence map.r   )rv   rw   rx   r   ry   rz   )
r	   r{   r   r}   r   r   _DEPTH_RENDER_INPUTSBooleanr   r   r   s    r*   r   DA3Render.define_schema  s   yy20n!!.1%%hb
 OO**7C4L4LMOO**?C<T<TUOO**:

((EKv(w8  OO**<

((EK|(}: 	 & $ XX__&'/
 	
r,   c           
        US   nUS;   a  US   nUS   nU(       a  SU;  a  [        S5      eUS   nUR                  S5      nU(       aX  UbU  [        R                  " [	        UR
                  S   5       Vs/ s H  n[        R                  " Xh   Xx   5      PM      snSS	9nU R                  XgU5      n	US
:X  a  [        U	S   5      OU	n
OUS:X  ah  SU;  a  [        S5      eUS   nUS   (       a  [        U5      n
OUR                  S5      R                  " / UR
                  QSP76 R                  5       n
OUS:X  aq  SU;  a  [        S5      e[        US   5      nUS   (       a  [        U5      n
OJUR                  S5      R                  " / UR
                  QSP76 R                  5       n
O[        SU 35      e[        R                  " U
R!                  5       5      $ s  snf )Nr  )r"   r  r  r  ra   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"   r   r   r  ).r   r  z8geometry has no sky output; run with Mono/Metric models.r  r   rD   rb   zBda3_geometry has no confidence output; run with Small/Base models.zUnknown output mode: )r   getr   r   rT   r   r   apply_sky_aware_clip_depth_to_image_turbor!   r   r   r_   r	   r   r2   )r   r   r  
output_valr  r  r"   ra   r[   greyresultrX   s               r*   r   DA3Render.execute  s   H%
33"?3M#$45N%|"; O  !)E""5)C#/"5;;q>2%2 #77#&I2%  &&u=AD-7?-JVDL)PTF:%L( ![\\u%Ci r*11@399@a@KKM<'</ !eff(l)CDDi +22BDJJBBMMO 4ZLABB}}V\\^,,9%s   8%G#c           
        U R                   S   nUS:X  aN  [        R                  " [        U5       Vs/ s H#  n[        R
                  " X   Ub  X   OS5      PM%     snSS9nOUS:X  a  [        R                  " U 5      nOU nUR                  S5      R                  SSSS5      nUS	:w  a  UR                  S
S5      nUR                  5       $ s  snf )z6Normalise depth and pack as an (B,H,W,3) image tensor.r   r  Nr   r  r   r/   rD   r  r.   r0   )r   r   r   rT   r   normalize_depth_v2_stylenormalize_depth_min_maxr!   repeatclampr   )r"   sky_for_normr  Nr[   normrZ   s          r*   r  DA3Render._depth_to_image  s     KKNJ&;; q  "A 77H1IlotU!  	D
 i'!99%@DDnnR ''1a3E!))C%C~~ s   *Cr   Nr   )r"   torch.Tensorr   torch.Tensor | Noner  strr   r$  )r   r   r   r   __doc__r	   r|   r}   r  r  r   r   r   staticmethodr  r   r   r,   r*   r  r  r  s    < 	2M 	 	N 	

)5^ 	 	_
 
 
6 *- *-X    r,   r  c                  <    \ rS rSrSr\S 5       r\SS j5       rSrg)DA3GeometryToMeshi  zXConvert a DA3_GEOMETRY packet into a Types.MESH by unprojecting depth and triangulating.c                   [         R                  " S/ SQSSS[        R                  S5      [         R                  R                  SSSS	S
S9[         R                  R                  SSSSSS9[         R
                  R                  SSSSSSS9[         R
                  R                  SSSSSSS9[         R                  R                  SSSS9[         R                  R                  SSSS9/[         R                  R                  5       /S 9$ )!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.rr   rU   rV   r   
decimationr/      z2Vertex stride. 1 = full resolution, 2 = half, etc.discontinuity_thresholdg{Gz?r.   r0   {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   DA3GeometryToMesh.define_schema  s   yy']70J!!.1]A1$  Qr  s\1!  MA  B8$CUX_c  ns  t5sQT[_[  \ 

    H   @

  DBq r
 WW^^%&#
 	
r,   c                F   US   nUR                   S   n	X):  a  [        SU SU	 S35      eX   n
U
R                   u  pU
R                  5       n
[        R                  " U
5      ) R                  5       R                  5       nU(       a8  [        R                  " S5      R                  SU SU S	S
U-  X-  -  S S35        SU
[        R                  " U
5      ) '   [        R                  " S5      R                  SU SU
R                  5       S SU
R                  5       S SU
R                  5       S 35        [        XX5      n[        X5      n[!        X5      nUb  [#        UU5      n[%        XXXV5      nUX   S:  -  [        R                  " X   5      -  nUR                  5       n['        S5      UU) '   [)        UUUU
S9u  nnnUR                   S   S:X  d  UR                   S   S:X  a  [        S5      eU[        R*                  " / SQUR,                  S9-  nUS S 2/ SQ4   R/                  5       nU(       a
  US   X"S-    OS n[0        R2                  " UR5                  S5      UR5                  S5      UR5                  S5      US9n[6        R8                  " U5      $ )Nr"   r   batch_index . is out of range; DA3_GEOMETRY has batch size .r9   zDA3GeometryToMesh: depth[z] has z non-finite pixels (d   z.1fz%) - zeroed before unproject.r.   z	] range [z.4gz, z], mean=inf)r1  r3  r"   zDA3GeometryToMesh produced an empty mesh. Try raising discontinuity_threshold, lowering confidence_threshold, or disabling use_sky_mask.)r0         r@  r1   )r      r/   r   r/   )verticesfacesuvsr6  )r   r   r   r   rE   sumitemr:   r;   r<   debugrU   rV   meanr?   r+   rB   rR   ri   r2   r
   r3   r   r   r   MESHr!   r	   r   )r   r   r.  r1  r3  re   rf   r6  	depth_allrY   r"   r$   r%   n_badr#   pointsrJ   rg   vertsrC  rD  texr,  s                          r*   r   DA3GeometryToMesh.execute  s    )	OOA|K=8fghfiijkll&{{
 ..'',,.335g&..+K=ug FIqsOC((EG ),u~~e$$%'"(('} 5		C 599;s"38EJJL;MO	

 |!7) |9=)&!4F |!@Tcy-12U^^IDZ5[[eu1!$;	
uc ;;q>Q%++a.A"5-  %6ekkJJal#..0DKl7#Ka@QUzz__Q'//!$a 	
 }}T""r,   r   Nr   	r   r   r   r   r'  r   r   r   r   r   r,   r*   r*  r*    s-    b
 
* A# A#r,   r*  c                  <    \ rS rSrSr\S 5       r\SS j5       rSrg)DA3GeometryToPointCloudi>  zCUnproject a DA3_GEOMETRY depth map into a filtered DA3_POINT_CLOUD.c                   [         R                  " S/ SQSSS[        R                  S5      [         R                  R                  SSSS	S
S9[         R
                  R                  SSSSSSS9[         R                  R                  SSSS9[         R                  R                  SSSSSS9/[        R                  SS9/S9$ )NrR  )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.r0  re   r5  r.   r0   r4  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	  
downsampler/      zbTake every Nth pixel (1 = full resolution). Higher values give fewer points and faster processing.point_cloud)rw   r   )	r	   r{   r   r}   r   r7  r  DA3PointCloudr   r   s    r*   r   %DA3GeometryToPointCloud.define_schemaA  s    yy-c>0D!!.1]A1$Ptu5sQT[_ x  y

   F ! G\1! A  B	 #))})EF#
 	
r,   c                   US   nUR                   S   nX':  a  [        SU SU S35      eXb   R                  5       nSU[        R                  " U5      ) '   UR                   u  p[        XX5      nUS:  aM  US S U2S S U24   R                  5       nUR                  5       nUSS S 24==   U-  ss'   USS S 24==   U-  ss'   UR                   u  p[        X5      n[        X5      nUb  [        X5      n[        XXX45      nUS:  a  US S U2S S U24   nU[        R                  " U5      -  nUR                  5       nUS==   S	-  ss'   US
==   S	-  ss'   UR                  SS5      UR                  S5         nS nSU;   a?  US   U   nUS:  a  US S U2S S U24   nUR                  SS5      UR                  S5         nS nSU;   a>  US   U   nUS:  a  US S U2S S U24   nUR                  S5      UR                  S5         nUR                   S   S:X  a  [        S5      e[        R                  " UUUS.5      $ )Nr"   r   r;  r<  r=  r.   r/   ).r/   r@  ).rA  r   rD   r   rb   zzDA3GeometryToPointCloud produced zero points after filtering. Try lowering confidence_threshold or disabling use_sky_mask.)rL  colorsrb   )r   r   r   r   rE   r?   r   r+   rB   rR   ri   rH   r	   r   )r   r   r.  re   rf   rU  rJ  rY   r"   r$   r%   r#   H_dsW_dsrL  rJ   rg   points_gltfpts_flatcolors_flatimg	conf_flatrX   s                          r*   r   DA3GeometryToPointCloud.executeW  s    )	OOA|K=8fghfiijkll&,,.(+u~~e$$%{{|!7>,J,*45@@BE	AadGz!GadGz!G[[
) |9=)&4F |!@Tc>*l
l23DennU++ llnFt#Ft#&&r1-dll2.>?l"w'4CA~,J,*45++b!,T\\"-=>K	<'-k:DA~LjL,J,67R(b)9:I>>!!O 
 }}!#
  	r,   r   Nr   rP  r   r,   r*   rR  rR  >  s-    M
 
* A Ar,   rR  c                  (    \ rS rSr\SS j5       rSrg)DA3Extensioni  c                6   #    [         [        [        [        /$ 7fN)rk   r   r  r*  )selfs    r*   get_node_listDA3Extension.get_node_list  s      	
 	
s   r   N)r   zlist[type[io.ComfyNode]])r   r   r   r   r   ri  r   r   r,   r*   re  re    s    
 
r,   re  c                    #    [        5       $ 7frg  )re  r   r,   r*   comfy_entrypointrl    s     >s   )r"   r$  r#   r$  r   r$  )r$   intr%   rm  r   r$  )
r=   dictr>   rm  r$   rm  r%   rm  r   r$  )r=   rn  r>   rm  r   r%  )rI   r$  rJ   r$  r   r$  )rX   r$  r   r$  )r=   rn  r>   rm  r$   rm  r%   rm  re   r2   rf   rd   r   r$  )r   )r   r$  r   rm  r   r&  )r   re  )-r'  
__future__r   r:   typing_extensionsr   r   comfy.model_managementmodel_managementr   comfy.sdr9   r~   comfy.ldm.colormapr   r  comfy.ldm.depth_anything_3r   r   comfy_api.latestr   r   r	   comfy.ldm.moge.geometryr
   Customr   r   rX  r+   r6   r?   rB   rR   r_   ri   	ComfyNoderk   r   r   r  r*  rR  re  rl  r   r,   r*   <module>rz     s    #  &  #   . C 6 6 9yy%ii'		+,*&@	 0&&#!$2<< !$H)"X['2<< ['|k  k \[# [#|[bll [|	
> 	
r,   