Ë
    :vžjâ:  ã            	       ó�  — U d Z ddlmZmZ ddlmZmZmZmZ ddl	Z
ddlmZ ddlmZ ddlmZ ed	   Zeed
<    G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Zeegef   Zeed<   edeeef   f   Zeed<   i Zeeeeef   f   ed<   dededdfd„Zdededeegef   fd„Zd ee   dedefd!„Z dd"œd eeef   dz  ded#edz  defd$„Z! ed%d&¬'«      d(edeeef   dz  fd)„«       Z" ed*d+¬'«      d(ede#dz  fd,„«       Z$d-Z%d.Z&d/Z' ed0d1¬'«      d(eddfd2„«       Z( ed0d3¬'«      d(ede)dz  fd4„«       Z*y)5z±
gltf_extensions.py
------------------

Extension registry for glTF import/export with scope-based handlers.
Each scope has a TypedDict defining the context passed to handlers.
é    )ÚCallableÚIterable)ÚAnyÚLiteralÚ	TypeAliasÚ	TypedDictNé   )Úlog)ÚIndexedDict)ÚNDArray)ÚmaterialÚtexture_sourceÚ	primitiveÚprimitive_preprocessÚprimitive_exportÚScopec                   óN   — e Zd ZU dZeeef   ed<   edeeef   f   ed<   e	ed<   y)ÚMaterialContextz$Context for material scope handlers.Údata.Úparse_texturesÚimagesN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚdictÚstrr   Ú__annotations__r   Úlist© ó    úq/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/exchange/gltf/extensions.pyr   r   4   s0   … Ù.à
ˆs�Cˆx‰.ÓØ˜S $ s¨C x¡.Ð0Ñ1Ó1Ø„Lr!   r   c                   ó&   — e Zd ZU dZeeef   ed<   y)ÚTextureSourceContextz*Context for texture_source scope handlers.r   N)r   r   r   r   r   r   r   r   r    r!   r"   r$   r$   <   s   … Ù4à
ˆs�Cˆx‰.Ôr!   r$   c                   óD   — e Zd ZU dZeeef   ed<   eed<   eed<   eed<   y)ÚPrimitiveContextz1Context for primitive scope handlers (post-load).r   r   Úmesh_kwargsÚ	accessorsN©	r   r   r   r   r   r   r   r   r   r    r!   r"   r&   r&   B   s#   … Ù;à
ˆs�Cˆx‰.ÓØƒOØÓØ„Or!   r&   c                   óD   — e Zd ZU dZeeef   ed<   eed<   eed<   eed<   y)ÚPrimitivePreprocessContextz;Context for primitive_preprocess scope handlers (pre-load).r   r   r(   ÚviewsNr)   r    r!   r"   r+   r+   K   s"   … ÙEà
ˆs�Cˆx‰.ÓØƒOØƒOØ„Kr!   r+   c                   óX   — e Zd ZU dZeed<   eed<   eed<   eed<   eed<   ee	e
f   ed<   y)	ÚPrimitiveExportContextz<Context for primitive_export scope handlers (during export).ÚmeshÚnameÚtreeÚbuffer_itemsr   ÚarraysN)r   r   r   r   r   r   r   r   r   Úintr   r    r!   r"   r.   r.   T   s0   … ÙFà
ƒIØ
ƒIØ
ƒJàÓØƒOð
 ��g�ÑÔr!   r.   ÚHandler.ÚParseTexturesÚ	_handlersÚtargetÚsourceÚreturnc                 ó¬   — |j                  «       D ]A  \  }}t        |t        «      r'|| v r#t        | |   t        «      rt        | |   |«       Œ=|| |<   ŒC y)z´
    Recursively merge source dict into target dict.

    Parameters
    ----------
    target
      Dict to merge into (modified in place)
    source
      Dict to merge from
    N)ÚitemsÚ
isinstancer   Ú_deep_merge)r8   r9   ÚkeyÚvalues       r"   r>   r>   o   sO   € ð —l‘l–n‰
ˆˆUÜ�eœTÔ" s¨f¡}¼ÀFÈ3ÁKÔQUÔ9Vä˜˜s™ UÕ+ð  ˆF�3ŠKñ %r!   r0   Úscopec                 óR   ‡ ‡— ‰t         vr	i t         ‰<   dt        dt        fˆ ˆfd„}|S )ax  
    Decorator to register a handler for a glTF extension.

    Parameters
    ----------
    name
      Extension name, e.g. "KHR_materials_pbrSpecularGlossiness".
    scope
      Handler scope, e.g. "material", "texture_source", "primitive".

    Returns
    -------
    decorator
      Function that registers the handler and returns it unchanged.

    Example
    -------
    >>> @register_handler("MY_extension", scope="material")
    ... def my_handler(context: MaterialContext) -> dict | None:
    ...     data = context["data"]
    ...     images = context["images"]
    ...     return {"baseColorFactor": [1, 0, 0, 1]}
    Úfuncr:   c                 ó    •— | t         ‰   ‰<   | S )N)r7   )rC   r0   rA   s    €€r"   Ú	decoratorz#register_handler.<locals>.decoratorž   s   ø€ Ø!%Œ	�%Ñ˜ÑØˆr!   )r7   r5   )r0   rA   rE   s   `` r"   Úregister_handlerrF   ƒ   s2   ù€ ð0 ”IÑØŒ	�%Ñðœð ¤Gö ð Ðr!   Ú
extensionsc                 ób   — t        | «      t        j                  |i «      j                  «       z
  S )aK  
    Find extension names with no registered handler for a scope.

    Parameters
    ----------
    extensions
      Extension names, i.e. the keys of a glTF "extensions" dict.
    scope
      Handler scope to check against.

    Returns
    -------
    missing
      Extension names with no handler registered for the scope.
    )Úsetr7   ÚgetÚkeys)rG   rA   s     r"   ÚunregisteredrL   ¥   s'   € ô  ˆz‹?œYŸ]™]¨5°"Ó5×:Ñ:Ó<Ñ<Ð<r!   )ÚfailedrM   c                 ó´  — | r|t         vr|j                  d«      si S dS i }| j                  «       D ]0  \  }}|t         |   vrŒ	 d|i|¥}t        |   |   |«      x}�|||<   Œ2 |j                  d«      r#t        t        |j                  «       «      d«      S |dk(  rud|v rq|d   }
|j                  «       D ]Y  }t        |t        «      sŒ|j                  «       D ]3  \  }}t        |t        «      r||
vri |
|<   t        |
|   |«       Œ/||
|<   Œ5 Œ[ |S # t        $ r9}	|�|j	                  |«       t        j                  d|› d|	› �«       Y d}	~	�Œ d}	~	ww xY w)aÇ  
    Process extensions dict for a given scope, calling registered handlers.

    Parameters
    ----------
    extensions
      The "extensions" dict from a glTF element, or None.
    scope
      Handler scope to invoke.
    failed
      If passed the name of any extension whose handler raised is added here.
    **kwargs
      Scope-specific arguments that will be combined with extension data
      into a typed context dict. Required kwargs by scope:
        - material: parse_textures, images
        - texture_source: (none)
        - primitive: primitive, mesh_kwargs, accessors
        - primitive_preprocess: primitive, accessors, views
        - primitive_export: mesh, name, tree, buffer_items, primitive, arrays

    Returns
    -------
    results
      Dict of {extension_name: result} for most scopes.
      For scopes ending in "_source", returns first non-None result.
      For "primitive" scope, automatically merges results into mesh_kwargs.
    Ú_sourceNr   zfailed to process extension z: r   r'   )r7   Úendswithr<   Ú	ExceptionÚaddr
   ÚwarningÚnextÚiterÚvaluesr=   r   r>   )rG   rA   rM   ÚkwargsÚresultsÚext_namer   ÚcontextÚresultÚer'   Ú
ext_resultr?   r@   s                 r"   Úhandle_extensionsr^   ¸   s�  € ñD ˜¤iÑ/ØŸ™¨	Ô2ˆrÐ<¸Ð<à€GØ$×*Ñ*Ö,‰ˆ�$Øœ9 UÑ+Ñ+Øð	Hà˜tÐ. vÐ.ˆGÜ# EÑ*¨8Ñ4°WÓ=Ð=�ÐJØ$*�˜Ñ!øð -ð ‡~�~�iÔ Ü”D˜Ÿ™Ó)Ó*¨DÓ1Ð1ð �Ò °Ñ 7Ø˜]Ñ+ˆØ!Ÿ.™.Ö*ˆJÜ˜j¬$Ô/Øà(×.Ñ.Ö0‘
��UÜ˜e¤TÔ*Ø +Ñ-Ø+-˜ CÑ(Ü ¨CÑ 0°%Õ8à',�K Ò$ñ 1ð	 +ð €Nøô1 ò 	HØÐ!Ø—
‘
˜8Ô$Ü�K‰KÐ6°x°jÀÀ1À#ÐF×GÒGûð	Hús   ÁDÄ	EÄ.EÅEÚ#KHR_materials_pbrSpecularGlossinessr   )rA   rZ   c                 ó†   — 	 ddl m}  |d
i  | d   | d   ¬«      ¤ŽS # t        $ r t        j                  dd¬«       Y y	w xY w)a'  
    Convert specular-glossiness material to PBR metallic-roughness.

    Parameters
    ----------
    context
      MaterialContext with extension data, parse_textures function, and images.

    Returns
    -------
    pbr_dict
      PBR metallic-roughness parameters, or None on failure.
    r	   )Úspecular_to_pbrr   r   )r   z%failed to convert specular-glossinessT)Úexc_infoNr    )Úvisual.glossra   rQ   r
   Údebug)rZ   ra   s     r"   Ú_specular_glossinessre     sK   € ðÝ3áÑQÐ!: Ð)9Ñ!:ÀÈÁÔ!PÑQÐQøÜò Ü�	‰	Ð9ÀDÕIÙðús   ‚ � A ¿A ÚEXT_texture_webpr   c                 ó*   — | d   j                  d«      S )zû
    Return image source index from EXT_texture_webp.

    Parameters
    ----------
    context
      TextureSourceContext with extension data.

    Returns
    -------
    source_index
      Index into glTF images array, or None if not present.
    r   r9   )rJ   )rZ   s    r"   Ú_texture_webp_sourcerh     s   € ð �6‰?×Ñ˜xÓ(Ð(r!   ))ÚCOLOR_0ÚcolorsÚCOLOR)Ú
TEXCOORD_0Ú	tex_coordÚ	TEX_COORD)ÚNORMALÚnormalsro   é   é   ÚKHR_draco_mesh_compressionr   c                 ó˜  — ddl }| d   }| d   }| d   j                  di «      }|j                  | d   |d      «      }|d   j                  «       D ��ci c]  \  }}||“Œ
 }}}|j                  D ]&  }	|j                  |	d	   «      }||v sŒ|	d   |||   <   Œ( | d   j                  d
«      }
t        |dd«      }|
�	|�|||
<   yyyc c}}w )aû  
    Replace a primitive's placeholder accessors with decompressed draco data.

    The accessors of a draco-compressed primitive have no `bufferView`, so the
    loader filled them with zeros before calling us. All of the geometry is in
    a single opaque blob, and the extension carries the indirection we need to
    unpack it: a mapping of glTF attribute name to draco attribute id.

    Parameters
    ----------
    context
      PrimitivePreprocessContext, whose `accessors` we mutate in-place.
    r   Nr   r(   r   Ú
attributesr,   Ú
bufferViewÚ	unique_idÚindicesÚfaces)ÚDracoPyrJ   Údecoder<   ru   Úgetattr)rZ   rz   r   r(   ru   Údecodedr0   ÚidentÚnamesÚattrrx   ry   s               r"   Údraco_decoder�   @  s  € ó à�6‰?€DØ˜Ñ$€IØ˜Ñ%×)Ñ)¨,¸Ó;€Jð �n‰n˜W WÑ-¨d°<Ñ.@ÑAÓB€Gð -1°Ñ,>×,DÑ,DÔ,FÔGÑ,F™[˜T 5ˆU�D‰[Ð,F€EÑGð ×"Ô"ˆØ�y‰y˜˜kÑ*Ó+ˆØ�:ÒØ*.¨v©,ˆI�j Ñ&Ò'ð #ð �kÑ"×&Ñ& yÓ1€GÜ�G˜W dÓ+€EØÐ˜uÐ0Ø"ˆ	�'Òð  1Ðùó Hs   ÁCr   c           
      ót  — ddl }ddlm} | d   }|d   }| d   }g i }}t        D ]}  \  }}	}
|j	                  |j	                  |«      «      }|€Œ*|j                  ||
f«       |j                  t        j                  k(  r|n|j                  t        j                  «      ||	<   Œ ||d      }||d	      } |j                  d||d
t        t        dœ|¤Ž}|j                  |«      }t        |j                   «      t        |«      k7  s!t        |j"                  «      t        |«      k7  rt%        j&                  d«       y|j(                  D �ci c]  }|d   |d   “Œ }}dg|¢D ��
ci c]  \  }}
||t+        |j,                  |
«         “Œ! }}}
 || d   |«      |dœ|j/                  di «      d<   y
c c}w c c}
}w )a[  
    Compress a primitive's geometry into a single draco buffer.

    Every array in `arrays` is absorbed, so the exporter left their accessors
    with no `bufferView` rather than storing the same data twice. Returning
    `None` makes it store them after all, so a failure here exports the
    primitive uncompressed rather than pointing at accessors full of zeros.

    Parameters
    ----------
    context
      PrimitiveExportContext, whose `primitive` and `buffer_items` we mutate.

    Returns
    -------
    compressed
      True if the geometry is now inside a draco buffer, None if not.
    r   Né   )Ú_buffer_appendr   ru   r3   ÚPOSITIONrx   T)Úpointsry   Úpreserve_orderÚquantization_bitsÚcompression_levelz:draco round trip changed the vertex count, not compressingÚattribute_typerw   )r…   r…   r2   )rv   ru   rG   rs   r    )rz   Ú r„   Ú_draco_optionalrJ   ÚappendÚdtypeÚnpÚuint8ÚastypeÚfloat64ÚencodeÚ_DRACO_QUANTIZATIONÚ_DRACO_COMPRESSIONr{   Úlenr†   ry   r
   rS   ru   r|   ÚAttributeTypeÚ
setdefault)rZ   rz   r„   r   ru   r3   ÚabsorbÚoptionalr0   ÚkeywordÚkindr@   Úpositionrx   ÚbufferÚcheckr€   ÚidsÚabsorbeds                      r"   Údraco_encoder¢   h  sê  € ó( å à˜Ñ$€IØ˜<Ñ(€Jð �XÑ€Fð ˜2ˆH€Fß.Ñˆˆg�tØ—
‘
˜:Ÿ>™>¨$Ó/Ó0ˆØˆ=ØØ�‰�t˜T�lÔ#à%*§[¡[´B·H±HÒ%<™EÀ%Ç,Á,ÌrÏzÉzÓBZˆ�Òð  /ð �j Ñ,Ñ-€HØ�Y˜yÑ)Ñ*€Gð
 ˆW�^‰^ð ØØØÜ-Ü,ñð ñ€Fð �N‰N˜6Ó"€EÜ
ˆ5�<‰<ÓœC ›MÒ)¬S°·±Ó-=ÄÀWÃÒ-MÜ�‰ÐPÔQØð BG×AQÒAQÓ
RÑAQ¸ˆ4Ð Ñ! 4¨Ñ#4Ñ4ÐAQ€CÐ
Rð 4Ð=°fÑ=ôá=‰JˆD�$ð 	ˆc”'˜'×/Ñ/°Ó6Ñ7Ñ7Ø=ð ñ ñ % W¨^Ñ%<¸fÓEØñL€I×Ñ˜ rÓ*Ð+GÑHð
 ùò Sùós   ÅF/Å%$F4)+r   Úcollections.abcr   r   Útypingr   r   r   r   Únumpyr�   Ú	constantsr
   Ú	iterationr   Útypedr   r   r   r   r$   r&   r+   r.   r5   r   r   r6   r7   r>   rF   rI   rL   r^   re   r4   rh   rŒ   r•   r”   r�   Úboolr¢   r    r!   r"   Ú<module>rª      sO  ðò÷ /ß 5Ó 5ã å Ý $Ý ð ØYñ€€yó ô8�iô ô˜9ô ô�yô ô ô ô˜Yô ð" ˜s˜e S˜jÑ)€ˆÓ )ð $ C¨¨c°3¨h©Ð$7Ñ8€ˆyÓ 8ð ,.€	ˆ4��T˜#˜w˜,Ñ'Ð'Ñ(Ó -ð ˜ð   dð  ¨tó  ð(˜3ð  uð °¸7¸)ÀWÐ:LÑ1Mó ðD=˜X c™]ð =°5ð =¸Só =ð. ò	Fà�S˜#�X‘ Ñ%ðFð ðFð �$‰Jð	Fð 	óFñ\ Ð7¸zÔJð /ð °d¸3À¸8±nÀtÑ6Kò ó Kðñ. Ð$Ð,<Ô=ð)Ð"6ð )¸3À¹:ò )ó >ð)ð&€ð Ð ð Ð ñ Ð.Ð6LÔMð$#Ð4ð $#¸ò $#ó Nð$#ñN Ð.Ð6HÔIðPÐ0ð P°T¸D±[ò Pó JñPr!   