KX_Mesh(EXP_Value)

base class — EXP_Value

class KX_Mesh(EXP_Value)

A mesh object.

You can only change the vertex properties of a mesh object, not the mesh topology.

To use mesh objects effectively, you should know a bit about how the game engine handles them.

  1. Mesh Objects are converted from Blender at scene load.
  2. The Converter groups polygons by Material. This means they can be sent to the renderer efficiently. A material holds:
    1. The texture.
    2. The Blender material.
    3. The Tile properties
    4. The face properties - (From the “Texture Face” panel)
    5. Transparency & z sorting
    6. Light layer
    7. Polygon shape (triangle/quad)
    8. Game Object
  3. Vertices will be split by face if necessary. Vertices can only be shared between faces if:
    1. They are at the same position
    2. UV coordinates are the same
    3. Their normals are the same (both polygons are “Set Smooth”)
    4. They are the same color, for example: a cube has 24 vertices: 6 faces with 4 vertices per face.

The correct method of iterating over every KX_VertexProxy in a game object

from bge import logic

cont = logic.getCurrentController()
object = cont.owner

for mesh in object.meshes:
   for m_index in range(len(mesh.materials)):
      for v_index in range(mesh.getVertexArrayLength(m_index)):
         vertex = mesh.getVertex(m_index, v_index)
         # Do something with vertex here...
         # ... eg: color the vertex red.
         vertex.color = [1.0, 0.0, 0.0, 1.0]
materials
Type:list of KX_BlenderMaterial type
numPolygons
Type:integer
numMaterials
Type:integer
polygons

Returns the list of polygons of this mesh.

Type:KX_PolyProxy list (read only)
getMaterialName(matid)

Gets the name of the specified material.

Parameters:matid (integer) – the specified material.
Returns:the attached material name.
Return type:string
getTextureName(matid)

Gets the name of the specified material’s texture.

Parameters:matid (integer) – the specified material
Returns:the attached material’s texture name.
Return type:string
getVertexArrayLength(matid)

Gets the length of the vertex array associated with the specified material.

There is one vertex array for each material.

Parameters:matid (integer) – the specified material
Returns:the number of verticies in the vertex array.
Return type:integer
getVertex(matid, index)

Gets the specified vertex from the mesh object.

Parameters:
  • matid (integer) – the specified material
  • index (integer) – the index into the vertex array.
Returns:

a vertex object.

Return type:

KX_VertexProxy

getPolygon(index)

Gets the specified polygon from the mesh.

Parameters:index (integer) – polygon number
Returns:a polygon object.
Return type:KX_PolyProxy
transform(matid, matrix)

Transforms the vertices of a mesh.

Parameters:
  • matid (integer) – material index, -1 transforms all.
  • matrix (4x4 matrix [[float]]) – transformation matrix.
transformUV(matid, matrix, uv_index=-1, uv_index_from=-1)

Transforms the vertices UV’s of a mesh.

Parameters:
  • matid (integer) – material index, -1 transforms all.
  • matrix (4x4 matrix [[float]]) – transformation matrix.
  • uv_index (integer) – optional uv index, -1 for all, otherwise 0 or 1.
  • uv_index_from (integer) – optional uv index to copy from, -1 to transform the current uv.
replaceMaterial(matid, material)

Replace the material in slot matid by the material material.

Parameters:
  • matid (integer) – The material index.
  • material (KX_BlenderMaterial) – The material replacement.

Warning

Changing the material of a mesh used by many objects can be slow. This function should be not called every frames

copy()

Return a duplicated mesh.

Returns:a duplicated mesh of the current used.
Return type:KX_Mesh.
constructBvh(transform=mathutils.Matrix.Identity(4), epsilon=0)

Return a BVH tree based on mesh geometry. Indices of tree elements match polygons indices.

Parameters:
  • transform (mathutils.Matrix) – The transform 4x4 matrix applied to vertices.
  • epsilon (float) – The tree distance epsilon.
Returns:

A BVH tree based on mesh geometry.

Return type:

mathutils.bvhtree.BVHTree