Pyglet Batch error "Value Error: Can only assign sequence of same size"

Viewed 186

So I am trying to use the batch class in pyglet to make some faces like this

class Block:
    def __init__(self, pos):
        self.pos = pos

        self.top = self.get_tex('img.png')
        self.batch = pyglet.graphics.Batch()
        tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
        self.batch.add(4, GL_QUADS, self.top, ('v3f', (self.add(pos, [0, 1, 0]), self.add(pos, [0, 1, 1]), self.add(pos, [1, 1, 1]), self.add(pos, [1, 1, 0]))), tex_coords)

    def add(self, vec, toAdd):
        vec = list(vec)
        toAdd = list(toAdd)
        vec[0] += toAdd[0]
        vec[1] += toAdd[1]
        vec[2] += toAdd[2]
        return vec

    def get_tex(self, file):
        tex = pyglet.image.load(file).get_texture()
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        return pyglet.graphics.TextureGroup(tex)

But I get the error:

ValueError: Can only assign sequence of same size
1 Answers

The argument must be a float list or tuple, instead of nested lists or tuples with tuple elements.

What your code generates is:

('v3f', ([0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]))

What it's supposed to generate is:

('v3f', (0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0))

Use the asterisk(*) operator to unpacked the vertex cooridantes:

('v3f', (self.add(pos, [0, 1, 0]), self.add(pos, [0, 1, 1]), self.add(pos, [1, 1, 1]), self.add(pos, [1, 1, 0])))

('v3f', (*self.add(pos, [0, 1, 0]), *self.add(pos, [0, 1, 1]), *self.add(pos, [1, 1, 1]), *self.add(pos, [1, 1, 0])))

Class Block

class Block:
    def __init__(self, pos):
        self.pos = pos
        self.batch = pyglet.graphics.Batch()
        tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
        vertices = ('v3f', (*self.add(pos, [0, 1, 0]), *self.add(pos, [0, 1, 1]), *self.add(pos, [1, 1, 1]), *self.add(pos, [1, 1, 0])))
        self.batch.add(4, GL_QUADS, self.top, vertices, tex_coords)

    def add(self, vec, toAdd):
        vec = list(vec)
        toAdd = list(toAdd)
        vec[0] += toAdd[0]
        vec[1] += toAdd[1]
        vec[2] += toAdd[2]
        return vec

    def get_tex(self, file):
        tex = pyglet.image.load(file).get_texture()
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        return pyglet.graphics.TextureGroup(tex)
Related