Blender Crawl: Improvements based on User Feedback #53
@ -27,6 +27,8 @@ import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
import tempfile
|
||||
import uuid
|
||||
|
||||
# Command line arguments.
|
||||
parser = argparse.ArgumentParser()
|
||||
@ -49,6 +51,12 @@ parser.add_argument(
|
||||
help="Path to blender python script(s) to execute inside .blend files during crawl. Execution is skipped if no script is provided",
|
||||
nargs='+',
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--nosave",
|
||||
help="Don't save .blend after script execution.",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-r",
|
||||
"--recursive",
|
||||
@ -120,7 +128,7 @@ def is_filepath_blend(path: Path) -> None:
|
||||
cancel_program(f"Not a blend file: {path.suffix}")
|
||||
|
||||
|
||||
def check_file_exists(file_path_str: str, error_msg: str):
|
||||
def check_file_exists(file_path_str: str, error_msg: str) -> Path:
|
||||
if file_path_str is None:
|
||||
return
|
||||
file_path = Path(file_path_str).absolute()
|
||||
@ -129,22 +137,39 @@ def check_file_exists(file_path_str: str, error_msg: str):
|
||||
else:
|
||||
cancel_program(error_msg)
|
||||
|
||||
def script_append_save(script: Path, skip_save:bool):
|
||||
if skip_save:
|
||||
return script
|
||||
save_script = get_default_script("save.py", True)
|
||||
# Reading data from file1
|
||||
with open(script) as script_file:
|
||||
script_data = script_file.read()
|
||||
# Reading data from file2
|
||||
with open(save_script) as save_file:
|
||||
save_data = save_file.read()
|
||||
script_data += "\n"
|
||||
script_data += save_data
|
||||
temp_dir = Path(tempfile.TemporaryDirectory().name).parent
|
||||
new_temp_file = Path.joinpath(Path(temp_dir), f"blender_crawl_{uuid.uuid4()}.py")
|
||||
with open(new_temp_file, "w") as new_file:
|
||||
new_file.write(script_data)
|
||||
return new_temp_file
|
||||
|
||||
def get_purge_path(purge: bool):
|
||||
|
||||
def get_default_script(file_name:str, purge: bool):
|
||||
# Cancel function if user has not supplied purge arg
|
||||
if not purge:
|
||||
return
|
||||
scripts_directory = Path((os.path.dirname(__file__))).joinpath("default_scripts/")
|
||||
purge_script = os.path.join(scripts_directory.resolve(), "purge.py")
|
||||
purge_script = os.path.join(scripts_directory.resolve(), file_name)
|
||||
return check_file_exists(str(purge_script), "Default scripts location may be invalid")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
import sys
|
||||
"""Crawl blender files in a directory and run a provided scripts"""
|
||||
# Parse arguments.
|
||||
args = parser.parse_args()
|
||||
purge_path = get_purge_path(args.purge)
|
||||
purge_path = get_default_script("purge.py", args.purge)
|
||||
recursive = args.recursive
|
||||
exec = args.exec
|
||||
regex = args.filter
|
||||
@ -154,11 +179,11 @@ def main() -> int:
|
||||
scripts = []
|
||||
if script_input:
|
||||
for script in script_input:
|
||||
script_name = check_file_exists(
|
||||
script_path = check_file_exists(
|
||||
script,
|
||||
"No --script was not provided as argument, printed found .blend files, exiting program.",
|
||||
)
|
||||
scripts.append(script_name)
|
||||
scripts.append(script_append_save(script_path, args.nosave))
|
||||
|
||||
# Purge is optional so it can be none
|
||||
if purge_path is not None:
|
||||
|
Loading…
Reference in New Issue
Block a user