Home Coding, Scripting, Shaders

Selection by name and ignore non-existing MEL in Maya

I will start off with I am just starting with MEL and Python and I am sorry if my code looks basic. I am trying to understand how MEL works beyond the regular commands that we get from Maya and I would appreciate everyone's help to increase my knowledge beyond this point.

I am having problems with a selection script I want to use for various Maya scenes.

I want to select and delete every item on the scene except for two common denominators. Right now, I am listing all the possible items someone can found on each scene and delete them. For example on scene A I have 10 objects, on scene B I have 7 objects, and on scene C I have 5 objects. I want to delete every object expect for 2 objects that are found on all three scene. So this is what I currently have written.

// Select and delete unnecessary objects and groups in the scene

select -r pSphere1 ;

select -add pCube1 ;

select -add pTorus3 ;

select -add Plane ;

select -add Camera ;

doDelete;

This may work on scene A where all those objects are present but on scene B and C some objects are missing. For example the object "pTorus3" is missing on scene B and C and when I run the script I get the following error:

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

Please let me know if someone has a better idea on how to approach something like this. Thank you all for your help.

Replies

  • sprunghunt
    Offline / Send Message
    sprunghunt interpolator


    you can do it in a loop over all the objects. Here's an example:

     //this selects all the objects in the scene you're allowed to delete
    select -ado;
     //this makes a list of the selected objects
    string $mySelection[] = `ls -selection`;
    //now we loop through the selection 
    for ($obj in $mySelection)
    {
       if ($obj == "pCube1")
       {
           //this skips the object called "pCube1"
           print("box skipped");
    
       } else {
           //this deletes anything else
           delete $obj;
       }
    }
    

    something to note is you can't delete certain objects - like the default cameras - so in the script example I've made sure you're only deleting objects you are allowed to.

  • Deadly Nightshade
    Offline / Send Message
    Deadly Nightshade polycounter lvl 9

    Things like doDelete should be avoided (there are exceptions but I will not go into details here). It is not a MEL-command but a function call to some MEL script, meaning it's better that you learn what said script does (hint: many scripts do a lot of things you can live withouit).

    You can find out what script by using the whatIs -command, like this:

    whatIs doDelete

    Once you know the command, look it up in the documentation:

    https://help.autodesk.com/view/MAYAUL/2022/ENU/?guid=__Commands_index_html

    TLDR: whatIs is a life saver when learning MEL and should be used from day 1.

Sign In or Register to comment.