ClipPlayer.component
This package holds more than this page lists. See all of its files in its world.
README.md
ClipPlayer
Plays one .animation clip on the skinned body it is attached to, through the
canonical AnimGraph. The graph retargets the clip from its source rig onto the
body's rig and drives the body's bone entities each frame, so any humanoid clip
plays on any humanoid body. It is the single-clip case of the animation
contract: build a one-clip graph on start, then advance and apply it each frame.
Attach it to the same entity as the skinned mesh. The body's rig comes from its
Skeleton; the clip's source rig from the clip itself (or sourceRig). All the
retarget and pose machinery lives in the graph — this component only configures
the clip.
Where the clip runs
Once the clip has bound onto the body, the engine runs the graph every frame in
place of the component's update: it advances the playhead, samples the clip
and writes the pose with no script running for the body, so a crowd of bodies
each playing a clip costs no Luau per body. A stopped clip (playing = false)
or one that has finished has its pose evaluated once and then held: the clip is
not sampled again until it plays, and the engine keeps the held pose on the
bones as it keeps a playing one there, putting back any bone a script, a
constraint or an editor move turned away from it and writing nothing on a frame
where no bone moved. stats() says where the graph runs:
local where = entity.find("hero").component.get("ClipPlayer"):stats()
-- { native = true, handle, frames } while the engine runs it,
-- { native = false, refusal } while it runs in script
The graph runs in script while startedAt places its playhead (the session
clock is read in update) and while it plays blend-shape tracks.
entity.find("hero").component.add("ClipPlayer", { clip = "A_Walk_F_Masc" })
Public fields: clip (the .animation to play), sourceRig (the clip's source
rig; empty resolves from the clip), looping, speed, playing and
startedAt.
Playing in phase on every peer
startedAt is a session time (multiplayer.sessionTime(), seconds on the
timeline every participant of a session reads alike). Set, the playhead sits at
(sessionTime - startedAt) * speed on every frame: wrapped by a looping clip,
held at the end by one that plays once, and held at the start before
startedAt arrives. The playhead follows the session clock on every frame the
component updates, whatever the engine's time scale. While the engine is
paused the component does not update and the playhead holds; on resume it is
placed in phase again. The field replicates like the others, so every peer that
holds the component plays the clip at the same clip time, within the session
clock's accuracy (multiplayer.sessionClock().accuracyMs). Choose a start a
little in the future so the value reaches every peer before the clip begins:
local player = entity.find("dancer").component.get("ClipPlayer")
player.startedAt = multiplayer.sessionTime() + 0.5
Unset, the clip advances from wherever its playhead is, by each frame's time.
Faces: blend-shape tracks
A clip can carry blend-shape (morph target) weight tracks beside its bones, one
per shape, addressed by the shape's name: an imported glTF keeps its weights
animations and an imported FBX its blend-shape curves. ClipPlayer plays those
tracks on the meshes of the body that carry shapes of those names, so a smile,
a blink or a set of visemes plays like any other clip. Attached to a mesh with
blend shapes and no skin (a face mesh), it plays the clip's weight tracks alone:
entity.find("face").component.add("ClipPlayer", {
clip = asset.resolve("Smile", "animation"),
looping = true,
})
skeleton.clipShapes(bytes) names the shapes a clip drives. To play a face
clip over a body clip, layer it on the body's graph with a shape mask
(AnimGraph.Layer with AnimGraph.shapeMask(layout, "face")) or use
AnimOverlay with region = "face"; the animation guide's "Faces" section
covers both.