Skip to content

Instantly share code, notes, and snippets.

@MrMagicPenguin
Last active June 28, 2022 11:13
Show Gist options
  • Save MrMagicPenguin/507446a5a51024fb1329e910ff5c177e to your computer and use it in GitHub Desktop.
Save MrMagicPenguin/507446a5a51024fb1329e910ff5c177e to your computer and use it in GitHub Desktop.
Convert a hierarchy of Blender Collections to a hierarchy of Empties. Useful for formatting .FBX or other file formats on export to reflect in-house hierarchy & organization.
# Credit to Christopher Baumeister on BlenderArtists for the base script.
# I have added logic for nested collections.
# Original topic: https://blenderartists.org/t/blender-2-8-export-fbx-while-keeping-collections-hierarchy/1142655/5
import bpy
sCollection = bpy.context.collection
def parentCol(_colParent, _objParent):
for col in _colParent.children:
# Generate an empty with the name of the parent collection
newObj = bpy.data.objects.new("empty", None)
bpy.context.scene.collection.objects.link(newObj)
# Rename & Parent new empty to higher level empty
newObj.name = col.name
newObj.parent = _objParent
# For objects within a collection, parent to the new empty
if len(col.objects) > 0:
objs = col.objects
for obj in objs:
obj.parent = newObj
else:
parentCol(col, newObj)
# Check for nested collections
if len(col.children) > 0:
for child in col.children:
parentCol(col, newObj)
root = bpy.data.objects.new("empty", None)
bpy.context.scene.collection.objects.link(root)
root.name = sCollection.name
parentCol(sCollection, root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment