Search code examples
maxscript

Looping through Selection And attaching Child Objects Together


(

on execute do
(

    local AttachableObjectArray = #()
    local SelectedNodes = getCurrentSelection()
    local Idx = 1





            for object in SelectedNodes[Idx].children do 
            (                   
                append AttachableObjectArray object
            )       

                for i = 1 to AttachableObjectArray.count do
                    (           
                        polyop.attach attachableobjectarray[1] attachableobjectarray[i]

                        for j = 1 to AttachableObjectArray.count do
                        (
                            if attachableobjectarray.count <= j do
                            (
                                deselect SelectedNodes[Idx]

                            )
                        )

                    )
                    SelectedNodes = getCurrentSelection()
                    print SelectedNodes.count

)

This Is My Current Script It Works But only works on one hierarchy at a time i need it Loop thru my selections till all parents children are attached to each other

problem: Parent----Child-Child-Child Parent-----Child-Child-Child current script: Parent----Children-Attached Parent-----Child-Child-Child

by parent its a dummy with boxes linked to the dummy

Thanks In Advance


Solution

  • for obj in $/selection/* do -- use just the top-level nodes
    (
        local children = join #() obj
        local childrenToAttach = for i = 2 to children.count where canConvertTo children[i] Mesh collect children[i]
        if childrenToAttach.count > 1 do convertToPoly childrenToAttach[1]
        for i = 2 to childrenToAttach.count do polyop.attach childrenToAttach[1] childrenToAttach[i]
    )