Rendering many points in Panda3D

Someone asked me how to render many points in Panda3D, here is the simplest snippet possible showing how to do this:

from panda3d.core import *
from random import random

num_points = 100000

vdata = GeomVertexData('points', GeomVertexFormat.get_v3(), GeomEnums.UH_static)
vdata.set_num_rows(num_points)

vertex = GeomVertexWriter(vdata, 'vertex')
for i in range(num_points):
    vertex.set_data3(random() - 0.5, random() - 0.5, random() - 0.5)

primitive = GeomPoints(GeomEnums.UH_static)
primitive.add_next_vertices(num_points)

geom = Geom(vdata)
geom.add_primitive(primitive)

gnode = GeomNode('points')
gnode.add_geom(geom)


# Render the node
from direct.showbase.ShowBase import ShowBase

base = ShowBase()
base.set_background_color((0, 0, 0))
base.render.attach_new_node(gnode)
base.cam.set_y(-5)
base.run()
6 Likes