PDA

View Full Version : Mel Questions, filter through objects, declaring variables


ah-fx
04-18-2009, 12:22 AM
Hey everyone I have a couple of mel questions, I am trying to figure out an effective way to filter though objects. For example if I wanted to filter through active rigidBodies in the scene:

string $rigidBodies[] = `ls -type rigidBody`;
string $aRigidBodies[];

if (size($rigidBodies) > 0) {
for ($rc = 0; $rc <= size($rigidBodies); $rc++) {

$active = getAttr($rigidBodies[$rc] + ".active");

if ($active) {
$aRigidBodies[$rc] = $rigidBodies[$rc];
}
}

print($aRigidBodies);
}

To me that should work but I keep getting: // Error: No object matches name: .active //
can anyone give me some pointers here.

Also how can I clear variables I have declared?
If im working in the script editor and I accidentally declare a variable the wrong type,
ie: int $myArray; instead of int $myArray[];
I would like to be able to rerun the script w/o getting the error:
// Error: Invalid redeclaration of variable "$myArray" as a different type. //

Excuse my noob questions, im new to the mel workflow.

ah-fx
04-18-2009, 12:25 AM
ugh... i saw the problem with my filter as soon as i created my post:

$rc <= size($rigidBodies);
should have been
$rc < size($rigidBodies);

(or start my counter as 1)

but my variable question still remains.

davegreenwood
04-19-2009, 12:36 PM
Hi, you have to declare the data type with your variable, and evaluate the assignment with back ticks (like in your first line). I think the data type will be integer in this case, so:

$active = getAttr($rigidBodies[$rc] + ".active");

becomes:

int $active = `getAttr($rigidBodies[$rc] + ".active")`;

Dave.

ah-fx
04-20-2009, 05:20 AM
Dave,

Thank you for the reply, can you explain the back ticks a little bit to me? I understand that in the case: `-ls -type rigidBody` the back ticks give me a return value. But what is the difference between

getAttr($rigidBodies[$rc] + ".active");
and
`getAttr($rigidBodies[$rc] + ".active")`;

?

claydough
04-20-2009, 05:29 AM
Also how can I clear variables I have declared?
If im working in the script editor and I accidentally declare a variable the wrong type,
ie: int $myArray; instead of int $myArray[];
I would like to be able to rerun the script w/o getting the error:
// Error: Invalid redeclaration of variable "$myArray" as a different type. //

Excuse my noob questions, im new to the mel workflow.

when testing code in the script editor always put yer code no matter how small

in {}
like
{
string $smallCode = `print "this variable can be redeclared at will cause I am in dis here block!\n"`;
eval $smallCode;
}

claydough
04-20-2009, 05:43 AM
Dave,

Thank you for the reply, can you explain the back ticks a little bit to me? I understand that in the case: `-ls -type rigidBody` the back ticks give me a return value. But what is the difference between

getAttr($rigidBodies[$rc] + ".active");
and
`getAttr($rigidBodies[$rc] + ".active")`;

?


there is none in this case
There are some exceptions to query... thankfully!

{

string $rigidBodies[] = `ls -type rigidBody`;

string $aRigidBodies[];

int $active;

if (size($rigidBodies) > 0) {

for ($rc = 0; $rc <= size($rigidBodies); $rc++) {



int $active = getAttr($rigidBodies[$rc] + ".active");

if ($active) {

$aRigidBodies[$rc] = $rigidBodies[$rc];

}

}



print($aRigidBodies);

}

}


will work just fine
as will
size( $bigArray )
and
objExists "myObject"
which will allow you to overload queries at times if u know the cheats
like

string $aVertSeltion[] = `ls -sl -fl`;
if ( size ( `filterExpand -sm 31 $aVertSeltion[size($aVertSeltion) -1 ]` ) == 1 )
print " Then I jes made 3 queries by only using 1 backtick block \n" ;

which normally would be impossible because buried backticks are illeagal

ah-fx
04-20-2009, 05:51 PM
Thanks for all the info roger, very useful.

Adam

CGTalk Moderation
04-20-2009, 05:51 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.