BGE: altering a cube's shape in realtime (python) how is it possible to get all vertices of an object (say a cube) and then randomly change their location? What I want to do is everytime I hit space, the cube alters and generate random shapes.

What I have figured out myself so far:

import bge
import random

cont = bge.logic.getCurrentController()
own = cont.owner

# get the 1st mesh
mesh = own.meshes[0]

# get the first vertex of the first mesh
vert  = mesh.getVertex( 0, 0)

# get the position
pos = vert.getXYZ()

# set the position
vert.setXYZ([ 10, 1.0, 15])


How does

getVertex(matid, index)


exactely work? It only gets me one vertex, how can I get one after another? And then alter their position?

Thank you for any hint!

UPDATED Script:

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, 0)
         vertex.setXYZ([ loc0])
         vertex = mesh.getVertex(m_index, 1)
         vertex.setXYZ([ loc1])
         vertex = mesh.getVertex(m_index, 2)
         vertex.setXYZ([ loc2])
         etc.
         etc.
Using the example shown for KX_MeshProxy

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]


mesh.getVertexArrayLength(matid) tells you how many vertices are in the mesh, you then loop through each by increasing the vertex index which is the second value to getVertex(matid, index)

Applying that to your sample -

mesh = own.meshes[0]
for v_index in range(mesh.getVertexArrayLength(0)):
    vert  = mesh.getVertex( 0, v_index)
    pos = vert.getXYZ()
    #alter pos
    vert.setXYZ(pos)