Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created May 15, 2023 00:09
Show Gist options
  • Save NanoDano/8bcdfee0effc3a258c9a8eca5f1c6a84 to your computer and use it in GitHub Desktop.
Save NanoDano/8bcdfee0effc3a258c9a8eca5f1c6a84 to your computer and use it in GitHub Desktop.
Export a Blender file to glTF from the command-line headless
"""
This script automates the process of exporting the .glb file.
You can run this script from the command-line in a headless mode.
Example usages:
```sh
blender -b -P export_gltf.py mymodel.blend
/Applications/Blender.app/Contents/MacOS/Blender -b -P export_gltf.py "Monkey.blend"
```
"""
import bpy
import sys
# help(bpy)
# Clear all data
bpy.ops.wm.read_factory_settings(use_empty=True)
# Load the .blend file, e.g. mymodel.blend
blender_file = sys.argv[-1]
print(f'Target Blender file for glTF export: {blender_file}')
if not blender_file.endswith('.blend'):
print('Exiting. Please provide a .blend file as input.')
sys.exit(1)
bpy.ops.wm.open_mainfile(filepath=blender_file)
# Export to glTF. No file extension needed, it will replace it anyway.
# Default is .glb unless otherwise specified
bpy.ops.export_scene.gltf(filepath=blender_file)
print(f'Done exporting from .blend to glTF: {blender_file}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment