init_config.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from __future__ import annotations
  2. import importlib
  3. import inspect
  4. import os
  5. import yaml
  6. from rich import box
  7. from rich.console import Console
  8. from rich.prompt import Confirm
  9. from rich.prompt import Prompt
  10. from rich.rule import Rule
  11. from rich.table import Table
  12. from typing import TYPE_CHECKING
  13. if TYPE_CHECKING:
  14. from typing import Any
  15. def get_manim_dir() -> str:
  16. manimlib_module = importlib.import_module("manimlib")
  17. manimlib_dir = os.path.dirname(inspect.getabsfile(manimlib_module))
  18. return os.path.abspath(os.path.join(manimlib_dir, ".."))
  19. def remove_empty_value(dictionary: dict[str, Any]) -> None:
  20. for key in list(dictionary.keys()):
  21. if dictionary[key] == "":
  22. dictionary.pop(key)
  23. elif isinstance(dictionary[key], dict):
  24. remove_empty_value(dictionary[key])
  25. def init_customization() -> None:
  26. configuration = {
  27. "directories": {
  28. "mirror_module_path": False,
  29. "output": "",
  30. "raster_images": "",
  31. "vector_images": "",
  32. "sounds": "",
  33. "temporary_storage": "",
  34. },
  35. "universal_import_line": "from manimlib import *",
  36. "style": {
  37. "tex_template": "",
  38. "font": "Consolas",
  39. "background_color": "",
  40. },
  41. "window_position": "UR",
  42. "window_monitor": 0,
  43. "full_screen": False,
  44. "break_into_partial_movies": False,
  45. "camera_resolutions": {
  46. "low": "854x480",
  47. "medium": "1280x720",
  48. "high": "1920x1080",
  49. "4k": "3840x2160",
  50. "default_resolution": "",
  51. },
  52. "fps": 30,
  53. }
  54. console = Console()
  55. console.print(Rule("[bold]Configuration Guide[/bold]"))
  56. # print("Initialize configuration")
  57. try:
  58. scope = Prompt.ask(
  59. " Select the scope of the configuration",
  60. choices=["global", "local"],
  61. default="local"
  62. )
  63. console.print("[bold]Directories:[/bold]")
  64. dir_config = configuration["directories"]
  65. dir_config["output"] = Prompt.ask(
  66. " Where should manim [bold]output[/bold] video and image files place [prompt.default](optional, default is none)",
  67. default="",
  68. show_default=False
  69. )
  70. dir_config["raster_images"] = Prompt.ask(
  71. " Which folder should manim find [bold]raster images[/bold] (.jpg .png .gif) in " + \
  72. "[prompt.default](optional, default is none)",
  73. default="",
  74. show_default=False
  75. )
  76. dir_config["vector_images"] = Prompt.ask(
  77. " Which folder should manim find [bold]vector images[/bold] (.svg .xdv) in " + \
  78. "[prompt.default](optional, default is none)",
  79. default="",
  80. show_default=False
  81. )
  82. dir_config["sounds"] = Prompt.ask(
  83. " Which folder should manim find [bold]sound files[/bold] (.mp3 .wav) in " + \
  84. "[prompt.default](optional, default is none)",
  85. default="",
  86. show_default=False
  87. )
  88. dir_config["temporary_storage"] = Prompt.ask(
  89. " Which folder should manim storage [bold]temporary files[/bold] " + \
  90. "[prompt.default](recommended, use system temporary folder by default)",
  91. default="",
  92. show_default=False
  93. )
  94. console.print("[bold]Styles:[/bold]")
  95. style_config = configuration["style"]
  96. tex_template = Prompt.ask(
  97. " Select a TeX template to compile a LaTeX source file",
  98. default="default"
  99. )
  100. style_config["tex_template"] = tex_template
  101. style_config["background_color"] = Prompt.ask(
  102. " Which [bold]background color[/bold] do you want [italic](hex code)",
  103. default="#333333"
  104. )
  105. console.print("[bold]Camera qualities:[/bold]")
  106. table = Table(
  107. "low", "medium", "high", "ultra_high",
  108. title="Four defined qualities",
  109. box=box.ROUNDED
  110. )
  111. table.add_row("480p15", "720p30", "1080p60", "2160p60")
  112. console.print(table)
  113. configuration["camera_resolutions"]["default_resolution"] = Prompt.ask(
  114. " Which one to choose as the default rendering quality",
  115. choices=["low", "medium", "high", "ultra_high"],
  116. default="high"
  117. )
  118. write_to_file = Confirm.ask(
  119. "\n[bold]Are you sure to write these configs to file?[/bold]",
  120. default=True
  121. )
  122. if not write_to_file:
  123. raise KeyboardInterrupt
  124. global_file_name = os.path.join(get_manim_dir(), "manimlib", "default_config.yml")
  125. if scope == "global":
  126. file_name = global_file_name
  127. else:
  128. if os.path.exists(global_file_name):
  129. remove_empty_value(configuration)
  130. file_name = os.path.join(os.getcwd(), "custom_config.yml")
  131. with open(file_name, "w", encoding="utf-8") as f:
  132. yaml.dump(configuration, f)
  133. console.print(f"\n:rocket: You have successfully set up a {scope} configuration file!")
  134. console.print(f"You can manually modify it in: [cyan]`{file_name}`[/cyan]")
  135. except KeyboardInterrupt:
  136. console.print("\n[green]Exit configuration guide[/green]")