PDA

View Full Version : selecting object names in array???


swardson
09-23-2005, 09:05 PM
hey y'all,

I have this simple script that reads in a list of objects in a text file. I then want to select each of the objects in that list. But maya is telling me that the objects dont exist. But they do.

I am totally stuck as to what the issue is.

here is the script.... what am I doing wrong??

*******************************************************

global proc readSelList (string $shader){

string $filePath = ("/Users/Brad/Desktop/" + $shader);

$listFileID = `fopen $filePath "r"`;

string $nextLine = `fgetline $listFileID`;
string $selList[];
int $i = 0;

while (size ($nextLine) > 0){
$selList[$i] = $nextLine;
$nextLine = `fgetline $listFileID`;
//print $selList[$i];
$i++;
}
fclose $listFileID;

select -clear;
for ($r=0; $r < size($selList); $r++){

select -add $selList[$r];
//print ($selList[$r]);
}
clear $selList;
}

swardson
09-23-2005, 09:09 PM
nevermind.

I just realized that the line I was reading in had white space in the end.

I just stripped the string when written and all is well.

see yah,
Brad

Robert Bateman
09-26-2005, 08:29 PM
of course, you could make life a little easier...

global proc readSelList (string $shader){

string $filePath = ("/Users/Brad/Desktop/" + $shader);

$listFileID = `fopen $filePath "r"`;

// check file opened ok, return if failed.
if($listFileID==0)
return;

string $selList[];

// while not at end of file
//
while ( !feof($listFileID) )
{
// you could use fgetword here since it ignore white space
// - i assume you can't have object names with spaces in (because you can't) ;)
//
$nextWord = `fgetword $listFileID`;

// check the size before adding to array. fgetword will always execute once
// before the end of a file and return "" (ie, a newline, space etc after last text).
// also don't need a loop counter since the size of the array gives you nice
// generic way of appending data to arrays.
//
if(size($nextWord))
$selList[ size($selList) ] = $nextWord ;
}
fclose $listFileID;

// select all items in array, replacing the current selection list.
// handily enough, mel is happy to recieve an array as an argument,
// which allows you to do things like :
//
// select -r `ls -type mesh`;
//
// (ls returns an array of strings)
//
select -replace $selList;

//
// you'd only need to clear the array if it's a global variable. Here, you
// can forget about it because it will be destroyed when the function exits.
//
}

as a final note, if you need to walk through each item in an array in mel, it's nicer to use for in (and it's a bit more efficient...) so,

for ($r=0; $r < size($selList); $r++)
{
print ($selList[$r] + "\n");
}

becomes :

for ( $item in $selList )
{
print ($item + "\n");
}

CGTalk Moderation
09-26-2005, 08:29 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.