Create a model programmatially

Could someone shows me a to draw just a triangle programmatically on the screen using panda3d and python?

I tried but… I can not see anything!

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
 
class MyApp(ShowBase):
 
    def __init__(self):
        ShowBase.__init__(self)
        
        format = GeomVertexFormat.getV3()
        
        vdata = GeomVertexData('geoide', format, Geom.UHStatic)
        
        vertex = GeomVertexWriter(vdata, 'vertex')
        vertex.addData3f(0, 0, 0)
        vertex.addData3f(0, 0, 5)
        vertex.addData3f(0, 5, 5)
        prim = GeomTriangles(Geom.UHStatic)
 
        prim.addVertices(0, 1, 2)
        #prim.closePrimitive()

        
        geom = Geom(vdata)
        geom.addPrimitive(prim)
         
        node = GeomNode('gnode')
        node.addGeom(geom)
         
        nodePath = render.attachNewNode(node)
        print(nodePath)

 
app = MyApp()
app.run()

[/code]

Some random suggestions:

  • Do you need to setup the camera (position and look at)?
  • I use UHDynamic for my generated geometry, but I’m not sure it matters here
  • Make sure to close the primitive
  • Turn on double face culling in case you are looking at your triangle on the wrong side (panda3d.org/manual/index.php … ce_Culling)
  • Set a background color that is easily distinguished from the black of your triangle

BTW I don’t think that’s the right forum to post in. But someone more knowledgeable can probably move this.

The triangle is white, and UHDynamic won’t change anything (UHStatic should be used here in fact). Closing the primitive is not necessary for triangles.

The problem is probably that you’re looking at the triangle from the wrong side, or that it’s too close to the camera, or something like that.