FORUMS: list search recent posts

Random Layer Size Script

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
martijn lambada
Random Layer Size Script
on Jun 26, 2015 at 7:57:29 am

Hi All,

I'm writing a script that randomises the scale of the selected layers (need a script since there will be over 270 layers in the comp).
The problem is, when I activate the myLayer.property("Scale").setValue(myVal +[rnd,rnd]);
line in the code, the random numbers aren't random any more.

E.g. without this line it outputs:
23
66
45
33
46
22
67

(7 layers selected in my comp).

With the line active it outputs:

63
17
17
17
17
17
17

(also with 7 layers selected in my comp).

Anybody has any clue what is going on here?
Help is very much appreciated.

Thanks!

Cheers,
Martijn

var myComp = app.project.activeItem;
var selectedLayers = myComp.selectedLayers;
for (var i = 0; i &lt; selectedLayers.length; i++) {

var myLayer = selectedLayers[i];

var rnd = Math.floor((Math.random() * 100) + 1); // round number between 1 and 100


$.writeln(rnd);

var myVal = myLayer.property("Scale").value;
//myLayer.property("Scale").setValue(myVal +[rnd,rnd]);



}


Return to posts index

martijn lambada
Re: Random Layer Size Script
on Jun 26, 2015 at 8:57:38 am

Hi!

After some more investigation I found out it has to do with after effects not finishing
the operation. If include a sleep(1000); at the end it does work.
Though this is not very elegant (nor reliable). Is there a way to get a callback from after effects informing the script that it has finished processing the command?

Or maybe my inexperience with programming has me approaching this problem from a completely wrong direction.

Thanks in advance!
Cheers.
Martijn

ps. update of script below

function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i &lt; 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}


var myComp = app.project.activeItem;
var selectedLayers = myComp.selectedLayers;
for (var i = 0; i &lt; selectedLayers.length; i++) {

var myLayer = selectedLayers[i];

var rnd = Math.floor((Math.random() * 100) + 1); // round number between 1 and 100

$.writeln(rnd);

var myVal = myLayer.property("Scale").value;
myLayer.property("Scale").setValue(myVal +[rnd,rnd]);

var myNewVal = myLayer.property("Scale").value;




sleep(1000);


}


Return to posts index

Dan Ebberts
Re: Random Layer Size Script
on Jun 26, 2015 at 1:53:43 pm

It works fine for me in CC 2015. With the setValue() line enabled, I get:

47
65
44
29
11
99
39

I'm not sure why it's not working for you.

Dan



Return to posts index


martijn lambada
Re: Random Layer Size Script
on Jun 26, 2015 at 2:14:42 pm

Hi Dan,

Thanks for your reply.

That's really weird! I'm also on CC2015 (MacBook Pro late 2013).
Same behaviour on CC2014, even on different workstations here at the office.

Hmmm.
Will try to find a solution, if I do, I'll upload the answer.

Thanks again.
M


Return to posts index

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