Highlighted

Get the actual width and height of layer

Engaged ,
Apr 28, 2020

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

691

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Get the actual width and height of layer

Engaged ,
Apr 28, 2020

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

692

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 28, 2020 0
Participant ,
Apr 28, 2020

Copy link to clipboard

Copied

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;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 28, 2020 0
Engaged ,
Apr 28, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 28, 2020 0
Engaged ,
May 03, 2020

Copy link to clipboard

Copied

Wait, you turn the object returned from the expression into a string, and then parse that string? That's crazy. Why not just use the object?
Also AVLayer objects have a sourceRectAtTime() function natively in extendscript now (AE 2020 at least).

 

var dimensions = myTextLayer.sourceRectAtTime(t, false);

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 03, 2020 1
Adobe Community Professional ,
May 04, 2020

Copy link to clipboard

Copied

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.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 04, 2020 0
Engaged ,
May 04, 2020

Copy link to clipboard

Copied

Hi Rick

 

Thank you for answering. I know how it works. Here's the problem. If you:

1. Create a shape layer with any shape.

2. Give it a thick stroke or a path effect like Pucker&Bloat.

3. Use the sourceRectAtTime method to get the size with includeExtends set to true.

Now it will return a size that is much larger than the layer visually is.

My question is: Does anybody know a workaraoud for that?

 

All the best

Jakob

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 04, 2020 0
Engaged ,
May 04, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 04, 2020 1
Engaged ,
May 04, 2020

Copy link to clipboard

Copied

To the best of my knowledge there aren't.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 04, 2020 0
Participant ,
May 04, 2020

Copy link to clipboard

Copied

Maybe the weird funcion that converts a function into string, and then returns a string, and then you covert it into an object with JSON do the job 😉

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 04, 2020 0
Engaged ,
May 04, 2020

Copy link to clipboard

Copied

I generally use the workaround of adding a point control with an expression that calculates toComp etc, and then getting the value of that point control (no string conversion involved, except for making the expression). I just was hoping that like sourceRectAtTime it had been brought from expressions to scripts.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 04, 2020 0