Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Reversing the order of a string array?

2 REPLIES 2
Reply
Message 1 of 3
tonthebone
924 Views, 2 Replies

Reversing the order of a string array?

Hi,

I'm trying to reverse the order of a string array. Example:

//select all the faces in a sphere .

select -r pSphere1.f ;

//for loop that prints the selected faces in order 0 to total # of faces

string $selected[] = `ls -selection -fl `;

for ($object in $selected)
{
print $object ;
}

This will print in ascending order, so $selected = pShere1.f , $selected = pShere1.f, etc.

What I want is to flip that. So that $selected = pShere1.f, $selected = pShere1.f, etc.

The actual number of faces selected is volatile, 20 or 2000 or whatever. I tried selecting the faces in opposite order

select -r pSphere1.f ;

but that won't work.

Any suggestions?

Thanks!
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: tonthebone

You don't really have an array of faces, you have a selection of faces.
Maya knows how to iterate over the selection, but if you want to do fancy things to the ordering, you're going to need to have a real array and you may want to try this in Python, since the lists are quite nice and easy to iterate over in either direction.
Below is MEL, that starts with your selection, iterates over the selecton and adds it into a real array starting from the end.
Come to think of it, you probably could just print out or use the directionality described here to builld the $reallySelected array, but you might want a realArray for some other purpose...

string $selected[] = `ls -selection -fl `;
string $reallySelected[];
int $i = `size( $selected)`;
for ($object in $selected)
{
$reallySelected = $object;
$i--;
}

for ($object in $reallySelected)
{
print $object;
print "/n";
}
Message 3 of 3
weshowe
in reply to: tonthebone

A simple way would be to invert the order you read the items from the array. If you use a variable

int $n;

as your iterator, then you could write:


string $reversed[];
for($n=0; $n<size($selected); ++$n) {
$reversed=$selected;
}

will put your items in reversed order. Like all array work, you would just need to make sure that you stayed in the array bounds (as the for loop above does). Of course, you could just leave them all in the original array and use the math in the indexing statement to retrieve them in reverse order.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report