The main module contains the most important classes.
A tile map. tmxlib’s core class
init arguments, which become attributes:
Other attributes:
- tilesets¶
A TilesetList of tilesets this map uses
- properties¶
A dict of properties, with string (or unicode) keys & values
Unpacked size attributes:
Loading and saving (see tmxlib.fileio.ReadWriteBase for detailed information):
Methods:
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:
Unpacked versions of tile_size:
Loading and saving (see tmxlib.fileio.ReadWriteBase for detailed information):
List-like access:
Overridable methods:
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.
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:
Other attributes:
See tmxlib.Tileset for tileset methods.
Base class for map layers
init agruments, which become attributes:
- map¶
The map this layer belongs to. Unlike tilesets, layers are tied to a particular map and cannot be shared.
- name¶
Name of the layer
- visible¶
A boolean setting whether the layer is visible at all. (Actual visibility also depends on opacity)
- opacity¶
Floating-point value for the visibility of the layer. (Actual visibility also depends on visible)
Other attributes:
Methods:
A tile layer
Acts as a 2D array of MapTile’s, indexed by [x, y] coordinates. Assignment is possible either via numeric values, or by assigning a TilesetTile. In the latter case, if the tileset is not on the map yet, it is added.
See Layer documentation for most init arguments.
Other init agruments, which become attributes:
- data¶
Optional list (or array) containing the values of tiles in the layer, as one long list in row-major order. See TileLikeObject.value for what the numbers will mean.
Methods:
Tile access:
Methods to be overridden in subclasses:
A layer of objects.
Acts as a named list of objects. This means semantics similar to layer/tileset lists: indexing by name is possible, where a name references the first object of such name.
See Layer for the init arguments.
Methods:
References a particular spot on a tile layer
init arguments, which become attributes:
Other attributes:
- value¶
Numeric value of a tile, representing the image and transformations.
See the TMX format for a hopefully more detailed specification. The upper bits of this number are used for flags:
- 0x80000000: tile is flipped horizontally.
- 0x40000000: tile is flipped vertically.
- 0x20000000: tile is flipped diagonally (axes are swapped).
- 0x10000000: tmxlib reserves this bit for now, just because 0x0FFFFFFF is a nice round number.
The rest of the value is zero if the layer is empty at the corresponding spot (or an object has no associated tile image), or it holds the GID of the tileset-tile.
The GID can be computed as 1 + X + Y where X is the number of tiles in all tilesets preceding the tile’s, and Y is the number of the tile within its tileset.
The individual parts of value are reflected in individual properties:
- flipped_horizontally (0x80000000)
- flipped_vertically (0x40000000)
- flipped_diagonally (0x20000000)
- gid (0x0FFFFFFF)
The properties themselves have a value attribute, e.g. tmxlib.MapTile.flipped_diagonally.value == 0x20000000.
- map¶
The map associated with this object
Attributes for accessing to the referenced tile:
- tileset_tile¶
Get the referenced tileset tile
- size[source]¶
Size of the referenced tile, taking rotation into account.
Empty tiles have (0, 0) size.
- tileset¶
Get the referenced tileset
- number¶
Get the number of the referenced tileset tile
- image¶
Get the image of the tile. (N.B. see full docstring!)
N.B. No transformations are applied to the image. This can change in future versions. Use self.tileset_tile.image for future-safe behavior.
- properties[source]¶
Properties of the referenced tileset-tile
If that wasn’t clear enough: Changing this will change properties of all tiles using this image. Possibly even across more maps if tilesets are shared.
See TilesetTile.
Unpacked pos and size attributes:
Methods:
- tile_to_image_coordinates(x, y)¶
Transform map-tile pixel coordinates to tileset-tile pixel coords.
Handles negative indices in the obvious way.
- get_pixel(x, y)¶
Get the pixel at the given x, y coordinates.
Handles negative indices in the obvious way.
- __nonzero__()¶
This object is “true” iff there’s a tile associated with it.
Empty, “false” tiles have a GID of zero.
Flipping:
A map object: something that’s not placed on the fixed grid
Can be either a “tile object”, which has an associated tile much like a map-tile, or a regular (non-tile) object that has a settable size.
init arguments, which become attributes:
- layer¶
The layer this object is on
- pixel_pos¶
The pixel coordinates
- size[source]¶
Size of this object, as a (width, height) tuple, in pixels. Must be specified for non-tile objects, and must not be specified for tile objects (unless the size matches the tile).
Similar restrictions apply to setting the property (and width & height).
- name¶
Name of the object. A string (or unicode)
- type¶
Type of the object. A string (or unicode). No semantics attached.
Attributes for accessing to the referenced tile:
- tileset_tile¶
Get the referenced tileset tile
- size[source]
Size of the referenced tile, taking rotation into account.
Empty tiles have (0, 0) size.
- tileset¶
Get the referenced tileset
- number¶
Get the number of the referenced tileset tile
- image¶
Get the image of the tile. (N.B. see full docstring!)
N.B. No transformations are applied to the image. This can change in future versions. Use self.tileset_tile.image for future-safe behavior.
- properties¶
Properties of the referenced tileset-tile
If that wasn’t clear enough: Changing this will change properties of all tiles using this image. Possibly even across more maps if tilesets are shared.
See TilesetTile.
Other attributes:
Unpacked pos, pixel_pos, and size:
Methods:
- tile_to_image_coordinates(x, y)¶
Transform map-tile pixel coordinates to tileset-tile pixel coords.
Handles negative indices in the obvious way.
- get_pixel(x, y)¶
Get the pixel at the given x, y coordinates.
Handles negative indices in the obvious way.
- __nonzero__()¶
This object is “true” iff there’s a tile associated with it.
Empty, “false” tiles have a GID of zero.
Flipping:
A list that supports indexing by element name, as a convenience, etc
lst[some_name] means the first element where element.name == some_name. The dict-like get method is provided.
Additionally, NamedElementList subclasses can use several hooks to control how their elements are stored or what is allowed as elements.
Same as list.insert, except indices may be names instead of ints.
Insert the new value after the position specified by index_or_name
For numerical indexes, the same as insert(index + 1, value). Useful when indexing by strings.
Move an item by the specified number of indexes
amount can be negative. For example, “move layer down” translates to layers.move(idx, -1)
The method will clamp out-of range amounts, so, for eample, lst.move(0, -1) will do nothing.
Hooks for subclasses:
- modification_context(*args, **kwds)[source]¶
Context in which all modifications take place.
The default implementation nullifies the modifications if an exception is raised.
Note that the manager may nest, in which case the outermost one should be treated as an atomic operation.
A list of layers.
Allows indexing by name, and can only contain layers of a single map.
See NamedElementList for LayerList’s methods.
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.
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.
An image. Conceptually, an 2D array of pixels.
init arguments that become attributes:
- size[source]¶
Size of the image, in pixels.
If given in constructor, the image doesn’t have to be decoded to get this information, somewhat speeding up operations that don’t require pixel access.
If it’s given in constructor and it does not equal the actual image size, an exception will be raised as soon as the image is decoded.
- source¶
The file name of this image, if it is to be saved separately from maps/tilesets that use it.
- trans¶
A color key used for transparency (currently not implemented)
Images support indexing (img[x, y]) as a shortcut for the get_pixel and set_pixel methods.
Get the color of the pixel at position (x, y) as a RGBA 4-tuple.
Supports negative indices by wrapping around in the obvious way.
Set the color of the pixel at position (x, y) to a RGBA 4-tuple
Supports negative indices by wrapping around in the obvious way.
Methods interesting for subclassers:
Note
It’s currently not possible to save modified images.