Detach elements by polycount


#1

Hello guys.

I have a client who takes a bunch of my models, change their positions and send me back in .STL.
The problem is: I need them as separate objects, the .STL merge all of them in a single object.
I tried for 2 weeks to code a script that detaches them by poly count (and name them properly) with no success.
Can someone please give a hand?
Thank you all in advance.


#2

Maybe something like this?

minMaxFaces = #(1,1000) --set the minimum and maximum number of faces for elements to be detatched.
groupByFaceCount = false
mainMesh = selection[1]
modClass = classOf (modPanel.getCurrentObject())
if (modClass == Editable_Poly) do (
    minMaxFaces = #(1,1000)
    mainMesh = selection[1]
    loopingDone = false
    for curLoop=1 to 1000 while loopingDone == false do (
        mainMesh.SetSelection #Face #{} --deselect all faces
        allFaces = (-(mainMesh.getSelection #Face)) as array
        checkedFacs = #()
        viableElements = #()
        for fac in allFaces while checkedFacs.count < allFaces do ( --viableElements.count == 0 do (
            if (finditem checkedFacs fac == 0) do (
                mainMesh.SetSelection #Face #{fac}
                mainMesh.ConvertSelection #Face #Element
                elemFacs = mainMesh.getSelection #Face as array
                if (elemFacs.count >= minMaxFaces[1]) and (elemFacs.count <= minMaxFaces[2]) then ( append viableElements elemFacs ; join checkedFacs elemFacs )
                else join checkedFacs elemFacs
            )
        )
        if (viableElements.count != 0) and (viableElements.count != allFaces.count) then (
            --Of the viable elements check if any others have same face count
            if groupByFaceCount == true and viableElements.count >= 2 then (
                elementsToDetatchFLAT = #()
                for i=2 to viableElements.count do (
                    if viableElements[i].count == viableElements[1].count do join elementsToDetatchFLAT viableElements[i]
                )
                polyop.detachFaces mainMesh elementsToDetatchFLAT asNode:true delete:true --name:""
            )
            else (polyop.detachFaces mainMesh viableElements[1] asNode:true delete:true --name:"")
        )
        else loopingDone = true
    )
)

Just put the minimum and maximum number of faces in the first array for the elements you want detatched as new meshs. Set groupByFaceCount to true if you want to detatch all elements that match each others face count together as one mesh. Can’t help you with naming the new meshs though without knowing the specifics about how you want them named.

You could also flag all the elements faces you don’t want detached right at the start in order to make it run a bit faster.