2
$\begingroup$

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.
$\endgroup$
3
$\begingroup$

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)
$\endgroup$
  • $\begingroup$ Thank you! Your explenation helped me a lot. However, if I apply this script to my cube, the cube disappears. I assume because the script sets all verteces to the same position... I've made a new script, where I go through every single vertex and apply a position to it (see original post). Is there a more simple way to do it though? So that one doesnt have to alter every single one of the vertex one by one? $\endgroup$ – Sputi Apr 30 '15 at 7:37
  • $\begingroup$ @Sputi you'll have to loop through, do you want to keep some of them at their position ? use a random variable $\endgroup$ – Chebhou Apr 30 '15 at 10:21
  • $\begingroup$ Thx chebhou: well how do i loop through? :) no i want to alter all of them. But in the "for loop" it places all verteces at the same position (even with random.random() - every vertex goes to the same random position. How can i use the "for loop" for puting all verteces to differet random positions? $\endgroup$ – Sputi Apr 30 '15 at 14:52
  • $\begingroup$ I got it now, thank you. I made some minor mistakes but it works like in the first answer from @sambler. Thanks. $\endgroup$ – Sputi May 1 '15 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.