diff --git a/scripts/resync_blends/README.md b/scripts/resync_blends/README.md new file mode 100644 index 00000000..db028f41 --- /dev/null +++ b/scripts/resync_blends/README.md @@ -0,0 +1,9 @@ +# Remap Tools + +This directory contains a script that resyncs any files older than one day and saves the file. The script requires two arguments: +1. The path to the project +2. the argument `--exec` followed by the path to the blender executable + +## Usage +1. Enter remap directory `cd blender-studio-pipeline/scripts/resync_blends` +2. Run `./run_resync_blends.py /data/your_project_name/ --exec /home/user/blender/bin/blender` diff --git a/scripts/resync_blends/resync_blend_file.py b/scripts/resync_blends/resync_blend_file.py new file mode 100644 index 00000000..8e6d3664 --- /dev/null +++ b/scripts/resync_blends/resync_blend_file.py @@ -0,0 +1,45 @@ +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import bpy +import contextlib + + +def main(): + if len(bpy.data.libraries) == 0: + return + + for lib in bpy.data.libraries: + lib.reload() + + with override_save_version(): + bpy.ops.wm.save_mainfile() + print(f"Reloaded & Saved file: '{bpy.data.filepath}'") + bpy.ops.wm.quit_blender() + + +@contextlib.contextmanager +def override_save_version(): + """Overrides the save version settings""" + save_version = bpy.context.preferences.filepaths.save_version + + try: + bpy.context.preferences.filepaths.save_version = 0 + yield + + finally: + bpy.context.preferences.filepaths.save_version = save_version + + +if __name__ == "__main__": + main() diff --git a/scripts/resync_blends/run_resync_blends.py b/scripts/resync_blends/run_resync_blends.py new file mode 100755 index 00000000..3103f7bf --- /dev/null +++ b/scripts/resync_blends/run_resync_blends.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +from pathlib import Path +import argparse +import os +import platform +import subprocess +import sys +import json +from typing import List +import time + + +def cancel_program(message: str) -> None: + """Cancel Execution of this file""" + print(message) + sys.exit(0) + + +parser = argparse.ArgumentParser() +parser.add_argument( + "path", + help="Path folder on which to perform crawl.", +) + +parser.add_argument( + "-e", + "--exec", + help="user must provide blender executable path.", + type=str, + required=True, +) + + +def get_bbatch_script_path() -> str: + """Returns path to script that runs with bbatch""" + dir = Path(__file__).parent.absolute() + return str(dir.joinpath("resync_blend_file.py")) + + +def get_files_to_crawl(project_path: str): # -> returns list of strings + + blend_files = [ + f for f in Path(project_path).glob("**/*") if f.is_file() and f.suffix == ".blend" + ] + + epoch_time = int(time.time()) + resync_blend_files = [] + for blend_file in blend_files: + elapse_time = epoch_time - os.path.getctime(blend_file) + # if file hasn't been saved in more than 24 hours, resync + if not elapse_time > 86400: + print("Skipping recently saved file:", str(blend_file)) + continue + resync_blend_files.append(blend_file) + return resync_blend_files + + +def main(): + """Resync Blender Files in a given folder""" + args = parser.parse_args() + project_path = Path(args.path) + if not project_path.exists(): + cancel_program("Provided Path does not exist") + exec_path = Path(args.exec) + if not exec_path.exists(): + cancel_program("Provided Executable path does not exist") + script_path = get_bbatch_script_path() + files_to_craw = get_files_to_crawl(project_path) + if len(files_to_craw) < 1: + cancel_program("No Files to resync") + + os.chdir("../bbatch") + print("Resyncing Files...") + cmd_list = [ + 'python', + '-m', + 'bbatch', + "--nosave", + "--recursive", + '--script', + script_path, + '--exec', + exec_path, + ] + + for item in files_to_craw: + cmd_list.insert(3, item) + + process = subprocess.Popen(cmd_list, shell=False) + if process.wait() != 0: + cancel_program(f"Resync Failed!") + print("Resync Completed Successfully") + return 0 + + +if __name__ == "__main__": + main()