Skip to content

Instantly share code, notes, and snippets.

@eelstork
Last active August 29, 2015 14:07
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 eelstork/00d1b13c3fca9c544961 to your computer and use it in GitHub Desktop.
Save eelstork/00d1b13c3fca9c544961 to your computer and use it in GitHub Desktop.
Export objects in specified LAYERS to the OUTPUT folder; objects will be exported to separate files (children objects export to the same file as their parent). This function will EXCLUDE objects with specified types. Set ONLY_SELECTED to only export selected objects. Set USE_SUBFOLDERS to create a subfolder with same name as current file. Set BA…
# =============================================================
# Copyright TEA DE SOUZA 2014. Free to use and
# modify, do not remove this notice.
# =============================================================
#
# Export objects in specified LAYERS to the OUTPUT folder;
# Objects will be exported to separate files.
# (Children objects export to the same file as their parent)
#
# this function will EXCLUDE objects with specified types.
# Set ONLY_SELECTED to only export selected objects.
# Set USE_SUBFOLDERS to create a subfolder with same name as
# current file.
# Set BACKUP_FILE if you want to create a backup file before
# processing.
#
# File name will match exported object name.
#
# =============================================================
OUTPUT = "MY_OUTPUT_FOLDER"
LAYERS = [0,1,2,3,4,5,6,7,8,9]
EXCLUDE = ['LATTICE','CAMERA','LAMP','SPEAKER']
ONLY_SELECTED = False
USE_SUBFOLDERS = True
USE_COMPRESSION = True
BACKUP_FILE = False
# =============================================================
import bpy
import ntpath
import os.path
from bpy.app.handlers import persistent
# =============================================================
def export(output,layers,useSelection):
print("\n--------------")
print("EXPORT OBJECTS")
print("--------------")
save();
saveAs(bpy.data.filepath+".backup")
print("REGISTER LOAD_POST HANDLER")
print("")
# add app handler
bpy.app.handlers.load_post.append(export_next)
export_next(None)
@persistent
def export_next(o):
if len(KEYS) is 0:
print("ALL OBJECTS EXPORTED")
bpy.app.handlers.load_post.remove(export_next)
return
key = KEYS[0]
KEYS.remove(key)
print("export object: "+key)
exportListOfObjects(MAP[key],key,USE_SUBFOLDERS)
def exportListOfObjects(list,name,useSubfolders):
root = bpy.data.objects[name]
if root.type in EXCLUDE:
print("skip "+name+" of type "+root.type)
revert()
return
print("Preparing to export "+name+".")
for k in bpy.context.scene.objects:
k.select = k.name not in list
numObjToDelete = 0
for k in bpy.context.scene.objects:
if k.select:
numObjToDelete+=1
print("will delete "+k.name)
precount = len(bpy.context.scene.objects)
deleteSelectedObjects()
postcount = len(bpy.context.scene.objects)
if precount-postcount!=numObjToDelete:
print("after delete still have "+str(postcount)+" objects, expected "+str(precount-numObjToDelete))
doContinue = false
path=makePath(name,useSubfolders)
saveAs(path)
revert()
def evalGroups(useSelection,layers):
map = dict()
for e in bpy.context.scene.objects:
if useSelection and e.select is False:
continue
if not isInExportableLayer(e,layers):
continue
key = rootName(e)
if key==e.name:
print(e.name+" is a root object.")
else:
print("Add "+e.name+" to "+key)
n = e.name
if key not in map: map[key] = []
map[key].append(n)
return map
def isInExportableLayer(o,layers):
for i in layers:
if o.layers[i]: return True
return False
def rootName(o):
while not (o.parent is None):
o = o.parent
return o.name
def baseName():
n=ntpath.basename(bpy.data.filepath)
return os.path.splitext(n)[0]
def makePath(name,useSubfolders):
path = OUTPUT
if useSubfolders:
path = os.path.join( path, baseName() );
path = os.path.join(path,name+".blend")
return path
def makeNearbyPath(name,useSubfolders):
main = bpy.data.filepath
i = main.rfind("/")
base = main[0:i+1]
path = base
if useSubfolders: path += baseName()+"/";
path += name+".blend"
return path
def save():
bpy.ops.wm.save_as_mainfile(compress=USE_COMPRESSION)
def saveAs(path):
dir=os.path.dirname(path)
if not os.path.exists(dir):
print("create dir: "+dir)
os.makedirs(dir)
print("save: "+path)
bpy.ops.wm.save_as_mainfile(filepath=path, copy=True, compress=USE_COMPRESSION)
def revert():
bpy.ops.wm.revert_mainfile()
def deleteSelectedObjects():
showAllLayers()
bpy.ops.object.delete()
def showAllLayers():
sel = []
for i in range(0,20): sel.append(True)
bpy.context.scene.layers = sel
# =============================================================
# Main section
# =============================================================
MAP = evalGroups(ONLY_SELECTED,LAYERS)
KEYS = list(MAP.keys())
export(OUTPUT,LAYERS,ONLY_SELECTED)
# EOF =========================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment