Script for setting custom properties on objects

I’m an artist laying out objects in Tiled that are almost all images that require “assetKey” property to be named the same as the PNG. I am repeating this often manually and would love a script that I can run after dragging multiple tiles from a tileset.

Are you talking about Tile Objects? You might not need a script for that: if you set those properties on the tiles in the tileset, the Tile Objects will inherit them. You can then either read the tiles to get their “assetKey” when you load the map, or you can export with the “resolve object types and properties” setting enabled (in Edit > Preferences) to include that inherited property explicitly with the Tile Objects.

If that doesn’t work for you, I can try to write such a script, but I’ll need some more details on how you want the script to work:

  • Are the tiles from an image collection, from a regular tileset, or both?
  • Should all Tile Objects have these properties set, or only the selected ones, or some other criteria? Tiled currently has no way to detect newly added objects except through a very tedious process that I wouldn’t dare code.
  • is this assetKey a string with just the file name of the PNG (e.g. “asset.png”), or a file path to the PNG?
  • Do you want this to be an action that you manually trigger, or something that runs automatically whenever you save the map?
  • If a given Tile Object’s assetKey doesn’t match its tile’s assetKey, should it be updated, or left as-is?

Edit: Had a go based on my guess as to what you’re going for.
For Image Collections, this will grab the tile filenames, for tilesheets, it’ll grab the tilesheet filename. All Tile Objects have their assetKey set regardless of current value or selection status, and assetKey is a string with just the filename. The assetKey is automatically set on save, but you can comment out or delete the last few lines if you don’t want that (I haven’t tested this automatic part, but it should work).

/*	Apply assetKeys script by eishiya, last updated Sep 28 2023

	Adds an action to the Map menu that sets "assetKey" on all Tile Objects
	to the tile's file name.
*/

var applyTileObjectProperties = tiled.registerAction("applyAssetKeys", function(action) {
	var map = tiled.activeAsset;
	if(!map || !map.isTileMap)
		return;

	function copyProperties(curLayer) {
		if(curLayer.isObjectLayer) {
			let objects = curLayer.objects;
			for(let obj = 0; obj < objects.length; ++obj) {
				let object = objects[obj];
				if(object.tile) {
					let tile = object.tile;
					let tileName = tile.imageFileName;
					if(!tileName || tileName.length == 0) tileName = tile.tileset.image; //fallback to tilesheet name for non-Collections
					if(tileName && tileName.length > 0) {
						tileName = FileInfo.fileName(tileName); // path/to/tile.png -> tile.png
						object.setProperty("assetKey", tileName);
					}
				}
			}
		} else if(curLayer.isGroupLayer || curLayer.isTileMap) {
			var numLayers = curLayer.layerCount;
			for(var layerID = 0; layerID < numLayers; layerID++) {
				copyProperties(curLayer.layerAt(layerID));
			}
		}
	}

	copyProperties(map);
});

applyTileObjectProperties.text = "Set assetKeys";
tiled.extendMenu("Map", [
	{ action: "applyAssetKeys", before: "MapProperties" }
]);

//Run this action automatically when saving the map:
tiled.assetAboutToBeSaved.connect(function(asset) {
	if(!asset.isTileMap) return;
	let prevAsset = tiled.activeAsset; //save the user's currently viewed asset
	tiled.activeAsset = asset;
	tiled.trigger("applyAssetKeys");
	tiled.activeAsset = prevAsset; //bring back user's viewed asset
});

Thank you so much for this! I really appreciate it. I’m sorry its taken me so long to reply back. I had passed this script on to my engineer to look at. He gave me some direction on adding the script to a new .tiled folder and he added something to the script to remove the file extension in the assetKey field I believe. I still haven’t run it yet as my work flow has been diverted back to Spine for animations. But I will let you know next time I lay out a new Tiled file. Thank you!