Introduction
The Source engine’s studio model is the format Valve used for Half-Life 2 and the games built on
the Source engine. It keeps the idea of the Half-Life studio
model (a skeleton of bones, meshes attached to it, animation sequences, hit boxes and
attachment points) but it is a different layout, and the vertices and triangles are no longer
inside the .mdl. The file starts with IDST like its predecessor, and the
version that follows is 44 to 49 instead of 10.
Models are compiled by Valve’s studiomdl tool from source files such as
SMD meshes and animations.
Structure
A Source model is a set of files that share one name and one checksum. The
.mdl holds the header, the skeleton, hit boxes, attachments, sequences, animation,
textures, body parts and the description of every mesh. The vertices and tangents are in a
.vvd file, and the triangle lists, split by level of detail and by bone, are in a
.vtx file. A physics description goes in a .phy file and demand-loaded
animation in a .ani file.
All numbers are little-endian. The header (studiohdr_t) is 408 bytes and gives a
count and a byte offset for each table. Almost every record holds further byte offsets (named
…index) that are relative to the record itself, so a record can be read wherever it
sits, and strings are zero-terminated and reached the same way. The checksum in the
header must be the same in the .vvd, .vtx and .phy files,
or the model does not load.
Offsets and sizes on these pages are not printed in Valve’s header. They are added up from the
declared field types for the 32-bit layout: 4-byte int and float,
12-byte Vector, 16-byte Quaternion and 48-byte matrix3x4_t.
For studiohdr_t the 64-bit build keeps the same layout by using
int placeholders for its runtime pointers.
Versions
The header file defines STUDIO_VERSION 48, and the table below is that version. What
the file says about the others:
- Before 46: some animation index data is incompatible. An
mstudioanimdesc_twhosesectionframesis not 0 (an old.anifile using sections, “v45 only”) cannot be used. - Before 47:
unused4in the header held zero-frame cache data, andzeroframeindexandzeroframespanin each animation were not valid. - Version 47:
zeroframeindexis not honoured; the converter clears it. - The comment on
mstudioeyeball_t::m_bNonFACSsays it was “never used before version 44”. - Version 49 is not mentioned in the file at all.
In this reference
- Header –
studiohdr_t,studiohdr2_t, the header flags and the limits - Skeleton – bones, bone flags, procedural bones, bone controllers, hit boxes, attachments, IK chains and the extra bone tables
- Sequences and animation – animations, sequences, events, layers, movements and the per-bone animation chain
- Textures, meshes and flexes – textures and skins, body parts, models, meshes, eyeballs, flex controllers and rules, and vertex animation
- Vertex and strip files – the
.vvdvertex file and the.vtxtriangle file, with the.anianimation block and.phycollision files, and the structures built in memory - Scenes and sentences – the speech and choreography scenes played on the models:
scenes.image,.vcdand the sentence data of speech sounds
Viewer
Open a version 44 to 49 model from its folder, or drop it together with its .vvd,
.vtx and .ani files of the same name. The viewer draws the model at a chosen level
of detail, skins it to the bones, plays the sequences and animations, shows bones, hit boxes and
attachments, and lists the header, bones, sequences, events, materials, skins, body parts, meshes, flexes
and vertex animation records. Options choose the model of each body part, the skin family, the strip file,
the level of detail, the values of the pose parameters and, for a model with flexes, how vertex
animation numbers are read. A sequence that holds a grid of animations is mixed by its pose parameters as
the engine does, including the three-way mix inside a square of the grid, and one whose cycle is a pose
parameter is held where that value puts it. The values are the running game’s; the viewer starts each at
0, or the nearest end of its range. Another choice plays any one animation of the grid, picked by column
and row. Without the companion files the .mdl records and the skeleton are
shown. An animation block is read from the .ani file at the byte position
datastart of its block record. Files up to 256 MB are read. The .vvd,
.vtx, .ani and .phy files also open on their own.
Flex animation is applied: the flex rules are
worked out from the flex controllers, the bone flex drivers set controllers from the pose, and the flexes
move the vertices before skinning. Each of the first 256 controllers has an option; the others are set
together in one text of controller and value pairs. They start at 0, or the nearest end of their range,
with the face at rest and no delay, so speed has no effect. One pose works through up to
33,554,432 vertex records (512 MB); the view says so over the model if flexes are left out.
A model with a controller named blink can be made to blink. The curve is the client’s
(C_BaseFlex::SetupGlobalWeights): from a blink’s start, over a length D (the setting
blink_duration, 0.2 s), take c = cos((D − s) π / 2D) at s seconds; the weight is 2√c, or
2 − 2√c where that is above 1, and it is added to the controller, the sum held to 0 to 1. When the running
game starts a blink is its own decision; as the viewer’s own choice, an option starts one at the first
moment and then every so many seconds (none unless chosen).
A sequence is played with its auto layers, including layers of those layers, and with the model’s autoplay sequences laid over it; options leave either out. A sequence flagged for it is blended in world space; where the SDK’s code would read a matrix it never set, the viewer takes the parent bone as it stands. A sequence’s layers are worked through up to 512 MB of poses for one pose, a bone’s pose counted as 256 bytes and a layer as at least 1 KB: 16,384 layers on a model of 128 bones, more on a smaller one. Only a damaged file is expected to reach it; the layers past it are left out, and the view says so over the model for every pose that lacks some, as it does for a layer that would lay a sequence over itself. Each of the first 256 body parts that have a choice gets an option of its own (a model is picked by number where a body part holds more than 4,096); the others are chosen together in one text of part and model numbers. Other options place the model by the animation’s root movement, set each bone controller input, and choose how procedural bones are worked out: as a release build of the engine does, with the summed rotation of quaternion triggers brought to length 1, or not at all. Axis interpolation, quaternion interpolation and aim-at bones are applied.
The sequences of included models are loaded and played on the model, joined as the engine joins them, with their
animations, pose parameters, attachments and autoplay locks; the options speak of the joined lists, and a
clip from another file is named after it. Open the model from a folder that holds the game’s
models folder: each included file, and its .ani file, is looked for under the
model’s folder and the folders above it by its whole path, then by shorter ends of it. Files dropped
together are matched by name. A file that is not found is named, with the path the model gives. Version 44
to 49 files are joined: up to 64 from one model (the engine’s own limit), 1,024 models in all and 512 MB
of included files; the view says which were not read. A model’s own tables of events, movements and
rules are listed when that file itself is opened.
Jiggle bones are simulated as the SDK’s code does, and their data is listed. Their motion in the game depends on how many pictures a second it draws: an option chooses 30, 60 (the start), 120 or 300, or leaves them as the animation has them. Another sets how many seconds the animation has been looping before the view starts (3 at the start; 0 starts the springs at rest). Each pass of the view’s loop repeats the same motion. One request simulates up to 8,388,608 bone poses (pictures times bones); beyond that only the last of the time asked for is simulated, from rest, and the view says so over the model. A model attached to a view model that the game draws mirrored is simulated by left-handed rules: another option chooses that.
Inverse kinematics is applied as far as the model
settles it: sequence locks, autoplay locks, and the IK rules of the types self and release, with errors
read from the .mdl or the .ani file. An option leaves it off. Rules that aim at the
ground, the world or another entity’s attachment need the running game and are left out, as is what the game
carries from frame to frame (latched feet, automatic release). A rule of the type unlatch only lets go of
a latched target, so with nothing latched it changes no pose. Choosing one animation of a grid applies the
autoplay locks but no rules, and an animation listed on its own is shown bare. For a damaged file, one pose
works through at most 524,288 locks and rules, and the view says so if some are left out.
The surfaces are textured when the material files are there too: open the model from a folder
that holds the game’s materials folder beside models, or drop the
.vmt and .vtf files with it. Each material is
searched for by the model’s search paths and drawn with the first frame of its $basetexture,
honouring transparency, the alpha test and two-sided drawing; the table “Material files” lists
what was found. One view holds 268,435,456 texture pixels (1 GB) of textures’ first frames; where a model
needs more, the option “Materials: start at material” chooses which materials come first. A texture too
large to be worked out as one is made in pieces, reached by that option and “Materials: start at piece”;
where one material’s triangles reach more than 4,000,000 pieces, “Materials: start at triangle” steps
through them.
The limits are on the texture page.
Not described
The pages follow Valve’s Source SDK 2013 and nothing else.
Known, not yet covered
- Reference: version 49. The SDK header defines version 48 and does not mention 49, so what changed is not documented here.
- Reference: the renderer’s use of a flex’s four targets and of
flexpairis not in the public SDK; the reading used here is not confirmed by its code. - Reference: the collision data inside the solids of the
.phyfile, past their first headers: the trees of convex pieces are read and written by the physics library, which is not in the SDK. - Viewer: of flex animation, the wrinkle values (they blend the textures
$compressand$stretch) and what the running game adds by itself (speech from sound files and scene files, which are listed but not played on the model) are not applied. - Viewer: eyeballs are not drawn as the game draws them: the irises are not aimed and the eyelids do not follow them.
- Viewer: IK rules of the types world, ground and attachment, and the latching and automatic release of IK targets over time, are not applied: they depend on the running game.
- Viewer: of materials, the bump, environment-map and phong layers, detail blend mode 10, proxies that depend on the game or draw random numbers, shader fallback blocks, and the eye and teeth shaders’ own textures are not drawn.
- Viewer: whatever version 49 added to these structures is not read.
- Viewer: the shapes of the solids in a
.phyfile are not drawn, and the file is not laid over the model.
Unknown
- Viewer: how the renderer makes the iris projection and the eyelid weights of an eyeball from its record and the view target. The client passes the target to a renderer whose source is not in the public SDK; the eye shader takes the finished values.
Other formats that use .mdl
The Source engine model is a different format from Quake’s and from Half-Life’s: they share only the extension (Half-Life’s starts with the same four bytes, IDST, but has version 10).
- Quake model (IDPO) – Quake’s animated model: palette skins inside the file and whole-frame animation
- Half-Life model (IDST) – Valve’s skeletal model for Half-Life (GoldSrc): bones, sequences and hit boxes
Links
Developers
- Source SDK 2013 – Valve’s engine headers and tools, including
studiomdl(C++) - Assimp – library that imports dozens of 3D formats, MDL among them (C++)
Reference
- studio.h – the SDK header that defines the model structures (version 48)
- optimize.h – the SDK header that defines the
.vtxfile - studio.cpp – how the flex rules are run
- bone_setup.cpp – how sequences are mixed and laid on, procedural bones and inverse kinematics
- c_baseanimating.cpp – the order the game applies animation, IK and bone matrices in
- jigglebones.cpp – the spring simulation of jiggle bones
- studio_virtualmodel.cpp – how a model and the models it includes are joined
- c_baseflex.cpp – flex controller values and bone flex drivers in the game
- morphaccumulate_ps30.fxc – the shader that mixes a flex’s weights by speed and side