# Any way to batch add layer properties across many tmx files?

**URL:** <https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490>\
**Category:** Questions\
**Created:** [January 4, 2024, 4:51pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490 "2024-01-04T16:51:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![radstronomical](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/radstronomical/32/4395_2.png) [@radstronomical](https://discourse.mapeditor.org/u/radstronomical)\
**Post date:** [January 4, 2024, 4:51pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/1 "2024-01-04T16:51:59Z")

</div>

Hey! I’ve got a set of maps that I’m trying to use in a unity port of a project. There are probably 25 or so maps, and each map has over 20 layers. I now need to add custom layer properties to each of those layers on all of the maps…

Has anyone used any kind of batch processing solution for back-modifying maps? I’m using SuperTiled2Unity for importing, so I’d like the end result to be a .tmx file – otherwise I’d just batch process the json output.

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [January 4, 2024, 4:56pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/2 "2024-01-04T16:56:11Z")

</div>

You could do this as a Tiled script. The Tiled API makes it easy to iterate open maps (or maps in a Project), iterate their layers, and add properties to them (whether all the same ones, or with dynamically determined values). Then, you can Save All to save your changes.

> **[Tiled Scripting API](https://www.mapeditor.org/docs/scripting/)**
>
> Documentation for Tiled Scripting API

---

<div class="post-metadata">

**Author:** ![radstronomical](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/radstronomical/32/4395_2.png) [@radstronomical](https://discourse.mapeditor.org/u/radstronomical)\
**Post date:** [January 4, 2024, 4:57pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/3 "2024-01-04T16:57:00Z")

</div>

Interesting - I’ll look into it! Thanks for the quick response.

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [January 4, 2024, 5:05pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/4 "2024-01-04T17:05:28Z")

</div>

One thing to keep in mind is that `TileMap.layers` only contains the _top-level_ layers. If you use Groups, you will need to iterate those yourself.

This example script iterates all the top-level layers in all the currently open maps and adds an “example” boolean property set to whether the layer is a tile layer:

```js
let addExampleProperty = tiled.registerAction("AddExampleProperty", function(action) {
   for(asset of tiled.openAssets) {
      if(!asset.isTileMap) continue;
      for(layer of asset.layers) {
         layer.setProperty("example", layer.isTileLayer);
      }
   }
});
addExampleProperty.text = "Add Example Property";

```

But if you want to correctly work with group layers, you’d have to modify it. I usually use recursive functions, e.g:

```js
let addExampleProperty = tiled.registerAction("AddExampleProperty", function(action) {
   function addProperty(layerOrMap) {
      if(layerOrMap.isGroupLayer || layerOrMap.isTileMap) {
         for(layer of layerOrMap.layers) {
            addProperty(layer);
         }
      }
      if(!layerOrMap.isTileMap) {
         layerOrMap.setProperty("example", layerOrMap.isTileLayer);
      }
   }

   for(asset of tiled.openAssets) {
      if(asset.isTileMap)
         addProperty(asset);
   }
});
addExampleProperty.text = "Add Example Property";

```

---

<div class="post-metadata">

**Author:** ![radstronomical](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/radstronomical/32/4395_2.png) [@radstronomical](https://discourse.mapeditor.org/u/radstronomical)\
**Post date:** [January 4, 2024, 6:26pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/5 "2024-01-04T18:26:23Z")

</div>

Thanks! This is exactly what I need, and I haven’t grouped my layers so it should be fairly simple. One question - can you think of any reason why asset.layers would return undefined here? I’ve just got one file open to test, and I’ve confirmed that it’s being recognized and is a tilemap through console output.

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [January 4, 2024, 6:34pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/6 "2024-01-04T18:34:39Z")

</div>

I didn’t test these scripts and it’s possible I made a typo or used the wrong property name, but I _think_ it should be alright…  
Is it _consistently_ undefined, or just occasionally? In some versions of Tiled, there are some garbage collection issues, causing things to be `null` or undefined when they shouldn’t be, at random.

In my own scripts, I don’t usually use `for(layer of asset.layers)`, but instead use a regular for loop that avoids storing `layers` locally where they can be garbage collected more easily, maybe it’ll work more consistently?

```js
for(let i = 0; i < layerOrMap.layerCount; ++i) {
   //do work on layerOrMap.layerAt(i);
}

```

---

<div class="post-metadata">

**Author:** ![radstronomical](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/radstronomical/32/4395_2.png) [@radstronomical](https://discourse.mapeditor.org/u/radstronomical)\
**Post date:** [January 4, 2024, 6:46pm UTC](https://discourse.mapeditor.org/t/any-way-to-batch-add-layer-properties-across-many-tmx-files/6490/7 "2024-01-04T18:46:55Z")

</div>

It seems like it was a Tiled version issue, I updated to the latest release and seems to have resolved. Thanks for your help!
