
    Ti                        d Z ddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZ 	 dd	lmZ n"# e$ rZ ej        e          ZY dZ[ndZ[ww xY wd
Z G d dej                  Z G d de          Z G d de          Z G d de          Zddeeej                          defdZddeeej                          fdZ	 	 	 	 ddefdZdS )z?
material.py
-------------

Store visual materials as objects.
    N   )
exceptionsutil)tol)NDArrayOptional   colorImagegh㈵>c                       e Zd Zd Zej        d             Zeej        d                         Zed             Z	e	j
        d             Z	d ZdS )Materialc                      t          d          Nzmust be subclassed!NotImplementedError)selfargskwargss      Q/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/visual/material.py__init__zMaterial.__init__   s    !"7888    c                      t          d          r   r   r   s    r   __hash__zMaterial.__hash__   s    !"7888r   c                     dS z
        The "average" color of this material.

        Returns
        ---------
        color : (4,) uint8
          Average color of this material.
        N r   s    r   
main_colorzMaterial.main_color#         r   c                 4    t          | d          r| j        S dS )N_name
material_0)hasattrr#   r   s    r   namezMaterial.name/   s!    4!! 	:|r   c                     |	|| _         d S d S N)r#   r   values     r   r&   zMaterial.name5   s    DJJJ r   c                 *    t          j        |           S r(   )copydeepcopyr   s    r   r,   zMaterial.copy:   s    }T"""r   N)__name__
__module____qualname__r   abcabstractmethodr   propertyr    r&   setterr,   r   r   r   r   r      s        9 9 9 	9 9 9    X   X
 
[  [# # # # #r   r   c                       e Zd ZdZ	 	 	 	 	 	 ddZd ZddZd Zed             Z	ed             Z
e
j        d	             Z
d
 ZdS )SimpleMaterialz&
    Hold a single image texture.
    Nc                     || _         || _        t          j        |          | _        t          j        |          | _        t          j        |          | _        || _        || _        d S r(   )	imager&   r   to_rgbaambientdiffusespecular
glossinessr   )r   r8   r;   r:   r<   r=   r&   r   s           r   r   zSimpleMaterial.__init__C   s[     
	}W--}W--h// % r   c                 6    t          j        || j                  S r(   )r   uv_to_colorr8   )r   uvs     r   to_colorzSimpleMaterial.to_color[   s     TZ000r   c                    t          j        | j                  dd         }t          j        | j                  dd         }t          j        | j                  dd         }|| j        }d|  dj        |  dj        |  dj        | d| j        dg}i }| j        | j        j        }|d	}| d
|	                                 }|
                    d|            t          j                    }	| j                            |	|           |	                    d           |	                                ||<   d                    |                              d          || d<   ||fS )a  
        Convert the current material to an OBJ format
        material.

        Parameters
        -----------
        name : str or None
          Name to apply to the material

        Returns
        -----------
        tex_name : str
          Name of material
        mtl_name : str
          Name of mtl file in files
        files : dict
          Data as {file name : bytes}
        N   znewmtl zKa {:0.8f} {:0.8f} {:0.8f}zKd {:0.8f} {:0.8f} {:0.8f}zKs {:0.8f} {:0.8f} {:0.8f}zNs z0.8fpng.zmap_Kd )fpformatr   
zutf-8z.mtl)r   to_floatr:   r;   r<   r&   rG   r=   r8   lowerappendr   BytesIOsaveseekreadjoinencode)
r   r&   KaKdKsmtldata
image_type
image_namef_objs
             r   to_objzSimpleMaterial.to_obj^   s   ( ^DL))"1"-^DL))"1"-^DM**2A2.<9D d/(/4/(/4/(/4($/(((
 :!*J!"
 77:#3#3#5#577JJJ---... LNNEJOOuZO888JJqMMM$zz||D"iinn33G<<]]]Tzr   c                 x   t          | j        d          r't          | j                                                  }nd}t          | j        d          r)|t          | j                                                  z  }t          | j        d          r)|t          | j                                                  z  }t          | j        d          r)|t          | j                                                  z  }t          | j        t                    r'|t          t          | j        dz                      z  }|S )z
        Provide a hash of the material so we can detect
        duplicates.

        Returns
        ------------
        hash : int
          Hash of image and parameters
        tobytesr   i  )r%   r8   hashr\   r:   r;   r<   
isinstancer=   floatint)r   hasheds     r   r   zSimpleMaterial.__hash__   s    4:y)) 	$*,,..//FF F 4<++ 	3d4<//11222F4<++ 	3d4<//11222F4=),, 	4d4=0022333Fdou-- 	8d3t566777Fr   c                     | j         S )z2
        Return the most prominent color.
        )r;   r   s    r   r    zSimpleMaterial.main_color   s    
 |r   c                 4    t          | d          r| j        S dS )N_glossiness      ?)r%   rd   r   s    r   r=   zSimpleMaterial.glossiness   s"    4'' 	$##sr   c                 6    |d S t          |          | _        d S r(   )r_   rd   r)   s     r   r=   zSimpleMaterial.glossiness   s     =F <<r   c                 Z    d| j         dz   z  dz  }t          || j        | j                  S )z
        Convert the current simple material to a
        PBR material.

        Returns
        ------------
        pbr : PBRMaterial
          Contains material information in PBR format.
        r   g      ?)roughnessFactorbaseColorTexturebaseColorFactor)r=   PBRMaterialr8   r;   )r   	roughnesss     r   to_pbrzSimpleMaterial.to_pbr   s>     $/A-.I>	%!Z L
 
 
 	
r   )NNNNNNr(   )r.   r/   r0   __doc__r   rA   rZ   r   r3   r    r=   r4   rm   r   r   r   r6   r6   >   s             01 1 18 8 8 8t  :   X   X
 ( ( (

 
 
 
 
r   r6   c                   V    e Zd ZddZd Zd Zd Zd Zd Ze	d             Z
d	 Zd
 ZdS )MultiMaterialNc                 *    |	g | _         dS || _         dS )z
        Wrapper for a list of Materials.

        Parameters
        ----------
        materials : Optional[List[Material]]
            List of materials with which the container to be initialized.
        N	materials)r   rs   r   s      r   r   zMultiMaterial.__init__   s      DNNN&DNNNr   c                 v    d | j         D             }t          |          dk    rt                      S |d         S )z"
        TODO : IMPLEMENT
        c                 <    g | ]}t          |t                    |S r   r^   rk   .0ms     r   
<listcomp>z(MultiMaterial.to_pbr.<locals>.<listcomp>   s'    GGGQJq+,F,FGqGGGr   r   )rs   lenrk   )r   pbrs     r   rm   zMultiMaterial.to_pbr   s<     HG$.GGGs88q==== 1vr   c                 x    t          t          j                            d | j        D                                 S )z
        Provide a hash of the multi material so we can detect
        duplicates.

        Returns
        ------------
        hash : int
          Xor hash of the contained materials.
        c                 ,    g | ]}t          |          S r   )r]   rw   s     r   rz   z*MultiMaterial.__hash__.<locals>.<listcomp>   s    )J)J)Ja$q'')J)J)Jr   )r`   npbitwise_xorreducers   r   s    r   r   zMultiMaterial.__hash__   s3     2>(()J)J4>)J)J)JKKLLLr   c                 *    t          | j                  S r(   )iterrs   r   s    r   __iter__zMultiMaterial.__iter__       DN###r   c                 *    t          | j                  S r(   )nextrs   r   s    r   __next__zMultiMaterial.__next__  r   r   c                 *    t          | j                  S r(   )r{   rs   r   s    r   __len__zMultiMaterial.__len__  s    4>"""r   c                     dS r   r   r   s    r   r    zMultiMaterial.main_color	  r!   r   c                 :    | j                             |           dS )z
        Adds new material to the container.

        Parameters
        ----------
        material : Material
            The material to be added.
        N)rs   rK   )r   materials     r   addzMultiMaterial.add  s      	h'''''r   c                     | j         |         S )z
        Get material by index.

        Parameters
        ----------
        idx : int
            Index of the material to be retrieved.

        Returns
        -------
            The material on the given index.
        rr   )r   idxs     r   getzMultiMaterial.get  s     ~c""r   r(   )r.   r/   r0   r   rm   r   r   r   r   r3   r    r   r   r   r   r   rp   rp      s        ' ' ' '  
M 
M 
M$ $ $$ $ $# # #   X	( 	( 	(# # # # #r   rp   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZed             Zej        d             Zed             Zej        d             Zed	             Z	e	j        d
             Z	ed             Z
e
j        d             Z
ed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zd Zd  Zd! Zed"             Zd# ZdS )%rk   z
    Create a material for physically based rendering as
    specified by GLTF 2.0:
    https://git.io/fhkPZ

    Parameters with `Texture` in them must be PIL.Image objects
    NFc                    i | _         || _        || _        || _        || _        || _        || _        || _        || _        |	| _	        |
| _
        || _        || _        || _        t          |          dk    rYt          j                            d                    d                    |                                                               d S d S )Nr   zunsupported material keys: {}z, )_dataemissiveFactorrj   metallicFactorrh   alphaCutoffnormalTextureemissiveTextureocclusionTextureri   metallicRoughnessTexturedoubleSidedr&   	alphaModer{   r   logdebugrG   rP   keys)r   r&   r   r   rj   r   rh   r   r   ri   r   r   r   r   r   s                  r   r   zPBRMaterial.__init__8  s    $ 
 -. -.& +. 0 0(@% ' 	"v;;??HNN/66tyy7O7OPP     ?r   c                 6    | j                             d          S )ac  
        The factors for the emissive color of the material.
        This value defines linear multipliers for the sampled
        texels of the emissive texture.

        Returns
        -----------
        emissiveFactor : (3,) float
           Ech element in the array MUST be greater than
           or equal to 0 and less than or equal to 1.
        r   r   r   r   s    r   r   zPBRMaterial.emissiveFactori       z~~.///r   c                    || j                             dd            d S t          j        |t          j                                      d          }|                                t           k     rt          d          || j         d<   d S )Nr   dtyperC   z$all factors must be greater than 0.0)	r   popr   arrayfloat64reshapemin_eps
ValueError)r   r*   emissives      r   r   zPBRMaterial.emissiveFactorx  s    =JNN+T22222 xRZ888@@CCH||~~%% !GHHH+3DJ'(((r   c                 6    | j                             d          S )z
        The material alpha rendering mode enumeration
        specifying the interpretation of the alpha value of
        the base color.

        Returns
        -----------
        alphaMode : str
          One of 'OPAQUE', 'MASK', 'BLEND'
        r   r   r   s    r   r   zPBRMaterial.alphaMode  s     z~~k***r   c                     || j                             dd            d S t          |                                                                          }|dvrt          d|          || j         d<   d S )Nr   )OPAQUEMASKBLENDzincorrect alphaMode: %s)r   r   strupperstripr   r)   s     r   r   zPBRMaterial.alphaMode  sw    =JNN;----- JJ$$&&,,..E777 !:EBBB&+DJ{###r   c                 6    | j                             d          S )a$  
        Specifies the cutoff threshold when in MASK alpha mode.
        If the alpha value is greater than or equal to this value
        then it is rendered as fully opaque, otherwise, it is rendered
        as fully transparent. A value greater than 1.0 will render
        the entire material as fully transparent. This value MUST be
        ignored for other alpha modes. When alphaMode is not defined,
        this value MUST NOT be defined.

        Returns
        -----------
        alphaCutoff : float
          Value of cutoff.
        r   r   r   s    r   r   zPBRMaterial.alphaCutoff  s      z~~m,,,r   c                 r    || j                             dd            d S t          |          | j         d<   d S )Nr   r   r   r_   r)   s     r   r   zPBRMaterial.alphaCutoff  s;    =JNN=$/////(-eDJ}%%%r   c                 6    | j                             d          S )z
        Specifies whether the material is double sided.

        Returns
        -----------
        doubleSided : bool
          Specifies whether the material is double sided.
        r   r   r   s    r   r   zPBRMaterial.doubleSided  s     z~~m,,,r   c                 r    || j                             dd            d S t          |          | j         d<   d S )Nr   )r   r   boolr)   s     r   r   zPBRMaterial.doubleSided  s;    =JNN=$/////(,UDJ}%%%r   c                 6    | j                             d          S )a%  
        The factor for the metalness of the material. This value
        defines a linear multiplier for the sampled metalness values
        of the metallic-roughness texture.


        Returns
        -----------
        metallicFactor : float
          How metally is the material
        r   r   r   s    r   r   zPBRMaterial.metallicFactor  r   r   c                 r    || j                             dd            d S t          |          | j         d<   d S )Nr   r   r)   s     r   r   zPBRMaterial.metallicFactor  s=    =JNN+T22222+0<<DJ'(((r   c                 6    | j                             d          S )a   
        The factor for the roughness of the material. This value
        defines a linear multiplier for the sampled roughness values
        of the metallic-roughness texture.

        Returns
        -----------
        roughnessFactor : float
          Roughness of material.
        rh   r   r   s    r   rh   zPBRMaterial.roughnessFactor       z~~/000r   c                 r    || j                             dd            d S t          |          | j         d<   d S )Nrh   r   r)   s     r   rh   zPBRMaterial.roughnessFactor  s=    =JNN,d33333,1%LLDJ()))r   c                 6    | j                             d          S )z
        The factors for the base color of the material. This
        value defines linear multipliers for the sampled texels
        of the base color texture.

        Returns
        ---------
        color : (4,) uint8
          RGBA color
        rj   r   r   s    r   rj   zPBRMaterial.baseColorFactor  r   r   c                 |    || j                             dd            d S t          j        |          | j         d<   d S )Nrj   )r   r   r   r9   r)   s     r   rj   zPBRMaterial.baseColorFactor  sC    =JNN,d33333 -2M%,@,@DJ()))r   c                 6    | j                             d          S )z
        The normal map texture.

        Returns
        ----------
        image : PIL.Image
          Normal texture.
        r   r   r   s    r   r   zPBRMaterial.normalTexture  s     z~~o...r   c                 X    || j                             dd            d S || j         d<   d S Nr   r   r   r)   s     r   r   zPBRMaterial.normalTexture  s5    =JNN?D11111*/DJ'''r   c                 6    | j                             d          S )z
        The emissive texture.

        Returns
        ----------
        image : PIL.Image
          Emissive texture.
        r   r   r   s    r   r   zPBRMaterial.emissiveTexture$  s     z~~/000r   c                 X    || j                             dd            d S || j         d<   d S )Nr   r   r)   s     r   r   zPBRMaterial.emissiveTexture0  s7    =JNN,d33333,1DJ()))r   c                 6    | j                             d          S )z
        The occlusion texture.

        Returns
        ----------
        image : PIL.Image
          Occlusion texture.
        r   r   r   s    r   r   zPBRMaterial.occlusionTexture8       z~~0111r   c                 X    || j                             dd            d S || j         d<   d S )Nr   r   r)   s     r   r   zPBRMaterial.occlusionTextureD  s7    =JNN-t44444-2DJ)***r   c                 6    | j                             d          S )z
        The base color texture image.

        Returns
        ----------
        image : PIL.Image
          Color texture.
        ri   r   r   s    r   ri   zPBRMaterial.baseColorTextureL  r   r   c                 X    || j                             dd            d S || j         d<   d S )Nri   r   r)   s     r   ri   zPBRMaterial.baseColorTextureX  s9    =JNN-t44444 .3DJ)***r   c                 6    | j                             d          S )z
        The metallic-roughness texture.

        Returns
        ----------
        image : PIL.Image
          Metallic-roughness texture.
        r   r   r   s    r   r   z$PBRMaterial.metallicRoughnessTexturea  s     z~~8999r   c                 X    || j                             dd            d S || j         d<   d S )Nr   r   r)   s     r   r   z$PBRMaterial.metallicRoughnessTexturem  s7    =JNN5t<<<<<5:DJ1222r   c                 6    | j                             d          S Nr&   r   r   s    r   r&   zPBRMaterial.nameu  s    z~~f%%%r   c                 X    || j                             dd            d S || j         d<   d S r   r   r)   s     r   r&   zPBRMaterial.namey  s5    =JNN64(((((!&DJvr   c                     i }| j                                         D ]G\  }}|t          |d          r|                                ||<   0t          j        |          ||<   Ht          di |S )Nr,   r   )r   itemsr%   r,   r-   rk   )r   r   kvs       r   r,   zPBRMaterial.copy  s    J$$&& 	- 	-DAqyq&!! -FFHHq		 !M!,,q		$$V$$$r   c                     t          j        || j                  }| | j        | j                                        }|S )z
        Get the rough color at a list of specified UV
        coordinates.

        Parameters
        -------------
        uv : (n, 2) float
          UV coordinates on the material

        Returns
        -------------
        colors
        )r@   r8   )r   r?   ri   rj   r,   )r   r@   colorss      r   rA   zPBRMaterial.to_color  sB     "b0EFFF>d2>)..00Fr   c                 D    t          | j        | j        | j                  S )z
        Get a copy of the current PBR material as
        a simple material.

        Returns
        ------------
        simple : SimpleMaterial
          Contains material information in a simple manner
        )r8   r;   r&   )r6   ri   rj   r&   r   s    r   	to_simplezPBRMaterial.to_simple  s,    " '(
 
 
 	
r   c                 8    t          j        | j                  }|S r(   )r   r9   rj   )r   results     r   r    zPBRMaterial.main_color  s     t344r   c                     t          d                    d | j                                        D                                 S )z
        Provide a hash of the material so we can detect
        duplicate materials.

        Returns
        ------------
        hash : int
          Hash of image and parameters
        r   c              3   f   K   | ],}|t          j        |                                          V  -d S r(   )r   
asanyarrayr\   )rx   r   s     r   	<genexpr>z'PBRMaterial.__hash__.<locals>.<genexpr>  sA        /01=a  ((**==== r   )r]   rP   r   valuesr   s    r   r   zPBRMaterial.__hash__  sQ     HH  48J4E4E4G4G    
 
 	
r   )NNNNNNNNNNFNN)r.   r/   r0   rn   r   r3   r   r4   r   r   r   r   rh   rj   r   r   r   ri   r   r&   r,   rA   r   r    r   r   r   r   rk   rk   /  s         !%/ / / /b 0 0 X0 	4 	4 	4 + + X+ 	, 	, 	, - - X-" 5 5 5 	- 	- X	- 4 4 4 0 0 X0 8 8 8 1 1 X1 9 9 9 1 1 X1 A A A 	/ 	/ X	/ 0 0 0 	1 	1 X	1 2 2 2 	2 	2 X	2 3 3 3 	2 	2 X	2 3 3 3 	: 	: X	: $; ; %$; & & X& 
[' ' ['% % %  &
 
 
.   X

 
 
 
 
r   rk   r   returnc                 >    t          t          |                     S )z
    Return an empty material set to a single color

    Parameters
    -----------
    color : None or (3,) uint8
      RGB color

    Returns
    -------------
    material : SimpleMaterial
      Image is a a four pixel RGB
    r
   r8   )r6   color_imager
   s    r   empty_materialr     s       % 8 8 89999r   c                    t          t          t          j                  rt          S t	          j        g dt          j                  }t	          j        |           dv r| |dt          |           <   t          j	        t	          j
        |d                              d                              t          j                            S )z
    Generate an image with one color.

    Parameters
    ----------
    color
      Optional uint8 color

    Returns
    ----------
    image
      A (2, 2) RGBA image with the specified color.
    )d   r   r      r   ))rC      Nr   )r   r   r   )r^   r   r   ExceptionWrapperr   r   uint8shaper{   	fromarraytiler   astype)r   singles     r   r   r     s     %455 X***"(;;;F	x,&&$|U|?2761--55i@@GGQQRRRr   T    paddingc           
      n   +,-./012345 ddl }ddlm+ ddlm4 +fd1+1fd,+fd.+1fd	-d
 /+fd0d }i 334fd}|rW|                    t                    55fdt                     D              t          5                                          }	n4t          j
        t                                                   d          }	t          |	          dk    r d         t          j        |          fS t          t          j        |	                                                    t          t#          t          |                              k    sJ t          |          t                     k    sJ t%          d  D                       }
d}|dk    rE, fd|	D             }|
rV. fd|	D             }- fd|	D             }/ fd|	D             }0 fd|	D             }g }t'          |||||          D ]}d |D             }t          j        d |D                       }|                    d          }|                                |k    rK|                                |z  }t          j        ||z                                t          j                  }|                    t5          |                      |||          } |||          } |||          } |||          } |||          }ng }|D ]}t          j        |j                  }|                                |k    rK|                                |z  }t          j        ||z                                t          j                  }|                    |            ||          \  }}|-|j        d         |j        d         z  |dz  k    r|dz  }|dz  }nn|dk    E|
r ||          \  }}t;          d |D                       rd}d}n ||          \  }}t;          d |D                       r ||          \  }}nd}t%          d |D                       r ||          \  }}nd}t          j        |j        t          j                  }i 2t'          |	||          D ]\  } }}!t          j        |j                  dz
  |z  }"|!|dz
  z  }#dt          j        |j                  z  }$| D ]}%||%                                         }&|&t          j         |&|$ k     |&d |$z   k              xx         d z  cc<   t          j!        |&|$d |$z
            }&|&|"z  |#z   }'tD          j#        rItI          j%        ||%         |          }(tI          j%        |'|          })t          j&        |(|)d!          sJ |'2|%<   ǐt          j        2fd"t#          t          |                    D                       }*|
rtO          ||||rg d#nddd$||%          |*fS tQ          |&          |*fS )'a  
    Pack multiple materials with texture into a single material.

    UV coordinates outside of the 0.0-1.0 range will be coerced
    into this range using a "wrap" behavior (i.e. modulus).

    Alpha blending and backface culling settings are not supported!
    Returns a material with alpha values set, but alpha blending disabled.

    Parameters
    -----------
    materials : (n,) Material
      List of multiple materials
    uvs : (n, m, 2) float
      Original UV coordinates
    padding : int
      Number of pixels to pad each image with.
    max_tex_size_individual : int
      Maximum size of each individual texture.
    max_tex_size_fused : int | None
      Maximum size of the combined texture.
      Individual texture size will be reduced to fit.
      Set to None to allow infinite size.

    Returns
    ------------
    material : SimpleMaterial
      Combined material.
    uv : (p, 2) float
      Combined UV coordinates in the 0.0-1.0 range.
    r   Nr   r   )packingc                    ||                      |          S t          j        |                      |          t          j                  |z                                                      t          j                  }  j        |           S )z0
        Multiply an image by a factor.
        Nr   )convertr   r   r   roundr   r   r   )imgfactormoder   s      r   multiply_factorzpack.<locals>.multiply_factor+  sr     >;;t$$$Xckk$''rz:::VCUWWVBH 	
 us###r   c                    d}t          | t                    rf| j         | j        | j        d          }n| j        t	          j        | j                                      d          }t          j        t	          j	        |dd                   |dd         g          }t          j
        |dz                                t          j                  }|j        dk    sJ t          |          }|| j        dk    r}|j        }t          j        |          }| j        d	k    r,t          j        |d
         | j        dz  k    dd          |d
<   n| j        dk    s| j        	d|v rd|d
<    j        |          }nTt+          | dd          | j        }n;t          j        t+          | dg                     dk    rt          | j                  }|t                      }|                    d          S )I
        Logic for extracting a simple image from each material.
        NRGBA)r   r   r   rC   r   r   r   r   ).rC   r   r   Ar8   r;   )r^   rk   ri   rj   r   rI   r   r   concatenatelinear_to_srgbr   r   r   r   r   r   r   r   wherer   r   getattrr8   r;   r   )matr   c_linearc_srgbcr   r   r   s         r   get_base_color_texturez$pack.<locals>.get_base_color_texture8  s   
 c;'' &	+#/%o(1D6   $0 !>#*=>>FFqII )(2A2,77!A#G 
 HVc\**11"(;;w$!!nn3=G#;#;xhsmm=F**"$(3v;39N+NPSUV"W"WCKK]h..#-2Gd{{&)F%eoc**S'4((4)CCXgc9b1122d::ck**C;--C{{6"""r   c           
         d}t          | t                    r| j        | j        j        dk    r-t	          j        | j                            d                    }nt	          j        | j                  }t          |j                  dk    s|j        d         dk    rq |j	        g |j        dd         dR  }t	          j
        |t	          j        |dddf                   dz  t	          j        |dddf                   gd	          }nE|j        d         dk    r4t	          j
        |t	          j        |dddf                   gd	          }| j        _t	          j        |d
                             t          j                  | j        z                                t          j                  |d
<   | j        _t	          j        |d                             t          j                  | j        z                                t          j                  |d<    j        |          }n| j        dn| j        }| j        dn| j        }t	          j        t	          j        d||gt          j                  dz            } j        |d                             t          j                            }|S )r   NBGRRGBr   r	   .r   axis).r   ).r	   g        re   r   )NN)r^   rk   r   rG   r   r   r   r{   r   r   r  	ones_like
zeros_liker   r   r   r   r   rh   r   )r  r   metallicrl   metallic_roughnesssr   s        r   get_metallic_roughness_texturez,pack.<locals>.get_metallic_roughness_texturel  s   
 c;'' %	X+7/6%??(3#?#G#G#N#NOOCC(3#?@@Csy>>Q&&#)B-1*<*<%#+8sy!}8a888C.LS"1"W66<M#c2A2g,77
    CC Yr]a''.#r}Sbqb\/J/J)KRTUUUC%1"$(F**2:669KK# #fRX&& K &2"$(F**2:669LL# #fRX&& K &eoc**"%"4"<33#BT#&#6#>CCCDW	 ')hHc9h7rzJJJSP' '# &eo&9*&E&L&LRX&V&VWW
r   c                    d}t          | t                    r| j         | j        | j        d          }n| j        =t	          j        | j                  } j        |                    d                    }nB j        t          j        g dd          	                    t          j
                            }|                    d          S )r   Nr  )r	   r	   r  )r   r   r   )r	   r	   rC   )r^   rk   r   r   r   r9   r   r   r   r   r   r   )r  r   r
  r   r   s      r   get_emissive_texturez"pack.<locals>.get_emissive_texture  s    
 c;'' 	Y".%oc&93;MuUU#/M#"455%eoaii
&;&;<<%eobjI&F&F&M&Mbh&W&WXX{{5!!!r   c                 $    t          | dd           S r   )r  )r  s    r   get_normal_texturez pack.<locals>.get_normal_texture  s    sOT222r   c                     t          | dd           }|1 j        t          j        dggt          j                            }n|                    d          }|S )Nr   r   r   L)r  r   r   r   r   r   )r  occlusion_texturer   s     r   get_occlusion_texturez#pack.<locals>.get_occlusion_texture  s`    #C);TBB$ /3%0Q0Q0Q R R 1 9 9# > >  r   c                     g }t          | |          D ]G\  }}||                    d            |                    |          }|                    |           H|S r(   )ziprK   resize)imagessizesresizedr   sizes        r   resize_imageszpack.<locals>.resize_images  si    VU++ 	$ 	$IC{t$$$$jj&&s####r   c           
          t          t          t          d | D                                           }|vsJ |v r|         S                     | ddddt	                              }||<   |S )Nc                 ,    g | ]}t          |          S r   )id)rx   is     r   rz   z-pack.<locals>.pack_images.<locals>.<listcomp>  s     7 7 71A 7 7 7r   FT*   
   )deduplicatepower_resizeseed
iterationsspacing)r]   tuplesortedr"  r`   )r"  keyr   packedr   r   s      r   pack_imageszpack.<locals>.pack_images  s     5 7 7 7 7 78899::&    &==#; LL   
 
 sr   c                 f    g | ]-\  }}t          |                                       |          .S r   )r]   rK   )rx   r*  ry   
unique_idxs      r   rz   zpack.<locals>.<listcomp>  s6    HHH41aDGG		#	#A	&	&HHHr   )r  r	   r	   c              3   @   K   | ]}t          |t                    V  d S r(   rv   rw   s     r   r   zpack.<locals>.<genexpr>  s,      @@*Q,,@@@@@@r      c                 >    g | ]} |d                             S r   r   )rx   gr  rs   s     r   rz   zpack.<locals>.<listcomp>  s-    KKKa((1Q499KKKr   c                 >    g | ]} |d                             S r<  r   )rx   r=  r  rs   s     r   rz   zpack.<locals>.<listcomp>  s:     " " "DE..y1??" " "r   c                 >    g | ]} |d                             S r<  r   )rx   r=  r  rs   s     r   rz   zpack.<locals>.<listcomp>  s-    OOO!,,Yqt_==OOOr   c                 >    g | ]} |d                             S r<  r   )rx   r=  r  rs   s     r   rz   zpack.<locals>.<listcomp>  s-    LLLq)))AaD/::LLLr   c                 >    g | ]} |d                             S r<  r   )rx   r=  r  rs   s     r   rz   zpack.<locals>.<listcomp>  s-    QQQA..y1??QQQr   c                     g | ]}||S r(   r   rx   texs     r   rz   zpack.<locals>.<listcomp>   s    GGGCsCr   c                 @    g | ]}t          j        |j                  S r   )r   r   r%  rC  s     r   rz   zpack.<locals>.<listcomp>  s$    %M%M%MSbhsx&8&8%M%M%Mr   r  c              3   j   K   | ].}t          j        |                                          d k    V  /dS )r   N)r   r   max)rx   xs     r   r   zpack.<locals>.<genexpr>,  s9      88!rx{{  A%888888r   c              3      K   | ]}|d uV  	d S r(   r   )rx   ns     r   r   zpack.<locals>.<genexpr>3  s&      ..q}......r   c              3   j   K   | ].}t          j        |                                          d k     V  /dS )r   N)r   r   r   )rx   os     r   r   zpack.<locals>.<genexpr>:  s9      ::1rx{{  3&::::::r   r   g      ?re   )atolc                      g | ]
}|         S r   r   )rx   r*  new_uvs     r   rz   zpack.<locals>.<listcomp>g  s    <<<q<<<r   )re   re   re   F)ri   r   r   r   r   r   r   r   r   ))collectionsPILr   pathr   defaultdictlist	enumerater   r   aranger{   r   vstacksetr  ravelrangeanyr   stackrG  r   r   int64rK   r2  r   r%  allr   r,   
logical_orclipr   strictr   uv_to_interpolated_colorallcloserk   r6   )6rs   uvsr-  r   max_tex_size_individualmax_tex_size_fusedrP  r&  r6  mat_idxuse_pbrdown_scale_iterationsr"  metallic_roughnessr   normals	occlusionunpadded_sizestextures	tex_sizesmax_tex_sizescaler   tex_sizefinaloffsetsfinal_metallic_roughness_final_emissivefinal_normalsfinal_occlusion
final_sizegroupoffsetuv_scale	uv_offsethalfr=  uvgmovedoldnewstackedr   r  r  r  r  r  r   rO  r5  r   r8  s6   `  `                                       @@@@@@@@@@@r   packr    s   P $ $ $ $ $2# 2# 2# 2# 2# 2#h, , , , ,\" " " " " ""3 3 3! ! ! ! !   F      .  = ,,T22
HHHH9Y3G3GHHHHz((**++ )C	NN++33G<<
7||q|RYs^^++r~g&&,,..//3uSXX3G3GGGGGs88s9~~%%%%@@i@@@@@G 
!
#
#KKKKK7KKK %	0" " " " "IP" " " POOOOwOOOHLLLLLGLLLGQQQQQQQQIN(:HgyYY 	; 	;GG8GGGH%M%MH%M%M%MNN	(}}!}44##%%(???(,,..1HHE#%8L5,@#A#A#H#H#R#RL%%eL&9&9:::: #]6>::F!./A>!R!R$}X~>>H#mG^<<G%i@@II  N 0 08CH--<<>>$;;;$LLNN-DDE!x5(899@@JJH%%h//// %V,,w *
1
1-0BA0EEE!Q&!#)##m  !
#
#p  # '2k2D&E&E# !88x88888 	6H!NN +H 5 5NA..g..... 	!  +{733M11 M::	::::: 	#!,Y!7!7OQQ"O %*BJ777JF!'67;;    sFHSX&&*j8 j1n-	 RXch''' 	 	A a&++--C cTEk3#*+=>>???3F???'#tS4Z00C 8^y0Ez 54SVSAA4UEBB{3!444444F1II+	0 i<<<<E#c((OO<<<==G 4!&)A .2@Jd!+!0	 	 	 
 	
 E***G33r   r(   )Tr   r   r   )rn   r1   r,   numpyr    r   r   	constantsr   typedr   r   r   rQ  r   BaseExceptionEr   r   ABCr   r6   rp   rk   r   r   r   r`   r  r   r   r   <module>r     sM    


                    % % % % % % % %      + + + +'J'**EEEEEE+  #  #  #  #  #tx  #  #  #F[
 [
 [
 [
 [
X [
 [
 [
|P# P# P# P# P#H P# P# P#f_
 _
 _
 _
 _
( _
 _
 _
D: :(728#45 : : : : :&S Sx 12 S S S S8  {4 {4 	{4 {4 {4 {4 {4 {4s   3 AAA