Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions parent a null to 2 different position

  • parent a null to 2 different position

    Posted by Raphael Schaaf on January 31, 2011 at 5:43 pm

    can someone please help me?
    4 days ago i started scripting in after effects, mainly because i wanted to automaticaly create a new null. the x-position of my null is parented to the x of one selected layer, the z-position is parented to the x of another selected layer.
    y-position is the average of the y-positions of the two selected layers

    it depends on the names of my first and second layer which selected layer is responsible for the x/z position of the null. this works fine, but the new null always stays in the middle of the comp…

    this is my script:

    thisComp = app.project.activeItem

    var x = thisComp.selectedLayers[0].position[0];
    var z = thisComp.selectedLayers[1].position[0];
    var Y = (thisComp.selectedLayers[0].position[1] + thisComp.selectedLayers[1].position[1]) / 2;

    front = new String (“front”);
    back = new String (“back”);
    right = new String (“right”);
    left = new String (“left”);

    video1 = String (thisComp.layers[1].name)
    video2 = String (thisComp.layers[2].name)

    if (video1 == front) {

    var X = x;

    }else{
    if (video1 == back) {

    var X = x * (-1);

    }else{
    if (video1 == right) {

    var Z = x;

    }else{
    if (video1 == left) {

    var Z = x * (-1)

    }else{

    alert (“choose compatible videoname”)
    }
    }
    }
    }

    if (video2 == front) {

    var X = z;

    }else{
    if (video2 == back) {

    var X = z * (-1);

    }else{
    if (video2 == right) {

    var Z = z;

    }else{
    if (video2 == left) {

    var Z = z * (-1)

    }else{

    alert (“choose compatible videoname”)
    }
    }
    }
    }

    number += 1;
    var trackName = “Trackpoint ” + number;

    newTrackpoint = thisComp.layers.addNull();
    newTrackpoint.name = trackName;
    newTrackpoint.threeDLayer = true;

    newTrackpoint.position= [X,Y,Z];

    …………………………………………………………..

    i hope someone can help me…

    by the way, does anybody know how to change the index of a layer? i want my new null to be on bottom, not on top…

    Txus Da silva replied 14 years, 2 months ago 3 Members · 14 Replies
  • 14 Replies
  • Dan Ebberts

    February 1, 2011 at 1:58 am

    What do you mean by “parented”? I don’t completely get what you’re trying to end up with, but I suspect it’s got to be simpler than the way you’re going at it. Are you trying to create a dynamic relationship?

    With just a cursory glance, I see some problems with your code. You set a property with setValue(), or setValueAtTime()not =. I couldn’t find where your variable number is defined.

    To answer your last question, use layer.moveToEnd() to move the layer to the bottom.

    Dan

  • Raphael Schaaf

    February 1, 2011 at 4:59 pm

    Thanks for the answer. My english isn\\\’t very good, but i try to explane it better.
    Basicaly i want to do a motion capturing effect and then export the data to use it in cinema 4d to animate a 3d model.
    I have 2 cameras, one in front and one left or right of my actor. The actor has markers all over his body.
    The markers get tracked in after effects and the tracking data is applied to nulls. At this point i have 2 nulls for every marker on the actor, one from the front and one from the side.
    Now i want to create a new null which moves 3dimensional, left/right from the track of the front video, forward/backward from the track of the side video and up/down from the average of the 2 tracking points.
    The first 2 layers in my comp are the videos. They get named front back left or right so the script knows from which sides they got filmed and orientates the nulls acordingly in 3d space.
    The next layers are the nulls that are tracked from the first video, after them those which are tracked from the second video.
    Now i select the 2 nulls i want to conect and run my script and hopefully it takes the positionkeyframes of the 2dimensional moving nulls and creates a new null that moves 3dimensional that is at the end of my layer list so it doesn\\\’t screw up the order of the other layers.

    I hope you know now what i want to achieve 🙂

  • Dan Ebberts

    February 1, 2011 at 6:01 pm

    I think the way I would tackle it would be to first develop expressions that dynamically position your nulls the way you want based on the tracking data. Once you have those working, I would change your script to just add the appropriate position expression after it creates the null.

    Dan

  • Raphael Schaaf

    February 1, 2011 at 7:12 pm

    it works with an expression, i already had this before i even started with the script:

    x = thisComp.layer(“Null 4”).position[0];
    value+[0,0,x*(-1)]
    ………………………………………………….

    i just wrote it into one of my nulls and the other null is “Null 4”.

    but i just don’t get it working in the script… and i don’t want to write this expression in every null, would be a boring solution 🙂
    with the script i learn at least something 🙂

  • Dan Ebberts

    February 1, 2011 at 7:50 pm

    Basically you would define your expressions in pieces like this:

    var expr1_1 = ‘x = thisComp.layer(“‘;
    var expr1_2 = ‘”).position[0];\rvalue+[0,0,x*(-1)]’

    Then, on the fly, you add in the name of the null that the new null is targeting to build the appropriate expression:

    var myExpr = expr1_1 + myTargetNull.name + expr1_2;

    Then apply the finished expression to the position property of your new null:

    myNewNull.property(“Position”).expression = myExpr;

    Something like that.

    Dan

  • Raphael Schaaf

    February 2, 2011 at 7:24 pm

    hmm… still doesn’t work

    i try to figure out which part of the script isn’t working, it has to be some little detail, because the 3 position “numbers” of the new null turn red, as if it had an expression, but the expression doesn’t get written in the null.
    and so it doesn’t animate and stays in the middle of the comp.
    also the little “stop-watch” next to the position of the new null isn’t activated, maybe it has something to do with that…?

    i’m trying to get it work…

    P.S. what does the “/rvalue” in the expr1_2 mean?

  • Dan Ebberts

    February 2, 2011 at 7:50 pm

    I can’t think of any way you can get the red numbers without an expression being active. The stopwatch shouldn’t look any different. If you select the layer and type ee , does the position property not open?

    The backslash r (not forward slash, as in your question) gets interpreted as a carriage return so that the rest of the expression is on a separate line.

    Dan

  • Raphael Schaaf

    February 3, 2011 at 7:54 pm

    Ok, i\’m an idiot and you\’re a genius 🙂

    It all works now there was realy just one little detail that ruined the whole thing. I created the new null before i created the final expression variable, and so the selected layer wasn\’t any longer the trackpoint, it was the new null.
    Because of that it followed itself and always stayed in the middle.
    But i don\’t know why the expression was empty yesterday, i didn\’t change the script and today it was written in…

    Anyway, thank you so much
    I will post the whole script if it is finished =D

  • Raphael Schaaf

    February 3, 2011 at 10:25 pm

    and it works =D

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Created by Raphael Schaaf 2011
    //Motion-Capturing Null-Conector

    {
    app.beginUndoGroup("Nullconector");

    thisComp = app.project.activeItem;

    var Name1 = String (thisComp.selectedLayers[0].name);
    var Name2 = String (thisComp.selectedLayers[1].name);
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //this part will compare the first 2 layers name and orientate the position of the new null correct
    front = new String ("front");
    back = new String ("back");
    right = new String ("right");
    left = new String ("left");

    var video1 = String (thisComp.layers[1].name);
    var video2 = String (thisComp.layers[2].name);

    if (video1 == front) {

    var expr1 = 'x = thisComp.layer("' + Name1 + '").position[0];';

    }else{
    if (video1 == back) {

    var expr1 = 'x = thisComp.layer("' + Name1 + '").position[0] * (-1);';

    }else{
    if (video1 == right) {

    var expr2 = 'z = thisComp.layer("' + Name1 + '").position[0];';

    }else{
    if (video1 == left) {

    var expr2 = 'z = thisComp.layer("' + Name1 + '").position[0] * (-1);';

    }else{

    alert ("choose compatible videoname")
    }
    }
    }
    }

    if (video2 == front) {

    var expr1 = 'x = thisComp.layer("' + Name2 + '").position[0];';

    }else{
    if (video2 == back) {

    var expr1 = 'x = thisComp.layer("' + Name2 + '").position[0] * (-1);';

    }else{
    if (video2 == right) {

    var expr2 = 'z = thisComp.layer("' + Name2 + '").position[0];';

    }else{
    if (video2 == left) {

    var expr2 = 'z = thisComp.layer("' + Name2 + '").position[0] * (-1);';

    }else{

    alert ("choose compatible videoname")
    }
    }
    }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //this part takes the average of the height of the 2 nulls to make the height of the new null more accurate
    var expr3 = 'y = (thisComp.layer("' + Name1 + '").position[1] + thisComp.layer("' + Name2 + '").position[1]) / 2;';
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //this part takes all the position information together
    var expr4 = 'value + [x,y,z];';
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //this part puts the single parts together to a single expression
    var expr = expr1 + expr2 + expr3 + expr4;
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //this part creates the new null, names it, makes it 3d, moves it to the end, and sets the initial position to 0
    var newTrackpoint = thisComp.layers.addNull();
    var trackName = 'Trackpoint';
    newTrackpoint.name = trackName;
    newTrackpoint.threeDLayer = true;
    newTrackpoint.moveToEnd();
    newTrackpoint.position.setValue ([0,0,0]);
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //this part writes the expression into the null
    newTrackpoint.position.expression = expr;

    app.endUndoGroup();
    }

  • Txus Da silva

    March 5, 2012 at 1:53 pm

    Hi mr schaaf.. im hoping u read this and answer me.

    I´m trying to achieve a similar thing like yours. My final destination is just a dot in 3D in after effects.

    Im quite a newby on expressions, never understood them never will…

    where do u place that expresion and where u point which layers are giving u which values???

    so
    if NULL1 has the front image tracking point for X – Y
    and
    NULL2 has the side image tracking point X – Y (but im gonna use X as the Z)
    then u create a new NULL with your script inside where???
    how o where you place this script?
    what you change to select each layer inside your marvellous script???
    is it:
    (thisComp.selectedLayers[0].name); where 0 is the name???
    or

    hope u answer me… cos my lack of understanding of scripting it has me doing each one by hand..

    thanx in advance

Page 1 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy