Strings and bytes: Python 2 vs 3

32 views
Skip to first unread message

Jon Martin

unread,
Sep 10, 2016, 4:18:59 PM9/10/16
to Kivy users support
I have the following snippet of code that works just fine under Python 2.7 but gives errors in Python 3.4:

def add_many_monkeys(self):
    m = list(self._scene.objects.values())[0]
    scalar = 2
    steps = [0]
    variance = 5  # must always be >= 0.  negative numbers will break it.
    
    if variance:
        steps = range(-variance, variance+1)
    
    for z in steps:        
        for y in steps:            
            for x in steps:
                graphics.PushMatrix()
                graphics.Translate(x*scalar, y*scalar, z*scalar)
                graphics.Mesh(
                    vertices=m.vertices,
                    indices=m.indices,
                    fmt=m.vertex_format,
                    mode='triangles',
                )
                graphics.PopMatrix()

I'm a noob at Python and Kivy.  I'm just experimenting writing a little demo and trying to learn.  When I run this with Python 3.4, I get an error.

Traceback (most recent call last):
   File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
     "__main__", mod_spec)
   File "C:\Python34\lib\runpy.py", line 85, in _run_code
     exec(code, run_globals)
   File "C:\Users\Jon Martin\mischief\__main__.py", line 6, in <module>
     with game.GameApp():
   File "C:\Users\Jon Martin\mischief\game.py", line 65, in __enter__
     self.run()
   File "C:\Python34\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "C:\Users\Jon Martin\mischief\game.py", line 110, in build
     return GameWidget()
   File "C:\Python34\lib\site-packages\kivy\uix\floatlayout.py", line 65, in __init__
     super(FloatLayout, self).__init__(**kwargs)
   File "C:\Python34\lib\site-packages\kivy\uix\layout.py", line 72, in __init__
     super(Layout, self).__init__(**kwargs)
   File "C:\Python34\lib\site-packages\kivy\uix\widget.py", line 320, in __init__
     Builder.apply(self, ignored_consts=self._kwargs_applied_init)
   File "C:\Python34\lib\site-packages\kivy\lang.py", line 1970, in apply
     self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
   File "C:\Python34\lib\site-packages\kivy\lang.py", line 2082, in _apply_rule
     child = cls(__no_builder=True)
   File "C:\Users\Jon Martin\mischief\renderer.py", line 77, in __init__
     self.setup_scene()
   File "C:\Users\Jon Martin\mischief\renderer.py", line 124, in setup_scene
     self.add_many_monkeys()
   File "C:\Users\Jon Martin\mischief\renderer.py", line 216, in add_many_monkeys
     mode=bytes('triangles','ASCII'),
   File "kivy\graphics\vertex_instructions.pyx", line 385, in kivy.graphics.vertex_instructions.Mesh.__init__ (kivy\graphics\vertex_instructions.c:20900)
   File "kivy\graphics\vertex.pyx", line 50, in kivy.graphics.vertex.VertexFormat.__init__ (kivy\graphics\vertex.c:1129)
 TypeError: expected bytes, str found


I tried changing the line:
mode = 'triangles'

Tried these other choices:
mode = b'triangles'
mode = bytes('triangles','UTF-8')
mode = bytes('triangles','ASCII')

I still get the same error.

I apologize if this is totally obvious, but could someone please tell me what I'm misunderstanding?!

Jon Martin

unread,
Sep 24, 2016, 7:34:02 PM9/24/16
to Kivy users support
Anybody have any thoughts about this one?  I would certainly appreciate any insight.  Thank you!!

Alexander Taylor

unread,
Sep 24, 2016, 8:06:13 PM9/24/16
to Kivy users support
I didn't look at this much when you posted it because the code example was incomplete so it's not clear what exactly could be wrong, but my guess would be it's to do with the vertex format. Try making sure you pass in the vertex format attribute names as bytes (as is done at https://github.com/kivy/kivy/blob/master/examples/3Drendering/objloader.py#L5 , although possibly the types should be as bytes too).

Jon Martin

unread,
Sep 25, 2016, 1:06:52 PM9/25/16
to Kivy users support
Apologies for the incomplete example.  You are correct though.  Never occurred to me.  Thank you!!
Reply all
Reply to author
Forward
0 new messages