Skip to main content
Inspiring
April 28, 2020
Question

Get the actual width and height of layer

  • April 28, 2020
  • 2 replies
  • 6199 views

Hi

 

Just checking really, maybe someone here found a way.

I want to get the actual size of a vector layer including strokes and path effects like Pucker&Bloat or repeater. Is that in any way possible with scripting?

I could write a script that would loop through all vector props and calculate strokes, but some things are specificly difficult/mystic like Pucker&Bloat or WigglePath.

 

Thanks,

Jakob Wagner

This topic has been closed for replies.

2 replies

Community Expert
May 4, 2020

Point to a layer defined as the target, target.sourceRectAtTime(time, true) will always return the outside bounds of one or more shapes on a shape layer including strokes and layer effects at the current time. Set the second operator to false and only the shape layer object defined by the actual path will be evaluated. Any additional paths or strokes added to a path will not be evaluated. The true or false operator will not ignore layer animators or the Transform>Shape or Transform>Group properties.

 

I'm not much of a scriptwriter but I do use sourceRectAtTime() all the time.

 

stib
Inspiring
May 4, 2020

Do you know if there is a script version of the expression function toComp() or fromComp()? They'd be super handy.

Inspiring
May 4, 2020

To the best of my knowledge there aren't.

migueld83371718
Inspiring
April 28, 2020

The best way I know to get any layer real size is to use sourceRectAtTime. The problem is that sourceRectAtTime is only available via expressions. So, a solution is to set an expressions on a text layer sourceText property to retrieve almost any data from the rest of the layers in the comp. Here is one way to do it( you will need to include a JSON  parser to your script):

#include "path/to/your/JSON/file"
/** Get any layer sourceRectAtTime 
 * @function getLayerSourceRectAtTime
 * @param {Layer} layer - The layer
 * @param {Number} time - The time where you want to get the SRT
 * @param {Property} sourceText - The sourceText property of a text layer
 * @param {Boolean} extents - Extents for the sourceRectAtTime expression
 * @returns -Object
 */
function getLayerSourceRectAtTime(layer, time, sourceText, extents) {
  var time = time || layer.inPoint;
  var index = layer.index
  //This is the function that goes on the text layer expression
  //lt => left - top
  //lb => left - bottom
  // ....
  function getSourceRectAtTime(tm, ind, extents) {
    s = thisComp.layer(ind);
    b = s.sourceRectAtTime(tm, extents || false);
    lt = s.toWorld([b.left, b.top], tm);
    lb = s.toWorld([b.left, b.top + b.height], tm);
    rt = s.toWorld([b.left + b.width, b.top], tm);
    rb = s.toWorld([b.left + b.width, b.top + b.height], tm);
    return '{ "lt":[' + lt[0] +',' +lt[1]+ '], "lb":[' + lb[0] +',' +lb[1]+ '],"rt":[' + rb[0] +',' +rb[1]+ '],"rb":[' + rb[0] +',' +rb[1]+ ']}';
  }

  sourceText.expression = getSourceRectAtTime.toString() + '; getSourceRectAtTime(' + time + ', ' + index +', ' + extents +')';

  var worldBounds = sourceText.value.text;

  return JSON.parse(worldBounds);
};


var myRect = getLayerSourceRectAtTime(yourLayer, theTime, yourTextLayerSourceText, theExtens);
var layerHeight = myRect.lb - myRect.lt;
Inspiring
April 28, 2020

Thank you for answering. Cool function. 🙂

SourceRectAtTime actually works very similar in expressions and scripting. The problem is it doesn't work with shape layers and extends.

If the layer is a shape layer and you set includeExtends in the sourceRectAtTime method to true, it will return the extends that include the "render canvas" for the stroke and path effects. And the size of that is unpredictable.

In other words. Using sourceRectAtTime will not do the job alone.

 

- Jakob