Repository navigation
Expand file tree
/
Copy pathtest_retarget_import.gd
More file actions
150 lines (130 loc) · 5.86 KB
/
Copy pathtest_retarget_import.gd
File metadata and controls
150 lines (130 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
## Guards Godot's built-in retargeting of the demo animation onto the Sidekick rig.
## No baking step is involved: demos/animations/silly_dancing.fbx carries retarget
## settings (bone map + fix_silhouette) in its .import, and the importer produces an
## animation whose tracks already address Synty bone names.
##
## The fixture holds bone-segment directions sampled from the ORIGINAL Mixamo rig, so
## this compares the retargeted result against the source pose without needing the
## un-retargeted file. It fails if the import settings regress - most importantly if
## fix_silhouette/threshold returns to its default 15, which leaves the rigs' 10-23 deg
## finger rest differences uncorrected and over-clenches the hands. Run headless:
## godot --headless -s tests/test_retarget_import.gd
extends SceneTree
const CLIP := "res://demos/animations/silly_dancing.fbx"
const FIXTURE := "res://tests/fixtures/silly_dancing_pose.json"
## Fingers land within a fraction of a degree. The looser overall bound is for the
## upper arms: the profile exported from the Skeleton3D menu contains every bone, so a
## twist bone sits alongside the real chain continuation and the silhouette fixer has an
## ambiguous child to align to, costing ~3 deg there. Visually irrelevant, and the price
## of a profile anyone can produce from the editor; a hand-curated profile listing only
## mappable bones measures 0.07 deg everywhere.
const TOLERANCE_DEGREES := 5.0
const FINGER_TOLERANCE_DEGREES := 1.0
var _failures := 0
var _reported := {}
func _init() -> void:
ProjectSettings.set_setting("kickdot/build/combine_meshes", false)
ProjectSettings.set_setting(SKResourceCache.SETTING_CACHE_DIR, "user://test_kickdot_cache")
call_deferred("_run")
func _run() -> void:
var file := FileAccess.open(FIXTURE, FileAccess.READ)
_check(file != null, "pose fixture loads")
if file == null:
return _finish()
var golden: Dictionary = JSON.parse_string(file.get_as_text())
file.close()
_check(golden.size() >= 150, "fixture has sampled directions (%d)" % golden.size())
# The clip imports straight to an AnimationLibrary, so it drops into an
# AnimationPlayer with no extracted file. Its tracks must already speak Synty's bone
# names and address a plain "Skeleton3D", or it cannot drive a SidekickCharacter.
var library := load(CLIP) as AnimationLibrary
_check(library != null, "clip imports as an AnimationLibrary")
if library == null:
return _finish()
var animation_name := String(library.get_animation_list()[0])
var animation := library.get_animation(animation_name)
var targets := {}
var positions := 0
for track in animation.get_track_count():
var path := animation.track_get_path(track)
targets[String(path.get_subname(0))] = true
if animation.track_get_type(track) == Animation.TYPE_POSITION_3D:
positions += 1
_check_once(String(path).begins_with("Skeleton3D:"), "tracks address Skeleton3D")
_check(targets.has("pelvis") and targets.has("upperarm_l"),
"tracks use Synty bone names (%d bones targeted)" % targets.size())
_check(positions == 1, "hips translation kept (%d position tracks)" % positions)
# Play it on a real character and compare against the source pose.
var parts := SKPartLibrary.new()
parts.scan()
var builder := SKCharacterBuilder.new(parts, null)
builder.animation_library = library
var character := builder.build(SKRecipe.load_from_file("res://synty_assets/characters/Starter_01.sk"))
root.add_child(character)
var skeleton: Skeleton3D = null
var player: AnimationPlayer = null
for child in character.get_children():
if child is Skeleton3D:
skeleton = child
elif child is AnimationPlayer:
player = child
_check(player != null, "character carries the imported library")
if player == null:
return _finish()
player.play(animation_name)
var worst := 0.0
var worst_key := ""
var worst_finger := 0.0
var worst_finger_key := ""
var compared := 0
for key: String in golden:
var parts_key := key.split("@")
var bones := parts_key[0].split(">")
player.seek(float(parts_key[1]), true)
var direction := _segment(skeleton, bones[0], bones[1])
if direction == Vector3.ZERO:
continue
var expected: Array = golden[key]
var reference := Vector3(expected[0], expected[1], expected[2])
var degrees := rad_to_deg(direction.angle_to(reference))
compared += 1
if degrees > worst:
worst = degrees
worst_key = key
if _is_finger(bones[0]) and degrees > worst_finger:
worst_finger = degrees
worst_finger_key = key
_check(compared >= 150, "segments compared (%d)" % compared)
_check(worst_finger < FINGER_TOLERANCE_DEGREES,
"finger segments match the source (worst %.2f deg at %s)" % [worst_finger, worst_finger_key])
_check(worst < TOLERANCE_DEGREES,
"every segment matches the source (worst %.2f deg at %s)" % [worst, worst_key])
character.free()
_finish()
func _segment(skeleton: Skeleton3D, from_name: String, to_name: String) -> Vector3:
var from_bone := skeleton.find_bone(from_name)
var to_bone := skeleton.find_bone(to_name)
if from_bone < 0 or to_bone < 0:
return Vector3.ZERO
var delta := skeleton.get_bone_global_pose(to_bone).origin \
- skeleton.get_bone_global_pose(from_bone).origin
return delta.normalized() if delta.length_squared() > 1e-12 else Vector3.ZERO
func _is_finger(bone_name: String) -> bool:
for prefix in ["thumb", "index", "middle", "ring", "pinky"]:
if bone_name.begins_with(prefix):
return true
return false
## Reports a repeated condition only when it first fails, so a per-track check does not
## print hundreds of identical lines.
func _check_once(ok: bool, what: String) -> void:
if ok or _reported.has(what):
return
_reported[what] = true
_check(false, what)
func _check(ok: bool, what: String) -> void:
if not ok:
_failures += 1
print(" %s %s" % ["ok " if ok else "FAIL", what])
func _finish() -> void:
print("RETARGET IMPORT: %s" % ("ALL PASSED" if _failures == 0 else "%d FAILURES" % _failures))
quit(0 if _failures == 0 else 1)