3
\$\begingroup\$

I made a scripted expression for after effects a while ago. This script is preventing objects to lay on odd pixels when a Keyframe is applied.

// Amount of Keyframes
n = transform.position.numKeys;

// Time
t = time;

// Time of Keyframe
timeOfKeyframe = null;

if ( n != 0 ){
     // Time of first Keyframe
     timeOfFirstFrame = transform.position.key(1).time;

     // Time of last Keyframe
     timeOfLastFrame = transform.position.key(n).time;
}
else{
     timeOfFirstFrame = null;
     timeOfLastFrame = null;
}

if ( n == 0 || t < timeOfFirstFrame || t > timeOfLastFrame ){
     posX = Math.round(transform.position[0]);
     posY = Math.round(transform.position[1]);
     posZ = Math.round(transform.position[2]);

     [posX,posY,posZ];
}
else{
     for(i=1; i <= n; i++){
          if ( t == transform.position.key(i).time ){
               timeOfKeyframe = transform.position.key(i).time;
          }
          else{
               value;
          }
     }

     if (t == timeOfKeyframe) {
          keyValue = transform.position.valueAtTime(timeOfKeyframe);
          roundedValue = [Math.round(keyValue[0]),Math.round(keyValue[1]),Math.round(keyValue[2])];
     }
     else{
          value;
     }
}

It's working for three dimensional objects. Since I am actually not a real programmer I wanted to know if there is any better way to achieve this. It seems to be a very long expression.

\$\endgroup\$
3
  • \$\begingroup\$ As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. For examples of good titles, check out Best of Code Review 2014 - Best Question Title Category You may also want to read How to get the best value out of Code Review - Asking Questions. \$\endgroup\$ Nov 16, 2015 at 11:06
  • \$\begingroup\$ I don't know how After Effects does things, but I can't see how this actually changes anything in the transform. You create an array of rounded values, but it's not returned or stored anywhere. It's just written there - and, I would imagine, ignored by the computer, since it's not doing anything. Also, at the very end you just have value;. I can't see value mentioned anywhere else, and again it's also just... there. Nothing's being set to value, and value is not being sent anywhere. So color me confused. \$\endgroup\$
    – Flambino
    Nov 16, 2015 at 20:53
  • \$\begingroup\$ @flambino an after effects expression is calculating a property's value at any frame. So on every single frame it is calculating the new value with the script above. When I write "value" it returns the actual value I entered in the user interface before. On the interface I can add multiple values and after effects calculates tweens between those values. When I write "[array];" The array will be returned. \$\endgroup\$ Nov 17, 2015 at 9:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.