Reltime random shape generator

Hi,
i want to set up a script that randomly changes the shape of an object. What I have so far is:

from bge import logic
import random

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)
         vertex.setXYZ([ random.random(), random.random(), random.random()])

Works pretty well, however since every single vertex gets its own random location, the cube turns out something like this:

what I want thoug is something like this:


So the question is: how can I achieve something like that?
I think the “broken” shapes happen because 3 verteces are connected to each other and if they all get a different random position, the shape “breaks”?

I assume, that I have to give the verteces 1-3 the same random position, then the verteces 4-6 the same random position and so on?

If that is true, how could I write that down in python? Help is very apprechiated, thanks in advance.

Your problem is that when you randomize the cube vertices, the faces are all messed up. And there is no guarantee that the faces still form a “convex” shape or that they don’t intersect. What you seem to want is a random solid object.

To be fair, I don’t know how to solve this in Python myself. I would have to seriously brush up my vector geometry knowledge to achieve that effect by manipulating the vertices directly.

My advice therefore is to try and use some of the modifiers to achieve the same thing. Randomize the vertices slightly and see what you can “get away” with. Another solution may be to turn a random 3D texture into a mesh. Not sure how that works.

The thing with the modifiers is a good hint, thank you.
Well, since nobody seems to know an easy solution, I’ll be experimenting a little further.
Cheers

Your cube’s geometry is being split this way becouse your cube actually consists of 24 vertices when flat shaded. A quick fix would be to change shading to smooth. In OpenGL, which blender uses for drawing, values(normals for example) are interpolated between vertices which are connected. The flat shading is produced by splitting the faces. This way each face of the cube consists of 4 vertices. 4 X 6 = 24. When shading is set to smooth there is no need for splitting the faces and the Vertex Array only has 8 vertices in it, so it should behave as expected.