From ba927bf6a6a6bd9c21b728ba916086c92f18c1cd Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Tue, 20 Feb 2024 14:39:47 -0500 Subject: [PATCH 1/8] Add Core Script to Resync File --- scripts/resync_blends/resync_blend_file.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 scripts/resync_blends/resync_blend_file.py diff --git a/scripts/resync_blends/resync_blend_file.py b/scripts/resync_blends/resync_blend_file.py new file mode 100644 index 00000000..7b65b6ba --- /dev/null +++ b/scripts/resync_blends/resync_blend_file.py @@ -0,0 +1,17 @@ +# 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 + +for lib in bpy.data.libraries: + lib.reload() -- 2.30.2 From 5639b9a176077f4d50a6aa65ecc9ac1c03211a5a Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Tue, 20 Feb 2024 14:40:05 -0500 Subject: [PATCH 2/8] Add Script to Crawl Directory and Resync All Files Older than 24 Hours --- scripts/resync_blends/run_resync_blends.py | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100755 scripts/resync_blends/run_resync_blends.py diff --git a/scripts/resync_blends/run_resync_blends.py b/scripts/resync_blends/run_resync_blends.py new file mode 100755 index 00000000..fa090316 --- /dev/null +++ b/scripts/resync_blends/run_resync_blends.py @@ -0,0 +1,129 @@ +#!/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.", +) + + +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_blender_path(project_path: Path) -> str: + """Get the path to a project's blender executable + + Args: + project_path (Path): Path Object, containing project's root path + + Returns: + str: Path to blender executable as a string + """ + local_blender_path = project_path.joinpath('local').joinpath('blender') + if not local_blender_path.exists(): + return + system_name = platform.system().lower() + blender_path_base = local_blender_path / system_name + if system_name == 'linux': + blender_path = blender_path_base / 'blender' + elif system_name == 'darwin': + blender_path = blender_path_base / 'Blender.app' / 'Contents' / 'MacOS' / 'Blender' + elif system_name == 'windows': + blender_path = blender_path_base / 'blender.exe' + return str(blender_path.absolute()) + + +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") + 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") + project_blender = get_blender_path(project_path) + os.chdir("../bbatch") + print("Resyncing Files...") + cmd_list = [ + 'python', + '-m', + 'bbatch', + "--nosave", + "--recursive", + '--script', + script_path, + ] + + for item in files_to_craw: + cmd_list.insert(3, item) + + if project_blender: + cmd_list.append('--exec') + cmd_list.append(project_blender) + + with open('/tmp/resync_log.txt', 'w') as log: + # TODO Debug why logging isn't working as expected + process = subprocess.Popen(cmd_list, stdout=log, shell=False) # stderr=log, + # 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() -- 2.30.2 From 9ac3996f2c73d8b247a962d10850ccb58ff036d9 Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Wed, 21 Feb 2024 09:45:57 -0500 Subject: [PATCH 3/8] Pass Blender Executable as an Argument --- scripts/resync_blends/run_resync_blends.py | 46 +++++++--------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/scripts/resync_blends/run_resync_blends.py b/scripts/resync_blends/run_resync_blends.py index fa090316..6bb3ebb8 100755 --- a/scripts/resync_blends/run_resync_blends.py +++ b/scripts/resync_blends/run_resync_blends.py @@ -37,6 +37,14 @@ parser.add_argument( 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""" @@ -44,29 +52,6 @@ def get_bbatch_script_path() -> str: return str(dir.joinpath("resync_blend_file.py")) -def get_blender_path(project_path: Path) -> str: - """Get the path to a project's blender executable - - Args: - project_path (Path): Path Object, containing project's root path - - Returns: - str: Path to blender executable as a string - """ - local_blender_path = project_path.joinpath('local').joinpath('blender') - if not local_blender_path.exists(): - return - system_name = platform.system().lower() - blender_path_base = local_blender_path / system_name - if system_name == 'linux': - blender_path = blender_path_base / 'blender' - elif system_name == 'darwin': - blender_path = blender_path_base / 'Blender.app' / 'Contents' / 'MacOS' / 'Blender' - elif system_name == 'windows': - blender_path = blender_path_base / 'blender.exe' - return str(blender_path.absolute()) - - def get_files_to_crawl(project_path: str): # -> returns list of strings blend_files = [ @@ -91,11 +76,14 @@ def main(): 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") - project_blender = get_blender_path(project_path) + os.chdir("../bbatch") print("Resyncing Files...") cmd_list = [ @@ -106,19 +94,15 @@ def main(): "--recursive", '--script', script_path, + '--exec', + exec_path, ] for item in files_to_craw: cmd_list.insert(3, item) - if project_blender: - cmd_list.append('--exec') - cmd_list.append(project_blender) - with open('/tmp/resync_log.txt', 'w') as log: - # TODO Debug why logging isn't working as expected - process = subprocess.Popen(cmd_list, stdout=log, shell=False) # stderr=log, - # process = subprocess.Popen(cmd_list, shell=False) + process = subprocess.Popen(cmd_list, stdout=log, stderr=log, shell=False) if process.wait() != 0: cancel_program(f"Resync Failed!") print("Resync Completed Successfully") -- 2.30.2 From fd239f66a77754e908bdf8a32860e8627236e29e Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Wed, 21 Feb 2024 10:55:27 -0500 Subject: [PATCH 4/8] Only Save if Libraries have been reloaded --- scripts/resync_blends/resync_blend_file.py | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/scripts/resync_blends/resync_blend_file.py b/scripts/resync_blends/resync_blend_file.py index 7b65b6ba..8e6d3664 100644 --- a/scripts/resync_blends/resync_blend_file.py +++ b/scripts/resync_blends/resync_blend_file.py @@ -12,6 +12,34 @@ # along with this program. If not, see . import bpy +import contextlib -for lib in bpy.data.libraries: - lib.reload() + +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() -- 2.30.2 From 28062d8dba2527f21490bb0b6068a21af88fd0b4 Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Wed, 21 Feb 2024 12:29:59 -0500 Subject: [PATCH 5/8] Don't print log to txt file --- scripts/resync_blends/run_resync_blends.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scripts/resync_blends/run_resync_blends.py b/scripts/resync_blends/run_resync_blends.py index 6bb3ebb8..3103f7bf 100755 --- a/scripts/resync_blends/run_resync_blends.py +++ b/scripts/resync_blends/run_resync_blends.py @@ -101,10 +101,9 @@ def main(): for item in files_to_craw: cmd_list.insert(3, item) - with open('/tmp/resync_log.txt', 'w') as log: - process = subprocess.Popen(cmd_list, stdout=log, stderr=log, shell=False) - if process.wait() != 0: - cancel_program(f"Resync Failed!") + process = subprocess.Popen(cmd_list, shell=False) + if process.wait() != 0: + cancel_program(f"Resync Failed!") print("Resync Completed Successfully") return 0 -- 2.30.2 From 9a87a6b116c49fa5efd1e25ad6ff62a7d9027050 Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Wed, 21 Feb 2024 12:32:39 -0500 Subject: [PATCH 6/8] Add Bash Script to Commit Resync Changes to SVN --- scripts/resync_blends/run_resync_blends.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 scripts/resync_blends/run_resync_blends.sh diff --git a/scripts/resync_blends/run_resync_blends.sh b/scripts/resync_blends/run_resync_blends.sh new file mode 100644 index 00000000..67ab3e57 --- /dev/null +++ b/scripts/resync_blends/run_resync_blends.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Stop execution if any command fails +set -e + +svn up +./run_resync_blends.py /data/gold/ --exec /home/user/blender/bin/blender +svn commit -m "[Automated] Resync files" \ No newline at end of file -- 2.30.2 From 39090dbc6caffa0e028b134dded00fcab7c623fb Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Wed, 21 Feb 2024 12:38:25 -0500 Subject: [PATCH 7/8] Remove Bash Script --- scripts/resync_blends/run_resync_blends.sh | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 scripts/resync_blends/run_resync_blends.sh diff --git a/scripts/resync_blends/run_resync_blends.sh b/scripts/resync_blends/run_resync_blends.sh deleted file mode 100644 index 67ab3e57..00000000 --- a/scripts/resync_blends/run_resync_blends.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# Stop execution if any command fails -set -e - -svn up -./run_resync_blends.py /data/gold/ --exec /home/user/blender/bin/blender -svn commit -m "[Automated] Resync files" \ No newline at end of file -- 2.30.2 From f44626f501cbda458d35116819f2fdc7c4916925 Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Wed, 21 Feb 2024 12:38:39 -0500 Subject: [PATCH 8/8] Add README --- scripts/resync_blends/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 scripts/resync_blends/README.md 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` -- 2.30.2