Created
February 1, 2026 15:59
-
-
Save zvodd/6a405c067d12f11f6b108258a5777888 to your computer and use it in GitHub Desktop.
Link all Objects from Blender Files under a subdirectory
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 bpy | |
| import os | |
| def link_objects_recursively(root_directory): | |
| """ | |
| Recursively walks through a directory, finds all .blend files, | |
| and links ALL objects from them into the current scene. | |
| """ | |
| # 1. Clean up the path input | |
| # If the user provides a relative path like "./assets", convert to absolute | |
| if root_directory.startswith("//") or root_directory.startswith("./"): | |
| root_directory = bpy.path.abspath(root_directory) | |
| abs_path = os.path.abspath(root_directory) | |
| if not os.path.exists(abs_path): | |
| print(f"ERROR: Directory not found: {abs_path}") | |
| return | |
| print(f"--- Starting recursive link from: {abs_path} ---") | |
| # 2. Walk through every subdirectory | |
| for dirpath, dirnames, filenames in os.walk(abs_path): | |
| for filename in filenames: | |
| if filename.endswith(".blend"): | |
| filepath = os.path.join(dirpath, filename) | |
| # Exclude the current file if it happens to be in the folder to prevent recursion errors | |
| if filepath == bpy.data.filepath: | |
| continue | |
| print(f"Linking from: {filename}") | |
| try: | |
| # 3. Open the file context | |
| # link=True (Link/Reference), link=False (Append/Copy) | |
| with bpy.data.libraries.load(filepath, link=True) as (data_from, data_to): | |
| # data_from.objects is a list of object names available in the source | |
| # We assign them to data_to.objects to import them | |
| data_to.objects = data_from.objects | |
| # 4. Link imported data blocks to the active Scene Collection | |
| # (Otherwise they exist in memory but aren't visible in the viewport) | |
| for obj in data_to.objects: | |
| if obj is not None: | |
| # Check if object is already in the collection to avoid errors | |
| if obj.name not in bpy.context.collection.objects: | |
| bpy.context.collection.objects.link(obj) | |
| except Exception as e: | |
| print(f"Failed to process {filename}. Error: {e}") | |
| print("--- Finished Linking ---") | |
| # ========================================== | |
| # CONFIGURATION | |
| # ========================================== | |
| # Replace this string with your directory path. | |
| # You can use absolute paths (C:/Assets) or relative paths (//Assets) | |
| # '//' denotes the directory where your current .blend file is saved. | |
| TARGET_DIRECTORY = "//assets/" | |
| # Execute the function | |
| link_objects_recursively(TARGET_DIRECTORY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment