Last active
November 16, 2023 13:21
-
-
Save riccardobl/afad7b69a4b55801ec4d08c697d151e8 to your computer and use it in GitHub Desktop.
Auto install addons in blend file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zipfile | |
import os | |
import bpy | |
import addon_utils | |
# Get the absolute path of the blend file | |
blend_file_path = bpy.data.filepath | |
blend_file_dir = os.path.dirname(blend_file_path) | |
# Check if ./tmp directory exists, and if not, create it | |
tmp_dir = os.path.join(blend_file_dir, 'tmp') | |
if not os.path.exists(tmp_dir): | |
os.mkdir(tmp_dir) | |
# Get a list of all directories under ./addons | |
addons_dir = os.path.join(blend_file_dir, 'addons') | |
addon_dirs = [d for d in os.listdir( | |
addons_dir) if os.path.isdir(os.path.join(addons_dir, d))] | |
# For each directory, create a zip file in ./tmp with the same name as the directory | |
for addon_dir in addon_dirs: | |
zip_filename = os.path.join(tmp_dir, addon_dir + '.zip') | |
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zip_file: | |
# Add all files and subdirectories in the directory to the zip file | |
for root, dirs, files in os.walk(os.path.join(addons_dir, addon_dir)): | |
for file in files: | |
# Set the arcname to the relative path of the file from the addons folder | |
arcname = os.path.relpath(os.path.join(root, file), addons_dir) | |
zip_file.write(os.path.join(root, file), arcname=arcname) | |
for dir in dirs: | |
# Set the arcname to the relative path of the directory from the addons folder | |
arcname = os.path.relpath(os.path.join(root, dir), addons_dir) | |
zip_file.write(os.path.join(root, dir), arcname=arcname) | |
# Install the addon from the zip file | |
bpy.ops.preferences.addon_install(overwrite=True, filepath=zip_filename) | |
# Enable the addon | |
addon_name = addon_dir.replace('.zip', '') | |
# bpy.ops.preferences.addon_enable(module=addon_name) | |
addon_utils.enable(addon_name) | |
os.remove(zip_filename) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Add your addons as subfolders under 'addons' next to your blend file eg. | |
--/home/user/Documents/myBlenderProject | |
---------------------------------------/myProject.blend | |
---------------------------------------/addons < place your addons here | |
----------------------------------------------/myAddon | |
------------------------------------------------------/[addon files .py] | |
2. Run the script in blender | |
3. Toggle Text -> Register to run automatically when the blend file is opened | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment