ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Keenan Parry
Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 8:20:02 pm

Hey there! So I'm trying to add additional functionality to the camera by easily selecting a layer target for the Focus Distance via a Layer Control expression controller. I started with the code that After Effects generates if you go to Layer>Camera>Link Focus Distance To Layer. I altered the code slightly and added this at the top:
target = thisComp.layer("Camera_Controls").effect("DOF_Layer_Link")("Layer")

This works as expected and it switches between layers which is really nice. But if I have the layer controller set to "None" it produces an error. I'd like for the Focus Distance to behave as it normally does when no layer is selected in the Layer Control and to get rid of the error.

Having trouble getting this to work as it's not as intuitive as I thought. I'm having trouble understanding the generated code so I don't know where to implement this or if I need another IF statement or another TRY statement. I posted the full code below- if anyone could help out I'd greatly appreciate it. Thank you!

target = thisComp.layer("Camera_Controls").effect("DOF_Layer_Link")("Layer")

try{
thisComp.layer("Camera 1");
transform.pointOfInterest;
}catch(err){
quote = String.fromCharCode(34);
$.error = "" + quote + "Camera 1" + quote + " cannot be a One-Node camera"
}

layer = target;

try{
layer.transform.position[2]
}catch(err){
quote = String.fromCharCode(34);
$.error = "" + quote + "Focus" + quote + " cannot be a 2D layer. The Focus Distance of " + quote + "Camera 1" + quote + " can only be set to follow 3D layers."
}
cam_pos = [transform.position[0] * thisComp.pixelAspect, transform.position[1], transform.position[2]];
cam_poi = [transform.pointOfInterest[0] * thisComp.pixelAspect, transform.pointOfInterest[1], transform.pointOfInterest[2]];
if (this.hasParent)
{
cam_pos = this.parent.toWorld( cam_pos );
cam_poi = this.parent.toWorld( cam_poi );
}
layer = target;
layer_pos = [layer.transform.position[0] * thisComp.pixelAspect, layer.transform.position[1], layer.transform.position[2]];
if ( layer.hasParent )
{
layer_pos = layer.parent.toWorld( layer.transform.position);
}
cam_vector = sub(cam_poi, cam_pos);
if( length(cam_vector) != 0) {
cam_vector = normalize(cam_vector);
}
layer_vector = sub(layer_pos, cam_pos);
dot(layer_vector, cam_vector);


dot(layer_vector, cam_vector);


Return to posts index

Dan Ebberts
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 9:14:02 pm

I think I'd use a single try/catch where the try section includes your first line and everything that happens if the layer control is set to a layer, and the catch section has the code for what happens if the control is set to None.


Dan



Return to posts index

Keenan Parry
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 9:30:04 pm

Thanks for response. Do you know what the expression is to identify that "none" is selected and what to do with it? I looked this up and saw a response with an if statement but it didn't seem to be able to "fit" into the rest of the code for linking focus distance. I hope I'm making sense, thanks again!


Return to posts index


Dan Ebberts
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 9:42:41 pm

I don't know of a way to detect None other than with the catch clause of a try/catch.

Dan



Return to posts index

Dan Ebberts
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 10:01:44 pm

Thinking about it a little more, I think I would start the expression this way:


try{
target = thisComp.layer("Camera_Controls").effect("DOF_Layer_Link")("Layer");
}catch (e){
target = null;
}


Then you can just test for target == null to see if None was selected. That gets the try/catch out of the way early in the expression--it's hard to debug an expression with a bunch of code inside a try/catch construct.

Dan



Return to posts index

Keenan Parry
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 10:39:55 pm

Thanks for taking the time. I entered your code at the beginning and it works selecting layers but still seem to get the same error when none is selected.



Return to posts index


Keenan Parry
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 11:04:42 pm



try{
target = thisComp.layer("Camera_Controls").effect("DOF_Layer_Link")("Layer");
}catch (e){
target = null;
}

try{
thisComp.layer("Camera 1");
transform.pointOfInterest;
}catch(err){
quote = String.fromCharCode(34);
$.error = "" + quote + "Camera 1" + quote + " cannot be a One-Node camera"
}

layer = target;

try{
layer.transform.position[2]
}catch(err){
quote = String.fromCharCode(34);
$.error = "" + quote + "Focus" + quote + " cannot be a 2D layer. The Focus Distance of " + quote + "Camera 1" + quote + " can only be set to follow 3D layers."
}
cam_pos = [transform.position[0] * thisComp.pixelAspect, transform.position[1], transform.position[2]];
cam_poi = [transform.pointOfInterest[0] * thisComp.pixelAspect, transform.pointOfInterest[1], transform.pointOfInterest[2]];
if (this.hasParent)
{
cam_pos = this.parent.toWorld( cam_pos );
cam_poi = this.parent.toWorld( cam_poi );
}
layer = target;
layer_pos = [layer.transform.position[0] * thisComp.pixelAspect, layer.transform.position[1], layer.transform.position[2]];
if ( layer.hasParent )
{
layer_pos = layer.parent.toWorld( layer.transform.position);
}
cam_vector = sub(cam_poi, cam_pos);
if( length(cam_vector) != 0) {
cam_vector = normalize(cam_vector);
}
layer_vector = sub(layer_pos, cam_pos);
dot(layer_vector, cam_vector);


dot(layer_vector, cam_vector);


Return to posts index

Dan Ebberts
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 29, 2018 at 11:44:36 pm

Honestly, rather than try to mess with the expression generated by AE, I'd just do this:


try{
target = thisComp.layer("Camera_Controls").effect("DOF_Layer_Link")("Layer");
}catch (e){
target = null;
}
if(target != null){
v1 = target.toWorld(target.anchorPoint) - toWorld([0,0,0]);
v2 = toWorldVec([0,0,1]);
dot(v1,v2)
}else{
value;
}



Dan



Return to posts index

Keenan Parry
Re: Trying to link camera Focus Distance to Layer Control but getting error when Layer Control selects "none"
on Dec 30, 2018 at 12:03:03 am

Dan, this is perfect! Thank you so much for your help, this is much easier to understand and work with.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]