Skip to content

Commit

Permalink
make media, tex, and video directories configurable via flags
Browse files Browse the repository at this point in the history
  • Loading branch information
eulertour committed Jun 5, 2019
1 parent caa4577 commit f81c275
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 37 deletions.
2 changes: 2 additions & 0 deletions manimlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import manimlib.config
import manimlib.constants
import manimlib.extract_scene
import manimlib.stream_starter

Expand All @@ -8,6 +9,7 @@ def main():
args = manimlib.config.parse_cli()
if not args.livestream:
config = manimlib.config.get_configuration(args)
manimlib.constants.initialize_directories(config)
manimlib.extract_scene.main(config)
else:
manimlib.stream_starter.start_livestream(
Expand Down
17 changes: 16 additions & 1 deletion manimlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ def parse_cli():
action="store_true",
help="Leave progress bars displayed in terminal",
)
parser.add_argument(
"--media_dir",
help="directory to write media",
)
parser.add_argument(
"--video_dir",
help="directory to write video",
)
parser.add_argument(
"--tex_dir",
help="directory to write tex",
)

# For live streaming
module_location.add_argument(
Expand Down Expand Up @@ -192,7 +204,10 @@ def get_configuration(args):
"start_at_animation_number": args.start_at_animation_number,
"end_at_animation_number": None,
"sound": args.sound,
"leave_progress_bars": args.leave_progress_bars
"leave_progress_bars": args.leave_progress_bars,
"media_dir": args.media_dir,
"video_dir": args.video_dir,
"tex_dir": args.tex_dir,
}

# Camera configuration
Expand Down
60 changes: 34 additions & 26 deletions manimlib/constants.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import numpy as np
import os

# Initialize directories
env_MEDIA_DIR = os.getenv("MEDIA_DIR")
if env_MEDIA_DIR:
MEDIA_DIR = env_MEDIA_DIR
elif os.path.isfile("media_dir.txt"):
with open("media_dir.txt", 'rU') as media_file:
MEDIA_DIR = media_file.readline().strip()
else:
MEDIA_DIR = os.path.join(
os.path.expanduser('~'),
"Dropbox (3Blue1Brown)/3Blue1Brown Team Folder"
)
if not os.path.isdir(MEDIA_DIR):
MEDIA_DIR = "./media"
print(
f"Media will be stored in {MEDIA_DIR + os.sep}. You can change "
"this behavior by writing a different directory to media_dir.txt."
)

VIDEO_DIR = os.path.join(MEDIA_DIR, "video")
ASSETS_DIR = os.path.join(MEDIA_DIR, "assets")
TEX_DIR = os.path.join(MEDIA_DIR, "Tex")

for folder in [VIDEO_DIR, ASSETS_DIR, TEX_DIR]:
if not os.path.exists(folder):
os.makedirs(folder)
MEDIA_DIR = ""
VIDEO_DIR = ""
TEX_DIR = ""

def initialize_directories(config):
global MEDIA_DIR
global VIDEO_DIR
global TEX_DIR
if not (config["video_dir"] and config["tex_dir"]):
if config["media_dir"]:
MEDIA_DIR = config["media_dir"]
else:
MEDIA_DIR = os.path.join(
os.path.expanduser('~'),
"Dropbox (3Blue1Brown)/3Blue1Brown Team Folder"
)
if not os.path.isdir(MEDIA_DIR):
MEDIA_DIR = "./media"
print(
f"Media will be written to {MEDIA_DIR + os.sep}. You can change "
"this behavior with the --media_dir flag."
)
else:
if config["media_dir"]:
print(
"Ignoring --media_dir, since --video_dir and --tex_dir were "
"both passed"
)
VIDEO_DIR = config["video_dir"] or os.path.join(MEDIA_DIR, "video")
TEX_DIR = config["tex_dir"] or os.path.join(MEDIA_DIR, "Tex")

for folder in [VIDEO_DIR, TEX_DIR]:
if not os.path.exists(folder):
os.makedirs(folder)

TEX_USE_CTEX = False
TEX_TEXT_TO_REPLACE = "YourTextHere"
Expand Down
3 changes: 2 additions & 1 deletion manimlib/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from colour import Color
import numpy as np

import manimlib.constants as consts
from manimlib.constants import *
from manimlib.container.container import Container
from manimlib.utils.color import color_gradient
Expand Down Expand Up @@ -109,7 +110,7 @@ def show(self, camera=None):

def save_image(self, name=None):
self.get_image().save(
os.path.join(VIDEO_DIR, (name or str(self)) + ".png")
os.path.join(consts.VIDEO_DIR, (name or str(self)) + ".png")
)

def copy(self):
Expand Down
6 changes: 3 additions & 3 deletions manimlib/scene/scene_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from time import sleep
import datetime

import manimlib.constants as consts
from manimlib.constants import FFMPEG_BIN
from manimlib.constants import STREAMING_IP
from manimlib.constants import STREAMING_PORT
from manimlib.constants import STREAMING_PROTOCOL
from manimlib.constants import VIDEO_DIR
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
Expand Down Expand Up @@ -50,7 +50,7 @@ def init_output_directories(self):
scene_name = self.file_name or self.get_default_scene_name()
if self.save_last_frame:
image_dir = guarantee_existence(os.path.join(
VIDEO_DIR,
consts.VIDEO_DIR,
module_directory,
scene_name,
"images",
Expand All @@ -61,7 +61,7 @@ def init_output_directories(self):
)
if self.write_to_movie:
movie_dir = guarantee_existence(os.path.join(
VIDEO_DIR,
consts.VIDEO_DIR,
module_directory,
scene_name,
self.get_resolution_directory(),
Expand Down
8 changes: 4 additions & 4 deletions manimlib/utils/tex_file_writing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import hashlib

from manimlib.constants import TEX_DIR
from manimlib.constants import TEX_TEXT_TO_REPLACE
from manimlib.constants import TEX_USE_CTEX
import manimlib.constants as consts


def tex_hash(expression, template_tex_file_body):
Expand All @@ -22,7 +22,7 @@ def tex_to_svg_file(expression, template_tex_file_body):

def generate_tex_file(expression, template_tex_file_body):
result = os.path.join(
TEX_DIR,
consts.TEX_DIR,
tex_hash(expression, template_tex_file_body)
) + ".tex"
if not os.path.exists(result):
Expand All @@ -44,7 +44,7 @@ def tex_to_dvi(tex_file):
"latex",
"-interaction=batchmode",
"-halt-on-error",
"-output-directory=" + TEX_DIR,
"-output-directory=" + consts.TEX_DIR,
tex_file,
">",
os.devnull
Expand All @@ -53,7 +53,7 @@ def tex_to_dvi(tex_file):
"-no-pdf",
"-interaction=batchmode",
"-halt-on-error",
"-output-directory=" + TEX_DIR,
"-output-directory=" + consts.TEX_DIR,
tex_file,
">",
os.devnull
Expand Down
4 changes: 2 additions & 2 deletions stage_scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import sys
import importlib

import manimlib.constants as consts
from manimlib.constants import PRODUCTION_QUALITY_CAMERA_CONFIG
from manimlib.constants import VIDEO_DIR
from manimlib.config import get_module
from manimlib.extract_scene import is_child_scene

Expand Down Expand Up @@ -43,7 +43,7 @@ def stage_scenes(module_name):
# }
# TODO, fix this
animation_dir = os.path.join(
VIDEO_DIR, "ode", "part2", "1440p60"
consts.VIDEO_DIR, "ode", "part2", "1440p60"
)
#
files = os.listdir(animation_dir)
Expand Down

0 comments on commit f81c275

Please sign in to comment.