-1

I have two variables in After Effects which get the X and Y position values of a layer named "Desk"…

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

I have some code that contains an array that needs to take those variables and add to them, but I'm not sure the syntax is correct… [posH+390.5,posV+218.5]

I know addition alone works, eg if I just do [1000+390.5,1000+218.5] then it works. I just need it to work with the variables.

EDIT ––––

I've tested it in isolation and it works, so maybe the bug is something to do with it being in a loop? The full code is here…https://pastebin.com/XfeUaqb8 you can see the variables at line 11 and 12 and the array im testing it with at line 24.

This script will position layers based on their name. Originally all my AE comps were 4000x4000 but now some are different sizes, so I need to modify it and use another layer as a constant reference point to refer to no matter the size of the AE comp or how it's cropped.

I have it working fine as an AE expression…

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

but obviously i need it as a script so that the character position can later be moved if needed without it just jumping back to values determined by the expression.

  • 1
    arrays contain values ... posH and posV are variables that now contain those values - so, yes ... posH+390.5 etc is correct – Jaromanda X Sep 23 '20 at 22:11
  • 3
    *"I'm not sure the syntax is correct"* - Did you try? Did you encounter a problem of some kind? – David Sep 23 '20 at 22:13
  • 1
    I don't understand whats the problem?! https://jsfiddle.net/ynhdufLb/ – ikiK Sep 23 '20 at 22:16
  • Thanks for your help. I edited the post and added some more information. – markpaterson Sep 23 '20 at 22:27
  • 1
    And again, https://jsfiddle.net/rwa8vpy9/ , its properly summed up in objects property, You still haven't answer whats the problem, what is result or error you are getting in those properties? You keep saying it does not work, and without saying what is the error – ikiK Sep 23 '20 at 22:33
  • There's no error message from After Effects. The layer just doesn't move. Your jsfiddle doesn't include my whole code. – markpaterson Sep 23 '20 at 22:50
  • There is a error in your loop that has nothing to do with this variables you are mentioning: https://jsfiddle.net/jan7pwyf/1/ Remove the loop, it will pass, add variables it will sum, the error is; `layer[i].name is undefined` – ikiK Sep 23 '20 at 22:54
  • That's probably just because you're not running it in AE and jsfiddle is not getting the selected layer name. Without my recent modifications, the loop works fine. – markpaterson Sep 23 '20 at 23:00
  • 1
    That may be the case but give example of layer object or array. Sample data, so we can test this properly. You can make [mre] out of this. – ikiK Sep 23 '20 at 23:00
  • https://jsfiddle.net/afoq4mgy/ You have here example that even after the loop, its added properly... I think your problem is not in this code. – ikiK Sep 23 '20 at 23:19
  • 1
    Thanks @iKiK. Your latest version works, at least so far that its helped me figure out that my variable is wrong. When I try `var posH = 100; var posV = 100;` it works, but when I change it back to my variables, it does not. Trying to make a min reproducible example. – markpaterson Sep 23 '20 at 23:35
  • That means you really need to show us the contents of posH and posV. That is why I asked for sample data. Its important, content and type. You need to extract it somehow. – ikiK Sep 23 '20 at 23:39
  • 1
    The data is just an after effects layer named "Desk", who's XY position value is 3285.5 x 2810.5. (I've renamed it from "Desk " because the trailing space was annoying me). The posH variable takes the X value, and the posV takes the Y. The ARI layer then adds 390.5 to the X and 218.5 to the Y. Sorry, I'm not sure what else I can provide. :( – markpaterson Sep 23 '20 at 23:46
  • I don't ether know how to help more from JS point of view. I dont know how you app is parsing the data if its fetched correctly at all... Btw maybe its a string, try using pasreFloat like this: https://jsfiddle.net/pyaqu281/ You will see i made vars in quote marks to make it strings, it does not work like that alone so parse it as number, then sum is working. Just add those two pars float lines to make them numbers, and maybe trim also to make sure... Thats the last I can think of, good luck. – ikiK Sep 24 '20 at 00:15

1 Answers1

1

If I understood you right, all you have to do is posH += 390.5 this will add 390.5 to the posH variable.

So let's say if we did this:

var posH = 10;
posH += 10;

if we would now print posH it would return 20.

Eryk
  • 36
  • 2
  • 7