在 AE 中播放 Sprite Sheet 图片的脚本

使用方法

  1. 将以下代码保存为:Sprite Sheet Animator.jsx
  2. 放到以下文件夹之一: ...\Adobe After Effects\Support Files\Scripts\ , 或带界面的版本:...\Scripts\ScriptUI Panels\
  3. 重新启动 AE, 在 AE 顶部菜单, File → Scripts → Sprite Sheet Animator.jsx Window → Sprite Sheet Animator(若放在 Panels 文件夹)
  4. 在 Project 面板里 选中你的 Sprite Sheet 图片, 输入 Columns, Rows, FPS , 点击 Create Animation, AE 会自动生成一个完整可播放的 Sprite 动画!
// Sprite Sheet Animator - 一键版
// Save as UTF-8 WITHOUT BOM

(function spriteSheetAnimator() {
    try {
        if (app.project === null) {
            alert("请先打开一个 AE 项目。");
            return;
        }

        var win = new Window("palette", "Sprite Sheet Animator", undefined);
        win.orientation = "column";
        win.alignChildren = "left";

        // 列数
        var grp1 = win.add("group");
        grp1.add("statictext", undefined, "Columns:");
        var colInput = grp1.add("edittext", undefined, "10");
        colInput.characters = 5;

        // 行数
        var grp2 = win.add("group");
        grp2.add("statictext", undefined, "Rows:");
        var rowInput = grp2.add("edittext", undefined, "3");
        rowInput.characters = 5;

        // FPS
        var grp3 = win.add("group");
        grp3.add("statictext", undefined, "FPS:");
        var fpsInput = grp3.add("edittext", undefined, "12");
        fpsInput.characters = 5;

        // 创建按钮
        var btn = win.add("button", undefined, "Create Animation");

        btn.onClick = function () {
            var cols = parseInt(colInput.text, 10);
            var rows = parseInt(rowInput.text, 10);
            var fps = parseInt(fpsInput.text, 10);

            if (isNaN(cols) || isNaN(rows) || cols < 1 || rows < 1) {
                alert("Columns / Rows 必须 >=1");
                return;
            }

            if (!(app.project.selection && app.project.selection.length > 0)) {
                alert("请在 Project 面板选择一张 Sprite Sheet 图片");
                return;
            }

            var item = app.project.selection[0];

            if (typeof FootageItem !== "undefined" && !(item instanceof FootageItem)) {
                alert("请选择图像素材(image footage)。");
                return;
            }

            if (typeof item.width === "undefined" || typeof item.height === "undefined") {
                alert("所选项目不是有效的位图素材。");
                return;
            }

            app.beginUndoGroup("Sprite Sheet Animation");

            // 计算单帧大小
            var frameWidth = Math.round(item.width / cols);
            var frameHeight = Math.round(item.height / rows);
            var totalFrames = cols * rows;
            var duration = Math.max(1, totalFrames / fps);

            // 创建合成
            var compName = item.name + "_SpriteAnim";
            var comp = app.project.items.addComp(compName, frameWidth, frameHeight, 1, duration, fps);

            // 添加图层
            var layer = comp.layers.add(item);

            // Scale 保持原比例
            layer.property("Scale").setValue([100, 100]);

            // Anchor Point 左上角
            layer.property("Anchor Point").setValue([0,0]);

            // Position 表达式(保证每帧显示正确)
            var expr =
                "cols = " + cols + ";\n" +
                "rows = " + rows + ";\n" +
                "fps = " + fps + ";\n" +
                "frameIndex = Math.floor(time * fps) % (cols * rows);\n" +
                "col = frameIndex % cols;\n" +
                "row = Math.floor(frameIndex / cols);\n" +
                "w = thisLayer.source.width / cols;\n" +
                "h = thisLayer.source.height / rows;\n" +
                "[-col * w, -row * h];";

            layer.property("Position").expression = expr;

            // 添加遮罩,只显示单帧
            var mask = layer.Masks.addProperty("ADBE Mask Atom");
            var shape = new Shape();
            shape.vertices = [[0,0],[frameWidth,0],[frameWidth,frameHeight],[0,frameHeight]];
            shape.inTangents = [[0,0],[0,0],[0,0],[0,0]];
            shape.outTangents = [[0,0],[0,0],[0,0],[0,0]];
            shape.closed = true;
            mask.maskShape = shape;
            mask.maskMode = "Add";

            app.endUndoGroup();

            alert("Sprite Animation 已创建:\n" + compName + "\n帧大小: " + frameWidth + "×" + frameHeight + "\n总帧数: " + totalFrames + "\nFPS: " + fps);
        };

        win.center();
        win.show();

    } catch (e) {
        alert("脚本执行出错:\n" + e.toString());
    }
})();

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值