Hi there,
I was forwarded a script I used to used to allow my animators to play with their camera's focal length while adjusting the camera's position to keep it's focal point in the same position. Unfortunately I am too dumb to figure out how to make this script run. Can someone help me figure out what steps to take to make this work? I've pasted the entire forward below.
thank you!
// ----------------------------------
// ++++
// focalLength
//
// Create a slider that allows a camera focal length to be adjusted based on
// on the camera's center of interest.
//
// ----------------------------------
global proc focalLength()
{
}
// ----------------------------------
// focalLength.run
//
// Launch the UI and set up the expression
//
//
// ----------------------------------
global proc focalLength.run()
{
// clean up any existing focal length expressions
focalLength.clean;
string $sel[] = ls -l -sl;
string $cam = $sel[0];
string $targ = $sel[1];
if(size $sel != 2)
msgs.error "Please select two transforms.";
string $camShape = getShapeNode $cam;
if(objectType $camShape != "camera")
msgs.error "First Selection Item must be a camera";
if(objectType $targ != "transform")
msgs.error "Second Selection Must be a trasnform";
string $exp = focalLength.createExpression;
string $win = focalLength.UI $camShape;
focalLength.createScriptJob $win $exp;
}
// ----------------------------------
// ++++
// focalLength.UI
//
// update the
//
// ----------------------------------
global proc string focalLength.UI(string $camShape)
{
float $v = getAttr ($camShape + ".fl");
string $win = "focalLengthUI";
if(window -ex $win)
deleteUI $win;
if(windowPref -ex $win)
windowPref -remove $win;
$win = window -t "FocalLengther" -wh 390 90 -s 1 $win;
columnLayout..;
string $slider = floatSliderGrp -min 2.5 -max 150 -v $v -field 1 -l "Focal Length" -cc ("focalLength.sliderCallback " + $camShape) -dc ("focalLength.sliderCallback " + $camShape) "focalLengthSlider";
button -h 30 -w 375 -l "Close" -c ("deleteUI " + $win);
setParent..;
showWindow $win;
return $win;
}
// ----------------------------------
// ++++
// focalLength.sliderCallBack
//
// callBack for slider
//
//
// ----------------------------------
global proc focalLength.sliderCallback(string $camShape)
{
float $v = floatSliderGrp -q -v "focalLengthSlider";
setAttr ($camShape + ".fl") $v;
}
// ----------------------------------
// ++++
// focalLength.clean
//
// Searches all cameras and removes any leftOver focalLength expressions
//
// ----------------------------------
global proc focalLength.clean()
{
string $chk[] = ls -type "expression";
for($exp in $chk)
{
if(attributeQuery -n $exp -ex "focalLengthExpression")
delete $exp;
}
}
// ----------------------------------
// ++++
// focalLength.createScriptJob
//
// create a scriptjob that will delete the expression on close of the UI
//
//
// ----------------------------------
global proc focalLength.createScriptJob(string $win, string $exp)
{
scriptJob -kws -p $win -uid $win (("delete " + $exp + ";") + "focalLength.clean;");
}
// ----------------------------------
// ++++
// focalLength.createExpression
//
// create the focal length expression
//
// ----------------------------------
global proc string focalLength.createExpression()
{
string $sel[] = ls -sl;
string $cx, $cy, $cz;
string $cam = $sel[0];
string $targ = $sel[1];
if (size($sel) == 1)
{
$cx = $cam + ".tumblePivotX";
$cy = $cam + ".tumblePivotY";
$cz = $cam + ".tumblePivotZ";
}
else
{
$cx = $targ + ".translateX";
$cy = $targ + ".translateY";
$cz = $targ + ".translateZ";
}
$fl0=getAttr ($cam+".fl");
$coi0=getAttr ($cam+".coi");
string $exp = "float $negV[];\n" +
"$negV[0] = " + $cam + ".translateX - " + $cx + ";\n" +
"$negV[1] = " + $cam + ".translateY - " + $cy + ";\n" +
"$negV[2] = " + $cam + ".translateZ - " + $cz + ";\n" +
"normalize($negV);\n" +
"float $tp[];\n" +
"$tp[0] = " + $cx + ";\n" +
"$tp[1] = " + $cy + ";\n" +
"$tp[2] = " + $cz + ";\n" +
$cam + ".translateX = "+ $cx + " + " + $cam + ".focalLength*" + $coi0 + "*$negV[0]/" + $fl0 + ";\n" +
$cam + ".translateY = "+ $cy + " + " + $cam + ".focalLength*" + $coi0 + "*$negV[1]/" + $fl0 + ";\n" +
$cam + ".translateZ = "+ $cz + " + " + $cam + ".focalLength*" + $coi0 + "*$negV[2]/" + $fl0 + ";";
string $exp = expression -n "focalLengthExpression" -s $exp;
addAttr -at "long" -min 1 -max 1 -dv 1 -k 1 -ln "focalLengthExpression" $exp;
return $exp;
}