Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects layer.applyPreset does not work when executed by a ScriptUI button

  • layer.applyPreset does not work when executed by a ScriptUI button

    Posted by Sacha Crispin on April 4, 2023 at 5:05 pm

    Hey all! I’m facing a really weird issue and can’t fix it on my own.

    I wrote (copied snippets from here and there) a ScriptUI Panel for AE that creates a rectangle shape layer of the size of the comp, adds it fill, and applies an external .ffx (Animation Preset) to it. And that works fine (lines 36 to 74 on their own).

    But when I try to make it run from the press of a button (lines 1 to 33), the script runs fine until line 53, then stops.
    That line is an .applyPreset and I have no clue why it works fine on its own, but not when it’s activated by the button. The referenced .ffx is in its correct place (same folder as the ScriptUI Panel).

    Would appreciate it immensely if someone could help me out with this! Code for the .jsx under.

    function createDockableUI(thisObj) {

    var dialog =

    thisObj instanceof Panel

    ? thisObj

    : new Window("dialog", undefined, undefined, { resizeable: true });

    dialog.onResizing = dialog.onResize = function() {

    this.layout.resize();

    };

    return dialog;

    }

    function showWindow(myWindow) {

    if (myWindow instanceof Window) {

    myWindow.center();

    myWindow.show();

    }

    if (myWindow instanceof Panel) {

    myWindow.layout.layout(true);

    myWindow.layout.resize();

    }

    }

    var win = createDockableUI(this);

    var PixelDisruptor = createDockableUI(this);

    var ApplyPXD = PixelDisruptor.add("button", undefined, undefined, {name: "ApplyPXD"});

    ApplyPXD.text = "Disrupt your pixels";

    ApplyPXD.onClick = function () {

    PXD()

    }

    showWindow(win);

    function PXD() {

    var currentComp = app.project.activeItem;

    app.beginUndoGroup("Pixel Disruptor layers creation");

    //Create Map layer

    var mapLayer = currentComp.layers.addShape();

    var mapGroup = mapLayer.property("Contents").addProperty("ADBE Vector Group");

    mapLayer.name = "PXD MAP";

    mapLayer.label = 0;

    // Map shape

    mapGroup.property("Contents").addProperty("ADBE Vector Shape - Rect");

    mapGroup.property("Contents").property("Rectangle Path 1").property("Size").expression = "var width = thisComp.width;\rvar height = thisComp.height;\r[width, height]";

    // Map color

    mapGroup.property("Contents").addProperty("ADBE Vector Graphic - Fill");

    // Apply the effects from the .ffx file to the map layer

    mapLayer.applyPreset(File("./PXDMAP.ffx"));

    app.endUndoGroup();

    }

    Sacha Crispin replied 3 years, 1 month ago 1 Member · 1 Reply
  • 1 Reply
  • Sacha Crispin

    April 5, 2023 at 3:59 pm

    Someone helped me on another place, so found it!
    Big thanks to Adam Plouff (founder of battleaxe.co, answered me on his Discord server).

    In his words “The script doesn’t understand relative path notation. So you want to get the path of the script, go up a folder then to the ffx file”

    Here’s the working code snippet:

    // Apply the effects from the .ffx file to the map layer
    var filePath = File(File($.fileName).parent.fullName + "/PXDMAP.ffx")
    if (filePath.exists) {
        mapLayer.applyPreset(filePath);
    } else {
        alert('PXDMAP.ffx is missing')
    }

    This also displays an error message if the file isn’t found.

    If anyone faces this problem, just did research and to go back folders (ie. this script is in the ScriptUI Panels folder and I want to reference a script from the Presets folder, located two folders above), you do

    ./../../Presets/PXDMAP.ffx

    Instead of

    /PXDMAP.ffx

    Hopefully this helps someone else too in the future!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy