Switching Parents


This little project demonstrates how you can use expressions to effectively switch an object's parent from one object to another.

Apply the following expression to position property of the child layer:


sw_time= 1.0 // time to switch parents

S2=this_comp.layer("pincer(l)");
S3=this_comp.layer("pincer(r)")

V1=position-S2.position.value_at_time(0) //initial position vector at time 0
L1=length(V1); //length of position vector
A1=Math.atan2(V1[1],V1[0]);  //initial angle of position vector

if(time <= sw_time){

//calculate rotation of S2 while parent
  R1=degrees_to_radians(S2.rotation-S2.rotation.value_at_time(0));
  A1b=R1+A1; //current angle of vector
//calculate positon while S2 is parent
  [L1*Math.cos(A1b),L1*Math.sin(A1b)]+S2.position;
}else{

  // first need to calculate final position due to previous parent
  R1b=S2.rotation.value_at_time(sw_time)-S2.rotation.value_at_time(0);
  A1c=degrees_to_radians(R1b)+A1;
  P1=[L1*Math.cos(A1c),L1*Math.sin(A1c)]+S2.position.value_at_time(sw_time);

  V2=P1-S3.position.value_at_time(sw_time); //position vector at sw_time
  L2=length(V2); //length of position vector
  A2=Math.atan2(V2[1],V2[0]); //angle of position vector at sw_time
//calculate rotation of S3 while parent
  R2=degrees_to_radians(S3.rotation-S3.rotation.value_at_time(0)); 
  A2b=R2+A2; //current angle of vector
//calculate position while S3 is parent
  [L2*Math.cos(A2b),L2*Math.sin(A2b)]+S3.position;
}

Add the following expression to the rotation property of the child layer:


sw_time= 1.0 // time to switch parents

S2=this_comp.layer("pincer(l)"); 
S3=this_comp.layer("pincer(r)");

if(time <= sw_time){
  rotation + S2.rotation - S2.rotation.value_at_time(0);
}else{
  R2=rotation + S2.rotation.value_at_time(sw_time) - S2.rotation.value_at_time(0);
  R2 + S3.rotation - S3.rotation.value_at_time(sw_time);
}

Make the following modifications to both expressions:

Set "sw_time" to the time where you want the object to switch from one parent to the other.

Set S2 to the layer that will be the first parent.

Set S3 to the layer that will be the second parent.

Note that you don't actually assign a real parent to your child layer - it's all done in the expression.

This may be an expression that's not really worth the trouble, but I think it demonstrates an intersting concept.