
    kj'                        d Z ddlmZ ddlZddlmZmZmZ ddlZddl	Z	ddl
Zddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZmZ  ej        e          Zerddlm Z mZm!Z!mZ  G d d          Z"dS )z-
Wrapper for a loaded scene with properties.
    )annotationsN)TYPE_CHECKINGAnyOptional)geometry)ProgramDescription)programs   )Material)Node)ColorLightProgramFallbackProgramMeshProgramTextureLightProgramTextureProgramTextureVertexColorProgramVertexColorProgram)Camerar   Meshr   c                      e Zd ZdZd2dZed3d	            Zed4d            Zej        d5d            Z	 d6d7dZ		 	 	 	 d8d9dZ
	 	 	 d:d;dZ	 d<d=d$Zd>d%Zd?d'Zd>d(Zd@dAd*Zd@dBd,Zd>d-Zd>d.ZdCd0ZdCd1ZdS )DScenezGeneric scenenameOptional[str]kwargsr   c                   || _         g | _        g | _        g | _        g | _        g | _        t          j                    | _        t          j                    | _	        d| _
        t          j                    | _        | j        j        i | j        _        | j        j                            d          | _        | j        s;t%          j        t)          d                    | _        | j        | j        j        d<   | j        j                            d          | _        | j        s;t%          j        t)          d                    | _        | j        | j        j        d<   t          j                    | _        dS )zmCreate a scene with a name.

        Args:
            name (str): Unique name or path for the scene
              ?NDEFAULT_BBOX_PROGRAMzscene_default/bbox.glsl)pathDEFAULT_WIREFRAME_PROGRAMzscene_default/wireframe.glsl)r   
root_nodesnodes	materialsmeshescamerasglmvec3bbox_minbbox_maxdiagonal_sizer   bboxbbox_vaoctxextragetbbox_programr	   loadr   wireframe_programmat4_matrix)selfr   r   s      V/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/scene/scene.py__init__zScene.__init__&   sC    	&( "$
)+"$%'"%(**"%(**  8>!DHN !HN../EFF  	G ("(ABBB! !D 6:5FDHN12 "&!3!34O!P!P% 	Q%-]"(FGGG& &D" ;?:PDHN67xzz    returnmoderngl.Contextc                (    t          j                    S )z%moderngl.Context: The current context)mglwr,   r4   s    r5   r,   z	Scene.ctxP   s     xzzr7   glm.mat4c                    | j         S )zQglm.mat4x4: The current model matrix

        This property is settable.
        )r3   r<   s    r5   matrixzScene.matrixU   s     |r7   r?   Nonec                \    || _         | j        D ]}|                    | j                    d S N)r3   r    calc_model_mat)r4   r?   nodes      r5   r?   zScene.matrix]   s=    O 	. 	.D----	. 	.r7           projection_matrixOptional[glm.mat4]camera_matrixtimefloatc                    | j         D ]}|                    |||           | j                            dd           dS )zDraw all the nodes in the scene.

        Args:
            projection_matrix (ndarray): projection matrix (bytes)
            camera_matrix (ndarray): camera_matrix (bytes)
            time (float): The current time
        )rF   rH   rI   r      N)r    drawr,   clear_samplers)r4   rF   rH   rI   rD   s        r5   rM   z
Scene.drawc   s^     O 	 	DII"3+      	1%%%%%r7   NT      ?rP   rP   childrencolortuple[float, float, float]c                .   |}|}| j         d                             |           | j         d                             | j                   | j         d                             |           | j         d                             | j                   | j         d                             | j                   || j         d         _        | j                            | j                    |sdS | j        D ]$}|	                    ||| j         | j                   %dS )a.  Draw scene and mesh bounding boxes.

        Args:
            projection_matrix (glm.mat4): mat4 projection
            camera_matrix (glm.mat4): mat4 camera matrix
            children (bool): Will draw bounding boxes for meshes as well
            color (tuple): Color of the bounding boxes
        m_projm_modelm_cambb_minbb_maxrR   N)
r/   writer3   r'   r(   valuer+   renderr    	draw_bbox)r4   rF   rH   rQ   rR   rD   s         r5   r]   zScene.draw_bboxy   s    .% 	(#))*;<<<)$**4<888'"((777(#))$-888(#))$-888+0'"(T./// 	F O 	_ 	_DNN,mT=NPTP]^^^^	_ 	_r7   rP   rP   rP   r   !tuple[float, float, float, float]c                h   |}|}| j         d                             |           | j         d                             | j                   | j         d                             |           || j         d<   d| j        _        | j        D ]}|                    ||| j                    d| j        _        dS )a(  Render the scene in wireframe mode.

        Args:
            projection_matrix (ndarray): mat4 projection
            camera_matrix (ndarray): mat4 camera matrix
            children (bool): Will draw bounding boxes for meshes as well
            color (tuple): Color of the wireframes
        rU   rV   rW   rR   TFN)r1   rZ   r3   r,   	wireframer    draw_wireframe)r4   rF   rH   rR   rD   s        r5   rb   zScene.draw_wireframe   s     .%x(../@AAAy)//===w'--m<<<*/w' "O 	Z 	ZD 1=$BXYYYY"r7   mesh_programsOptional[list[MeshProgram]]clearboolc           	     d   |r| j         D ]	}d|_        
|s| j        j                            d          }|s_t                      t                      t                      t                      t                      t                      g}|| j        j        d<   | j         D ]}|D ]f}|                    |          }|Mt          |t                    r	||_         n0t          d                    t!          |                              g|j        s t"                              d|j                   dS )zApplies mesh programs to meshes.
        If not mesh programs are passed in we assign default ones.

        Args:
            mesh_programs (list): List of mesh programs to assign
            clear (bool): Clear all assigned mesh programs
        NDEFAULT_PROGRAMSz2apply() must return a MeshProgram instance, not {}z'WARING: No mesh program applied to '%s')r#   mesh_programr,   r-   r.   r   r   r   r   r   r   apply
isinstancer   
ValueErrorformattypeloggerwarningr   )r4   rc   re   mesh	mesh_proginstances         r5   apply_mesh_programszScene.apply_mesh_programs   s^     	) ) )$(!! 	C HN../ABBM  	C'))"$$&((-//%''#%%! 6C12K 	U 	UD*  	$??400'!(K88 ,5)(PWW $X    ( $ UH$)TTT	U 	Ur7   c                    d}d}| j         D ]-}|                    t          j                    ||          \  }}.|| _        || _        t          j        | j        | j        z
            | _        dS )zCalculate scene bboxN)r    calc_global_bboxr%   r2   r'   r(   lengthr)   )r4   r'   r(   rD   s       r5   calc_scene_bboxzScene.calc_scene_bbox   sr    $($(O 	W 	WD!%!6!6sxzz8X!V!VHhh   Z(EFFr7   glm.vec3c                6    | j         | j        | j         z
  dz  z   S )z6Calculate the center of the scene using bounding boxesg       @)r'   r(   r<   s    r5   
get_centerzScene.get_center   s    } =DDDr7   c                ^    |                                   t          j                    | _        dS )zprepare the scene for rendering.

        Calls ``apply_mesh_programs()`` assigning default meshprograms if needed
        and sets the model matrix.
        N)rt   r%   r2   r?   r<   s    r5   preparezScene.prepare   s'     	  """hjjr7   Optional[Node]c                8    | j         D ]}|j        |k    r|c S dS )zFinds a :py:class:`~moderngl_window.scene.Node`

        Keyword Args:
            name (str): Case sensitive name
        Returns:
            A :py:class:`~moderngl_window.scene.Node` or ``None`` if not found.
        N)r!   r   )r4   r   rD   s      r5   	find_nodezScene.find_node  s7     J 	 	DyD   ! tr7   Optional[Material]c                8    | j         D ]}|j        |k    r|c S dS )zFinds a :py:class:`~moderngl_window.scene.Material`

        Keyword Args:
            name (str): Case sensitive material name
        Returns:
            A :py:class:`~moderngl_window.scene.Material` or ``None``
        N)r"   r   )r4   r   mats      r5   find_materialzScene.find_material  s7     > 	 	Cx4


   tr7   c                .    |                                   dS z*Destroys the scene data and vertex buffersN)destroyr<   s    r5   releasezScene.release  s    r7   c                    | j         D ]"}|j        |j                                         #| j        D ]}|                                 g | _         g | _        dS r   )r#   vaor   r"   r    )r4   rq   r   s      r5   r   zScene.destroy"  sd    K 	# 	#Dx#  """ > 	 	CKKMMMMr7   strc                6    d                     | j                  S )Nz<Scene: {}>)rm   r   r<   s    r5   __str__zScene.__str__0  s    ##DI...r7   c                     t          |           S rB   )r   r<   s    r5   __repr__zScene.__repr__3  s    4yyr7   )r   r   r   r   )r8   r9   )r8   r=   )r?   r=   r8   r@   )rE   )rF   rG   rH   rG   rI   rJ   r8   r@   )NNTrO   )
rF   rG   rH   rG   rQ   rJ   rR   rS   r8   r@   )NNr^   )rF   rG   rH   rG   rR   r_   r8   r@   )NT)rc   rd   re   rf   r8   r@   )r8   r@   )r8   ry   rB   )r   r   r8   r~   )r   r   r8   r   )r8   r   )__name__
__module____qualname____doc__r6   propertyr,   r?   setterrM   r]   rb   rt   rx   r{   r}   r   r   r   r   r   r    r7   r5   r   r   #   s       (" (" (" ("T    X    X ]. . . ]. 	& & & & &0 15,0,> _  _  _  _  _H 15,03J	# # # # #> PT,U ,U ,U ,U ,U\G G G GE E E E! ! ! !              / / / /     r7   r   )#r   
__future__r   loggingtypingr   r   r   r%   modernglmoderngl_windowr;   r   moderngl_window.metar   "moderngl_window.resources.programsr	   materialr   rD   r   r   r   r   r   r   r   r   	getLoggerr   ro   moderngl_window.scener   r   r   r   r7   r5   <module>r      s    # " " " " "  / / / / / / / / / / 



      $ $ $ $ $ $ 3 3 3 3 3 3 7 7 7 7 7 7                              
	8	$	$ CBBBBBBBBBBBBQ Q Q Q Q Q Q Q Q Qr7   