Garry's Mod Wiki

util.GetModelMeshes

  table, table util.GetModelMeshes( string model, number lod = 0, number bodygroupMask = 0 )

Description

Retrieves vertex, triangle, and bone data for the visual meshes of a given model.

This does not work on brush models (Models with names in the format *number)

Arguments

1 string model
The full path to the model to get the visual meshes of.
2 number lod = 0
Which of the model's Level of Detail (LOD) models to retrieve.

0 is the best quality with higher numbers progressively lowering the quality.

3 number bodygroupMask = 0
The combination of bodygroups to retrieve meshes for.

For more information, see Entity:SetBodyGroups

Returns

1 table modelMeshes
A sequential table of ModelMeshData Structures

Each index in this table corresponds to a mesh within the model passed as an argument to this function.

2 table modelBindPoses
A sequential table of BoneBindPose Structures

This tables indices are bone IDs for the BoneBindPose structure stored at each index.

Example

A snippet of Entity code that renders a wireframe helicopter bomb by retrieving the model's mesh and using it to create a new mesh.

-- The default material to render with in case we for some reason don't have one local myMaterial = Material( "models/wireframe" ) -- models/debug/debugwhite function ENT:CreateMesh() -- Destroy any previous meshes if ( self.Mesh ) then self.Mesh:Destroy() end -- Get a list of all meshes of a model local visualMeshes = util.GetModelMeshes( "models/combine_helicopter/helicopter_bomb01.mdl" ) -- Make sure the model exists before continuing if ( !visualMeshes ) then return end -- Select the first mesh local visualMesh = visualMeshes[ 1 ] -- Set the material to draw the mesh with from the model data myMaterial = Material( visualMesh.material ) -- You can apply any changes to visualMesh.verticies table here, distorting the mesh -- or any other changes you can come up with -- Create and build the mesh self.Mesh = Mesh() self.Mesh:BuildFromTriangles( visualMesh.triangles ) end -- A special hook to override the normal mesh for rendering function ENT:GetRenderMesh() -- If the mesh doesn't exist, create it! if ( !self.Mesh ) then return self:CreateMesh() end return { Mesh = self.Mesh, Material = myMaterial } end function ENT:Draw() -- Draw the entity's model normally, this will internally call ENT:GetRenderMesh() self:DrawModel() end