Generated automatic Tiled tilesets (.tsx) with chroma key #00FF00 configured
This commit is contained in:
127
scripts/generate_tiled_project.py
Normal file
127
scripts/generate_tiled_project.py
Normal file
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
🧱 GENERATE TILED TILESETS (TSX)
|
||||
Creates .tsx files for all green-screened assets with transparency configured.
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import glob
|
||||
|
||||
# Constants
|
||||
ASSET_ROOT = "assets"
|
||||
TILESET_OUTPUT_DIR = "assets/maps/tilesets"
|
||||
TRANSPARENT_COLOR = "00ff00" # Green Screen Color
|
||||
|
||||
# XML Templates
|
||||
TSX_HEADER = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<tileset version="1.10" tiledversion="1.10.2" name="{name}" tilewidth="{width}" tileheight="{height}" tilecount="{count}" columns="0">
|
||||
<grid orientation="orthogonal" width="1" height="1"/>
|
||||
"""
|
||||
|
||||
TILE_TEMPLATE = """ <tile id="{id}">
|
||||
<image width="{width}" height="{height}" source="{source}" trans="{trans}"/>
|
||||
</tile>
|
||||
"""
|
||||
|
||||
TSX_FOOTER = """</tileset>
|
||||
"""
|
||||
|
||||
def get_image_size(path):
|
||||
# Try to get image dimensions without PIL to avoid dependency if possible,
|
||||
# but since we already used PIL for green screen, we can use it here.
|
||||
try:
|
||||
from PIL import Image
|
||||
with Image.open(path) as img:
|
||||
return img.width, img.height
|
||||
except Exception:
|
||||
return 32, 32 # Default fallback
|
||||
|
||||
def generate_tileset(directory, name_prefix):
|
||||
"""Generates a Collection of Images tileset for a directory"""
|
||||
|
||||
# Find all PNGs
|
||||
png_files = sorted(list(Path(directory).rglob("*.png")))
|
||||
if not png_files:
|
||||
return False
|
||||
|
||||
tileset_name = f"{name_prefix}_{os.path.basename(directory)}"
|
||||
tsx_content = TSX_HEADER.format(
|
||||
name=tileset_name,
|
||||
width=32, # Default (doesn't matter much for collection of images)
|
||||
height=32,
|
||||
count=len(png_files)
|
||||
)
|
||||
|
||||
print(f"📦 Generating tileset: {tileset_name}.tsx ({len(png_files)} images)")
|
||||
|
||||
for i, img_path in enumerate(png_files):
|
||||
# Calculate relative path from tileset location to image
|
||||
# Tiled needs relative paths
|
||||
abs_img = img_path.resolve()
|
||||
abs_tileset_dir = Path(TILESET_OUTPUT_DIR).resolve()
|
||||
|
||||
try:
|
||||
rel_path = os.path.relpath(abs_img, abs_tileset_dir)
|
||||
except ValueError:
|
||||
# Fallback if on different drives (unlikely here)
|
||||
rel_path = str(abs_img)
|
||||
|
||||
width, height = get_image_size(abs_img)
|
||||
|
||||
tsx_content += TILE_TEMPLATE.format(
|
||||
id=i,
|
||||
width=width,
|
||||
height=height,
|
||||
source=rel_path,
|
||||
trans=TRANSPARENT_COLOR # THIS IS THE MAGIC PART!
|
||||
)
|
||||
|
||||
tsx_content += TSX_FOOTER
|
||||
|
||||
# Save TSX
|
||||
output_path = os.path.join(TILESET_OUTPUT_DIR, f"{tileset_name}.tsx")
|
||||
with open(output_path, "w") as f:
|
||||
f.write(tsx_content)
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
print("🧱 TILED TILESET GENERATOR")
|
||||
print("=" * 50)
|
||||
print(f"Target Transparency: #{TRANSPARENT_COLOR}")
|
||||
print(f"Output Directory: {TILESET_OUTPUT_DIR}\n")
|
||||
|
||||
os.makedirs(TILESET_OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
directories_to_process = [
|
||||
("assets/PHASE_PACKS/0_DEMO", "DEMO"),
|
||||
("assets/PHASE_PACKS/1_FAZA_1", "FAZA1"),
|
||||
("assets/PHASE_PACKS/2_FAZA_2", "FAZA2"),
|
||||
("assets/sprites", "SPRITES"),
|
||||
("assets/crops", "CROPS"),
|
||||
("assets/characters", "CHARS")
|
||||
]
|
||||
|
||||
total_generated = 0
|
||||
|
||||
for dir_path, prefix in directories_to_process:
|
||||
if os.path.exists(dir_path):
|
||||
# Process main directory
|
||||
if generate_tileset(dir_path, prefix):
|
||||
total_generated += 1
|
||||
|
||||
# Optionally process subdirectories as separate tilesets if needed
|
||||
# For now, we put everything in one big tileset per main folder to be safe
|
||||
# But "sprites" is huge, let's split sprites by immediate subdirectory
|
||||
if "sprites" in dir_path:
|
||||
for subdir in Path(dir_path).iterdir():
|
||||
if subdir.is_dir():
|
||||
generate_tileset(subdir, f"SPRITE_{subdir.name.upper()}")
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print(f"✅ Generated {total_generated} main tilesets + sub-tilesets.")
|
||||
print("👉 Import these .tsx files into Tiled map to see automatic transparency!")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user