Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGArtPython/d4f6dc609da2530dd86c09bfc5d62473 to your computer and use it in GitHub Desktop.
Save CGArtPython/d4f6dc609da2530dd86c09bfc5d62473 to your computer and use it in GitHub Desktop.
Beginner Blender Python tutorial code for linking objects, materials, and scens from one .blend file into another (tutorial video: https://youtu.be/ZrN9w8SMFjo)
# extend Python's functionality to work with file paths
import pathlib
# give Python access to Blender's functionality
import bpy
def remove_libraries():
"""remove the linked blend files"""
bpy.data.batch_remove(bpy.data.libraries)
def link_blend_file_objects(blend_file_path, link=False):
"""link the blender file objects into the current blender file"""
with bpy.data.libraries.load(blend_file_path, link=link) as (data_from, data_to):
data_to.objects = data_from.objects
scene = bpy.context.scene
# link the objects into the scene collection
for obj in data_to.objects:
if obj is None:
continue
scene.collection.objects.link(obj)
def link_blend_file_scenes(blend_file_path, link=False):
"""link the blender file scenes into the current blender file"""
with bpy.data.libraries.load(blend_file_path, link=link) as (data_from, data_to):
data_to.scenes = data_from.scenes
def link_blend_file_materials(blend_file_path, link=False):
"""link the blender file materials into the current blender file"""
with bpy.data.libraries.load(blend_file_path, link=link) as (data_from, data_to):
for material_name in data_from.materials:
if "blue" in material_name:
data_to.materials.append(material_name)
def main():
remove_libraries()
# define the path to the blend file
blend_file_path = str(pathlib.Path().home() / "tmp" / "objects.blend")
link_blend_file_objects(blend_file_path, link=True)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment