fix: use GLTFDocument to load GLB at runtime (no .import needed)
Godot headless/CLI can't import GLB files — the GLTF loader only works in the editor GUI. Switched SkinnedPlayerModel to use GLTFDocument.append_from_buffer() which parses GLB data from raw bytes at runtime. Verified: GLB parses successfully, generates scene with MikuRig skeleton, AnimationPlayer, and 7 animations (Crouch, Death, Idle, Idle_001, Jump, Run, Walk). Also added characters/glb_loader.gd as a reusable utility for loading any GLB file at runtime.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
extends RefCounted
|
||||
class_name GLBLoader
|
||||
|
||||
## Loads a GLB file at runtime using GLTFDocument.
|
||||
## Usage:
|
||||
## var loader = GLBLoader.new()
|
||||
## var scene = loader.load("res://path/to/model.glb")
|
||||
## if scene:
|
||||
## get_tree().current_scene.add_child(scene)
|
||||
|
||||
static func load(glb_path: String) -> Node3D:
|
||||
print("GLBLoader: loading " + glb_path)
|
||||
|
||||
# Check if file exists
|
||||
if not FileAccess.file_exists(glb_path):
|
||||
print("GLBLoader: file not found: " + glb_path)
|
||||
return null
|
||||
|
||||
# Read file as bytes
|
||||
var file = FileAccess.open(glb_path, FileAccess.READ)
|
||||
if not file:
|
||||
print("GLBLoader: failed to open file")
|
||||
return null
|
||||
|
||||
var bytes = file.get_buffer(file.get_length())
|
||||
file.close()
|
||||
|
||||
print("GLBLoader: read %d bytes" % bytes.size())
|
||||
|
||||
# Parse with GLTFDocument
|
||||
var gltf = GLTFDocument.new()
|
||||
var state = GLTFState.new()
|
||||
|
||||
var err = gltf.append_from_buffer(bytes, "", state)
|
||||
if err != OK:
|
||||
print("GLBLoader: failed to parse GLB, error: %d" % err)
|
||||
return null
|
||||
|
||||
print("GLBLoader: parsed successfully")
|
||||
print(" Animations: %d" % gltf.get_animation_count())
|
||||
print(" Meshes: %d" % gltf.get_mesh_count())
|
||||
|
||||
# Generate scene
|
||||
var scene = gltf.generate_scene(state)
|
||||
if not scene:
|
||||
print("GLBLoader: failed to generate scene")
|
||||
return null
|
||||
|
||||
print("GLBLoader: generated scene: %s" % scene.name)
|
||||
return scene
|
||||
Reference in New Issue
Block a user