harvest/game/tools/build_player_animations.gd
2026-10-04 12:07:36 -04:00

137 lines
5.1 KiB
GDScript

extends SceneTree
## Builds the KayKit animation libraries the game's characters use, copied out of the shared
## animation files into one AnimationLibrary per rig so AnimationPlayers can reference (and the
## editor can preview) them:
## res://game/resources/player_animations.res Rig_Medium: the scarecrow and ground thieves
## res://game/resources/breaker_animations.res Rig_Large: the fence breaker
## Tracks for bones the mannequin lacks (handslot.l/.r on the medium one) 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 BUILDS (editor closed):
## godot --headless --path . --script res://game/tools/build_player_animations.gd
const KIT := "res://assets/kaykit_characters/"
## Per library: mannequin, animation folder, skeleton path, output, and clips (name as in
## KayKit -> [source file, loops]).
const BUILDS := [
{
"mannequin": KIT + "characters/Mannequin_Medium.glb",
"anims": KIT + "animations/Rig_Medium/",
"skeleton": "Rig_Medium/Skeleton3D",
"output": "res://game/resources/player_animations.res",
"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],
"Sneaking": ["Rig_Medium_MovementAdvanced.glb", true],
"Digging": ["Rig_Medium_Tools.glb", true],
"Chopping": ["Rig_Medium_Tools.glb", true],
"Hammering": ["Rig_Medium_Tools.glb", true],
"Lockpicking": ["Rig_Medium_Tools.glb", true],
"Melee_Unarmed_Attack_Punch_A": ["Rig_Medium_CombatMelee.glb", true],
"Hit_A": ["Rig_Medium_General.glb", false],
"Waving": ["Rig_Medium_Simulation.glb", false],
"Cheering": ["Rig_Medium_Simulation.glb", true],
"Death_A": ["Rig_Medium_General.glb", false],
},
},
{
"mannequin": KIT + "characters/Mannequin_Large.glb",
"anims": KIT + "animations/Rig_Large/",
"skeleton": "Rig_Large/Skeleton3D",
"output": "res://game/resources/breaker_animations.res",
"clips": {
"Idle_A": ["Rig_Large_General.glb", true],
"Walking_A": ["Rig_Large_MovementBasic.glb", true],
"Running_A": ["Rig_Large_MovementBasic.glb", true],
"Melee_2H_Idle": ["Rig_Large_CombatMelee.glb", true],
"Melee_2H_Attack": ["Rig_Large_CombatMelee.glb", true],
"Melee_2H_Slam": ["Rig_Large_CombatMelee.glb", true],
"Hit_A": ["Rig_Large_General.glb", false],
"Death_A": ["Rig_Large_General.glb", false],
},
},
]
var _mannequin := ""
var _anim_dir := ""
var _output := ""
var _skeleton := ""
var _clips := {}
func _initialize() -> void:
var ok := true
for b: Dictionary in BUILDS:
_mannequin = b["mannequin"]
_anim_dir = b["anims"]
_output = b["output"]
_skeleton = b["skeleton"]
_clips = b["clips"]
ok = _build() and ok
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 \
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) as Skeleton3D
var bones := {}
if skel == null:
push_error("mannequin skeleton not found at " + _skeleton)
else:
for b in skel.get_bone_count():
bones[skel.get_bone_name(b)] = true
root.free()
return bones