mdl
Module¶
Source code: mdl.py
The mdl
module provides an Mdl
class which derives
from ReadWriteFile
and is used to read and write Quake
mdl data.
- vgio.quake.mdl.is_mdlfile(filename)¶
Quickly see if a file is a mdl 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.
Mdl
Class¶
- class vgio.quake.mdl.Mdl¶
Class for working with Mdl files
Example
Basic usage:
from vgio.quake.mdl import Mdl m = Mdl.open(file)
- identifier¶
The magic number of the file, must be b’IDPO’
- version¶
The version of the file, should be 6.
- scale¶
The scale of the model. Used to correctly resize the model as the frame vertexes are packed into a (0, 0, 0) to (255, 255, 255) local space.
- origin¶
The offset of the model. Used to correctly position the model.
Note: The frame vertexes are packed into a (0, 0, 0) to (255, 255, 255) local space.
- bounding_radius¶
The bounding radius of the model.
- eye_position¶
The eye position for the model.
- number_of_skins¶
The number of skins contained inside the model.
- skin_width¶
The pixel width of the skin texture.
- skin_height¶
The pixel height of the skin texture.
- number_of_vertexes¶
The number of vertexes for the model.
- number_of_triangles¶
The number of triangles for the model.
- number_of_frames¶
The number of frames for the model.
- synctype¶
The synchronization type for the model. It is either SYNC or RAND.
- flags¶
A bit field of entity effects.
- size¶
The average size of the triangles.
- skins¶
The list of Skin or SkinGroup objects. Use the type attribute to identify the object. The type is either SINGLE or GROUP.
- st_vertexes¶
The list of StVertex objects.
- triangles¶
The list of Triangle objects.
- frames¶
The list of Frame or FrameGroup objects. Use the type attribute to identify the object. The type is either SINGLE or GROUP.
- Mdl.__init__()¶
Constructs an Mdl object.
- classmethod Mdl.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.
- Mdl.close()¶
Closes the file pointer if possible. If mode is ‘w’ or ‘a’, the file will be written to.
- Mdl.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.quake.mdl.Skin¶
Class for representing a skin
A skin is an indexed image embedded within the model. Models may contain more than one skin, and there may be as many skins as are there are separate parts in the model.
- type¶
The SkinType for the skin. For a Skin object the type must be SINGLE
- pixels¶
A tuple of unstructured indexed pixel data represented as integers. A palette must be used to obtain RGB data. The size of this tuple is:
mdl.skin_width * mdl.skin_height.
- Skin.__init__()¶
- static Skin.read(file, size)¶
- static Skin.write(file, skin, size)¶
SkinGroup
Class¶
- class vgio.quake.mdl.SkinGroup¶
Class for representing a skin group
A skin group is a sequence of indexed images embedded within the model.
- type¶
The SkinType for the skin group. For a SkinGroup the type must be GROUP
- number_of_skins¶
The number of skins contain within this SkinGroup.
- intervals¶
The time intervals between skin.
- pixels¶
A tuple of unstructured indexed pixel data represented as integers. A palette must be used to obtain RGB data. This size of this tuple is:
mdl.skin_width * mdl.skin_height * number_of_frames
- SkinGroup.__init__()¶
- static SkinGroup.read(file, size)¶
- static SkinGroup.write(file, skin_group, size)¶
StVertex
Class¶
- class vgio.quake.mdl.StVertex(on_seam, 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.
- on_seam¶
Indicates if the StVertex is on a skin boundary. The value will be 0 if not on the seam and 0x20 if it does lie on the seam.
- s¶
The s-coordinate on the skin.
- t¶
The t-coordinate on the skin.
- StVertex.__init__(on_seam, s, t)¶
- classmethod StVertex.read(file)¶
- classmethod StVertex.write(file, stvertex)¶
Triangle
Class¶
- class vgio.quake.mdl.Triangle(faces_front, vertexes_0, vertexes_1, vertexes_2)¶
Class for representing a triangle
Note
The triangle winding direction is clockwise.
- faces_front¶
Indicates if the triangle faces the front of the model, or towards the back. The value will be 0 for back-facing and 0x10 for front-facing.
- vertexes¶
A triple of vertex indices.
- Triangle.__init__(faces_front, vertexes_0, vertexes_1, vertexes_2)¶
- classmethod Triangle.read(file)¶
- classmethod Triangle.write(file, triangle)¶
TriVertex
Class¶
- class vgio.quake.mdl.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 * mdl.scale) + mdl.offset
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 quake 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, trivertex)¶
Frame
Class¶
- class vgio.quake.mdl.Frame¶
Class for representing a frame
A Frame is a set of vertexes that represent the state of the model at a single frame of animation.
Note
The TriVertices that describe the bounding box do not use their light_normal_index attribute.
- type¶
The FrameType of the frame. For a Frame object the type must be SINGLE
- bounding_box_min¶
The minimum coordinate of the bounding box containing the vertexes in this frame.
- bounding_box_max¶
The maximum coordinate of the bounding box containing all the vertexes in this frame.
- name¶
The name of the frame.
- vertexes¶
A sequence of TriVertex objects.
- Frame.__init__()¶
- static Frame.read(file, number_of_vertexes)¶
- static Frame.write(file, frame, number_of_vertexes)¶
FrameGroup
Class¶
- class vgio.quake.mdl.FrameGroup¶
Class for representing a frame group
- type¶
The FrameType of the frame group. For a Frame object the type must be GROUP
- bounding_box_min¶
The minimum coordinate of the bounding box containing the vertexes of all frames in this group.
- bounding_box_max¶
The maximum coordinate of the bounding box containing the vertexes of all the frames in this group.
- number_of_frames¶
The number of frames in this group.
- intervals¶
A sequence of timings for each frame.
- frames¶
A sequence of Frame objects.
- FrameGroup.__init__()¶
- static FrameGroup.read(file, number_of_vertexes)¶
- static FrameGroup.write(file, frame_group, number_of_vertexes)¶