After Effects Expression 3d To 2d Position

6 min read Aug 31, 2024
After Effects Expression 3d To 2d Position

After Effects Expression: 3D to 2D Position

This article will guide you on how to use After Effects expressions to convert 3D position data to 2D coordinates. This is a handy technique for various animation scenarios, particularly when working with 3D cameras and objects in your After Effects compositions.

Understanding the Concept

In After Effects, 3D objects have an X, Y, and Z position in space. However, your composition's viewport is essentially a 2D plane. To project a 3D object onto this 2D plane, you need to translate its 3D coordinates into 2D coordinates.

The Expression

This expression leverages the thisComp property to access the composition's properties, including the camera's position and target. It then calculates the 2D position based on the 3D object's position relative to the camera:

// This assumes your object is named "My 3D Object"
var obj = thisComp.layer("My 3D Object");
var cam = thisComp.activeCamera;

// Calculate the vector pointing from the camera to the object
var vec = obj.position - cam.position;

// Project the vector onto the camera's plane
var projX = vec.dot(cam.toWorldVec([1, 0, 0]));
var projY = vec.dot(cam.toWorldVec([0, 1, 0]));

// Adjust the projection based on the composition's resolution
projX = projX * thisComp.width / (2 * cam.zoom);
projY = projY * thisComp.height / (2 * cam.zoom);

// Return the 2D position
[projX, projY]

Breaking Down the Expression

  1. Target Object: The expression begins by defining a variable obj and assigning it the layer with the name "My 3D Object". Make sure to replace this with the actual name of your 3D object.

  2. Camera: The cam variable represents the active camera in your composition.

  3. Vector Calculation: This part calculates the vector pointing from the camera to the object. This is achieved by subtracting the camera's position from the object's position.

  4. Projection: The dot function is used to project the vector onto the camera's plane. The toWorldVec method is used to get the camera's world-space vectors for the X and Y axes.

  5. Adjustment: The projection is adjusted based on the composition's resolution and the camera's zoom level to ensure accurate placement.

  6. Return: Finally, the expression returns an array containing the calculated X and Y positions.

Application

  1. Create a Null Object: In your composition, create a Null object. This will be the target of the expression.

  2. Paste the Expression: Right-click on the Null object's position property and select "Expression." Paste the provided code into the expression window.

  3. Adjust the Expression: Replace "My 3D Object" with the actual name of your 3D object.

  4. Link the Null Object: Use the Null object to control the position of your 2D elements. For example, you can link the position of a text layer or shape to the Null object's position.

Variations and Considerations

  • Camera Alignment: This expression assumes your camera's orientation is aligned with the X and Y axes. If your camera is rotated, you may need to adjust the expression accordingly.

  • Depth of Field: The expression doesn't take into account the camera's depth of field. For realistic results, you might need to adjust the size of your 2D element based on its distance from the camera.

  • Multiple Objects: You can modify this expression to handle multiple 3D objects. Simply iterate over the objects and calculate their respective 2D positions.

Conclusion

Using After Effects expressions to convert 3D position data to 2D coordinates is a versatile technique. It allows you to create dynamic and visually interesting effects, enabling you to achieve a wider range of creative possibilities with your 3D animations. Remember to adjust the expression to match your specific needs and project requirements.

Featured Posts