The tmxlib.tileset module

Tileset

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 ReadWriteBase.open() for more information.

init arguments, which become attributes:

name

Name of the tileset

tile_size:

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.

source

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

Other attributes:

properties

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.)

terrains

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.

tile_offset

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

Unpacked versions of tuple attributes:

tile_width[source]
tile_height[source]
tile_offset_x
tile_offset_y

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

classmethod open(filename, shared=False)
classmethod load(string)
save(filename)
dump(string)
to_dict(**kwargs)[source]

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:

__getitem__(n)[source]

Get tileset tile with the given number.

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

__len__()[source]

Return the number of tiles in this tileset.

Subclasses need to override this method.

__iter__()[source]

Iterate through tiles in this tileset.

Overridable methods:

tile_image(number)[source]

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:

Note

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.

first_gid(map)[source]

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

end_gid(map)[source]

Return the first gid after this tileset in the given map

ImageTileset

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:

name
tile_size
source

see Tileset

image

The Image this tileset is based on.

margin

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

spacing

Space between adjacent tiles, in pixels.

Other attributes:

column_count[source]

Number of columns of tiles in the tileset

row_count[source]

Number of rows of tiles in the tileset

See Tileset for generic tileset methods.

ImageTileset methods:

tile_image(number)[source]

Return the image used by the given tile

TilesetTile

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

Reference to a tile within a tileset

init arguents, which become attributes:

tileset

the tileset this tile belongs to

number

the number of the tile

Other attributes:

pixel_size[source]

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

properties[source]

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

image[source]

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

terrain_indices[source]

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

terrains[source]

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

probability[source]

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

Methods:

gid(map)[source]

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.

TilesetList

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