MEL scripting - Select and Delete all objects except for two common denominators


#1

I am trying to teach myself how to script with Maya. I am starting with MEL and I am having problems with a more complex situation.

I am trying to select all objects and delete them from the scene except for two objects that can be found on every scene. Those two objects can be found together on the same scene or individually.

For example

Scene A has 10 objects, scene B has 8, scene C has 6. Scene A contains the two “desireObjects”, scene B and C contains only one of the “desireObjects.”

Right now I have something very basic as:

string $selection[] = {“desireObject1”, “desireObject2”}

select -all ;

for ($selected in $selection){
select -d desireObject1;
select -d desireObject2;
}

doDelete;

On scene A where both objects can be found works perfectly. The script selects all the objects, deselect “desireObject1”, and “desireObject2” and deletes the rest. However on scene B and C, where only one of the two “desireObjects” can be found I get the following error:

// Error: line 3: No object matches name: desireObject2 //

So the scrip stops and it doesn’t execute the rest of the commands.

I am trying to work on a if{} statement but I would like to get help from the community to try to understand this process. I would really appreciate everyone’s help. Thank you!


#2

Try something like:

for ($selected in $selection){
if ($selected.name == desireObject1){
–Do nothing if true
}else if ($selected.name == desireObject2){
–Do nothing if true
}else{
doDelete;
}

Obviously put in the correct names and commands for what you are doing, but this is the basic idea.