bitcraft / pytmx

Python library to read Tiled Map Editor's TMX maps.
GNU Lesser General Public License v3.0
389 stars 81 forks source link

A way to load tsx only? #142

Closed xivol closed 3 years ago

xivol commented 3 years ago

I was trying to figure a simple way to store animated sprites without writing my own serializer.

I see that pytmx supports animated tiles and external tsx load. But is there a way to deserialize tsx animations from filename? I tried TiledTileset, but it skips over individual tiles and animations:

{'columns': 13,
 'firstgid': 0,
 'height': 416,
 'margin': 0,
 'name': 'sprites32',
 'offset': (0, 0),
 'parent': <tsx_data.TSXData.__FakeTileMap object at 0x10df2dac0>,
 'properties': {},
 'source': 'img/sprites32.png',
 'spacing': 0,
 'tilecount': 169,
 'tiledversion': '1.4.3',
 'tileheight': 32,
 'tilewidth': 32,
 'trans': '000000',
 'version': '1.4',
 'width': 416}
bitcraft commented 3 years ago

load tsx only

i don't test using pytmx like this, its meant to be used with the parent class and image loader.

deserialize tsx animations from filename

idk what this is supposed to mean, sorry. the tile animations are stored in the tile properties dictionary. if you want to get the animations from a tileset, you can find them by following this section of the readme:

https://github.com/bitcraft/pytmx#getting-tile-animations

pytmx by default won't load tiles unless they are used in a layer somewhere. you can avoid this by passing load_all_tiles=True to the loader

from pytmx.util_pygame import load_pygame
tiled_map = load_pygame(path_to_tmx_file, load_all_tiles=True)

there isn't a more direct way of getting animations from the tileset, currently.

xivol commented 3 years ago

Thank you. load_all argument is what I was looking for! I created tmx file with associated tileset, and it works!

The only problem I've encountered is with empty layer. Tiled allows to add layers with no data. pytmx crashes on empty data:

 <layer id="1" name="Tile Layer 1" width="1" height="1">
  <data encoding="csv"/>
 </layer>

It's easely fixed by adding any tile id into the data. But maybe it should be considered an Exception.

bitcraft commented 3 years ago

Thanks for letting me know about the crash with an empty layer. I’ll get that fixed! Glad it’s working for you.

On Fri, Apr 9, 2021 at 8:56 AM Ilya Loshkarev @.***> wrote:

Closed #142 https://github.com/bitcraft/pytmx/issues/142.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/bitcraft/pytmx/issues/142#event-4574597667, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEH5J2TA45WNSSULIKVZSLTH4BRTANCNFSM42USYIDA .

xivol commented 3 years ago

Another bug I found:

TiledImageLayer.parse_xml
        self.visible = node.get('visible', self.visible)

gives youself.visible, <class 'str'>, 0. So the result is always visible.

Not a big issue for ImageLayers, but still.

bitcraft commented 3 years ago

i agree it should be fixed, thanks for letting me know