Turning Depth of Field off - works only sometimes

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
Hausgross
Posts: 5
Joined: December 8th, 2010, 3:44 am
Location: Saarbrücken, Saarland, Germany
Contact:

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) 
Haus&Gross communications GmbH
Homepage - Motionblog - Twitter
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

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.
User avatar
Hausgross
Posts: 5
Joined: December 8th, 2010, 3:44 am
Location: Saarbrücken, Saarland, Germany
Contact:

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..
Haus&Gross communications GmbH
Homepage - Motionblog - Twitter
Post Reply