Export selected objects to separate files. Files export in same folder as source file. File name will match object name. Parent relationships are ignored.
# =============================================================
# Export selected objects to separate files.
# Files will be exported in the same folder as the source file.
# File name will match exported object name.
# Parent relationships are ignored.
# =============================================================
import bpy
def exportObj(objName):
obj = bpy.context.scene.objects[objName]
main = bpy.data.filepath
i = main.rfind("/")
base = main[0:i+1]
path = base+obj.name+".blend"
if main==path:
print("Skip "+objName+" as this would overwrite source file.")
return
print("export: "+path)
for k in bpy.context.scene.objects:
if k==obj:
k.select = False
else:
k.select = True
bpy.ops.object.delete()
bpy.ops.wm.save_as_mainfile(filepath=path,copy=True,compress=True)
bpy.ops.wm.revert_mainfile()
# ----------------------------------------
print("EXPORT SELECTED OBJECTS")
bpy.ops.wm.save_as_mainfile(compress=True)
# get the list of objects to export -------
sel = []
for k in bpy.context.scene.objects:
if k.select: sel.append(k.name)
# export all selected objects to files
for obj in sel: exportObj(obj)
# finally restore initial selection state
for k in bpy.context.scene.objects:
if k.name in sel:
k.select = True
else:
k.select = False
print("EXPORT COMPLETE")
# =============================================================
# Copyright TEA DE SOUZA 2014. Free to use and
# modify, do not remove this notice.
# =============================================================