Creating a mesh while BGE is running

Hi,
I’m trying to use the BGE for real time visualization. The data is being piped in via a UDP socket - this is working fine. I’d like to plot it as a line so I want to create a new mesh (the line) every frame (from my data). It seems possible to take an existing KX_MeshProxy and change the XYZ location of vertexes with in it (assuming you work out which is which) which is a start - i.e. I can model a line (actually a cylinder subdivided many times a long its length) and distort it using my data points. However I haven’t yet worked out how to create a new KX_MeshProxy. I need to do this since my lines have varying numbers of data points so I can’t just deform an existing mesh for each new one.

Essentially I want to be able to procedurally generate meshes in the game engine. Is this possible?

Thanks!

The following example plots two piecewise linear diagrams in 3D space.
Points are generated mathematically, rather than acquired externally, but the principle is the same.
Each new point is appended to its relevant data set, then the entire data set is redrawn each iteration using drawLine().

import bge, random
from math import sin, cos

# Coordinate x, y, z iniziali dei grafici
bge.data1 = [[-10, 0, 0]]
bge.data2 = [[-10, 0, 0]]

def main():
    dataSet1 = bge.data1
    dataSet2 = bge.data2
    
    NP = len(dataSet1)
    
    # Coordinate del nuovo punto da plottare:
    newX1 = dataSet1[NP-1][0] + 0.1
    newY1 = 5*sin(newX1)
    newZ1 = 0.
    newVal1 = [newX1, newY1, newZ1]
    
    newX2 = dataSet2[NP-1][0] + 0.1
    newY2 = 0
    newZ2 = 5*cos(newX2)
    newVal2 = [newX2, newY2, newZ2]
    
    # Aggiorna la lista di punti:
    dataSet1.append(newVal1)
    dataSet2.append(newVal2)
    
    # Plotta tutti i punti:
    for i in range(NP):
        bge.render.drawLine(dataSet1[i], dataSet1[i+1], [0,1,0])
        bge.render.drawLine(dataSet2[i], dataSet2[i+1], [1,0,0])
    
    # Memorizza la nuova lista:
    bge.data1 = dataSet1
    bge.data2 = dataSet2


I have basically posted the same question over here: http://www.blenderartists.org/forum/showthread.php?293681-dynamically-generated-track

You would only use a cylindrical model instead of the track piece.

I’ll share the code once its bug free.
currently it’s main drawbacks are that you need a second dummy mesh on a hidden layer and that I’m using Numpy + Scipy for the interpolation between the raw points.

Well that’s cool - I didn’t think of that, thank you. However, I guess the fact you need to do that means you can’t create actual meshes with polygons and proper rendering - unless anybody else knows how?

You can’t create new meshes, but you can combine small pieces into bigger meshes and deform them which practically allows you to create dynamic meshes on the go.

And the wrong rendering you see (I assume you watched the video) only happens when this code is combined with the threading module. It works pretty well without any rendering issues when done on a single thread, but then blender freezes for some seconds which isn’t nice.

Thanks Zinggi - that will do the job - sorry for the confusing reply, it was meant for the comment above. It’s a bit messy and it’ll be interesting to see what level of performance will be achieved but it’s a good start!

I’ve uploaded a small .blend with the code here:
http://www.blenderartists.org/forum/showthread.php?293681-dynamically-generated-track/page2

I have absolute zero knowledge about navigation meshes inside the blender game engine. But if you can put NavMeshes together and move vertices like any other mesh, then probably yes.

Haven’t been able to, unfortunately. Can’t even tie the Steering actuator to a py script, and assign the NavMesh and Goal. Does nothing.