GeomTriangles.addPrimitive - Assertion failed: primitive-&gt

I wonder if anyone could shed any light on a deep bug I have run into when trying to extend my terrain code to use LOD. The bug has nothing to do with Panda3D’s LOD functionality, but I had to edit my mesh building function to build meshes of different resolutions and something went wrong:

Assertion failed: primitive->check_valid(cdata->_data) at line 326 of panda/src/ gobj/geom.cxx
Traceback (most recent call last):
  File "Demo.py", line 19, in ?
    terrainNodePath = TerrainNodePath( heightMapFileName, textureFileName, detai lFileName, scaleValue, bumpValue )
  File "/home/seanh/sync/terrain-project/trunk/TerrainNodePath.py", line 25, in __init__
    self.terrainGeoms = makeTerrainGeoms(self.heightMap,16,16) # This function i s in TerrainGeom.py.
  File "/home/seanh/sync/terrain-project/trunk/TerrainGeom.py", line 49, in make TerrainGeoms
    terrainGeoms[len(terrainGeoms)-1].append( TerrainGeom( Patch(heightMap,x,y,w idth,height) ) )
  File "/home/seanh/sync/terrain-project/trunk/TerrainGeom.py", line 36, in __in it__
    geom.addPrimitive(triangles)
AssertionError: primitive->check_valid(cdata->_data) at line 326 of panda/src/go bj/geom.cxx

If anyone could shed any light on that output, it might help me get at this bug, which has had me stuck for a couple of days when I thought I was about ready to release.

This assertion failure means you are attempting to add a GeomPrimitive whose vertex indices are inconsistent with the GeomVertexData associated with the Geom.

That is to say, your primitive references vertices that do not exist in your Geom. For instance, you tried to reference vertex number 4, but your Geom only defines vertices 0 through 3.

David