Download

Export mesh as GLTF with Python

This plugin allows for exporting GLB/GLTF out of Unreal.

Problem is that it forces you to either place the assets on the scene before batch exporting them, which is too resource intensive for hundreds of assets, or keeps prompting for each and every asset if you try to export from the content browser.

Solution would be to use Python. The following code will export static meshes that have been selected on the content browser as FBX. Now the only roadblock is to alter that to GLTF and skip the prompt. Searching GLB or GLTF on the Unreal docs turn nothing, and the GLTF docs are cryptic for me.

import unreal

# instances of unreal classes

editor_util = unreal.EditorUtilityLibrary()

system_lib = unreal.SystemLibrary()

# get the selected assets

selected_assets = editor_util.get_selected_assets()

# export selected assets

for asset in selected_assets:

    asset_name = system_lib.get_object_name(asset)

    export_task = unreal.AssetExportTask()

    export_task.set_editor_property("object", asset)

    export_task.set_editor_property("filename", "C:/Desktop/UE_Exported/{}".format(asset_name))

    export_task.set_editor_property("automated", False)

    export_task.set_editor_property("exporter", unreal.StaticMeshExporterFBX())

    unreal.Exporter.run_asset_export_task(export_task)
1 Like

Hack using AHK for the time being. Won’t be able to do anything else while it is running. Can this be accomplished with the Editor Utility Widget?

#SingleInstance, force
SendMode, Input
SetTitleMatchMode, 2
CoordMode, Mouse, Screen
Wait_UE_Export := 0

CapsLock & F1:: ; Hover over Asset Actions > Export button, and press hotkey.
Send, {LButton}
Loop { 
		Loop {
		If WinExist("Save: ")
			{
				Sleep 500
				Control, Choose, 3 , ComboBox2, Save: ; 'Choose, 3' for .gltf, or 'Choose, 2' for .glb
				Send, !d
				Sleep 500
				Send, {Text}C:\Users\Me\Desktop\UE_Exported ; Destination Folder
				Sleep 2000
				Send, {Enter 6}
				Wait_UE_Export := 0
				break
			}
		Wait_UE_Export += 1
		ToolTip, %Wait_UE_Export%
		If (Wait_UE_Export >= 6000) ; ~1 minute
			{
				Send, {Enter 2}
			}
		If (Wait_UE_Export >= 60000) ; ~10 minutes
			{
				break 2
			}
		}
		Loop {
		If WinExist("Export Options")
			{
				Sleep 500
				WinMove, Export Options,, 705, 270
				Sleep 500
				MouseClick, L, 1100, 725, 1, 0
				break
			}
		Wait_UE_Export += 1
		ToolTip, %Wait_UE_Export%
		If (Wait_UE_Export >= 6000) ; ~1 minute 
			{
				Send, {Enter 2}
			}
		If (Wait_UE_Export >= 60000) ; ~10 minutes
			{
				break 2
			}
		}
}
ToolTip, Concluded... If nothing went wrong!
return

$Space:: Send, {Space}
Space & y:: ExitApp

What about:

import unreal

selected_assets = unreal.EditorUtilityLibrary.get_selected_assets()
for asset in selected_assets:
    # set up the gltf export options
    task = unreal.AssetExportTask()
    task.object = asset
    task.filename = "C:/Temp/GLTFExport/"+asset.get_name() + ".gltf"
    task.automated = True           # don't display the export options dialog
    task.replace_identical = True   # always overwrite the output
    task.exporter = unreal.GLTFStaticMeshExporter()
    task.options = unreal.GLTFExportOptions()
    result = unreal.Exporter.run_asset_export_task(task)

This plugin is on the marketplace, thus it is not processed to be added to the official python API.
What you can do is

  1. Turn GLTF Exporter plugin and python plugin on in your project
  2. in project settings, python, turn developer mode on
    image
  3. restart project, look into “Intermediate\PythonStub\unreal.py” you will have the python API including the one for the activated plugins.

Bonus, you can use this stub in your IDE to get some autocompletion.

Bests,
Flavien.

1 Like

Thanks. That worked. It seems that there is no way to export without loading, so now it would be ideal if I could unload each asset after it has been exported, so that the editor only has the minimum amount of data loaded at a time and does not crash upon loading hundreds of assets. I have tried a couple of codes, but none seemed to work so far.

Just started out with Python, watched two 1h tutorials and read OOP in an article a few hours ago. Shooting in the dark, this version gave me no errors, but also does not seem to do anything:

import unreal

system_lib = unreal.SystemLibrary()

selected_assets = unreal.EditorUtilityLibrary.get_selected_assets()

for asset in selected_assets:

    asset_name = system_lib.get_object_name(asset)

    PrimA = unreal.PrimaryAssetId(primary_asset_type=['None'], primary_asset_name=asset_name)

    system_lib.unload_primary_asset(PrimA)

If unloading is not possible, I will just delete the assets with ''unreal.EditorAssetLibrary.delete_loaded_asset(asset)" after creating a backup. Hopefully that releases it from memory as well.

On internal channel I found someone pointing to
unreal.EditorLoadingAndSavingUtils.unload_packages, but mentioning it is also unreliable and will fail if anything still has a reference to the package.