Friday, January 12, 2007

minimal surface script

// minimum surface creator.
// copyright 2005 Theo Calvin
// free to use for educational purposes.
// for any other use please contact theo@mediumlite.com


minSurfUI;

/////////////////////////////////////////////////////////////////////////////////////

proc string[] parseName (string $input) {
string $return[];
string $next;
string $currentString = "";
int $strCount = 0;
for ($i=2; $i<=`size($input)`; $i++) { $next = `substring ($input) $i $i `; if ($next == "|") { $return[$strCount] = $currentString; $currentString = ""; $strCount++; } else { $currentString = $currentString + $next; } if ($i == `size($input)`) { $return[`size($return)`] = $currentString; } } return $return; } ///////////////////////////////////////////////////////////////////////////////////// proc vector getLocV(string $one){ float $currentpos[] = `xform -ws -q -t ($one)`; return <<$currentpos[0],$currentpos[1],$currentpos[2]>>;
}


// ================================================================================
//
// minSurf
//
// ================================================================================

global proc minSurf() {
waitCursor -state on;
float $curveMin;
float $curveMax;
float $curveSpan;
int $updatePercentage ;
float $smoothLoops;
float $updateAmmount;

progressWindow
-title "Creating Surface"
-status ("Complete: "+$updatePercentage+"%")
-isInterruptable true;
;

//........................
// get selection
//........................
string $curves[] = `ls -sl -l`;
string $path[] = parseName ($curves[0]);

//........................
// create polySurface
//........................
int $minSXarray[] = `intFieldGrp -q -v polySubX_field`;
int $minSYarray[] = `intFieldGrp -q -v polySubY_field`;
int $minSX = $minSXarray[0];
int $minSY = $minSYarray[0];
string $minSholder[] = `polyPlane -w 1 -h 1 -sx $minSX -sy $minSY -ax 0 1 0 -tx 1 -ch 0 -n "minSurface"`;
string $minS = $minSholder[0];

//...............................
// place edges on borders (x1)
//...............................
$curveMin = `getAttr ($curves[0]+".min")`;
$curveMax = `getAttr ($curves[0]+".max")`;
$curveSpan = `abs $curveMax`-`abs $curveMin`;
float $subSpan = $curveSpan/$minSX;


float $currentU;
float $pointLoc[];
for ($i=0; $i<=$minSX; $i++) { $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[0]`; select -r ($minS+ ".vtx["+$i+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //............................... // place edges on borders (y1) //............................... $curveMin = `getAttr ($curves[1]+".min")`; $curveMax = `getAttr ($curves[1]+".max")`; $curveSpan = `abs $curveMax`-`abs $curveMin`; float $subSpan = $curveSpan/$minSY; int $vtx; for ($i=1; $i<$minSY; $i++) { $vtx = $minSX +($i*($minSX+1)); //print ("vtx: " + $vtx + "\n"); $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[1]`; select -r ($minS+ ".vtx["+$vtx+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //............................... // place edges on borders (x2) //............................... $curveMin = `getAttr ($curves[2]+".min")`; $curveMax = `getAttr ($curves[2]+".max")`; $curveSpan = `abs $curveMax`-`abs $curveMin`; float $subSpan = $curveSpan/$minSX; for ($i=0; $i<=$minSX; $i++) { $vtx = ((($minSX+1)*($minSY+1))-1)-$i; $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[2]`; select -r ($minS+ ".vtx["+$vtx+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //............................... // place edges on borders (y2) //............................... $curveMin = `getAttr ($curves[3]+".min")`; $curveMax = `getAttr ($curves[3]+".max")`; $curveSpan = `abs $curveMax`-`abs $curveMin`; float $subSpan = $curveSpan/$minSY; for ($i=1; $i<$minSY; $i++) { $vtx = ((($minSX+1)*($minSY)))-($i*($minSX+1)); $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[3]`; select -r ($minS+ ".vtx["+$vtx+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //...................................... // place vertices into initial position //...................................... vector $v1; vector $v2; vector $v3; vector $v4; vector $new1; vector $new2; vector $pos; int $row; for ($i=($minSX+2); $i< ((($minSX+1)*$minSY)-1); $i++) { int $vMod = $i%($minSX+1); if ($vMod>0 && $vMod < $minSX) { $v1 = getLocV($minS+ ".vtx["+($i-$vMod)+"]"); $v2 = getLocV($minS+ ".vtx["+($i-$vMod+$minSX)+"]"); $v3 = getLocV($minS+ ".vtx["+($vMod)+"]"); $v4 = getLocV($minS+ ".vtx["+($vMod+(($minSX+1)*$minSY))+"]"); $new1 = (($v1*($minSX-$vMod))+($v2*$vMod))/$minSX; $row = $i/($minSX+1); $new2 = (($v3*($minSY-$row))+($v4*$row))/$minSY; $pos = ($new1 + $new2)*.5; select -r ($minS+ ".vtx["+$i+"]") ; move ($pos.x) ($pos.y) ($pos.z); //currentTime 0; } } int $vMod; $smoothLoops = `intSliderGrp -q -v maxIterationsField`; for ($loop=0; $loop<$smoothLoops ; $loop++) { for ($i=($minSX+2); $i< ((($minSX+1)*$minSY)-1); $i++) { $vMod = $i%($minSX+1); if ($vMod>0 && $vMod < $minSX) { //print (" " + $i); $v1 = getLocV($minS+ ".vtx["+($i-1)+"]"); $v2 = getLocV($minS+ ".vtx["+($i+1)+"]"); $v3 = getLocV($minS+ ".vtx["+($i-1-$minSX)+"]"); $v4 = getLocV($minS+ ".vtx["+($i+1+$minSX)+"]"); $pos = ($v1 + $v2 + $v3 + $v4)/4; select -r ($minS+ ".vtx["+$i+"]") ; move ($pos.x) ($pos.y) ($pos.z); } } select -r $minS; //print ("$loop: "+$loop+"\n"); if ( `progressWindow -query -isCancelled` ) break; $updateAmmount = ($loop/$smoothLoops )*100; $updatePercentage= $updateAmmount; //print ("$updatePercentage: " + ($updateAmmount) + "\n"); progressWindow -edit -progress ($updatePercentage) -status ("Complete: "+$updatePercentage+"%") ; if (`checkBox -q -v UIshowSurface`) { refresh -cv; } } progressWindow -endProgress; //print ("curveSpan: " + $curveSpan); select -r $minS; xform -cp; if (`size($path)`>2) {
parent ($minS) ($path[0]+"|"+$path[1]+"|"+$path[2]) ;
}
waitCursor -state off;
}

// ================================================================================
//
// minSurfUI
//
// ================================================================================

global proc minSurfUI () {
if (`window -exists minSurfUIWin` == 1) {
deleteUI minSurfUIWin;
}

string $minSurfUIWin = `window
-title "Minimum Surface Builder"
-menuBar 0
-mxb 0
-rtf 1
minSurfUIWin`;

//scrollLayout;
columnLayout;
separator -h 15 -w 400 -st "none";// whitespace
intFieldGrp -numberOfFields 1
-label "PolyDivisions X" -value1 15
polySubX_field; //`intFieldGrp -q -v polySubX_field`

intFieldGrp -numberOfFields 1
-label "PolyDivisions Y" -value1 15
polySubY_field; //`intFieldGrp -q -v polySubY_field`

// smoothing ITERATIONS
intSliderGrp -label "Smooth Iterations" -field true
-minValue 0 -maxValue 20
-fieldMinValue 0 -fieldMaxValue 100
-value 20
maxIterationsField; //`intSliderGrp -q -v maxIterationsField`


// show updates
separator -h 15 -w 400 -st "none";// whitespace
checkBox -label "Show Surface During Creation" -v 0 UIshowSurface;
/*
separator -h 15 -w 400 -st "in";// whitespace

text -l "0% Complete" UIupdateText;
*/

separator -h 15 -w 400 -st "in";// whitespace
// BUTTONS
//--------------
rowLayout
-nc 2
-cat 1 "both" 0
-cat 2 "both" 0
-columnWidth 1 100
-columnWidth 2 100
;

button -label "Create" -align "center" -command "minSurf";
button -label "Cancel" -align "center" -command "deleteUI minSurfUIWin";

showWindow $minSurfUIWin;
}