Skip to main content
Inspiring
December 12, 2009
Question

Relative Stacking order of Sub Layers and items

  • December 12, 2009
  • 5 replies
  • 8170 views

I want to get the items in a layer sorted in their stacking order (including any sub-layers).

For example:

Layer 1

   item A

   Layer 1a

   Group B

   item C

In the example above, I would get back an array like this:

[item A, layer 1a, Group B, item C]

Of course, you'd think this is a simple enough thing given that zOrderPosition is freely available:

function getChildrenInStackingOrder(layer){

    var sublayers=layer.layers;

    var pageItems=layer.pageItems;

    var result=new Array ();

    var size=sublayers.length+pageItems.length;

    for(var i=0;i<sublayers.length;i++){

       result[sublayers.zOrderPosition]=sublayers;  <<<----------- ERROR

    }

    for(var i=0;i<pageItems.length;i++){

       result[pageItems.zOrderPosition]=pageItems;

    }

    return result;

}

My problem is that AI CS4 throws up at the line marked above with a message "Internal Error 1200". I'm guessing that it's illegal to get the zOrderPosition of a sublayer.  Any thoughts?

Anurag.

PS. Do any Adobe engineers read this forum at all? Seems like there's a lot of internal documentation that could help in answering some of our questions.

This topic has been closed for replies.

5 replies

nickcombs
Inspiring
July 18, 2024

There's good news on this old issue. Each layer and pageItem now has an absoluteZOrderPosition property. So we can compare those index numbers to determine exactly how the stacking order is composed.

Jung K Kwon
Participating Frequently
September 11, 2015
Participant
August 31, 2015

Does anyone know if there is an easier way to do this with more recent versions of illustrator?  Like CC 2014 or 2015?  Or maybe the bug has been fixed?

Participating Frequently
November 8, 2016

Bug still seems to be present in Illustrator CC 2015 and 2017. I have seen internal error 1200 trying to access zOrderPosition on anything. However, I discovered by writing some ExtendScript using the reflection property that there is a new and undocumented property called absoluteZOrderPosition that always seems to return a valid integer. Google suggests this has been around since Illustrator CC 2014.

CarlosCanto
Community Expert
Community Expert
November 9, 2016

google is right

Known Participant
May 5, 2011

Did anybody ever figure out a reliable solution to this?  I've come to agree that the zOrderPosition for a sublayer can't be compared to the zOrderPosition of other items on the layer (as they are sometimes identical values), so you can't tell if a given item on a layer is above or below a sublayer on the same layer.  Also, I've seen cases where siblings within a group have identical zOrderPosition values, making it impossible to detect z-order among elements of a group.  Further, I'm not convinced the documentation is correct when it declares that the zOrderPosition of a layer (in my case actually a sublayer) is the z-order among all layers in the document -- it seems more to be relative to just the parent layer.

I'd love to see some code that extends the example put forth by Muppet Mark to include (along with sublayers) groups and also pageitems on a layer that also has a sublayer.  To really test the solution, though, you need to make sure you re-order these items using the Layers panel, so you don't accidentally get a solution that works simply because the creation order was never disrupted.

Thanks,

- Rich

May 11, 2011

Not sure if it will help, but I posted a little while ago about  trying to "flatten" (kinda) some random amount of nested groups and  artwork.

Since I was trying to flatten everything, I pretty much  disregarded any layers or sub-layers, and therefore didn't really use  zOrderPosition for some reason.

Anyway, what I did learn was that,  while layers are listed in a different Array than PageItems, at the  Document level, all page items will appear in the proper order. So, if  your Layer palette looks like:

Layer 1

     Item 1

     Item 2

Layer 2

     Group A

          Item 3

     Item 4

     SubLayer 1

          Item 5

          Item 6

     Item 7

Accessing the doc.pageItems[] array would get you the following:

doc.pageItems[0]; // Item 1

doc.pageItems[1]; // Item 2

doc.pageItems[2]; // Group A

doc.pageItems[3]; // Item 3

doc.pageItems[4]; // Item 4

doc.pageItems[5]; // Item 5

doc.pageItems[6]; // Item 6

doc.pageItems[7]; // Item 7

So, while this doesn't directly tell you the "nesting" order of the items, you can find that doc.pageItems[5].parent == SubLayer 1, and since Item 5 is after Item 4, you can assume that SubLayer 1 is after Item 4?

Obviously it would take smarter logic, but might be a helpful tidbit.

Also, if you start messing around with pageItems[] indices, it's been  my experience that it is better to loop through from the "bottom" of  the stack and always operate on the last index to keep the indexes  intact.

Known Participant
May 11, 2011

Thanks for the tip!  Do you know if the document's "pageItems" array will still represent the stacking order if you re-arrange items in the Layers panel?  Like move a sublayer and a group around within their parent layer?  I had been using the pageItems array previously, but somehow got the idea that it didn't keep track of changes like this -- maybe I assumed wrong?

Participating Frequently
December 13, 2009

well for once layers and pageitems do not share the same zOrderPosition, so your code will fail to get correct results from the begining ... with a little testing you'll see that even if you didn't expected this there are pageitems that have the same zorder as some layers. Which points us to the fact that document priority is layer and after that page items.

Now regarding your problem ... for example if you have 5 empty layers and a shape within a layer called "ParentLayer" in the order Layer 1, Layer 2, Layer 3, Layer 4, Layer 5, Shape 1 you'll get your zOrderPosition just fine, but if you add a shape to both Layer 2 and Layer 3 you'll get an 1200 Internal Error. My guess it has something to do with indexing in composite layers, probably because of the Document.pathItems collection which makes 2 pathitems with same zOrderPosition. Seems like the problem appears when you have a orphan shape after a layer that contains a shape [this data might be wrong].

My workaround when getting this problem sometime in the past was to get every child that was not a layer into it's own layer, with the layer named as the object or if the situation permitted, get rid of the layers within that layer and keep only page items inside the layer. Keep in mind that this is a problem only with nested layers that have composite layer/shape structure.

hope it helps;

cheers;

Inspiring
December 13, 2009

Thanks sonic.
I was afraid that this might be the case but was hoping desperately that I was missing something that could be easily fixed :-)
A.

Muppet_Mark-QAl63s
Inspiring
December 14, 2009

I did not understand this one aren't all objects in objects and there stacking order position by index?