Convert Blender-Purge
to a more generic Blender-Crawl
Tool
#42
@ -39,6 +39,10 @@ parser = argparse.ArgumentParser()
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"path", help="Path to a file or folder on which to perform purge", type=str
|
"path", help="Path to a file or folder on which to perform purge", type=str
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"script", help="Name of default script like 'purge' or path to a valid python script file", type=str
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-R",
|
"-R",
|
||||||
"--recursive",
|
"--recursive",
|
||||||
@ -77,16 +81,7 @@ def exception_handler(func):
|
|||||||
f"\n# Error: {error}"
|
f"\n# Error: {error}"
|
||||||
"\n# Program will be cancelled."
|
"\n# Program will be cancelled."
|
||||||
)
|
)
|
||||||
cancel_program()
|
cancel_program
|
||||||
|
|
||||||
except Exception as error:
|
|
||||||
print(
|
|
||||||
"# Oops. Something went wrong during the execution of the Program!"
|
|
||||||
f"\n# Error: {error}"
|
|
||||||
"\n# Program will be cancelled."
|
|
||||||
)
|
|
||||||
cancel_program()
|
|
||||||
|
|
||||||
return func_wrapper
|
return func_wrapper
|
||||||
|
|
||||||
|
|
||||||
@ -101,19 +96,13 @@ def get_blender_path() -> Path:
|
|||||||
return Path(json_obj["blender_path"])
|
return Path(json_obj["blender_path"])
|
||||||
|
|
||||||
|
|
||||||
def get_project_root_path() -> Path:
|
def get_cmd_list(path: Path, script: Path) -> Tuple[str]:
|
||||||
config_path = get_config_path()
|
|
||||||
json_obj = load_json(config_path)
|
|
||||||
return Path(json_obj["project_root"])
|
|
||||||
|
|
||||||
|
|
||||||
def get_cmd_list(path: Path) -> Tuple[str]:
|
|
||||||
cmd_list: Tuple[str] = (
|
cmd_list: Tuple[str] = (
|
||||||
get_blender_path().as_posix(),
|
get_blender_path().as_posix(),
|
||||||
path.as_posix(),
|
path.as_posix(),
|
||||||
"-b",
|
"-b",
|
||||||
"-P",
|
"-P",
|
||||||
f"{PURGE_PATH}",
|
script,
|
||||||
"--factory-startup",
|
"--factory-startup",
|
||||||
)
|
)
|
||||||
return cmd_list
|
return cmd_list
|
||||||
@ -144,9 +133,9 @@ def prompt_confirm(path_list: List[Path]):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
def purge_file(path: Path) -> int:
|
def purge_file(path: Path, script: Path) -> int:
|
||||||
# Get cmd list.
|
# Get cmd list.
|
||||||
cmd_list = get_cmd_list(path)
|
cmd_list = get_cmd_list(path, script)
|
||||||
p = subprocess.Popen(cmd_list, shell=False)
|
p = subprocess.Popen(cmd_list, shell=False)
|
||||||
# Stdout, stderr = p.communicate().
|
# Stdout, stderr = p.communicate().
|
||||||
return p.wait()
|
return p.wait()
|
||||||
@ -245,12 +234,19 @@ def is_config_valid() -> bool:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def get_default_scipt(script_input:str):
|
||||||
|
if script_input == "purge":
|
||||||
|
folder = Path(os.path.abspath(__file__)).parent
|
||||||
|
default_scripts = folder.joinpath("default_scripts")
|
||||||
|
return default_scripts.joinpath("purge.py").absolute()
|
||||||
|
return Path(script_input).absolute()
|
||||||
|
|
||||||
@exception_handler
|
@exception_handler
|
||||||
def run_blender_purge(args: argparse.Namespace) -> int:
|
def run_blender_purge(args: argparse.Namespace) -> int:
|
||||||
|
|
||||||
# Parse arguments.
|
# Parse arguments.
|
||||||
path = Path(args.path).absolute()
|
path = Path(args.path).absolute()
|
||||||
|
script = get_default_scipt(args.script)
|
||||||
recursive = args.recursive
|
recursive = args.recursive
|
||||||
config_path = get_config_path()
|
config_path = get_config_path()
|
||||||
regex = args.regex
|
regex = args.regex
|
||||||
@ -271,6 +267,9 @@ def run_blender_purge(args: argparse.Namespace) -> int:
|
|||||||
if not path:
|
if not path:
|
||||||
raise Exception("Please provide a path as first argument")
|
raise Exception("Please provide a path as first argument")
|
||||||
|
|
||||||
|
if not script.exists():
|
||||||
|
raise Exception("Please provide a valid python script as second argument")
|
||||||
|
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
raise Exception(f"Path does not exist: {path.as_posix()}")
|
raise Exception(f"Path does not exist: {path.as_posix()}")
|
||||||
|
|
||||||
@ -322,7 +321,7 @@ def run_blender_purge(args: argparse.Namespace) -> int:
|
|||||||
# Purge each file two times.
|
# Purge each file two times.
|
||||||
for blend_file in files:
|
for blend_file in files:
|
||||||
for i in range(PURGE_AMOUNT):
|
for i in range(PURGE_AMOUNT):
|
||||||
return_code = purge_file(blend_file)
|
return_code = purge_file(blend_file, script)
|
||||||
if return_code != 0:
|
if return_code != 0:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
f"Blender Crashed on file: {blend_file.as_posix()}",
|
f"Blender Crashed on file: {blend_file.as_posix()}",
|
||||||
|
Loading…
Reference in New Issue
Block a user