Select Random Elements by Percentage

This little script allows you to randomly select a number (given by percent) of elements in a mesh. Works both on editable poly and editable mesh objects. Note that for certain usage scenarios, applying MaterialByElement modifier and using the ID percentages generated by that might be faster.

try destroyDialog selectRandElements catch()
rollout selectRandElements "Select Random Elements" width:205
    label lblPercentage "Percent of total:" across:3 offset:[0,3]
    spinner spnPercentage width:50 scale:1 range:[0,100,10.] offset:[0,2]
    button btnSelect "SELECT" height:22 offset:[12,0]

    fn randomizeArray arr =
        local arrayLength = arr.count

        for i = 1 to arrayLength do
            local index = random i arrayLength
            swap arr[i] arr[index]

    on btnSelect pressed do
        if selection.count > 0 AND (isKindOf (obj = selection[1]) Editable_Poly OR isKindOf obj Editable_Mesh) do
            if heapSize < 102400000 do heapSize = 102400000

            local faces = #{}
            local obj_faces = obj.faces as bitArray
            local obj_faces_arr = obj_faces as array
            local obj_faces_count = obj_faces_arr.count
            local op = if isKindOf obj Editable_Poly then polyOp else meshOp
            local getElement = op.getElementsUsingFace
            local step = 100./spnPercentage.value

            local elements = for i in obj_faces where obj_faces[i] == true collect
                local element = getElement obj i
                obj_faces -= element

            randomizeArray elements
            local elements_count = elements.count

            for i = 1 to elements_count by step do
                faces += elements[i]

            obj.selectedFaces = faces
            max modify mode
            subObjectLevel = 4
createDialog selectRandElements

USAGE: Run the script, enter desired value (in percent), select a mesh and press the SELECT button.

Randomize UVW UI

DISCLAIMER: All scripts and snippets are provided as is under Creative Commons Zero (public domain, no restrictions) license. The author and this blog cannot be held liable for any loss caused as a result of inaccuracy or error within these web pages. Use at your own risk.

This Post needs Your Comment!

This comment has been removed by a blog administrator.
Return to top