// Create from Curves: Branches, Wires, Vines and Ropes
////////////////////////////////////////////////////////////////////////
// by Leela Siragusa for www.leelasiragusa.com portfolio
// tested on Maya 2009 and Maya 2012
////////////////////////////////////////////////////////////////////////
{
if(`window -exists CreateBranches`)
{
deleteUI CreateBranches;
}
window -s true -t "Create Branches from Curves" CreateBranches;
	
	frameLayout -borderStyle "etchedOut"; 
	columnLayout -adjustableColumn true;
	
	text -l "DO NOT DELETE HISTORY UNTIL ADJUSTMENTS ARE DONE";
	
	text -l "select curves to convert";
	textFieldGrp -l "Name of new Polys" -tx branch -ed true nameOfPoly; 
	button -label "Create Polys from Curves" -bgc .5 .75 .5 -align "center" -command "treeBranches (`textFieldGrp -q -text nameOfPoly`)";

	text -l "";
	text -l "select branches to adjust";
	floatSliderGrp -label "Amount to change width" -field true -pre 1 -min -1 -max 1 -value 0  changeWidth;

	button -label "Change Width from Slider Value" -bgc .75 .5 .75 -align "center" -command "changeBranchWidth (`floatSliderGrp -q -value changeWidth`)";
	button -label "Increase Width by .1" -bgc .5 .5 .75 -align "center" -command "changeBranchWidth (.1)";
	button -label "Decrease Width by .1" -bgc .55 .5 .75 -align "center" -command "changeBranchWidth (-.1)";
	
	text -l "select One branch at a time";
	button -label "Select Width Adjustment" -bgc .7 .2 .9 -align "center" -command "selectScaleAxisofCircle()";
	button -label "Select Tube Angle Adjustment" -bgc .7 .2 .6 -align "center" -command "selectRotationAxisofCircle()";

	text -l "";	
	text -l "select branches to adjust";
	intSliderGrp -label "Edges Around" -field true -min 3 -max 8 -value 5  changeEdges;

	button -label "Change Edges with Slider Value" -bgc .8 .7 .1 -align "center" -command "changeEdgeVerts (`intSliderGrp -q -value changeEdges`)";

	text -l "select branches to adjust";
	intSliderGrp -label "Edges on Length" -field true -min -20 -max 20 -value -5  changeRingSize;

	button -label "Change Edge Rings with Slider Value" -bgc .2 .7 .8 -align "center" -command "changeEdgeRings (`intSliderGrp -q -value changeRingSize`)";
	button -label "Increase Edge Rings by 1" -bgc .1 .6 .4 -align "center" -command "changeEdgeRings (1)";
	button -label "Decrease Edge Rings by 1" -bgc .5 .7 .6 -align "center" -command "changeEdgeRings (-1)";

	floatSliderGrp -label "Taper Value" -field true -pre 2 -min 0 -max 1 -value .1  changeTaper;

	button -label "Change Taper Amount" -bgc .9 .7 .1 -align "center" -command "changeBranchTaper (`floatSliderGrp -q -value changeTaper`)";

	text -l "";
	text -l "select One branch at a time";
	button -label "Change Poly Position with Control Vertices" -bgc .1 .7 .6 -align "center" -command "selectControlVertex()";
	text -l "select Control Vertex and press to End";
	button -label "End Adjustment" -bgc .8 .2 .4 -align "center" -command "endAdjustment()";

	text -l "";
	text -l "";
	text -l "Only Delete History When Adjustments Are Done";
	text -l "select branches to finalize";
	button -label "Delete History" -bgc .9 .4 .1 -align "center" -command "DeleteHistory()";
	button -label "Delete referenceTube and nurbsCircle" -bgc .8 .2 .8 -align "center" -command "DeleteTubeCircle()";
	button -label "Delete nurbsCurve and Group" -bgc .8 .2 .6 -align "center" -command "DeleteCurveGroup()";

showWindow CreateBranches;
window -e -wh 400 800 CreateBranches;
}

//Create Polygon branches from curves
proc treeBranches(string $nameOfCurvePolys)
{
string $curveGroup[] = `ls -sl`;

for ($u=0; $u<size($curveGroup); $u++)
{
float $positionEndPoint[] = `pointPosition ($curveGroup[$u]+".ep[0]")`;

//Create and select circle
string $branchWidthNurb[] =`circle -c 0 0 0 -nr 0 1 0 -sw 360 -r .1 -d 3 -ut 0 -tol 0.01 -s 6 -ch 1`;

move -a -ws $positionEndPoint[0] $positionEndPoint[1] $positionEndPoint[2] $branchWidthNurb[0];
float $positionDirectionPoint[] = `pointPosition ($curveGroup[$u]+".ep[1]")`;

//create and rename Camera
camera -centerOfInterest 5 -focalLength 35 -lensSqueezeRatio 1 -cameraScale 1 -horizontalFilmAperture 1.41732 -horizontalFilmOffset 0 -verticalFilmAperture     0.94488 -verticalFilmOffset 0 -filmFit Fill -overscan 1 -motionBlur 0 -shutterAngle 144 -nearClipPlane 0.01 -farClipPlane 1000 -orthographic 0 -    orthographicWidth 30; objectMoveCommand; cameraMakeNode 2 "";
renameSelectionList cameraToDelete;
string $selectCameraGroup[] = `listRelatives -p`;
rename $selectCameraGroup[0] cameraToDelete_group;

move -a -ws $positionEndPoint[0] $positionEndPoint[1] $positionEndPoint[2] cameraToDelete1;
move -a -ws $positionDirectionPoint[0] $positionDirectionPoint[1] $positionDirectionPoint[2] cameraToDelete2;
float $lineAngle[] = `getAttr cameraToDelete1.rotate`;

rotate -r -ws ($lineAngle[0]+90) $lineAngle[1] $lineAngle[2] $branchWidthNurb[0];

delete cameraToDelete_group;

// Extrude settings: nurbs
extrude -ch true -rn false -po 0 -et 2 -ucp 0 -fpt 0 -upn 0 -rotation 1 -scale 1 -rsp 1 $branchWidthNurb[0] $curveGroup[$u];
rename referenceTube1;
string $tubeToExtrude[] = `ls -sl`;
nurbsToPoly -mnd 1  -ch 1 -f 2 -pt 1 -pc 200 -chr 0.9 -ft 0.01 -mel 0.001 -d 0.1 -ut 1 -un 6 -vt 1 -vn 20 -uch 1 -ucr 1 -cht 0.01 -es 0 -ntr 0 -mrt 0 -uss   1   $tubeToExtrude[0];

// Rename and Group
string $createdPoly = `rename ($nameOfCurvePolys+"_1")`;

string $groupA = `rename $branchWidthNurb[0] ("nurbsCircle_"+$createdPoly)`;
string $groupB = `rename $tubeToExtrude[0] ("referenceTube_"+$createdPoly)`;
string $groupC = `rename $curveGroup[$u] ("curve_"+$createdPoly)`;

rename `group $groupA $groupB $groupC` ("group_"+$createdPoly);
string $namedCurveGroup[] = `ls -sl`;
hide $namedCurveGroup;

select $createdPoly;
}
}

//Increase Branch Width
proc changeBranchWidth(float $newBranchWidth)
{
string $branchesToSize[] = `ls -sl`;

for($u=0; $u<size($branchesToSize); $u++)
{
string $circleToScale = "nurbsCircle_"+$branchesToSize[$u];

float $originalCircleScale[] = `getAttr ($circleToScale+".scale")`;
select $circleToScale;
scale ($originalCircleScale[0]+$newBranchWidth) ($originalCircleScale[1]+$newBranchWidth) ($originalCircleScale[2] +$newBranchWidth);
select $branchesToSize;
}
}

// Change the amount of sides to a branch
proc changeEdgeVerts (int $newEdgeNumber)
{
string $selectedBranch[]=`ls -sl`;

for ($u=0; $u<size($selectedBranch); $u++)
{
string $branchNumber[];
tokenize $selectedBranch[$u] "_" $branchNumber;

string $nameOfInput[] = `listConnections ($branchNumber[0]+"_Shape"+$branchNumber[1])`;

int $typeTest = `objectType -isType "nurbsTessellate" $nameOfInput[0]`;
if ($typeTest == 1){
select $nameOfInput[0];
}else{
select $nameOfInput[1];
}
string $nameOfTess[] = `ls -sl`;

setAttr ($nameOfTess[0]+".uNumber") ($newEdgeNumber+1);
}
select $selectedBranch;
}

// Change the ammount of Edges on a Branch
proc changeEdgeRings(int $newEdgesLength)
{
string $selectedBranch[]=`ls -sl`;

for ($u=0; $u<size($selectedBranch); $u++)
{
string $branchNumber[];
tokenize $selectedBranch[$u] "_" $branchNumber;

string $nameOfInput[] = `listConnections ($branchNumber[0]+"_Shape"+$branchNumber[1])`;
int $typeTest = `objectType -isType "nurbsTessellate" $nameOfInput[0]`;
if ($typeTest == 1){
select $nameOfInput[0];
}else{
select $nameOfInput[1];
}
string $nameOfTess[] = `ls -sl`;

int $originalEdgeNumber = `getAttr ($nameOfTess[0]+".vNumber")`;
setAttr ($nameOfTess[0]+".vNumber") ($originalEdgeNumber+$newEdgesLength);
}
select $selectedBranch;
}

//Change Branch Taper
proc changeBranchTaper(float $taperAmmount)
{
string $selectedBranch[]=`ls -sl`;

for ($u=0; $u<size($selectedBranch); $u++)
{
string $branchNumber[];
tokenize $selectedBranch[$u] "_" $branchNumber;
string $nameOfInput[] = `listConnections ("nurbsCircle_"+($branchNumber[0]+"_Shape"+$branchNumber[1]))`;
setAttr ($nameOfInput[1]+".scale") $taperAmmount;
}

select $selectedBranch;
}

// Select nurbs Circle and select rotation pivot
proc selectRotationAxisofCircle()
{
string $selectedBranch[]=`ls -sl`;
string $circleSelected = ("nurbsCircle_"+$selectedBranch[0]);
if (`objExists $circleSelected`){
	select ("nurbsCircle_"+$selectedBranch[0]);	
	RotateTool;
 } else {
	warning("Select the Polygon you want to adjust");
 }
}

// Select nurbs Circle and select scale pivot
proc selectScaleAxisofCircle()
{
string $selectedBranch[]=`ls -sl`;
string $circleSelected = ("nurbsCircle_"+$selectedBranch[0]);
if (`objExists $circleSelected`){
	select ("nurbsCircle_"+$selectedBranch[0]);	
	ScaleTool;
 } else {
	warning("Select the Polygon you want to adjust");
 }
}

// View curve Control Vertex
proc selectControlVertex()
{
string $selectedBranch[]=`ls -sl`;

showHidden ("group_"+$selectedBranch[0]);
hide ("referenceTube_"+$selectedBranch[0]);
hide ("nurbsCircle_"+$selectedBranch[0]);
changeSelectMode -component;
hilite -r ("curve_"+$selectedBranch[0]);
}

// Return to PolyView
proc endAdjustment()
{
string $selectedCV[]=`ls -sl`;
string $selectedNameCV[];
tokenize $selectedCV[0] "." $selectedNameCV;
string $selectedName[];
tokenize $selectedNameCV[0] "_" $selectedName;

string $selectedBranch = ($selectedName[1]+"_"+$selectedName[2]);

showHidden ("referenceTube_"+$selectedBranch);
showHidden ("nurbsCircle_"+$selectedBranch);
hide ("group_"+$selectedBranch);

select $selectedBranch;
changeSelectMode -object;
}

// Delete History
proc DeleteHistory()
{
DeleteHistory;
}

//Delete referenceTube and nurbsCircle
proc DeleteTubeCircle()
{
string $selectedBranch[]=`ls -sl`;

for ($u=0; $u<size($selectedBranch); $u++)
{
delete ("referenceTube_"+$selectedBranch[$u]);
delete ("nurbsCircle_"+$selectedBranch[$u]);
}
}

//Delete curve and group
proc DeleteCurveGroup()
{
string $selectedBranch[]=`ls -sl`;

for ($u=0; $u<size($selectedBranch); $u++)
{
delete ("curve_"+$selectedBranch[$u]);
delete ("group_"+$selectedBranch[$u]);
}
}