import xml.etree.ElementTree as ET tmx_file = 'assets/maps/Faza1_Finalna_v2.tmx' tree = ET.parse(tmx_file) root = tree.getroot() # Find the last tileset to calculate the next firstgid last_gid = 1 for tileset in root.findall('tileset'): firstgid = int(tileset.get('firstgid')) # This logic is a bit flawed if tilesets are not in order, but usuall good enough. # Better: finding max(firstgid + tilecount) # We will assume existing ones are okay and append to the end. # We need to know the tilecount, which might be in the image dimensions. # Simplified: We will just pick a high starting GID for new ones to avoid collision. # Existing max GID seems to be around 7000 (from previous logs). pass next_gid = 10000 new_tilesets = [ { 'firstgid': next_gid, 'name': 'Ground_Tilled', 'tilewidth': 32, 'tileheight': 32, 'tilecount': 1, 'columns': 1, 'image': 'tilesets_source/ground/soil_tilled.png', 'imagewidth': 32, 'imageheight': 32 }, { 'firstgid': next_gid + 100, 'name': 'Farming_Potato', // Collection of images approach is better for single sprites, but let's try standard 'tilewidth': 32, 'tileheight': 32, 'tilecount': 5, 'columns': 5, 'image': 'tilesets_source/crops/potato_stage5.png', # Placeholder logic: TMX usually links to one image per tileset or a collection. # Since we have individual files, we should create a "Collection of Images" tileset or merge them. # For simplicity in this script, I will just add single image tilesets for the key items. 'imagewidth': 32, 'imageheight': 32 }, { 'firstgid': next_gid + 200, 'name': 'Props_Farm', 'tilewidth': 32, 'tileheight': 32, 'tilecount': 1, 'columns': 1, 'image': 'tilesets_source/props/chest_closed.png', 'imagewidth': 32, 'imageheight': 32 }, { 'firstgid': next_gid + 300, 'name': 'Decals_Blood', 'tilewidth': 32, 'tileheight': 32, 'tilecount': 1, 'columns': 1, 'image': 'tilesets_source/decals/blood_pool.png', 'imagewidth': 32, 'imageheight': 32 }, { 'firstgid': next_gid + 400, 'name': 'Collision_Red', 'tilewidth': 32, 'tileheight': 32, 'tilecount': 1, 'columns': 1, 'image': 'collision_red.png', 'imagewidth': 32, 'imageheight': 32 } ] # Append new tilesets for ts in new_tilesets: new_node = ET.SubElement(root, 'tileset') new_node.set('firstgid', str(ts['firstgid'])) new_node.set('name', ts['name']) new_node.set('tilewidth', str(ts['tilewidth'])) new_node.set('tileheight', str(ts['tileheight'])) new_node.set('tilecount', str(ts['tilecount'])) new_node.set('columns', str(ts['columns'])) img_node = ET.SubElement(new_node, 'image') img_node.set('source', ts['image']) img_node.set('width', str(ts['imagewidth'])) img_node.set('height', str(ts['imageheight'])) tree.write(tmx_file) print("Done injecting tilesets.")