harvest/game/tools/build_player_animations.gd
2026-10-03 12:14:28 -04:00

95 lines
3.6 KiB
GDScript

extends SceneTree
## Builds res://game/resources/player_animations.res: the KayKit Rig_Medium clips the farmer
## uses, copied out of the shared animation files into one AnimationLibrary so the player
## scene's AnimationPlayer can reference (and the editor can preview) them.
## Tracks for bones the mannequin lacks (handslot.l/.r) are dropped; loop flags are set here
## because every source clip is imported as non-looping. Source assets are not modified.
## Re-run after editing CLIPS (editor closed):
## godot --headless --path . --script res://game/tools/build_player_animations.gd
const MANNEQUIN := "res://assets/kaykit_characters/characters/Mannequin_Medium.glb"
const ANIM_DIR := "res://assets/kaykit_characters/animations/Rig_Medium/"
const OUTPUT := "res://game/resources/player_animations.res"
const SKELETON_PATH := "Rig_Medium/Skeleton3D"
## Clip name (kept as in KayKit) -> [source file, loops].
const CLIPS := {
"Idle_A": ["Rig_Medium_General.glb", true],
"Idle_B": ["Rig_Medium_General.glb", true],
"Interact": ["Rig_Medium_General.glb", false],
"PickUp": ["Rig_Medium_General.glb", false],
"Use_Item": ["Rig_Medium_General.glb", false],
"Walking_A": ["Rig_Medium_MovementBasic.glb", true],
"Running_A": ["Rig_Medium_MovementBasic.glb", true],
"Digging": ["Rig_Medium_Tools.glb", true],
"Chopping": ["Rig_Medium_Tools.glb", true],
"Hammering": ["Rig_Medium_Tools.glb", true],
"Waving": ["Rig_Medium_Simulation.glb", false],
}
func _initialize() -> void:
var ok := _build()
quit(0 if ok else 1)
func _build() -> bool:
var bones := _mannequin_bones()
if bones.is_empty():
return false
var library := AnimationLibrary.new()
var sources := {} # file -> instantiated scene root (freed at the end)
var dropped := 0
var ok := true
for clip: String in CLIPS:
var file: String = CLIPS[clip][0]
if not sources.has(file):
var scene := load(ANIM_DIR + file) as PackedScene
if scene == null:
push_error("missing animation file " + file)
ok = false
continue
sources[file] = scene.instantiate()
var player := (sources[file] as Node).get_node_or_null("AnimationPlayer") as AnimationPlayer
if player == null or not player.has_animation(clip):
push_error("clip %s not found in %s" % [clip, file])
ok = false
continue
var anim := player.get_animation(clip).duplicate(true) as Animation
for t in range(anim.get_track_count() - 1, -1, -1):
var path := anim.track_get_path(t)
var keep := String(path.get_concatenated_names()) == SKELETON_PATH \
and path.get_subname_count() == 1 and bones.has(String(path.get_subname(0)))
if not keep:
anim.remove_track(t)
dropped += 1
anim.loop_mode = Animation.LOOP_LINEAR if CLIPS[clip][1] else Animation.LOOP_NONE
library.add_animation(clip, anim)
for file: String in sources:
(sources[file] as Node).free()
if not ok:
return false
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(OUTPUT.get_base_dir()))
var err := ResourceSaver.save(library, OUTPUT)
if err != OK:
push_error("could not save %s (error %d)" % [OUTPUT, err])
return false
print("saved %s: %d clips, %d foreign tracks dropped" % [OUTPUT, library.get_animation_list().size(), dropped])
return true
func _mannequin_bones() -> Dictionary:
var scene := load(MANNEQUIN) as PackedScene
if scene == null:
push_error("missing mannequin " + MANNEQUIN)
return {}
var root := scene.instantiate()
var skel := root.get_node_or_null(SKELETON_PATH) as Skeleton3D
var bones := {}
if skel == null:
push_error("mannequin skeleton not found at " + SKELETON_PATH)
else:
for b in skel.get_bone_count():
bones[skel.get_bone_name(b)] = true
root.free()
return bones