Remap: Refactor scripts #177
@ -5,6 +5,7 @@ import json
|
||||
import hashlib
|
||||
import time
|
||||
import contextlib
|
||||
from typing import List
|
||||
|
||||
file_updated = False
|
||||
|
||||
@ -30,7 +31,16 @@ def override_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"):
|
||||
return [strip.filepath]
|
||||
if hasattr(strip, "directory"):
|
||||
@ -41,9 +51,9 @@ def paths_for_vse_strip(strip):
|
||||
|
||||
|
||||
def generate_checksum(filepath: str) -> str:
|
||||
"""
|
||||
Generate a checksum for a zip file
|
||||
Arguments:
|
||||
"""Generate a checksum for a zip file
|
||||
|
||||
Args:
|
||||
archive_path: String of the archive's file path
|
||||
Returns:
|
||||
sha256 checksum for the provided archive as string
|
||||
@ -58,20 +68,37 @@ def generate_checksum(filepath: str) -> str:
|
||||
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 old_json_path in value['old']:
|
||||
# Match paths using the filepath stored in Blender
|
||||
if old_json_path.endswith(old_path.split("/..")[-1]):
|
||||
if value['new'] != old_json_path:
|
||||
return value['new']
|
||||
for _, value in file_map_dict.items():
|
||||
for old_json_path in value['old']:
|
||||
# Match paths using filename only
|
||||
if old_json_path.endswith(old_path.split("/")[-1]):
|
||||
if value['new'] != old_json_path:
|
||||
return value['new']
|
||||
|
||||
|
||||
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
|
||||
for scn in bpy.data.scenes:
|
||||
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):
|
||||
if path == "":
|
||||
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:
|
||||
print(f"No new path for '{strip.name}' at '{path}' ")
|
||||
continue
|
||||
@ -110,10 +137,15 @@ def update_vse_references(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
|
||||
for img in bpy.data.images:
|
||||
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:
|
||||
print(f"Remapping Image Datablock {img.filepath }")
|
||||
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:
|
||||
"""Update file references for libraries (linked/appended data)
|
||||
|
||||
Args:
|
||||
file_map_dict (dict): Dictionary of 'old' and 'new' paths
|
||||
"""
|
||||
global file_updated
|
||||
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:
|
||||
lib.filepath = new_path
|
||||
print(f"Remapping {lib.filepath}")
|
||||
@ -131,10 +168,11 @@ def update_libs(file_map_dict: dict) -> None:
|
||||
|
||||
|
||||
def remap_all_blender_paths():
|
||||
"""Remap all references to files from blender via dictionary"""
|
||||
start = time.time()
|
||||
|
||||
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_data = open(file_map_json)
|
||||
|
Loading…
Reference in New Issue
Block a user