Search code examples
pythonpyglet

'pyglet.graphics' has no attribute 'vertex_list'


I'm trying to make a Triangle with pyglet but I keep seeing this error.

AttributeError: module 'pyglet.graphics' has no attribute 'vertex_list'

I am not sure what is the problem exactly.

This is the code I'm trying to run:

from pyglet.gl import *


class Triangle:
    def __init__(self) -> None:
        self.vertices = pyglet.graphics.vertex_list(3, ('v3f', [-0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.0,0.5,0.0]),
                                                       ('c3B', [100,200,220, 200,110,100, 100,250,100]))

class MyWindow(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_minimum_size(400,300)

        self.triangle = Triangle()

    def on_draw(self):
        self.triangle.vertices.draw(GL_TRIANGLES)

    def on_resize(self, width, height):
        glViewport(0, 0, width, height)
    
if __name__ =="__main__":
    window = MyWindow(1280, 720, "Hello", resizable=True)
    window.on_draw()
    pyglet.app.run()

Solution

  • Please check which version of pyglet you are using. Recently (on 01.11.2022) pyglet version 2.0 got released which changed how to use vertex based drawing majorly.

    pyglet.graphics.vertex_list was used in earlier versions of pyglet (for information how to use this refer to https://pyglet.readthedocs.io/en/pyglet-1.5-maintenance/modules/graphics/index.html which is the documentation for pyglet version 1.5-maintenance). If you want to work with this older version make sure to use pyglet version 1.5.27 or lower

    In pyglet version 2.0 you have to create a Shader Program and use this to draw vertex based graphics. For more information about this refer to pyglets documentation regarding Shaders and Rendering: https://pyglet.readthedocs.io/en/latest/programming_guide/graphics.html