• EditorBugs
  • Illustrator export scrip can fail to generate json

I have found that the illustrator export script can silently fail to generate the json file when the illustrator project contains a group that has a hidden element inside it.

(no tags were fitting for this topic)

Related Discussions
...

It appears that the IllustratorToSpine script can indeed fail to generate the JSON file under certain conditions, such as when there are hidden elements within groups or locked layers. Here are some steps you can take to troubleshoot and potentially resolve this issue:

  1. Check for Hidden Elements and Locked Layers: Ensure that there are no hidden elements or locked layers within your Illustrator project. These can cause the script to fail silently.

  2. Enable "Write Spine JSON" Option: Make sure the "Write Spine JSON" option is checked when you run the script. If this option is not enabled, the script will only output images and not the JSON file.

  3. Simplify the Project: Try simplifying your Illustrator project by removing complex groupings or unnecessary elements to see if the script runs successfully.

  4. Send the File for Review: If the issue persists, you can send your Illustrator file to the support team at [contact@esotericsoftware.com](mailto:contact@esotericsoftware.com) for further investigation. Mention the specific issue and any steps you have already taken to troubleshoot.

Here is an example of a similar issue where a hidden element within a group caused the script to fail:

Screenshot

By following these steps, you should be able to identify and resolve the issue causing the script to fail.

Spinebot giving me my own screenshot as an example ๐Ÿ˜„

I was really just wanting to report this as a bug but sounds like it is already known about.

Is this something that has been looked into and is unfixable? It is quite limiting to have to avoid having hidden elements.

I believe I have managed to fix the export script.

I replaced the line:
pageItems[j].selected = true;

with:

			if (subItems != undefined)
			{
				for (var subIndex=0; subIndex<subItems.length; subIndex++)
				{
					var subItem = subItems[subIndex];
					if (subItem.locked == false && subItem.hidden == false) subItem.selected = true;
				}
			}
			else
			{
				pageItems[j].selected = true;
			}

(not sure why that won't format properly)

Basically if the page item is a group then loop through its contents and only select non hidden/locked things.

Haha, silly Spinebot!

I've incorporated your fix into the script, thank you!

So it turns out there were several other issues with the illustrator export script.

The main problem I was having was that it gave the wrong bounds for clipping groups so everything was misaligned when importing into spine. Other than that it was just some edge cases that broke things.

I ended up re-writing some of the export script, I think what I have now catches all of the issues that I found.

I re-wrote the parseLayer function:

function parseLayer(layer, info)
{
	var pageItems = layer.pageItems;
	var visibleBounds;

	var left = Number.POSITIVE_INFINITY;
	var top = Number.NEGATIVE_INFINITY;
	var right = Number.NEGATIVE_INFINITY;
	var bottom = Number.POSITIVE_INFINITY;
	
	var found = false;
	
	for (var j = 0; j < pageItems.length; j++ )
	{ 
		if (!pageItems[j].hidden)
		{
			visibleBounds = getVisibleBounds(pageItems[j]);
			if (visibleBounds == undefined) continue;

			found = true;

			left = Math.min(visibleBounds[0], left);
			right = Math.max(visibleBounds[2], right);
			top = Math.max(visibleBounds[1], top);
			bottom = Math.min(visibleBounds[3], bottom);
		}
	}
	
	if (found)
	{
		var layerName = getLayerName(layer);
		info.skinLayers[info.skinLayers.length] = layerName;

		ldata = {};
		ldata.width = (right - left).toFixed(2);
		ldata.height = (top - bottom).toFixed(2);
		ldata.x = (left + (ldata.width/2)).toFixed(2);
		ldata.y = (top - (ldata.height/2)).toFixed(2);

		info.slots[layerName] = ldata;
	}
}



function getVisibleBounds(object)
{
	var bounds, clippingItem;
	if (object.typename == "GroupItem")
	{
		if (object.clipped)
		{
			for (var i = 0; i < object.pageItems.length; i++)
			{
				if (object.pageItems[i].hidden) continue;

				if (object.pageItems[i].clipping)
				{
					clippingItem = object.pageItems[i];
					break;
				} 
				else if (object.pageItems[i].typename == "CompoundPathItem")
				{
					if (object.pageItems[i].pathItems[0].clipping) 
					{
						clippingItem = object.pageItems[i];
						break;
					}
				} 
			}

			if (clippingItem != undefined)
			{
				bounds = clippingItem.visibleBounds;
			}
		}

		if (bounds == undefined)
		{
			var subObjectBounds;

			var bounds = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY];
			var found = false;
			
			for (var i = 0; i < object.pageItems.length; i++)
			{
				if (object.pageItems[i].hidden) continue;
				found = true;

				subObjectBounds = getVisibleBounds(object.pageItems[i]);
				bounds[0] = Math.min(bounds[0], subObjectBounds[0]); // Left
				bounds[1] = Math.max(bounds[1], subObjectBounds[1]); // Up
				bounds[2] = Math.max(bounds[2], subObjectBounds[2]); // Right
				bounds[3] = Math.min(bounds[3], subObjectBounds[3]); // Down
			}

			if (!found) bounds = undefined;
		}
	}
	else
		bounds = object.visibleBounds;

	return bounds;
}

I forgot to say that another issue that the export script had was that it didn't take stoke width into account when calculating bounds, this would also cause things to be misaligned when importing into spine. My re-write should also fix this.

Thanks! I've updated the script.

Did you know your braces are on the wrong line? ๐Ÿ˜†

BTW, in 4.2 you can use Import PSD to bring a PSD file into Spine directly. This is much, much faster than using a script and it gives you all the layer tags and other functionality.
https://esotericsoftware.com/blog/Import-PSDs-directly-no-Photoshop-scripts-needed

Maybe you can save your Illustrator file as a PSD and use Import PSD.

    Nate Did you know your braces are on the wrong line? ๐Ÿ˜†

    ๐Ÿ˜’

    4 months later

    HelIo I'm new here, but I also found a bug in the Ai export script. It breaks the origin (images placement) when we use scale different than 100%. I checked the script and it does not change the coordinates accordingly to the scale, it only changes the scale and exports images. The origin is broken when we scale every layer separately.
    I have no time to fix the script now, but it will need some math calculations to rearrange the layers in new temp. document when scale different than 100%. I would do it in the following steps:

    1. Merge all layers into one group and scale it using (center point of the group, not origin)
    2. Calculate the delta (x,y) between the origin location before scale and after scale
    3. Reposition the group by this delta to preserve the same origin as before scaling
    4. Ungroup the layers
    5. Now export layers one by one
      I would be able to do it, but I have no time now to dig into your JSX code.

    The Illustrator script has never had all the features of the PSD script. You may be better off saving a PSD and using the PSD script so you get more features and fewer bugs, since many more users use the PSD script.

    However, recently Spine supports reading a PSD directly. That is MUCH faster, though has some caveats where not all PS features can be supported -- effects and other features that would be difficult or impossible for Spine to emulate what PS does (filters, effects, vector, etc). Often that can be worked around by using a smart object, which causes PS to save a raster version of the layer that Spine can use.
    https://esotericsoftware.com/spine-import-psd

    I know you are likely using Illustrator to create your graphics, but it may be feasible to bring it into PS just to export it for spine using the PS script or by saving a PSD that Spine can parse.

    I totally understand your point. You may be right. Wirth to check if this is easier to embed objects from Ai in Ps ๐Ÿ˜‰
    But I think that it may also fail somehow.

    PS (and Illustrator) scripts are very slow. Spine's PSD support being faster shouldn't be underestimated! It's very nice to process a PSD in 1-2 seconds. You're right that there may be some issues, particularly that Spine can't rasterize vector layers (unless inside a smart object). The PS script can do it though.