if __name__ == "__main__":
import subprocess
import time
import os
import platform
import shutil
import re
import glob
import argparse
parser = argparse.ArgumentParser(description="A script that packages the hallr blender addon files")
parser.add_argument(
"--dev_mode",
action="store_true",
help="Enable development mode"
)
args = parser.parse_args()
if not os.path.isfile("Cargo.toml"):
print("Error: This directory does not contain a Cargo.toml file.")
exit(1)
with open("Cargo.toml", "r") as f:
content = f.read()
if "name = \"hallr\"" not in content:
print("Error: The Cargo.toml file does not specify the project name as 'hallr'. Are you in the correct cwd?")
exit(1)
result = subprocess.run(["cargo", "build", "--release"])
if result.returncode != 0:
print(f"Cargo command failed with return code {result.returncode}.")
exit(1)
timestamp = str(int(time.time()))
system = platform.system()
library_extension = ".dylib"
if system == "Linux":
library_extension = ".so"
elif system == "Windows":
library_extension = ".dll"
source_directory = 'blender_addon'
destination_directory = 'blender_addon_exported'
dest_lib_directory = os.path.join(destination_directory, "lib")
os.makedirs(destination_directory, exist_ok=True)
os.makedirs(dest_lib_directory, exist_ok=True)
lib_files = [f for f in os.listdir("target/release") if f.startswith("libhallr") and f.endswith(library_extension)]
if len(lib_files) == 0:
print(f"Could not find the libfile in ´target/release´.")
exit(1)
old_lib_files = [f for f in os.listdir(dest_lib_directory) if
f.startswith("libhallr_") and f.endswith(library_extension)]
for lib_file in old_lib_files:
old_file = os.path.join(dest_lib_directory, lib_file)
os.remove(old_file)
for lib_file in lib_files:
if args.dev_mode:
new_name = os.path.join(dest_lib_directory, f"libhallr_{timestamp}{library_extension}")
else:
new_name = os.path.join(dest_lib_directory,lib_file)
shutil.copy(f"target/release/{lib_file}", new_name)
file_extension = '.py'
source_files = glob.glob(f"{source_directory}/*{file_extension}")
for source_file in source_files:
shutil.copy(source_file, os.path.join(destination_directory, os.path.basename(source_file)))
base_directory = os.getcwd()
addon_exported_path = os.path.join(base_directory, 'blender_addon_exported')
target_release_path = os.path.join(base_directory, addon_exported_path, 'lib')
file_list = [f for f in os.listdir(addon_exported_path) if
os.path.isfile(os.path.join(addon_exported_path, f)) and f.endswith(".py")]
for file in file_list:
file_path = os.path.join(addon_exported_path, file)
with open(file_path, 'r') as f:
content = f.read()
content = re.sub(r'HALLR__BLENDER_ADDON_PATH', addon_exported_path, content)
content = re.sub(r'HALLR__TARGET_RELEASE', target_release_path, content)
if args.dev_mode:
content = re.sub(r'DEV_MODE = False', 'DEV_MODE = True', content)
content = re.sub(r'from . import', 'import', content)
with open(file_path, 'w') as f:
f.write(content)
if not args.dev_mode:
subprocess.run("mv blender_addon_exported hallr", shell=True)
subprocess.run("zip -r hallr.zip hallr", shell=True) subprocess.run("mv hallr blender_addon_exported", shell=True)
print("Created a new hallr.zip file in the root, install it as an addon in blender.")
else:
print("Updated the files under blender_addon_exported, use blender to run __init__.py")