Using Python to create maps, objects and events in Tiled?


(S R) #1

Hi,

I would like to programmatically create my own maps in Tiled,

I’ve skimmed the tsx format and I’ve noticed it’s XML based and I saw that PyTMX is available for reading TSX files.

I was wondering if there is any library, (maybe PyTMX supports writing TSX files too? I didn’t use it, only skimmed the GH page), which allows manipulating and creating my own TSX files, if not - it looks like the PyTMX is a good candidate to add support for writing and creating those files

So, is there any python library which allows this? Does it have any documentation or examples?

Any hints would be appreciated.


(Thorbjørn Lindeijer) #2

There are two Python libraries listed here that support the TMX / TSX formats, both of them already support writing:

  • pytmxlib was last updated in 2014, but is able to save tilesets.

  • python-tmx has recently been updated with Tiled 1.0 features, but does not seem to have a method for saving a tileset.

I would check out both, and probably ask @onpon4 about the tileset saving functionality in python-tmx.


#3

Regarding python-tmx, it’s really simple: create a tmx.TileMap object (with the regular constructor for a new one, or with tmx.TileMap.load for an existing one), modify its attributes in the way you want, and then use the “save” method on that TileMap object. Here’s an example I whipped up of creating a new TMX file from scratch (it also creates a TSX file, that’s just as simple as setting an external file name for the tileset):

https://pastebin.com/UH4SshXm