RSS FEED

Select every n-th

During the last project I had to deal with quite many different selections in a procedural way. To make my life easier I created a simple tool to handle selecting every n-th element of the current selection. This is the barebone version of the main function.
fn selectEveryNth obj nr:2 sub_obj: =
(
    struct elementSelection (_get,_set)
    local element_selection

    case classOf obj of
    (
        Editable_Poly:
        (
            if sub_obj == unsupplied do
            (
                local sub_object_level = if classOf subObjectLevel != Integer then 1 else subObjectLevel
                sub_obj = #(#vert,#edge,#edge,#poly,#poly)[sub_object_level]
            )
            case sub_obj of
            (
                #vert: element_selection = elementSelection _get:getVertSelection _set:polyOp.setVertSelection
                #edge: element_selection = elementSelection _get:getEdgeSelection _set:polyOp.setEdgeSelection
                #poly: element_selection = elementSelection _get:getFaceSelection _set:polyOp.setFaceSelection
            )
            local element_count = (local original_selection = (element_selection._get obj) as array).count - 1
            local new_selection = for i = 1 to element_count by nr collect original_selection[i]
            element_selection._set obj new_selection
            completeRedraw()    
        )
        SplineShape:
        (
            if sub_obj == unsupplied do
            (
                local sub_object_level = if classOf subObjectLevel != Integer then 1 else subObjectLevel
                sub_obj = #(#vert,#edge,#edge)[sub_object_level]
            )
            case sub_obj of
            (
                #vert: element_selection = elementSelection _get:getKnotSelection _set:setKnotSelection
                #edge: element_selection = elementSelection _get:getSegSelection _set:setSegSelection
            )
            local original_selection = element_selection._get obj 1 --for the sake of simplicity works on 1-segment splines
                                                                    --feel free to collect multiple arrays for each segment
                                                                    --if you for some reason need it
            local element_count = original_selection.count - 1
            local new_selection = for i = 1 to element_count by nr collect original_selection[i]
            element_selection._set obj 1 new_selection
            completeRedraw()    
        )
    )
)
USAGE: evaluate and supply the function with a valid node (editable spline or editable poly)with elements you want to filter out already selected, like so:

selectEveryNth $

The optional parameters nr and sub_obj allow to choose every which element to select (default results in selecting every 2nd) and what element it should be (#vert, #edge and in case of editable poly also #poly):

selectEveryNth $ nr:3 sub_obj:#poly --select every 3rd polygon

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!

Return to top