File size: 2,955 Bytes
6fc3143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
3D Animation Schema and Processing

Handles 3D scene rendering with Manim's ThreeDScene.
"""

import os
import subprocess
from pathlib import Path
from typing import Optional


class ManimProcessor3D:
    """Processor for rendering 3D Manim scenes"""
    
    def __init__(self):
        self.temp_dir = None
    
    def render_scene(
        self,
        scene_file: str,
        scene_name: str,
        temp_dir: str,
        quality: str = "high"
    ) -> Optional[str]:
        """
        Render a 3D Manim scene
        
        Args:
            scene_file: Path to Python file containing the scene
            scene_name: Name of the scene class to render
            temp_dir: Temporary directory for output
            quality: Rendering quality (low, medium, high, ultra)
            
        Returns:
            Path to rendered video file, or None if failed
        """
        # Quality flag mapping
        quality_flags = {
            "low": "-pql",      # 480p15
            "medium": "-pqm",   # 720p30
            "high": "-pqh",     # 1080p60
            "ultra": "-pqk"     # 4K60
        }
        
        quality_dirs = {
            "low": "480p15",
            "medium": "720p30",
            "high": "1080p60",
            "ultra": "2160p60"
        }
        
        quality_flag = quality_flags.get(quality, "-pqh")
        quality_dir = quality_dirs.get(quality, "1080p60")
        
        # Build command
        cmd = [
            "manim",
            quality_flag,
            "--media_dir",
            temp_dir,
            scene_file,
            scene_name,
        ]
        
        try:
            # Run manim
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                check=True
            )
            
            # Construct video path
            scene_file_base = Path(scene_file).stem
            video_path = os.path.join(
                temp_dir,
                "videos",
                scene_file_base,
                quality_dir,
                f"{scene_name}.mp4"
            )
            
            if os.path.exists(video_path):
                return video_path
            else:
                print(f"Video not found at expected path: {video_path}")
                return None
                
        except subprocess.CalledProcessError as e:
            print(f"Manim rendering failed: {e.stderr}")
            return None
        except Exception as e:
            print(f"Error during rendering: {str(e)}")
            return None
    
    def validate_3d_scene(self, code: str) -> bool:
        """
        Validate that the code uses ThreeDScene
        
        Args:
            code: Python code to validate
            
        Returns:
            True if code appears to use ThreeDScene, False otherwise
        """
        return "ThreeDScene" in code or "threed" in code.lower()