Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions [HELP] My Script UI code isn’t working but i don’t know why

  • [HELP] My Script UI code isn’t working but i don’t know why

  • Vincenzo Imbimbo

    December 10, 2022 at 1:45 pm

    Hello guys, i’m trying to create an script ui with a button that does this:

    Add an expression to any selected properties, but if you are selecting a layer (or multiple layers) instead of properties then pre-compose that layers, i don’t know if this is possible, but the code should be working, and it doesn’t, it adds the expression to selected properties but the pre-compose option is not working can you guys help me?

    This is the code:

    var win = new Window("palette", "My Script UI", undefined); // Add a button for the expression

    var myButton = win.add("button", undefined, "Button");

    // Add an event listener for the button's click event

    myButton.onClick = function() {

    // Get the active composition

    var comp = app.project.activeItem;

    // Get the selected properties

    var selProps = comp.selectedProperties;

    // Loop through the selected properties

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

    // Get the current property

    var prop = selProps[i];

    // Check if the property is a shape path

    if (prop.propertyValueType == PropertyValueType.SHAPE) {

    // Add the custom expression to the shape path property

    prop.expression = "my custom expression";

    } else {

    // Add the another custom expression to the property

    prop.expression = "my other custom expression";

    }

    // Check if the active item is a layer

    if (comp.activeItem instanceof AVLayer) {

    // Get the active layer

    var layer = comp.activeItem;

    // Precompose the layer

    var precomp = layer.precompose(layer.name, true);

    }

    }

    }

    // Show the window

    win.show();



  • Andrei Popa

    December 10, 2022 at 2:48 pm

    Hi Vicenzo.

    You can try this.

    var win = new Window("palette", "My Script UI", undefined);
    // Add a button for the expression
    var myButton = win.add("button", undefined, "Button");
    // Add an event listener for the button's click event
    myButton.onClick = function() {
    // Get the active composition
    var comp = app.project.activeItem;
    // Get the selected properties
    var selProps = comp.selectedProperties;
    if(selProps.length > 0 ){
    // Loop through the selected properties
    for (var i = 0; i < selProps.length; i++) {
    // Get the current property
    var prop = selProps[i];
    // Check if the property is a shape path
    if (prop.propertyValueType == PropertyValueType.SHAPE) {
    // Add the custom expression to the shape path property
    prop.expression = "my custom expression";
    } else {
    // Add the another custom expression to the property
    prop.expression = "my other custom expression";
    }
    // Check if the active item is a layer
    if (comp.activeItem instanceof AVLayer) {
    // Get the active layer
    var layer = comp.activeItem;
    // Precompose the layer
    var precomp = layer.precompose(layer.name, true);
    }
    }
    //if no selected property
    }else{
    var selLayers = comp.selectedLayers;
    //get an array with the indexes of the selected layers
    var indices =[];
    for(var i=0; i< selLayers.length; i++){
    indices[i] = selLayers[i].index;
    }
    comp.layers.precompose(indices, "someName", true);
    }
    }
    // Show the window
    win.show();

    You need to check if no property is selected, and then precomp the selected layers. You can change the “someName” to any name you want, or you could use selLaters[0].name to use the name of the first selected layers (works even if only one layer is selected).

  • Vincenzo Imbimbo

    December 10, 2022 at 4:23 pm

    Thank you Andrei! It works pefect now, i forgot to ask another thing, after the layers got pre-composed i want to enable the time remapping of that precomp, so, is there a way to do it? Because i need to add something like “if the selected layer is already a precomp then don’t pre-compose it, just enable the time remap”.

  • Andrei Popa

    December 11, 2022 at 9:04 am

    This applies expressions if properties are selected, precomposes if layers are selected, enables time-remap if a layer that is a comp is selected.

    var win = new Window("palette", "My Script UI", undefined);
    // Add a button for the expression
    var myButton = win.add("button", undefined, "Button");
    // Add an event listener for the button's click event
    myButton.onClick = function() {
    // Get the active composition
    var comp = app.project.activeItem;
    // Get the selected properties
    var selProps = comp.selectedProperties;
    if(selProps.length > 0 ){
    // Loop through the selected properties
    for (var i = 0; i < selProps.length; i++) {
    // Get the current property
    var prop = selProps[i];
    // Check if the property is a shape path
    if (prop.propertyValueType == PropertyValueType.SHAPE) {
    // Add the custom expression to the shape path property
    prop.expression = "my custom expression";
    } else {
    // Add the another custom expression to the property
    prop.expression = "my other custom expression";
    }
    // Check if the active item is a layer
    if (comp.activeItem instanceof AVLayer) {
    // Get the active layer
    var layer = comp.activeItem;
    // Precompose the layer
    var precomp = layer.precompose(layer.name, true);
    }
    }
    //if no selected property
    }else{
    var selLayers = comp.selectedLayers;
    //check if only one layer is selected, and if it is a comp
    if(selLayers.length == 1 && selLayers[0].source && selLayers[0].source.typeName == "Composition"){
    selLayers[0].timeRemapEnabled = true;
    }else{
    //get an array with the indexes of the selected layers
    var indices =[];
    for(var i=0; i< selLayers.length; i++){
    indices[i] = selLayers[i].index;
    }
    comp.layers.precompose(indices, "someName", true);
    }
    }
    }
    // Show the window
    win.show();
Viewing 1 - 4 of 4 posts

Log in to reply.

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