Skip to content

Instantly share code, notes, and snippets.

@Hoikohroh
Last active August 29, 2015 14:25
Show Gist options
  • Save Hoikohroh/c0b0a810ca3d2f0c6ae3 to your computer and use it in GitHub Desktop.
Save Hoikohroh/c0b0a810ca3d2f0c6ae3 to your computer and use it in GitHub Desktop.
Adobe AfterEffects Script (JSX):Z_fake_DOF
/*
Adobe AfterEffects Script (JSX)
fake_DOF
v20150719
*/
////////////////////////////////variable declaration////////////////////////////////
var thisComp= app.project.activeItem;
var selectedLayers= new Array();
var thisLayer = null;
var focusNul,foucusZ,Zpos,slider1,slider2;
var exp = "Focus_Null = thisComp.layer(\"Focus_Null\");" + "\n" +
"FocusZ = Focus_Null.transform.position[2];" + "\n" +
"thisZ = transform.position[2];" + "\n" +
"range = Focus_Null.effect(\"range_near/far\")(1);" + "\n" +
"offset = Focus_Null.effect(\"offset_near/far\")(1);" + "\n" +
"blur = Focus_Null.effect(\"Blur_near/far\")(1);" + "\n" +
"if (thisZ < FocusZ){" + "\n" +
"if (thisZ < range[0]){temp = blur[0]}else{" + "\n" +
"if (thisZ > (FocusZ + offset[0])){temp = 0}else{" + "\n" +
"temp = ((thisZ - FocusZ) - offset[0]) / (range[0] - (FocusZ + offset[0]));" + "\n" +
"temp = Math.abs(temp) * blur[0];" + "\n" +
"};" + "\n" +
"};" + "\n" +
"}else if (thisZ > FocusZ){" + "\n" +
"if (thisZ > range[1]){temp = blur[1];}else{" + "\n" +
"if (thisZ < (FocusZ + offset[1])){temp = 0}else{" + "\n" +
"temp = ((thisZ - FocusZ) - offset[1]) / (range[1] - (FocusZ + offset[1]));" + "\n" +
"temp = Math.abs(temp) * blur[1];" + "\n" +
"};" + "\n" +
"};" + "\n" +
"}else {temp = 0}" + "\n" +
"temp";
////////////////////////////////body////////////////////////////////
if(thisComp instanceof CompItem){
selectedLayers = thisComp.selectedLayers;
if(selectedLayers.length > 0){
Zpos = compareZ(selectedLayers);
if (typeof(Zpos[0]) == "number" && typeof(Zpos[1]) == "number"){
// add DOF Null
app.beginUndoGroup("add_focus_Null");
foucusZ = (Zpos[1]-((Zpos[1]-Zpos[0])/2));
focusNul = create3dNull(thisComp,foucusZ,"Focus_Null");
chkBox0 = focusNul.property("ADBE Effect Parade").addProperty("ADBE Checkbox Control");
chkBox0.name = "Blur_ON/OFF";
chkBox0.property("ADBE Checkbox Control-0001").setValue(true);
slider1 = focusNul.property("ADBE Effect Parade").addProperty("ADBE Point Control");
slider1.name = "Blur_near/far";
slider1.property("ADBE Point Control-0001").setValue([30,30]);
slider2 = focusNul.property("ADBE Effect Parade").addProperty("ADBE Point Control");
slider2.name = "range_near/far";
slider2.property("ADBE Point Control-0001").setValue([Zpos[0],Zpos[1]]);
slider3 = focusNul.property("ADBE Effect Parade").addProperty("ADBE Point Control");
slider3.name = "offset_near/far";
slider3.property("ADBE Point Control-0001").setValue([0,0]);
app.endUndoGroup();
// add DOF effect
app.beginUndoGroup("add_Blur");
for (var i = 0; i < selectedLayers.length; i++) {
//fastBlur
thisFill = selectedLayers[i].property("ADBE Effect Parade").addProperty("ADBE Fast Blur");
thisFill.property("ADBE Fast Blur-0001").expression = exp;
thisFill.property("ADBE Fast Blur-0003").setValue(true);
};
app.endUndoGroup();
};
} else {
alert('unselected layer');
};
} else {
alert('unselected compositon');
};
////////////////////////////////functions////////////////////////////////
// Zを比較
function compareZ(layers){
var max = null;
var min = null;
for (var i = 0; i < layers.length; i++) {
if (! layers[i].threeDLayer){}else {
tmpN = layers[i].position.value[2];
if (typeof(max) == "number") {max = Math.max(max, tmpN)} else {max = tmpN};
if (typeof(min) == "number") {min = Math.min(min, tmpN)} else {min = tmpN};
};
};
return [min,max];
};
//3Dnullを作成
function create3dNull (comp,value,string){
var tempNull = comp.layers.addNull();
tempNull.threeDLayer = true;
tempNull.position.setValue([(comp.width/2),(comp.height/2),value]);
tempNull.name = string;
return tempNull;
};
/*エクスプレッション
Focus_Null = thisComp.layer("Focus_Null");
FocusZ = Focus_Null.transform.position[2];
thisZ = transform.position[2];
range = Focus_Null.effect("range_near/far")(1);
offset = Focus_Null.effect("offset_near/far")(1);
blur = Focus_Null.effect("Blur_near/far")(1);
if (thisZ < FocusZ){
if (thisZ < range[0]){
temp = blur[0];
}else{
if (thisZ > (FocusZ + offset[0])){temp = 0}else{
temp = ((thisZ - FocusZ) - offset[0]) / (range[0] - (FocusZ + offset[0]));
temp = Math.abs(temp) * blur[0];
};
};
}else if (thisZ > FocusZ){
if (thisZ > range[1]){
temp = blur[1];
}else{
if (thisZ < (FocusZ + offset[1])){temp = 0}else{
temp = ((thisZ - FocusZ) - offset[1]) / (range[1] - (FocusZ + offset[1]));
temp = Math.abs(temp) * blur[1];
};
};
}else {temp = 0}
temp
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment