Script to set expression on property

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
histevemedia
Posts: 2
Joined: August 18th, 2018, 4:24 am
Location: Brighton, UK
Contact:

August 18th, 2018, 4:43 am

Hi all, first time posting so firstly thanks for all the help I've already found on here, hopefully you might be able to help with my next issue.

I'm creating my first script, which is designed to apply an expression to all "Stroke Width" properties of multiple selected layers. The trouble with this is that the "Stroke Width" property can be nested any number of times within properties, so it can't be indexed, and I can't just list through a layer's properties; I have to go through all of that properties properties, and all of that properties properties properties and so on. My solution for this is to add the "contents" property to an array, then iterate through that. Every time a property group is found, it's added to the end of the array, so that group will also eventually be iterated through until there's only individual properties. Then it checks the name of that property and adds the expression if it's a stroke width. It might be long winded, but it was the only solution I could work out.

Problem however, is that even though this seems to work, it doesn't actually add the expression to the property in AE. It finds them fine, even when I'm debugging and stepping through it shows that the variable I created for the property has the expression I added, but AE acts like nothing happened. My first thought is that maybe the property as a variable is sort of standing alone; I've made the variable the same as the property I'm looking for, but now I'm setting the expression on a variable, and not the actual property inside AE? Either way, I can't seem to work out how to get it onto the actual live property...

Here's how it looks right now:

Code: Select all

var layers = new Array();
var props = new Array();
var propsFilter = new Array();

//get selection
layers = app.project.activeItem.selectedLayers;

//loop through layers
for (i = 0; i < layers.length; i++){
    var lyr = layers[i];
    
    //if it's a shape layer...
    if(lyr instanceof ShapeLayer) {
        
        //loop through properties
        for (p = 1; p <= lyr.numProperties; p++){
            var prop = lyr.property(p);
            
            //fint contents and add to array.
            if (prop.name == "Contents"){
                props.push(prop);
                }
        }
    }
}

//once all "contents" properties are found, cycle through array
filterProps();

//cycles through all array items to find individual properties within nested groups
function filterProps(){
    for (p = 0; p < props.length; p++){
        var prp = props[p]
        
        //if the property is a property group, add all of it's properties to the end of the array (other common groups excluded
        if (prp.numProperties >= 1 && prp.name != "Material Options" && prp.name !="Transform" && prp.name != "Geometry Options"){
            for(p2 = 1; p2 <= prp.numProperties; p2++){
                props.push(prp.property(p2));
                }
            }
        //if the property is a stroke width, add the expression.
        else if(prp.name == "Stroke Width"){
                prp.expresson = "value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001";
                }
        }
    }
Thanks in advance for any help!
User avatar
histevemedia
Posts: 2
Joined: August 18th, 2018, 4:24 am
Location: Brighton, UK
Contact:

August 19th, 2018, 1:59 am

Fixed it. Was missing a return statement, duh!

Code if anyone wants it:

Code: Select all

var layers = new Array();
var props = new Array();
var propsFilter = new Array();

//get selection
layers = app.project.activeItem.selectedLayers;

//loop through layers
for (i = 0; i < layers.length; i++){
    var lyr = layers[i];
    
    //if it's a shape layer...
    if(lyr instanceof ShapeLayer) {
        
        //loop through properties
        for (p = 1; p <= lyr.numProperties; p++){
            var prop = lyr.property(p);
            
            //fint contents and add to array.
            if (prop.name == "Contents"){
                props.push(prop);
                }
        }
    }
}

//once all "contents" properties are found, cycle through array
filterProps();

//cycles through all array items to find individual properties within nested groups
function filterProps(){
    for (p = 0; p < props.length; p++){
        var prp = props[p]
        
        //if the property is a property group, add all of it's properties to the end of the array (other common groups excluded
        if (prp.numProperties >= 1 && prp.name != "Material Options" && prp.name !="Transform" && prp.name != "Geometry Options"){
            for(p2 = 1; p2 <= prp.numProperties; p2++){
                props.push(prp.property(p2));
                }
            }
        //if the property is a stroke width, add the expression.
        else if(prp.name == "Stroke Width"){
                prp = addExpression(prp);
                }
        }
    }

function addExpression(prpt){
    prpt.expression = "value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001;"
    return prpt;
    }
Post Reply