Size Selection Script

General / 06 December 2018

This is a script which selects objects below a certain size in maya. I made it to answer someone's question on polycount but it seemed pretty useful so I thought I'd blog about it. Here's it in action:

And this is the code you can save to your scripts folder : 

//use following command in a button:
//source getSmallObjects.mel; GetSmallObjectsUI();

//UI function
global proc GetSmallObjectsUI(){
    
    if ( ` window -exists GetSmallObjectsWindow`){
        deleteUI GetSmallObjectsWindow;
    }
    
    window -t "Select Small Objects" GetSmallObjectsWindow;
    columnLayout -adj true -rs 8 smallObjColumnMain;
    floatSliderGrp -label "size X" -field true sizeXSlider;
    floatSliderGrp -label "size Y" -field true sizeYSlider;
    floatSliderGrp -label "size Z" -field true sizeZSlider;

    button -l "select objects" -command "GetSmallObjectsPress()";
    showWindow GetSmallObjectsWindow; 

}

//function wrapper for the UI
global proc GetSmallObjectsPress(){

    float $xLimit = ` floatSliderGrp -q -v sizeXSlider`;
    float $yLimit = ` floatSliderGrp -q -v sizeYSlider`;
    float $zLimit = ` floatSliderGrp -q -v sizeZSlider`;

    GetSmallObjects( $xLimit, $yLimit, $zLimit );

}

//main function
global proc GetSmallObjects(    float $xLimit, 
                                float $yLimit, 
                                float $zLimit ){
    
    string $sceneObjs[] = ` ls -l `;
    select -cl;
    
    for ($obj in $sceneObjs){
        float $bbox[] = ` exactWorldBoundingBox $obj`;
        
        float $xSize = $bbox[3] - $bbox[0];
        float $ySize = $bbox[4] - $bbox[1];
        float $zSize = $bbox[5] - $bbox[2];
        
        if (( $xSize < $xLimit) && ( $zSize < $zLimit) && ( $zSize < $zLimit)){
            
            select -add $obj;
        }
   }
}