|
| 1 | +import numpy as np |
| 2 | +from .. import config, camera_config, file_writer_config |
| 3 | +from ..utils.iterables import list_update |
| 4 | +from ..utils.exceptions import EndSceneEarlyException |
| 5 | +from ..constants import DEFAULT_WAIT_TIME |
| 6 | +from ..scene.scene_file_writer import SceneFileWriter |
| 7 | +from ..utils.caching import handle_caching_play, handle_caching_wait |
| 8 | +from ..camera.camera import Camera |
| 9 | + |
| 10 | + |
| 11 | +def pass_scene_reference(func): |
| 12 | + def wrapper(self, scene, *args, **kwargs): |
| 13 | + func(self, scene, *args, **kwargs) |
| 14 | + |
| 15 | + return wrapper |
| 16 | + |
| 17 | + |
| 18 | +def handle_play_like_call(func): |
| 19 | + """ |
| 20 | + This method is used internally to wrap the |
| 21 | + passed function, into a function that |
| 22 | + actually writes to the video stream. |
| 23 | + Simultaneously, it also adds to the number |
| 24 | + of animations played. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + func : function |
| 29 | + The play() like function that has to be |
| 30 | + written to the video file stream. |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + function |
| 35 | + The play() like function that can now write |
| 36 | + to the video file stream. |
| 37 | + """ |
| 38 | + |
| 39 | + def wrapper(self, scene, *args, **kwargs): |
| 40 | + allow_write = not file_writer_config["skip_animations"] |
| 41 | + self.file_writer.begin_animation(allow_write) |
| 42 | + func(self, scene, *args, **kwargs) |
| 43 | + self.file_writer.end_animation(allow_write) |
| 44 | + self.num_plays += 1 |
| 45 | + |
| 46 | + return wrapper |
| 47 | + |
| 48 | + |
| 49 | +class CairoRenderer: |
| 50 | + """A renderer using Cairo. |
| 51 | +
|
| 52 | + num_plays : Number of play() functions in the scene. |
| 53 | + time: time elapsed since initialisation of scene. |
| 54 | + """ |
| 55 | + |
| 56 | + def __init__(self, camera_class=None, **kwargs): |
| 57 | + # All of the following are set to EITHER the value passed via kwargs, |
| 58 | + # OR the value stored in the global config dict at the time of |
| 59 | + # _instance construction_. Before, they were in the CONFIG dict, which |
| 60 | + # is a class attribute and is defined at the time of _class |
| 61 | + # definition_. This did not allow for creating two Cameras with |
| 62 | + # different configurations in the same session. |
| 63 | + self.file_writer = None |
| 64 | + self.video_quality_config = {} |
| 65 | + for attr in [ |
| 66 | + "pixel_height", |
| 67 | + "pixel_width", |
| 68 | + "frame_height", |
| 69 | + "frame_width", |
| 70 | + "frame_rate", |
| 71 | + ]: |
| 72 | + self.video_quality_config[attr] = kwargs.get(attr, config[attr]) |
| 73 | + camera_cls = camera_class if camera_class is not None else Camera |
| 74 | + self.camera = camera_cls(self.video_quality_config, **camera_config) |
| 75 | + self.original_skipping_status = file_writer_config["skip_animations"] |
| 76 | + self.animations_hashes = [] |
| 77 | + self.num_plays = 0 |
| 78 | + self.time = 0 |
| 79 | + |
| 80 | + def init(self, scene): |
| 81 | + self.file_writer = SceneFileWriter( |
| 82 | + self, |
| 83 | + self.video_quality_config, |
| 84 | + scene.__class__.__name__, |
| 85 | + **file_writer_config, |
| 86 | + ) |
| 87 | + |
| 88 | + @pass_scene_reference |
| 89 | + @handle_caching_play |
| 90 | + @handle_play_like_call |
| 91 | + def play(self, scene, *args, **kwargs): |
| 92 | + scene.play_internal(*args, **kwargs) |
| 93 | + |
| 94 | + @pass_scene_reference |
| 95 | + @handle_caching_wait |
| 96 | + @handle_play_like_call |
| 97 | + def wait(self, scene, duration=DEFAULT_WAIT_TIME, stop_condition=None): |
| 98 | + scene.wait_internal(duration=duration, stop_condition=stop_condition) |
| 99 | + |
| 100 | + def update_frame( # TODO Description in Docstring |
| 101 | + self, |
| 102 | + scene, |
| 103 | + mobjects=None, |
| 104 | + background=None, |
| 105 | + include_submobjects=True, |
| 106 | + ignore_skipping=True, |
| 107 | + **kwargs, |
| 108 | + ): |
| 109 | + """Update the frame. |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + mobjects: list, optional |
| 114 | + list of mobjects |
| 115 | +
|
| 116 | + background: np.ndarray, optional |
| 117 | + Pixel Array for Background. |
| 118 | +
|
| 119 | + include_submobjects: bool, optional |
| 120 | +
|
| 121 | + ignore_skipping : bool, optional |
| 122 | +
|
| 123 | + **kwargs |
| 124 | +
|
| 125 | + """ |
| 126 | + if file_writer_config["skip_animations"] and not ignore_skipping: |
| 127 | + return |
| 128 | + if mobjects is None: |
| 129 | + mobjects = list_update( |
| 130 | + scene.mobjects, |
| 131 | + scene.foreground_mobjects, |
| 132 | + ) |
| 133 | + if background is not None: |
| 134 | + self.camera.set_frame_to_background(background) |
| 135 | + else: |
| 136 | + self.camera.reset() |
| 137 | + |
| 138 | + kwargs["include_submobjects"] = include_submobjects |
| 139 | + self.camera.capture_mobjects(mobjects, **kwargs) |
| 140 | + |
| 141 | + def get_frame(self): |
| 142 | + """ |
| 143 | + Gets the current frame as NumPy array. |
| 144 | +
|
| 145 | + Returns |
| 146 | + ------- |
| 147 | + np.array |
| 148 | + NumPy array of pixel values of each pixel in screen. |
| 149 | + The shape of the array is height x width x 3 |
| 150 | + """ |
| 151 | + return np.array(self.camera.pixel_array) |
| 152 | + |
| 153 | + def add_frame(self, frame, num_frames=1): |
| 154 | + """ |
| 155 | + Adds a frame to the video_file_stream |
| 156 | +
|
| 157 | + Parameters |
| 158 | + ---------- |
| 159 | + frame : numpy.ndarray |
| 160 | + The frame to add, as a pixel array. |
| 161 | + num_frames: int |
| 162 | + The number of times to add frame. |
| 163 | + """ |
| 164 | + dt = 1 / self.camera.frame_rate |
| 165 | + self.time += num_frames * dt |
| 166 | + if file_writer_config["skip_animations"]: |
| 167 | + return |
| 168 | + for _ in range(num_frames): |
| 169 | + self.file_writer.write_frame(frame) |
| 170 | + |
| 171 | + def show_frame(self): |
| 172 | + """ |
| 173 | + Opens the current frame in the Default Image Viewer |
| 174 | + of your system. |
| 175 | + """ |
| 176 | + self.update_frame(ignore_skipping=True) |
| 177 | + self.camera.get_image().show() |
| 178 | + |
| 179 | + def update_skipping_status(self): |
| 180 | + """ |
| 181 | + This method is used internally to check if the current |
| 182 | + animation needs to be skipped or not. It also checks if |
| 183 | + the number of animations that were played correspond to |
| 184 | + the number of animations that need to be played, and |
| 185 | + raises an EndSceneEarlyException if they don't correspond. |
| 186 | + """ |
| 187 | + if file_writer_config["from_animation_number"]: |
| 188 | + if self.num_plays < file_writer_config["from_animation_number"]: |
| 189 | + file_writer_config["skip_animations"] = True |
| 190 | + if file_writer_config["upto_animation_number"]: |
| 191 | + if self.num_plays > file_writer_config["upto_animation_number"]: |
| 192 | + file_writer_config["skip_animations"] = True |
| 193 | + raise EndSceneEarlyException() |
| 194 | + |
| 195 | + def revert_to_original_skipping_status(self): |
| 196 | + """ |
| 197 | + Forces the scene to go back to its original skipping status, |
| 198 | + by setting skip_animations to whatever it reads |
| 199 | + from original_skipping_status. |
| 200 | +
|
| 201 | + Returns |
| 202 | + ------- |
| 203 | + Scene |
| 204 | + The Scene, with the original skipping status. |
| 205 | + """ |
| 206 | + if hasattr(self, "original_skipping_status"): |
| 207 | + file_writer_config["skip_animations"] = self.original_skipping_status |
| 208 | + return self |
| 209 | + |
| 210 | + def finish(self, scene): |
| 211 | + file_writer_config["skip_animations"] = False |
| 212 | + self.file_writer.finish() |
| 213 | + if file_writer_config["save_last_frame"]: |
| 214 | + self.update_frame(scene, ignore_skipping=True) |
| 215 | + self.file_writer.save_final_image(self.camera.get_image()) |
0 commit comments