Hamburger

10 October 2011, 02: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?

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?