FORUMS: list search recent posts

Advice on Script that Creates Keys of a Keyboard

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Tim Franklin
Advice on Script that Creates Keys of a Keyboard
on Oct 20, 2015 at 3:49:13 pm

Hi all,

I'm new to javascript and script writing and have been wrting some test scripts. Below is a short script that creates 3 rows of the keys on a keyboard. In the javascript course I was going through they constantly said to not repeat yourself and this script definitely is repetitive.

After line 19 everything is pretty much the same so I put in a comment "///You Can Stop Reading Here" so you don't have to read through the whole thing. The only change is

How can I use fewer loops? I used two for loops for every row- one loop created the text layers (searched array) and the other repositioned them.(searched the comp item collection)
Is it possible to create layers from an array and position them at the same time?

I'm trying hard to improve my scripting so any advice would be greatly appreciated.

Thanks in advance for any help!

-Tim

app.beginUndoGroup("Keyboard Setup");

var items = app.project.items;
{
var asdfComp = items.addComp("asdf",1920,1080,1,20,24);
var asdf = ["Caps Lock","A","S","D","F","G","H","J","K","L",";","'","Enter","4","5","6"];
asdf.reverse(); //reversed the array so After Effects would create them in the correct order
var aLength = asdf.length;
for(i=0;i<aLength;i++){ //creates text layers
var text = asdf[i];
asdfComp.layers.addText(text);
};

for(i=1;i&lt;=aLength;i++){ //repositions the text layers in the new comp
var currentPos = asdfComp.layer(i).property("Position");
var xPos = (i*75)
var newPos = currentPos.setValue([xPos,540]);
};
}; ////// You can Stop Reading Here

{
var qwerComp = items.addComp("qwer",1920,1080,1,20,24);
var qwer = ["Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","|","Delete","End","Page Down","7","8","9","+"];
qwer.reverse();
var qLength=qwer.length;

for(i=0;i<qLength;i++){
var text = qwer[i];
qwerComp.layers.addText(text);
};

for(i=1;i&lt;=qLength;i++){
var currentPos = qwerComp.layer(i).property("Position");
var xPos = (i*75)
var newPos = currentPos.setValue([xPos,340]); // This changes the Position of the Text Layers
};
};
{
var zxcvComp = items.addComp("zxcv",1920,1080,1,20,24);
var zxcv = ["Shift","Z","X","C","V","B","N","M",",",".","/","Shift"," ","1","2","3","Enter"]
zxcv.reverse();
var zLength=zxcv.length;

for(i=0;i<zLength;i++){
var text = zxcv[i];
zxcvComp.layers.addText(text);

};
for(i=1;i&lt;=zLength;i++){
var currentPos = zxcvComp.layer(i).property("Position");
var xPos = (i*75)
var newPos = currentPos.setValue([xPos,740]);
};
};


Tim Franklin
Watchout System Programmer/Compositor - CBS Sports
Freelance Visual Effects Artist/ Video Editor
http://www.TimSFranklin.com


Return to posts index

Dan Ebberts
Re: Advice on Script that Creates Keys of a Keyboard
on Oct 20, 2015 at 4:53:13 pm

I think I'd do it like this:


function buildRow(theName,theArray,theYPos){
var myComp = app.project.items.addComp(theName,1920,1080,1,20,24);
var myLayer;
for (var i = 0; i < theArray.length; i++){
myLayer = myComp.layers.addText(theArray[i]);
myLayer.moveToEnd();
myLayer.property("Position").setValue([(i+1)*75,theYPos]);
}
return myComp; // optional
}
buildRow("adsf",["Caps Lock","A","S","D","F","G","H","J","K","L",";","'","Enter","4","5","6"],540);
buildRow("qwer",["Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","|","Delete","End","Page Down","7","8","9","+"],340);
buildRow("zxcv",["Shift","Z","X","C","V","B","N","M",",",".","/","Shift"," ","1","2","3","Enter"],740);



Dan



Return to posts index

Tim Franklin
Re: Advice on Script that Creates Keys of a Keyboard
on Oct 21, 2015 at 8:06:32 pm

Hi Dan,
Wow! That is definitely much more clean. I was having trouble figuring out how to use functions to create new comps but this definitely cleared it up.

Thanks so much for the quick reply!

-Tim

Tim Franklin
Watchout System Programmer/Compositor - CBS Sports
Freelance Visual Effects Artist/ Video Editor
http://www.TimSFranklin.com


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]