Activity › Forums › Adobe After Effects Expressions › Scale Selected Layers Script
-
Scale Selected Layers Script
Posted by Jon Smith on April 18, 2013 at 4:59 pmHey All,
After Effects comes with a script called “Scale Selected Layers”. It basically creates a null either in the center or upper left of the composition then parents the layers and scales it by a user inputed value. I’m looking for something that just scales the layers without parenting them to a null. I figured I could customize the script to do what I need but after trying a few things I can’t seem to pull it off. Could someone point me in the right direction. Thanks!
Jon Smith replied 13 years ago 1 Member · 1 Reply -
1 Reply
-
Jon Smith
April 18, 2013 at 9:44 pmI finally got it to work. If anyone is interested here’s the script. It’s a modified version of the “Scale Selected Layers” Script and is handy if you need to scale a bunch of layers by a certain percentage. You can just save the script out as a .jsx file and it should work.
{// Scale Selected Layers Individually.jsx
// This is a modified script based on :
// "Scale Selected Layers.jsx" that comes with After Effects
//
// This script scales the selected layers within the active comp.
//
// First, it prompts the user for a scale_factor.
// Next, it scales all selected layers, individuallyfunction ScaleSelectedLayers(thisObj)
{
var scriptName = "Scale Selected Layers";// This variable stores the scale_factor.
var scale_factor = 1.0;//
// This function is called when the user enters text for the scale.
//
function on_textInput_changed()
{
// Set the scale_factor based on the text.
var value = this.text;
if (isNaN(value)) {
alert(value + " is not a number. Please enter a number.", scriptName);
} else {
scale_factor = value;
}
}function onScaleClick()
{
var activeItem = app.project.activeItem;
if ((activeItem == null) || !(activeItem instanceof CompItem)) {
alert("Please select or open a composition first.", scriptName);
} else {
var selectedLayers = activeItem.selectedLayers;
if (activeItem.selectedLayers.length == 0) {
alert("Please select at least one layer in the active comp first.", scriptName);
} else {
// Validate the input field, in case the user didn't defocus it first (which often can be the case).
this.parent.parent.optsRow.text_input.notify("onChange");var activeComp = activeItem;
// By bracketing the operations with begin/end undo group, we can
// undo the whole script with one undo operation.
app.beginUndoGroup(scriptName);// Scales the layers.
var myLayers = activeItem.selectedLayers;
for (i=0; i <= myLayers.length-1; i++){currentLayer = myLayers[i];
var layerScale = currentLayer.scale.value;
layerScale[0] = layerScale[0] * scale_factor;
layerScale[1] = layerScale[1] * scale_factor;
currentLayer.scale.setValue(layerScale);}
app.endUndoGroup();
// Reset scale_factor to 1.0 for next use.
scale_factor = 1.0;
this.parent.parent.optsRow.text_input.text = "1.0";
}
}
}//
// This function puts up a modal dialog asking for a scale_factor.
// Once the user enters a value, the dialog closes, and the script scales the comp.
//
function BuildAndShowUI(thisObj)
{
// Create and show a floating palette.
var my_palette = (thisObj instanceof Panel) ? thisObj : new Window("palette", scriptName, undefined, {resizeable:true});
if (my_palette != null)
{
var res =
"group {
orientation:'column', alignment:['fill','top'], alignChildren:['left','top'], spacing:5, margins:[0,0,0,0],
optsRow: Group {
orientation:'column', alignment:['fill','top'],
text_input: EditText { text:'1.0', alignment:['center','top'], preferredSize:[200,20] },
},
cmds: Group {
alignment:['fill','top'],
okButton: Button { text:'Scale', alignment:['fill','center'] },
},
}";my_palette.margins = [10,10,10,10];
my_palette.grp = my_palette.add(res);// Workaround to ensure the edittext text color is black, even at darker UI brightness levels.
var winGfx = my_palette.graphics;
var darkColorBrush = winGfx.newPen(winGfx.BrushType.SOLID_COLOR, [0,0,0], 1);
my_palette.grp.optsRow.text_input.graphics.foregroundColor = darkColorBrush;// Set the callback. When the user enters text, this will be called.
my_palette.grp.optsRow.text_input.onChange = on_textInput_changed;my_palette.grp.cmds.okButton.onClick = onScaleClick;
my_palette.onResizing = my_palette.onResize = function () {this.layout.resize();}
}return my_palette;
}//
// The main script.
//
if (parseFloat(app.version) < 8) {
alert("This script requires After Effects CS3 or later.", scriptName);
return;
}var my_palette = BuildAndShowUI(thisObj);
if (my_palette != null) {
if (my_palette instanceof Window) {
my_palette.center();
my_palette.show();
} else {
my_palette.layout.layout(true);
my_palette.layout.resize();
}
} else {
alert("Could not open the user interface.", scriptName);
}
}ScaleSelectedLayers(this);
}
Reply to this Discussion! Login or Sign Up