
Copy link to clipboard
Copied
What would be an easy way to programmatically replace a rectangle (either a solid or a shape) with an image? I'm assuming that the image has the same aspect ratio as the initial rectangle. Thanks!
try this (where comp.layer(1) is a solid with properties, keyframed or not, that you want to apply to the image -- I didn't test this with a shape layer):
var proj = app.project;
var comp;
for (var i = 1; i <= proj.numItems; i++){
if (proj.item(i) instanceof CompItem){
comp = proj.item(i);
}
}
var shape1 = comp.layer(1);
var importOpts = new ImportOptions(new File("/yourDirectory/yourImage.png"));
var importImg = proj.importFile(importOpts);
shape1.replaceSource(importImg,false);
Copy link to clipboard
Copied
A copy of the image file appears on the page, with the same resolution as the original file.
Copy link to clipboard
Copied
This example works for a local file and gives you the idea of how to duplicate properties and then remove the "replaced" layer.
var proj = app.project;
var comp = proj.item(1);
var shape1 = comp.layer(1);
var importOpts = new ImportOptions(new File("/yourDirectoryName/yourImageFile.png"));
var importImg = app.project.importFile(importOpts);
var myImg = comp.layers.add(importImg);
myImg.inPoint = shape1.inPoint;
myImg.outPoint = shape1.outPoint;
myImg.property("Position").setValue(shape1.property("Position").value);
myImg.property("Scale").setValue(shape1.property("Scale").value);
shape1.remove();
Copy link to clipboard
Copied
Thanks tardigrade01. I'm sure this approach will work. Of course, we will need to take into account other properties, such as shape rotation and scale in addition to layer properties. I was wondering if ExtendScript had a simple method encapsulating all those tedious calculations
Copy link to clipboard
Copied
try this (where comp.layer(1) is a solid with properties, keyframed or not, that you want to apply to the image -- I didn't test this with a shape layer):
var proj = app.project;
var comp;
for (var i = 1; i <= proj.numItems; i++){
if (proj.item(i) instanceof CompItem){
comp = proj.item(i);
}
}
var shape1 = comp.layer(1);
var importOpts = new ImportOptions(new File("/yourDirectory/yourImage.png"));
var importImg = proj.importFile(importOpts);
shape1.replaceSource(importImg,false);
Copy link to clipboard
Copied
A solid works perfectly for this. Thanks!!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now