PDA

View Full Version : collecting vertex coordinates on a specified axis


inverse catheter
03-29-2006, 12:04 AM
.

given a selection of verts .i 'm wanting to capture their coordinates on a particular axis ( in this case .x ) and stack them to an array .here 's the guts of the procedure .without adding the collected data to an array .just printing the result :

//
string $sel[] = `ls -sl`;
string $vertInSelection;
vector $coords;
//
for ($i = 0;$i < size ($sel);$i ++)
{
//
$vertInSelection = $sel[$i];
$coords = `xform -ws -q -t $vertInSelection`;
//
print ($coords.x + "\n");
//
}

if there are only a few elements in the selection .it works fine .too many and i get this :

// Error: line 10: Cannot convert data of type float[] to type vector due to array size mismatch (9 elements). //

.

my understanding of MEL is pretty weak .so this is very much a learning exercise .i can 't make any sense of the error i 'm getting .anybody able to shed some light on this ?

john_homer
03-29-2006, 12:52 AM
its probably just a coincidence that when you select a larger number there are some verts in sequence.. (ie it doesn't fail because of the number of verts selected)
you need to list the selection with the -flatten flag..
so you get..
// Result: pSphere1.vtx[0] pSphere1.vtx[1] pSphere1.vtx[2] pSphere1.vtx[3] pSphere1.vtx[4] pSphere1.vtx[5] pSphere1.vtx[6] pSphere1.vtx[7] pSphere1.vtx[8] pSphere1.vtx[9] pSphere1.vtx[10] pSphere1.vtx[11] pSphere1.vtx[12] pSphere1.vtx[13]//

rather than...
// Result: pSphere1.vtx[0:13] //
you cant query the position of pSphere1.vtx[0:13] because thats 14 verts.

here, try this...

string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;

for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
print ($coords.x + "\n");
}

.j

inverse catheter
03-29-2006, 01:39 AM
.

john .you 're a handy guy to have around .thanks

gotta get the world checkbox working .and put a float field in to define an explicit position .but the bulk of my little script 's all good .likely something similar floating around highend .but i needed to work on my MELs so .flatten verts .an extension of a feature found in Silo .and far more convenient than going into the component editor to perform the same

again .thanks a .i would 've dicked around for far too long trying to figure it on my own

.

it 's pretty dirty .and could be sleeked out i 'm sure .but as is .here 's the script . :

//
//
global proc flatVertsX ()
{
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;
float $vertsInArray[];
float $sum = 0;
float $moveTo;
int $i;
//
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$i++;
$vertsInArray[$i] = $coords.x;
$sum = $sum + $vertsInArray[$i];
//
}
$moveTo = ($sum / size ($sel));
move -x $moveTo;
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + $moveTo + " << IN .X \n");
}
//
global proc flatVertsY ()
{
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;
float $vertsInArray[];
float $sum = 0;
float $moveTo;
int $i;
//
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$i++;
$vertsInArray[$i] = $coords.y;
$sum = $sum + $vertsInArray[$i];
//
}
$moveTo = ($sum / size ($sel));
move -y $moveTo;
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + $moveTo + " << IN .Y \n");
}
global proc flatVertsZ ()
{
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;
float $vertsInArray[];
float $sum = 0;
float $moveTo;
int $i;
//
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$i++;
$vertsInArray[$i] = $coords.z;
$sum = $sum + $vertsInArray[$i];
//
}
$moveTo = ($sum / size ($sel));
move -z $moveTo;
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + $moveTo + " << IN .Z \n");
}
//
//
//
global proc jkbAverageVerts ()
{
if (`window -q -ex jkbAVertswindow`)
//
deleteUI jkbAVertswindow;
//
window -w 100 -h 100 -title " jkb FLATTEN VERTS" jkbAVertswindow;
rowColumnLayout -nc 2 -cw 1 100 -cw 2 100;
//
//
button -label "X" -c "flatVertsX" xButton;
text ".";
button -label "Y" -c "flatVertsY" yButton;
text ".";
button -label "Z" -c "flatVertsZ" zButton;
text ".";
text ".";
checkBox -label "WORLD .0" worldCheckBox;
text ".";
text ".";
//
//
showWindow jkbAVertswindow;
//
}
jkbAverageVerts;
//

john_homer
03-29-2006, 02:58 AM
to make it a little cleaner...
i would recommend having 1 proc and passing it and arguement rather than 3 global procs to do the same thing in different axis..

here is what I mean in non-cody-code..

global proc scaleEm (float $x, float $y, float $z)
{
find the average position in all axis...then scale with the average as the pivot
scale -r -p averageX averageY averageZ $x $y $z;
}

window...
button -l scaleX -c "scaleEm 0 1 1";
button -l scaleY -c "scaleEm 1 0 1";
button -l scaleZ -c "scaleEm 1 1 0";

inverse catheter
03-30-2006, 01:55 AM
.


john .i had a play with what you suggested .modified it a little since using the scaling method was a bit weird .basically .in non cody code

if($x == 1)
{
$axis = "x";
}
//
.
etc

move $axis THEAVERAGEOFWHATEVER;

this works .to a point .'cause the only difference between the three procs is the axis on which they average .you provided me that difference with the sent arguments ( i altered them a little too ) .where i 'm getting bugs is combining all the strings .i know i 'm not writing them right .but don 't know where / how i 'm wrong

the only two differences in the proc are :

$coords.x ( for example .or $coords.? ) becomes .what i 'm writing ( $coords + $userDef ) where string $userDef = ("." + $axis) ;

and move ( "-" + $axis ) THEAVERAGEOFWHATEVER;

this doesn 't quite work as i 'd expect .yeah .i 'm a mess .anyway .giving me a headache .but i 'll keep trying .thanks for your help

here 's the old .with the world float working :


//
//
global proc flatVertsX ()
{
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;
float $vertsInArray[];
float $sum = 0;
float $moveTo;
int $i;
//
string $qFloat = `checkBox -q -v worldCheckBox`;
float $doFloat = `floatField -q -v absPosition`;
//
//print $qFloat;
//print $doFloat;
//
if($qFloat == 1)
{
move -x $doFloat;
}
else
{
//
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$i++;
$vertsInArray[$i] = $coords.x;
$sum = $sum + $vertsInArray[$i];
//
}
$moveTo = ($sum / size ($sel));
move -x $moveTo;
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + $moveTo + " << IN .X \n");
}
}
//
global proc flatVertsY ()
{
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;
float $vertsInArray[];
float $sum = 0;
float $moveTo;
int $i;
//
string $qFloat = `checkBox -q -v worldCheckBox`;
float $doFloat = `floatField -q -v absPosition`;
//
//print $qFloat;
//print $doFloat;
//
if($qFloat == 1)
{
move -y $doFloat;
}
else
{
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$i++;
$vertsInArray[$i] = $coords.y;
$sum = $sum + $vertsInArray[$i];
//
}
$moveTo = ($sum / size ($sel));
move -y $moveTo;
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + $moveTo + " << IN .Y \n");
}
}
global proc flatVertsZ ()
{
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;
float $vertsInArray[];
float $sum = 0;
float $moveTo;
int $i;
//
string $qFloat = `checkBox -q -v worldCheckBox`;
float $doFloat = `floatField -q -v absPosition`;
//
//print $qFloat;
//print $doFloat;
//
if($qFloat == 1)
{
move -z $doFloat;
}
else
{
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$i++;
$vertsInArray[$i] = $coords.z;
$sum = $sum + $vertsInArray[$i];
//
}
$moveTo = ($sum / size ($sel));
move -z $moveTo;
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + $moveTo + " << IN .Z \n");
}
}
//
//
//
global proc enableFloat ()
{
//
int $chckValue = `checkBox -q -v worldCheckBox`;
//
if($chckValue == 1)
{
floatField -e -en true absPosition;
print "GO WORLD .YEAH!\n";
}
else
{
floatField -e -en false absPosition;
print "GO LOCAL .OK\n";
}
//
}
//
global proc chirp ()
{
print "CHEEP CHEEP\n";
}
//
global proc jkbAverageVerts ()
{
if (`window -q -ex jkbAVertswindow`)
//
deleteUI jkbAVertswindow;
//
window -w 100 -h 100 -title " jkb FLATTEN VERTICLES" jkbAVertswindow;
rowColumnLayout -nc 2 -cw 1 100 -cw 2 100;
//
//
checkBox -label "WORLD ." -cc "enableFloat" worldCheckBox;
floatField -en false absPosition;
text ".";
separator;
separator;
separator;
separator;
button -label "X" -c "flatVertsX" xButton;
text ".";
button -label "Y" -c "flatVertsY" yButton;
text ".";
button -label "Z" -c "flatVertsZ" zButton;
text ".";
text ".";
text ".";
text ".";
separator;
separator;
text ".";
button -label "{' ^ '}" -bgc 1 1 1 -c "chirp" absButton;
text ".";
text ".";
text ".";
separator;
separator;
//
//
showWindow jkbAVertswindow;
//
}
jkbAverageVerts;
//

john_homer
03-30-2006, 09:04 PM
here you go.. basically just went through and optimized it.. and did some things my way ;)
mainly got rid of the vectors.. easier to deal with arrays..
and removed about half a dozen unnecessary variables...

hope that helps you.. was sorta rushed.. but it seems to work.

.j

global proc flatVerts (int $axis) // args: 0=x 1=y 2=z
{
string $sel[] = `ls -sl -flatten`;
float $coords[], $sum;
string $direction;
if ($axis == 0) // set the axis flag
$direction = "x";
else if ($axis == 1)
$direction = "y";
else
$direction = "z";

float $doFloat = `floatField -q -v absPosition`;

if(`checkBox -q -v worldCheckBox`) // modified this, to not need the variable $qFloat, removed the line above that declared $qFloat
{
move -x $doFloat;
}
else
{
for ($current in $sel)
{
$coords = `xform -ws -q -t $current`;
$sum += $coords[$axis]; // add up the values of all verts for required axis
}

move ("-" + $direction) ($sum / size($sel)); // move selection to the average position
print ("AVERAGED > " + size ($sel)+ " < VERTICES TO >> " + ($sum / size($sel) + " << IN ." + $direction + "\n"));
}
}

global proc enableFloat ()
{
int $chckValue = `checkBox -q -v worldCheckBox`;
if($chckValue == 1)
{
floatField -e -en true absPosition;
print "GO WORLD .YEAH!\n";
}
else
{
floatField -e -en false absPosition;
print "GO LOCAL .OK\n";
}
}

global proc chirp ()
{
print "CHEEP CHEEP\n";
}


global proc jkbAverageVerts ()
{
if (`window -q -ex jkbAVertswindow`)
deleteUI jkbAVertswindow;

window -w 100 -h 100 -title " jkb FLATTEN VERTICLES" jkbAVertswindow;
rowColumnLayout -nc 2 -cw 1 100 -cw 2 100;
checkBox -label "WORLD ." -cc "enableFloat" worldCheckBox;
floatField -en false absPosition;
text ".";
separator;
separator;
separator;
separator;
button -label "X" -c "flatVerts 0" xButton;
text ".";
button -label "Y" -c "flatVerts 1" yButton;
text ".";
button -label "Z" -c "flatVerts 2" zButton;
text ".";
text ".";
text ".";
text ".";
separator;
separator;
text ".";
button -label "{' ^ '}" -bgc 1 1 1 -c "chirp" absButton;
text ".";
text ".";
text ".";
separator;
separator;
showWindow jkbAVertswindow;
}

inverse catheter
03-30-2006, 11:18 PM
yeah .works just fine .i had to fix one little thing ( move -x $doFloat to move ( "-" + $direction ) ) .but .no bother .while the behaviour is the same as what i had going .it 's a real help to see how a ' pro ' would handle the problem .and provide a cleaner solution .i 've learned a bunch from this little exercise .and came out with a tool which is a great help to me in my modelling .thanks john .thanks heaps

.

CGTalk Moderation
03-30-2006, 11:18 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.