unable to execute script line 4. null is not an object.
Hello!
I'm trying to write my first script for after effects but I keep getting this message 'unable to execute script line 4. null is not an object.' What does it mean?
Thanks!

Hello!
I'm trying to write my first script for after effects but I keep getting this message 'unable to execute script line 4. null is not an object.' What does it mean?
Thanks!

There's a fair bit wrong in that code but I guess it's tripping up because of the prop.addProperty thing. addProperty is a method for adding things like an effect to a layer but it's not applicable here. You said you wanted to add position expressions so you don't even need selectedProperties either, although you weren't using that correctly too as selectedProperties is an array of all selected properties where you'd then access them one at a time using prop[0], prop[1], etc.
And you really should be testing whether activeItem is a valid comp as it returns either the currently active comp, a selected item in the project panel, or null if no comps are selected or multiple items selected in project panel. At every stage you need to be verifying the validity of what you're then relying on in later code.
As for whether you even need a function for this, that depends on where you're taking the script. Typically you might want to add a UI and have a button that on clicking calls a function. But in that case you really need to be putting everything beginning with defining and checking the activeItem into your onClick function. It's a common mistake people make to define those variables on initial script launch and fail to consider it needs to keep working as expected when the user switches comps. If you plan to launch the script every time though then you don't even really need a function, just something to loop through any currently selected layers.
You might also want to avoid just accessing variables defined outside the function when inside the function, rather than sending the relevant object to the function (I kinda show that here in that prop and theProp are two different variables with theProp defined inside the function). You can use global variables and access them inside a function if you really need to but possibly not best practice for you right now. You can get all kinds of unexpected behaviour if you don't fully understand the scope of your variables and use the same ones inside and outside of functions.
I fixed your expression too as I presume you meant 'time' not 't'. So this isn't necessarily the right solution as I don't know what you're ultimately planning, but it is at least a working solution.
var layer, prop, x;
var comp = app.project.activeItem;
function bdy(theProp){
theProp.expression =
'y=Math.sin(time*2*Math.PI)*10;\n' +
'value + [0,y]'
}
if (comp != null && comp instanceof CompItem) {
for (x = 0; x < comp.selectedLayers.length; x++) {
layer = comp.selectedLayers[x];
prop = layer.property("ADBE Transform Group").property("ADBE Position");
bdy(prop);
}
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.