Creative COW SIGN IN :: SPONSORS :: ADVERTISING :: ABOUT US :: CONTACT US
Creative COW's LinkedIn GroupCreative COW's Facebook PageCreative COW on TwitterCreative COW's Google+ PageCreative COW on YouTube
ADOBE AFTER EFFECTS:HomeForumBasicsExpressionsTutorialsPodcastsAE TechniquesCreative Cloud DebateFAQ

AE script to create solids and automatically parent them to a null.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Share on Facebook
Chris GavinAE script to create solids and automatically parent them to a null.
by on Jul 8, 2010 at 1:57:05 pm

Hello,

I'm quite new to expressions/scripting.
I'm trying to write an AE .jsx script to create a scene which automatically creates a comp, generates many solids and places them (by 3D rotations) around a central origin to create the population of people standing on a planet.
Also in the script I create a null at the centre of the planet.

My script works so far and generates all of the 3D layers, it also generates the Null and even a camera for the scene.

But I can't seem to write the script which automatically parents all of the solids to the null object. I.e. I'm dynamically generating the layers but unable to dynamically link them up to the null from within my .jsx code.

The problem is right at the end of the script, here...
// connect new solid to null at planet centre

Any help would be very gratefully received, with thanks, Chris

{

// Create script undo group

app.beginUndoGroup("Create Square");

// create project if necessary

var proj = app.project;
if(!proj) proj = app.newProject();

// create new comp named 'my comp'

var compW = 1920; // comp width
var compH = 1080; // comp height
var compL = 15; // comp length (seconds)
var compRate = 25; // comp frame rate
var compBG = [48/255,63/255,84/255] // comp background color



var myItemCollection = app.project.items;
var myComp = myItemCollection.addComp('my comp',compW,compH,1,compL,compRate);
myComp.bgColor = compBG;

//create camera
var w = myComp.width /2 ;
var h = myComp.height /2 ;
var newCamera = myComp.layers.addCamera("New Camera",[w,h]);
newCamera.position.setValue([w,h,-2500]);


//create null
var myNull = myComp.layers.addNull();
myNull.threeDLayer = true;


//set planet radius
var planetRadius=400;

// for loop to create multiple solids
var totalCount=50;
for (i=1; i<=totalCount; i++)
{

var randomnumber=Math.floor(Math.random()*361)

// create new solid named "my square"
var manXsize=20;
var manYsize=58;
var mySolid = myComp.layers.addSolid([1.0,1.0,0], "man"+i, manXsize, manYsize, 1, compL);
mySolid.threeDLayer = true;

// place anchor point at base of solid + planetRadius
mySolid.property("anchorPoint").setValue([manXsize/2,manYsize+planetRadius,0]);

// 3D rotations on the solids
mySolid.property("rotationY").setValue([randomnumber]);
mySolid.property("rotationZ").setValue([360*i/totalCount]);

// connect new solid to null at planet centre
myComp.layer(i).parent.setValue(myNull);






}
app.endUndoGroup();

}


Return to posts index
Reply   Like  


cowcowcowcowcow
Filip VanduerenRe: AE script to create solids and automatically parent them to a null.
by on Jul 8, 2010 at 3:11:05 pm

I believe you can simply do:

myComp.layer(i).parent = myNull;

setValue() is for properties that can have keyframes.




Return to posts index
Reply   Like  
+1

Chris GavinRe: AE script to create solids and automatically parent them to a null.
by on Jul 9, 2010 at 9:41:42 am

Felip,

Thanks so much for your help.
This works for one layer!
I'd hoped that applying it from within my for loop might hook up all of my solids to the null as I generate them. I'm thinking it's probably just a case of me not addressing the layers properly (as .parent needs to be addressed to layers) so I'll keep trying more things...
I've just noticed that my when i=1 I get a man1 solid created but this is on layer 50 and this is the one that gets hooked up to the null. If I can figure out how these layer numbers are being attributed, I've a better chance of addressing them properly!


I much appreciate you putting me right on the syntax of layer.parent, thanks again.
Chris


Return to posts index
Reply   Like  


Chris GavinRe: AE script to create solids and automatically parent them to a null.
by on Jul 9, 2010 at 10:02:53 am

Filip,

Okay,

I don't know why my script genrates the solids and calls them man1 - to man50 ,but puts them on layers numbered 50 down to 1 !
(i.e. the layer numbers are descending from 50 as the solid number increments.)
But I DO have a work around.
I can make a new for loop to connect all of the solids to the null after they have all been generated. I use your line within this loop and it works just fine.

for (i=1; i<=totalCount; i++)
{
// connect new solid to null at planet centre
myComp.layer(i).parent = myNull;
}

As I say, I don't know why the layer numbers are out of step with the i number when I generate the solids, maybe this is just 'one of those things'.
thanks for making my first posting to Creative Cow a good experience!
Chris


if(!proj) proj = app.newProject();

// create new comp named 'my comp'

var compW = 1920; // comp width
var compH = 1080; // comp height
var compL = 15; // comp length (seconds)
var compRate = 25; // comp frame rate
var compBG = [48/255,63/255,84/255] // comp background color



var myItemCollection = app.project.items;
var myComp = myItemCollection.addComp('my comp',compW,compH,1,compL,compRate);
myComp.bgColor = compBG;

//create camera
var w = myComp.width /2 ;
var h = myComp.height /2 ;
var newCamera = myComp.layers.addCamera("New Camera",[w,h]);
newCamera.position.setValue([w,h,-2500]);


//create null
var myNull = myComp.layers.addNull();
myNull.threeDLayer = true;


//set planet radius
var planetRadius=400;

// for loop to create multiple solids
var totalCount=50;
for (i=1; i<=totalCount; i++)
{

var randomnumber=Math.floor(Math.random()*361)

// create new solid named "my square"
var manXsize=20;
var manYsize=58;
var mySolid = myComp.layers.addSolid([1.0,1.0,0], "man"+i, manXsize, manYsize, 1, compL);
mySolid.threeDLayer = true;

// place anchor point at base of solid + planetRadius
mySolid.property("anchorPoint").setValue([manXsize/2,manYsize+planetRadius,0]);

// 3D rotations on the solids
mySolid.property("rotationY").setValue([randomnumber]);
mySolid.property("rotationZ").setValue([360*i/totalCount]);

// end of for loop to create solids
}

// this for loop to hook up solids to null
for (i=1; i<=totalCount; i++)
{
// connect new solid to null at planet centre
myComp.layer(i).parent = myNull;
}




Return to posts index
Reply   Like  

Filip VanduerenRe: AE script to create solids and automatically parent them to a null.
by on Jul 9, 2010 at 3:17:37 pm

It might seem counter intuitive but it's not:
layer 1 is the topmost layer of your comp.
After effects supposes that when you add a layer to a comp, it is on top,
You could just use this in the loop:

myComp.layer(1).parent = myNull;

Because the last layer you created will always be layer 1

However, even easier, Doesn't this just work?

mySolid.parent = myNull;



Return to posts index
Reply   Like  

Chris GavinRe: AE script to create solids and automatically parent them to a null.
by on Jul 9, 2010 at 3:40:10 pm

Got it! Your latest and simplest solution works a treat and doesn't even need to use the layer numbers. It seems then that I can use the .parent method with mySolid just as I can with a Layer.

Also now I understand why the layer numbers are all 'backwards' too, and it does indeed make perfect sense.

thanks very much
Chris


Return to posts index
Reply   Like  

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Share on Facebook


FORUMSTUTORIALSFEATURESVIDEOSPODCASTSEVENTSSERVICESNEWSLETTERNEWSBLOGS

Creative COW LinkedIn Group Creative COW Facebook Page Creative COW on Twitter
© 2013 CreativeCOW.net All rights are reserved. - Privacy Policy

[Top]