In Blender 2.53, it is now possible in the game engine during runtime to, via python, create a new mesh, add faces and materials to a mesh (and I assume remove them as well), and then assign that mesh to an object, using the bpy module. Unfortunately, this is barely or not documented, and after a good chunk of the day, by trying to reverse-engineer random script bits I’ve found around the web, I’ve managed to add a new mesh and assign it to an object, and maybe add vertices to it but since I can’t find a way to tell Blender where to place these vertices, or even return a list of vertices currently on the mesh, I can’t really verify this.
So far, my script looks about like this:
import bpy
g=GameLogic
c=g.getCurrentController()
o=c.owner
if 'init' not in o:
o['init']=0
o['me']=bpy.data.meshes.new('newMesh')
g.LibNew('newMesh','Mesh',[o['me'].name])
o.replaceMesh(o['me'].name)
o['i']=0
o['me'].add_geometry(4,0,1)
o['me'].update()
me=o.meshes[0] # the data stored in o['me'] is not a meshproxy
vert=me.getVertex(0,0)
vert.x=-1
vert.y=-1
vert.z=o['i']
vert=me.getVertex(0,1)
vert.x=1
vert.y=-1
vert.z=o['i']
vert=me.getVertex(0,2)
vert.x=1
vert.y=1
vert.z=o['i']
vert=me.getVertex(0,3)
vert.x=-1
vert.y=1
vert.z=o['i']
o['i']-=1
the idea is I initialize the mesh, assign it to the object, and then each frame add a plane and move it one unit below the last. Unfortunately, I get an error telling me that it “could not get a vertex at the given indices”.
My best guess is that the object doesn’t have a material, so even though there are vertices there’s no material to reference them with, but I ahve a feeling that guess is way off.
Additionally, I’d prefer to be able to assign an already existing material to the object, though the function to add a material wants a reference to the material (the first thing I tried was a string with the materials name)- and I’m not sure how to get that.
Ideally eventually this would allow me to create arbitrary numbers of polytrails with arbitrary numbers of polygons, and avoid the issue of multiple added objects sharing the same mesh- so if I move the vertices of one, all the other objects get the same change.
I was unable to find any documentation that was significantly helpful, and there aren’t any tutorials or explanations about this sort of thing yet (not that I expect there to be)
So I suppose what I’m leading up to is, is there anyone on this forum who knows what they’re doing with the new RNA functions with regards to the game engine, and could enlighten me- or just point me in the right direction?