adobe-photoshop/generator-core

How to get layer bounds in each layer comps

chrislearn opened this issue · 5 comments

Hello,
In my psd, it have two layer comps, and I want to compute a layer's bounds in these two layer comps. I use some code like this, but not sure it is correct:

comp.layerSettings.forEach(function(ls){
          if (ls.layerID === layer.id){
            if (ls.vectorMask && ls.vectorMask.offset){
              var dx = bounds.left - ls.vectorMask.offset.horizontal, dy = bounds.top - ls.vectorMask.offset.vertical;
              bounds.left = ls.vectorMask.offset.horizontal;
              bounds.top = ls.vectorMask.offset.vertical;
              bounds.right -= dx;
              bounds.bottom -= dy;
            }else if (ls.offset){
              bounds.left += ls.offset.horizontal;
              bounds.right += ls.offset.horizontal;
              bounds.top += ls.offset.vertical;
              bounds.bottom += ls.offset.vertical;
            }
          }
        });

I found some layer's bounds will have 1 pixel offset, for example it's actual bounds is: {left:0, top: 0, width:100, height:200}, but I will get {left:-1, top:-1, width:99, height:199}.

In comp layerSetting's data, what's differences about offset and vectorMask field?

offset is the offset for the layer content, vectorMask is just the vector mask. The layer "content", the vector shape and the user mask can (but don't always) move independently, so you have to consider all three if you want to know the comp bounds. When you also consider the fx can change, and that the masks are applied differently depending on the advanced blending settings (applied to fx or not), it can be a little tricky to compute the bounds with the comp applied, just given the comp and layer settings. If you are going to get the thumbnail/pixels for the comp anyway, it might be better to just use the bounds of the thumbnail returned.

Thanks, @timothynoel, I really need the position data of layers in each comps.

Really the only way to get boundsWithFx for comps is to get pixmap and rely on bounds data in pixmap (keeping in mind that it might have random offset in about 3px).
Really I didn't find any way to find out what layers where modified in composition as current structure contains mostly all layers even if they were not modified. So you have to do it for all layers in document.

closing as answered. There doesn't seem to be an immediate generator work to be done here.