ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Find and Replace the item from bin to comp.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nathaniel Logan
Find and Replace the item from bin to comp.
on Feb 26, 2020 at 5:57:47 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 &lt;= 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 &lt; 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();


Return to posts index

Dan Ebberts
Re: Find and Replace the item from bin to comp.
on Feb 27, 2020 at 12:00: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



Return to posts index

Nathaniel Logan
Re: Find and Replace the item from bin to comp.
on Feb 27, 2020 at 12:08:09 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.


Return to posts index


Dan Ebberts
Re: Find and Replace the item from bin to comp.
on Feb 27, 2020 at 12:16:28 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



Return to posts index

Nathaniel Logan
Re: Find and Replace the item from bin to comp.
on Feb 27, 2020 at 12:29:37 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();


Return to posts index

Dan Ebberts
Re: Find and Replace the item from bin to comp.
on Feb 27, 2020 at 12:37:09 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



Return to posts index


Nathaniel Logan
Re: Find and Replace the item from bin to comp.
on Feb 27, 2020 at 1:03:24 am

Hi Dan,

Thank you so much, works like charm. ☺

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


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]