Skip to content

Instantly share code, notes, and snippets.

@g4borg
Last active March 8, 2020 12:34
Show Gist options
  • Save g4borg/6d5c7656b2919b5ef0711e4bb94bef59 to your computer and use it in GitHub Desktop.
Save g4borg/6d5c7656b2919b5ef0711e4bb94bef59 to your computer and use it in GitHub Desktop.
Export Groups in main collection of blender
"""
Exports all groups nested in the main scene collection as separate files.
Useful e.g. for kitbash3d blend files
* Modify the file to your needs (this uses FBX export to the same directory the blend file is residing)
* Open Scripting, paste or load and run this script
* Written and tested in Blender 2.8x
* I as author take no responsibility whatsoever what you do with this script. Make Backups. Modify at your own will.
"""
import bpy
import os
# access the scene
scene = bpy.context.scene
current_path = os.path.dirname(bpy.data.filepath)
# you could select another path, like a subpath like this.
# save_path = os.path.join(current_path, "exp")
save_path = current_path
# start with empty selection
bpy.ops.object.select_all(action='DESELECT')
for group in scene.collection.children:
# select group
for o in group.all_objects:
o.select_set(True)
# export (as FBX, other exports can be used here)
# other export functions documented: https://docs.blender.org/api/current/bpy.ops.export_scene.html
bpy.ops.export_scene.fbx(filepath=os.path.join(save_path, group.name + '.fbx'), use_selection=True)
# e.g. for .obj:
# bpy.ops.export_scene.obj(filepath=os.path.join(save_path, group.name + '.obj'), use_selection=True)
# unselect group
for o in group.all_objects:
o.select_set(False)
# show a message i am done.
# taken from https://blender.stackexchange.com/questions/109711/how-to-popup-simple-message-box-from-python-console
def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
ShowMessageBox(f"Done exporting {len(scene.collection.children)} Children", "Export Script")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment