Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions SCRIPT: Determine the number of values in a property

  • SCRIPT: Determine the number of values in a property

    Posted by Yoan Boisjoli on March 29, 2023 at 12:51 pm

    Hey everyone!
    I’m writing a short script where I need a function to determine how many set of values a property has (Example is opacity is selected, the alert should read 1, if it’s position it should read 2 (x and y) and so on).

    So far it seems to work with single properties (like opacity) and 3D position (x, y and z). It return respectively 1 and 3. But it also returns 3 when dealing with arrays of two like position and scale.

    Here’s the function:

    function getPropertyValuesCount() {

    var activeItem = app.project.activeItem;

    var selectedLayers = activeItem.selectedLayers;

    var selectedProperties = selectedLayers[0].selectedProperties;

    var property = selectedProperties[0];

    var valuesCount = 0;

    if (property.propertyValueType !== PropertyValueType.NO_VALUE) {

    try {

    if (property.value.length) {

    valuesCount = property.value.length;

    } else {

    valuesCount = 1;

    }

    } catch (e) {

    alert("Error occurred while determining the number of values: " + e.message);

    return;

    }

    } else {

    alert("The selected property has no value.");

    return;

    }

    alert(valuesCount);

    }

    getPropertyValuesCount();

    If you’re able to spot what I got wrong.

    Thanks in advance !

    Filip Vandueren replied 1 year, 8 months ago 3 Members · 5 Replies
  • 5 Replies
  • Tomas Bumbulevičius

    March 29, 2023 at 1:05 pm

    Hey Yoan, the problem is that even though two dimensional property is set to 2D, it reads three values?

    propertyValueType actually returns a four digits number, specific for the property. If you are dealing only with transformations, you could use that number to specify counts.

  • Yoan Boisjoli

    March 29, 2023 at 1:12 pm

    Thanks for the reply Tomas!

    Yeah I don’t think I’m on the right track with propertyValueType I’m starting to think that I should maybe use value.length instead.

    I actually managed to do it in the expression editor in after effects with:

    var p = thisComp.layer("Orange Solid 1").transform.position;
    var numVal = p.value.length;
    if(!numVal){
    1;
    }else{
    p.value.length;
    };

    When there was only one value, it returned nothing that my reasoning behind the if(!numVal).

    But can’t get it to work in the script.

  • Filip Vandueren

    March 29, 2023 at 9:55 pm

    Like Tomas said, behind the scenes position is always a 3D property, it’s just that when the layer is set to 2D, the 3rd value is 0.

    Just like normal rotation is still called Z rotation for a 2D layer, it’s probably programmed so values don’t need to be copied around into different properties when a layer changes.

    In Epressions, AE seem to do some logic so they see it as 2 vs -long arrays, but not in scripting alas.

  • Yoan Boisjoli

    March 30, 2023 at 1:08 am

    I manage to obtain the result that I wanted by checking if the layer is 2D or 3D and manually targeting scale, position and anchor point. But if I select an effect’s property, I still have to figure out how to let the script know about it. Right now I’m manually putting the index of the effect’s property.

    Here’s what I have so far:

    function getPropertyValuesCount() {

    var activeItem = app.project.activeItem;

    if (!activeItem || !(activeItem instanceof CompItem)) {

    alert("Please select a composition.");

    return;

    }

    var selectedLayers = activeItem.selectedLayers;

    if (selectedLayers.length === 0) {

    alert("Please select a layer.");

    return;

    }

    var selectedProperties = selectedLayers[0].selectedProperties;

    if (selectedProperties.length === 0) {

    alert("Please select a property.");

    return;

    }

    var property = selectedProperties[0];

    var layer = selectedLayers[0];

    var valuesCount = 0;

    var is3DLayer = layer.threeDLayer;

    var isEffectProperty = property.propertyGroup(1).matchName.indexOf("ADBE Effect Parade");

    if (property.propertyValueType !== PropertyValueType.NO_VALUE) {

    try {

    if (property.value && property.value.length) {

    valuesCount = property.value.length;

    // Check if the layer is 2D and the selected property is either Position or Scale

    if (!is3DLayer && (property.matchName === "ADBE Position" || property.matchName === "ADBE Scale" || property.matchName === "ADBE Anchor Point")) {

    valuesCount = 2;

    }

    // Check if the property is within an effect and has two values

    if (isEffectProperty && valuesCount === 1 && typeof property.value[1] !== "undefined") {

    valuesCount = 2;

    }

    } else {

    valuesCount = 1;

    }

    } catch (e) {

    alert(e.message);

    return;

    }

    } else {

    alert("The selected property has no value.");

    return;

    }

    alert(valuesCount);

    }

    getPropertyValuesCount();

  • Filip Vandueren

    March 30, 2023 at 7:08 am

    Well for Effect Properties I think you can just use value.length ?

    They use Point controls or 3D Point controls and those don’t do the switching around like position parameters do based on 2D/3D status ?

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