Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Find and Replace the item from bin to comp.

  • Find and Replace the item from bin to comp.

    Posted by Nathaniel Logan on February 26, 2020 at 5:57 pm

    Hello,

    I’m trying to replaceSource of a layer from the project bin/root to the Active Comp of particular Layer, also the position of the replaced layer has to match the position of the exisiting layer.
    I did happen to come up with a script that happened to be having lots of errors.

    Can you guys help me with this to figure out to achieve this, please…..

    Attaching a screenshot reference

    function getItem(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i)) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    function replaceFile(){
    var myComp = app.project.activeItem;
    var myItems = myItem;
    var myLayers = myComp.layer('blue.png');
    myItem.layer('blue.png').property("Position").setValue([positionValues[0],positionValues[1],positionValues[2]]);
    if (myLayers.length == myItems.length){
    for (var i = 0; i < myLayers.length; i++){
    myLayers[i].replaceSource(myItems[i],false);
    }
    }else{
    alert("Number of selected layers and bin items does't match.");
    }

    var itemName = "pink.png";
    var myItem = getItem(itemName);
    // alert(itemName + (myComp == null ? " not " : " ") + "found.");
    replaceFile();

    Nathaniel Logan replied 6 years, 5 months ago 2 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    February 27, 2020 at 12:00 am

    Try this:


    function getItem(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i)) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    function replaceFile(){
    var myComp = app.project.activeItem;
    if ((myComp == null) || ! (myComp instanceof CompItem)){
    alert("No comp active.");
    return;
    }
    var myLayer = myComp.layer('blue.png');
    var itemName = "pink.png";
    var myItem = getItem(itemName);
    if (myItem == null){
    alert("Can't find item '" + itemName + "'.");
    return;
    }
    myLayer.replaceSource(myItem,false);
    }
    replaceFile();

    Dan

  • Nathaniel Logan

    February 27, 2020 at 12:08 am

    Hi Dan,

    Thank you so much, you’re the best ☺
    Exactly what I was looking for.

    Just one thing what if my comp is not active but I know my comp name how do I specify my comp name =|

    Thank you in advance.

  • Dan Ebberts

    February 27, 2020 at 12:16 am

    You could add a function to find a comp, like this (not tested):


    function findComp(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    If it returns null, the comp was not found.

    Dan

  • Nathaniel Logan

    February 27, 2020 at 12:29 am

    Hi Dan,

    Sorry to bother, I tried to edit the script to find the comp, but im getting an error.

    line 28. Function compName.layer is undefined.

    Please check the code that I modified.

    Pls help 🙁

    function getItem(theName){
    for (var i =1; i &lt;= app.project.numItems; i++){
    if ((app.project.item(i)) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    function findComp(theName){
    for (var i =1; i &lt;= app.project.numItems; i++){
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    function replaceFile(){

    var setCompName = findComp(compName);
    var compName = "mycomp"
    var myLayer = compName.layer('pink.png');
    var itemName = "blue.png";
    var myItem = getItem(itemName);
    if (myItem == null){
    alert("Can't find item '" + itemName + "'.");
    return;
    }
    myLayer.replaceSource(myItem,false);
    }
    replaceFile();

  • Dan Ebberts

    February 27, 2020 at 12:37 am

    try it like this:


    function getItem(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i)) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    function findComp(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    function replaceFile(){

    var compName = "mycomp";
    var myComp = findComp(compName);
    if (myComp == null){
    alert("Can't find comp '" + compName + "'.");
    return;
    }
    var myLayer = myComp.layer('pink.png');
    var itemName = "blue.png";
    var myItem = getItem(itemName);
    if (myItem == null){
    alert("Can't find item '" + itemName + "'.");
    return;
    }
    myLayer.replaceSource(myItem,false);
    }
    replaceFile();

    Also, if you post code in this forum, please don’t preview your post. It messes things up.

    Dan

  • Nathaniel Logan

    February 27, 2020 at 1:03 am

    Hi Dan,

    Thank you so much, works like charm. ☺

    I see, the preview messed up the for loop. I will avoid that. 🙂

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