The tmxlib.tileset module


class tmxlib.tileset.Tileset(name, tile_size, source=None)[source]

Base class for a tileset: bank of tiles a map can use.

There are two kinds of tilesets: external and internal. Internal tilesets are specific to a map, and their contents are saved inside the map file. External tilesets are saved to their own file, so they may be shared between several maps. (Of course, any tileset can be shared between maps at the Python level; this distinction only applies to what happens on disk.) External tilesets have the file path in their source attribute; internal ones have source set to None.

tmxlib will try to ensure that each external tileset gets only loaded once, an the resulting Python objects are shared. See for more information.

init arguments, which become attributes:


Name of the tileset


A (width, height) pair giving the size of a tile in this tileset. In cases where a tileset can have unequally sized tiles, the tile size is not defined. This means that this property should not be used unless working with a specific subclass that defines tile_size better.


For external tilesets, the file name for this tileset. None for internal ones.

Other attributes:


A dict with string (or unicode) keys and values. Note that the official TMX format does not support tileset properties (yet), so editors like Tiled will remove these. (tmxlib saves and loads them just fine, however.)


A TerrainList of terrains belonging to this tileset. Note that tileset tiles reference these by index, and the indices are currently not updated when the TerrainList is modified. This may change in the future.


An offset in pixels to be applied when drawing a tile from this tileset.

Unpacked versions of tuple attributes:


Loading and saving (see tmxlib.fileio.ReadWriteBase for more information):

classmethod open(filename, shared=False)
classmethod load(string)

Export to a dict compatible with Tiled’s JSON plugin

classmethod from_dict(dct)[source]

Import from a dict compatible with Tiled’s JSON plugin

List-like access:


Get tileset tile with the given number.

Supports negative indices by wrapping around, as one would expect.


Return the number of tiles in this tileset.

Subclasses need to override this method.


Iterate through tiles in this tileset.

Overridable methods:


Return the image used by the given tile.

Usually this will be a region of a larger image.

Subclasses need to override this method.

GID calculation methods:


TilesetList depends on the specific GID calculation algorithm provided by these methods to renumber a map’s tiles when tilesets are moved around. Don’t override these unless your subclass is not used with vanilla TilesetLists.


Return the first gid used by this tileset in the given map


Return the first gid after this tileset in the given map


class tmxlib.tileset.ImageTileset(name, tile_size, image, margin=0, spacing=0, source=None, base_path=None)[source]

A tileset whose tiles form a rectangular grid on a single image.

This is the default tileset type in Tiled.

init arguments, which become attributes:


see Tileset


The Image this tileset is based on.


Size of a border around the image that does not contain tiles, in pixels.


Space between adjacent tiles, in pixels.

Other attributes:


Number of columns of tiles in the tileset


Number of rows of tiles in the tileset

See Tileset for generic tileset methods.

ImageTileset methods:


Return the image used by the given tile


class tmxlib.tileset.TilesetTile(tileset, number)[source]

Reference to a tile within a tileset

init arguents, which become attributes:


the tileset this tile belongs to


the number of the tile

Other attributes:


The size of the tile, in pixels. Also available as (pixel_width, pixel_height).


A string-to-string dictionary holding custom properties of the tile


Image this tile uses. Most often this will be a region of the tileset’s image.


List of indices to the tileset’s terrain list for individual corners of the tile. See the TMX documentation for details.


Tuple of terrains for individual corners of the tile. If no terrain is given, None is used instead.


The probability that this tile will be chosen among others with the same terrain information. May be None.



Return the GID of this tile for a given map

The GID is a map-specific identifier unique for any tileset-tile the map uses.

get_pixel(x, y)[source]

Get a pixel at the specified location.

Pixels are returned as RGBA 4-tuples.


class tmxlib.tileset.TilesetList(map, lst=None)[source]

A list of tilesets.

Allows indexing by name.

Whenever the list is changed, GIDs of tiles in the associated map are renumbered to match the new set of tilesets.

See NamedElementList for TilesetList’s methods.

modification_context(*args, **kwds)[source]

Context manager that “wraps” modifications to the tileset list

While this manager is active, the map’s tiles are invalid and should not be touched. After all modification_contexts exit, tiles are renumbered to match the new tileset list. This means that multiple operations on the tileset list can be wrapped in a modification_context for efficiency.

If a used tileset is removed, an exception will be raised whenever the outermost modification_context exits.

Table Of Contents

Previous topic

The tmxlib.tile module

Next topic

The tmxlib.mapobject module

This Page