Skip to content

Instantly share code, notes, and snippets.

@eobet
Last active December 10, 2021 21:39
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 eobet/e3246afe67bad4cee348deb5538991b5 to your computer and use it in GitHub Desktop.
Save eobet/e3246afe67bad4cee348deb5538991b5 to your computer and use it in GitHub Desktop.
Creates a duplicate of the scene containing only instances
import bpy
def full_linked_copy_scene():
scene_copy = bpy.data.scenes.new(bpy.context.scene.name + ' - Full Linked Copy')
# https://blender.stackexchange.com/a/167889/38603
collections_in_scene = [
c for c in bpy.data.collections
if bpy.context.scene.user_of_id(c)
]
# duplicate all collections
for c in collections_in_scene:
collection_copy = bpy.data.collections.new(c.name + ' - Copy')
scene_copy.collection.children.link(collection_copy)
# make instances of all objects in the collection
for o in c.objects:
if o.parent == None:
instance_recursive(o, collection_copy)
return None
def instance_recursive(original_object, target_collection):
instance = original_object.copy()
instance.name = original_object.name + ' - Instance'
target_collection.objects.link(instance)
# not sure why I have to set this...
instance.hide_viewport = False
if len(original_object.children) > 0:
for c in original_object.children:
i = instance_recursive(c, target_collection)
i.parent = instance
return instance
# ensure that every instance can have their own materials
for o in bpy.data.objects:
if o.type == 'MESH':
for s in o.material_slots:
# need to back up the material first, because Blender UX is as always awful
m = s.material
s.link = 'OBJECT'
s.material = m
full_linked_copy_scene()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment