manipulating vertex positions and colors via python, bge

hallo together,

i like to make some realtime modifications of some objects in the bge.
is there a way to get all verts as an array, like you can get in blender?
or is it not possible because objects are converted to gl display lists or buffer objects?
id like to make simple vertex position and color manipultaions via python.

best,
glow

hallo,

i catched this script in documentation from the gamelogic, but i have problems to run it.


import GameLogic

co = GameLogic.getCurrentController()
obj = co.owner

m_i = 0

allmeshes = obj.meshes
print(allmeshes)

mesh = obj.getMesh("Cube") # There can be more than one mesh...
while mesh != None:
   for mat in range(mesh.getNumMaterials()):
      for v_index in range(mesh.getVertexArrayLength(mat)):
         vertex = mesh.getVertex(mat, v_index)
         # Do something with vertex here...
         # ... eg: colour the vertex red.
         vertex.colour = [1.0, 0.0, 0.0, 1.0]
   m_i += 1
   mesh = obj.getMesh(m_i)

i get the following error:

AttributeError: 'KX_GameObject' object has no attribute 'getMesh'

i am using 2.57b on ubuntu 64bit.

any ideas whats going wrong, are there any changes in gamelogic since updating the documentation?

best,
glow

Yes, the 2.5 doc on KX_GameObject don’t have it but a list called meshes instead:

http://www.blender.org/documentation/blender_python_api_2_56_4/bge.types.html#bge.types.KX_GameObject

Try change .getMesh(…) to .meshes[0]

when i use meshes i can print all meshes i have in the object but i cant use it to get the mesh i want.
if i use the following:

mesh = obj.meshes(0) 

i get the following error:

'list' object is not callable

so it seems that this .meshes() only exists to list all meshes.

You’re calling obj.meshes as a function ( obj.meshes(0) ), not as a list ( obj.meshes[0] ).

mesh = obj.meshes[0]

That should work fine.

EDIT: Once you’ve gotten the mesh, you can get the vertex list with:

for v in range(mesh.getVertexArrayLength(0)):

 vert = mesh.getVertex(0, v)

That also should work. Once you’ve gotten the vertex, you can edit the vertex color and vertex positions accordingly.

hallo,

yeah thats it, sometimes mixing codesnippet makes to much confusion. for sure you cant use a list as an method.
now it works.
is there a way to make an animation with this? at the moment i can change the propperties only once. i like to do it every frame. something like a draw loop.
i call the script via the always sensor.

import GameLogic

co = GameLogic.getCurrentController()
obj = co.owner

m_i = 0

mesh = obj.meshes[0] # There can be more than one mesh...

for v in range(mesh.getVertexArrayLength(0)):
    vert = mesh.getVertex(0, v)
    vert.color = [1,0,1,1]
    print(vert.color)

now it works. i forgot to switch on pulse mode. thanks for your help.