Page 1 of 1

Turning Depth of Field off - works only sometimes

Posted: December 9th, 2010, 7:25 am
by Hausgross
Hi there!

I wrote a little script to turn off depth of field quickly - but it doesn't work in every AE project.

If I start a new project, make a new comp and put a bunch of layers and a camera in there it works properly. If I load older projects (which are set up nearly identical) and run the script nothing happens.
Can somebody tell me what's going wrong?

Here is my script:

Code: Select all

// Dof Quicktoggle

// Version 1.0
// Changelog:
// initial release

// Author: M. Bürster for Haus&Gross
// 	

var scriptTitle = "DoF Quicktoggle";

var myComp = app.project.selection;

var myCamera

var myPalette = buildUI(this);

if (myPalette != null && myPalette instanceof Window) {
	myPalette.show();
	}

function buildUI (thisObject) {

if (thisObject instanceof Panel) {
	var myPalette = thisObject;
	} else { 
    var myPalette = new Window ("palette", scriptTitle, undefined, {resizeable:true});
	}

if (myPalette != null ) {
	
	var res =
	"Group { \
		orientation: 'column', \
		alignment: ['fill','fill'], \
		alignChildren: ['left','top'], \
         btnturnoff: Button {text:'DoF off', alignment: ['left','top']}, \
         btnturnon: Button {text:'DoF on', alignment: ['left','top']}, \
      }";
	
	myPalette.grp = myPalette.add(res);
	myPalette.layout.layout(true);
	myPalette.layout.resize();
	
     myPalette.grp.btnturnoff.onClick = function () {
         for (var i = 0; i < myComp.length; i++) {
            myCamera = myComp[i].activeCamera;
            myCamera.depthOfField.setValue([0]);
            }
		}
	
    myPalette.grp.btnturnon.onClick = function () {
          for (var i = 0; i < myComp.length; i++) {
            myCamera = myComp[i].activeCamera;
            myCamera.depthOfField.setValue([1]);
            }
		}

     myPalette.onResizing = myPalette.onRize = function () {this.layout.resize();}

	} //if (myPalette != null ) 
return myPalette;
} //function buildUI (thisObject) 

Re: Turning Depth of Field off - works only sometimes

Posted: December 9th, 2010, 8:59 am
by Paul Tuersley
I think the main problem is that you're defining app.project.selection when you initially run the script, but this isn't a live link, you need to grab it at the point where the button is pressed otherwise it will be referencing the selection at the time when the script was first run.

For example, change the function to this and see what happens as you select different numbers of project items:

Code: Select all

 myPalette.grp.btnturnoff.onClick = function () {
  $.writeln(myComp.length);
  for (var i = 0; i < myComp.length; i++) {
    myCamera = myComp[i].activeCamera;
    myCamera.depthOfField.setValue([0]);
  }
}
Then compare the results to this:

Code: Select all

 myPalette.grp.btnturnoff.onClick = function () {
  myComp = app.project.selection;
  $.writeln(myComp.length);
  for (var i = 0; i < myComp.length; i++) {
    myCamera = myComp[i].activeCamera;
    myCamera.depthOfField.setValue([0]);
  }
}
General advice - use plenty of alert() or $.writeln() lines to examine your variables and ensure they are doing what you expect to help track down these kind of errors.

You may also want to ensure that whatever is selected in the project panel is a comp (xxx instanceof CompItem), not footage or folders, and that there is an activeCamera in the comp, otherwise you'll probably get errors if that isn't the case.

Re: Turning Depth of Field off - works only sometimes

Posted: December 10th, 2010, 12:58 am
by Hausgross
Thank you very much :D

Yesterday I completely forgot to check the variables, so I thought, that I may use a wrong function to determine the comp's camera..