Highlighted

Having trouble with variables referencing a specific layer's X & Y position in ScriptUI

Explorer ,
Sep 23, 2020

Copy link to clipboard

Copied

I work on an animated TV series that involves characters often sitting at their desks. In order to speed up scene setups, I created a ScriptUI button that when clicked, looks at the character name to determine who it is, then applies appropriate position, scale, and anchor point values using a loop.

 

This worked great when our comps were all the same size, but recently they've been varying sizes. So I've decided to take a reference point from each comp, specifically the position of the character's desk. The script now looks at the position of the layer named "Desk" and adds (or subtracts) to its position values to arrive at the X,Y coords needed for correct placement.

 

You'll see examples of both approaches in the code below (the original version that worked purely on comp coords, and the new approach that uses layer reference). Please note that in the example below there's only two characters in the code. I've removed all the other characters purely so the post isn't super long. 

 

The issue I'm having is my posH and posV desk variables do nothing! However, if I comment them out and replace them with the dummy variables directly below, the script works!

 

 

myPanel.grp.group20.desk.onClick = function() {
         
    app.beginUndoGroup("Auto Desk Blocking")

    // Layers
    var layer = app.project.activeItem.selectedLayers;
    
    // Array for all the settings
    var movSettings = [];

    // Desk variables
    var posH = app.project.activeItem.layer("Desk").transform.position[0];
    var posV = app.project.activeItem.layer("Desk").transform.position[1];
             
    // Desk dummy variables
    // var posH = 3285.5;
    // var posV = 2810.5;

    // Objects for all the settings
    
    // Original example for 4000x4000 compositions
    movSettings[0] = {
        name: ["AQU.mov","AQU_VERTICAL.mov","AQU_PORTRAIT.mov","AQU_LANDSCAPE.mov","AQU_Vertical.mov","AQU_Portrait.mov","AQU_Landscape.mov","AQU_vertical.mov","AQU_portrait.mov","AQU_landscape.mov"],
        anchorPoint: [2000,3600],
        position: [1267,2984],
        scale: [47,47]
        }

    // This example uses another layer as a reference point, allowing for comps of any size
     movSettings[1] = {
        name: ["ARI.mov","ARI_VERTICAL.mov","ARI_PORTRAIT.mov","ARI_LANDSCAPE.mov","ARI_Vertical.mov","ARI_Portrait.mov","ARI_Landscape.mov","ARI_vertical.mov","ARI_portrait.mov","ARI_landscape.mov"],
        anchorPoint: [2000,3410],
        position: [posH+390.5,posV+218.5],
        scale: [50,50]
        }

       // Go though all layers
    for(var i = 0; i < layer.length; i++){
        // Look for layer with name in layer name
        for(var j = 0; j < movSettings.length; j++){
            for(var k = 0; k < movSettings[j].name.length; k++){
                
                if(layer[i].name.indexOf(movSettings[j].name[k]) != -1){
                    // Change layer settings   
                    layer[i].property("Anchor Point").setValue(movSettings[j].anchorPoint);
                    layer[i].transform.position.setValue(movSettings[j].position);
                    layer[i].transform.scale.setValue(movSettings[j].scale);
                    break;
                    
                }
            }
        }
    }
    
    app.endUndoGroup();
}

 

 

As a proof of concept, I have it working as an expression on the character's Position thus…

 

posH = thisComp.layer("Desk").transform.position[0]+390.5;
posV = thisComp.layer("Desk").transform.position[1]+218.5;
[posH, posV]

 

… however that's no good in real world use because the character can never move from their desk. How horrid! I need it to work as a ScriptUI button so that it simply stamps the values into place and allows for further adjustment after that.

Assuming the desk position is not animated, it seems like you should be doing something like this:

 

var pos = app.project.activeItem.layer("Desk").transform.position.value;
var newPos = [pos[0]+390.5,pos[1]+218.5];
alert(newPos);

 

Dan

TOPICS
Expressions, Scripting

Views

138

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

Having trouble with variables referencing a specific layer's X & Y position in ScriptUI

Explorer ,
Sep 23, 2020

Copy link to clipboard

Copied

I work on an animated TV series that involves characters often sitting at their desks. In order to speed up scene setups, I created a ScriptUI button that when clicked, looks at the character name to determine who it is, then applies appropriate position, scale, and anchor point values using a loop.

 

This worked great when our comps were all the same size, but recently they've been varying sizes. So I've decided to take a reference point from each comp, specifically the position of the character's desk. The script now looks at the position of the layer named "Desk" and adds (or subtracts) to its position values to arrive at the X,Y coords needed for correct placement.

 

You'll see examples of both approaches in the code below (the original version that worked purely on comp coords, and the new approach that uses layer reference). Please note that in the example below there's only two characters in the code. I've removed all the other characters purely so the post isn't super long. 

 

The issue I'm having is my posH and posV desk variables do nothing! However, if I comment them out and replace them with the dummy variables directly below, the script works!

 

 

myPanel.grp.group20.desk.onClick = function() {
         
    app.beginUndoGroup("Auto Desk Blocking")

    // Layers
    var layer = app.project.activeItem.selectedLayers;
    
    // Array for all the settings
    var movSettings = [];

    // Desk variables
    var posH = app.project.activeItem.layer("Desk").transform.position[0];
    var posV = app.project.activeItem.layer("Desk").transform.position[1];
             
    // Desk dummy variables
    // var posH = 3285.5;
    // var posV = 2810.5;

    // Objects for all the settings
    
    // Original example for 4000x4000 compositions
    movSettings[0] = {
        name: ["AQU.mov","AQU_VERTICAL.mov","AQU_PORTRAIT.mov","AQU_LANDSCAPE.mov","AQU_Vertical.mov","AQU_Portrait.mov","AQU_Landscape.mov","AQU_vertical.mov","AQU_portrait.mov","AQU_landscape.mov"],
        anchorPoint: [2000,3600],
        position: [1267,2984],
        scale: [47,47]
        }

    // This example uses another layer as a reference point, allowing for comps of any size
     movSettings[1] = {
        name: ["ARI.mov","ARI_VERTICAL.mov","ARI_PORTRAIT.mov","ARI_LANDSCAPE.mov","ARI_Vertical.mov","ARI_Portrait.mov","ARI_Landscape.mov","ARI_vertical.mov","ARI_portrait.mov","ARI_landscape.mov"],
        anchorPoint: [2000,3410],
        position: [posH+390.5,posV+218.5],
        scale: [50,50]
        }

       // Go though all layers
    for(var i = 0; i < layer.length; i++){
        // Look for layer with name in layer name
        for(var j = 0; j < movSettings.length; j++){
            for(var k = 0; k < movSettings[j].name.length; k++){
                
                if(layer[i].name.indexOf(movSettings[j].name[k]) != -1){
                    // Change layer settings   
                    layer[i].property("Anchor Point").setValue(movSettings[j].anchorPoint);
                    layer[i].transform.position.setValue(movSettings[j].position);
                    layer[i].transform.scale.setValue(movSettings[j].scale);
                    break;
                    
                }
            }
        }
    }
    
    app.endUndoGroup();
}

 

 

As a proof of concept, I have it working as an expression on the character's Position thus…

 

posH = thisComp.layer("Desk").transform.position[0]+390.5;
posV = thisComp.layer("Desk").transform.position[1]+218.5;
[posH, posV]

 

… however that's no good in real world use because the character can never move from their desk. How horrid! I need it to work as a ScriptUI button so that it simply stamps the values into place and allows for further adjustment after that.

Assuming the desk position is not animated, it seems like you should be doing something like this:

 

var pos = app.project.activeItem.layer("Desk").transform.position.value;
var newPos = [pos[0]+390.5,pos[1]+218.5];
alert(newPos);

 

Dan

TOPICS
Expressions, Scripting

Views

139

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
Sep 23, 2020 0
Explorer ,
Sep 23, 2020

Copy link to clipboard

Copied

I'm unable to edit my post for some reason, but the latest version has the variables with valueAtTime(0), as I now understand that scripting can't get those values without it. Regardless, it still doesn't work 😞

         var posH = app.project.activeItem.layer("Desk").transform.position.valueAtTime(0)[0];
         var posV = app.project.activeItem.layer("Desk").transform.position.valueAtTime(0)[1];

 

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...
Sep 23, 2020 0
Explorer ,
Sep 23, 2020

Copy link to clipboard

Copied

I thought maybe a previous variable might be interfering with my posH and posV variables. As you can see I have `var layer = app.project.activeItem.selectedLayers;` right before it. Could it be that the layer variable is screwing up the `app.project.activeItem.layer("Desk")` bit?

 

In any case, I changed it (and any references to it) from `var layer = app.project.activeItem.selectedLayers;` to `var slayer = app.project.activeItem.selectedLayers;` but it still doesn't work.

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...
Sep 23, 2020 0
Guide ,
Sep 23, 2020

Copy link to clipboard

Copied

You can help yourself pretty well if you alert() your variables, to see their actual data. You can use alert() as well to check, if the loops are running correctly.

alert() whatever you need to know on runtime - that's how I find my bugs.

 

*Martin

 

 

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...
Sep 23, 2020 0
Explorer ,
Sep 23, 2020

Copy link to clipboard

Copied

Interesting. I've not used Alerts before. Could you give me an example for posH ?

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...
Sep 23, 2020 0
Guide ,
Sep 24, 2020

Copy link to clipboard

Copied

You mean something like alert(posH);  -  duuude?!

 

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...
Sep 24, 2020 0
Explorer ,
Sep 24, 2020

Copy link to clipboard

Copied

I tried that before I wrote you back! haha. Nothing happened. Was I supposed to get an alert dialog? 

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...
Sep 24, 2020 0
Guide ,
Sep 24, 2020

Copy link to clipboard

Copied

Nothing happend? This already means something if off in your code.

 

Just to clearify: you run the script inside of AE?

 

And yes, there should be a dialog box popping up with the value of posH. Or something like [object], or even NaN, Null, undefinded.

When working with alert, you might add .name, or .value, .length to your variable to get a useful information, like e.g.:

alert(mySelectedLayers) -> [object]

alert(mySelectedLayers.length) -> 3

 

Put the alert beneath this line:

var posV = app.project.activeItem.layer("Desk").transform.position[1];

 

It should work. Then go on, put it into the for-loop and check the result (turn of your speakers, you'll get a lot alert when putting it into the for-loop).

 

*Martin

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...
Sep 24, 2020 0
Explorer ,
Sep 24, 2020

Copy link to clipboard

Copied

Yes, I'm running it in AE. Ok, so when I put alert(layer); under var layer = app.project.activeItem.selectedLayersI get an popup alert saying "object AVLayer".

When I put it under alert(posH); under

varposH = app.project.activeItem.layer("Desk").transform.position.valueAtTime(0)[0]; I get nothing! Which leads me to believe something is fundamentally wrong with this line of code, and I have no idea what.
If I change those variable lines to…
var posH = 3285.5;
alert(posH);
var posV = 2810.5;
alert(posV);
I get alerts with those values and the whole script works. Obviously, I need to reference the Desk layer, so hard coded numbers won't cut it for me, but as a demo of what's broken it's obviously those two original variable lines. So weird.

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...
Sep 24, 2020 0
Explorer ,
Sep 24, 2020

Copy link to clipboard

Copied

Just discovered that if I do… 

var posH = app.project.activeItem.layer("Desk").transform.position;
alert(posH);
I get an alert
 

If I add [0] to the end…

var posH = app.project.activeItem.layer("Desk").transform.position[0];
alert(posH);
I DO NOT get an alert
 

If I add valueAtTime(0)[0]

var posH = app.project.activeItem.layer("Desk").transform.position.valueAtTime(0)[0];
alert(posH);
I DO NOT get an alert

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...
Sep 24, 2020 0
Guide ,
Sep 24, 2020

Copy link to clipboard

Copied

Assuming the desk position is not animated, it seems like you should be doing something like this:

 

var pos = app.project.activeItem.layer("Desk").transform.position.value;
var newPos = [pos[0]+390.5,pos[1]+218.5];
alert(newPos);

 

Dan

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...
Sep 24, 2020 0
Explorer ,
Sep 24, 2020

Copy link to clipboard

Copied

Thanks Dan! I was able to get alert feedback from that! So a slight modification did the trick in terms of modifying it for my script…

 

I put var pos = app.project.activeItem.layer("Desk").transform.position.value; at the top, and then 

position: [pos[0]+390.5,pos[1]+218.5], inside the loop! I've really been banging my head against a wall trying to figure this out, so it's much appreciated!
 
 
 

 

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...
Sep 24, 2020 0
Explorer ,
Sep 23, 2020

Copy link to clipboard

Copied

After some more tinkering, I've found that the 'position' section in the loop just fails if there's variables in the ARI section. 

 

position: [posH+390.5,posV+218.5],

 

Not only that, but it makes ALL other character layers fail, even though they currently only have standard number values. If my entire code is as above except I change ARI to this…

 

position: [1267,2984],

 

then all character layers works fine. Is there a special way I need to add a variable inside of a loop that doesn't just break everything?

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...
Sep 23, 2020 0