Move saved mobject directory logic to scene_file_writer.py

This commit is contained in:
Grant Sanderson 2022-04-21 15:02:11 -07:00
parent 4caa033323
commit 78a7078772
2 changed files with 37 additions and 4 deletions

View file

@ -605,10 +605,12 @@ class Scene(object):
mob.become(mob_state)
self.mobjects.append(mob)
def save_mobect(self, mobject: Mobject, file_name: str):
directory = self.file_writer.get_saved_mobject_directory()
path = os.path.join(directory, file_name)
mobject.save_to_file(path)
def save_mobect(self, mobject: Mobject, file_path: str | None = None) -> None:
if file_path is None:
file_path = self.file_writer.get_saved_mobject_path(mobject)
if file_path is None:
return
mobject.save_to_file(file_path)
def load_mobject(self, file_name):
if os.path.exists(file_name):

View file

@ -11,6 +11,7 @@ from pydub import AudioSegment
from tqdm import tqdm as ProgressDisplay
from manimlib.constants import FFMPEG_BIN
from manimlib.mobject.mobject import Mobject
from manimlib.utils.config_ops import digest_config
from manimlib.utils.file_ops import guarantee_existence
from manimlib.utils.file_ops import add_extension_if_not_present
@ -127,6 +128,36 @@ class SceneFileWriter(object):
str(self.scene),
))
def get_saved_mobject_path(self, mobject: Mobject) -> str | None:
directory = self.get_saved_mobject_directory()
files = os.listdir(directory)
default_name = str(mobject) + "_0.mob"
index = 0
while default_name in files:
default_name = default_name.replace(str(index), str(index + 1))
index += 1
if platform.system() == 'Darwin':
cmds = [
"osascript", "-e",
f"""
set chosenfile to (choose file name default name "{default_name}" default location "{directory}")
POSIX path of chosenfile
""",
]
process = sp.Popen(cmds, stdout=sp.PIPE)
file_path = process.stdout.read().decode("utf-8").split("\n")[0]
if not file_path:
return
else:
user_name = input(f"Enter mobject file name (default is {default_name}): ")
file_path = os.path.join(directory, user_name or default_name)
if os.path.exists(file_path) or os.path.exists(file_path + ".mob"):
if input(f"{file_path} already exists. Overwrite (y/n)? ") != "y":
return
if not file_path.endswith(".mob"):
file_path = file_path + ".mob"
return file_path
# Sound
def init_audio(self) -> None:
self.includes_sound: bool = False