"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_T开发者_高级运维EXTURE_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))))
Your problem is in this line:
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))))
I believe it should be:
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)))
Notice how I changed the last argument from the format ((tuple), (tuple))
to (tuple), (tuple))
. I'm not familiar with pyglet, but discovered this is the correct way of calling batch.add()
from the documentation. Note that *data
represents a variable list of parameters at the end of the function call, not a tuple or a list like you attempted.
Try that and let us know how it works out for you.
"Too many values to unpack" is an error you get when you do things like
a, b = "a b c".split(" ")
The split returns three values, but you try to stick them into two. I guess you have a parenthesis wrong somewhere in that last line. Try to use some clearer syntax for that. As it is now it's quite horrid and unreadable.
I would guess that your 4th argument to batch.add()
does not follow a format that pyglet expects. It might be useful to check this.
Also, looking at the lines in pyglet where the error occurs could give you more information. The problem is likely to be a problem with the arguments you pass to a pyglet function.
PS: the documentation for add
might be of interest.
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.
精彩评论