mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
identify the scene name based on the line number
This commit is contained in:
parent
39fbb677dc
commit
dec8fc5eeb
3 changed files with 24 additions and 9 deletions
|
@ -44,7 +44,7 @@ window:
|
||||||
# If not full screen, the default to give it half the screen width
|
# If not full screen, the default to give it half the screen width
|
||||||
full_screen: False
|
full_screen: False
|
||||||
# Other optional specifications that override the above include:
|
# Other optional specifications that override the above include:
|
||||||
# position: (500, 500) # Specific position, in pixel coordiantes, for upper right corner
|
# position: (500, 500) # Specific position, in pixel coordinates, for upper right corner
|
||||||
# size: (1920, 1080) # Specific size, in pixels
|
# size: (1920, 1080) # Specific size, in pixels
|
||||||
camera:
|
camera:
|
||||||
resolution: (1920, 1080)
|
resolution: (1920, 1080)
|
||||||
|
|
|
@ -12,6 +12,7 @@ from manimlib.scene.interactive_scene import InteractiveScene
|
||||||
from manimlib.scene.scene import Scene
|
from manimlib.scene.scene import Scene
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
Module = importlib.util.types.ModuleType
|
Module = importlib.util.types.ModuleType
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
@ -142,7 +143,7 @@ def get_indent(code_lines: list[str], line_number: int) -> str:
|
||||||
return n_spaces * " "
|
return n_spaces * " "
|
||||||
|
|
||||||
|
|
||||||
def insert_embed_line_to_module(module: Module, line_number: int):
|
def insert_embed_line_to_module(module: Module, run_config: Dict) -> None:
|
||||||
"""
|
"""
|
||||||
This is hacky, but convenient. When user includes the argument "-e", it will try
|
This is hacky, but convenient. When user includes the argument "-e", it will try
|
||||||
to recreate a file that inserts the line `self.embed()` into the end of the scene's
|
to recreate a file that inserts the line `self.embed()` into the end of the scene's
|
||||||
|
@ -150,27 +151,40 @@ def insert_embed_line_to_module(module: Module, line_number: int):
|
||||||
the last line in the sourcefile which includes that string.
|
the last line in the sourcefile which includes that string.
|
||||||
"""
|
"""
|
||||||
lines = inspect.getsource(module).splitlines()
|
lines = inspect.getsource(module).splitlines()
|
||||||
|
line_number = run_config.embed_line
|
||||||
|
|
||||||
# Add the relevant embed line to the code
|
# Add the relevant embed line to the code
|
||||||
indent = get_indent(lines, line_number)
|
indent = get_indent(lines, line_number)
|
||||||
lines.insert(line_number, indent + "self.embed()")
|
lines.insert(line_number, indent + "self.embed()")
|
||||||
new_code = "\n".join(lines)
|
new_code = "\n".join(lines)
|
||||||
|
|
||||||
|
# When the user executes the `-e <line_number>` command,
|
||||||
|
# it should automatically identifies the nearest class
|
||||||
|
# defined above `<line_number>` as 'scene_names'.
|
||||||
|
classes = list(filter(lambda line: line.startswith("class"), lines[:line_number]))
|
||||||
|
if classes:
|
||||||
|
from re import search
|
||||||
|
|
||||||
|
name_search = search(r"(\w+)\(", classes[-1])
|
||||||
|
run_config.update(scene_names=[name_search.group(1)])
|
||||||
|
else:
|
||||||
|
log.error(f"No 'class' has been found above {line_number}!")
|
||||||
|
|
||||||
# Execute the code, which presumably redefines the user's
|
# Execute the code, which presumably redefines the user's
|
||||||
# scene to include this embed line, within the relevant module.
|
# scene to include this embed line, within the relevant module.
|
||||||
code_object = compile(new_code, module.__name__, 'exec')
|
code_object = compile(new_code, module.__name__, 'exec')
|
||||||
exec(code_object, module.__dict__)
|
exec(code_object, module.__dict__)
|
||||||
|
|
||||||
|
|
||||||
def get_module(file_name: Optional[str], embed_line: Optional[int], is_reload: bool = False) -> Module:
|
def get_module(run_config: Dict) -> Module:
|
||||||
module = ModuleLoader.get_module(file_name, is_reload)
|
module = ModuleLoader.get_module(run_config.file_name, run_config.is_reload)
|
||||||
if embed_line:
|
if run_config.embed_line:
|
||||||
insert_embed_line_to_module(module, embed_line)
|
insert_embed_line_to_module(module, run_config)
|
||||||
return module
|
return module
|
||||||
|
|
||||||
|
|
||||||
def main(scene_config: Dict, run_config: Dict):
|
def main(scene_config: Dict, run_config: Dict):
|
||||||
module = get_module(run_config.file_name, run_config.embed_line, run_config.is_reload)
|
module = get_module(run_config)
|
||||||
all_scene_classes = get_scene_classes(module)
|
all_scene_classes = get_scene_classes(module)
|
||||||
scenes = get_scenes_to_render(all_scene_classes, scene_config, run_config)
|
scenes = get_scenes_to_render(all_scene_classes, scene_config, run_config)
|
||||||
if len(scenes) == 0:
|
if len(scenes) == 0:
|
||||||
|
|
|
@ -43,6 +43,7 @@ class SampleSpace(Rectangle):
|
||||||
fill_opacity=fill_opacity,
|
fill_opacity=fill_opacity,
|
||||||
stroke_width=stroke_width,
|
stroke_width=stroke_width,
|
||||||
stroke_color=stroke_color,
|
stroke_color=stroke_color,
|
||||||
|
**kwargs
|
||||||
)
|
)
|
||||||
self.default_label_scale_val = default_label_scale_val
|
self.default_label_scale_val = default_label_scale_val
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue