Based on what i tried to do here: JmeConvert tool
These add a gradle task to convert all .blend
files in your assets folder to .blend.glb
files (in folder)
It works by calling a powershell file as a gradle task.
The powershell file enumerates the folder and checks if the generated file needs updating.
This calls the blender.exe with a background task and the python script on each file.
Task in gradle:
task convertBlendFiles(type: Exec) {
workingDir 'src/assets'
//sorry windows only unless you install powershell on linux
commandLine 'cmd', '/c', 'Powershell -File blend2gltf.ps1'
}
The blend2gltf.ps1
called in gradle:
<#
.SYNOPSIS
Converts all .blend files in the project assets folder to .blend.glb files
.NOTES
Author: [email protected]
#>
$ErrorActionPreference = "Stop"
$blenderPath = 'C:\Program Files\Blender Foundation\Blender 2.81\'
$blenderPythonFile = 'blend2gltf.py'
function Convert-gltf {
param(
[Parameter(Mandatory="true")] [string] $FilePath,
[Parameter(Mandatory = "true")] [string] $BlenderPython
)
$generatedFile = @(Get-ChildItem -ErrorAction SilentlyContinue "$FilePath.glb")
$blendFile = Get-ChildItem $FilePath
if (($generatedFile.Length -gt 0) -and ($blendFile.LastWriteTime -lt $generatedFile[0].LastWriteTime)) {
Write-Host "Ignored: $blendFile"
return $false # already converted, so ignore
}
#run blender command and ignore output
& "$blenderPath\blender" $FilePath --background --python $BlenderPython > $null
Write-Host "Converted: $FilePath"
return $true
}
$files = Get-ChildItem -Recurse -Include '*.blend'
Write-Host "$(($files).Length) .blend files found"
$results = $files | ForEach-Object { Convert-gltf $_ $blenderPythonFile } | Where-Object { $_ }
Write-Host "$(($results).Length) updated .blend.glb files"
And the called blend2gltf.py
file:
import bpy
import sys
bpy.ops.export_scene.gltf(filepath=bpy.data.filepath)