srctools.smd

Parses SMD model/animation data.

class srctools.smd.Mesh(
bones: Dict[str, Bone],
animation: Dict[int, List[BoneFrame]],
triangles: List[Triangle],
)

The contents of an SMD model.

This contains: * A bone tree * Animation frames * Optionally triangle data.

root_bone() Bone

Return the root of our bone hierachy.

static blank(root_name: str) Mesh

Create an empty mesh, with a single root bone.

static parse_smd(
file: Iterable[bytes],
) Mesh

Parse a SMD file.

The file argument should be an iterable of lines. It is parsed in binary, since non-ASCII characters are not permitted in SMDs.

export(file: _BinaryFile) None

Write out the SMD to the given file.

append_model(
mdl: Mesh,
rotation: Angle | Matrix | Vec | None = None,
offset: Vec | None = None,
scale: float | Vec = 1.0,
) None

Append another model’s geometry onto this one.

All geometry is attached to the root bone.

classmethod build_bbox(
root_bone: str,
mat: str,
bbox_min: Vec,
bbox_max: Vec,
) Mesh

Construct a mesh for a bounding box.

weld_vertexes(
dist_tol: float = 1e-05,
normal_tol: float = 0.999,
) None

Run through all vertexes in the triangles, ‘welding’ close ones together.

This will result in adjacent faces sharing vertex objects. The shared vertexes should have approximately the same position as well as normal. This can be accomplished using a mesh with smoothed normals as with most studioMDL collision models, or by giving each section the same unique normal.

split_collision() List[Mesh]

Partition a concave collision mesh into each convex volume.

This will first ‘weld’ the vertexes, so each convex volume will share vertex objects.

compute_volume() float

Compute the volume of this mesh. It does not need to be convex.

See www.ma.ic.ac.uk/~rn/centroid.pdf.

smooth_normals() None

Replace all normals with ones smoothing adjacient faces.

flatten_normals() None

Replace all vertex normals with the triangle normal.

class srctools.smd.Triangle(
mat: str,
p1: Vertex,
p2: Vertex,
p3: Vertex,
)

Represents one triangle.

copy() Triangle

Duplicate this triangle.

surface_area() float

Compute the surface area of this triangle.

normal() Vec

Compute the normal of this triangle, ignoring vertex normals.

class srctools.smd.Vertex(
pos: Vec,
norm: Vec,
tex_u: float,
tex_v: float,
links: List[Tuple[Bone, float]],
)

A single vertex.

copy() Vertex

Copy the vertex.

with_pos(
pos: Vec,
norm: Vec | None = None,
) Vertex

Copy this vertex, changing the position.

with_uv(
u: float,
v: float,
) Vertex

Copy this vertex, changing the UV.

class srctools.smd.Bone(name: str, parent: Bone | None)

Represents a single bone.

class srctools.smd.BoneFrame(
bone: Bone,
position: Vec,
rotation: Angle,
)

Represents a single frame of bone animation.

exception srctools.smd.ParseError(line_num: int | str, msg: str, *args: object)

Invalid model format.