aa_turtleThis is a featured page

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

/*
***************************************************************************
This is a library of functions to perform "turtle" functions, or Logo style
relative drawing functions. Functions in this library include
aa_turtle_newturtle(string $args)
Creates new turtle object, returns name of turtle object to be used in consequent functions
The turtle can be a drawing or non drawing turtle
Here are the following possible arguments:
"CURVE" - draws a 3 degree curve while moving
"LINE" - draws a 1 degree curve
"CURVEONSURFACE" - draws a 3 degree curve on sruface. IMPORTANT: a surface must be
currently selected when the aa_turtle_newturtle("CURVEONSURFACE") command is envoked
"NULL" or "" - does not draw anything.
aa_turtle_getPosition(string $thisturtle)
Gets the position of a turtle passed as an argument
Returns a float array of x,y and z positions
aa_turtle_setPosition(string $thisturtle,vector $position)
Sets the position of a turtle
aa_turtle_setDirection(string $thisturtle,int $azimuth,int $altitude)
Sets the direction of the turtle in azimuth and altitude
aa_turtle_getDirection(string $thisturtle)
Gets the direction of the turtle, returns a float array ofazimuth and altitude, in radians
aa_turtle_getRotation(string $thisturtle)
Gets the direction of the turtle, returns a float array ofazimuth and altitude, in degrees.
Suitable for using in the 'rotate' command
aa_turtle_move(string $thisturtle,vector $distance)
Moves a turtle "foward" relative to the last position and current direction
aa_turtle_turn(string $thisturtle,int $azimuth,int $altitude)
Turns a turtle specified azimuth and altitude based on current direction.
aa_turtle_snapshotAtKeyframe(string $thisturtle,int $frame)
Snapshots the entire current curve (if you are drawing with the turtle), at the
specified keyframe. To snapshot the curve it duplicates it and keyframes it as hidden
untile frame number 'keyframe' and then unhides it, and then re-hides it immediately after
keyframe.
aa_turtle_getCurve($thisturtle);
Returns the name of the turtle curve for use in loft functions and isInside

aa_turtle_setModelCommand($thisturtle, $commandName);
sets a functionto be called with each call of the aa_turtle_move command.
The function that is set here should take only one argument and that is length.
aa_turtle_getModel($thisturtle);
Returns the name of the turtle model
aa_turtle_close($thisturtle);
Closes the curve
aa_turtle_done($thisturtle);
Cleans up any resources the turtle is using but does not delete the curve.



example 1:
//set up start point
$myturtle = aa_turtle_newturtle("CURVE");
$vector = <<1,2,3>>;
aa_turtle_setPostion($myturtle,$vector);
aa_turtle_turn($myturtle,40,40);
aa_turtle_move($myturtle,10);
example 2:

$spiral = aa_turtle_newturtle("CURVE");
$i = 1;
aa_turtle_setDirection($spiral,0,20);
while ($i < 100) {
// Go forward and turn
aa_turtle_move($spiral,($i/10));
//to have this function animate as it grows, uncomment the line below to 'snapshot'
//aa_turtle_snapshotAtKeyframe($spiral,$i);
aa_turtle_turn($spiral,10,0);
// Increase Counter
$i = $i + 1;
}
example 3:
//create a sphere
sphere -s 20 -nsp 20;
//draw a curve on its surface
$turtle = aa_turtle_newturtle("CURVEONSURFACE");
aa_turtle_turn($turtle,45,0);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);
aa_turtle_move($turtle,5);



aa_turtle
Cory Clarke - cory@nthd.org
***************************************************************************
*/
global proc aa_turtle_done(string $args){
delete $args;
}
global proc string aa_turtle_newturtle(string $args){
//cue anything selected
$object = `ls -sl`;
string $thisgroup = `group -em`;

//add the line attribute
addAttr -ln "turtlecommand" -dt "string";
setAttr ($thisgroup +".turtlecommand") -type "string" "none";
addAttr -ln "turtlemodel" -dt "string";
setAttr ($thisgroup +".turtlemodel") -type "string" "none";
$statement = "addAttr -ln turtlecurve -at short |" + $thisgroup;
eval($statement);

if($args == "CURVE"){
$statement = "setAttr " + $thisgroup + ".turtlecurve 3";
eval($statement);
$statement = "addAttr -ln turtlecurvename -dt \"string\" |" + $thisgroup;
eval($statement);
}
else if($args == "LINE"){
$statement = "setAttr " + $thisgroup + ".turtlecurve 1";
eval($statement);
$statement = "addAttr -ln turtlecurvename -dt \"string\" |" + $thisgroup;
eval($statement);
}else if($args == "CURVEONSURFACE"){
$statement = "setAttr " + $thisgroup + ".turtlecurve -1";
eval($statement);
$statement = "addAttr -ln turtlecurvename -dt \"string\" |" + $thisgroup;
eval($statement);
print($object[0]);
makeLive $object[0];
$statement = "addAttr -ln surfacename -dt \"string\" |" + $thisgroup;
eval($statement);

$statement = "setAttr " + $thisgroup + ".surfacename -type \"string\" " + $object[0];
eval($statement);
}

return $thisgroup;


};
global proc aa_turtle_setModelCommand(string $thisturtle, string $command) {
$model = `group -em`;
setAttr ($thisturtle +".turtlecommand") -type "string" $command;
setAttr ($thisturtle +".turtlemodel") -type "string" $model;
}
global proc string aa_turtle_getModel(string $thisturtle) {
$model = getAttr ($thisturtle +".turtlemodel") ;
return $model;
}


global proc float[] aa_turtle_getPosition(string $thisturtle){
//if drawing curve
$statement = "getAttr " + $thisturtle + ".turtlecurve";

$bool = eval($statement);

float $thistmp[];
if($bool == -1){
//curve on surface
$curve = aa_turtle_getCurve($thisturtle);
select $curve;
$num = `getAttr ".spans"`;
$cmd = "xform -ws -q -t " + $curve + ".controlPoints[" + $num + "]";
$thistmp = eval ($cmd);
return $thistmp;
}else{
float $thistmp[];
string $statement = "getAttr " + $thisturtle + ".translate;";
//print($statement);
//$thistmp = eval($statement);
$thistmp = `getAttr ($thisturtle + ".translate")`;
//print($statement);
//print($thistmp);
return $thistmp;
}

return $thistmp;

}
global proc string aa_turtle_getCurve(string $thisturtle){
string $curve;
string $statement;
$statement = "getAttr " + $thisturtle + ".turtlecurve";
$bool = eval($statement);
if($bool != 0){
$statement = "getAttr " + $thisturtle + ".turtlecurvename";
string $curve;
$curve = eval($statement);
return $curve;
}
return $curve;
}

global proc aa_turtle_snapshotAtKeyframe(string $thisturtle,int $keyframe){
string $attcommand;
string $keycommand;
string $newturtle[];
//if drawing curve
$statement = "getAttr " + $thisturtle + ".turtlecurve";
$bool = eval($statement);
if($bool == 1){
$statement = "getAttr " + $thisturtle + ".turtlecurvename";
string $curve;
$curve = eval($statement);
$length = size($curve);
//duplicate current turtle
$newturtle = `duplicate $curve`;

$keycommand = "setKeyframe " + $newturtle[0] + ".visibility";

//keyframe off at 1
currentTime 1;
$attcommand = "setAttr " + $newturtle[0] + ".visibility off";
eval($attcommand);
eval($keycommand);
//set on at current
currentTime $keyframe;
$attcommand = "setAttr " + $newturtle[0] + ".visibility on";
eval($attcommand);
eval($keycommand);
//set back off after current
$keyframe = $keyframe + 1;
currentTime $keyframe;
$command = "setAttr " + $newturtle[0] + ".visibility off";
eval($attcommand);
eval($keycommand);
}
}
global proc aa_turtle_setPosition(string $thisturtle,vector $position){

float $thistmp;
string $statement = "setAttr " + $thisturtle + ".translate " + $position.x + " " + $position.y + " " + $position.z;
//print($statement + "\n");
eval($statement);
//if drawing curve
$statement = "getAttr " + $thisturtle + ".turtlecurve";

$bool = eval($statement);
if($bool > 0){
$statement = "getAttr " + $thisturtle + ".turtlecurvename";
string $curve;
$curve = eval($statement);
$length = size($curve);

if($curve == ""){
//create curve at point
$statement = "curve -d " + $bool + " -p " + $position.x + " " + $position.y + " " + $position.z;
$curve = eval($statement);
$statement = "setAttr " + $thisturtle + ".turtlecurvename -type \"string\" " + $curve;
eval($statement);
}else{
//add a point
$statement = "curve -a -p " + $position.x + " " + $position.y + " " + $position.z + " " + $curve;
eval($statement);

}

}else if($bool == -1){
//curve on surface
$statement = "getAttr " + $thisturtle + ".turtlecurvename";
string $curve;
$curve = eval($statement);
$length = size($curve);
$statement = "getAttr " + $thisturtle + ".surfacename";
string $surface;
$surface = eval($statement);

if($curve == ""){
//create curve at point
$statement = "curveOnSurface -d 3 -uv " + $position.x + " " + $position.z + " " + $surface;
//print($statement);
//$curve = eval($statement);
$statement = "setAttr " + $thisturtle + ".turtlecurvename -type \"string\" " + $curve;
//print($statement);
eval($statement);

//make the surface not live so it is unlive after running the turtle
makeLive;
}else{
//add a point
$statement = "curveOnSurface -a -uv " + $position.x + " " + $position.z + " " + $curve;
eval($statement);

}


}

}

global proc aa_turtle_setDirection(string $thisturtle,float $azimuth,float $altitude){

float $thistmp;
float $raz;
float $ralt;
$statement = "deg_to_rad " + $azimuth;
$raz = eval($statement);

$statement = "deg_to_rad " + $altitude;
$ralt = eval($statement);
string $statement = "setAttr " + $thisturtle + ".rotate " + $raz + " " + $ralt + " 0";
eval($statement);

}


global proc float[] aa_turtle_getDirection(string $thisturtle){
float $thistmp[];
string $statement = "getAttr " + $thisturtle + ".rotate;";
//print($statement);
$thistmp = eval($statement);
//print($statement);
//$thistmp[1] = $thistmp[1] * -1;
return $thistmp;


}
global proc float[] aa_turtle_getRotation(string $thisturtle){
float $thistmp[];
string $statement = "getAttr " + $thisturtle + ".rotate;";
//print($statement);
$thistmp = eval($statement);
//print($statement);
//$thistmp[1] = $thistmp[1] * -1;
$thistmp[0] = rad_to_deg($thistmp[0]);
$thistmp[1] = rad_to_deg($thistmp[1]);
return $thistmp;


}

global proc aa_turtle_move(string $thisturtle,float $distance){

float $thistmpp[];
$thistmpp = aa_turtle_getPosition($thisturtle);
float $thistmpd[];
$thistmpd = aa_turtle_getDirection($thisturtle);
//print($thistmpd);

// if the user has specified a command to execute along each move
// call it now
$command = `getAttr ($thisturtle + ".turtlecommand")`;
if ($command != "none")
{
$cmd = $command + "(" + $distance + ")";
$usermodel = eval($cmd);
select $usermodel;

move $thistmpp[0] $thistmpp[1] $thistmpp[2];

$roty = -1 * rad_to_deg($thistmpd[0]);
rotate 0 $roty 0;
$turtlemodel = getAttr ($thisturtle + ".turtlemodel");
parent $usermodel $turtlemodel;
}

$movx = $distance * cos($thistmpd[0]) * cos($thistmpd[1]);
$movy = $distance * sin($thistmpd[1]) * -1;
$movz = $distance * cos($thistmpd[1]) * sin($thistmpd[0]);
//print($movz + "\n");
vector $v = <<$movx+$thistmpp[0],$movy+$thistmpp[1],$movz+$thistmpp[2]>>;
//aa_turtle_setPosition($thisturtle,$v);
string $statement = "setAttr " + $thisturtle + ".translate " + $v.x + " " + $v.y + " " + $v.z;
//print($statement + "\n");
eval($statement);
//if drawing curve
$statement = "getAttr " + $thisturtle + ".turtlecurve";
$bool = eval($statement);
if($bool > 0){
$statement = "getAttr " + $thisturtle + ".turtlecurvename";
string $curve;
$curve = eval($statement);
$length = size($curve);
if($curve == ""){
//create curve at point
$statement = "curve -d " + $bool + " -p " + $v.x + " " + $v.y + " " + $v.z;
//print("-");
//print($statement + "\n");
$curve = eval($statement);
$statement = "setAttr " + $thisturtle + ".turtlecurvename -type \"string\" " + $curve;
eval($statement);
}else{
//add a point
$statement = "curve -a -p " + $v.x + " " + $v.y + " " + $v.z + " " + $curve;
//print($statement + "\n");
eval($statement);

}
//print($statement + "\n");
}else if($bool == -1){
//curve on surface
$statement = "getAttr " + $thisturtle + ".turtlecurvename";
string $curve;
$curve = eval($statement);
$length = size($curve);
$statement = "getAttr " + $thisturtle + ".surfacename";
string $surface;
$surface = eval($statement);

if($curve == ""){
//create curve at point
$statement = "curveOnSurface -d 3 -uv " + $v.x + " " + $v.z + " " + $surface;
$curve = eval($statement);

$statement = "setAttr " + $thisturtle + ".turtlecurvename -type \"string\" " + $curve;
//print($statement);
eval($statement);
//make the surface not live so it is unlive after running the turtle commands
makeLive;
}else{
//add a point
$statement = "curveOnSurface -a -uv " + $v.x + " " + $v.z + " " + $curve;
eval($statement);
//print($statement);

}


}



};

global proc aa_turtle_turn(string $thisturtle,float $azimuth,float $altitude){
float $thistmp[];
string $statement = "getAttr " + $thisturtle + ".rotate;";
$thistmp = eval($statement);
float $daz;
float $dalt;
//print("starting at " + $thistmp[0] + " " + $thistmp[1] + "\n");
//print("turning " + $azimuth + " " + $altitude + "\n");
$statement = "deg_to_rad " + $azimuth;
//print($statement + "\n");
$daz = eval($statement);
$statement = "deg_to_rad " + $altitude;
//print($statement + "\n");
$dalt = eval($statement);
$newaz = $thistmp[0] + $daz;
$newalt = $thistmp[1] + $dalt;
string $statement = "setAttr " + $thisturtle + ".rotate " + $newaz + " " + $newalt + " 0";
eval($statement);
};
global proc aa_turtle_close( string $thisturtle ) {
string $curve;
$curve = aa_turtle_getCurve( $thisturtle );
print ("--" + $curve);
// closeCurve($curve);
closeCurve -ch 1 -ps 1 -rpo 1 -bb 0.5 -bki 0 -p 0.1 $curve;
};


print("aa_turtle....loaded\n");


gilakos
gilakos
Latest page update: made by gilakos , Oct 4 2006, 11:17 AM EDT (about this update About This Update gilakos Edited by gilakos

2 words added

view changes

- complete history)
Keyword tags: None (edit keyword tags)
More Info: links to this page

Anonymous  (Get credit for your thread)


There are no threads for this page.  Be the first to start a new thread.