Help with level design with blender

I’m making a code to make export data while using blender for level design based on the youtube video

import bpy
import json
import os

def export_levels(filepath):
    levels = {}

    for collection in bpy.data.collections:
        level_name = collection.name
        level_objects = []

        for obj in collection.all_objects:
            if obj.type in {'MESH', 'EMPTY'}:
                entry = {
                    "name": obj.name,
                    "type": obj.type,
                    "location": list(obj.location),
                    "rotation": list(obj.rotation_euler),
                    "scale": list(obj.scale),
                    "custom_properties": {
                        k: obj[k] for k in obj.keys() if k not in '_RNA_UI'
                    }
                }
                level_objects.append(entry)

        if level_objects:
            levels[level_name] = level_objects

    with open(filepath, 'w') as f:
        json.dump(levels, f, indent=4)

# Usage
blend_dir = os.path.dirname(bpy.data.filepath)
export_path = os.path.join(blend_dir, "levels.json")
export_levels(export_path)
print(f"Exported levels to {export_path}")

Now i want to make sure the collections are the levels but i have a feeling it might not be right

We are going to be releasing that 3D Platformer starter kit when 3.8.5 releases, it will include all the python code we used for the export. The layout looks ok to me. Perhaps add some more printf statements to print the names of the collections that you are processing to make sure the for collection in bpy.data.collections: walks down the tree… it might only do the immediate children…

1 Like