2018-04-29 10:52:48 -07:00
|
|
|
import os
|
2019-01-10 17:06:22 -08:00
|
|
|
import numpy as np
|
2018-04-29 10:52:48 -07:00
|
|
|
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import VIDEO_DIR
|
2018-04-29 10:52:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
def add_extension_if_not_present(file_name, extension):
|
|
|
|
# This could conceivably be smarter about handling existing differing extensions
|
|
|
|
if(file_name[-len(extension):] != extension):
|
|
|
|
return file_name + extension
|
|
|
|
else:
|
|
|
|
return file_name
|
|
|
|
|
|
|
|
|
2018-05-01 01:53:59 -07:00
|
|
|
def guarantee_existance(path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2018-04-29 10:52:48 -07:00
|
|
|
def get_scene_output_directory(scene_class):
|
2019-01-09 12:49:43 -08:00
|
|
|
return guarantee_existance(os.path.join(
|
|
|
|
VIDEO_DIR,
|
|
|
|
scene_class.__module__.replace(".", os.path.sep)
|
|
|
|
))
|
2018-04-29 10:52:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_movie_output_directory(scene_class, camera_config, frame_duration):
|
|
|
|
directory = get_scene_output_directory(scene_class)
|
|
|
|
sub_dir = "%dp%d" % (
|
2018-05-21 12:11:46 -07:00
|
|
|
camera_config["pixel_height"],
|
2018-04-29 10:52:48 -07:00
|
|
|
int(1.0 / frame_duration)
|
|
|
|
)
|
2018-05-01 01:53:59 -07:00
|
|
|
return guarantee_existance(os.path.join(directory, sub_dir))
|
2018-04-29 10:52:48 -07:00
|
|
|
|
|
|
|
|
2019-01-10 17:06:22 -08:00
|
|
|
def get_partial_movie_output_directory(scene_class, camera_config, frame_duration):
|
|
|
|
directory = get_movie_output_directory(scene_class, camera_config, frame_duration)
|
|
|
|
return guarantee_existance(
|
2019-01-10 17:45:56 -08:00
|
|
|
os.path.join(
|
|
|
|
directory,
|
|
|
|
"partial_movie_files",
|
|
|
|
scene_class.__name__
|
|
|
|
)
|
2019-01-10 17:06:22 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-04-29 10:52:48 -07:00
|
|
|
def get_image_output_directory(scene_class, sub_dir="images"):
|
2018-05-01 01:53:59 -07:00
|
|
|
directory = get_scene_output_directory(scene_class)
|
|
|
|
return guarantee_existance(os.path.join(directory, sub_dir))
|
2019-01-10 17:06:22 -08:00
|
|
|
|
|
|
|
|
|
|
|
def get_sorted_integer_files(directory,
|
|
|
|
min_index=0,
|
|
|
|
max_index=np.inf,
|
|
|
|
remove_non_integer_files=False,
|
2019-01-10 17:29:16 -08:00
|
|
|
remove_indices_greater_than=None,
|
|
|
|
extension=None,
|
|
|
|
):
|
2019-01-10 17:06:22 -08:00
|
|
|
indexed_files = []
|
|
|
|
for file in os.listdir(directory):
|
|
|
|
if '.' in file:
|
|
|
|
index_str = file[:file.index('.')]
|
|
|
|
else:
|
|
|
|
index_str = file
|
|
|
|
|
|
|
|
full_path = os.path.join(directory, file)
|
|
|
|
if index_str.isdigit():
|
|
|
|
index = int(index_str)
|
|
|
|
if remove_indices_greater_than is not None:
|
|
|
|
if index > remove_indices_greater_than:
|
|
|
|
os.remove(full_path)
|
|
|
|
continue
|
2019-01-10 17:29:16 -08:00
|
|
|
if extension is not None and not file.endswith(extension):
|
|
|
|
continue
|
2019-01-10 17:06:22 -08:00
|
|
|
if index >= min_index and index < max_index:
|
|
|
|
indexed_files.append((index, file))
|
|
|
|
elif remove_non_integer_files:
|
|
|
|
os.remove(full_path)
|
|
|
|
indexed_files.sort(key=lambda p: p[0])
|
2019-01-11 14:23:39 -08:00
|
|
|
return list(map(lambda p: p[1], indexed_files))
|