Page 1 of 1

MEL script: camera shake !

Posted: Sat Aug 09, 2008 12:51 pm
by deadalvs
a little script i wrote to pimp your camera moves to give them an unsteady touch

have fun !

-->
Code: Select all
// randCamTranslateWin V1; deadalvs, august 2008
////////////////////////////////////////////////////////////////////////////////////// start GUI 

if (`window -exists "randCamTranslateWin"` == 1){deleteUI "randCamTranslateWin";} 

window -t "randCamTranslateWin V1" randCamTranslateWin;
columnLayout -adjustableColumn 1;

frameLayout -label "info" -cll true -collapse 1 randCamTranslateInfoLayout; 
columnLayout -adjustableColumn 0;
text -l "";
text -l "scripted by deadalvs, august 2008";
text -l "comments: matthias.buehler@mac.com";
text -l "";
text -l "use this script to automatically duplicate a camera";
text -l "and introduce unsteadiness by keyframe randomization";
text -l "";
text -l "multiple camera selections possible !";
text -l "";
text -l "easy to simply duplicate smooth simple camera flights";
text -l "and shake them up. since a new cam is generated, it can";
text -l "always be deleted without touching the original animation";
text -l "";
text -l "copied camera's are named [camName] + SHAKEN";
text -l "";
text -l "* * *";
text -l "for constant stepping use stepping values like 3/3 or 5/5";
text -l "for not linear keyframe stepping can be achieced by using";
text -l "values like 4 / 8";
text -l "like this keyframes are shaken on a randomized increment";
text -l "--> 4,4,6,7,8,6,8,7,5,7,8,5,4, ..";
text -l "";
text -l "only the min max range of the camera's animation is";
text -l "considered and thus re-keyed";
text -l "";
text -l "* * *";
text -l "";
text -l "and a free maya tip: MiddleMouseDragNDrop a camera node";
text -l "from the outliner to the viewport to look thru that cam !";
text -l "";
text -l "HAVE FUN !";
text -l "";

setParent ..; 
setParent ..;

frameLayout -label "randCamTranslateSettings" -cll true -collapse 0 randCamTranslateSettingsLayout; 
columnLayout -adjustableColumn 1;
text -l "";
text -l "choose random min/max frame stepping";
intField  -width 300 -v 3 minFrameStepping_intField;
intField  -width 300 -v 7 maxFrameStepping_intField;
text -l "";
text -l "xyz translate [units] per step";
floatField  -width 300 -v 0.03 xTranslate_floatField;
floatField  -width 300 -v 0.05 yTranslate_floatField;
floatField  -width 300 -v 0.02 zTranslate_floatField;
text -l "";
text -l "xyz rotations [degs] per step";
floatField  -width 300 -v 2 xRotate_floatField;
floatField  -width 300 -v 3 yRotate_floatField;
floatField  -width 300 -v 1 zRotate_floatField;
text -l "";
button -h 45 -l "duplicate selected cams and shake !" -command "randCamTranslate();";
setParent ..; 
setParent ..;

showWindow randCamTranslateWin;

////////////////////////////////////////////////////////////////////////////////////// end GUI


global proc randCamTranslate() {

global int $minFrameStepping;
global int $maxFrameStepping;
global float $xTranslate;
global float $yTranslate;
global float $zTranslate;
global float $xRotate;
global float $yRotate;
global float $zRotate;
$minFrameStepping = `intField -q -v minFrameStepping_intField`;
$maxFrameStepping = `intField -q -v maxFrameStepping_intField`;
$xTranslate = `floatField -q -v xTranslate_floatField`;
$yTranslate = `floatField -q -v yTranslate_floatField`;
$zTranslate = `floatField -q -v zTranslate_floatField`;
$xRotate = `floatField -q -v xRotate_floatField`;
$yRotate = `floatField -q -v yRotate_floatField`;
$zRotate = `floatField -q -v zRotate_floatField`;


global string $selectedCamerasArray[];
$selectedCamerasArray = `ls -sl`;

for ($eachCamera in $selectedCamerasArray){
select -r $eachCamera;
duplicate -rr -un;
rename ($eachCamera + "SHAKEN");

// get firstKeyFrameNumber
// get lastKeyFrameNumber
global int $firstKeyFrameNumber;
global int $lastKeyFrameNumber;
$firstKeyFrameNumber = `findKeyframe -timeSlider -which first`;
$lastKeyFrameNumber = `findKeyframe -timeSlider -which last`;

currentTime $firstKeyFrameNumber;
currentTime -q;

	do {
	currentTime ((`currentTime -query`) + floor ( rand ($minFrameStepping, ($maxFrameStepping + 1) )));

	global float $xTranslate;
	global float $yTranslate;
	global float $zTranslate;
	global float $xRotate;
	global float $yRotate;
	global float $zRotate;
	move -r (rand (-$xTranslate,$xTranslate)) (rand (-$yTranslate,$yTranslate)) (rand (-$zTranslate,$zTranslate));
	rotate -r (rand (-$xRotate,$xRotate)) (rand (-$yRotate,$yRotate)) (rand (-$zRotate,$zRotate));
	
	if ( (`currentTime -query`) < $lastKeyFrameNumber){
	setKeyframe -attribute "translateX" -attribute "translateY" -attribute "translateZ" -attribute "rotateX" -attribute "rotateY" -attribute "rotateZ" ($eachCamera + "SHAKEN");
	}
	
	else{
	break;
	}
	
	}
	while ( (`currentTime -query`) < $lastKeyFrameNumber);

currentTime $firstKeyFrameNumber;

// end
}

// end randCamTranslate
}

Posted: Sun Aug 10, 2008 9:13 am
by deadalvs
update ..

the problem was linear curve interpolation between keyframes. has been fixed by baking the curve first and deleting all unnecessary keys.

have more fun now !

Code: Select all

// randCamTranslateWin V1.10; deadalvs, august 2008
////////////////////////////////////////////////////////////////////////////////////// start GUI 

if (`window -exists "randCamTranslateWin"` == 1){deleteUI "randCamTranslateWin";} 

window -t "randCamTranslateWin V1.10" randCamTranslateWin;
columnLayout -adjustableColumn 1;

frameLayout -label "info" -cll true -collapse 1 randCamTranslateInfoLayout; 
columnLayout -adjustableColumn 0;
text -l "";
text -l "scripted by deadalvs, august 2008";
text -l "comments: matthias.buehler@mac.com";
text -l "";
text -l "use this script to automatically duplicate a camera";
text -l "and introduce unsteadiness by keyframe randomization";
text -l "";
text -l "multiple camera selections possible !";
text -l "";
text -l "easy to simply duplicate smooth simple camera flights";
text -l "and shake them up. since a new cam is generated, it can";
text -l "always be deleted without touching the original animation";
text -l "";
text -l "copied camera's are named [camName] + SHAKEN";
text -l "";
text -l "* * *";
text -l "for constant stepping use stepping values like 3/3 or 5/5";
text -l "for not linear keyframe stepping can be achieced by using";
text -l "values like 4 / 8";
text -l "like this keyframes are shaken on a randomized increment";
text -l "--> 4,4,6,7,8,6,8,7,5,7,8,5,4, ..";
text -l "";
text -l "only the min max range of the camera's animation is";
text -l "considered and thus re-keyed";
text -l "";
text -l "* * *";
text -l "";
text -l "and a free maya tip: MiddleMouseDragNDrop a camera node";
text -l "from the outliner to the viewport to look thru that cam !";
text -l "";
text -l "HAVE FUN !";
text -l "";

setParent ..; 
setParent ..;

frameLayout -label "randCamTranslateSettings" -cll true -collapse 0 randCamTranslateSettingsLayout; 
columnLayout -adjustableColumn 1;
text -l "";
text -l "choose random min/max frame stepping";
intField  -width 300 -v 6 minFrameStepping_intField;
intField  -width 300 -v 10 maxFrameStepping_intField;
text -l "";
text -l "xyz translate [units] per step";
floatField  -width 300 -v .03 xTranslate_floatField;
floatField  -width 300 -v .03 yTranslate_floatField;
floatField  -width 300 -v .01 zTranslate_floatField;
text -l "";
text -l "xyz rotations [degs] per step";
floatField  -width 300 -v 2 xRotate_floatField;
floatField  -width 300 -v 3.5 yRotate_floatField;
floatField  -width 300 -v 1.5 zRotate_floatField;
text -l "";
button -h 45 -l "duplicate selected cams and shake !" -command "randCamTranslate();";
setParent ..; 
setParent ..;

showWindow randCamTranslateWin;

////////////////////////////////////////////////////////////////////////////////////// end GUI


global proc randCamTranslate() {

global int $minFrameStepping;
global int $maxFrameStepping;
global float $xTranslate;
global float $yTranslate;
global float $zTranslate;
global float $xRotate;
global float $yRotate;
global float $zRotate;
$minFrameStepping = `intField -q -v minFrameStepping_intField`;
$maxFrameStepping = `intField -q -v maxFrameStepping_intField`;
$xTranslate = `floatField -q -v xTranslate_floatField`;
$yTranslate = `floatField -q -v yTranslate_floatField`;
$zTranslate = `floatField -q -v zTranslate_floatField`;
$xRotate = `floatField -q -v xRotate_floatField`;
$yRotate = `floatField -q -v yRotate_floatField`;
$zRotate = `floatField -q -v zRotate_floatField`;

//string $eachCamera = "camera1";

global string $selectedCamerasArray[];
$selectedCamerasArray = `ls -sl`;
for ($eachCamera in $selectedCamerasArray){
select -r $eachCamera;
duplicate -rr -un;
rename ($eachCamera + "SHAKEN");
global string $activeCameraName;
$activeCameraName = $eachCamera + "SHAKEN";
// get firstKeyFrameNumber
// get lastKeyFrameNumber
global int $firstKeyFrameNumber;
global int $lastKeyFrameNumber;
$firstKeyFrameNumber = `findKeyframe -timeSlider -which first`;
$lastKeyFrameNumber = `findKeyframe -timeSlider -which last`;

currentTime ($firstKeyFrameNumber + 1);


// create all steady keyframes without shaking
	
	// first bake curve
	string $command1 = ("bakeResults -sampleBy 1 -time \"" );
	string $command2 = ("\"" + $activeCameraName + "\"");
	string $commandComplete = $command1 + $firstKeyFrameNumber + ":" + $lastKeyFrameNumber + "\"" +" -preserveOutsideKeys 1 -sparseAnimCurveBake 0 {" + $command2 + "}";
	eval ($commandComplete);
	// delete all keyframes that are not randomly stepped
	
	do {
	// find a random time stepping 
	global int $currentTimeStepping;
	$currentTimeStepping = `floor ( rand ($minFrameStepping, ($maxFrameStepping + 1)))`;
	
		for ($stepCounter = 1; $stepCounter <= $currentTimeStepping; $stepCounter++){
		
		if (($stepCounter != $currentTimeStepping) &&  ((`currentTime -query`) !=  $firstKeyFrameNumber) &&  ((`currentTime -query`) !=  $lastKeyFrameNumber)){
		cutKey -clear -t  (`currentTime -query`);	
		}		
		if (($stepCounter == $currentTimeStepping)&&  ((`currentTime -query`) !=  $lastKeyFrameNumber) ){
		global float $xTranslate;
		global float $yTranslate;
		global float $zTranslate;
		global float $xRotate;
		global float $yRotate;
		global float $zRotate;
		move -r (rand (-$xTranslate,$xTranslate)) (rand (-$yTranslate,$yTranslate)) (rand (-$zTranslate,$zTranslate));
		rotate -r (rand (-$xRotate,$xRotate)) (rand (-$yRotate,$yRotate)) (rand (-$zRotate,$zRotate));
		setKeyframe -attribute "translateX" -attribute "translateY" -attribute "translateZ" -attribute "rotateX" -attribute "rotateY" -attribute "rotateZ" ($eachCamera + "SHAKEN");
		}
		currentTime ((`currentTime -query`) + 1);	
		
		}
	
	}
	while ( (`currentTime -query`) < $lastKeyFrameNumber);

currentTime $firstKeyFrameNumber;

// end
}

// end randCamTranslate
}