Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

can you create an animation for after effect using a script. the animation should depict…

  1. User

    can you create an animation for after effect using a script. the animation should depict a flat vector city

  2. Assistant

    Creating a flat vector city animation in Adobe After Effects can be streamlined using ExtendScript (Adobe's version of JavaScript for scripting in Creative Suite applications). Below is a step-by-step guide and a sample script to help you create a simple flat vector city animation. This script will:

    1. Set Up the Composition
    2. Create Building Shapes
    3. Animate Buildings (e.g., Scale or Position)
    4. Add Simple Camera Movement

    Prerequisites

    • Adobe After Effects installed on your computer.
    • Basic understanding of After Effects interface and scripting.

    Step 1: Setting Up the Composition

    First, we'll create a new composition where our city animation will reside.

    // Create a new Composition
    var compWidth = 1920;
    var compHeight = 1080;
    var compDuration = 10; // seconds
    var compFrameRate = 30;
    
    var myComp = app.project.items.addComp("Flat Vector City", compWidth, compHeight, 1, compDuration, compFrameRate);
    

    Step 2: Create Building Shapes

    We'll define a function to create buildings of different heights and colors. For simplicity, buildings will be rectangles aligned at the bottom of the composition.

    // Function to create a building
    function createBuilding(comp, width, height, xPos, color) {
        var shapeLayer = comp.layers.addShape();
        shapeLayer.name = "Building_" + xPos;
        
        var rectGroup = shapeLayer.property("Contents").addProperty("ADBE Vector Group");
        var rectPath = rectGroup.property("Contents").addProperty("ADBE Vector Shape - Rect");
        rectPath.property("Size").setValue([width, height]);
        rectPath.property("Position").setValue([0, 0]);
        
        var fill = rectGroup.property("Contents").addProperty("ADBE Vector Graphic - Fill");
        fill.property("Color").setValue(color);
        
        // Position the building
        shapeLayer.property("Transform").property("Position").setValue([xPos, compHeight/2 - height/2]);
        
        return shapeLayer;
    }
    
    // Define building parameters
    var buildingWidth = 100;
    var buildingSpacing = 150;
    var numberOfBuildings = Math.floor(compWidth / buildingSpacing);
    var baseHeight = 300;
    var colors = [
        [0.8, 0.1, 0.1],
        [0.1, 0.8, 0.1],
        [0.1, 0.1, 0.8],
        [0.8, 0.8, 0.1],
        [0.8, 0.1, 0.8]
    ];
    
    // Create multiple buildings
    for (var i = 0; i < numberOfBuildings; i++) {
        var xPos = -compWidth / 2 + i * buildingSpacing + buildingSpacing / 2;
        var heightVariation = baseHeight + Math.random() * 200; // Random height between baseHeight and baseHeight + 200
        var color = colors[Math.floor(Math.random() * colors.length)];
        createBuilding(myComp, buildingWidth, heightVariation, xPos, color);
    }
    

    Step 3: Animate Buildings

    Let's add a simple scaling animation to simulate buildings growing into place.

    // Animate each building's scale
    for (var i = 1; i <= myComp.layers.length; i++) {
        var layer = myComp.layers[i];
        
        // Set initial scale to 0%
        layer.property("Transform").property("Scale").setValue([0, 0]);
        
        // Create a scale animation
        var scaleProp = layer.property("Transform").property("Scale");
        scaleProp.setValueAtTime(0, [0, 0]);
        scaleProp.setValueAtTime(1, [100, 100]);
        
        // Ease the keyframes
        var easeIn = new KeyframeEase(0.7, 50);
        var easeOut = new KeyframeEase(0.7, 50);
        scaleProp.setTemporalEaseAtKey(2, [easeOut], [easeIn]);
    }
    

    Step 4: Add Camera Movement

    Adding a camera can give a dynamic feel to the animation. We'll add a simple pan across the city.

    // Add a camera
    var cameraLayer = myComp.layers.addCamera("Camera 1", [compWidth / 2, compHeight / 2]);
    cameraLayer.transform.position.setValue([compWidth / 2, compHeight / 2, -1000]);
    
    // Animate camera position to pan right over the duration
    var camPos = cameraLayer.transform.position;
    camPos.setValueAtTime(0, [compWidth / 2, compHeight / 2, -1000]);
    camPos.setValueAtTime(compDuration, [compWidth / 2 + 500, compHeight / 2, -1000]);
    
    // Optional: Add slight y-axis movement for a more dynamic effect
    camPos.setTemporalEaseAtKey(2, [new KeyframeEase(0.7, 50)], [new KeyframeEase(0.7, 50)]);
    

    Running the Script

    1. Open After Effects and create a new project or use an existing one.
    2. Access the ExtendScript Toolkit:
      • Go to File > Scripts > Open Script Editor in After Effects.
    3. Copy and Paste the Script:
      • Paste the entire script provided above into the editor.
    4. Run the Script:
      • Press the "Run" button or press Ctrl + Enter (Windows) / Cmd + Enter (Mac).
    5. Review the Composition:
      • After running the script, a new composition named "Flat Vector City" will appear in your Project panel.
      • Open the composition to see the vector city with animated buildings and camera movement.

    Customization Tips

    • Building Styles: Enhance buildings by adding windows or more intricate vector shapes.
    • Colors: Adjust the colors array to match your desired palette.
    • Animations: Experiment with different animations such as moving buildings, changing colors, or adding additional effects like lights.
    • Camera Effects: Add zooming, rotation, or multiple camera angles for a more cinematic look.

    Conclusion

    This script provides a foundational framework for creating a flat vector city animation in After Effects. You can expand upon this by adding more complex shapes, textures, lighting effects, and intricate animations to create a more detailed and engaging cityscape. Scripting in After Effects can significantly speed up your workflow, especially for repetitive tasks or complex animations.

    Feel free to modify and enhance the script to better fit your creative vision!