0
$\begingroup$

I am trying to find or make a means of opening separate Blender files and exporting the a GLTF from each separate blend file using the same export settings?

Not sure whether to start by making a batch script outside of Blender to open each file, change the export settings to a default setting then export.. or to write a python script inside Blender that imports each file from a folder sequentially and exports from them with a default setting for each GLTF.

$\endgroup$

1 Answer 1

0
$\begingroup$

1.Take the .py script from the code sample below these steps and put it somewhere sensible

  1. The output directories in the file are these, please change to your own: basedir = 'C:\\Users\\###USERNAME###\\Downloads\\TestBatch' output_base_dir = 'C:\\Users\\###USERNAME###\\Downloads\\TestBatch\\compressed'

  2. In powershell/terminal/command line on windows I ran the below command from this location where my Blender was installed, please change the location of 'blender-batch-gltf-export.py' to your own location for it.

Location to open powershell from: 'C:\Program Files\Blender Foundation\Blender 3.4'

Command to run (change the location of the script here): .\blender --background -P C:\Users\###USERNAME###\Downloads\TestBatch\blender-batch-gltf-export.py

  1. The script can be edited easily if you look at the lines 31 and 39 for 'bpy.ops.export_scene.gltf('

e.g. I can add animations export here via adding 'export_animations=True,'

bpy.ops.export_scene.gltf(
            export_animations=True,
            export_format='GLB',
            export_draco_mesh_compression_enable=True,
            filepath=out_path)

You can see documentation on the GLTF export options you can change here, they will match up with those of the Blender GUI export GLTF menu properties:

The script, save as blender-batch-gltf-export.py:

import bpy
import os

# Blender application is needed
# Script for Windows
# From Blender install folder: .\blender --background -P C:\Users\###USERNAME###Robert.Farthing\Downloads\TestBatch\blender-batch-gltf-export.py

# Edit your own
basedir = 'C:\\Users\\###USERNAME###\\Downloads\\TestBatch'
output_base_dir = 'C:\\Users\\###USERNAME###\\Downloads\\TestBatch\\compressed'

files = [f for f in os.listdir(basedir) if f.endswith('.blend')]

print(f'Found {len(files)} blend files. Processing...')

for f in files:
    if not f.endswith('.blend'):
        continue
    path = os.path.join(basedir, f)
    bpy.ops.wm.open_mainfile(filepath=path)
    bpy.ops.object.select_all(action='DESELECT')

    try:
        just_name = f.replace('.blend', '_draco')
        out_path = os.path.join(output_base_dir, just_name)
        bpy.ops.export_scene.gltf(
            export_format='GLB',
            export_draco_mesh_compression_enable=True,
            filepath=out_path)
    except:
        just_name = f.replace('.blend', '')
        out_path = os.path.join(output_base_dir, just_name)
        bpy.ops.export_scene.gltf(
            export_format='GLB',
            export_draco_mesh_compression_enable=False,
            filepath=out_path)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .