My first AE Script.Plz review and help with calc and notepad

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
manjunath7472
Posts: 7
Joined: June 1st, 2015, 8:42 am

Plz review and also help me with calculator and notepad buttons.After effects freezes until i close opened item(calc or notepad).


//Creates a parented 2D or 3D null in the average positions of selected 2D or 3D layers.
// If u have small 2d or 3d layers scattered in one corner of comp, select those layers and click "Create Null".

//_____________________________________buildUI
function buildUI(thisObj) {
var myPanel = (thisObj instanceof Panel) ? thisObj:new Window('palette', 'ParenNull',undefined, {resizable: true});

myPanel.nullButton= myPanel.add('button', [0,0,100,30], 'Create Null'); //Create Null
myPanel.nullButton.onClick = function(){


mySelectedLayers=app.project.activeItem.selectedLayers;

var layerPos= new Array();
var totalPos=0;
var avgPosX
var avgPosY
var avgPosZ

app.beginUndoGroup("delete all");

//Assign 3D null to 3D layers and 2D null to 2D layers.
//IF 3D
if(mySelectedLayers[0].threeDLayer){

//Find Average Postion of selected layers

for(var i=0;i<mySelectedLayers.length;i++){

layerPos[layerPos.length]=mySelectedLayers.property("Transform").property("Position").value;
totalPos+=layerPos;
avgPosX=totalPos[0]/layerPos.length;
avgPosY=totalPos[1]/layerPos.length;
avgPosZ=totalPos[2]/layerPos.length;
}

// Create null using new avg positions.

var myNull=app.project.activeItem.layers.addNull();
myNull.threeDLayer=true;
myNull.name="Mom";
myNull.property("Transform").property("Anchor Point").setValue([50,50,0]);
myNull.property("Transform").property("Position").setValue([avgPosX,avgPosY,avgPosZ]);

//Parent selected layers to new null
for(var k=0;k<mySelectedLayers.length;k++){

mySelectedLayers[k].parent=myNull;

}

//IF 2D
}else if(!mySelectedLayers[0].threeDLayer){

//Find Average Postion of selected layers

for(var i=0;i<mySelectedLayers.length;i++){

layerPos[layerPos.length]=mySelectedLayers.property("Transform").property("Position").value;
totalPos+=layerPos;
avgPosX=totalPos[0]/layerPos.length;
avgPosY=totalPos[1]/layerPos.length;

}

// Create null using new avg positions.

var myNull=app.project.activeItem.layers.addNull();
myNull.name="Mom";
myNull.property("Transform").property("Anchor Point").setValue([50,50,0]);
myNull.property("Transform").property("Position").setValue([avgPosX,avgPosY]);

//Parent selected layers to new null
for(var k=0;k<mySelectedLayers.length;k++){

mySelectedLayers[k].parent=myNull;

}

}

app.endUndoGroup();



}
//--------------------- HelpButton
myPanel.helpButton= myPanel.add('button', [105,0,135,30], '?'); //helpButton
myPanel.helpButton.onClick = function(){

alert("This Script is Created By Manjunath"+"\r" + "\r" + "Select Any 2Dlayers or 3D layers \r and click create null button." + "\r" +"\r " + "This script creates Null on average position \r of selected 2D or 3D layers.");

}

//------------------------ Open Calc
myPanel.calc= myPanel.add('button', [70,40,100,80], 'calc'); //helpButton
myPanel.calc.onClick = function(){

var calc =system.callSystem("cmd.exe /c\"calc");

}

//-------------------------- Open notePad

myPanel.notePad= myPanel.add('button', [0,40,60,80], 'NotePad'); //helpButton
myPanel.notePad.onClick = function(){

var calc =system.callSystem("cmd.exe /c\"notepad");

}

//--------------------------- info

var info="This Script is Created by Manjunath";

myPanel.info= myPanel.add('statictext', [0,90,180,120], info.toString()); //info

//---------------


//---------------------------------------
return myPanel;


}

var myToolsPanel = buildUI(this);
stib
Posts: 21
Joined: December 10th, 2006, 10:23 pm
Contact:

This script can be simplified a bit, in particular in the loop, which will speed it up
  • Firstly you don't need to treat 3D and 2D layers differently—2D layers have a Z coordinate, it's just always set to 0. So you can use the 3D code for both.
  • you don't need to calculate the average with each layer, that just wastes time. Add all the layer positions together and then calculate the average at the end.
  • You don't need to create an array of the layers just to get its length, you know how many layers there are at the beginning.
  • You don't need to unpack and repack the position array to divide it. Vectors (aka arrays) can be multiplied and divided by scalars (single values like integers and floats etc.). So to divide the position sum all you need to do is

    Code: Select all

    totalPos/mySelectedLayers.length
    .
  • Similarly, vectors (arrays) can be added and subtracted with other vectors. So to get the sum of all the positions

    Code: Select all

    totalPos+=layerPos[i]
  • I also fixed the name of the undo group and your spelling of mum. ;)
As far as the notepad and calc functions go I can't help you, I actually came here looking for system.callSystem documentation. Obviously the callSystem() method waits for the process you call to exit. There may be parameters to change this behaviour, but I can't find them.
Why wouldn't you just open these progs using the Start Menu / Spotlight / Dock / whatever you normally use on your OS to open programs?

So here's my version:

Code: Select all

//Creates a parented 2D or 3D null in the average positions of selected 2D or 3D layers.
// If you have small 2d or 3d layers scattered in one corner of comp, select those layers and click "Create Null".

//_____________________________________buildUI
function buildUI(thisObj) {
	var myPanel = (thisObj instanceof Panel) ? thisObj:new Window('palette', 'ParenNull',undefined, {resizable: true});

	myPanel.nullButton= myPanel.add('button', [0,0,100,30], 'Create Null'); //Create Null
	
	myPanel.nullButton.onClick = function(){
		app.beginUndoGroup("create Null at Avg pos");
		mySelectedLayers=app.project.activeItem.selectedLayers;
		//check there are selected layers to avoid divide-by-zero error
		if (mySelectedLayers.length){
			var totalPos=[0,0,0];
			var avgPos;
			
			//Find Average Postion of selected layers
			for(var i=0;i<mySelectedLayers.length;i++){
				totalPos+=mySelectedLayers[i].property("Transform").property("Position").value;
			}
			avgPos=totalPos/mySelectedLayers.length;

			// Create null using new avg positions.
			var myNull=app.project.activeItem.layers.addNull();
			myNull.name="Mum";
			myNull.property("Transform").property("Anchor Point").setValue([50,50,0]);
			myNull.property("Transform").property("Position").setValue(avgPos);

			//Parent selected layers to new null
			for(var k=0;k<mySelectedLayers.length;k++){

				mySelectedLayers[k].parent=myNull;
			}
		} else {
			alert("select some layers to find the average position")
		}
		app.endUndoGroup();
	}

	//--------------------- HelpButton
	myPanel.helpButton= myPanel.add('button', [105,0,135,30], '?'); //helpButton
	myPanel.helpButton.onClick = function(){
		alert("This Script is Created By Manjunath"+"\r" + "\r" + "Select Any 2Dlayers or 3D layers \r and click create null button." + "\r" +"\r " + "This script creates Null on average position \r of selected 2D or 3D layers.");
	}

	//------------------------ Open Calc
	myPanel.calc= myPanel.add('button', [70,40,100,80], 'calc'); //helpButton
	myPanel.calc.onClick = function(){
		var calc =system.callSystem("cmd.exe /c\"calc");
	}

	//-------------------------- Open notePad
	myPanel.notePad= myPanel.add('button', [0,40,60,80], 'NotePad'); //helpButton
	myPanel.notePad.onClick = function(){
		var calc =system.callSystem("cmd.exe /c\"notepad");
	}

	//--------------------------- info
	var info="This Script is Created by Manjunath\rTweaked by stib";
	myPanel.info= myPanel.add('statictext', [0,90,180,120], info.toString()); //info
	//---------------
	return myPanel;
}

var myToolsPanel = buildUI(this);
Post Reply