Search code examples
pythonopenglpyglet

"too many values to unpack error" when trying to create a batch in pyglet


I have been trying to get batches to work in pyglet, but I am completely confused by the error message "too many values to unpack" coming from the pyglet/graphics/__init__.py file. My guess is that that I am doing something wrong syntaxwise when adding the geometry to the batch.

I cut down my code to the essential parts that create the error:

from pyglet.gl import *
from pyglet.graphics import *
import pyglet

batch = pyglet.graphics.Batch()
img = pyglet.image.load('pic.png')
texture = img.get_texture()

class TextureEnableGroup(pyglet.graphics.Group):
    def set_state(self):
        glEnable(GL_TEXTURE_2D)
    def unset_state(self):
        glDisable(GL_TEXTURE_2D)

texture_enable_group = TextureEnableGroup()

class TextureBindGroup(pyglet.graphics.Group):
    def __init__(self, texture):
        super(TextureBindGroup, self).__init__(parent=texture_enable_group)
        self.texture = texture
    def set_state(self):
        glBindTexture(GL_TEXTURE_2D, self.texture.id)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    def __eq__(self, other):
        return (self.__class__ is other.__class__ and self.texture == other.__class__)

batch.add(12, GL_TRIANGLES, TextureBindGroup(texture), (('t2f', (0, 0)), ('v3f', (64, 64, 0)), ('t2f', (1, 1)), ('v3f', (-64, -64, 205)), ('t2f', (0, 1)), ('v3f', (-64, 64, 205)), ('t2f', (1, 1)), ('v3f', (64, -64, 205)), ('t2f', (1, 0)), ('v3f', (64, 64, 0)), ('t2f', (0, 1)), ('v3f', (-64, -64, 205))))

Solution

  • Thanks to marcog, The correct final line of the script is:

    batch.add(6, GL_TRIANGLES, TextureBindGroup(texture), ('v3i', (64, 64, 0, -64, -64, 205, -64, 64, 205, 64, -64, 205, 64, 64, 0, -64, -64, 205)), ('t2i', (0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1)))
    

    i.e. problem solved =)

    The problem was partially that I sent all the data as a single tuple (which marcog pointed out), as well as passing a wrong value of the length of the batch of geeometry data; 6 vertices instead of 12.