[SOLVED]Replace triangle use point with Geom

self.broadleaf.find("**/mesh_Leaf_Model").setShader(myShader)
geomNode = self.broadleaf.find("**/mesh_Leaf_Model").node()		# 需要 node
for i in range(geomNode.getNumGeoms()):
	newPrimList = []
	geom = geomNode.modifyGeom(i)		
	state = geomNode.getGeomState(i)		
	vdata = geom.getVertexData()
	vertex = GeomVertexReader(vdata, 'vertex')
	#新建一个顶点池
	newvdata = GeomVertexData('', GeomVertexFormat.getV3n3(), Geom.UHStatic)
	newvertex = GeomVertexWriter(newvdata, 'vertex')
	#新建点列表
	for j in range(geom.getNumPrimitives()):		# 循环读取图元
		newPrimList.append(GeomPoints(Geom.UHStatic))
		prim = geom.getPrimitive(j)
		for p in range(prim.getNumPrimitives()):
			s = prim.getPrimitiveStart(p)		# 图元顶点开始的索引号
			e = prim.getPrimitiveEnd(p)			# 图元顶点结束的索引号
			v1v2len = 0
			for k in range(s, e):
				vi = prim.getVertex(k)	
				vertex.setRow(vi)
				v1 = vertex.getData3f()
				for l in range(k+1, e):
					vi = prim.getVertex(l)
					vertex.setRow(vi)
					v2 = vertex.getData3f()
					# 判断两个顶点之间的距离
					if (v1-v2).length() > v1v2len:
						v1v2len = (v1-v2).length()
						v1v2 = (v1+v2)/2		# 获得两个点的中心
			newvertex.addData3f(v1v2)			# 将中心添加到顶点池当中
			newPrimList[-1].addNextVertices(1)
		newPrimList[-1].closePrimitive()
	geom.clearPrimitives()
	for prim in newPrimList:
		geom.addPrimitive(prim)
	geom.setVertexData(newvdata)

This my new code:

geomNode = self.broadleaf.find("**/mesh_Leaf_Model").node()		# 需要 node
newPrimList = []
for i in range(geomNode.getNumGeoms()):
	geom = geomNode.modifyGeom(i)
	vdata = geom.getVertexData()
	vertex = GeomVertexReader(vdata, 'vertex')

	#new vertex pool
	newvdata = GeomVertexData('', GeomVertexFormat.getV3n3(), Geom.UHStatic)
	newvertex = GeomVertexWriter(newvdata, 'vertex')


	for j in range(geom.getNumPrimitives()):		# 循环读取图元
		#new vertex point list
		newPrimList.append(GeomPoints(Geom.UHStatic))
		prim = geom.getPrimitive(j)
		for p in range(prim.getNumPrimitives()):
			s = prim.getPrimitiveStart(p)		# 图元顶点开始的索引号
			e = prim.getPrimitiveEnd(p)	
			v1v2len = 0
			for k in range(s, e):
				vi = prim.getVertex(k)			# 不明白,应该是在顶点池当中的位置吧?
				vertex.setRow(vi)
				v1 = vertex.getData3f()
				for l in range(k+1, e):
					vi = prim.getVertex(l)
					vertex.setRow(vi)
					v2 = vertex.getData3f()
					# 判断两个顶点之间的距离
					if (v1-v2).length() > v1v2len:
						v1v2len = (v1-v2).length()
						v1v2 = (v1+v2)/2		# 获得两个点的中心

			# Add a vertex
			newvertex.addData3f(v1v2)			# 将中心添加到顶点池当中

			# Add a point
			newPrimList[-1].addNextVertices(1)

		newPrimList[-1].closePrimitive()
	geom.clearPrimitives()

	for prim in newPrimList:
		geom.addPrimitive(prim)
	geom.setVertexData(newvdata)

It said:

Assertion failed: check_will_be_valid(data) at line 185 of panda/src/gobj/geom.cxx
Traceback (most recent call last):
  File "main2.py", line 67, in <module>
    app = MyApp()
  File "main2.py", line 58, in __init__
    geom.setVertexData(newvdata)
AssertionError: check_will_be_valid(data) at line 185 of panda/src/gobj/geom.cxx

The error means you have added vertex indices which are not defined in the GeomVertexData.

David

Thank you !