PDA

View Full Version : Edge Length Script - need to add total length!


Hamburger
10-26-2011, 03:03 AM
Hi all, I've cobbled and hacked together a script which maybe of use to someone here in its current state.

It simply returns selected edge length to the script editor in both millimetres and metres with rounded numbers to two decimal places making it nice and clean.

just call edgeLength. Also - this will only work in default cms as that's all I ever use but if anyone else wants to add their own conversions in there it should be possible with a few extra lines.

proc float edgeCalc(string $edge)
{

//WIP

float $distance;
string $unit = `currentUnit -q -l`;
select -r `polyListComponentConversion -tv`;
string $connectedVerts[] = `filterExpand -ex 1 -sm 31`;
int $conversion;

if ($unit == "cm")
{
$conversion = "10";
}

vector $PT1 = `pointPosition -w $connectedVerts[0]`;
vector $PT2 = `pointPosition -w $connectedVerts[1]`;
string $edges[]= `filterExpand -ex 1 -sm 32`;
float $distance = mag(($PT1-$PT2)) * $conversion;
float $rounded_mm = `roundoff $distance 0`;
float $rounded_m = `roundoff (($distance)/1000) 2`;

print ($rounded_mm + " mm / " +$rounded_m+ " m\n");

return $distance;

}

global proc edgeLength(){

string $edges[]= `filterExpand -ex 1 -sm 32`;
float $edgeLength[];

//save edgelength to var for later
for($i=0;$i<`size $edges`;$i++)
$edgeLength[$i] = edgeCalc($edges[$i]);

select -r $edges;

}

It requires this extra proc below - I can't remember who wrote it? I think someone on this forum years ago.

global proc float roundoff( float $f, int $n )
{
// we divide if n < 0 to avoid numeric
// precision problems
if( $n > 0 )
{
float $roundScale = pow(10,$n);
if( $f > 0 )
return( ((float)(int)($f * $roundScale + 0.5)) /$roundScale );
else
return( ((float)(int)($f * $roundScale - 0.5)) /$roundScale );
}
else
{
float $roundScale = pow(10,-$n);
if( $f > 0 )
return( ((float)(int)($f/$roundScale + 0.5)) *$roundScale );
else
return( ((float)(int)($f/$roundScale - 0.5)) *$roundScale );
}

}

Anyway, with regards to the first script it successfully calculates, but only one edge at a time. I'm struggling to find where to put the loop in order for it to calculate multiple edges and get a string to add the results together to get a total edge length area.

Would anyone have any insight to this?

Azrail
10-26-2011, 10:11 AM
You already have the loop you need I think, so you just have to sum up the results you get for each edge... something like this:

global proc edgeLength(){

string $edges[]= `filterExpand -ex 1 -sm 32`;
float $edgeLength[];
float $totalLength=0; // <= Init your sum variable
//save edgelength to var for later
for($i=0;$i<`size $edges`;$i++)
$edgeLength[$i] = edgeCalc($edges[$i]);
$totalLength += $edgeLength[$i]; // <= Add this length to the sum
select -r $edges;
}

Hamburger
10-27-2011, 04:47 AM
Thanks, but I'm not sure what you mean, as this kind of scripting with "returns" and float procs are kind of foreign to me.

I've added those lines and at the moment of trying to print out the value of totalLength it still seems to equal a value of 0.

Anyway, thanks for the pointer though - i'll get it eventually. :)

Azrail
10-27-2011, 09:38 AM
Now that I tested it even the original code was not working with more than one edge selected - it always returns the length of the first edge, so here is how it should work:

global proc edgeLength()
{
string $edges[]= `filterExpand -ex 1 -sm 32`;
float $edgeLength[];
float $totalLength=0; // <= Init your sum variable
//save edgelength to var for later
for($i=0;$i<`size $edges`;$i++)
{
select -r $edges[$i]; // Select only one edge
$edgeLength[$i] = edgeCalc($edges[$i]);
$totalLength += $edgeLength[$i]; // <= Add this length to the sum
print ($edges[$i] + ": " + $edgeLength[$i] + "\n");
}
print ("Sum: " + $totalLength + "\n");
select -r $edges; // restore previous selection
}


The only modifications are in the edgeLength() procedure.

Hamburger
10-27-2011, 11:40 PM
THanks for all your help, it works perfectly now and I understand it much better! (Most of the edgeCalc I added is redundant). :beer:

CGTalk Moderation
10-27-2011, 11:40 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.