Scripts: Re-Sync Blend Files #240
129
scripts/resync_blends/run_resync_blends.py
Executable file
129
scripts/resync_blends/run_resync_blends.py
Executable file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
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()
|
Loading…
Reference in New Issue
Block a user