How to copy mesh to new files with Python? I have a blend file with hundreds of mesh objects as part of a kit bash pack.

I want to copy each mesh to its own file and turn those files into KPacks for Kit Ops addon.

To export each mesh into a new file I tried to use this code:

import bpy

base_path = "c:\\temp\\"

for idx, obj in enumerate(bpy.data.objects):
    bpy.data.libraries.write(f'{base_path}obj_{idx}.blend', {obj}, fake_user=True)


The problem is that in the files created by the script, only the data blocks are copied, there are no meshes created.

How can I copy each mesh to a new file? It would be very tedious to do that manually for hundreds of files.
Found how to do it. The code I had was half the solution.

After creating a new file, executing the following script adds the mesh to a collection in the scene as I needed. It assumes there's no collection and only one data block in the blend file.

add_object.py

import bpy

collection = bpy.data.collections.new("Collection 1")
bpy.context.scene.collection.children.link(collection)
collection.objects.link(bpy.data.objects[0])
bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)



To execute the script, I executed this in a loop:

blender.exe  &lt;path_to_new_blend_file&gt; --background --python add_object.py