-
Notifications
You must be signed in to change notification settings - Fork 56
/
upscaleComp.jsx
119 lines (94 loc) · 3.6 KB
/
upscaleComp.jsx
1
/*This script is used to rescale composition with all its contentspreserving keyframes etc Also preserves its scale in parent compNik Ska, 2014CC-BY-SA */#script "Rescale precomp";var ddRescale = this;ddRescale.buildGUI = function(thisObj){ thisObj.w = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Rescale precomp", undefined, {resizeable:true}); thisObj.w.alignChildren = ['left', 'top'] var g = thisObj.w.add("group{orientation:'column', alignChildren: ['left', 'top']}"); g.add("staticText", undefined, "New scale"); var scaleLine = g.add("group{orientation:'row', alignChildren: ['left', 'top']}"); var scaleText = scaleLine.add("editText", undefined, "0"); scaleText.size = [60, 30]; var goBttn = scaleLine.add("button", undefined, "Go!"); goBttn.size = [30,30]; scaleText.text = 100; scaleText.onChanging = function(){ preFilter = /[+-]?([0-9\:]*)/; //this one does not allow to input anything but numbers and some symbols; this.text = this.text.match(preFilter)[0]; } scaleText.onEnterKey = function(){ thisObj.run(Number(scaleText.text)); } goBttn.onClick = function(){ thisObj.run(Number(scaleText.text)); } if (thisObj.w instanceof Window){ thisObj.w.center(); thisObj.w.show(); } else thisObj.w.layout.layout(true);}ddRescale.rescale = function(comp, scaleFactor){ /* Rescales the comp completely with all its contents Uses a temporary null object for that */ var moveVector = [comp.width, comp.height]*(scaleFactor-1)/2; comp.width *= scaleFactor; comp.height *= scaleFactor; //adding null for auto-rescale var cNull = comp.layers.addNull(); cNull.moveToBeginning(); //moving it to comp center cNull.transform.position.setValue([comp.width/2, comp.height/2]); for(var l = 2; l <= comp.layers.length; l++){ //now parent each parentless layer to it var layer = comp.layers[l]; if(!layer.hasParent) layer.parent = cNull; if(layer.canSetCollapseTransformation) layer.collapseTransformation = true; //now move if(layer.transform.position.isTimeVarying == false) layer.transform.position.setValue(layer.transform.position.value + moveVector); else{ //if we have keyframes var pos = layer.transform.position; for(var k = 1; k <= pos.numKeys; k++){ pos.setValueAtKey(k, pos.keyValue(k) + moveVector) } } if(layer.source instanceof CompItem){ ddRescale.rescale(layer.source, scaleFactor) } } cNull.transform.scale.setValue([100,100,100]*scaleFactor); cNull.remove(); }ddRescale.run = function(newScale){ var activeComp = app.project.activeItem; if(activeComp && activeComp instanceof CompItem){ app.beginUndoGroup("Upscaling comp"); var sel = activeComp.selectedLayers; var factor = 100; if(!isNaN(newScale)) factor = newScale/100; if(sel.length > 0){ for(var s = 0; s < sel.length; s++){ if(sel[s].source instanceof CompItem){ //scale precomp contents up this.rescale(sel[s].source, factor); //and precomp itself down sel[s].transform.scale.setValue((1/factor)*sel[s].transform.scale.value); } } } else{ this.rescale(activeComp, factor) } app.endUndoGroup(); }}ddRescale.buildGUI(ddRescale);