0
$\begingroup$

is there a method to write each collection and its content of a given blend file into separate blend files, each new blend with the name of the collection?

The process should be:

  • Take a blend file x.blend with n collections
  • specify the target folder where to save the output
  • each collection n of x.blend is then saved into the given target folder as a separated blend file n.blend, named after the collection.

Thanks for help!

$\endgroup$
2
$\begingroup$

The following uses hard coded windows paths for the ip_path, op_path but as the intention was to specify your own path and file this should be no issue.

  • Open a new blender file (the script will be removing all content from collections in the current file while running).
  • Copy the code into a text editor.
  • Modify the ip_path, ip_fname, op_path for your specific targets.
  • Run the script.
import bpy
import os


# folder containing blend files
ip_path = "C:\\Blender Projects\\bridge\\"
ip_fname = "bridge_002.blend"

# target folder
op_path = "C:\\Blender Projects\\bridge\\temp\\"

blendfiles = [ip_fname,]

def clear_cur_colls():
    while len(bpy.data.collections) > 0:
        bpy.data.collections.remove(bpy.data.collections[0])
    override = bpy.context.copy()
    override["area.type"] = ['OUTLINER']
    override["display_mode"] = ['ORPHAN_DATA']
    bpy.ops.outliner.orphans_purge(override)

def get_coll_names(assets_path):
    names = []
    with bpy.data.libraries.load(assets_path) as (data_from, data_to):
        names = [name for name in data_from.collections]
    return names

for file in blendfiles:
    # prep to make a folder based on blend file name
    filepath = os.path.join(ip_path, file)

    # make a folder based on blend file name to store new files
    if not os.path.isdir(op_path):
        os.mkdir(op_path)
    # get each object from blend file
    colls = get_coll_names(filepath)
    for coll in colls:
        # clear the current file
        clear_cur_colls()
        # append the object
        bpy.ops.wm.append(filepath=file, directory=str(filepath)+"\\Collection\\", filename=coll)
        # save blend file
        bpy.ops.wm.save_as_mainfile(filepath=os.path.join(op_path, coll+".blend"))

clear_cur_colls()
$\endgroup$
3
  • $\begingroup$ Hi Ratt,thank you very much for your detailed answer. One more question though: Why is it that the content of the collections of the input file will be removed? The initial file should remain unchanged, or at least as an option to choose to either keep it as is, or the remove all contents. Nevertheless i thank you for your great support! $\endgroup$ – pixelpoems.de Apr 26 at 7:45
  • $\begingroup$ @pixelpoems.de Your original file with multiple collections of items you want to retrieve from remains unchanged. The script clears it's own (open file's) collections so that the 1st save won't have your default scene cube light etc in it; and after the script imports (appends) a collection from your ip_path, ip_fname it saves itself as the collection name, it needs to clear itself before importing the next collection in the list of collections from your original file. This avoids using auto execute scripting and system commands to attempt to create blank blend files. $\endgroup$ – Ratt Apr 26 at 13:55
  • $\begingroup$ Hi Ratt, now i got it. Thanks a lot for your help! The Question is now solved. $\endgroup$ – pixelpoems.de Apr 28 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.