md2 Module

Source code: md2.py

The md2 module provides an Md2 class which derives from ReadWriteFile and is used to read and write Quake md2 data.

vgio.quake2.md2.is_md2file(filename)

Quickly see if a file is a md2 file by checking the magic number.

The filename argument may be a file for file-like object.

Parameters

filename – File to check as string or file-like object.

Returns

True if given file’s magic number is correct.

Md2 Class

class vgio.quake2.md2.Md2

Class for working with Md2 files

Example

Basic usage:

from vgio.quake2.md2 import Md2
m = Md2.open(file)
identity

The magic number of the file, must be b’IDP2’

version

The version of the file, should be 8.

skin_width

The pixel width of the skin texture.

skin_height

The pixel height of the skin texture.

frames

A sequence of Frame objects.

skins

A sequence of Skin objects.

st_vertexes

A sequence of StVertex objects.

triangles

A sequence of Triangle objects.

gl_commands

A sequence of GlCommand objects.

Md2.__init__()

Constructs an Md2 object.

classmethod Md2.open(file, mode='r')

Open a ReadWriteFile object where file can be a path to a file (a string), or a file-like object.

The mode parameter should be ‘r’ to read an existing file, ‘w’ to truncate and write a new file, or ‘a’ to append to an existing file.

open() is also a context manager and supports the with statement:

with ReadWriteFile.open('file.ext') as file:
    file.save('file2.ext')
Parameters
  • file – Either the path to the file, a file-like object, or bytes.

  • mode – An optional string that indicates which mode to open the file

Returns

An ReadWriteFile object constructed from the information read from the file-like object.

Raises
  • ValueError – If an invalid file mode is given.

  • TypeError – If attempting to write to a bytes object.

  • OSError – If the file argument is not a file-like object.

Md2.close()

Closes the file pointer if possible. If mode is ‘w’ or ‘a’, the file will be written to.

Md2.save(file)

Writes data to file.

Parameters

file – Either the path to the file, or a file-like object.

Raises

OSError – If file argument is not a file-like object.

Skin Class

class vgio.quake2.md2.Skin(name)
Skin.__init__(name)
classmethod Skin.read(file)
classmethod Skin.write(file, skin)

TriVertex Class

class vgio.quake2.md2.TriVertex(x, y, z, light_normal_index)

Class for representing a trivertex

A TriVertex is a set of XYZ coordinates and a light normal index.

Note

The XYZ coordinates are packed into a (0, 0, 0) to (255, 255, 255) local space. The actual position can be calculated:

position = (packed_vertex * frame.scale) + frame.translate

Note

The light normal index is an index into a set of pre-calculated normal vectors. These can be found in the anorms attribute of the quake2 module.

x

The x-coordinate

y

The y-coordinate

z

The z-coordinate

light_normal_index

The index for the pre-calculated normal vector of this vertex used for lighting.

TriVertex.__init__(x, y, z, light_normal_index)
classmethod TriVertex.read(file)
classmethod TriVertex.write(file, tri_vertex)

StVertex Class

class vgio.quake2.md2.StVertex(s, t)

Class for representing an st vertex

StVertices are similar to UV coordinates but are expressed in terms of surface space and span (0,0) to (texture_width, texture_height).

Note

If an StVertex lies on a seam and belongs to a back facing triangle, the s-component must be incremented by half of the skin width.

s

The x-coordinate on the skin.

t

The y-coordinate on the skin.

StVertex.__init__(s, t)
classmethod StVertex.read(file)
classmethod StVertex.write(file, st_vertex)

Triangle Class

class vgio.quake2.md2.Triangle(vertex_0, vertex_1, vertex_2, st_vertex_0, st_vertex_1, st_vertex_2)

Class for representing a triangle

Note

The triangle winding direction is clockwise.

vertexes

A triple of vertex indexes. XYZ data can be obtained by indexing into the frame.vertexes attribute.

Triangle.__init__(vertex_0, vertex_1, vertex_2, st_vertex_0, st_vertex_1, st_vertex_2)
classmethod Triangle.read(file)
classmethod Triangle.write(file, triangle)

Frame Class

class vgio.quake2.md2.Frame(scale_x, scale_y, scale_z, translate_x, translate_y, translate_z, name)

Class for representing a frame

A Frame is an object that represents the state of the model at a single frame of animation.

scale

The frame scale

translate

The frame offset

name

The name of the frame.

vertexes

A sequence of TriVertex objects.

Frame.__init__(scale_x, scale_y, scale_z, translate_x, translate_y, translate_z, name)
classmethod Frame.read(file, number_of_vertexes)
classmethod Frame.write(file, frame)

GlVertex Class

class vgio.quake2.md2.GlVertex(s, t, vertex)
GlVertex.__init__(s, t, vertex)
classmethod GlVertex.read(file)
classmethod GlVertex.write(file, gl_vertex)

GlCommand Class

class vgio.quake2.md2.GlCommand(mode)
GlCommand.__init__(mode)
classmethod GlCommand.read(file)
classmethod GlCommand.write(file, gl_command)