
    Mi                        S r SSKrSSKrSSKrSSKJrJr  SSKJ	r	  SSK
JrJr  SSKJr   SS	KJr  S
r " S S\R(                  5      r " S S\5      r " S S\5      r " S S\5      rSS\\\R2                        S\4S jjrSS\\\R2                        4S jjr    SS\4S jjrg! \ a  r\R$                  " \5      r SrCNSrCff = f)z?
material.py
-------------

Store visual materials as objects.
    N   )
exceptionsutil)tol)NDArrayOptional   colorImagegh㈵>c                       \ rS rSrS r\R                  S 5       r\\R                  S 5       5       r	\S 5       r
\
R                  S 5       r
S rSrg	)
Material   c                     [        S5      eNzmust be subclassed!NotImplementedError)selfargskwargss      q/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/visual/material.py__init__Material.__init__   s    !"788    c                     [        S5      er   r   r   s    r   __hash__Material.__hash__   s    !"788r   c                     gzo
The "average" color of this material.

Returns
---------
color : (4,) uint8
  Average color of this material.
N r   s    r   
main_colorMaterial.main_color#       r   c                 >    [        U S5      (       a  U R                  $ g)N_name
material_0)hasattrr'   r   s    r   nameMaterial.name/   s    4!!::r   c                     Ub  Xl         g g Nr'   r   values     r   r*   r+   5   s    J r   c                 .    [         R                  " U 5      $ r-   )copydeepcopyr   s    r   r2   Material.copy:   s    }}T""r   r.   N)__name__
__module____qualname____firstlineno__r   abcabstractmethodr   propertyr#   r*   setterr2   __static_attributes__r"   r   r   r   r      st    9 	9 9     
 
[[ #r   r   c                       \ rS rSrSr      SS jrS rSS jrS r\	S 5       r
\	S	 5       r\R                  S
 5       rS rSrg)SimpleMaterial>   z
Hold a single image texture.
Nc                     Xl         X`l        [        R                  " U5      U l        [        R                  " U5      U l        [        R                  " U5      U l        XPl        Xpl        g r-   )	imager*   r   to_rgbaambientdiffusespecular
glossinessr   )r   rB   rE   rD   rF   rG   r*   r   s           r   r   SimpleMaterial.__init__C   sL     
	}}W-}}W-h/ % r   c                 B    [         R                  " XR                  5      $ r-   )r   uv_to_colorrB   )r   uvs     r   to_colorSimpleMaterial.to_color[   s      ZZ00r   c                    [         R                  " U R                  5      SS n[         R                  " U R                  5      SS n[         R                  " U R                  5      SS nUc  U R
                  nSU 3SR                  " U6 SR                  " U6 SR                  " U6 SU R                  S 3/n0 nU R                  b  U R                  R                  nUc  S	nU S
UR                  5        3nUR                  SU 35        [        R                  " 5       n	U R                  R                  XS9  U	R                  S5        U	R                  5       Xh'   SR!                  U5      R#                  S5      Xa S3'   Xa4$ )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_floatrD   rE   rF   r*   rS   rG   rB   lowerappendr   BytesIOsaveseekreadjoinencode)
r   r*   KaKdKsmtldata
image_type
image_namef_objs
             r   to_objSimpleMaterial.to_obj^   sd   ( ^^DLL)"1-^^DLL)"1-^^DMM*2A.<99D dV(//4(//4(//4$//$'(
 ::!**J!"
 6:#3#3#5"67JJJ-. LLNEJJOOuO8JJqM$zz|D"iin33G<vT]zr   c                    [        U R                  S5      (       a$  [        U R                  R                  5       5      nOSn[        U R                  S5      (       a&  U[        U R                  R                  5       5      -  n[        U R
                  S5      (       a&  U[        U R
                  R                  5       5      -  n[        U R                  S5      (       a&  U[        U R                  R                  5       5      -  n[        U R                  [        5      (       a$  U[        [        U R                  S-  5      5      -  nU$ )z}
Provide a hash of the material so we can detect
duplicates.

Returns
------------
hash : int
  Hash of image and parameters
tobytesr   i  )r)   rB   hashri   rD   rE   rF   
isinstancerG   floatint)r   hasheds     r   r   SimpleMaterial.__hash__   s     4::y))$**,,./F F 4<<++d4<<//122F4<<++d4<<//122F4==),,d4==00233Fdoou--d3t5677Fr   c                     U R                   $ )z"
Return the most prominent color.
)rE   r   s    r   r#   SimpleMaterial.main_color   s    
 ||r   c                 >    [        U S5      (       a  U R                  $ g)N_glossiness      ?)r)   rs   r   s    r   rG   SimpleMaterial.glossiness   s    4''###r   c                 ,    Uc  g [        U5      U l        g r-   )rl   rs   r/   s     r   rG   ru      s    = <r   c                 j    SU R                   S-   -  S-  n[        UU R                  U R                  S9$ )z
Convert the current simple material to a
PBR material.

Returns
------------
pbr : PBRMaterial
  Contains material information in PBR format.
r   g      ?)roughnessFactorbaseColorTexturebaseColorFactor)rG   PBRMaterialrB   rE   )r   	roughnesss     r   to_pbrSimpleMaterial.to_pbr   s:     $//A-.I>	%!ZZ LL
 	
r   )rs   rD   rE   rG   rB   r   r*   rF   )NNNNNNr-   )r5   r6   r7   r8   __doc__r   rL   rf   r   r;   r#   rG   r<   r}   r=   r"   r   r   r?   r?   >   sw     018t:    
 ( (

r   r?   c                   X    \ rS rSrSS jrS rS rS rS rS r	\
S	 5       rS
 rS rSrg)MultiMaterial   Nc                 &    Uc  / U l         gXl         g)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   r   r   s      r   r   MultiMaterial.__init__   s     DN&Nr   c                     U R                    Vs/ s H  n[        U[        5      (       d  M  UPM     nn[        U5      S:X  a
  [        5       $ US   $ s  snf )z
TODO : IMPLEMENT
r   )r   rk   r{   len)r   mpbrs      r   r}   MultiMaterial.to_pbr   sG     ..G.QJq+,Fq.Gs8q== 1v Hs
   AAc           
          [        [        R                  R                  U R                   Vs/ s H  n[        U5      PM     sn5      5      $ s  snf )z
Provide a hash of the multi material so we can detect
duplicates.

Returns
------------
hash : int
  Xor hash of the contained materials.
)rm   npbitwise_xorreducer   rj   )r   r   s     r   r   MultiMaterial.__hash__   s8     2>>((4>>)J>a$q'>)JKLL)Js   Ac                 ,    [        U R                  5      $ r-   )iterr   r   s    r   __iter__MultiMaterial.__iter__       DNN##r   c                 ,    [        U R                  5      $ r-   )nextr   r   s    r   __next__MultiMaterial.__next__  r   r   c                 ,    [        U R                  5      $ r-   )r   r   r   s    r   __len__MultiMaterial.__len__  s    4>>""r   c                     gr!   r"   r   s    r   r#   MultiMaterial.main_color	  r%   r   c                 :    U R                   R                  U5        g)zn
Adds new material to the container.

Parameters
----------
material : Material
    The material to be added.
N)r   rW   )r   materials     r   addMultiMaterial.add  s     	h'r   c                      U R                   U   $ )z
Get material by index.

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

Returns
-------
    The material on the given index.
r   )r   idxs     r   getMultiMaterial.get  s     ~~c""r   r   r-   )r5   r6   r7   r8   r   r}   r   r   r   r   r;   r#   r   r   r=   r"   r   r   r   r      s?    '
M$$#  	(#r   r   c                      \ rS rSrSr             S%S jr\S 5       r\R                  S 5       r\S 5       r	\	R                  S 5       r	\S	 5       r
\
R                  S
 5       r
\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       rS rS  rS! r\S" 5       rS# rS$rg)&r{   i/  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
Nc                 d   0 U l         X l        X@l        XPl        X`l        Xl        Xpl        X0l        Xl        Xl	        Xl
        Xl        Xl        Xl        [        U5      S:  aL  [        R                   R#                  SR%                  SR'                  UR)                  5       5      5      5        g g )Nr   zunsupported material keys: {}z, )_dataemissiveFactorrz   metallicFactorrx   alphaCutoffnormalTextureemissiveTextureocclusionTexturery   metallicRoughnessTexturedoubleSidedr*   	alphaModer   r   logdebugrS   r\   keys)r   r*   r   r   rz   r   rx   r   r   ry   r   r   r   r   r   s                  r   r   PBRMaterial.__init__8  s    $ 
 -. -.& +. 0 0(@% ' 	"v;?HHNN/66tyy7OP r   c                 8    U R                   R                  S5      $ )a  
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   PBRMaterial.emissiveFactori       zz~~.//r   c                    Uc  U R                   R                  SS 5        g [        R                  " U[        R                  S9R                  S5      nUR                  5       [        * :  a  [        S5      eX R                   S'   g )Nr   dtyperO   z$all factors must be greater than 0.0)	r   popr   arrayfloat64reshapemin_eps
ValueError)r   r0   emissives      r   r   r   x  sd    =JJNN+T2 xxRZZ8@@CH||~% !GHH+3JJ'(r   c                 8    U R                   R                  S5      $ )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   PBRMaterial.alphaMode  s     zz~~k**r   c                     Uc  U R                   R                  SS 5        g [        U5      R                  5       R	                  5       nUS;  a  [        SU5      eXR                   S'   g )Nr   )OPAQUEMASKBLENDzincorrect alphaMode: %s)r   r   strupperstripr   r/   s     r   r   r     sY    =JJNN;- J$$&,,.E77 !:EBB&+JJ{#r   c                 8    U R                   R                  S5      $ )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   PBRMaterial.alphaCutoff  s      zz~~m,,r   c                 t    Uc  U R                   R                  SS 5        g [        U5      U R                   S'   g )Nr   r   r   rl   r/   s     r   r   r     s+    =JJNN=$/(-eDJJ}%r   c                 8    U R                   R                  S5      $ )z
Specifies whether the material is double sided.

Returns
-----------
doubleSided : bool
  Specifies whether the material is double sided.
r   r   r   s    r   r   PBRMaterial.doubleSided  s     zz~~m,,r   c                 t    Uc  U R                   R                  SS 5        g [        U5      U R                   S'   g )Nr   )r   r   boolr/   s     r   r   r     s+    =JJNN=$/(,UDJJ}%r   c                 8    U R                   R                  S5      $ )z
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   PBRMaterial.metallicFactor  r   r   c                 t    Uc  U R                   R                  SS 5        g [        U5      U R                   S'   g )Nr   r   r/   s     r   r   r     s-    =JJNN+T2+0<DJJ'(r   c                 8    U R                   R                  S5      $ )z
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.
rx   r   r   s    r   rx   PBRMaterial.roughnessFactor       zz~~/00r   c                 t    Uc  U R                   R                  SS 5        g [        U5      U R                   S'   g )Nrx   r   r/   s     r   rx   r     s-    =JJNN,d3,1%LDJJ()r   c                 8    U R                   R                  S5      $ )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
rz   r   r   s    r   rz   PBRMaterial.baseColorFactor  r   r   c                     Uc  U R                   R                  SS 5        g [        R                  " U5      U R                   S'   g )Nrz   )r   r   r   rC   r/   s     r   rz   r     s4    =JJNN,d3 -2MM%,@DJJ()r   c                 8    U R                   R                  S5      $ )zQ
The normal map texture.

Returns
----------
image : PIL.Image
  Normal texture.
r   r   r   s    r   r   PBRMaterial.normalTexture  s     zz~~o..r   c                 `    Uc  U R                   R                  SS 5        g XR                   S'   g Nr   r   r   r/   s     r   r   r     s%    =JJNN?D1*/JJ'r   c                 8    U R                   R                  S5      $ )zQ
The emissive texture.

Returns
----------
image : PIL.Image
  Emissive texture.
r   r   r   s    r   r   PBRMaterial.emissiveTexture$  s     zz~~/00r   c                 `    Uc  U R                   R                  SS 5        g XR                   S'   g )Nr   r   r/   s     r   r   r   0  s'    =JJNN,d3,1JJ()r   c                 8    U R                   R                  S5      $ )zS
The occlusion texture.

Returns
----------
image : PIL.Image
  Occlusion texture.
r   r   r   s    r   r   PBRMaterial.occlusionTexture8       zz~~011r   c                 `    Uc  U R                   R                  SS 5        g XR                   S'   g )Nr   r   r/   s     r   r   r   D  s'    =JJNN-t4-2JJ)*r   c                 8    U R                   R                  S5      $ )zV
The base color texture image.

Returns
----------
image : PIL.Image
  Color texture.
ry   r   r   s    r   ry   PBRMaterial.baseColorTextureL  r   r   c                 `    Uc  U R                   R                  SS 5        g XR                   S'   g )Nry   r   r/   s     r   ry   r   X  s)    =JJNN-t4 .3JJ)*r   c                 8    U R                   R                  S5      $ )ze
The metallic-roughness texture.

Returns
----------
image : PIL.Image
  Metallic-roughness texture.
r   r   r   s    r   r   $PBRMaterial.metallicRoughnessTexturea  s     zz~~899r   c                 `    Uc  U R                   R                  SS 5        g XR                   S'   g )Nr   r   r/   s     r   r   r   m  s'    =JJNN5t<5:JJ12r   c                 8    U R                   R                  S5      $ Nr*   r   r   s    r   r*   PBRMaterial.nameu  s    zz~~f%%r   c                 `    Uc  U R                   R                  SS 5        g XR                   S'   g r   r   r/   s     r   r*   r   y  s%    =JJNN64(!&JJvr   c                     0 nU R                   R                  5        HG  u  p#Uc  M
  [        US5      (       a  UR                  5       X'   M/  [        R                  " U5      X'   MI     [        S0 UD6$ )Nr2   r"   )r   itemsr)   r2   r3   r{   )r   r   kvs       r   r2   PBRMaterial.copy  sd    JJ$$&DAyq&!!FFH	 !MM!,	 ' $V$$r   c                     [         R                  " XR                  S9nUc'  U R                  b  U R                  R	                  5       nU$ )z
Get the rough color at a list of specified UV
coordinates.

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

Returns
-------------
colors
)rK   rB   )r   rJ   ry   rz   r2   )r   rK   colorss      r   rL   PBRMaterial.to_color  sC     ""b0E0EF>d22>))..0Fr   c                 T    [        U R                  U R                  U R                  S9$ )z
Get a copy of the current PBR material as
a simple material.

Returns
------------
simple : SimpleMaterial
  Contains material information in a simple manner
)rB   rE   r*   )r?   ry   rz   r*   r   s    r   	to_simplePBRMaterial.to_simple  s*    " ''((
 	
r   c                 F    [         R                  " U R                  5      nU$ r-   )r   rC   rz   )r   results     r   r#   PBRMaterial.main_color  s     t334r   c                 t    [        SR                  S U R                  R                  5        5       5      5      $ )z
Provide a hash of the material so we can detect
duplicate materials.

Returns
------------
hash : int
  Hash of image and parameters
r   c              3   r   #    U  H-  oc  M  [         R                  " U5      R                  5       v   M/     g 7fr-   )r   
asanyarrayri   ).0r   s     r   	<genexpr>'PBRMaterial.__hash__.<locals>.<genexpr>  s,      4Gq*a ((**4Gs   7+7)rj   r\   r   valuesr   s    r   r   PBRMaterial.__hash__  s7     HH 48JJ4E4E4G 
 	
r   )r   r   r   rz   ry   r   r   r   r   r   r*   r   r   rx   )NNNNNNNNNNFNN)r5   r6   r7   r8   r   r   r;   r   r<   r   r   r   r   rx   rz   r   r   r   ry   r   r*   r2   rL   r   r#   r   r=   r"   r   r   r{   r{   /  s    !%/b 0 0 	4 	4 + + 	, 	, - -" 5 5 	- 	- 4 4 0 0 8 8 1 1 9 9 1 1 A A 	/ 	/ 0 0 	1 	1 2 2 	2 	2 3 3 	2 	2 3 3 	: 	: $$; %; & & 
[[' '%&
.  

r   r{   r   returnc                 "    [        [        U S9S9$ )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
   rB   )r?   color_imager
   s    r   empty_materialr    s      % 899r   c                    [        [        [        R                  5      (       a  [        $ [        R
                  " / SQ[        R                  S9n[        R                  " U 5      S;   a  XS[        U 5      & [        R                  " [        R                  " US5      R                  S5      R                  [        R                  5      5      $ )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   ))rO      Nr  )r   r   r  )rk   r   r   ExceptionWrapperr   r   uint8shaper   	fromarraytiler   astype)r   singles     r   r  r    s     %4455XX*"((;F	xx,&$|U??27761-55i@GGQRRr   paddingc                   ^^5^6^7^8 SSK nSSKJm5  SSKJm8  U54S jm6U5U64S jnU54S jnU5U64S	 jn	S
 n
U54S jnS n0 m7U7U8U4S jnU(       aj  UR                  [        5      n[        U 5       VVs/ s H#  u  nnU[        U5         R                  U5      PM%       nn[        UR                  5       5      nO.[        R                  " [        U 5      5      R                  S5      n[        U5      S:X  a  U S   [        R                  " U5      4$ [!        [        R"                  " U5      R%                  5       5      [!        ['        [        U5      5      5      :X  d   e[        U5      [        U 5      :X  d   e[)        S U  5       5      nSnUS:  Ga  U Vs/ s H  nU" U US      5      PM     nnU(       Ga  U Vs/ s H  nU" U US      5      PM     nnU Vs/ s H  nU	" U US      5      PM     nnU Vs/ s H  nU
" U US      5      PM     nnU Vs/ s H  nU" U US      5      PM     nn/ n[+        UUUUU5       H  nU Vs/ s H
  nUc  M  UPM     nn[        R,                  " U Vs/ s H#  n[        R.                  " UR0                  5      PM%     sn5      nUR3                  SS9nUR3                  5       U:  aI  UR3                  5       U-  n[        R4                  " UU-  5      R7                  [        R8                  5      nUR                  [;        U5      5        M     U" UU5      nU" UU5      nU" UU5      nU" UU5      nU" UU5      nO/ nU H  n [        R.                  " U R0                  5      n!U!R3                  5       U:  aI  U!R3                  5       U-  n[        R4                  " U!U-  5      R7                  [        R8                  5      n!UR                  U!5        M     U" U5      u  n"n#Ub1  U"R0                  S   U"R0                  S   -  US-  :  a  US-  nUS-  nOO	US:  a  GM  U(       a|  U" W5      u  n$n%[=        S W 5       5      (       a  SnSn&OU" U5      u  n&n%[=        S W 5       5      (       a  U" U5      u  n'n%OSn'[)        S W 5       5      (       a  U" U5      u  n(n%OSn([        R.                  " W"R0                  [        R>                  S9n)0 n*[+        UWW#5       GH,  u  n+n n,[        R.                  " U R0                  5      S-
  U)-  n-U,U)S-
  -  n.S[        R.                  " U R0                  5      -  n/U+ H  nUU   RA                  5       n0U0[        RB                  " U0U/* :  U0SU/-   :  5      ==   S-  ss'   [        RD                  " U0U/SU/-
  5      n0U0U--  U.-   n1[F        RH                  (       aN  [J        RL                  " UU   U 5      n2[J        RL                  " U1U"5      n3[        RN                  " U2U3SS9(       d   eU1U*U'   M     GM/     [        R                  " ['        [        U5      5       Vs/ s H  nU*U   PM
     sn5      n4U(       a  [Q        U"W$W&U&(       a  / SQOSSSW'W(S9U44$ [S        U"S9U44$ s  snnf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf )ac  
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                   > Uc  U R                  U5      $ [        R                  " U R                  U5      [        R                  S9U-  R	                  5       R                  [        R                  5      n TR                  " U 5      $ )z 
Multiply an image by a factor.
r   )convertr   r   r   roundr  r  r  )imgfactormoder   s      r   multiply_factorpack.<locals>.multiply_factor+  sd     >;;t$$XXckk$'rzz:VCUWVBHH 	
 s##r   c                 N  > Sn[        U [        5      (       Ga  U R                  b  T" U R                  U R                  SS9nOU R                  b  [        R
                  " U R                  5      R                  S5      n[        R                  " [        R                  " USS 5      USS /5      n[        R                  " US-  5      R                  [        R                  5      nUR                  S:X  d   e[        U5      nUb  U R                  S:w  a  UR                   n[        R"                  " U5      nU R                  S	:X  a/  [        R$                  " US
   U R&                  S-  :  SS5      US
'   O(U R                  S:X  d  U R                  c  SU;   a  SUS
'   TR(                  " U5      nOU[+        U SS5      b  U R,                  nO:[        R                  " [+        U S/ 5      5      S:X  a  [        U R.                  5      nUc
  [        5       nUR1                  S5      $ )9
Logic for extracting a simple image from each material.
NRGBA)r%  r&  r  rO   r  r  r   r   ).rO   r   r   ArB   rE   )rk   r{   ry   rz   r   rU   r   r   concatenatelinear_to_srgbr#  r  r  r  r  r   r&  r   wherer   r  getattrrB   rE   r"  )matr$  c_linearc_srgbcr&  r   r'  s         r   get_base_color_texture$pack.<locals>.get_base_color_texture8  s   
 c;''##/%((1D1D6 $$0 !>>#*=*=>FFqI ))(2A,7!AG
 HHVc\*11"((;ww$&!!n3==G#;xxhhsm==F*"$((3v;39N+NPSUV"WCK]]h.#--2Gd{&)Fooc*S'4(4))CXXgc9b12d:ckk*C;-C{{6""r   c           
         > Sn[        U [        5      (       Ga  U R                  GbB  U R                  R                  S:X  a0  [        R
                  " U R                  R                  S5      5      nO [        R
                  " U R                  5      n[        UR                  5      S:X  d  UR                  S   S:X  as  UR                  " / UR                  SS QSP76 n[        R                  " U[        R                  " USSS24   5      S-  [        R                  " USSS24   5      /SS	9nOEUR                  S   S:X  a2  [        R                  " U[        R                  " USSS24   5      /SS	9nU R                  bc  [        R                  " US
   R                  [        R                   5      U R                  -  5      R                  [        R"                  5      US
'   U R$                  bc  [        R                  " US   R                  [        R                   5      U R$                  -  5      R                  [        R"                  5      US'   TR&                  " U5      nU$ U R                  c  SOU R                  nU R$                  c  SOU R$                  n[        R                  " [        R
                  " SX2/[        R                   S9S-  5      nTR&                  " US   R                  [        R"                  5      5      nU$ )r*  NBGRRGBr   r	   .r  axis).r   ).r	   g        rt   r   )NN)rk   r{   r   rS   r   r   r"  r   r  r   r-  	ones_like
zeros_liker   r#  r  r   r  rx   r  )r1  r$  metallicr|   metallic_roughnesssr   s        r   get_metallic_roughness_texture,pack.<locals>.get_metallic_roughness_texturel  sx   
 c;''++7//66%?((3#?#?#G#G#NOC((3#?#?@Csyy>Q&#))B-1*<++8syy!}8a8C..LLS"1"W6<MM#c2A2g,7
  C YYr]a'..#r}}Sbqb\/J)KRTUC%%1"$((F**2::69K9KK#fRXX& K &&2"$((F**2::69L9LL#fRXX& K ooc* 
 #&"4"4"<3#BTBT#&#6#6#>CCDWDW	 ')hhHHc97rzzJSP'# oo&9*&E&L&LRXX&VW
r   c                   > Sn[        U [        5      (       a  U R                  b  T" U R                  U R                  S5      nOU R                  bB  [        R
                  " U R                  5      nTR                  " UR                  S5      5      nOFTR                  " [        R                  " / SQS5      R                  [        R                  5      5      nUR                  S5      $ )r*  Nr9  )r	   r	   r:  )r   r   r   )r	   r	   rO   )rk   r{   r   r   r   rC   r  r   r   r  r  r"  )r1  r$  r4  r   r'  s      r   get_emissive_texture"pack.<locals>.get_emissive_texture  s    
 c;''"".%c&9&93;M;MuU##/MM#"4"45ooaii
&;<oobjjI&F&M&Mbhh&WX{{5!!r   c                     [        U SS 5      $ r   )r0  )r1  s    r   get_normal_texture pack.<locals>.get_normal_texture  s    sOT22r   c                    > [        U SS 5      nUc7  TR                  " [        R                  " S//[        R                  S95      nU$ UR                  S5      nU$ )Nr   r  r   L)r0  r  r   r   r  r"  )r1  occlusion_texturer   s     r   get_occlusion_texture#pack.<locals>.get_occlusion_texture  s\    #C);TB$ %3%0Q R !  !2 9 9# >  r   c                     / n[        X5       H=  u  p4Uc  UR                  S 5        M  UR                  U5      nUR                  U5        M?     U$ r-   )ziprW   resize)imagessizesresizedr$  sizes        r   resize_imagespack.<locals>.resize_images  sJ    V+IC{t$jj&s# , r   c                    > [        [        [        U  Vs/ s H  n[        U5      PM     sn5      5      5      nUT;  d   eUT;   a  TU   $ TR	                  U SSSS[        T5      S9nUTU'   U$ s  snf )NFT*   
   )deduplicatepower_resizeseed
iterationsspacing)rj   tuplesortedidrQ  rm   )rQ  ikeyr  packedr   r  s       r   pack_imagespack.<locals>.pack_images  s     5 71A 789:&   &=#; L   
 s !8s   A0)r:  r	   r	   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr-   )rk   r{   )r  r   s     r   r	  pack.<locals>.<genexpr>  s     @i*Q,,is      r;  c              3   p   #    U  H,  n[         R                  " U5      R                  5       S :H  v   M.     g7f)r   N)r   r   max)r  xs     r   r	  rh  ,  s$     8x!rxx{ A%x   46c              3   (   #    U  H  oS Lv   M
     g 7fr-   r"   )r  ns     r   r	  rh  3  s     .g}gs   c              3   p   #    U  H,  n[         R                  " U5      R                  5       S :  v   M.     g7f)r  N)r   r   r   )r  os     r   r	  rh  :  s$     :	1rxx{ 3&	rm  r   g      ?rt   )atol)rt   rt   rt   F)ry   r   r   r   r   r   r   r   r  )*collectionsPILr   pathr   defaultdictlist	enumeraterj   rW   r  r   aranger   r   vstacksetr-  ravelrangeanyrO  stackr   rT  rk  r#  r  int64r_  allr   r2   
logical_orclipr   strictr   uv_to_interpolated_colorallcloser{   r?   )9r   uvsrZ  r  max_tex_size_individualmax_tex_size_fusedrs  r5  rA  rD  rG  rL  rU  re  
unique_idxrb  r   mat_idxuse_pbrdown_scale_iterationsgrQ  metallic_roughnessr   normals	occlusionunpadded_sizestexturestex	tex_sizesmax_tex_sizescaler$  tex_sizefinaloffsetsfinal_metallic_roughness_final_emissivefinal_normalsfinal_occlusion
final_sizenew_uvgroupoffsetuv_scale	uv_offsethalfuvgmovedoldnewstackedr   r'  rd  r   s9      `                                                 @@@@r   packr    sD   P $2#h,\""3! F.  ,,T2
3<Y3GH3G41aDG		#	#A	&3GHz((*+ ))C	N+33G<
7|q|RYYs^++r~~g&,,./3uSX3GGGGs8s9~%%%@i@@G 
!
#CJK7a(1Q497K JQ"IPA.y1?  " HOOw!,Yqt_=wHODKLGq))AaD/:GGLIPQA.y1?IQN(:HgyY+3G8CsC8GHHH%MHSbhhsxx&8H%MN	(}}!}4##%(??(,,.1HHE#%88L5,@#A#H#H#RL%%eL&9: Z #6>:F!./A>!R$X~>H#G^<G%i@I  N88CHH-<<>$;;$LLN-DDE!xx5(89@@JH%%h/  %V,w *

1

1-0BA0EE!Q&!#)#m  !
#p  '22D&E# !8x888H!N +H 5NA.g...  +73M1 M:	:::!,Y!7OQ"O %**BJJ7JF!'67;sFHHSXX&*j8 j1n-	 RXXchh''A a&++-C cTEk3#*+=>?3F?''#tS4Z0C 8^y0Ezz44SVSA44UEB{{3!444F1I+  <F iiE#c(O<OqO<=G!&)A .2@d!+!0	 
 	
 E*G33w 	I( L
" PLQ
 H%ML =s<   0*ZZ<Z Z%8Z*Z/Z4Z4/*Z9
Z>r-   )Tr       r  )r   r9   r2   numpyr    r   r   	constantsr   typedr   r   r   rt  r   BaseExceptionEr  r   ABCr   r?   r   r{   r  r  r  rm   r  r"   r   r   <module>r     s         % +
  #txx  #F[
X [
|P#H P#f_
( _
D:(7288#45 : :&Sx 12 S8  {4 	{4S  +''*E+s   B, ,C2C		C