Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Script for Precomping Text

  • Script for Precomping Text

    Posted by Royce Jacobs on February 29, 2024 at 11:38 pm

    I’m trying to write a script to precomp text layers, with the precomp being the size of the text bounding box, just as you can do with source layers (“Leave all attributes”). I found -this script binary- but it was written 13 years ago and doesn’t quite work right now. Its problems are the same as the problems I’m running into as I try to recreate the script, which is that the text transforms within the new comp should be default and centered, and the new precomp’s transforms should match the text’s, but are default. I can’t seem to get a grasp of transforms in scripting, can only get the return [object global]. Here’s what I have currently, which makes a new comp from the text later and copies the text into it:

    // Check if there is a project open

    if (app.project) {

    // Get the active composition

    var comp = app.project.activeItem;

    // Check if the active item is a composition

    if (comp && comp instanceof CompItem) {

    // Get the selected layer

    var selectedLayer = comp.selectedLayers[0];

    // Check if there is a selected layer

    if (selectedLayer) {

    // Create a new composition with the dimensions of the selected layer

    var layerTransf = selectedLayer.property("ADBE Transform Group");

    var layerPos = selectedLayer.property("ADBE Transform Group").property("ADBE Position");

    var newComp = app.project.items.addComp(selectedLayer.name, Math.round(selectedLayer.sourceRectAtTime(0, true).width), Math.round(selectedLayer.sourceRectAtTime(0, true).height), comp.pixelAspect, comp.duration, comp.frameRate);

    // Copy the selected layer to the new composition

    var newLayer = comp.layer(selectedLayer.name).copyToComp(newComp);

    // Reset transforms of layer in new composition

    // newLayer.position.setValue([newComp.width / 2, newComp.height / 2]);

    // newLayer.scale.setValue([100, 100]);

    // newLayer.rotation.setValue([0]);

    // newLayer.Transform.setValue(layerTransf);

    } else {

    alert("Please select a layer.");

    }

    } else {

    alert("Please open a composition.");

    }

    } else {

    alert("Please open a project.");

    }

    I’m still trying to figure out how to set the transforms of the new text layer and new precomp, so any help would be greatly appreciated by this newbie 🙏

    Dan Ebberts replied 2 years, 4 months ago 2 Members · 1 Reply
  • 1 Reply
  • Dan Ebberts

    March 1, 2024 at 12:32 am

    The plain vanilla version (no position keyframes, no scaling) would look something like this (replaces your copyToComp line):

    comp.layer(selectedLayer.name).copyToComp(newComp);
    var newLayer = newComp.layer(1);
    var rect = newLayer.sourceRectAtTime(0,false);
    newLayer.property("Position").setValue([-rect.left, -rect.top]);

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy