How To Maintain Stroke Width In After Effects - Expression
What It Does
Now, if you've played around with shape layers, you might have noticed that when you scale them up or down, the thickness of their outline, known as the stroke, changes too. This can be a bit of a nuisance, especially when you want that stroke to remain consistent. That's where our Maintain Stroke Width expression comes into play.
Imagine you've drawn a simple circle with a 5-pixel wide stroke. As you scale the circle to make it bigger, that 5-pixel stroke might become 10 pixels wide, altering its appearance. The "Maintain Stroke Width" expression cleverly adjusts the stroke width based on how much you're scaling the layer. It's like having an invisible assistant who ensures that no matter how much you resize your shape, the stroke remains visually the same.
Basic Expression
value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001;
Apply this expression to the "Stroke Width" property of a shape layer to maintain the visual thickness of the stroke, regardless of the scaling applied to the layer.
Alternative Method (Using Average Scale Values)
sv = transform.scale;
avScale = (sv[0] + sv[1])/2;
sFactor = avScale * .01;
value / sFactor;
This method averages the X and Y scale values and adjusts the stroke width based on the average scale factor.
Maintaining Stroke with Parented Layer
strokeSize = DESIRED_VALUE; // Replace DESIRED_VALUE with your desired stroke width
try {
strokeSize / Math.abs(thisLayer.parent.transform.scale[0]/100);
} catch {
strokeSize / Math.abs(transform.scale[0]/100);
}
This expression tries to use the parented layer for scale. If there's no parented layer, it uses the current layer's scale instead.
How To Use It
- Locate the Shape Layer: In your After Effects timeline, find the shape layer you're working with. If you've named your layers (which is always a good practice!), it'll be easier to spot.
- Access the Stroke Width Property: Expand the shape layer by clicking on the small triangle next to its name. Dive deeper by expanding the "Contents" section, then the specific shape (like "Ellipse 1" if it's a circle), and finally, you'll find the "Stroke 1" section. Here, you'll see the "Stroke Width" property.
- Activate Expressions: Alt-click (or Option-click on Mac) on the stopwatch icon next to "Stroke Width." This will open up a space for you to type in or paste an expression.
- Enter the Expression: In the space provided, type in the "Maintain Stroke Width" expression. Once you've done this, any changes you make to the layer's scale will automatically adjust the stroke width to maintain its appearance.
A Word on Scaling with Different X and Y Values
Now, a quick heads-up. If you've tried scaling your shape differently in width (X) and height (Y), you might find the stroke behaving a bit unpredictably. This is because the expression has to figure out how to adjust the stroke when the shape is being stretched more in one direction than the other. A simple workaround is to average the X and Y scale values and use that average to adjust the stroke width. It's a bit of math magic that ensures your stroke looks just right.
Alternative Approach: Using Transform Shape
If you're looking for another method, here's a pro tip. Instead of scaling the entire layer, you can adjust the scale within the "Transform Shape" property of the shape layer. This way, the stroke remains below the transformation, ensuring it doesn't get affected by the scaling. To do this:
- Find the Transform Shape Property: Inside your shape layer, expand the "Contents" section. Beneath your specific shape (like "Ellipse 1"), you'll find the "Transform: Shape Name" option.
- Adjust the Scale: Here, instead of scaling the entire layer, adjust the scale within this property. You'll notice the stroke remains consistent, giving you the desired effect.
Top Tips & Techniques To Ensure Consistent Stroke Width
1. Using the “Constant Stroke Width” Option
When working with shape layers:
- Select your shape layer in the timeline.
- Open the Transform options under the shape group (not the layer’s Transform properties).
- Ensure that you scale the shape using the Contents > Transform > Scale rather than the layer’s Transform properties.
- The stroke width remains constant because it scales only the path and not the stroke.
2. Adding a Layer Style
You can use the Stroke layer style instead of the shape layer's stroke:
- Right-click the layer in the timeline and go to Layer Styles > Stroke.
- Adjust the stroke width in the layer styles settings.
- Scale the layer freely; the stroke width remains unaffected by the scaling.
3. Using Expressions to Lock Stroke Width
If you want the stroke width to remain constant when scaling the layer, you can add an expression:
- Select the Stroke Width property of the shape layer.
- Alt-click (Option-click on Mac) the stopwatch icon to add an expression.
- Use this expression:
- javascript
- Copy code
value / thisLayer.transform.scale[0] * 100
- This divides the stroke width by the layer’s scaling factor, ensuring a consistent appearance.
4. Pre-Composing and Scaling
If you need to animate scale but keep stroke widths constant:
- Pre-compose the shape layer (Layer > Pre-compose) while moving all attributes into the new composition.
- Scale the pre-comp instead of the shape layer. Since the shape is not directly scaled, the stroke width remains constant.
5. Using the “Shape Stroke Width” Effect
You can use plugins or scripts, such as Motion Tools, to maintain consistent stroke width during scaling. These tools provide automation for managing scaling issues.
6. Rasterizing the Shape
If you don’t need the vector qualities:
- Convert your shape to a rasterized image (e.g., by exporting it as PNG or using Render Queue).
- Re-import the image and scale it freely, as stroke width is baked into the rasterized image.
Practical Examples:
Example 1: Scaling a Circle with Consistent Stroke
- Draw a circle using the ellipse tool with a stroke.
- Animate the scaling using Contents > Ellipse 1 > Transform > Scale. The stroke width remains constant.
Example 2: Consistent Stroke on Animated Lines
- Create a line using the pen tool and add a stroke.
- Add a Trim Paths animation.
- Use an expression on the stroke width to ensure consistency during any scale changes:
value / thisLayer.transform.scale[1] * 100