Skip to content
1        10        20        30        40        50        60        70        801"""Export the current Blender scene to a GLB for Torment.23Run Blender headless with this script after `--`, so Blender's own arguments4stay separate from ours:56    blender -b path/to/model.blend -P tools/blender/export_glb.py -- \7      --out assets/models/my-prop.glb89Only the scene is exported (not cameras or lights by default), with modifiers10applied and Blender's Z-up space converted to glTF's Y-up space. The game's11`GLTFLoader` can then load it like any bundled model, and12`bun scripts/shot.js models` shows it in the gallery.13"""1415import argparse16import sys17from pathlib import Path1819import bpy202122def export_scene(23    out,24    *,25    use_selection=False,26    apply_modifiers=True,27    animations=False,28    cameras=False,29    animation_mode="ACTIONS",30):31    """Write the active scene to a GLB and print a one-line summary."""32    out = Path(out).expanduser().resolve()33    out.parent.mkdir(parents=True, exist_ok=True)3435    meshes = [obj for obj in bpy.context.scene.objects if obj.type == "MESH"]36    if not meshes:37        raise SystemExit("Nothing to export: the scene has no mesh objects.")3839    export_options = {40        "filepath": str(out),41        "export_format": "GLB",42        "use_selection": use_selection,43        "export_apply": apply_modifiers,44        "export_yup": True,45        "export_animations": animations,46        "export_cameras": cameras,47        "export_lights": cameras,48        "export_materials": "EXPORT",49        "export_image_format": "AUTO",50    }51    if animations:52        export_options["export_animation_mode"] = animation_mode53        export_options["export_anim_single_armature"] = True54    bpy.ops.export_scene.gltf(**export_options)5556    triangles = 057    for obj in meshes:58        obj.data.calc_loop_triangles()59        triangles += len(obj.data.loop_triangles)6061    print(62        f"Exported {out} "63        f"({len(meshes)} mesh{'es' if len(meshes) != 1 else ''}, "64        f"{triangles} triangles, {out.stat().st_size / 1024:.1f} KiB)"65    )66    return out676869def main(argv):70    parser = argparse.ArgumentParser(description="Export a Blender scene to GLB.")71    parser.add_argument("--out", required=True, help="destination .glb path")72    parser.add_argument(73        "--select",74        action="store_true",75        help="export only selected objects instead of the whole scene",76    )77    parser.add_argument(78        "--no-apply",79        action="store_true",80        help="keep modifiers unapplied (applied by default)",81    )82    parser.add_argument(83        "--animations",84        action="store_true",85        help="include actions and NLA strips (off by default)",86    )87    parser.add_argument(88        "--cameras",89        action="store_true",90        help="include cameras and lights (off by default)",91    )92    args = parser.parse_args(argv)93    export_scene(94        args.out,95        use_selection=args.select,96        apply_modifiers=not args.no_apply,97        animations=args.animations,98        cameras=args.cameras,99    )100101102if __name__ == "__main__":103    main(sys.argv[sys.argv.index("--") + 1 :] if "--" in sys.argv else [])