Search code examples
3dsmaxmaxscript

3ds maxscript group objects according to name and name + suffix


macroScript Grouper category: "MaxScript==Shit"
(
on isEnabled return
 selection.count > 0 

on execute do
    (
        createDialog (
                            rollout mf_main "LOD Grouper"
                            (


                                button savebtn "Group Proper LODs"
                                on savebtn pressed do
                                (


                                    max_count = 2   
                                    lodlist = #()




                                        for index in 1 to $.count do
                                        (


                                            if($[1].name == $[index].name + "_lod1")
                                            then(append lodlist $[1])
                                            else()

                                            if($[1].name == $[index].name + "_lod1")
                                            then(append lodlist $[index])
                                            else(reset)


                                            print lodlist

                                        )




                                        lodgroup = group lodlist 
                                        select lodgroup


                                )


                            )
                         )
    )




)

This is my script it is doing what I want by checking names in the selection and comparing them to see which match based on prefix and suffix but its only doing it to one the objects in my selection instead of looping through my selection array

example of what Im trying to make the script do

objects name box01, box01_lod1 / box02, box02_lod1 / box03 , box03_lod1

               group-1              group-2           group-3

any help is much appreciated

thanks in advance


Solution

  • In your example, the same exact test is executed twice in a row:

    if($[1].name == $[index].name + "_lod1")
    then(append lodlist $[1])
    else()
    
    if($[1].name == $[index].name + "_lod1")
    then(append lodlist $[index])
    else(reset)
    

    I suspect this is a typo and you wanted to test against a prefix then a suffix...

    The code above is comparing with the first item of the selection array. Without knowing what you're selecting, its hard to know if this is indeed the correct way.

    The way I'd approach this is have a name array to test against in order to group your selected nodes. Every time a selected node is not 'matched' in the name array, you add it as a unique array to lod_groups. If it is matched, then it just gets appended to one of the existing lod_groups arrays.

    Note this assumes you will always have a node called 'Box001' in the scene. For instance, 'Lod1_Box001' and 'Box001_Lod2' will not be grouped if you don't have a 'Box001' node in the scene.

    local lod_groups = #() -- 2d array
    local _names = #() -- list of names to test against
    local selected_nodes = getCurrentSelection() -- get selected nodes
    for i = 1 to selected_nodes.count do
    (
        local found = False
        local index = 0
        local node_name = selected_nodes[i].name
        -- test node_name against _names
        for j = 1 to _names.count do
        (
            local n = node_name
            local pattern = "*" + _names[j] + "*"
            -- if the name in the list is greater swap the pattern test, this will cover suffix and prefix
            if _names[j].count > node_name.count then
            (
                n = _names[j]
                pattern = "*" + node_name + "*"
            )
            -- if it matches the current name, then record the j-index
            if (matchpattern n pattern:(pattern)) then
            (
                found = True
                index = j
            )
        )
        -- append to lod_groups based on results
        if found then
        (
            append lod_groups[index] selected_nodes[i]
        )
        else
        (
            -- add this name to the _names list, it most likely is unique
            append _names selected_nodes[i].name
            append lod_groups #(selected_nodes[i])
        )
    )
    -- do your grouping here
    for lod_group in lod_groups do
    (
        group lod_group
    )