PMX (Polygon Model eXtended) 2.1

December 19, 2016 ยท View on GitHub

This is an English description of the .PMX file format used in Miku Miku Dance (MMD).

PMX is the successor to the .PMD format (Polygon Model Data).

This is work-in-progress! Please leave feedback in the comments.

Todo

If you are able to help with any of these, please do! Either fork and request a merge or write in the comments below what needs to be done.

  • Figure out how SDEF uses C, R0, R1
  • A lot of unknown/incomplete information in the Bone section onwards

Read Format

  • Must be read in binary mode
  • Format is Little-Endian throughout
  • Text encoding is UTF16LE or UTF8 depending on the global encoding setting

Types

C Types

NameSize (bytes)RangeNotesstd type
byte1-128..127charint8_t
ubyte10..255unsigned charuint8_t
short2-2152^{15}..( 2152^{15} - 1 )shortint16_t
ushort20..( 2162^{16} - 1 )unsigned shortuint16_t
int4-2312^{31}..( 2312^{31} - 1 )intint32_t
uint40..( 2322^{32} - 1 )unsigned intuint32_t
float4IEEE 754 singlefloatfloat

PMX Types

NameSize (bytes)StructureNotes
vec28float, floatXY vector
vec312float, float, floatXYZ vector
vec416float, float, float, floatXYZW vector
text4 + lengthint, byte[]Byte sequence encoding defined in Globals
flag1byte8 flags per byte. 0 = false, 1 = true
index1/2/4byte/ubyte/short/ushort/intType defined in file header, sign depends on usage

Index Types

NameType 1Type 2Type 4Type 1 limitType 2 limitType 4 limitNil Value
Vertexubyteushortint255655352147483647N/A
Bonebyteshortint127327672147483647-1
Texturebyteshortint127327672147483647-1
Materialbyteshortint127327672147483647-1
Morphbyteshortint127327672147483647-1
Rigidbodybyteshortint127327672147483647-1

File Structure

HeaderVer
Model information2.0
[int] Vertex countVertices2.0
[int] Surface countSurfaces2.0
[int] Texture countTextures2.0
[int] Material countMaterials2.0
[int] Bone countBones2.0
[int] Morph countMorphs2.0
[int] Displayframe countDisplayframes2.0
[int] Rigidbody countRigidbodies2.0
[int] Joint countJoints2.0
[int] SoftBody countSoftBodies2.1
FieldStructureValueNotes
Signaturebyte[4]"PMX " [0x50, 0x4D, 0x58, 0x20]Notice the space at the end
Versionfloat2.0, 2.1Compare as floating point values
Globals countbyteFixed at 8 for PMX 2.0This could be variable
Globalsbyte[N]N = Globals countSee Globals below
Model name localtextName of modelUsually in Japanese
Model name universaltextName of modelUsually in English
Comments localtextAdditional information (license)Usually in Japanese
Comments universaltextAdditional information (license)Usually in English

Globals

IndexNameValuesEffect
0Text encoding0, 1Byte encoding for the "text" type, 0 = UTF16LE, 1 = UTF8
1Additional vec4 count0..4Additional vec4 values are added to each vertex
2Vertex index size1, 2, 4The index type for vertices (See Index Types above)
3Texture index size1, 2, 4The index type for textures (See Index Types above)
4Material index size1, 2, 4The index type for materials (See Index Types above)
5Bone index size1, 2, 4The index type for bones (See Index Types above)
6Morph index size1, 2, 4The index type for morphs (See Index Types above)
7Rigidbody index size1, 2, 4The index type for rigid bodies (See Index Types above)

Vertex

Additional Vec4s

Each vertex may gain up to 4 additional vec4 values as defined in the Globals list in the header. Usage tends to vary as most applications will simply pass the values to a vertex shader and let the shader decide the usage.

As an example, additional vec4 may be used for the following;

  • Additional texture UV
  • Specular map UV
  • Normal map UV
  • Vertex colour (PMX 2.1 has a material extension that uses the first additional vec4 for this)

As they are implementation dependant it is most-likely safe to ignore additional vec4, however this isn't a guarantee, a PMX model can be produced expecting additional vec4 to be used in a specific way.

Deform Types

Each vertex can be tied and weighted to bones based on the Bone Index (See Index Types above). A single vertex can be tied to a maximum of 4 bones.

There are multiple types of deforms, each one varies in length and usage.

The bone index of -1 is a nil value, the bone should be ignored.

BDEF 1

Version 2.0

typenameNotes
indexBone indexWeight = 1.0

BDEF2

Version 2.0

typenameNotes
indexBone index 1
indexBone index 2
floatBone 1 weightBone 2 weight = 1.0 - Bone 1 weight

BDEF4

Version 2.0

typenameNotes
indexBone index 1
indexBone index 2
indexBone index 3
indexBone index 4
floatBone 1 weightWeight total not guaranteed to be 1.0
floatBone 2 weightWeight total not guaranteed to be 1.0
floatBone 3 weightWeight total not guaranteed to be 1.0
floatBone 4 weightWeight total not guaranteed to be 1.0

SDEF

Spherical deform blending

Version 2.0

typenameNotes
indexBone index 1
indexBone index 2
floatBone 1 weightBone 2 weight = 1.0 - Bone 1 weight
vec3C???
vec3R0???
vec3R1???

QDEF

Dual quaternion deform blending

Version 2.1

typenameNotes
indexBone index 1
indexBone index 2
indexBone index 3
indexBone index 4
floatBone 1 weightWeight total not guaranteed to be 1.0
floatBone 2 weightWeight total not guaranteed to be 1.0
floatBone 3 weightWeight total not guaranteed to be 1.0
floatBone 4 weightWeight total not guaranteed to be 1.0

Vertex Data

The vertex section starts with an int defining how many vertices there are (Note that it is a signed int).

NameTypeNotes
Positionvec3XYZ
Normalvec3XYZ
UVvec2XY
Additional vec4vec4[N]N is defined in the Globals (Can be 0)
Weight deform typebyte0 = BDEF1, 1 = BDEF2, 2 = BDEF4, 3 = SDEF, 4 = QDEF
Weight deform~See Deform Types above
Edge scalefloatPencil-outline scale (1.0 should be around 1 pixel)

Surface

Surfaces are in groups of 3, they define a single triangle face based on the vertices that they are referencing.

[surface 0 -> A, surface 1 -> B, surface 2 -> C] = triangle [vertex A, vertex B, vertex C]

Triangle facing is defined in clockwise winding order (As with DirectX).

Surface Data

The surface section starts with an int defining how many surfaces there are (Note that it is a signed int).

NameTypeNotes
Vertex indexindexSee Index Types above

Additional Surface Types (PMX 2.1)

The material data is extended for PMX 2.1 to include point and line drawing. See the Material section to learn more about this.

Texture

This is a table of texture paths. The format of the paths is not defined, so it may be system dependant and it may feature absolute paths, this information is beyond the spec.

The file structure itself is also not defined, so textures may be in sub folders or may be expecting textures to be pre-defined by the implementation.

Some implementations will change texture behaviour based on file name, such as using "_s" as sphere map or "_n" as normal map or may have texture entries that do not even reference files but reference some other data.

There is also no preferred texture format; the most common texture formats are probably BMP, PNG, TGA but JPG and DDS formats are also sometimes used.

Reserved texture names

Textures "toon01.bmp" through to "toon10.bmp" are reserved. A model should not be referencing these textures via the texture data section. The resulting behaviour if this occurs is implementation dependant, some implementations will ignore these entries or some will directly reference internal textures.

Texture Data

The texture section starts with an int defining how many textures there are (Note that it is a signed int).

NameTypeNotes
PathtextFile system path to a texture file (Usually relative)

Material

Internally, materials would define the shading for the model when being rendered.

PMX is intended for material-based rendering, not physically-based rendering,

Material Flags

Materials can have the following bit flags:

Bit indexNameSet effectVersion
0No-cullDisables back-face culling2.0
1Ground shadowProjects a shadow onto the geometry2.0
2Draw shadowRenders to the shadow map2.0
3Receive shadowReceives a shadow from the shadow map2.0
4Has edgeHas "pencil" outline2.0
5Vertex colourUses additional vec4 1 for vertex colour2.1
6Point drawingEach of the 3 vertices are points2.1
7Line drawingThe triangle is rendered as lines2.1

Point and Line drawing (PMX 2.1)

The point-drawing flag will override the line-drawing flag if both are set.

  • A standard triangle is rendered with vertices [A->B->C].
  • With point drawing each vertex is rendered as individual points [A, B, C] resulting in 3 points.
  • Line drawing will render 3 lines [A->B, B->C, C->A] resulting in 3 lines.

When point rendering is enabled the edge scale value from the material (Not the vertices) will control the size of the point.

It is undefined if point or lines will have edges if the edge flag is set.

Material Data

The material section starts with an int defining how many materials there are (Note that it is a signed int).

NameTypeNotes
Material name localtextHandy name for the material (Usually Japanese)
Material name universaltextHandy name for the material (Usually English)
Diffuse colourvec4RGBA colour (Alpha will set a semi-transparent material)
Specular colourvec3RGB colour of the reflected light
Specular strengthfloatThe "size" of the specular highlight
Ambient colourvec3RGB colour of the material shadow (When out of light)
Drawing flagsflagSee Material Flags
Edge colourvec4RGBA colour of the pencil-outline edge (Alpha for semi-transparent)
Edge scalefloatPencil-outline scale (1.0 should be around 1 pixel)
Texture indexindexSee Index Types, this is from the texture table by default
Environment indexindexSame as texture index, but for environment mapping
Environment blend modebyte0 = disabled, 1 = multiply, 2 = additive, 3 = additional vec4*
Toon referencebyte0 = Texture reference, 1 = internal reference
Toon valueindex/byteBehaviour depends on Toon reference value**
Meta datatextThis is used for scripting or additional data
Surface countintHow many surfaces this material affects***

* Environment blend mode 3 will use the first additional vec4 to map the environment texture, using just the X and Y values as the texture UV. It is mapped as an additional texture layer. This may conflict with other uses for the first additional vec4.

** Toon value will be a texture index much like the standard texture and environment texture indexes unless the Toon reference byte is equal to 1, in which case Toon value will be a byte that references a set of 10 internal toon textures (Most implementations will use "toon01.bmp" to "toon10.bmp" as the internal textures, see the reserved names for Textures above).

*** Surface count will always be a multiple of 3. It is based on the offset of the previous material through to the size of the current material. If you add up all the surface counts for all materials you should end up with the total number of surfaces.

Bone

Bones are used for vertex animation.

Bone Flags

Bones can have the following bit flags:

Bit indexNameSet effectVersion
0Indexed tail positionIs the tail position a vec3 or bone index2.0
1RotatableEnables rotation2.0
2TranslatableEnables translation (shear)2.0
3Is visible???2.0
4Enabled???2.0
5IKUse inverse kinematics (physics)2.0
8Inherit rotationRotation inherits from another bone2.0
9Inherit translationTranslation inherits from another bone2.0
10Fixed axisThe bone's shaft is fixed in a direction2.0
11Local co-ordinate???2.0
12Physics after deform???2.0
13External parent deform???2.0

Inherit Bone

NameTypeNotes
Parent indexindexSee Index Types
Parent influencefloatWeight of how much influence this parent has on this bone

Bone Fixed Axis

NameTypeNotes
Axis directionvec3Direction this bone points

Bone Local Co-ordinate

NameTypeNotes
X vectorvec3???
Z vectorvec3???

Bone External Parent

NameTypeNotes
Parent indexindex???

IK Angle Limit

NameTypeNotes
Limit minvec3Minimum angle (radians)
Limit maxvec3Maximum angle (radians)
NameTypeNotes
Bone indexindexSee Index Types
Has limitsbyteWhen equal to 1, use angle limits
IK Angle limits~Used if has limits is 1. See IK Angle Limit

Bone IK

NameTypeNotes
Target indexindexSee Index Types
Loop countint???
Limit radianfloat???
Link countintHow many bones this IK links with
IK links~[N]N is link count; can be zero. See IK Links

Bone Data

The bone section starts with an int defining how many bones there are (Note that it is a signed int).

NameTypeNotes
Bone name localtextHandy name for the bone (Usually Japanese)
Bone name universaltextHandy name for the bone (Usually English)
Positionvec3The local translation of the bone
Parent bone indexindexSee Index Types
LayerintDeformation hierarchy
Flagsflag[2]See Bone Flags
Tail positionvec3/indexIf indexed tail position flag is set then this is a bone index
Inherit bone~Used if either of the inherit flags are set. See Inherit Bone
Fixed axis~Used if fixed axis flag is set. See Bone Fixed Axis
Local co-ordinate~Used if local co-ordinate flag is set. See Bone Local Co-ordinate
External parent~Used if external parent deform flag is set. See Bone External Parent
IK~Used if IK flag is set. See Bone IK

Morph

Offset Data

Offset data depends on the morph type.

IndexTypeNotesVersion
0Group2.0
1Vertex2.0
2Bone2.0
3UV2.0
4UV ext12.0
5UV ext22.0
6UV ext32.0
7UV ext42.0
8Material2.0
9Flip2.1
10Impulse2.1

Group

NameTypeNotes
Morph indexindexSee Index Types
InfluencefloatWeight of indexed morph

Vertex

NameTypeNotes
Vertex indexindexSee Index Types
Translationvec3Translation to apply to vertex

Bone

NameTypeNotes
Bone indexindexSee Index Types
Translationvec3Translation to apply to bone
Rotationvec4Rotation to apply to bone

UV

NameTypeNotes
Vertex indexindexSee Index Types
Floatsvec4What these do depends on the UV ext

Material

NameTypeNotes
Material indexindexSee Index Types. -1 is all materials
byte
Diffusevec4
Specularvec3
Specularityfloat
Ambientvec3
Edge colourvec4
Edge sizefloat
Texture tintvec4
Environment tintvec4
Toon tintvec4

Flip

NameTypeNotes
Morph indexindexSee Index Types
InfluencefloatWeight of indexed morph

Impulse

NameTypeNotes
Rigid body indexindexSee Index Types
Local flagbyte
Movement speedvec3
Rotation torquevec3

Morph Data

The morph section starts with an int defining how many morphs there are (Note that it is a signed int).

NameTypeNotes
Morph name localtextHandy name for the morph (Usually Japanese)
Morph name universaltextHandy name for the morph (Usually English)
Panel typebyteSee Morph Panel Types
Morph typebyteSee Morph Types
Offset sizeintNumber of morph offsets
Offset data~[N]N is offset count; can be zero. See Offset Data

Display Frame

Frame Type

IndexTypeNotesVersion
0Bone2.0
1Morph2.0

Bone Frame Data

NameTypeNotes
Bone indexindexSee Index Types

Morph Frame Data

NameTypeNotes
Morph indexindexSee Index Types

Frame Data

NameTypeNotes
Frame typebyteSee Frame Type
Frame data~See Frame Datas

Display Data

The display frame section starts with an int defining how many frames there are (Note that it is a signed int).

NameTypeNotes
Display name localtextHandy name for the display (Usually Japanese)
Display name universaltextHandy name for the display (Usually English)
Special flagbyte0 = normal frame, 1 = special frame
Frame countintHow many frames are in this display
Frames~[N]N is frame count. See Frame Data

Rigid Body

Shape Type

IndexTypeNotesVersion
0Sphere2.0
1Box2.0
2Capsule2.0

Physics Modes

IndexTypeNotesVersion
0Follow boneRigid body sticks to bone2.0
1PhysicsRigid body uses gravity2.0
2Physics + boneRigid body uses gravity pivoted to bone2.0

Rigid Body Data

The rigid body section starts with an int defining how many rigid bodies there are (Note that it is a signed int).

NameTypeNotes
Rigid body name localtextHandy name for the rigid body (Usually Japanese)
Rigid body name universaltextHandy name for the rigid body (Usually English)
Related bone indexindexSee Index Types
Group idbyteGroup id
Non-collision groupshortNon collision mask
ShapebyteSee Shape Type
Shape sizevec3XYZ bounds of the shape
Shape positionvec3XYZ shape location
Shape rotationvec3XYZ shape rotation angles in radians
Massfloat
Move attenuationfloat
Rotation dampingfloat
Repulsionfloat
Friction forcefloat
Physics modebyteSee Physics Modes

Joint

Joint Type

IndexTypeNotesVersion
0Spring 6DOF2.0
16DOF2.1
2P2P2.1
3ConeTwist2.1
4Slider2.1
5Hinge2.1

Joint Data

The joint section starts with an int defining how many joints there are (Note that it is a signed int).

NameTypeNotes
Joint name localtextHandy name for the rigid body (Usually Japanese)
Joint name universaltextHandy name for the rigid body (Usually English)
TypebyteSee Joint Type
Rigid body index AindexSee Index Types
Rigid body index Bindex
Positionvec3
Rotationvec3Rotation angle radian
Position minimumvec3Minimum position value
Position maximumvec3Maximum position value
Rotation minimumvec3Minimum rotation value
Rotation maximumvec3Maximum rotation value
Position springvec3
Rotation springvec3

Soft Body

Soft bodies are based on Bullet Physics and are introduced with PMX 2.1

Shape Types

IndexTypeNotesVersion
0TriMesh2.1
1Rope2.1

Flags

Bit indexNameSet effectVersion
0B-Link???2.1
1Cluster creation???2.1
2Link crossing???2.1

Aerodynamics Models

IndexTypeNotesVersion
0V_Point???2.1
1V_TwoSided???2.1
2V_OneSided???2.1
3F_TwoSided???2.1
4F_OneSided???2.1

Anchor Rigid Bodies

NameTypeNotes
Rigid body indexindexSee Index Types
Vertex indexindexSee Index Types
Near modebyte???

Vertex Pins

NameTypeNotes
Vertex indexindexSee Index Types

Soft Body Data

The soft body section starts with an int defining how many soft bodies there are (Note that it is a signed int).

NameTypeNotes
Soft body name localtextHandy name for the soft body (Usually Japanese)
Soft body name universaltextHandy name for the soft body (Usually English)
ShapebyteSee Shape Types
Material indexindexSee Index Types
GroupbyteGroup id
No collision maskshortNon collision mask
FlagsflagSee Flags
B-link create distanceint???
Number of clustersint???
Total massfloat
Collision marginfloat
Aerodynamics modelintSee Aerodynamics Models
Config VCFfloatVelocities correction factor (Baumgarte)
Config DPfloatDamping coefficient
Config DGfloatDrag coefficient
Config LFfloatLift coefficient
Config PRfloatPressure coefficient
Config VCfloatVolume conversation coefficient
Config DFfloatDynamic friction coefficient
Config MTfloatPose matching coefficient
Config CHRfloatRigid contacts hardness
Config KHRfloatKinetic contacts hardness
Config SHRfloatSoft contacts hardness
Config AHRfloatAnchors hardness
Cluster SRHR_CLfloatSoft vs rigid hardness
Cluster SKHR_CLfloatSoft vs kinetic hardness
Cluster SSHR_CLfloatSoft vs soft hardness
Cluster SR_SPLT_CLfloatSoft vs rigid impulse split
Cluster SK_SPLT_CLfloatSoft vs kinetic impulse split
Cluster SS_SPLT_CLfloatSoft vs soft impulse split
Interation V_ITintVelocities solver iterations
Interation P_ITintPositions solver iterations
Interation D_ITintDrift solver iterations
Interation C_ITintCluster solver iterations
Material LSTintLinear stiffness coefficient
Material ASTintArea / Angular stiffness coefficient
Material VSTintVolume stiffness coefficient
Anchor rigid body countintHow many anchoring rigid bodies there are
Anchor rigid bodies~[N]N is the anchor rigid body count. See Anchor Rigid Bodies
Vertex pin countintHow many vertex pins there are
Vertex pins~[N]N is the vertex pin count. See Vertex Pins