Ultra simple script (but failed....)

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Hello,
I'm new in scripting, and I try to make a really simple script. Here it is :

{
app.beginUndoGroup("Particular script");
// Creating project
var currentProject = (app.project) ? app.project : app.newProject();
// Creating comp
var compSettings = cs = [1920, 1080, 1, 120, 25];
var defaultCompName = "Particular Script";
var currentComp = cc = (currentProject.activeItem) ? currentProject.activeItem : currentProject.items.addComp(defaultCompName, cs[0], cs[1], cs[2], cs[3], cs[4]);
currentComp.openInViewer();

// Creating particular layer, ajouter effet particular et ajouter effet parametre glissiere
var myWord = "Curseur";
var particularLayer = currentComp.layers.addSolid([93, 5, 2], "Particular", cs[0], cs[1], cs[2]);
var effetParticular = particularLayer.Effects.addProperty("Particular");
var effetNbParticules = particularLayer.Effects.addProperty("ADBE Slider Control");
effetNbParticules.name = "nbParticules";

effetNbParticules.property(1).setValue(500);
effetParticular.property("Particles/sec").setValue(1);
effetParticular.property("Particles/sec").expression = "value*thisComp.layer(\"" + effetNbParticules.name + "\")(\""+myWord+ "\")";

app.endUndoGroup();
}

Any idea?

Thanks!
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

Well for me it failed because I don't have Particular installed.
Have you gone into Preferences > General and turned on Enable Javascript Debugger? What error message do you get?

Paul
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Paul Tuersley wrote:Well for me it failed because I don't have Particular installed.
Have you gone into Preferences > General and turned on Enable Javascript Debugger? What error message do you get?

Paul
Yes, it underlines the line 19 : "effetParticular.property("Particles/sec").setValue(1);"
And when I turn Javascript Debugger off, AE says "Unable to execute script at line 19. Object is invalid"
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

I haven't tested this but it's possibly losing the connection to the Particular effect object stored in the variable effetParticular when the second effect is added. This kind of thing is unfortunately quite common in scripting.

Try reassigning the variable again after you've added the other effect:
effetParticular = particularLayer.Effects.property(1);

or just access it directly:
particularLayer.Effects.property(1).property("Particles/sec").setValue(1);

Paul
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Paul Tuersley wrote:I haven't tested this but it's possibly losing the connection to the Particular effect object stored in the variable effetParticular when the second effect is added. This kind of thing is unfortunately quite common in scripting.

Try reassigning the variable again after you've added the other effect:
effetParticular = particularLayer.Effects.property(1);

or just access it directly:
particularLayer.Effects.property(1).property("Particles/sec").setValue(1);

Paul
OK, that works!! I didn't know it often loses the connection!

Thank you very much!
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

As a little tip, you know from the error message that it must be either the effect object or the effect' property that is causing the error. In this case, to debug I would insert a couple of lines like this to try to narrow down the error.
$.writeln(effetParticular.name);
$.writeln(effetParticular.property("Particles/sec").name);

On finding the first of those lines was causing an error I might also try this to confirm I need to reassign the variable:
$.writeln(particularLayer.Effects.property(1).name);
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Paul Tuersley wrote:As a little tip, you know from the error message that it must be either the effect object or the effect' property that is causing the error. In this case, to debug I would insert a couple of lines like this to try to narrow down the error.
$.writeln(effetParticular.name);
$.writeln(effetParticular.property("Particles/sec").name);

On finding the first of those lines was causing an error I might also try this to confirm I need to reassign the variable:
$.writeln(particularLayer.Effects.property(1).name);
Ok thank you, I don't understand everything but I will try!
Another (little) help : I would want to create this solid in the same width and height as the comp selected in my project, how can I write that?
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

currentComp.width
currentComp.height
beginUndoGroup
Posts: 81
Joined: November 27th, 2012, 6:41 am

Hello,
small (off topic) comment, after reading through your initial question.

When you declare multiple variables like this: var compSettings = cs = [1920, 1080, 1, 120, 25];
it "works": both entities (compSettings and cs) are now variables and are assigned the value [1920, 1080, 1, 120, 25];
But if you are in a function scope, only the first one is declared as a local variable.
The second one (cs), if it already exists will be simply assigned the value, otherwise it is declared as a global variable.
You can check that it indeed works this way running this test script in ESTK:

Code: Select all

(function ()
{
	var cs1 = cs2 = 3;
	
	alert($.global.cs1);
	alert($.global.cs2);
	})();
The first alert alerts undefined because cs1 is a local variable.
The second alert alerts 3, meaning that cs2 has been attached as a property of the global object. Not good.

In fact var compSettings = cs = [1920, 1080, 1, 120, 25]; is equivalent to
  • var compSettings = [1920, 1080, 1, 120, 25];
    cs = [1920, 1080, 1, 120, 25];
If you don't want to "pollute" the global workspace, you should do either:
  • var compSettings, cs;
    compSettings = cs = [1920, 1080, 1, 120, 25];
either:
  • var compSettings = [1920, 1080, 1, 120, 25],
    cs = [1920, 1080, 1, 120, 25];
This may sounds minor detail, but it may have some consequences.
For instance it is often the case that several scripts, sometimes from different authors, are active at the same time.
So each script should (1) keep its variables safe, (2) care about other scripts.
Xavier.
Post Reply