Remap: Refactor scripts #177

Merged
Nick Alberelli merged 17 commits from :feature/refactor-remap-script into main 2023-12-07 16:17:27 +01:00
Showing only changes of commit 48a4a9fa2f - Show all commits

View File

@ -5,6 +5,7 @@ import json
import hashlib import hashlib
import time import time
import contextlib import contextlib
from typing import List
file_updated = False file_updated = False
@ -30,7 +31,16 @@ def override_save_version():
bpy.context.preferences.filepaths.save_version = save_version bpy.context.preferences.filepaths.save_version = save_version
def paths_for_vse_strip(strip): def paths_for_vse_strip(strip: bpy.types.Sequence) -> List[str]:
"""Returns all paths related to Movie, Image and Sound strips
in Blender's Video Sequence Editor
Args:
strip (bpy.types.Sequence): Movie, Image or Sounds Strip
Returns:
List[str]: List of all paths related to strip
"""
if hasattr(strip, "filepath"): if hasattr(strip, "filepath"):
return [strip.filepath] return [strip.filepath]
if hasattr(strip, "directory"): if hasattr(strip, "directory"):
@ -41,9 +51,9 @@ def paths_for_vse_strip(strip):
def generate_checksum(filepath: str) -> str: def generate_checksum(filepath: str) -> str:
""" """Generate a checksum for a zip file
Generate a checksum for a zip file
Arguments: Args:
archive_path: String of the archive's file path archive_path: String of the archive's file path
Returns: Returns:
sha256 checksum for the provided archive as string sha256 checksum for the provided archive as string
@ -58,20 +68,37 @@ def generate_checksum(filepath: str) -> str:
return sha256.hexdigest() return sha256.hexdigest()
def find_new_from_old(old_path: str, file_map_dict: dict) -> str: def dict_find_new_from_old(old_path: str, file_map_dict: dict) -> str:
"""Returns the matching 'new' filepath stored in file_map_dict
using the 'old' filepath.
Args:
old_path (str): 'old' filepath referencing a file from Blender
file_map_dict (dict): Dictionary of 'old' and 'new' paths
Returns:
str: 'new' filepath to replace the 'old' filepath
"""
for _, value in file_map_dict.items(): for _, value in file_map_dict.items():
for old_json_path in value['old']: for old_json_path in value['old']:
# Match paths using the filepath stored in Blender
if old_json_path.endswith(old_path.split("/..")[-1]): if old_json_path.endswith(old_path.split("/..")[-1]):
if value['new'] != old_json_path: if value['new'] != old_json_path:
return value['new'] return value['new']
for _, value in file_map_dict.items(): for _, value in file_map_dict.items():
for old_json_path in value['old']: for old_json_path in value['old']:
# Match paths using filename only
if old_json_path.endswith(old_path.split("/")[-1]): if old_json_path.endswith(old_path.split("/")[-1]):
if value['new'] != old_json_path: if value['new'] != old_json_path:
return value['new'] return value['new']
def update_vse_references(file_map_dict: dict) -> None: def update_vse_references(file_map_dict: dict) -> None:
"""Update file references for VSE strips
Args:
file_map_dict (dict): Dictionary of 'old' and 'new' paths
"""
global file_updated global file_updated
for scn in bpy.data.scenes: for scn in bpy.data.scenes:
if not scn.sequence_editor: if not scn.sequence_editor:
@ -80,7 +107,7 @@ def update_vse_references(file_map_dict: dict) -> None:
for path in paths_for_vse_strip(strip): for path in paths_for_vse_strip(strip):
if path == "": if path == "":
continue continue
new_path = find_new_from_old(path, file_map_dict) new_path = dict_find_new_from_old(path, file_map_dict)
if not new_path: if not new_path:
print(f"No new path for '{strip.name}' at '{path}' ") print(f"No new path for '{strip.name}' at '{path}' ")
continue continue
@ -110,10 +137,15 @@ def update_vse_references(file_map_dict: dict) -> None:
def update_referenced_images(file_map_dict: dict) -> None: def update_referenced_images(file_map_dict: dict) -> None:
"""Update file references for Image data-blocks
Args:
file_map_dict (dict): Dictionary of 'old' and 'new' paths
"""
global file_updated global file_updated
for img in bpy.data.images: for img in bpy.data.images:
if img.filepath is not None and img.filepath != "": if img.filepath is not None and img.filepath != "":
new_path = find_new_from_old(img.filepath, file_map_dict) new_path = dict_find_new_from_old(img.filepath, file_map_dict)
if new_path: if new_path:
print(f"Remapping Image Datablock {img.filepath }") print(f"Remapping Image Datablock {img.filepath }")
img.filepath = new_path img.filepath = new_path
@ -121,9 +153,14 @@ def update_referenced_images(file_map_dict: dict) -> None:
def update_libs(file_map_dict: dict) -> None: def update_libs(file_map_dict: dict) -> None:
"""Update file references for libraries (linked/appended data)
Args:
file_map_dict (dict): Dictionary of 'old' and 'new' paths
"""
global file_updated global file_updated
for lib in bpy.data.libraries: for lib in bpy.data.libraries:
new_path = find_new_from_old(lib.filepath, file_map_dict) new_path = dict_find_new_from_old(lib.filepath, file_map_dict)
if new_path: if new_path:
lib.filepath = new_path lib.filepath = new_path
print(f"Remapping {lib.filepath}") print(f"Remapping {lib.filepath}")
@ -131,10 +168,11 @@ def update_libs(file_map_dict: dict) -> None:
def remap_all_blender_paths(): def remap_all_blender_paths():
"""Remap all references to files from blender via dictionary"""
start = time.time() start = time.time()
args = parser.parse_args() args = parser.parse_args()
json_file_path = args.path # File Path to read/write JSON File to json_file_path = args.path
file_map_json = Path(json_file_path) file_map_json = Path(json_file_path)
file_map_data = open(file_map_json) file_map_data = open(file_map_json)